File Coverage

blib/lib/SVG/Fill.pm
Criterion Covered Total %
statement 61 68 89.7
branch 18 26 69.2
condition n/a
subroutine 13 14 92.8
pod 0 7 0.0
total 92 115 80.0


line stmt bran cond sub pod time code
1             package SVG::Fill;
2 6     6   3362 use 5.008001;
  6         17  
  6         190  
3 6     6   25 use strict;
  6         7  
  6         183  
4 6     6   31 use warnings;
  6         7  
  6         184  
5              
6 6     6   2974 use Mojo::DOM;
  6         374189  
  6         228  
7 6     6   2988 use Path::Class;
  6         170655  
  6         376  
8 6     6   3331 use URI;
  6         22611  
  6         4155  
9              
10             our $VERSION = "0.08";
11              
12             sub new {
13              
14 5     5 0 3448 my ( $package, $filename ) = @_;
15              
16 5         10 my $self = bless {};
17              
18 5         21 my $file = file($filename);
19 5         1276 my $content = $file->slurp( iomode => '<:encoding(UTF-8)' );
20              
21 5         106112 my $dom = Mojo::DOM->new($content);
22              
23 5         172188 $self->{_dom} = $dom;
24 5         22 $self->{_file} = $file;
25              
26 5         24 return $self;
27              
28             }
29              
30             sub convert {
31              
32 0     0 0 0 my ( $self, $filename, $format ) = @_;
33              
34 0         0 die "Coming soon";
35              
36             }
37              
38             sub find_elements {
39              
40 2     2 0 2085 my ( $self, $id ) = @_;
41              
42 2         4 my $dom = $self->{_dom};
43 2         17 return [ $dom->find( sprintf( 'image[id*="%s"],text[id*="%s"]', $id, $id ) )->each ];
44             }
45              
46             sub fill_text {
47              
48 1     1 0 717 my ( $self, $id, $text ) = @_;
49              
50 1 50       8 $id = "#" . $id, unless $id =~ /^#/;
51              
52 1         3 my $dom = $self->{_dom};
53              
54 1 50       4 if ( my $element = $dom->at($id) ) {
55              
56 1 50       718 if ( $element->tag eq 'text' ) {
57              
58 1         33 $element->content($text);
59              
60             } else {
61              
62 0         0 warn "$id is a <" . $element->tag . ">, please use only ";
63             }
64              
65             } else {
66              
67 0         0 warn "Could not found $id";
68              
69             }
70             }
71              
72             sub fill_image {
73              
74 4     4 0 35273 my ( $self, $id, $image ) = @_;
75              
76 4 50       48 $id = "#" . $id, unless $id =~ /^#/;
77              
78 4         15 my $dom = $self->{_dom};
79              
80 4 50       24 if ( my $element = $dom->at($id) ) {
81              
82 4 50       2705 if ( $element->tag eq 'image' ) {
83              
84 4 50       210 if ( -e $image ) {
85              
86 4         30 my $u = URI->new('data:');
87              
88 4 100       1627 $u->media_type('image/png') if $image =~ /png$/;
89 4 100       175 $u->media_type('image/svg+xml') if $image =~ /svg$/;
90 4 100       70 $u->media_type('image/jpeg') if $image =~ /jpg$/;
91 4 100       55 $u->media_type('image/gif') if $image =~ /gif$/;
92 4         71 my $content = file($image)->slurp;
93 4         1847 $u->data($content);
94 4         18385 $element->attr( 'xlink:href', $u->as_string );
95              
96             } else {
97              
98 0         0 warn "could not find $image";
99             }
100              
101             } else {
102              
103 0         0 warn "$id is a <" . $element->tag . ">, please use only for fill_image";
104              
105             }
106              
107             } else {
108              
109 0         0 warn "Could not find $id";
110             }
111              
112             }
113              
114             sub save {
115              
116 6     6 0 827 my ( $self, $filename ) = @_;
117 6         31 my $content = $self->{_dom}->to_string;
118 6 50       47512 defined $filename
119             ? file($filename)->spew( iomode => '>:encoding(UTF-8)', $content )
120             : $self->{_file}->spew( iomode => '>:encoding(UTF-8)', $content );
121             }
122              
123             sub font_fix {
124              
125 1     1 0 669 my $self = shift;
126              
127 1         3 my $dom = $self->{_dom};
128              
129             $dom->find('text')->map(
130             sub {
131              
132 3     3   774 my $element = $_;
133              
134 3 100       8 if ( my $font = $element->attr('font-family') ) {
135              
136             # Fix '' escaping by Illustrator
137 1         32 $font =~ s/^'//g;
138 1         5 $font =~ s/'$//g;
139              
140             # Remove MT-Variant
141 1         9 $font =~ s/MT//g;
142            
143 1         4 $element->attr( 'font-family' => $font );
144             }
145              
146             }
147 1         5 );
148             }
149              
150             1;
151             __END__