为GD::SVG添加newFromSVG

想要在已存在的 SVG 基础上做图,所以需要添加函数 newFromSVG()

这里采用模块 SVG::Parser 来将 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;
}

测试了一下,可以成功执行。

转载于:https://my.oschina.net/u/727594/blog/785497

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值