line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package SVG::Fill; |
2
|
6
|
|
|
6
|
|
3120
|
use 5.008001; |
|
6
|
|
|
|
|
18
|
|
|
6
|
|
|
|
|
186
|
|
3
|
6
|
|
|
6
|
|
22
|
use strict; |
|
6
|
|
|
|
|
8
|
|
|
6
|
|
|
|
|
181
|
|
4
|
6
|
|
|
6
|
|
31
|
use warnings; |
|
6
|
|
|
|
|
9
|
|
|
6
|
|
|
|
|
168
|
|
5
|
|
|
|
|
|
|
|
6
|
6
|
|
|
6
|
|
2892
|
use Mojo::DOM; |
|
6
|
|
|
|
|
358849
|
|
|
6
|
|
|
|
|
216
|
|
7
|
6
|
|
|
6
|
|
2819
|
use Path::Class; |
|
6
|
|
|
|
|
168556
|
|
|
6
|
|
|
|
|
404
|
|
8
|
6
|
|
|
6
|
|
3263
|
use URI; |
|
6
|
|
|
|
|
21142
|
|
|
6
|
|
|
|
|
3697
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = "0.09"; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
|
|
|
|
|
|
|
14
|
5
|
|
|
5
|
0
|
2396
|
my ( $package, $filename ) = @_; |
15
|
|
|
|
|
|
|
|
16
|
5
|
|
|
|
|
11
|
my $self = bless {}; |
17
|
|
|
|
|
|
|
|
18
|
5
|
|
|
|
|
22
|
my $file = file($filename); |
19
|
5
|
|
|
|
|
1270
|
my $content = $file->slurp( iomode => '<:encoding(UTF-8)' ); |
20
|
|
|
|
|
|
|
|
21
|
5
|
|
|
|
|
87950
|
my $dom = Mojo::DOM->new($content); |
22
|
|
|
|
|
|
|
|
23
|
5
|
|
|
|
|
165967
|
$self->{_dom} = $dom; |
24
|
5
|
|
|
|
|
24
|
$self->{_file} = $file; |
25
|
|
|
|
|
|
|
|
26
|
5
|
|
|
|
|
27
|
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
|
1572
|
my ( $self, $id ) = @_; |
41
|
|
|
|
|
|
|
|
42
|
2
|
|
|
|
|
5
|
my $dom = $self->{_dom}; |
43
|
2
|
|
|
|
|
19
|
return [ $dom->find( sprintf( 'image[id*="%s"],text[id*="%s"]', $id, $id ) )->each ]; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub fill_text { |
47
|
|
|
|
|
|
|
|
48
|
1
|
|
|
1
|
0
|
436
|
my ( $self, $id, $text ) = @_; |
49
|
|
|
|
|
|
|
|
50
|
1
|
50
|
|
|
|
7
|
$id = "#" . $id, unless $id =~ /^#/; |
51
|
|
|
|
|
|
|
|
52
|
1
|
|
|
|
|
3
|
my $dom = $self->{_dom}; |
53
|
|
|
|
|
|
|
|
54
|
1
|
50
|
|
|
|
5
|
if ( my $element = $dom->at($id) ) { |
55
|
|
|
|
|
|
|
|
56
|
1
|
50
|
|
|
|
920
|
if ( $element->tag eq 'text' ) { |
57
|
|
|
|
|
|
|
|
58
|
1
|
|
|
|
|
53
|
$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
|
360447
|
my ( $self, $id, $image ) = @_; |
75
|
|
|
|
|
|
|
|
76
|
4
|
50
|
|
|
|
35
|
$id = "#" . $id, unless $id =~ /^#/; |
77
|
|
|
|
|
|
|
|
78
|
4
|
|
|
|
|
12
|
my $dom = $self->{_dom}; |
79
|
|
|
|
|
|
|
|
80
|
4
|
50
|
|
|
|
21
|
if ( my $element = $dom->at($id) ) { |
81
|
|
|
|
|
|
|
|
82
|
4
|
50
|
|
|
|
2565
|
if ( $element->tag eq 'image' ) { |
83
|
|
|
|
|
|
|
|
84
|
4
|
50
|
|
|
|
204
|
if ( -e $image ) { |
85
|
|
|
|
|
|
|
|
86
|
4
|
|
|
|
|
26
|
my $u = URI->new('data:'); |
87
|
|
|
|
|
|
|
|
88
|
4
|
100
|
|
|
|
1582
|
$u->media_type('image/png') if $image =~ /png$/; |
89
|
4
|
100
|
|
|
|
174
|
$u->media_type('image/svg+xml') if $image =~ /svg$/; |
90
|
4
|
100
|
|
|
|
70
|
$u->media_type('image/jpeg') if $image =~ /jpg$/; |
91
|
4
|
100
|
|
|
|
81
|
$u->media_type('image/gif') if $image =~ /gif$/; |
92
|
4
|
|
|
|
|
77
|
my $content = file($image)->slurp; |
93
|
4
|
|
|
|
|
1776
|
$u->data($content); |
94
|
4
|
|
|
|
|
18392
|
$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
|
723
|
my ( $self, $filename ) = @_; |
117
|
6
|
|
|
|
|
28
|
my $content = $self->{_dom}->to_string; |
118
|
6
|
50
|
|
|
|
46194
|
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
|
387
|
my $self = shift; |
126
|
|
|
|
|
|
|
|
127
|
1
|
|
|
|
|
3
|
my $dom = $self->{_dom}; |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
$dom->find('text')->map( |
130
|
|
|
|
|
|
|
sub { |
131
|
|
|
|
|
|
|
|
132
|
3
|
|
|
3
|
|
745
|
my $element = $_; |
133
|
|
|
|
|
|
|
|
134
|
3
|
100
|
|
|
|
8
|
if ( my $font = $element->attr('font-family') ) { |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
# Fix '' escaping by Illustrator |
137
|
1
|
|
|
|
|
31
|
$font =~ s/^'//g; |
138
|
1
|
|
|
|
|
11
|
$font =~ s/'$//g; |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
# Remove MT-Variant |
142
|
1
|
|
|
|
|
4
|
$font =~ s/MT//g; |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
# Font-weight as attribute |
145
|
1
|
|
|
|
|
5
|
$font =~ s/\-bold$/:bold/ig; |
146
|
|
|
|
|
|
|
|
147
|
1
|
|
|
|
|
4
|
$element->attr( 'font-family' => $font ); |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
} |
151
|
1
|
|
|
|
|
5
|
); |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
1; |
155
|
|
|
|
|
|
|
__END__ |