File Coverage

blib/lib/SVGPDF/Image.pm
Criterion Covered Total %
statement 62 72 86.1
branch 22 30 73.3
condition 9 19 47.3
subroutine 6 6 100.0
pod 0 1 0.0
total 99 128 77.3


line stmt bran cond sub pod time code
1             #! perl
2              
3 2     2   869 use v5.26;
  2         7  
4 2     2   30 use Object::Pad;
  2         4  
  2         13  
5 2     2   224 use utf8;
  2         4  
  2         12  
6 2     2   106 use Carp;
  2         4  
  2         546  
7              
8             class SVGPDF::Image :isa(SVGPDF::Element);
9              
10 18     18 0 37 method process () {
  18         53  
  18         36  
11 18         88 my $atts = $self->atts;
12 18         78 my $xo = $self->xo;
13 18 50       73 return if $atts->{omit}; # for testing/debugging.
14              
15 18         101 my ( $x, $y, $w, $h, $link, $tf ) =
16             $self->get_params( $atts, qw( x:H y:V width:H height:V href:! transform:s ) );
17              
18 18   100     68 $x ||= 0; $y ||= 0;
  18   50     89  
19 18   50     56 $w ||= 0; $h ||= 0;
  18   50     46  
20              
21 18 50 33     73 unless ( $w && $h ) {
22 0         0 $self->_dbg( $self->name, " x=$x y=$y w=$w h=$h **skipped**" );
23 0         0 return;
24             }
25              
26 18         72 $self->_dbg( $self->name, " x=$x y=$y w=$w h=$h" );
27              
28 18         41 my $img;
29 18 100 66     600 if ( $link =~ /^data:/ ) {
    100 33        
    50          
30             # In-line image asset.
31 8         55 my $info = $self->data_inline($link);
32 8 50       39 if ( $info->{error} ) {
33 0         0 warn( "SVG: ", $info->{error}, "\n" );
34 0         0 $self->css_pop, return;
35             }
36              
37 8         22 my $mimetype = $info->{mimetype};
38 8         18 my $subtype = $info->{subtype};
39              
40 8 50       36 unless ( $mimetype eq "image" ) {
41 0         0 warn("SVG: Unhandled mime type \"mimetype/$subtype\" in inline image\n");
42 0         0 $self->css_pop, return;
43             }
44 8         72 my $data = $info->{data};
45              
46             # Get info.
47 8         809 require Image::Info;
48 8         2753 $info = Image::Info::image_info(\$data);
49 8 50       29812 if ( $info->{error} ) {
50 0         0 warn($info->{error});
51 0         0 $self->css_pop, return;
52             }
53              
54 8         50 my $format = $info->{file_ext};
55 8 100       44 $format = "jpeg" if $format eq "jpg";
56 8 100       48 $format = "pnm" if $format =~ /^p[bgp]m$/;
57 8 50       32 $format = "pnm" if $format =~ /^x[bp]m$/; # bonus
58 8 100       32 $format = "tiff" if $format eq "tif";
59              
60             # Make the image. Silence missing library warnings.
61 8         15 my $fh;
62             # Also, do not use the fast IPL module, it cannot read from scalar.
63 8 100       25 if ( $format eq "tiff" ) {
64             # TIFF can't read from scalar file handle.
65 2     2   3625 use File::Temp;
  2         30144  
  2         2175  
66 1         10 ( my $fd, $fh ) = File::Temp::tempfile( UNLINK => 1 );
67 1         1335 binmode $fd => ':raw';
68 1         68 print $fd $data;
69 1         27 close($fd);
70             # Yes, trickery... $fh is now a file name, not a handle.
71             }
72             else {
73 7         99 open( $fh, '<:raw', \$data );
74             }
75 8         63 $img = $self->root->pdf->image( $fh, format => $format,
76             silent => 1, nouseIPL => 1 );
77             }
78             elsif ( $link =~ m!^.+\.(png|jpe?g|gif)$!i && -s $link ) {
79             # Autodetected. Make the image.
80 5         23 $img = $self->root->pdf->image( $link, silent => 1 );
81             }
82             elsif ( $link =~ m!^.+\.(tiff?|p[bgp]m|x[bp]m)$!i && -s $link ) {
83             # Not autodetected, need format.
84             # Note that xbm and xpm are bonus.
85 5 100       32 my $format = $1 =~ /tif/i ? "tiff" : "pnm";
86             # Make the image.
87 5         53 $img = $self->root->pdf->image( $link, format => $format, silent => 1 );
88             }
89             else {
90 0   0     0 warn("SVG: Unhandled or missing image link: ",
91             "\"$link\""//"", "\n");
92 0         0 return;
93             }
94              
95 18         261321 $self->_dbg( "xo save" );
96 18         292 $xo->save;
97              
98             # Place the image.
99 18 50       1505 $self->set_transform($tf) if $tf;
100 18         170 $xo->transform( translate => [ $x, $y+$h ] );
101 18         8833 $xo->image( $img, 0, 0, $w, -$h );
102              
103 18         4990 $self->_dbg( "xo restore" );
104 18         95 $xo->restore;
105 18         709 $self->css_pop;
106             }
107              
108              
109             1;