| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
$j < @$segs and $segs->[$j]{"font"} == $font
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 55 | 1229 | ▶ | if (@$segs && $segs->[-1]{font} == $font) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@$segs and $segs->[-1]{"font"} == $font
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
| |||||||||||||||||||||||||||||
| 56 | 1137 | $segs->[-1]{text} .= $sep . $word; | |||||||||||||||||||||||||||
| 57 | } else { | ||||||||||||||||||||||||||||
| 58 | 92 | ▶ | $segs->[-1]{text} .= $sep if @$segs && length $sep; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@$segs and length $sep
MC/DC50%1/2
@$segs and length $sep
@$segs
length $sep
Truth table66%2/3
A and B
A= @$segsB= length $sep
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 78 | 409 | ▶ | $lh = $fl if $fl > $lh; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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'} || []
MC/DC0%0/1
$args{'runs'} || []
$args{'runs'}
Truth table50%1/2
A
A= $args{'runs'}
| |||||||||||||||||||||||||||||
| 108 | 66 | ▶ | my $width = $args{width} || 1; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{'width'} || 1
MC/DC0%0/1
$args{'width'} || 1
$args{'width'}
Truth table50%1/2
A
A= $args{'width'}
| |||||||||||||||||||||||||||||
| 109 | 66 | ▶ | my $indent_w = $args{indent_w} || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{'indent_w'} || 0
MC/DC100%1/1
$args{'indent_w'} || 0
$args{'indent_w'}
Truth table100%2/2
A
A= $args{'indent_w'}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 1229 | ▶ | if ($cw > $width && @$segs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$cw > $width and @$segs
MC/DC100%2/2
$cw > $width and @$segs
$cw > $width
@$segs
Truth table100%3/3
A and B
A= $cw > $widthB= @$segs
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 66 | return \@lines; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 53 | 1; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | __END__ | ||||||||||||||||||||||||||||