想要在已存在的 SVG 基礎上作圖,因此須要添加函數 newFromSVG()
。less
這裏採用模塊 SVG::Parser
來將 SVG 文件中內容讀進來,成爲一個 svg 對象。svg
示例代碼:函數
#!/usr/bin/perl # by lhtk : lhtk80t7@gmail.com # use strict; use warnings; use GD::SVG; use SVG::Parser; use autodie qw/open close/; # input, output my $svg_i = 'input.svg'; my $svg_o = 'tmp.svg'; my $im = GD::SVG::Image->newFromSVG(2000,2000,$svg_i); # 直接從 SVG 文件來構建 # ========================================= # 一些什麼亂七八糟的代碼………… # ========================================= # 輸出 my $fh_svg_o; open $fh_svg_o, '>', $svg_o; #print SVGO $svg->xmlify; print $fh_svg_o $im->svg(); close $fh_svg_o; # ========================================= # subroutines # ========================================= package GD::SVG::Image; # 對 GD::SVG::Image::new 進行修改獲得 # 簡單改了下 sub newFromSVG { my ($self,$width,$height,$svg_i,$debug) = @_; my $this = bless {},$self; # load SVG in open my $fh, '<', $svg_i; my $xml; { local $/ = undef; $xml = <$fh>; } close $fh; my $parser = new SVG::Parser(-debug => 0); my $img = $parser->parse($xml); $this->{img} = [$img]; $this->{width} = $width; $this->{height} = $height; # Let's create an internal representation of the image in GD # so that I can easily use some of GD's methods ###GD###$this->{gd} = GD::Image->new($width,$height); # Let's just assume that we always want the foreground color to be # black This, for the most part, works for Bio::Graphics. This # certainly needs to be fixed... $this->{foreground} = $this->colorAllocate(0,0,0); $this->{debug} = ($debug) ? $debug : GD::SVG::DEBUG; return $this; }
測試了一下,能夠成功執行。測試