Layout.pm

filter: Statementstmt 48.5% 186 / 383 Branchbran 44.9% 97 / 216 Conditioncond 46.1% 24 / 52 MC/DCmcdc 26.4% 9 / 34 Subroutinesub 36.5% 30 / 82 Podpod 88.2% 60 / 68 totaltotal 48.6% 406 / 835 CC 134 SCAR 48.8
CC
134
Cov
47%
__ANON__
478.7
set_markup
106.9
__ANON__
72.0
CRAP 131.0
« Layout/PDFAPI2/ImageElement.pm ↑ Summary Layout/FontConfig.pm »
1 #! perl
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 package Text::Layout;
8  
9 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 our $VERSION = "0.045";
12  
13 =head1 NAME
14  
15 Text::Layout - Pango style markup formatting
16  
17 This module will cooperate with PDF::API2, PDF::Builder, Cairo, and Pango.
18  
19 =head1 SYNOPSIS
20  
21 Text::Layout provides methods for Pango style text formatting. Where
22 possible the methods have identical names and (near) identical
23 behaviour as their Pango counterparts.
24  
25 See L<https://developer.gnome.org/pango/stable/pango-Layout-Objects.html>.
26  
27 The package uses Text::Layout::FontConfig (included) to organize fonts
28 by description.
29  
30 If module HarfBuzz::Shaper is installed, Text::Layout can use it for
31 text shaping.
32  
33 Example, using PDF::API2 integration:
34  
35 use PDF::API2; # or PDF::Builder
36 use Text::Layout;
37  
38 # Create a PDF document.
39 my $pdf = PDF::API2->new; # or PDF::Builder->new
40 $pdf->default_page_size("a4") # ISO A4
41  
42 # Set up page and get the text context.
43 my $page = $pdf->page;
44 my $ctx = $page->text;
45  
46 # Create a markup instance.
47 my $layout = Text::Layout->new($pdf);
48  
49 # This example uses PDF corefonts only.
50 Text::Layout::FontConfig->register_corefonts;
51  
52 $layout->set_font_description(Text::Layout::FontConfig->from_string("times 40"));
53 $layout->set_markup( q{The <i><span foreground="red">quick</span> <span size="20"><b>brown</b></span></i> fox} );
54  
55 # Center text.
56 $layout->set_width(595); # width of A4 page
57 $layout->set_alignment("center");
58  
59 # Render it.
60 $layout->show( 0, 600, $ctx );
61 $pdf->save("out.pdf");
62  
63 All PDF::API2 graphic and text methods can still be used, they won't
64 interfere with the layout methods.
65  
66 =head1 NOTES FOR PDF::API2/Builder USERS
67  
68 =head2 Baselines
69  
70 PDF::API2 and PDF::Builder render texts using the font baseline as origin.
71  
72 This module typesets text in an area of possibly limited width and
73 height. The origin is the top left of this area. Currently this area
74 contains only a single line of text. This will change in the future
75 when line breaking and paragraph formatting is implemented.
76  
77 PDF::API2 and PDF::Builder coordinates have origin bottom left. This
78 module produces information with respect to top left coordinates.
79  
80 =head1 IMPORTANT NOTES FOR PANGO USERS
81  
82 =head2 Coordinate system
83  
84 Pango, layered upon Cairo, uses a coordinate system that starts off
85 top left. So for western text the direction is increasing I<x> and
86 increasing I<y>.
87  
88 PDF::API2 uses the coordinate system as defined in the PDF
89 specification. It starts off bottom left. For western text the
90 direction is increasing I<x> and B<de>creasing I<y>.
91  
92 =head1 Pango Conformance Mode
93  
94 Text::Layout can operate in one of two modes: I<convenience mode>
95 (enabled by default), and I<Pango conformance mode>. The desired mode
96 can be selected by calling the method set_pango_scaling().
97  
98 =head2 Pango coordinates
99  
100 Pango uses two device coordinates units: Pango units and device units.
101 Pango units are 1024 (C<PANGO_SCALE>) times the device units.
102  
103 Several methods have two variants, e.g. get_size() and
104 get_pixel_size(). The pixel-variant uses device units while the other
105 variant uses Pango units.
106  
107 In I<convenience mode>, this module assumes no scaling. All units are
108 PDF device units (1/72 inch).
109  
110 =head2 Pango device units
111  
112 Device units are used for font rendering. Pango device units are 96dpi
113 while PDF uses 72dpi.
114  
115 In I<convenience mode> this is ignored. E.g. a C<Times 20> font
116 will be of equal size in the two systems,
117  
118 In I<Pango conformance mode> you would need to specify a font size of
119 C<15360> to get a 20pt font.
120  
121 =head1 SUPPORTED MARKUP
122  
123 Text::Layout recognizes most of the Pango markup as provided by the
124 Pango library version 1.50 or newer. However, not everything is supported.
125  
126 =head2 Span attributes
127  
128 =over 8
129  
130 =item font="I<DESC>" font_desc="I<DESC>"
131  
132 Specifies a font to be used, e.g. C<Serif 20>.
133  
134 =item font_face="I<FAM>" face="I<FAM>"
135  
136 Specifies a font family to be used.
137  
138 =item font_family="I<FAM>"
139  
140 Same as font_face="I<FAM>".
141  
142 =item size=I<FNUM> size=I<FNUM>pt size=I<FNUM>%
143  
144 Font size in 1024ths of a point (conformance mode), or in points (e.g.
145 '12.5pt'), or a percentage (e.g. '200%'), or one of the relative sizes
146 'smaller' or 'larger'.
147  
148 Note that in Pango conformance mode, the actual font size is 96/72
149 larger. So C<"45pt"> gives a 60pt font.
150  
151 =item style="I<STYLE>" font_style="I<STYLE>"
152  
153 Specifes the font style, e.g. C<italic>.
154  
155 =item weight="I<WEIGHT>" font_weight="I<WEIGHT>"
156  
157 Specifies the font weight, e.g. C<bold>.
158  
159 =item foreground="I<COLOR>" fgcolor="I<COLOR>" color="I<COLOR>"
160  
161 Specifies the foreground colour, e.g. C<black>.
162  
163 =item background="I<COLOR>" bgcolor="I<COLOR>"
164  
165 Specifies the background colour, e.g. C<white>.
166  
167 =item underline="I<TYPE>"
168  
169 Enables underlining.
170 I<TYPE> must be C<none>, C<single>, or C<double>.
171  
172 =item underline_color="I<COLOR>"
173  
174 The colour to be used for underlining, if enabled.
175  
176 =item overline="I<TYPE>"
177  
178 Enables overlining.
179 I<TYPE> must be C<none>, C<single>, or C<double>.
180  
181 =item overline_color="I<COLOR>"
182  
183 The colour to be used for ovderlining, if enabled.
184  
185 =item strikethrough="I<ARG>"
186  
187 Enables or disables overlining. I<ARG> must be C<true> or C<1> to
188 enable, and C<false> or C<0> to disable.
189  
190 =item strikethrough_color="I<COLOR>"
191  
192 The colour to be used for striking, if enabled.
193  
194 =item rise=C<NUM>
195  
196 In convenience mode, lowers the text by I<NUM>/1024 of the font size.
197 May be negative to rise the text.
198  
199 In Pango conformance mode, rises the text by I<NUM> units from the baseline.
200 May be negative to lower the text.
201  
202 Note: In Pango conformance mode, C<rise> does B<not> accumulate.
203 Use C<baseline_shift> instead.
204  
205 =item rise=C<NUM>pt rise=C<NUM>% rise=C<NUM>em rise=C<NUM>ex
206  
207 Rises the text from the baseline. May be negative to lower the text.
208  
209 Units are points if postfixed by B<pt>, and a percentage of the current font size if postfixed by B<%>.
210  
211 B<em> units are equal to the current font size, B<ex> half the font size.
212  
213 Note: This is not (yet?) part of the Pango markup standard.
214  
215 =item baseline_shift=C<NUM> beseline_shift=C<NUM>pt baseline_shift=C<NUM>%
216  
217 Like C<rise>, but accumulates.
218  
219 =back
220  
221 Also supported but not part of the official Pango Markup specification.
222  
223 =over 8
224  
225 =item href="C<TARGET>"
226  
227 Creates a clickable target that activates the I<TARGET>.
228  
229 The I<TARGET> can be:
230  
231 =over 4
232  
233 =item *
234  
235 The name of an external PDF document, e.g. C<"that.pdf">.
236  
237 =item *
238  
239 A named destination in the document, e.g. C<"#there">.
240 See L<Strut attributes>.
241  
242 =item *
243  
244 a named destination in an external document, e.g. C<"that.pdf#there">.
245  
246 =back
247  
248 =back
249  
250 =head2 Img (image) attributes
251  
252 I<This is an extension to Pango markup.>
253  
254 Note that image markup elements may only occur as closed elements,
255 i.e., C<< <img/> >>.
256  
257 =over 8
258  
259 =item id="I<ID>"
260  
261 Implementation dependent.
262  
263 =item src="I<URI>"
264  
265 Source filename or url for the image.
266  
267 =item width="I<WIDTH>" (short: w="I<WIDTH>")
268  
269 The width the image should be considered to occupy, regardless its
270 actual dimensions.
271  
272 =item height="I<HEIGHT>" (short: h="I<HEIGHT>")
273  
274 The height the image should be considered to occupy, regardless its
275 actual dimensions.
276  
277 =item x="I<XDISP>"
278  
279 Horizontal displacement of the image relative to the C<< <img/> >> element.
280  
281 =item y="I<YDISP>"
282  
283 Vertical displacement of the image relative to the C<< <img/> >> element.
284  
285 =item border="I<THICK>"
286  
287 Provide a border around the element. I<THICK> denotes its thickness.
288  
289 =back
290  
291 =head2 Strut attributes
292  
293 I<This is an extension to Pango markup.>
294  
295 A C<strut> is a markup element that has bounding box dimensions but no
296 ink dimensions.
297  
298 Note that strut markup elements may only occur as closed elements,
299 i.e., C<< <strut/> >>.
300  
301 =over 8
302  
303 =item label="I<LABEL>"
304  
305 An optional identifying label.
306  
307 When a label is used, this will create a I<named destination>, a
308 symbolic name for a location in the document.
309 See the C<span> C<href> attribute,
310  
311 =item width="I<WIDTH>" (short w="I<WIDTH>")
312  
313 The width of the strut. Default value is zero.
314  
315 Width may be expressed in points, C<em> (font size) or C<ex> (half of
316 font size).
317  
318 =item ascender="I<ASC>" (short: a="I<ASC>")
319  
320 The ascender of the strut. Optional.
321  
322 May be expressed in points, C<em> (font size) or C<ex> (half of
323 font size).
324  
325 =item descender="I<DESC>" (short: d="I<DESC>")
326  
327 The descender of the strut. Optional.
328  
329 May be expressed in points, C<em> (font size) or C<ex> (half of
330 font size).
331  
332 =back
333  
334 =head2 Shortcuts
335  
336 Equivalent C<span> attributes for shortcuts.
337  
338 =over 8
339  
340 =item b
341  
342 weight=bold
343  
344 =item big
345  
346 larger
347  
348 =item emp
349  
350 style=italic
351  
352 =item i
353  
354 style=italic
355  
356 =item s
357  
358 strikethrough=true
359  
360 =item small
361  
362 size=smaller
363  
364 =item strong
365  
366 weight=bold
367  
368 =item sub
369  
370 size=smaller rise=-30%
371  
372 =item sup
373  
374 size=smaller rise=30%
375  
376 =item tt
377  
378 face=monospace
379  
380 =item u
381  
382 underline=single
383  
384 =back
385  
386 =cut
387  
388 8 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
389 8 PANGO_SCALE => 1024,
390 PANGO_DEVICE_UNITS => 96,
391 PDF_DEVICE_UNITS => 72,
392 8 };
393  
394 # Default is no Pango scaling.
395 25 ▶sub px2pu { $_[0] };
Subroutine px2pu: called CC=1 SCAR=0.0
Pod undocumented
396 0 ▶sub pu2px { $_[0] };
Subroutine pu2px: not called CC=1 SCAR=6.9
Pod undocumented
397  
398 8 ▶use Text::Layout::FontConfig;
Subroutine BEGIN: called CC=1 SCAR=0.0
399 8 ▶use Text::Layout::Utils qw(parse_kv);
Subroutine BEGIN: called CC=1 SCAR=0.0
400  
401 # Global (persistent) shortcodes.
402 # These map <XX> -> <span YY>.
403 8 my %shortcodes =
404 ( b => "weight=bold",
405 big => "size=larger",
406 emp => "style=italic",
407 i => "style=italic",
408 s => "strikethrough=true",
409 small => "size=smaller",
410 strong => "weight=bold",
411 sub => "size=smaller rise=-30%",
412 sup => "size=smaller rise=30%",
413 tt => "face=monospace",
414 u => "underline=single",
415 );
416  
417 =head1 METHODS
418  
419 =over
420  
421 =item new( $pdf )
422  
423 Creates a new layout instance for PDF::API2. This is for convenience.
424 It is equivalent to
425  
426 use Text::Layout::PDFAPI2;
427 $layout = Text::Layout::PDFAPI2->new($pdf);
428  
429 For other implementations only the above method can be used.
430  
431 The argument is the I<context> for text formatting. In the case of
432 PDF::API2 this will be a PDF::API2 object.
433  
434 =back
435  
436 =cut
437  
438 8 ▶sub new {
Subroutine new: called CC=4 SCAR=14.6
Pod documented
439 8 my ( $pkg, @data ) = @_;
440  
441 8 $pkg =~ s/::$//;
442 8 ▶ if ( $pkg eq __PACKAGE__ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg eq __PACKAGE__) 2 6 8
443 # For convenience (and backward compatibility)...
444 2 ▶ unless ( @data >= 1 && ref($data[0]) =~ /^PDF::(API2|Builder)\b/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@data >= 1 and ref($data[0]) =~ /^PDF::(API2|Builder)\b/) 0 2 2
Condition33%1/3
@data >= 1 and ref($data[0]) =~ /^PDF::(API2|Builder)\b/
!ll&&!rl&&r
002
MC/DC0%0/2
@data >= 1 and ref($data[0]) =~ /^PDF::(API2|Builder)\b/
@data >= 1 ref($data[0]) =~ /^PDF::(API2|Builder)\b/
Truth table33%1/3
A and B
A= @data >= 1B= ref($data[0]) =~ /^PDF::(API2|Builder)\b/
ABresult
0X0
100
111
445 0 croak("Please instantiate a backend, e.g. ".__PACKAGE__."::PDFAPI2");
446 }
447 2 require Text::Layout::PDFAPI2;
448 2 return Text::Layout::PDFAPI2->new(@data); # CUL8R
449 }
450 6 bless { _context => undef,
451 _fonts => {},
452 _content => [],
453 _px2pu => \&px2pu,
454 _pu2px => \&pu2px,
455 _pango => 0,
456 _sc => {},
457 } => $pkg;
458 }
459  
460 =over
461  
462 =item copy
463  
464 Copies (clones) a layout instance.
465  
466 The content is copied deeply, the context and fonts are copied by
467 reference.
468  
469 =back
470  
471 =cut
472  
473 0 ▶sub copy {
Subroutine copy: not called CC=2 SCAR=17.9
Pod documented
474 0 my ( $self ) = @_;
475  
476 0 my $copy = bless { %$self } => ref($self);
477 0 $copy->{_content} = [];
478 0 for ( @{ $self->{_content} } ) {
479 0 my %h;
480 0 @h{ keys %$_ } = values %$_;
481 0 push( @{ $copy->{_content} }, { %h } );
482 }
483 0 $copy->{_sc} = { %{ $self->{_sc} } };
484 0 return $copy;
485 }
486  
487 =over
488  
489 =item get_context
490  
491 Gets the context of this layout.
492  
493 =back
494  
495 =cut
496  
497 0 ▶sub get_context {
Subroutine get_context: not called CC=1 SCAR=6.9
Pod documented
498 0 my ( $self ) = ( $_ );
499 0 $self->{_context};
500 }
501  
502 =over
503  
504 =item context_changed
505  
506 Not supported.
507  
508 =back
509  
510 =cut
511  
512 0 ▶sub context_changed { nyi() }
Subroutine context_changed: not called CC=1 SCAR=6.9
Pod documented
513  
514 =over
515  
516 =item get_serial
517  
518 Not supported.
519  
520 =back
521  
522 =cut
523  
524 0 ▶sub get_serial { nyi() }
Subroutine get_serial: not called CC=1 SCAR=6.9
Pod documented
525  
526 =over
527  
528 =item set_text( $text )
529  
530 Puts a string in this layout instance. No markup is processed.
531  
532 Note that if you have used set_markup() on this layout before, you may
533 want to call set_attributes() to clear the attributes set on the
534 layout from the markup as this function does not clear all attributes.
535  
536 =back
537  
538 =cut
539  
540 0 ▶sub set_text {
Subroutine set_text: not called CC=1 SCAR=6.9
Pod documented
541 0 my ( $self, $string ) = @_;
542 $self->{_content} =
543 [ { type => "text",
544 text => $string,
545 font => $self->{_currentfont},
546 size => $self->{_currentsize},
547 color => $self->{_currentcolor},
548 0 base => 0,
549 } ];
550 0 delete( $self->{_bbcache} );
551 0 delete( $self->{_struts} );
552 }
553  
554 =over
555  
556 =item get_text
557  
558 Gets the content of this layout instance as a single string.
559 Markup is ignored.
560  
561 Returns undef if no text has been set.
562  
563 =back
564  
565 =cut
566  
567 0 ▶sub get_text {
Subroutine get_text: not called CC=2 SCAR=17.9
Pod documented
568 0 my ( $self ) = @_;
569 0 ▶ return unless $self->{_content};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_content"} 0 0 0
570 0 join( "", map { $_->{text} } @{ $self->{_content} } );
571 }
572  
573 =over
574  
575 =item get_character_count
576  
577 Returns the number of characters in the text of this layout.
578  
579 Basically the same as length of get_text().
580  
581 Returns undef if no text has been set.
582  
583 =back
584  
585 =cut
586  
587 0 ▶sub get_character_count {
Subroutine get_character_count: not called CC=3 SCAR=24.8
Pod documented
588 0 my ( $self ) = @_;
589 0 ▶ return unless $self->{_content};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_content"} 0 0 0
590 0 my $len = 0;
591 0 $len += length($_->{text}) for @{ $self->{_content} };
592 0 return $len;
593 }
594  
595 =over
596  
597 =item set_markup( $string )
598  
599 Puts a string in this layout instance.
600  
601 The string can contain Pango-compatible markup. See
602 L<https://developer.gnome.org/pygtk/stable/pango-markup-language.html>.
603  
604 Implementation note: Although all markup is parsed, not all is implemented.
605  
606 =back
607  
608 =cut
609  
610 8 ▶use Text::ParseWords;
Subroutine BEGIN: called CC=1 SCAR=0.0
611 8 ▶use constant STEP => 0.83333125; # Cf. Pango
Subroutine BEGIN: called CC=1 SCAR=0.0
612  
613 8 my %magstep =
614 ( "xx-small" => 1.0*STEP*STEP*STEP,
615 "x-small" => 1.0*STEP*STEP,
616 "small" => 1.0*STEP,
617 "smaller" => 1.0*STEP,
618 "medium" => 1.0,
619 "large" => 1.0/STEP,
620 "larger" => 1.0/STEP,
621 "x-large" => 1.0/(STEP*STEP),
622 "xx-large" => 1.0/(STEP*STEP*STEP),
623 );
624  
625 25 ▶sub set_markup {
Subroutine set_markup: called CC=27 SCAR=46.7
Pod documented
626 25 my ( $self, $string ) = @_;
627 25 ▶ confess("set_markup with UNDEF") unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 25 25
628 25 my @stack;
629 my @content;
630 25 my $fcur = $self->{_currentfont};
631 25 my $fcol = $self->{_currentcolor};
632 25 my $fsiz = $self->{_currentsize};
633 25 my $href;
634 my $undl;
635 25 my $uncl = $fcol;
636 25 my $ovrl;
637 25 my $ovcl = $fcol;
638 25 my $strk;
639 25 my $stcl = $fcol;
640 25 my $bcol;
641 25 my $base = 0;
642  
643 2 ▶ my $try_size = sub {
Subroutine __ANON__: called CC=4 SCAR=15.0
644 2 my ( $k ) = @_;
645 2 ▶ if ( exists $magstep{$k} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $magstep{$k}) { } 1 1 2
elsif ($k =~ /^(smaller)$/) { } 0 1 1
elsif ($k =~ /^(larger)$/) { } 0 1 1
646 1 $fsiz *= $magstep{$k};
647 }
648 elsif ( $k =~ /^(smaller)$/ ) {
649 0 $fsiz *= STEP;
650 }
651 elsif ( $k =~ /^(larger)$/ ) {
652 0 $fsiz /= STEP;
653 }
654 else {
655 1 return; # fail
656 }
657 1 return 1; # success
658 25 };
659  
660 25 my $span;
661 27 ▶ $span = sub {
Subroutine __ANON__: called CC=50 SCAR=61.7
662 27 my ( $v ) = @_;
663  
664 # Split the span attributes. Note that shellwords is so kind to
665 # split 'font="xx yy"' as a single word 'font=xx yy'.
666 # NOTE: can't use parse_kv -- order is important
667 27 foreach my $k ( shellwords($v) ) {
668  
669 # key=value
670 34 ▶ if ( $k =~ /^([-\w]+)=(.+)$/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($k =~ /^([-\w]+)=(.+)$/) { } 34 0 34
elsif ($k eq 'strikethrough') { } 0 0 0
elsif ($k eq 'fallback') { } 0 0 0
671 34 my ( $k, $v ) = ( $1, $2 );
672  
673 # Ignore case unless required.
674 34 ▶ $v = lc $v unless $k =~ /^(link|href|a)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $k =~ /^(link|href|a)$/ 34 0 34
675  
676 # <span font_desc="Sans 20">
677 34 ▶ if ( $k =~ /^(font|font_desc)$/ ) {
Branch: 21 branches
BranchTrueFalseTotal
if ($k =~ /^(font|font_desc)$/) { } 1 33 34
elsif ($k =~ /^(face|font_face|font_family)$/) { } 1 32 33
elsif ($k =~ /^(size|font_size)$/) { } 2 30 32
elsif ($k =~ /^(style|font_style)$/) { } 9 21 30
elsif ($k =~ /^(weight|font_weight)$/) { } 10 11 21
elsif ($k =~ /^(?:font_)?(variant|stretch)$/) { } 0 11 11
elsif ($k =~ /^(features|background_alpha|alpha)$/) { } 0 11 11
elsif ($k =~ /^(foreground|fgcolor|color)$/) { } 0 11 11
elsif ($k =~ /^(background|bgcolor)$/) { } 0 11 11
elsif ($k eq 'underline' and $v =~ /^(none|single|double)$/i) { } 2 9 11
elsif ($k eq 'underline_color') { } 1 8 9
elsif ($k eq 'overline' and $v =~ /^(none|single|double)$/i) { } 1 7 8
elsif ($k eq 'overline_color') { } 1 6 7
elsif ($k eq 'rise') { } 0 6 6
elsif ($k eq 'baseline_shift') { } 0 6 6
elsif ($k eq 'strikethrough' and $v =~ /^(true|1)$/i) { } 4 2 6
elsif ($k eq 'strikethrough' and $v =~ /^(false|0)$/i) { } 0 2 2
elsif ($k eq 'strikethrough_color') { } 2 0 2
elsif ($k eq 'fallback') { } 0 0 0
elsif ($k eq 'lang') { } 0 0 0
elsif ($k eq 'href') { } 0 0 0
678 1 $fcur = Text::Layout::FontConfig->from_string($v);
679 1 ▶ $fsiz = $fcur->get_size if $fcur->get_size;
Branch: 1 branch
BranchTrueFalseTotal
if $fcur->get_size 1 0 1
680 }
681  
682 # <span face="Sans">
683 elsif ( $k =~ /^(face|font_face|font_family)$/ ) {
684 $fcur = Text::Layout::FontConfig->find_font( $v,
685 $fcur->{style},
686 1 $fcur->{weight});
687 }
688  
689 # <span size=20>
690 elsif ( $k =~ /^(size|font_size)$/ ) {
691 2 ▶ if ( $try_size->($v) ) {
Branch: 4 branches
BranchTrueFalseTotal
if (&$try_size($v)) { } 1 1 2
elsif ($v =~ /^(\d+(?:\.\d+)?)pt$/) { } 0 1 1
elsif ($v =~ /^\d+(?:\.\d+)?$/) { } 1 0 1
elsif ($v =~ /^(\d+(?:\.\d+)?)\%$/) { } 0 0 0
692 #ok
693 }
694 elsif ( $v =~ /^(\d+(?:\.\d+)?)pt$/ ) {
695 $fsiz = $self->{_pango}
696 0 ▶ ? $1 * PANGO_DEVICE_UNITS / PDF_DEVICE_UNITS
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_pango'} ? : 0 0 0
697 : $1;
698 # warn("fsiz \"$v\" -> $fsiz\n");
699 }
700 elsif ( $v =~ /^\d+(?:\.\d+)?$/ ) {
701 $fsiz = $self->{_pango}
702 1 ▶ ? $v * PANGO_DEVICE_UNITS / PDF_DEVICE_UNITS / PANGO_SCALE
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_pango'} ? : 0 1 1
703 : $v;
704 # warn("fsiz \"$v\" -> $fsiz\n");
705 }
706 elsif ( $v =~ /^(\d+(?:\.\d+)?)\%$/ ) {
707 0 $fsiz *= $1 / 100;
708 # warn("fsiz \"$v\" -> $fsiz\n");
709 }
710 else {
711 0 carp("Invalid size: \"$v\"\n");
712 }
713 }
714  
715 # <span style="Italic">
716 elsif ( $k =~ /^(style|font_style)$/ ) {
717 9 $v = Text::Layout::FontConfig::_norm_style($v);
718 $fcur = Text::Layout::FontConfig->find_font( $fcur->{family},
719 9 $v, $fcur->{weight} );
720 }
721  
722 # <span weight="bold">
723 elsif ( $k =~ /^(weight|font_weight)$/ ) {
724 10 $v = Text::Layout::FontConfig::_norm_weight($v);
725 $fcur = Text::Layout::FontConfig->find_font( $fcur->{family},
726 10 $fcur->{style}, $v );
727 }
728  
729 # <span variant="...">
730 # <span stretch="...">
731 elsif ( $k =~ /^(?:font_)?(variant|stretch)$/ ) {
732 # ignore for now
733 }
734 elsif ( $k =~ /^(features|background_alpha|alpha)$/ ) {
735 # ignore for now
736 }
737  
738 # <span foreground="red">
739 elsif ( $k =~ /^(foreground|fgcolor|color)$/ ) {
740 0 $fcol = $v;
741 }
742  
743 # <span background="red">
744 elsif ( $k =~ /^(background|bgcolor)$/ ) {
745 0 $bcol = $v;
746 }
747  
748 # <span underline="double">
749 ▶ elsif ( $k eq "underline" && $v =~ /^(none|single|double)$/i ) {
Condition66%2/3
$k eq "underline" and $v =~ /^(none|single|double)$/i
!ll&&!rl&&r
902
MC/DC50%1/2
$k eq "underline" and $v =~ /^(none|single|double)$/i
$k eq "underline" $v =~ /^(none|single|double)$/i
Truth table66%2/3
A and B
A= $k eq "underline"B= $v =~ /^(none|single|double)$/i
ABresult
0X0
100
111
750 2 $undl = lc $v;
751 }
752 elsif ( $k eq "underline_color" ) {
753 1 $uncl = lc $v;
754 }
755  
756 # <span overline="double">
757 ▶ elsif ( $k eq "overline" && $v =~ /^(none|single|double)$/i ) {
Condition66%2/3
$k eq "overline" and $v =~ /^(none|single|double)$/i
!ll&&!rl&&r
701
MC/DC50%1/2
$k eq "overline" and $v =~ /^(none|single|double)$/i
$k eq "overline" $v =~ /^(none|single|double)$/i
Truth table66%2/3
A and B
A= $k eq "overline"B= $v =~ /^(none|single|double)$/i
ABresult
0X0
100
111
758 1 $ovrl = lc $v;
759 }
760 elsif ( $k eq "overline_color" ) {
761 1 $ovcl = lc $v;
762 }
763  
764 # <span rise=324>
765 elsif ( $k eq "rise" ) {
766 0 ▶ if ( $v =~ /^(-?\d+(?:\.\d*)?)pt$/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($v =~ /^(-?\d+(?:\.\d*)?)pt$/) { } 0 0 0
elsif (not $self->{'_pango'} and $v =~ /^(-?\d+(?:\.\d*)?)\%$/) { } 0 0 0
elsif (not $self->{'_pango'} and $v =~ /^(-?\d+(?:\.\d*)?)e([mx])$/) { } 0 0 0
767 0 $base = -$1;
768 }
769 ▶ elsif ( !$self->{_pango} && $v =~ /^(-?\d+(?:\.\d*)?)\%$/ ) {
Condition0%0/3
not $self->{"_pango"} and $v =~ /^(-?\d+(?:\.\d*)?)\%$/
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->{"_pango"} and $v =~ /^(-?\d+(?:\.\d*)?)\%$/
not $self->{"_pango"} $v =~ /^(-?\d+(?:\.\d*)?)\%$/
Truth table0%0/3
not(A) and B
A= not $self->{"_pango"}B= $v =~ /^(-?\d+(?:\.\d*)?)\%$/
ABresult
0X0
100
111
770 0 $base = -$1 * $fsiz / 100;
771 }
772 ▶ elsif ( !$self->{_pango} && $v =~ /^(-?\d+(?:\.\d*)?)e([mx])$/ ) {
Condition0%0/3
not $self->{"_pango"} and $v =~ /^(-?\d+(?:\.\d*)?)e([mx])$/
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->{"_pango"} and $v =~ /^(-?\d+(?:\.\d*)?)e([mx])$/
not $self->{"_pango"} $v =~ /^(-?\d+(?:\.\d*)?)e([mx])$/
Truth table0%0/3
not(A) and B
A= not $self->{"_pango"}B= $v =~ /^(-?\d+(?:\.\d*)?)e([mx])$/
ABresult
0X0
100
111
773 0 ▶ $base = $2 eq 'x' ? -$1 * $fsiz / 2 : -$1 * $fsiz;
Branch: 1 branch
BranchTrueFalseTotal
$2 eq 'x' ? : 0 0 0
774 }
775 else {
776 0 $v /= PANGO_SCALE;
777 0 ▶ $base = $self->{_pango} ? -$v : $base + $v * $fsiz;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_pango'} ? : 0 0 0
778 }
779 }
780  
781 # <span baseline_shift=324>
782 # Line rise, but accumulate.
783 elsif ( $k eq "baseline_shift" ) {
784 0 ▶ if ( $v =~ /^(-?\d+(?:\.\d*)?)pt$/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($v =~ /^(-?\d+(?:\.\d*)?)pt$/) { } 0 0 0
elsif ($v =~ /^(-?\d+(?:\.\d*)?)\%$/) { } 0 0 0
elsif ($v =~ /^(-?\d+(?:\.\d*)?)e([mx])$/) { } 0 0 0
785 0 $base -= $1;
786 }
787 elsif ( $v =~ /^(-?\d+(?:\.\d*)?)\%$/ ) {
788 0 $base -= $1 * $fsiz / 100;
789 }
790 elsif ( $v =~ /^(-?\d+(?:\.\d*)?)e([mx])$/ ) {
791 0 ▶ $base = $2 eq 'x' ? $1 * $fsiz / 2 : $1 * $fsiz;
Branch: 1 branch
BranchTrueFalseTotal
$2 eq 'x' ? : 0 0 0
792 }
793 else {
794 0 ▶ $base -= $self->{_pango} ? $v : -$v * $fsiz;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_pango'} ? : 0 0 0
795 }
796 }
797  
798 # <span strikethrough=false>
799 ▶ elsif ( $k eq "strikethrough" && $v =~ /^(true|1)$/i ) {
Condition66%2/3
$k eq "strikethrough" and $v =~ /^(true|1)$/i
!ll&&!rl&&r
204
MC/DC50%1/2
$k eq "strikethrough" and $v =~ /^(true|1)$/i
$k eq "strikethrough" $v =~ /^(true|1)$/i
Truth table66%2/3
A and B
A= $k eq "strikethrough"B= $v =~ /^(true|1)$/i
ABresult
0X0
100
111
800 4 $strk = 1;
801 }
802 ▶ elsif ( $k eq "strikethrough" && $v =~ /^(false|0)$/i ) {
Condition33%1/3
$k eq "strikethrough" and $v =~ /^(false|0)$/i
!ll&&!rl&&r
200
MC/DC0%0/2
$k eq "strikethrough" and $v =~ /^(false|0)$/i
$k eq "strikethrough" $v =~ /^(false|0)$/i
Truth table33%1/3
A and B
A= $k eq "strikethrough"B= $v =~ /^(false|0)$/i
ABresult
0X0
100
111
803 0 $strk = 0;
804 }
805 elsif ( $k eq "strikethrough_color" ) {
806 2 $stcl = $v;
807 }
808  
809 # <span fallback=false>
810 elsif ( $k eq "fallback" ) {
811 # Not supported.
812 }
813  
814 # <span lang="en">
815 elsif ( $k eq "lang" ) {
816 # Not supported.
817 }
818  
819 # <span href="...">
820 elsif ( $k eq "href" ) {
821 0 $href = $v;
822 }
823 }
824  
825 # <span strikethrough>
826 elsif ( $k eq "strikethrough" ) {
827 0 $span->("strikethrough=true");
828 }
829  
830 # <span fallback>
831 elsif ( $k eq "fallback" ) {
832 0 $span->("fallback=true");
833 }
834  
835 else {
836 0 carp("Invalid span markup: \"$k\"\n");
837 }
838 }
839 25 };
840  
841 0 ▶ my $image = sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
842 0 my ( $v ) = @_;
843  
844 0 my %img;
845 # Split the attributes.
846 0 ▶ while ( my ( $k, $v ) = each %{parse_kv($v)} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{parse_kv($v);}) 0 0 0
847  
848 # Ignore case unless required.
849 0 ▶ $v = lc $v unless $k =~ /^(src|id)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $k =~ /^(src|id)$/ 0 0 0
850  
851 0 ▶ if ( $k eq "src" ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($k eq 'src') { } 0 0 0
elsif ($k eq 'id') { } 0 0 0
elsif ($k =~ /^(width|height|w|h)$/) { } 0 0 0
elsif ($k =~ /^(x|y)$/) { } 0 0 0
elsif ($k eq 'border') { } 0 0 0
852 0 $img{src} = $v;
853 }
854 elsif ( $k eq "id" ) {
855 0 $img{id} = $v;
856 }
857 elsif ( $k =~ /^(width|height|w|h)$/ ) {
858 0 $img{$k} = $v;
859 }
860 elsif ( $k =~ /^(x|y)$/ ) {
861 0 $img{$k} = $v;
862 }
863 elsif ( $k eq "border" ) {
864 0 $img{border} = $v;
865 }
866 else {
867 0 carp("Invalid image attribute: \"$k\"\n");
868 }
869 }
870 0 return \%img;
871 25 };
872  
873  
874 # Split the string on markup instructions.
875 25 L: foreach my $a ( split( /(<.*?>)/, $string ) ) {
876  
877 # Closing markup, e.g. </b> or </span>.
878 137 ▶ if ( $a =~ m;^<\s*/\s*(\w+)(.*)>$; ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($a =~ m[^<\s*/\s*(\w+)(.*)>$]) { } 29 108 137
elsif ($a =~ /^<\s*([-\w]+)(.*)?>$/) { } 29 79 108
879 29 my $k = lc $1;
880 29 ▶ if ( @stack ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) { } 29 0 29
881 # Check if it is closing the currently pending markup.
882 29 ▶ if ( $stack[-1][0] =~ /^<\s*$k\b/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stack[-1][0] =~ /^<\s*$k\b/) { } 29 0 29
883 # Restore.
884 ( undef,
885 $fcur, $fsiz, $fcol, $undl, $uncl, $ovrl, $ovcl,
886 29 $bcol, $strk, $stcl, $base, $href ) = @{$stack[-1]};
887 29 pop(@stack);
888 }
889 else {
890 0 carp("Markup error: \"$string\"\n",
891 " Closing <$k> but $stack[-1][0] is open\n");
892 0 next;
893 }
894 }
895 else {
896 0 carp("Markup error: \"$string\"\n",
897 " Closing <$k> but no markup is open\n");
898 0 next;
899 }
900 }
901  
902 # Opening markup, e.g. <b> or <span ...>.
903 elsif ( $a =~ m;^<\s*([-\w]+)(.*)?>$; ) {
904 29 my $k = lc $1;
905 29 my $v = $2;
906 29 my $closed = $v =~ s;\s*/\s*$;;;
907  
908 # Save.
909 29 push( @stack, [ "<$k".lc($v).">",
910 $fcur, $fsiz, $fcol, $undl, $uncl, $ovrl, $ovcl,
911 $bcol, $strk, $stcl, $base, $href ] );
912  
913 # Find existing shortcode.
914 29 ▶ if ( my $sc = $self->{_sc}->{$k} // $shortcodes{$k} ) {
Branch: 4 branches
BranchTrueFalseTotal
if (my $sc = $self->{'_sc'}{$k} // $shortcodes{$k}) { } 16 13 29
elsif ($k eq 'strut' and $closed) { } 2 11 13
elsif ($k =~ /^(span)$/) { } 11 0 11
elsif (my $p = $self->get_element_handler($k) and $closed) { } 0 0 0
Condition100%3/3
$self->{"_sc"}{$k} // $shortcodes{$k}
l!l&&r!l&&!r
11513
MC/DC100%2/2
$self->{"_sc"}{$k} // $shortcodes{$k}
$self->{"_sc"}{$k} $shortcodes{$k}
Truth table100%3/3
A // B
A= $self->{"_sc"}{$k}B= $shortcodes{$k}
ABresult
1X1
011
000
915 16 $span->($sc);
916 }
917  
918 # <strut width=".."/>
919 ▶ elsif ( $k eq "strut" && $closed ) {
Condition66%2/3
$k eq "strut" and $closed
!ll&&!rl&&r
1102
MC/DC50%1/2
$k eq "strut" and $closed
$k eq "strut" $closed
Truth table66%2/3
A and B
A= $k eq "strut"B= $closed
ABresult
0X0
100
111
920 2 my $args = { w => 0, %{parse_kv($v)} };
921 # Split the attributes.
922 2 ▶ while ( my ( $k, $v ) = each(%$args) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$args) 5 2 7
923 5 ▶ if ( $k =~ /^(w(?:idth)?|a(?:scend)?|d(?:escend)?)$/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($k =~ /^(w(?:idth)?|a(?:scend)?|d(?:escend)?)$/i) { } 3 2 5
elsif ($k =~ /^(label)$/i) { } 2 0 2
924 3 ▶ if ( $v =~ /^([.\d]+)em$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($v =~ /^([.\d]+)em$/) { } 0 3 3
elsif ($v =~ /^([.\d]+)ex$/) { } 0 3 3
925 0 $args->{$k} = $1 * $fsiz;
926 }
927 elsif ( $v =~ /^([.\d]+)ex$/ ) {
928 0 $args->{$k} = 0.5 * $1 * $fsiz;
929 }
930 else {
931 3 $args->{$k} = 0+$v;
932 }
933 }
934 elsif ( $k =~ /^(label)$/i ) {
935 2 $args->{$k} = $v;
936 }
937 else {
938 0 carp("Unknown strut attribute: \"$k\"\n");
939 }
940 }
941 push( @content, { type => $k,
942 width => $args->{width} // $args->{w},
943 desc => $args->{descend} // $args->{d},
944 asc => $args->{ascend} // $args->{a},
945 2 ▶ $args->{label} ? ( label => $args->{label} ) : (),
Branch: 1 branch
BranchTrueFalseTotal
$args->{'label'} ? : 2 0 2
Condition44%4/9
$args->{"width"} // $args->{"w"}
l!l&&r!l&&!r
110
$args->{"descend"} // $args->{"d"}
l!l&&r!l&&!r
002
$args->{"ascend"} // $args->{"a"}
l!l&&r!l&&!r
002
MC/DC0%0/2
$args->{"width"} // $args->{"w"}
$args->{"width"} $args->{"w"}
MC/DC0%0/2
$args->{"descend"} // $args->{"d"}
$args->{"descend"} $args->{"d"}
MC/DC0%0/2
$args->{"ascend"} // $args->{"a"}
$args->{"ascend"} $args->{"a"}
Truth table66%2/3
A // B
A= $args->{"width"}B= $args->{"w"}
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $args->{"descend"}B= $args->{"d"}
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $args->{"ascend"}B= $args->{"a"}
ABresult
1X1
011
000
946 } );
947 }
948  
949 # <span ...>.
950 elsif ( $k =~ /^(span)$/ ) {
951 11 $span->($v);
952 }
953  
954 # <.../>.
955 ▶ elsif ( ( my $p = $self->get_element_handler($k) ) && $closed ) {
Condition0%0/3
my $p = $self->get_element_handler($k) and $closed
!ll&&!rl&&r
000
MC/DC0%0/2
my $p = $self->get_element_handler($k) and $closed
my $p = $self->get_element_handler($k) $closed
Truth table0%0/3
A and B
A= my $p = $self->get_element_handler($k)B= $closed
ABresult
0X0
100
111
956 push( @content, { type => $k,
957 0 %{ $p->parse
958 ( { type => $k,
959 font => $fcur,
960 size => $fsiz,
961 color => $fcol,
962 bgcolor => $bcol,
963 underline => $undl,
964 underline_color => $uncl,
965 overline => $ovrl,
966 overline_color => $ovcl,
967 strikethrough => $strk,
968 strikethrough_color => $stcl,
969 base => $base,
970 href => $href,
971 }, $k, $v ) } } );
972 }
973  
974 else {
975 0 carp("Invalid markup: \"$k\"\n");
976 }
977 29 ▶ if ( $closed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($closed) 2 27 29
978 2 $a = "</$k>";
979 2 redo L;
980 }
981 }
982  
983 # Text.
984 else {
985 79 ▶ push( @content,
Branch: 1 branch
BranchTrueFalseTotal
if defined $a and $a ne "" 72 7 79
Condition66%2/3
defined $a and $a ne ""
!ll&&!rl&&r
0772
MC/DC50%1/2
defined $a and $a ne ""
defined $a $a ne ""
Truth table66%2/3
A and B
A= defined $aB= $a ne ""
ABresult
0X0
100
111
986 { type => "text",
987 text => $a,
988 font => $fcur,
989 size => $fsiz,
990 color => $fcol,
991 bgcolor => $bcol,
992 underline => $undl,
993 underline_color => $uncl,
994 overline => $ovrl,
995 overline_color => $ovcl,
996 strikethrough => $strk,
997 strikethrough_color => $stcl,
998 base => $base,
999 href => $href,
1000 } ) if defined $a && $a ne '';
1001 }
1002 }
1003  
1004 # Verify that the markup is decently closed.
1005 25 ▶ if ( @stack ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) 0 25 25
1006 carp("Markup error: \"$string\"\n",
1007 " Unclosed markup: ",
1008 0 join( " ", map { $_->[0] } @{[ reverse @stack ]} ), "\n" );
1009 }
1010  
1011 # Store content.
1012 25 $self->{_content} = \@content;
1013 25 delete( $self->{_bbcache} );
1014 25 delete( $self->{_struts} );
1015 }
1016  
1017 =over
1018  
1019 =item set_markup_with_accel
1020  
1021 Not supported.
1022  
1023 =back
1024  
1025 =cut
1026  
1027 0 ▶sub set_markup_with_accel { nyi() }
Subroutine set_markup_with_accel: not called CC=1 SCAR=6.9
Pod documented
1028  
1029 =over
1030  
1031 =item set_attributes
1032  
1033 =item get_attributes
1034  
1035 Not yet implemented.
1036  
1037 =back
1038  
1039 =cut
1040  
1041 0 ▶sub set_attributes { nyi() }
Subroutine set_attributes: not called CC=1 SCAR=6.9
Pod documented
1042  
1043 0 ▶sub get_attributes { nyi() }
Subroutine get_attributes: not called CC=1 SCAR=6.9
Pod documented
1044  
1045 =over
1046  
1047 =item set_font_description( $description )
1048  
1049 Sets the default font description for the layout. If no font
1050 description is set on the layout, the font description from the
1051 layout's context is used.
1052  
1053 $description is a Text::Layout::FontConfig object.
1054  
1055 =back
1056  
1057 =cut
1058  
1059 5 ▶sub set_font_description {
Subroutine set_font_description: called CC=6 SCAR=19.5
Pod documented
1060 5 my ( $self, $description ) = @_;
1061 5 my $o = "Text::Layout::FontDescriptor";
1062 5 ▶ croak("set_font_description requires a $o object (not $description)")
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($description, $o) 0 5 5
1063 unless UNIVERSAL::isa( $description, $o );
1064  
1065 5 $self->{_currentfont} = $description;
1066  
1067 5 ▶ if ( my $sz = $description->{size} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sz = $description->{"size"}) 5 0 5
1068 # Font sizes can be in either Pango units, or device units.
1069 # Use heuristics.
1070 5 ▶ if ( $sz > 2 * PANGO_SCALE ) { # Pango units
Branch: 1 branch
BranchTrueFalseTotal
if ($sz > 2048) 0 5 5
1071 0 $sz /= PANGO_SCALE;
1072 }
1073 5 ▶ if ( $self->{_pango} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_pango"}) 0 5 5
1074 # Pango uses 96dpi while PDF is 72dpi.
1075 0 $sz *= PANGO_DEVICE_UNITS / PDF_DEVICE_UNITS;
1076 }
1077 5 $self->{_currentsize} = $sz;
1078 }
1079  
1080 5 ▶ $self->{_currentcolor} = $description->{color} || "black";
Condition50%1/2
$description->{'color'} || 'black'
l!l
05
MC/DC0%0/1
$description->{'color'} || 'black'
$description->{'color'}
Truth table50%1/2
A
A= $description->{'color'}
Aresult
11
00
1081  
1082 5 delete( $self->{_bbcache} );
1083 5 delete( $self->{_struts} );
1084 }
1085  
1086 =over
1087  
1088 =item get_font_description
1089  
1090 Gets the font description for the layout.
1091  
1092 Returns undef if no font has been set yet.
1093  
1094 =back
1095  
1096 =cut
1097  
1098 1 ▶sub get_font_description {
Subroutine get_font_description: called CC=1 SCAR=0.0
Pod documented
1099 1 my ( $self ) = ( @_ );
1100 1 my $res = $self->{_currentfont};
1101 1 $res->{size} = $self->get_font_size;
1102 1 return $res;
1103 }
1104  
1105 =over
1106  
1107 =item set_width( $width )
1108  
1109 Sets the width to which the lines of the layout should align,
1110 wrap or ellipsized. A value of zero or less means unlimited width.
1111 The width is in Pango units.
1112  
1113 Implementation note: Only alignment is implemented.
1114  
1115 =back
1116  
1117 =cut
1118  
1119 0 ▶sub set_width {
Subroutine set_width: not called CC=1 SCAR=6.9
Pod documented
1120 0 my ( $self, $width ) = @_;
1121 0 $self->{_width} = $self->{_pu2px}->($width);
1122 0 delete( $self->{_bbcache} );
1123 0 delete( $self->{_struts} );
1124 }
1125  
1126 =over
1127  
1128 =item get_width
1129  
1130 Gets the width in Pango units for for this instance, or zero if
1131 unlimited.
1132  
1133 =back
1134  
1135 =cut
1136  
1137 0 ▶sub get_width {
Subroutine get_width: not called CC=2 SCAR=17.9
Pod documented
1138 0 my ( $self ) = @_;
1139 0 ▶ $self->{_px2pu}->($self->{_width}) || 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_px2pu"}->($self->{"_width"}) or 0 0 0 0
1140 }
1141  
1142 =over
1143  
1144 =item set_height( $height )
1145  
1146 Sets the height in Pango units for this instance.
1147  
1148 Implementation note: Height restrictions are not yet implemented.
1149  
1150 =back
1151  
1152 =cut
1153  
1154 0 ▶sub set_height {
Subroutine set_height: not called CC=1 SCAR=6.9
Pod documented
1155 0 my ( $self, $height ) = @_;
1156 0 $self->{_height} = $self->{_pu2px}->($height);
1157 0 delete( $self->{_bbcache} );
1158 0 delete( $self->{_struts} );
1159 }
1160  
1161 =over
1162  
1163 =item get_height
1164  
1165 Gets the height in Pango units for this instance, or zero if no height
1166 restrictions apply.
1167  
1168 =back
1169  
1170 =cut
1171  
1172 0 ▶sub get_height {
Subroutine get_height: not called CC=2 SCAR=17.9
Pod documented
1173 0 my ( $self ) = @_;
1174 0 ▶ $self->{_px2pu}->($self->{_height}) || 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_px2pu"}->($self->{"_height"}) or 0 0 0 0
1175 }
1176  
1177 =over
1178  
1179 =item set_wrap( $mode )
1180  
1181 Sets the wrap mode; the wrap mode only has effect if a width is set on
1182 the layout with set_width(). To turn off wrapping, set the width to
1183 zero or less.
1184  
1185 Not yet implemented.
1186  
1187 =back
1188  
1189 =cut
1190  
1191 0 ▶sub set_wrap {
Subroutine set_wrap: not called CC=1 SCAR=6.9
Pod documented
1192 0 my ( $self, $mode ) = @_;
1193 0 $self->{_wrap} = $mode;
1194 0 nyi();
1195 }
1196  
1197 =over
1198  
1199 =item get_wrap
1200  
1201 Returns the current wrap mode.
1202  
1203 Not yet implemented.
1204  
1205 =back
1206  
1207 =cut
1208  
1209 0 ▶sub get_wrap {
Subroutine get_wrap: not called CC=1 SCAR=6.9
Pod documented
1210 0 my ( $self ) = @_;
1211 0 $self->{_wrap};
1212 }
1213  
1214 =over
1215  
1216 =item is_wrapped
1217  
1218 Queries whether the layout had to wrap any paragraphs.
1219  
1220 =back
1221  
1222 =cut
1223  
1224 0 ▶sub is_wrapped {
Subroutine is_wrapped: not called CC=1 SCAR=6.9
Pod documented
1225 0 my ( $self ) = @_;
1226 0 nyi();
1227 }
1228  
1229 =over
1230  
1231 =item set_ellipsize( $mode )
1232  
1233 Sets the type of ellipsization being performed for the layout.
1234  
1235 Not yet implemented.
1236  
1237 =back
1238  
1239 =cut
1240  
1241 0 ▶sub set_ellipsize {
Subroutine set_ellipsize: not called CC=1 SCAR=6.9
Pod documented
1242 0 my ( $self ) = @_;
1243 0 nyi();
1244 }
1245  
1246 =over
1247  
1248 =item get_ellipsize
1249  
1250 Gets the type of ellipsization being performed for the layout.
1251  
1252 =back
1253  
1254 =cut
1255  
1256 0 ▶sub get_ellipsize {
Subroutine get_ellipsize: not called CC=1 SCAR=6.9
Pod documented
1257 0 my ( $self ) = @_;
1258 0 return;
1259 }
1260  
1261 =over
1262  
1263 =item is_ellipsized
1264  
1265 Queries whether the layout had to ellipsize any paragraphs.
1266  
1267 Not yet implemented.
1268  
1269 =back
1270  
1271 =cut
1272  
1273 0 ▶sub is_ellipsized {
Subroutine is_ellipsized: not called CC=1 SCAR=6.9
Pod documented
1274 0 my ( $self ) = @_;
1275 0 nyi();
1276 }
1277  
1278 =over
1279  
1280 =item set_indent( $value )
1281  
1282 Sets the width in Pango units to indent for each paragraph.
1283  
1284 A negative value of indent will produce a hanging indentation. That
1285 is, the first line will have the full width, and subsequent lines will
1286 be indented by the absolute value of indent.
1287  
1288 The indent setting is ignored if layout alignment is set to C<center>.
1289  
1290 Not yet implemented.
1291  
1292 =back
1293  
1294 =cut
1295  
1296 0 ▶sub set_indent {
Subroutine set_indent: not called CC=1 SCAR=6.9
Pod documented
1297 0 my ( $self, $size ) = @_;
1298 0 $self->{_currentindent} = $self->{_pu2px}->($size);
1299 0 nyi();
1300 }
1301  
1302 =over
1303  
1304 =item get_indent
1305  
1306 Gets the current indent value in Pango units.
1307  
1308 =back
1309  
1310 =cut
1311  
1312 0 ▶sub get_indent {
Subroutine get_indent: not called CC=2 SCAR=17.9
Pod documented
1313 0 my ( $self ) = @_;
1314 0 ▶ $self->{_px2pu}->($self->{_currentindent}) || 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_px2pu"}->($self->{"_currentindent"}) or 0 0 0 0
1315 }
1316  
1317 =over
1318  
1319 =item set_spacing( $value )
1320  
1321 Sets the amount of spacing, in Pango units, between lines of the
1322 layout.
1323  
1324 When placing lines with spacing, things are arranged so that
1325  
1326 line2.top = line1.bottom + spacing
1327  
1328 Note: By default the line height (as determined by the font) for
1329 placing lines is used. The spacing set with this function is only
1330 taken into account when the line-height factor is set to zero with
1331 set_line_spacing().
1332  
1333 Not yet implemented.
1334  
1335 =back
1336  
1337 =cut
1338  
1339 0 ▶sub set_spacing {
Subroutine set_spacing: not called CC=1 SCAR=6.9
Pod documented
1340 0 my ( $self, $spacing ) = @_;
1341 0 $self->{_currentspacing} = $self->{_pu2px}->($spacing);
1342 0 delete( $self->{_bbcache} );
1343 0 delete( $self->{_struts} );
1344 }
1345  
1346 =over
1347  
1348 =item get_spacing
1349  
1350 Gets the current amount of spacing, in Pango units.
1351  
1352 =back
1353  
1354 =cut
1355  
1356 0 ▶sub get_spacing {
Subroutine get_spacing: not called CC=1 SCAR=6.9
Pod documented
1357 0 my ( $self ) = @_;
1358 0 $self->{_px2pu}->($self->{_currentspacing});
1359 }
1360  
1361 =over
1362  
1363 =item set_line_spacing( $factor )
1364  
1365 Sets a factor for line spacing. Typical values are: 0, 1, 1.5, 2. The
1366 default value is 0.
1367  
1368 If factor is non-zero, lines are placed so that
1369  
1370 baseline2 = baseline1 + factor * height2
1371  
1372 where height2 is the line height of the second line (as determined by
1373 the font(s)). In this case, the spacing set with set_spacing() is ignored.
1374  
1375 If factor is zero, spacing is applied as before.
1376  
1377 Not yet implemented.
1378  
1379 =back
1380  
1381 =cut
1382  
1383 0 ▶sub set_line_spacing {
Subroutine set_line_spacing: not called CC=1 SCAR=6.9
Pod documented
1384 0 my ( $self, $factor ) = @_;
1385 0 $self->{_currentlinespacing} = $factor;
1386 0 delete( $self->{_bbcache} );
1387 0 delete( $self->{_struts} );
1388 }
1389  
1390 =over
1391  
1392 =item get_line_spacing
1393  
1394 Gets the current line spacing factor.
1395  
1396 =back
1397  
1398 =cut
1399  
1400 0 ▶sub get_line_spacing {
Subroutine get_line_spacing: not called CC=2 SCAR=17.9
Pod documented
1401 0 my ( $self ) = @_;
1402 0 ▶ $self->{_currentlinespacing} || 0
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_currentlinespacing"} or 0 0 0 0
1403 ;
1404 }
1405  
1406 =over
1407  
1408 =item set_justify( $state )
1409  
1410 Sets whether each complete line should be stretched to fill the entire
1411 width of the layout. This stretching is typically done by adding
1412 whitespace.
1413  
1414 Not yet implemented.
1415  
1416 =back
1417  
1418 =cut
1419  
1420 0 ▶sub set_justify {
Subroutine set_justify: not called CC=1 SCAR=6.9
Pod documented
1421 0 my ( $self, $state ) = @_;
1422 0 $self->{_currentjustify} = !!$state;
1423 0 nyi();
1424 }
1425  
1426 =over
1427  
1428 =item get_justify
1429  
1430 Gets whether each complete line should be stretched to fill the entire
1431 width of the layout.
1432  
1433 =back
1434  
1435 =cut
1436  
1437 0 ▶sub get_justify {
Subroutine get_justify: not called CC=1 SCAR=6.9
Pod documented
1438 0 my ( $self ) = @_;
1439 0 $self->{_currentjustify};
1440 }
1441  
1442 =over
1443  
1444 =item set_auto_dir( $state )
1445  
1446 =item get_auto_dir
1447  
1448 Not supported.
1449  
1450 =back
1451  
1452 =cut
1453  
1454 0 ▶sub set_auto_dir { nyi() }
Subroutine set_auto_dir: not called CC=1 SCAR=6.9
Pod documented
1455  
1456 0 ▶sub get_auto_dir { nyi() }
Subroutine get_auto_dir: not called CC=1 SCAR=6.9
Pod documented
1457  
1458 =over
1459  
1460 =item set_alignment( $align )
1461  
1462 Sets the alignment for the layout: how partial lines are positioned
1463 within the horizontal space available.
1464  
1465 $align must be one of C<left>, C<center>, or C<right>,
1466  
1467 =back
1468  
1469 =cut
1470  
1471 0 ▶sub set_alignment {
Subroutine set_alignment: not called CC=2 SCAR=17.9
Pod documented
1472 0 my ( $self, $align ) = @_;
1473 0 ▶ if ( $align =~ /^(left|right|center)$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($align =~ /^(left|right|center)$/i) { } 0 0 0
1474 0 $self->{_alignment} = lc $align;
1475 }
1476 else {
1477 0 croak("Invalid alignment: \"$align\"");
1478 }
1479 0 delete( $self->{_bbcache} );
1480 0 delete( $self->{_struts} );
1481 }
1482  
1483 =over
1484  
1485 =item get_alignment
1486  
1487 Gets the alignment for this instance.
1488  
1489 =back
1490  
1491 =cut
1492  
1493 0 ▶sub get_align {
Subroutine get_align: not called CC=1 SCAR=6.9
Pod undocumented
1494 0 my ( $self ) = @_;
1495 0 $self->{_alignment};
1496 }
1497  
1498 =over
1499  
1500 =item set_tabs( $stops )
1501  
1502 =item get_tabs
1503  
1504 Not yet implemented.
1505  
1506 =back
1507  
1508 =cut
1509  
1510 0 ▶sub set_tabs { nyi() }
Subroutine set_tabs: not called CC=1 SCAR=6.9
Pod documented
1511  
1512 0 ▶sub get_tabs { nyi() }
Subroutine get_tabs: not called CC=1 SCAR=6.9
Pod documented
1513  
1514 =over
1515  
1516 =item set_single_paragraph_mode( $state )
1517  
1518 =item get_single_paragraph_mode
1519  
1520 Not yet implemented.
1521  
1522 =back
1523  
1524 =cut
1525  
1526 0 ▶sub set_single_paragraph_mode { nyi() }
Subroutine set_single_paragraph_mode: not called CC=1 SCAR=6.9
Pod documented
1527  
1528 0 ▶sub get_single_paragraph_mode { nyi() }
Subroutine get_single_paragraph_mode: not called CC=1 SCAR=6.9
Pod documented
1529  
1530 =over
1531  
1532 =item get_unknown_glyphs_count
1533  
1534 Counts the number unknown glyphs in the layout.
1535  
1536 Not yet implemented.
1537  
1538 =back
1539  
1540 =cut
1541  
1542 0 ▶sub get_unknown_glyphs_count { nyi() }
Subroutine get_unknown_glyphs_count: not called CC=1 SCAR=6.9
Pod documented
1543  
1544 =over
1545  
1546 =item get_log_attrs
1547  
1548 =item get_log_attrs_readonly
1549  
1550 Not implemented.
1551  
1552 =back
1553  
1554 =cut
1555  
1556 0 ▶sub get_log_attrs { nyi() }
Subroutine get_log_attrs: not called CC=1 SCAR=6.9
Pod documented
1557  
1558 0 ▶sub get_log_attrs_readonly { nyi() }
Subroutine get_log_attrs_readonly: not called CC=1 SCAR=6.9
Pod documented
1559  
1560 =over
1561  
1562 =item index_to_pos( $index )
1563  
1564 Converts from a character index within the layout to the onscreen position
1565 corresponding to the grapheme at that index, which is represented as
1566 rectangle.
1567  
1568 Not yet implemented.
1569  
1570 =back
1571  
1572 =cut
1573  
1574 0 ▶sub index_to_pos { nyi() }
Subroutine index_to_pos: not called CC=1 SCAR=6.9
Pod documented
1575  
1576 =over
1577  
1578 =item index_to_line_x ( $index )
1579  
1580 Converts from a character index within the layout to line and X position.
1581  
1582 Not yet implemented.
1583  
1584 =back
1585  
1586 =cut
1587  
1588 0 ▶sub index_to_line_x { nyi() }
Subroutine index_to_line_x: not called CC=1 SCAR=6.9
Pod documented
1589  
1590 =over
1591  
1592 =item xy_to_index ( $x, $y )
1593  
1594 Converts from $x,$y position to a character index within the layout.
1595  
1596 Not yet implemented.
1597  
1598 =back
1599  
1600 =cut
1601  
1602 0 ▶sub xy_to_index { nyi() }
Subroutine xy_to_index: not called CC=1 SCAR=6.9
Pod documented
1603  
1604 =over
1605  
1606 =item get_extents
1607  
1608 Computes the logical and ink extents of the layout.
1609  
1610 Logical extents are usually what you want for positioning things.
1611  
1612 Return value in scalar context is a hash ref with 4 values:
1613 C<x>, C<y>, C<width>, and C<height>
1614 describing the logical extents of the layout.
1615 In list context an array of two hashrefs is returned.
1616 The first reflects the ink extents, the second the logical extents.
1617  
1618 In the extents, C<x> will reflect the offset when text is centered or
1619 right aligned. It will be zero for left aligned text. For right
1620 aligned text, it will be the width of the layout.
1621  
1622 C<y> will reflect the offset when text is centered vertically or
1623 bottom aligned. It will be zero for top aligned text.
1624  
1625 See also get_pixel_extents below.
1626  
1627 Implementation note: If the PDF::API support layer cannot calculate ink,
1628 this function returns two identical extents.
1629  
1630 =back
1631  
1632 =cut
1633  
1634 3 ▶sub get_extents {
Subroutine get_extents: called CC=3 SCAR=11.1
Pod documented
1635 3 my ( $self ) = @_;
1636 3 my $need_ink = wantarray;
1637  
1638 3 my @bb = $self->get_bbox($need_ink);
1639 3 my $res = { bl => $bb[0],
1640 x => $bb[1], y => $bb[2],
1641 width => $bb[3], height => $bb[4] };
1642 3 my $ink = $res;
1643 3 ▶ if ( @bb > 5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bb > 5) 0 3 3
1644 0 $ink = { bl => $bb[0],
1645 x => $bb[5], y => $bb[6],
1646 width => $bb[7], height => $bb[8] };
1647 }
1648 3 ▶ return $need_ink ? ( $ink, $res ) : $res;
Branch: 1 branch
BranchTrueFalseTotal
$need_ink ? : 1 2 3
1649 }
1650  
1651 =over
1652  
1653 =item get_pixel_extents
1654  
1655 Same as get_extents, but using device units.
1656  
1657 The returned values are suitable for (assuming $pdf_text and
1658 $pdf_gfx are the PDF text and graphics contexts):
1659  
1660 $layout->render( $x, $y, $pdf_text );
1661 $box = $layout->get_pixel_extents;
1662 $pdf_gfx->translate( $x, $y );
1663 $pdf_gfx->rect( @$box{ qw( x y width height ) } );
1664 $pdf_gfx->stroke;
1665  
1666 =back
1667  
1668 =cut
1669  
1670 4 ▶sub get_pixel_extents {
Subroutine get_pixel_extents: called CC=3 SCAR=11.1
Pod documented
1671 4 my ( $self ) = @_;
1672  
1673 4 my $need_ink = wantarray;
1674  
1675 4 my @bb = $self->get_pixel_bbox($need_ink);
1676 4 my $res = { bl => $bb[0],
1677 x => $bb[1], y => $bb[2],
1678 width => $bb[3], height => $bb[4] };
1679 4 my $ink = $res;
1680 4 ▶ if ( @bb > 5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bb > 5) 0 4 4
1681 0 $ink = { bl => $bb[0],
1682 x => $bb[5], y => $bb[6],
1683 width => $bb[7], height => $bb[8] };
1684 }
1685 4 ▶ return $need_ink ? ( $ink, $res ) : $res;
Branch: 1 branch
BranchTrueFalseTotal
$need_ink ? : 1 3 4
1686 }
1687  
1688 =over
1689  
1690 =cut
1691  
1692 =item get_size
1693  
1694 Returns the width and height of this layout.
1695  
1696 In list context, width and height are returned as an two-element list.
1697 In scalar context a hashref with keys C<width> and C<height> is
1698 returned.
1699  
1700 =back
1701  
1702 =cut
1703  
1704 2 ▶sub get_size {
Subroutine get_size: called CC=2 SCAR=6.9
Pod documented
1705 2 my ( $self ) = @_;
1706 2 my $e = $self->get_extents;
1707 wantarray
1708 ? return ( $e->{width}, $e->{height} )
1709 2 ▶ : return { width => $e->{width}, height => $e->{height} };
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
1710 }
1711  
1712 =over
1713  
1714 =item get_pixel_size
1715  
1716 Same as get_size().
1717  
1718 =back
1719  
1720 =cut
1721  
1722 2 ▶sub get_pixel_size {
Subroutine get_pixel_size: called CC=2 SCAR=6.9
Pod documented
1723 2 my ( $self ) = @_;
1724 2 my $e = $self->get_pixel_extents;
1725 wantarray
1726 ? return ( $e->{width}, $e->{height} )
1727 2 ▶ : return { width => $e->{width}, height => $e->{height} };
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
1728 }
1729  
1730 =over
1731  
1732 =item get_iter
1733  
1734 Returns the layout for the first line.
1735  
1736 Implementation note: This is a dummy, it returns the layout.
1737 It is provided so you can write $layout->get_iter()->get_baseline()
1738 to be compatible with the official Pango API.
1739  
1740 =back
1741  
1742 =cut
1743  
1744 1 ▶sub get_iter { $_[0] }
Subroutine get_iter: called CC=1 SCAR=0.0
Pod documented
1745  
1746 =over
1747  
1748 =item get_baseline
1749  
1750 Gets the Y position of the baseline of the first line in this layout.
1751  
1752 Implementation note: Position is relative to top left, so due to the
1753 PDF coordinate system this is a negative number.
1754  
1755 Note: The Python API only supports this method on iteration objects.
1756 See get_iter().
1757  
1758 =back
1759  
1760 =cut
1761  
1762 2 ▶sub get_baseline {
Subroutine get_baseline: called CC=1 SCAR=0.0
Pod documented
1763 2 my ( $self ) = @_;
1764 2 return -$self->get_bbox->[0];
1765 }
1766  
1767 =head1 METHODS NOT IMPLEMENTED
1768  
1769 =over
1770  
1771 =item get_line_count
1772  
1773 =item get_line( $index )
1774  
1775 =item get_line_readonly( $index )
1776  
1777 =item get_lines
1778  
1779 =item get_lines_readonly
1780  
1781 =item line_get_extents
1782  
1783 =item line_get_pixel_entents
1784  
1785 =item line_index_to_x
1786  
1787 =item line_x_to_index
1788  
1789 =item line_get_x_ranges
1790  
1791 =item line_get_height
1792  
1793 =item get_cursor_pos
1794  
1795 =item move_cursor_visually
1796  
1797 =back
1798  
1799 =head2 ADDITIONAL METHODS
1800  
1801 The following methods are not part of the Pango API.
1802  
1803 =over
1804  
1805 =item set_font_size( $size )
1806  
1807 Sets the size for the current font.
1808  
1809 =back
1810  
1811 =cut
1812  
1813 0 ▶sub set_font_size {
Subroutine set_font_size: not called CC=1 SCAR=6.9
Pod documented
1814 0 my ( $self, $size ) = @_;
1815 0 $self->{_currentsize} = $size;
1816 0 delete( $self->{_bbcache} );
1817 0 delete( $self->{_struts} );
1818 }
1819  
1820 =over
1821  
1822 =item get_font_size
1823  
1824 Returns the size of the current font.
1825  
1826 =back
1827  
1828 =cut
1829  
1830 1 ▶sub get_font_size {
Subroutine get_font_size: called CC=1 SCAR=0.0
Pod documented
1831 1 my ( $self ) = @_;
1832 1 $self->{_currentsize};
1833 }
1834  
1835 =over
1836  
1837 =item get_bbox
1838  
1839 Returns the bounding box of the text, w.r.t. the (top-left) origin.
1840  
1841 bb = ( bl, x, y, width, height )
1842  
1843 bb[0] = baseline distance from the top.
1844  
1845 bb[1] = displacement from the left, nonzero for centered and right aligned text
1846  
1847 bb[2] = displacement from the top, usually zero
1848  
1849 bb[3] = advancewidth
1850  
1851 bb[4] = height
1852  
1853 Note that the bounding box will in general be equal to the font
1854 bounding box except for the advancewidth.
1855  
1856 NOTE: Some fonts do not include accents on capital letters in the ascend.
1857  
1858 If an argument is supplied and true, get_bbox() will attempt to
1859 calculate the ink extents as well, and add these as another set of 4
1860 elements,
1861  
1862 In list context returns the array of values, in scalar context an
1863 array ref.
1864  
1865 =back
1866  
1867 =cut
1868  
1869 10 ▶sub get_pixel_bbox {
Subroutine get_pixel_bbox: called CC=5 SCAR=16.1
Pod undocumented
1870 10 my ( $self, $all ) = @_;
1871 10 my $res;
1872 10 ▶ if ( $self->{_bbcache}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_bbcache'} and @{$self->{'_bbcache'};} == ($all ? 9 : 5)) { } 7 3 10
Condition100%3/3
$self->{"_bbcache"} and @{$self->{"_bbcache"};} == ($all ? 9 : 5)
!ll&&!rl&&r
127
MC/DC100%2/2
$self->{"_bbcache"} and @{$self->{"_bbcache"};} == ($all ? 9 : 5)
$self->{"_bbcache"} @{$self->{"_bbcache"};} == ($all ? 9 : 5)
Truth table100%3/3
A and B
A= $self->{"_bbcache"}B= @{$self->{"_bbcache"};} == ($all ? 9 : 5)
ABresult
0X0
100
111
1873 9 ▶ && @{ $self->{_bbcache} } == ($all ? 9 : 5) ) {
Branch: 1 branch
BranchTrueFalseTotal
$all ? : 2 7 9
1874 7 $res = $self->{_bbcache};
1875 }
1876 else {
1877 3 $res = $self->{_bbcache} = $self->bbox($all);
1878 }
1879 10 ▶ wantarray ? @$res : $res;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 10 0 10
1880 }
1881  
1882 5 ▶sub get_bbox {
Subroutine get_bbox: called CC=2 SCAR=6.9
Pod documented
1883 5 my ( $self, $all ) = @_;
1884 5 my @res = map { $self->{_px2pu}->($_) } ( $self->get_pixel_bbox($all) );
1885 5 ▶ wantarray ? @res : \@res;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 2 5
1886 }
1887  
1888 =over
1889  
1890 =item get_struts
1891  
1892 Returns the list of the struts in the layout, if any.
1893  
1894 Each element of the list is a hash, with key/value pairs for all the
1895 attributes of the corresponding C<< <strut/> >> markup item.
1896  
1897 Additionally, in each element there's a key C<_x> that contains the
1898 horizontal displacement of the strut relative to the star of the layout.
1899  
1900 In list context returns the array of values, in scalar context an
1901 array ref.
1902  
1903 =back
1904  
1905 =cut
1906  
1907 1 ▶sub get_struts {
Subroutine get_struts: called CC=3 SCAR=11.4
Pod documented
1908 1 my ( $self ) = @_;
1909 1 ▶ $self->bbox unless $self->{_struts};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_struts"} 1 0 1
1910 1 my $res = $self->{_struts};
1911 1 ▶ wantarray ? @$res : $res;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
1912 }
1913  
1914 =over
1915  
1916 =item align_struts( $other )
1917  
1918 Aligns the fragments in both layouts to each other, based on the struts.
1919  
1920 This will adjust the widths of the struts of both participants.
1921  
1922 =back
1923  
1924 =cut
1925  
1926 0 ▶sub align_struts {
Subroutine align_struts: not called CC=5 SCAR=34.0
Pod documented
1927 0 my ( $self, $other ) = @_;
1928 0 my @s1 = $self->get_struts;
1929 0 my @s2 = $other->get_struts;
1930 # warn("Struts mismatch\n") unless @s1 == @s2;
1931  
1932 # Accumulated displacements.
1933 0 my $dx1 = 0;
1934 0 my $dx2 = 0;
1935  
1936 # Process struts.
1937 0 for my $s1 ( @s1 ) {
1938 0 ▶ last unless @s2;
Branch: 1 branch
BranchTrueFalseTotal
unless @s2 0 0 0
1939  
1940 0 my $s2 = shift(@s2);
1941  
1942 # Displacement.
1943 0 my $d = ( $s1->{_x} + $dx1 ) - ( $s2->{_x} + $dx2 );
1944  
1945 # Adjust the smaller to the larger.
1946 0 ▶ if ( $d < 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($d < 0) { } 0 0 0
elsif ($d > 0) { } 0 0 0
1947 0 $s1->{_strut}->{width} -= $d;
1948 0 $dx1 -= $d;
1949 }
1950 elsif ( $d > 0 ) {
1951 0 $s2->{_strut}->{width} += $d;
1952 0 $dx2 += $d;
1953 }
1954 }
1955 }
1956  
1957 =over
1958  
1959 =item spread_struts
1960  
1961 Evenly distributes the available space over the struts, if any.
1962  
1963 =back
1964  
1965 =cut
1966  
1967 0 ▶sub spread_struts {
Subroutine spread_struts: not called CC=5 SCAR=34.0
Pod documented
1968 0 my ( $self, $width ) = @_;
1969 0 ▶ my $w = $width || $self->get_width;
Condition0%0/3
$width || $self->get_width
l!l&&r!l&&!r
000
MC/DC0%0/2
$width || $self->get_width
$width $self->get_width
Truth table0%0/3
A || B
A= $widthB= $self->get_width
ABresult
1X1
011
000
1970 0 ▶ return unless $w;
Branch: 1 branch
BranchTrueFalseTotal
unless $w 0 0 0
1971 0 my @s1 = $self->get_struts;
1972 0 ▶ return unless @s1;
Branch: 1 branch
BranchTrueFalseTotal
unless @s1 0 0 0
1973 0 my $dx1 = ( $w - $self->get_pixel_size->{width} ) / @s1;
1974  
1975 # Process struts.
1976 0 for my $s1 ( @s1 ) {
1977 0 $s1->{_strut}->{width} += $dx1;
1978 }
1979 }
1980  
1981 =over
1982  
1983 =item show( $x, $y, $text )
1984  
1985 Transfers the content of this layout instance to the designated
1986 graphics environment.
1987  
1988 Use this instead of Pango::Cairo::show_layout().
1989  
1990 For PDF::API2, $text must be an object created by the $page->text method.
1991  
1992 =back
1993  
1994 =cut
1995  
1996 1 ▶sub show {
Subroutine show: called CC=1 SCAR=0.0
Pod documented
1997 1 my ( $self, @data ) = @_;
1998 1 $self->render( @data );
1999 }
2000  
2001 =over
2002  
2003 =item set_pango_mode( $enable )
2004  
2005 Enable/disable Pango conformance mode.
2006 See L<Pango Conformance Mode>.
2007  
2008 Returns the internal Pango scaling factor if enabled.
2009  
2010 =back
2011  
2012 =cut
2013  
2014 0 ▶sub set_pango_mode {
Subroutine set_pango_mode: not called CC=4 SCAR=30.0
Pod documented
2015 0 my ( $self, $conformant ) = @_;
2016  
2017 0 ▶ if ( $conformant ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conformant) 0 0 0
2018 delete( $self->{_bbcache} ), delete( $self->{_struts} )
2019 0 ▶ unless $self->{_pango};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_pango"} 0 0 0
2020 0 ▶ $self->{_px2pu} = sub { $_[0] * PANGO_SCALE };
Subroutine __ANON__: not called CC=1 SCAR=6.9
2021 0 ▶ $self->{_pu2px} = sub { $_[0] / PANGO_SCALE };
Subroutine __ANON__: not called CC=1 SCAR=6.9
2022 0 return $self->{_pango} = PANGO_SCALE;
2023 }
2024  
2025 delete( $self->{_bbcache} ), delete( $self->{_struts} )
2026 0 ▶ if $self->{_pango};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_pango"} 0 0 0
2027 0 $self->{_px2pu} = \&px2pu;
2028 0 $self->{_pu2px} = \&pu2px;
2029 0 return $self->{_pango} = 0;
2030 }
2031  
2032 # Legacy.
2033 8 *set_pango_scale = \&set_pango_mode;
2034  
2035 =over
2036  
2037 =item get_pango
2038  
2039 See L<Pango Conformance Mode>.
2040  
2041 Returns the internal Pango scaling factor if conformance mode is
2042 enabled, otherwise it returns 1 (one).
2043  
2044 =back
2045  
2046 =cut
2047  
2048 0 ▶sub get_pango_scale {
Subroutine get_pango_scale: not called CC=2 SCAR=17.9
Pod undocumented
2049 0 my ( $self ) = @_;
2050 0 ▶ $self->{_pango} ? PANGO_SCALE : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_pango'} ? : 0 0 0
2051 }
2052  
2053 0 ▶sub nyi {
Subroutine nyi: not called CC=1 SCAR=6.9
Pod undocumented
2054 0 croak("Method \"" . (caller(1))[3] . "\" not implemented");
2055 }
2056  
2057 8 my $aliens;
2058 3 ▶sub register_element {
Subroutine register_element: called CC=3 SCAR=11.1
Pod undocumented
2059 3 my ( $self, $hd, @tags ) = @_;
2060 3 ▶ croak("Element handler for \"$tags[0]\" does not play nicely")
Branch: 1 branch
BranchTrueFalseTotal
unless $hd->DOES("Text::Layout::ElementRole") 0 3 3
2061 unless $hd->DOES(qw(Text::Layout::ElementRole));
2062 3 for ( @tags ) {
2063 3 $aliens->{$_} = $hd;
2064 }
2065 }
2066  
2067 8 ▶sub get_element_handler {
Subroutine get_element_handler: called CC=2 SCAR=7.1
Pod undocumented
2068 8 my ( $self, $tag ) = @_;
2069 8 ▶ return unless $tag;
Branch: 1 branch
BranchTrueFalseTotal
unless $tag 0 8 8
2070 8 $aliens->{$tag};
2071 }
2072  
2073 =over
2074  
2075 =item register_shortcode( $code, $span, %flags )
2076  
2077 Add user-defined shortcodes.
2078 This is just a replacement of <$code> -> <span $span>.
2079  
2080 When used as a class method, the shortcodes are accessible to all
2081 layout instances,
2082  
2083 Flags:
2084  
2085 remove => 1 -- remove this shortcode
2086  
2087 =back
2088  
2089 =cut
2090  
2091 3 ▶sub register_shortcode {
Subroutine register_shortcode: called CC=4 SCAR=13.9
Pod documented
2092 3 my ( $self, $key, $value, %flags ) = @_;
2093  
2094 3 ▶ my $ctl = ref($self) ? $self->{_sc} : \%shortcodes;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 2 1 3
2095 3 $key = lc($key);
2096  
2097 3 ▶ if ( $flags{remove} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags{'remove'}) { } 1 2 3
2098 1 ▶ delete($ctl->{$key}) // croak("No such shortcode: \"$key\"");
Condition50%1/2
delete $ctl->{$key} // croak(qq[No such shortcode: "$key"])
l!l
10
MC/DC0%0/1
delete $ctl->{$key} // croak(qq[No such shortcode: "$key"])
delete $ctl->{$key}
Truth table50%1/2
A
A= delete $ctl->{$key}
Aresult
11
00
2099 }
2100 else {
2101 2 $ctl->{$key} = $value;
2102 }
2103 }
2104  
2105 =head1 SEE ALSO
2106  
2107 Description of the Pango Markup Language:
2108 L<https://docs.gtk.org/Pango/pango_markup.html#pango-markup>.
2109  
2110 Documentation of the Pango Layout class:
2111 L<https://docs.gtk.org/Pango/class.Layout.html>.
2112  
2113 L<PDF::API2>, L<PDF::Builder>, L<HarfBuzz::Shaper>, L<Font::TTF>.
2114  
2115 =head1 AUTHOR
2116  
2117 Johan Vromans, C<< <JV at CPAN dot org> >>
2118  
2119 =head1 SUPPORT
2120  
2121 Development of this module takes place on GitHub:
2122 L<https://github.com/sciurius/perl-Text-Layout>.
2123  
2124 You can find documentation for this module with the perldoc command.
2125  
2126 perldoc Text::Layout
2127  
2128 Please report any bugs or feature requests using the issue tracker on
2129 GitHub.
2130  
2131 =head1 LICENSE
2132  
2133 Copyright (C) 2019,2024 Johan Vromans
2134  
2135 This module is free software. You can redistribute it and/or
2136 modify it under the terms of the Artistic License 2.0.
2137  
2138 This program is distributed in the hope that it will be useful,
2139 but without any warranty; without even the implied warranty of
2140 merchantability or fitness for a particular purpose.
2141  
2142 =cut
2143  
2144 8 1;
« Layout/PDFAPI2/ImageElement.pm ↑ Summary Layout/FontConfig.pm »