File Coverage

blib/lib/SVG/Fill.pm
Criterion Covered Total %
statement 52 61 85.2
branch 16 24 66.6
condition n/a
subroutine 11 13 84.6
pod 0 7 0.0
total 79 105 75.2


line stmt bran cond sub pod time code
1             package SVG::Fill;
2 5     5   2852 use 5.008001;
  5         14  
  5         178  
3 5     5   40 use strict;
  5         7  
  5         159  
4 5     5   29 use warnings;
  5         9  
  5         147  
5              
6 5     5   2440 use Mojo::DOM;
  5         314455  
  5         199  
7 5     5   2441 use Path::Class;
  5         141421  
  5         295  
8 5     5   2661 use URI;
  5         17373  
  5         2758  
9              
10             our $VERSION = "0.07";
11              
12             sub new {
13              
14 4     4 0 1584 my ( $package, $filename ) = @_;
15              
16 4         8 my $self = bless {};
17              
18 4         15 my $file = file($filename);
19 4         929 my $content = $file->slurp( iomode => '<:encoding(UTF-8)' );
20              
21 4         67619 my $dom = Mojo::DOM->new($content);
22              
23 4         126271 $self->{_dom} = $dom;
24 4         14 $self->{_file} = $file;
25              
26 4         22 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 1883 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 380 my ( $self, $id, $text ) = @_;
49              
50 1 50       7 $id = "#" . $id, unless $id =~ /^#/;
51              
52 1         2 my $dom = $self->{_dom};
53              
54 1 50       4 if ( my $element = $dom->at($id) ) {
55              
56 1 50       612 if ( $element->tag eq 'text' ) {
57              
58 1         31 $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 32463 my ( $self, $id, $image ) = @_;
75              
76 4 50       38 $id = "#" . $id, unless $id =~ /^#/;
77              
78 4         9 my $dom = $self->{_dom};
79              
80 4 50       20 if ( my $element = $dom->at($id) ) {
81              
82 4 50       2355 if ( $element->tag eq 'image' ) {
83              
84 4 50       194 if ( -e $image ) {
85              
86 4         26 my $u = URI->new('data:');
87              
88 4 100       1599 $u->media_type('image/png') if $image =~ /png$/;
89 4 100       149 $u->media_type('image/svg+xml') if $image =~ /svg$/;
90 4 100       54 $u->media_type('image/jpeg') if $image =~ /jpg$/;
91 4 100       96 $u->media_type('image/gif') if $image =~ /gif$/;
92 4         65 my $content = file($image)->slurp;
93 4         1693 $u->data($content);
94 4         16972 $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 5     5 0 546 my ( $self, $filename ) = @_;
117 5         20 my $content = $self->{_dom}->to_string;
118 5 50       27591 defined $filename
119             ? file($filename)->spew( iomode => '>:encoding(UTF-8)', $content )
120             : $self->{_file}->spew( iomode => '>:encoding(UTF-8)', $content );
121             }
122              
123             sub fix_fonts {
124              
125 0     0 0   my $self = @_;
126              
127 0           my $dom = $self->{_dom};
128              
129             }
130              
131             1;
132             __END__