blib/lib/PDF/Make/Builder/Runs.pm

filter: Statementstmt 96.8% 61 / 63 Branchbran 90.9% 20 / 22 Conditioncond 77.7% 14 / 18 MC/DCmcdc 54.5% 6 / 11 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 89.4% 110 / 123 CC 22 SCAR 18.0
CC
22
Cov
92%
layout
10.0
_append
6.0
_measure
4.1
CRAP 6.1
« blib/lib/PDF/Make/Crypt.pm ↑ Summary blib/lib/PDF/Make/Form.pm »
1 package PDF::Make::Builder::Runs;
2 53 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 53 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 53 our $VERSION = '0.15';
6  
7 # Line breaking across styled runs.
8 #
9 # A paragraph is a list of runs, each with its own font. Breaking has to
10 # happen across run boundaries - "the <b>total</b> due" is three runs and one
11 # sentence - so the width of a candidate line is the sum of its segments
12 # rather than one measurement.
13 #
14 # Adjacent segments sharing a font object are coalesced into a single string
15 # before measuring. That is not an optimisation: measure_text sums per-glyph
16 # advances in double precision, so measuring "a b c" as one string and as
17 # three pieces differ in the last few bits. Coalescing makes the single-run
18 # case measure exactly one string per candidate, which is what the legacy
19 # path in PDF::Make::Builder::Text does, so both produce identical breaks.
20 # t/45-runs.t holds that equality down.
21  
22 # Split a run into words, dropping the whitespace between them. Empty strings
23 # from leading whitespace are skipped; the legacy path tolerates them by
24 # accident, we do it on purpose.
25 97 ▶sub _words {
Subroutine _words: called CC=2 SCAR=7.1
26 97 my ($text) = @_;
27 97 ▶ return grep { length } split /\s+/, defined $text ? $text : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $text ? : 97 0 97
28 }
29  
30 # Width of a segment list, coalescing runs of the same font.
31 1615 ▶sub _measure {
Subroutine _measure: called CC=4 SCAR=14.2
32 1615 my ($segs) = @_;
33 1615 my $w = 0;
34 1615 my $i = 0;
35 1615 ▶ while ($i < @$segs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$segs) 1686 1615 3301
36 1686 my $font = $segs->[$i]{font};
37 1686 my $text = $segs->[$i]{text};
38 1686 my $j = $i + 1;
39 1686 ▶ while ($j < @$segs && $segs->[$j]{font} == $font) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @$segs and $segs->[$j]{"font"} == $font) 0 1686 1686
Condition66%2/3
$j < @$segs and $segs->[$j]{"font"} == $font
!ll&&!rl&&r
1615710
MC/DC0%0/2
$j < @$segs and $segs->[$j]{"font"} == $font
$j < @$segs $segs->[$j]{"font"} == $font
Truth table66%2/3
A and B
A= $j < @$segsB= $segs->[$j]{"font"} == $font
ABresult
0X0
100
111
40 0 $text .= $segs->[$j]{text};
41 0 $j++;
42 }
43 1686 $w += $font->measure_text($text);
44 1686 $i = $j;
45 }
46 1615 return $w;
47 }
48  
49 # Append a word to a segment list, with a separating space when the line is
50 # not empty. The space is measured in the font of the segment it follows,
51 # which keeps it inside that segment's coalesced string.
52 1229 ▶sub _append {
Subroutine _append: called CC=6 SCAR=17.9
53 1229 my ($segs, $font, $word) = @_;
54 1229 ▶ my $sep = @$segs ? ' ' : '';
Branch: 1 branch
BranchTrueFalseTotal
@$segs ? : 1164 65 1229
55 1229 ▶ if (@$segs && $segs->[-1]{font} == $font) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$segs and $segs->[-1]{'font'} == $font) { } 1137 92 1229
Condition100%3/3
@$segs and $segs->[-1]{"font"} == $font
!ll&&!rl&&r
65271137
MC/DC100%2/2
@$segs and $segs->[-1]{"font"} == $font
@$segs $segs->[-1]{"font"} == $font
Truth table100%3/3
A and B
A= @$segsB= $segs->[-1]{"font"} == $font
ABresult
0X0
100
111
56 1137 $segs->[-1]{text} .= $sep . $word;
57 } else {
58 92 ▶ $segs->[-1]{text} .= $sep if @$segs && length $sep;
Branch: 1 branch
BranchTrueFalseTotal
if @$segs and length $sep 27 65 92
Condition66%2/3
@$segs and length $sep
!ll&&!rl&&r
65027
MC/DC50%1/2
@$segs and length $sep
@$segs length $sep
Truth table66%2/3
A and B
A= @$segsB= length $sep
ABresult
0X0
100
111
59 92 push @$segs, { font => $font, text => $word };
60 }
61 1229 return $segs;
62 }
63  
64 1229 ▶sub _clone_segs {
Subroutine _clone_segs: called CC=1 SCAR=0.0
65 1229 my ($segs) = @_;
66 1229 return [ map { { font => $_->{font}, text => $_->{text} } } @$segs ];
67 }
68  
69 # Per-segment widths for rendering, plus the line's vertical metrics.
70 386 ▶sub _finish_line {
Subroutine _finish_line: called CC=4 SCAR=13.9
71 386 my ($segs, $is_first) = @_;
72 386 my ($size, $lh) = (0, 0);
73 386 for my $s (@$segs) {
74 409 $s->{w} = $s->{font}->measure_text($s->{text});
75 409 my $fs = $s->{font}->size;
76 409 my $fl = $s->{font}->effective_line_height;
77 409 ▶ $size = $fs if $fs > $size;
Branch: 1 branch
BranchTrueFalseTotal
if $fs > $size 389 20 409
78 409 ▶ $lh = $fl if $fl > $lh;
Branch: 1 branch
BranchTrueFalseTotal
if $fl > $lh 388 21 409
79 }
80 return {
81 386 segs => $segs,
82 w => _measure($segs),
83 is_first => $is_first,
84 size => $size,
85 lh => $lh,
86 };
87 }
88  
89 =head2 layout
90  
91 my $lines = PDF::Make::Builder::Runs->layout(
92 runs => [ { font => $font, text => 'Total ' }, ... ],
93 width => $content_w,
94 indent_w => $indent_w,
95 );
96  
97 Greedy first-fit, matching the legacy single-font algorithm word for word.
98 Returns an arrayref of lines, each a hashref of C<segs> (each with C<font>,
99 C<text> and C<w>), the total C<w>, C<is_first>, and the line's C<size> and
100 C<lh> - the largest font size and line height present on it, so a bold or
101 larger run makes room for itself rather than overprinting the line above.
102  
103 =cut
104  
105 66 ▶sub layout {
Subroutine layout: called CC=10 SCAR=23.0
Pod documented
106 66 my ($class, %args) = @_;
107 66 ▶ my $runs = $args{runs} || [];
Condition50%1/2
$args{'runs'} || []
l!l
660
MC/DC0%0/1
$args{'runs'} || []
$args{'runs'}
Truth table50%1/2
A
A= $args{'runs'}
Aresult
11
00
108 66 ▶ my $width = $args{width} || 1;
Condition50%1/2
$args{'width'} || 1
l!l
660
MC/DC0%0/1
$args{'width'} || 1
$args{'width'}
Truth table50%1/2
A
A= $args{'width'}
Aresult
11
00
109 66 ▶ my $indent_w = $args{indent_w} || 0;
Condition100%2/2
$args{'indent_w'} || 0
l!l
660
MC/DC100%1/1
$args{'indent_w'} || 0
$args{'indent_w'}
Truth table100%2/2
A
A= $args{'indent_w'}
Aresult
11
00
110  
111 66 my @lines;
112 66 my $segs = [];
113 66 my $first_line = 1;
114  
115 66 for my $run (@$runs) {
116 97 my $font = $run->{font};
117 97 for my $word (_words($run->{text})) {
118 1229 my $candidate = _clone_segs($segs);
119 1229 _append($candidate, $font, $word);
120 1229 ▶ my $cw = _measure($candidate) + ($first_line ? $indent_w : 0);
Branch: 1 branch
BranchTrueFalseTotal
$first_line ? : 400 829 1229
121  
122 1229 ▶ if ($cw > $width && @$segs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cw > $width and @$segs) { } 321 908 1229
Condition100%3/3
$cw > $width and @$segs
!ll&&!rl&&r
9035321
MC/DC100%2/2
$cw > $width and @$segs
$cw > $width @$segs
Truth table100%3/3
A and B
A= $cw > $widthB= @$segs
ABresult
0X0
100
111
123 321 push @lines, _finish_line($segs, $first_line);
124 321 $first_line = 0;
125 321 $segs = [ { font => $font, text => $word } ];
126 } else {
127 908 $segs = $candidate;
128 }
129 }
130 }
131 66 ▶ push @lines, _finish_line($segs, $first_line) if @$segs;
Branch: 1 branch
BranchTrueFalseTotal
if @$segs 65 1 66
132  
133 66 return \@lines;
134 }
135  
136 53 1;
137  
138 __END__
« blib/lib/PDF/Make/Crypt.pm ↑ Summary blib/lib/PDF/Make/Form.pm »