Layout/PDFAPI2/ImageElement.pm

filter: Statementstmt 19.1% 33 / 172 Branchbran 0.0% 0 / 78 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 64.2% 9 / 14 Podpod 60.0% 3 / 5 totaltotal 14.7% 45 / 306 CC 51 SCAR 58.6
CC
51
Cov
12%
bbox
702.0
parse
240.0
render
56.0
CRAP 350.2
« Layout/PDFAPI2.pm ↑ Summary Layout.pm »
1 #! perl
2  
3 3 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Object::Pad;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶class Text::Layout::PDFAPI2::ImageElement :does(Text::Layout::ElementRole);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 =head1 NAME
11  
12 Text::Layout::PDFAPI2::ImageElement - <img> element for PDF images
13  
14 =head1 DESCRIPTION
15  
16 This class implements support for an C<< <img> >> element. It can be
17 used to include inline images in marked-up texts.
18  
19 The class provides the three mandatory methods according to the
20 requirements of L<Text::Layout::ElementRole>.
21  
22 =over 4
23  
24 =item parse
25  
26 To parse the C<< <img> >> tag in marked-up text.
27  
28 =item bbox
29  
30 To provide the augmented bounding box for the image.
31  
32 =item render
33  
34 To render the image using one of the PDF::API2 compatible libraries.
35  
36 =back
37  
38 An additional, overridable method getimage() is provided to actually
39 produce the desired image object. See L</"IMAGE PRODUCER">
40  
41 =head1 THE C<< <img> >> ELEMENT
42  
43 <img attributes />
44  
45 Note that the C<< <img> >> element must be self-closed, i.e., end with
46 C<< /> >>.
47  
48 The image is placed at the current location in the text and aligned on
49 the baseline of the text. The image dimensions contribute to the
50 resultant bounding box of the formatted text. See C<dx> and C<dy>
51 below.
52  
53 All attributes are key=value pairs. The value should (but need not) be
54 enclosed in quotes.
55  
56 Dimensional values may be a (fractional) number optionally
57 postfixed by C<em> or C<ex>, or a percentage.
58 A number indicates points.
59 C<em> values are multiplied by the current font size and
60 C<ex> values are multiplied by half the font size.
61  
62 =over 4
63  
64 =item C<src=>I<IMAGE>
65  
66 Provides the source of the image. This can be the filename of a jpg,
67 png or gif image.
68  
69 =item C<width=>I<NNN>
70  
71 The desired width for the image.
72 Dimensional.
73 The image is scaled if necessary.
74  
75 =item C<height=>I<NNN>
76  
77 The desired height for the image.
78 Dimensional.
79 The image is scaled if necessary.
80  
81 =item C<dx=>I<NNN>
82  
83 A horizontal offset for the image, wrt. the current location in the text.
84 Dimensional.
85  
86 =item C<dy=>I<NNN>
87  
88 Same, but vertical. Positive amounts move up.
89  
90 Note the direction is opposite to the Pango C<rise>.
91  
92 =item C<scale=>I<NNN>
93  
94 A scaling factor, to be applied I<after> width/height scaling.
95 The value may be expressed as a percentage.
96  
97 Independent horizontal and vertical scaling can be specified as two
98 comma-separated scale values.
99  
100 =item C<align=>I<XXX>
101  
102 Align the image in the width given by C<w=>I<NNN>.
103  
104 Possible alignments are C<left>, C<center>, and C<right>.
105  
106 =item C<bbox=>I<N>
107  
108 If true, the actual bounding box of an object is used for placement.
109  
110 By default the bounding box is only used to obtain the width and height.
111  
112 This attribute has no effect on image objects.
113  
114 =item C<w=>I<NNN>
115  
116 The advance width of the image.
117 Dimensional.
118 Default advance is the image width plus horizontal offset.
119 This overrides the advance and may be zero.
120  
121 =item C<h=>I<NNN>
122  
123 The advance height of the image.
124 Dimensional.
125 Default advance is the image height plus vertical offset.
126 This overrides the advance and may be zero.
127  
128 =back
129  
130 =head1 CONSTRUCTOR
131  
132 This class is usually instantiated in a Text::Layout register_element call:
133  
134 $layout->register_element
135 ( Text::Layout::PDFAPI2::ImageElement->new( pdf => $pdf ) );
136  
137 =cut
138  
139 3 ▶use constant TYPE => "img";
Subroutine BEGIN: called CC=1 SCAR=0.0
140 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
141  
142 3 ▶use Text::Layout::Utils qw(parse_kv);
Subroutine BEGIN: called CC=1 SCAR=0.0
143  
144 0 ▶field $pdf :param :accessor;
Subroutine pdf: not called CC=2 SCAR=14.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $pdf = shift() 0 0 0
145  
146 3 ▶use constant DEBUG => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
147  
148 0 ▶method parse( $ctx, $k, $v ) {
Subroutine parse: not called CC=15 SCAR=54.8
Pod documented
149  
150 0 my %ctl = ( type => TYPE );
151  
152 # Split the attributes.
153 0 my $attr = parse_kv($v);
154 0 ▶ while ( my ( $k, $v ) = each( %$attr ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$attr) 0 0 0
155  
156 # Ignore case unless required.
157 0 ▶ $v = lc $v unless $k =~ /^(src)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $k =~ /^(src)$/u 0 0 0
158  
159 0 ▶ if ( $k =~ /^(src|bbox)$/ ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($k =~ /^(src|bbox)$/u) { } 0 0 0
elsif ($k eq 'align' and $v =~ /^(left|right|center)$/u) { } 0 0 0
elsif ($k =~ /^(width|height|dx|dy|w|h)$/u) { } 0 0 0
elsif ($k =~ /^(scale)$/u) { } 0 0 0
160 0 $ctl{$k} = $v;
161 }
162 ▶ elsif ( $k eq "align" && $v =~ /^(left|right|center)$/ ) {
Condition0%0/3
$k eq "align" and $v =~ /^(left|right|center)$/u
!ll&&!rl&&r
000
MC/DC0%0/2
$k eq "align" and $v =~ /^(left|right|center)$/u
$k eq "align" $v =~ /^(left|right|center)$/u
Truth table0%0/3
A and B
A= $k eq "align"B= $v =~ /^(left|right|center)$/u
ABresult
0X0
100
111
163 0 $ctl{$k} = $v;
164 }
165 elsif ( $k =~ /^(width|height|dx|dy|w|h)$/ ) {
166 0 ▶ $v = $1 * $ctx->{size} if $v =~ /^(-?[\d.]+)em$/;
Branch: 1 branch
BranchTrueFalseTotal
if $v =~ /^(-?[\d.]+)em$/u 0 0 0
167 0 ▶ $v = $1 * $ctx->{size} / 2 if $v =~ /^(-?[\d.]+)ex$/;
Branch: 1 branch
BranchTrueFalseTotal
if $v =~ /^(-?[\d.]+)ex$/u 0 0 0
168 0 ▶ $v = $1 * $ctx->{size} / 100 if $v =~ /^(-?[\d.]+)\%$/;
Branch: 1 branch
BranchTrueFalseTotal
if $v =~ /^(-?[\d.]+)\%$/u 0 0 0
169 0 $ctl{$k} = $v;
170 }
171 elsif ( $k =~ /^(scale)$/ ) {
172 0 my @s;
173 0 for ( split( /,/, $v ) ) {
174 0 ▶ $_ = $1 / 100 if /^([\d.]+)\%$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^([\d.]+)\%$/u 0 0 0
175 0 push( @s, $_ );
176 }
177 0 ▶ push( @s, $s[0] ) unless @s > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @s > 1 0 0 0
178 0 ▶ carp("Invalid " . TYPE . " attribute: \"$k\" (too many values)\n")
Branch: 1 branch
BranchTrueFalseTotal
unless @s == 2 0 0 0
179 unless @s == 2;
180 0 $ctl{$k} = \@s;
181 }
182 else {
183 0 carp("Invalid " . TYPE . " attribute: \"$k\"\n");
184 }
185 }
186  
187 0 return \%ctl;
188 3 }
189  
190 0 ▶method render( $fragment, $gfx, $x, $y ) {
Subroutine render: not called CC=7 SCAR=40.3
Pod documented
191  
192 0 my $b = $self->bbox($fragment);
193 0 my @bbox = @{$b->{bbox}};
194 0 my @bb = @{$b->{bb}};
195 0 my @abox = @{$b->{abox}};
196  
197 0 my $width = $bb[2] - $bb[0];
198 0 my $height = $bb[3] - $bb[1];
199 0 my $img = $self->getimage($fragment);
200 0 my $is_image = ref($img) =~ /::Image::/;
201 0 my @a;
202  
203 0 ▶ if ( $is_image ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_image) { } 0 0 0
204 0 @a = ( $x + $bb[0], $y + $bb[1], $width, $height );
205 0 warn("IMG x=$a[0], y=$a[1], width=$a[2], height=$a[3]\n") if DEBUG;
206 }
207 else {
208 0 my ( $xscale, $yscale ) = @bb[4,5];
209  
210 0 @a = ( $x + $bb[0],
211 $y + $bb[1] - $yscale*($bbox[1]),
212 $xscale, $yscale );
213 0 ▶ unless ( $fragment->{bbox} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fragment->{"bbox"}) 0 0 0
214 0 $a[0] -= $xscale*($bbox[0]);
215 }
216 0 warn("OBJ x=${x}->$a[0], y=${y}->$a[1], width=$width, height=$height",
217 ( $xscale != 1 || $yscale != 1 )
218 ? ", scale=$xscale" : "",
219 ( $xscale != $yscale )
220 ? ",$yscale" : "", "\n") if DEBUG;
221 }
222  
223 0 $gfx->object( $img, @a );
224  
225 0 ▶ if ( $fragment->{href} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fragment->{"href"}) 0 0 0
226 0 my $ann = $gfx->{' apipage'}->annotation;
227 0 my $target = $fragment->{href};
228  
229 0 ▶ if ( $target =~ /^#(.+)/ ) { # named destination
Branch: 2 branches
BranchTrueFalseTotal
if ($target =~ /^#(.+)/u) { } 0 0 0
elsif ($target =~ /^(?!\w{3,}:)(.*)(\#.+)$/u) { } 0 0 0
230 # Augmented API for apps that keep track of bookmarks.
231 0 my $pdf = $gfx->{' api'};
232 0 ▶ if ( my $c = $pdf->can("named_dest_fiddle") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = $pdf->can("named_dest_fiddle")) 0 0 0
233 0 $target = $pdf->$c($1);
234 }
235  
236 0 $ann->link($target);
237 }
238 # Named destination in other PDF.
239 elsif ( $target =~ /^(?!\w{3,}:)(.*)(\#.+)$/ ) {
240 0 $ann->pdf( $1, $2 );
241 }
242 # Arbitrary document.
243 else {
244 0 $ann->uri($target);
245 }
246 # $ann->border( 0, 0, 1 );
247 0 $ann->rect( $x + $bb[0], $y + $bb[1], $x + $bb[2], $y + $bb[3] );
248 }
249  
250 0 return { abox => \@abox };
251 3 }
252  
253 0 ▶method bbox( $fragment ) {
Subroutine bbox: not called CC=26 SCAR=65.5
Pod documented
254  
255 0 ▶ return $fragment->{_bb} if $fragment->{_bb};
Branch: 1 branch
BranchTrueFalseTotal
if $fragment->{"_bb"} 0 0 0
256  
257 0 my @bbox; # bbox of image or object
258 my @bb; # bbox after scaling/displacement, plus scale factors
259 0 my @abox; # advance box
260  
261 0 my $img = $self->getimage($fragment);
262 0 my $is_image = ref($img) =~ /::Image::/;
263  
264 0 my $img_width;
265 my $img_height;
266 0 ▶ if ( $is_image ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_image) { } 0 0 0
267 0 $img_width = $img->width;
268 0 $img_height = $img->height;
269 0 @bbox = ( 0, 0, $img_width, $img_height );
270 }
271 else {
272 0 @bbox = $img->bbox;
273 0 ▶ @bbox[0,2] = @bbox[2,0] if $bbox[2] < $bbox[0];
Branch: 1 branch
BranchTrueFalseTotal
if $bbox[2] < $bbox[0] 0 0 0
274 0 ▶ @bbox[1,3] = @bbox[3,1] if $bbox[3] < $bbox[1];
Branch: 1 branch
BranchTrueFalseTotal
if $bbox[3] < $bbox[1] 0 0 0
275 0 $img_width = $bbox[2] - $bbox[0];
276 0 $img_height = $bbox[3] - $bbox[1];
277 }
278  
279 # Target width and height.
280 0 ▶ my $width = $fragment->{width} || $img_width;
Condition0%0/3
$fragment->{'width'} || $img_width
l!l&&r!l&&!r
000
MC/DC0%0/2
$fragment->{'width'} || $img_width
$fragment->{'width'} $img_width
Truth table0%0/3
A || B
A= $fragment->{'width'}B= $img_width
ABresult
1X1
011
000
281 0 ▶ my $height = $fragment->{height} || $img_height;
Condition0%0/3
$fragment->{'height'} || $img_height
l!l&&r!l&&!r
000
MC/DC0%0/2
$fragment->{'height'} || $img_height
$fragment->{'height'} $img_height
Truth table0%0/3
A || B
A= $fragment->{'height'}B= $img_height
ABresult
1X1
011
000
282  
283 # Calculate scale factors.
284 0 my $xscale = 1;
285 0 ▶ if ( $width != $img_width ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($width != $img_width) 0 0 0
286 0 $xscale = $width / $img_width;
287 }
288 0 my $yscale = $xscale;
289 0 ▶ if ( $height != $img_height ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($height != $img_height) 0 0 0
290 0 $yscale = $height / $img_height;
291 }
292  
293 # Apply design scale. This cannot be set via properties but it
294 # intended for 3rd party plugins.
295 0 ▶ my $ds = $fragment->{design_scale} || 1;
Condition0%0/2
$fragment->{'design_scale'} || 1
l!l
00
MC/DC0%0/1
$fragment->{'design_scale'} || 1
$fragment->{'design_scale'}
Truth table0%0/2
A
A= $fragment->{'design_scale'}
Aresult
11
00
296 0 ▶ if ( $ds != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ds != 1) 0 0 0
297 0 $_ *= $ds for $xscale, $yscale, $width, $height;
298 }
299  
300 # Apply custom scale.
301 0 ▶ my ( $sx, $sy ) = @{$fragment->{scale} // [1,1]};
Condition0%0/2
$fragment->{"scale"} // [1, 1]
l!l
00
MC/DC0%0/1
$fragment->{"scale"} // [1, 1]
$fragment->{"scale"}
Truth table0%0/2
A
A= $fragment->{"scale"}
Aresult
11
00
302 0 ▶ if ( $sx != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sx != 1) 0 0 0
303 0 $xscale *= $sx;
304 0 $width *= $sx;
305 }
306 0 ▶ if ( $sy != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sy != 1) 0 0 0
307 0 $yscale *= $sy;
308 0 $height *= $sy;
309 }
310  
311 # Displacement wrt. the origin.
312 0 ▶ my $dx = $fragment->{dx} || 0;
Condition0%0/2
$fragment->{'dx'} || 0
l!l
00
MC/DC0%0/1
$fragment->{'dx'} || 0
$fragment->{'dx'}
Truth table0%0/2
A
A= $fragment->{'dx'}
Aresult
11
00
313 0 ▶ my $dy = $fragment->{dy} || 0;
Condition0%0/2
$fragment->{'dy'} || 0
l!l
00
MC/DC0%0/1
$fragment->{'dy'} || 0
$fragment->{'dy'}
Truth table0%0/2
A
A= $fragment->{'dy'}
Aresult
11
00
314  
315 0 ▶ if ( !$is_image && $fragment->{bbox} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_image and $fragment->{"bbox"}) 0 0 0
Condition0%0/3
not $is_image and $fragment->{"bbox"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $is_image and $fragment->{"bbox"}
not $is_image $fragment->{"bbox"}
Truth table0%0/3
not(A) and B
A= not $is_imageB= $fragment->{"bbox"}
ABresult
0X0
100
111
316 0 $dx += $bbox[0] * $xscale;
317 0 $dy += $bbox[1] * $yscale;
318 }
319  
320 # Use the image baseline, if any.
321 0 ▶ if ( $fragment->{base} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fragment->{"base"}) 0 0 0
322 0 $dy += ( $bbox[1] - $fragment->{base} ) * $yscale;
323 }
324
325 0 @bb = ( $dx, $dy, $width + $dx, $height + $dy, $xscale, $yscale );
326 0 @abox = @bb;
327  
328 # Bounding box width/height.
329 0 ▶ if ( defined $fragment->{w} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fragment->{"w"}) 0 0 0
330 0 $abox[0] = 0;
331 0 $abox[2] = $fragment->{w};
332 }
333 0 ▶ if ( defined $fragment->{a} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fragment->{"a"}) 0 0 0
334 0 $abox[3] = $fragment->{a};
335 }
336 0 ▶ if ( defined $fragment->{d} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fragment->{"d"}) 0 0 0
337 0 $abox[1] = $fragment->{d};
338 }
339 0 ▶ if ( $fragment->{align} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fragment->{"align"}) 0 0 0
340 0 ▶ if ( $fragment->{align} eq "right" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fragment->{'align'} eq 'right') { } 0 0 0
elsif ($fragment->{'align'} eq 'center') { } 0 0 0
341 0 $bb[0] += $abox[2] - $width;
342 }
343 elsif ( $fragment->{align} eq "center" ) {
344 0 $bb[0] += ($abox[2]-$width)/2;
345 }
346 }
347  
348 0 warn( ref($img) =~ /::Image::/ ? "IMG" : "OBJ",
349 " bbox [@bbox]",
350 " bb [@bb]",
351 " abox [@abox]",
352 ( $xscale != 1 || $yscale != 1 )
353 ? " scale=$xscale" : "",
354 ( $xscale != $yscale )
355 ? ",$yscale" : "", "\n") if DEBUG;
356  
357 0 return $fragment->{_bb} = { bbox => \@bbox, bb => \@bb, abox => \@abox };
358 3 }
359  
360 =head1 IMAGE PRODUCER
361  
362 The image object is produced with a call to method getimage(), that
363 can be overridden in a subclass.
364 The method gets a hash ref as argument.
365 This hash contains all the attributes and may be used for cacheing purposes.
366  
367 For example,
368  
369 method getimage ($fragment) {
370 $fragment->{_img} //= $self->pdf->image($fragment->{src});
371 }
372  
373 An overridden getimage() may produce a PDF XObject instead of an image
374 object. An XObject is treated similar to an image object, but is
375 aligned according to its bounding box if attribute C<bbox> is set to a
376 I<true> value, i.e., not zero.
377  
378 =cut
379  
380 0 ▶method getimage ($fragment) {
Subroutine getimage: not called CC=5 SCAR=34.0
Pod undocumented
381 0 ▶ return $fragment->{_img} if $fragment->{_img};
Branch: 1 branch
BranchTrueFalseTotal
if $fragment->{"_img"} 0 0 0
382  
383 0 my $src = $fragment->{src};
384  
385 # API suports jpg, png, gif and tiff.
386 # If we have the SVGPDF module, we can do SVG images.
387 0 ▶ if ( $src =~ /\.svg$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src =~ /\.svg$/iu) 0 0 0
388 0 local $SIG{__WARN__} = '__IGNORE__';
389 0 ▶ if ( eval "require SVGPDF" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval "require SVGPDF") 0 0 0
390 $fragment->{_img} = SVGPDF->new( pdf => $pdf )
391 0 ->process( $src, combine => "stacked" )->[0]->{xo};
392 }
393 # API will complain.
394 }
395  
396 # Pass to API.
397 0 ▶ $fragment->{_img} //= $pdf->image($src);
Condition0%0/3
$fragment->{"_img"} //= $pdf->image($src)
l!l&&r!l&&!r
000
MC/DC0%0/2
$fragment->{"_img"} //= $pdf->image($src)
$fragment->{"_img"} $pdf->image($src)
Truth table0%0/3
A //= B
A= $fragment->{"_img"}B= $pdf->image($src)
ABresult
1X1
011
000
398 3 }
399  
400 =head1 SEE ALSO
401  
402 L<Text::Layout>, L<PDF::API2>, L<PDF::Builder>.
403  
404 =head1 AUTHOR
405  
406 Johan Vromans, C<< <JV at CPAN dot org> >>
407  
408 =head1 SUPPORT
409  
410 This class is part of <Text::Layout>.
411  
412 Development takes place on GitHub:
413 L<https://github.com/sciurius/perl-Text-Layout>.
414  
415 Please report any bugs or feature requests using the issue tracker for
416 Text::Layout on GitHub.
417  
418 =head1 LICENSE
419  
420 See L<Text::Layout>.
421  
422 =cut
423  
424 3 1;
« Layout/PDFAPI2.pm ↑ Summary Layout.pm »