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

filter: Statementstmt 77.9% 191 / 245 Branchbran 54.3% 63 / 116 Conditioncond 51.1% 44 / 86 MC/DCmcdc 7.0% 4 / 57 Subroutinesub 84.6% 11 / 13 Podpod 50.0% 1 / 2 totaltotal 60.5% 314 / 519 CC 98 SCAR 38.4
CC
98
Cov
67%
_wrap_pre_line
72.0
_add_runs
64.4
add
58.5
CRAP 46.5
« blib/lib/PDF/Make/Signature.pm ↑ Summary blib/lib/PDF/Make/Builder/SignatureAppearance.pm »
1 package PDF::Make::Builder::Text;
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 53 ▶use Object::Proto;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 53 ▶use PDF::Make::Builder::Font;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 53 ▶use PDF::Make::Builder::Runs;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 53 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 53 Object::Proto::define('PDF::Make::Builder::Text',
10 'text:Str:required',
11 'align:Str:default(left)',
12 'indent:Int:default(0)',
13 'padding:Num:default(0)',
14 'spacing:Num:default(0)',
15 'pad:Str',
16 'pad_end:Str',
17 'margin:Num:default(5)',
18 'overflow:Bool:default(0)',
19 'preformatted:Bool:default(0)',
20 'font:HashRef',
21 'runs:ArrayRef',
22 'x:Num', 'y:Num', 'w:Num', 'h:Num',
23 'end_w:Num:default(0)',
24 'end_y:Num:default(0)',
25 );
26 53 Object::Proto::import_accessors('PDF::Make::Builder::Text');
27 }
28  
29 436 ▶sub _resolve_font {
Subroutine _resolve_font: called CC=8 SCAR=21.2
30 436 my ($self, $builder) = @_;
31 436 my $base = $builder->font;
32 436 my $overrides = font $self;
33 436 ▶ if ($overrides) {
Branch: 1 branch
BranchTrueFalseTotal
if ($overrides) 146 290 436
34 my %init = (
35 colour => $overrides->{colour} // $base->colour,
36 size => $overrides->{size} // $base->size,
37 family => $overrides->{family} // $base->family,
38 bold => $overrides->{bold} // $base->bold,
39 146 ▶ italic => $overrides->{italic} // $base->italic,
Condition66%10/15
$overrides->{"colour"} // $base->colour
l!l&&r!l&&!r
191270
$overrides->{"size"} // $base->size
l!l&&r!l&&!r
88580
$overrides->{"family"} // $base->family
l!l&&r!l&&!r
211250
$overrides->{"bold"} // $base->bold
l!l&&r!l&&!r
101360
$overrides->{"italic"} // $base->italic
l!l&&r!l&&!r
81380
MC/DC0%0/2
$overrides->{"colour"} // $base->colour
$overrides->{"colour"} $base->colour
MC/DC0%0/2
$overrides->{"size"} // $base->size
$overrides->{"size"} $base->size
MC/DC0%0/2
$overrides->{"family"} // $base->family
$overrides->{"family"} $base->family
MC/DC0%0/2
$overrides->{"bold"} // $base->bold
$overrides->{"bold"} $base->bold
MC/DC0%0/2
$overrides->{"italic"} // $base->italic
$overrides->{"italic"} $base->italic
Truth table66%2/3
A // B
A= $overrides->{"colour"}B= $base->colour
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $overrides->{"size"}B= $base->size
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $overrides->{"family"}B= $base->family
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $overrides->{"bold"}B= $base->bold
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $overrides->{"italic"}B= $base->italic
ABresult
1X1
011
000
40 registry => $base->registry,
41 );
42 146 my $lh = PDF::Make::Builder::Font->resolve_line_height($base, $overrides);
43 146 ▶ $init{line_height} = $lh if defined $lh;
Branch: 1 branch
BranchTrueFalseTotal
if defined $lh 121 25 146
44 146 return PDF::Make::Builder::Font->new(%init);
45 }
46 290 return $base;
47 }
48  
49 # Soft-wrap one physical line of preformatted text to $max_w, preserving all
50 # whitespace. Breaks at whitespace boundaries where possible; a single token
51 # wider than the line is broken between characters. Returns the wrapped
52 # segments (continuation segments start at the left margin, not re-indented).
53 0 ▶sub _wrap_pre_line {
Subroutine _wrap_pre_line: not called CC=8 SCAR=42.8
54 0 my ($font, $line, $max_w) = @_;
55 0 ▶ return ($line) if $font->measure_text($line) <= $max_w;
Branch: 1 branch
BranchTrueFalseTotal
if $font->measure_text($line) <= $max_w 0 0 0
56  
57 # Words and whitespace runs, everything preserved. Any atom that is itself
58 # wider than the line is pre-split into character-sized chunks.
59 0 my @atoms;
60 0 for my $a (grep { length } split /(\s+)/, $line) {
61 0 ▶ if ($font->measure_text($a) <= $max_w) {
Branch: 1 branch
BranchTrueFalseTotal
if ($font->measure_text($a) <= $max_w) { } 0 0 0
62 0 push @atoms, $a;
63 } else {
64 0 push @atoms, _char_chunks($font, $a, $max_w);
65 }
66 }
67  
68 0 my @out;
69 0 my $cur = '';
70 0 for my $a (@atoms) {
71 0 ▶ if ($cur eq '' || $font->measure_text($cur . $a) <= $max_w) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur eq '' or $font->measure_text($cur . $a) <= $max_w) { } 0 0 0
Condition0%0/3
$cur eq "" or $font->measure_text($cur . $a) <= $max_w
l!l&&r!l&&!r
000
MC/DC0%0/2
$cur eq "" or $font->measure_text($cur . $a) <= $max_w
$cur eq "" $font->measure_text($cur . $a) <= $max_w
Truth table0%0/3
A or B
A= $cur eq ""B= $font->measure_text($cur . $a) <= $max_w
ABresult
1X1
011
000
72 0 $cur .= $a;
73 } else {
74 0 push @out, $cur;
75 0 $cur = $a;
76 }
77 }
78 0 ▶ push @out, $cur if length $cur;
Branch: 1 branch
BranchTrueFalseTotal
if length $cur 0 0 0
79 0 return @out;
80 }
81  
82 # Break a string into the longest character prefixes that each fit in $max_w.
83 0 ▶sub _char_chunks {
Subroutine _char_chunks: not called CC=5 SCAR=34.0
84 0 my ($font, $s, $max_w) = @_;
85 0 my @chunks;
86 0 my $cur = '';
87 0 for my $ch (split //, $s) {
88 0 ▶ if ($cur eq '' || $font->measure_text($cur . $ch) <= $max_w) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur eq '' or $font->measure_text($cur . $ch) <= $max_w) { } 0 0 0
Condition0%0/3
$cur eq "" or $font->measure_text($cur . $ch) <= $max_w
l!l&&r!l&&!r
000
MC/DC0%0/2
$cur eq "" or $font->measure_text($cur . $ch) <= $max_w
$cur eq "" $font->measure_text($cur . $ch) <= $max_w
Truth table0%0/3
A or B
A= $cur eq ""B= $font->measure_text($cur . $ch) <= $max_w
ABresult
1X1
011
000
89 0 $cur .= $ch;
90 } else {
91 0 push @chunks, $cur;
92 0 $cur = $ch;
93 }
94 }
95 0 ▶ push @chunks, $cur if length $cur;
Branch: 1 branch
BranchTrueFalseTotal
if length $cur 0 0 0
96 0 return @chunks;
97 }
98  
99 # The plain string behind a run list: what tagged output, extraction and the
100 # accessibility tree see. Callers that build a Text directly with runs must
101 # still pass text (the property is required); Builder::add_text derives it.
102 6 ▶sub plain_text_from_runs {
Subroutine plain_text_from_runs: called CC=3 SCAR=11.4
Pod undocumented
103 6 my ($runs) = @_;
104 6 ▶ return '' unless ref $runs eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $runs eq "ARRAY" 0 6 6
105 6 ▶ return join '', map { defined $_->{text} ? $_->{text} : '' } @$runs;
Branch: 1 branch
BranchTrueFalseTotal
defined $_->{'text'} ? : 18 0 18
106 }
107  
108 # Resolve each run's style overrides against the block font, returning the
109 # list PDF::Make::Builder::Runs wants. Identical styles share one font object,
110 # which is what lets adjacent segments coalesce into a single measurement.
111 7 ▶sub _resolve_runs {
Subroutine _resolve_runs: called CC=12 SCAR=27.7
112 7 my ($self, $builder) = @_;
113 7 my $base = $self->_resolve_font($builder);
114 7 my %cache;
115 my @out;
116  
117 7 for my $run (@{ runs $self }) {
118 20 ▶ next unless defined $run->{text} && length $run->{text};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $run->{"text"} and length $run->{"text"} 0 20 20
Condition33%1/3
defined $run->{"text"} and length $run->{"text"}
!ll&&!rl&&r
0020
MC/DC0%0/2
defined $run->{"text"} and length $run->{"text"}
defined $run->{"text"} length $run->{"text"}
Truth table33%1/3
A and B
A= defined $run->{"text"}B= length $run->{"text"}
ABresult
0X0
100
111
119 my %spec = (
120 colour => $run->{colour} // $base->colour,
121 size => $run->{size} // $base->size,
122 family => $run->{family} // $base->family,
123 bold => $run->{bold} // $base->bold,
124 italic => $run->{italic} // $base->italic,
125 20 ▶ line_height => $run->{line_height} // $base->effective_line_height,
Condition61%11/18
$run->{"colour"} // $base->colour
l!l&&r!l&&!r
2180
$run->{"size"} // $base->size
l!l&&r!l&&!r
1190
$run->{"family"} // $base->family
l!l&&r!l&&!r
1190
$run->{"bold"} // $base->bold
l!l&&r!l&&!r
4160
$run->{"italic"} // $base->italic
l!l&&r!l&&!r
3170
$run->{"line_height"} // $base->effective_line_height
l!l&&r!l&&!r
0200
MC/DC0%0/2
$run->{"colour"} // $base->colour
$run->{"colour"} $base->colour
MC/DC0%0/2
$run->{"size"} // $base->size
$run->{"size"} $base->size
MC/DC0%0/2
$run->{"family"} // $base->family
$run->{"family"} $base->family
MC/DC0%0/2
$run->{"bold"} // $base->bold
$run->{"bold"} $base->bold
MC/DC0%0/2
$run->{"italic"} // $base->italic
$run->{"italic"} $base->italic
MC/DC0%0/2
$run->{"line_height"} // $base->effective_line_height
$run->{"line_height"} $base->effective_line_height
Truth table66%2/3
A // B
A= $run->{"colour"}B= $base->colour
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $run->{"size"}B= $base->size
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $run->{"family"}B= $base->family
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $run->{"bold"}B= $base->bold
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $run->{"italic"}B= $base->italic
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $run->{"line_height"}B= $base->effective_line_height
ABresult
1X1
011
000
126 registry => $base->registry,
127 );
128 20 ▶ my $key = join "\0", map { defined $spec{$_} ? $spec{$_} : '' }
Branch: 1 branch
BranchTrueFalseTotal
defined $spec{$_} ? : 120 0 120
129 qw(colour size family bold italic line_height);
130 20 ▶ $cache{$key} ||= PDF::Make::Builder::Font->new(%spec);
Condition66%2/3
$cache{$key} ||= "PDF::Make::Builder::Font"->new(%spec)
l!l&&r!l&&!r
3170
MC/DC0%0/2
$cache{$key} ||= "PDF::Make::Builder::Font"->new(%spec)
$cache{$key} "PDF::Make::Builder::Font"->new(%spec)
Truth table66%2/3
A ||= B
A= $cache{$key}B= "PDF::Make::Builder::Font"->new(%spec)
ABresult
1X1
011
000
131 20 push @out, { font => $cache{$key}, text => $run->{text} };
132 }
133 7 return \@out;
134 }
135  
136 436 ▶sub add {
Subroutine add: called CC=44 SCAR=40.7
Pod documented
137 436 my ($self, $builder) = @_;
138  
139 436 my $runs = runs $self;
140 436 ▶ return $self->_add_runs($builder) if ref $runs eq 'ARRAY' && @$runs;
Branch: 1 branch
BranchTrueFalseTotal
if ref $runs eq "ARRAY" and @$runs 7 429 436
Condition66%2/3
ref $runs eq "ARRAY" and @$runs
!ll&&!rl&&r
42907
MC/DC50%1/2
ref $runs eq "ARRAY" and @$runs
ref $runs eq "ARRAY" @$runs
Truth table66%2/3
A and B
A= ref $runs eq "ARRAY"B= @$runs
ABresult
0X0
100
111
141  
142 429 my $page = $builder->page;
143 429 my $canvas = $page->canvas;
144 429 my $font = $self->_resolve_font($builder);
145 429 my $res_name = $font->ensure_loaded($page->xs_page);
146 429 my $font_size = $font->size;
147 429 my $lh = $font->effective_line_height;
148 429 my $line_spacing = spacing $self;
149 429 ▶ $line_spacing = 0 if !defined($line_spacing) || $line_spacing < 0;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $line_spacing or $line_spacing < 0 0 429 429
Condition33%1/3
not defined $line_spacing or $line_spacing < 0
l!l&&r!l&&!r
00429
MC/DC0%0/2
not defined $line_spacing or $line_spacing < 0
not defined $line_spacing $line_spacing < 0
Truth table33%1/3
not(A) or B
A= not defined $line_spacingB= $line_spacing < 0
ABresult
1X1
011
000
150 429 my ($cr, $cg, $cb) = $font->hex_to_rgb($font->colour);
151  
152 429 my $pad = padding $self;
153 429 ▶ $pad = 0 if !defined($pad) || $pad < 0;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $pad or $pad < 0 0 429 429
Condition33%1/3
not defined $pad or $pad < 0
l!l&&r!l&&!r
00429
MC/DC0%0/2
not defined $pad or $pad < 0
not defined $pad $pad < 0
Truth table33%1/3
not(A) or B
A= not defined $padB= $pad < 0
ABresult
1X1
011
000
154  
155 429 ▶ my $text_w = ($self->w // $page->width) - (2 * $pad);
Condition66%2/3
$self->w // $page->width
l!l&&r!l&&!r
104190
MC/DC0%0/2
$self->w // $page->width
$self->w $page->width
Truth table66%2/3
A // B
A= $self->wB= $page->width
ABresult
1X1
011
000
156 429 ▶ $text_w = 1 if $text_w < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $text_w < 1 0 429 429
157 429 ▶ my $cx = ($self->x // $page->content_x) + $pad;
Condition66%2/3
$self->x // $page->content_x
l!l&&r!l&&!r
94200
MC/DC0%0/2
$self->x // $page->content_x
$self->x $page->content_x
Truth table66%2/3
A // B
A= $self->xB= $page->content_x
ABresult
1X1
011
000
158 429 my $cy = $self->y;
159  
160 # Explicit y is already in builder/PDF bottom-left coordinates
161 429 ▶ if (!defined $cy) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cy) 420 9 429
162 420 $cy = $page->cursor_y;
163 }
164 429 $cy -= $pad;
165  
166 # Apply indent
167 429 my $indent_w = 0;
168 429 my $ind = indent $self;
169 429 ▶ if ($ind > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ind > 0) 3 426 429
170 3 $indent_w = $font->space_width * $ind;
171 }
172  
173 # Build the list of physical lines to render. Each entry is
174 # [ line_text, line_width, is_first ].
175 429 my $raw = text $self;
176 429 my $preformatted = preformatted $self;
177  
178 429 my @lines;
179  
180 429 ▶ if ($preformatted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($preformatted) { } 0 429 429
181 # Verbatim: preserve hard newlines and all in-line whitespace (leading
182 # spaces indent via the space glyph's advance), expand tabs to four
183 # spaces, and never collapse runs of whitespace. Physical lines wider
184 # than the content width are soft-wrapped (at whitespace where
185 # possible, mid-token otherwise) so nothing runs off the page.
186 0 ▶ return $self unless length $raw;
Branch: 1 branch
BranchTrueFalseTotal
unless length $raw 0 0 0
187 0 for my $phys (split /\n/, $raw, -1) {
188 0 $phys =~ s/\t/ /g;
189 0 ▶ if ($phys eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($phys eq "") 0 0 0
190 0 push @lines, ['', 0, 0]; # blank line -> vertical space
191 0 next;
192 }
193 0 for my $seg (_wrap_pre_line($font, $phys, $text_w)) {
194 0 push @lines, [$seg, $font->measure_text($seg), 0];
195 }
196 }
197 # A trailing newline yields one empty line; drop it so a block that
198 # merely ends in "\n" doesn't gain a spurious blank line.
199 0 ▶ pop @lines if @lines > 1 && $lines[-1][0] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if @lines > 1 and $lines[-1][0] eq "" 0 0 0
Condition0%0/3
@lines > 1 and $lines[-1][0] eq ""
!ll&&!rl&&r
000
MC/DC0%0/2
@lines > 1 and $lines[-1][0] eq ""
@lines > 1 $lines[-1][0] eq ""
Truth table0%0/3
A and B
A= @lines > 1B= $lines[-1][0] eq ""
ABresult
0X0
100
111
200 }
201 else {
202 # Word-wrap: collapse whitespace and reflow to the content width.
203 429 my @words = split /\s+/, $raw;
204 429 ▶ return $self unless @words;
Branch: 1 branch
BranchTrueFalseTotal
unless @words 0 429 429
205  
206 429 my $line = '';
207 429 my $line_w = $indent_w;
208 429 my $first_line = 1;
209  
210 429 for my $word (@words) {
211 15246 ▶ my $candidate = $line eq '' ? $word : ($line . ' ' . $word);
Branch: 1 branch
BranchTrueFalseTotal
$line eq '' ? : 429 14817 15246
212 15246 my $candidate_w = $font->measure_text($candidate);
213 15246 ▶ my $test_w = $candidate_w + ($first_line ? $indent_w : 0);
Branch: 1 branch
BranchTrueFalseTotal
$first_line ? : 2908 12338 15246
214 15246 my $max_w = $text_w;
215  
216 15246 ▶ if ($test_w > $max_w && $line ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($test_w > $max_w and $line ne '') { } 674 14572 15246
Condition66%2/3
$test_w > $max_w and $line ne ""
!ll&&!rl&&r
145720674
MC/DC50%1/2
$test_w > $max_w and $line ne ""
$test_w > $max_w $line ne ""
Truth table66%2/3
A and B
A= $test_w > $max_wB= $line ne ""
ABresult
0X0
100
111
217 674 push @lines, [$line, $line_w, $first_line];
218 674 $first_line = 0;
219 674 $line = $word;
220 674 $line_w = $font->measure_text($line);
221 } else {
222 14572 $line = $candidate;
223 14572 $line_w = $test_w;
224 }
225 }
226 429 ▶ push @lines, [$line, $line_w, $first_line] if $line ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $line ne "" 429 0 429
227 }
228  
229 # Render lines. Preformatted text is always left-aligned; reflow-only
230 # features (alignment, dot-leader padding) don't apply to it.
231 429 ▶ my $al = $preformatted ? 'left' : align $self;
Branch: 1 branch
BranchTrueFalseTotal
$preformatted ? : 0 429 429
232 429 my $can_overflow = overflow $self;
233  
234 429 for my $idx (0 .. $#lines) {
235 1103 my $entry = $lines[$idx];
236 1103 my ($line_text, $lw, $is_first) = @$entry;
237  
238 # Check if we have room
239 1103 ▶ if ($cy - $lh < $page->bottom_y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cy - $lh < $page->bottom_y) 15 1088 1103
240 # Try next column first
241 15 ▶ if ($page->has_next_column) {
Branch: 2 branches
BranchTrueFalseTotal
if ($page->has_next_column) { } 3 12 15
elsif ($can_overflow) { } 4 8 12
242 3 $page->next_column;
243 3 $cx = $page->content_x + $pad;
244 3 $cy = $page->cursor_y - $pad;
245 3 $text_w = $page->width - (2 * $pad);
246 3 ▶ $text_w = 1 if $text_w < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $text_w < 1 0 3 3
247 } elsif ($can_overflow) {
248 # All columns full — overflow to new page
249 # Inherit settings from current page
250 4 my $cols = $page->columns;
251 4 my $psz = $page->page_size;
252 4 my $page_pad = $page->padding;
253 4 my $bg = $page->background;
254 4 $builder->add_page(
255 page_size => $psz,
256 padding => $page_pad,
257 columns => $cols,
258 background => $bg,
259 );
260 4 $page = $builder->page;
261 4 $canvas = $page->canvas;
262 4 $res_name = $font->ensure_loaded($page->xs_page);
263 4 $cx = $page->content_x + $pad;
264 4 $cy = $page->cursor_y - $pad;
265 4 $text_w = $page->width - (2 * $pad);
266 4 ▶ $text_w = 1 if $text_w < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $text_w < 1 0 4 4
267 } else {
268 8 last;
269 }
270 }
271  
272 # Baseline sits near top of the line slot (font_size below cursor).
273 # The full line_height advances the cursor to the bottom of the slot.
274 1095 my $baseline_y = $cy - $font_size;
275  
276 # Calculate x based on alignment
277 1095 my $tx = $cx;
278 1095 ▶ my $extra_indent = $is_first ? $indent_w : 0;
Branch: 1 branch
BranchTrueFalseTotal
$is_first ? : 421 674 1095
279 1095 ▶ if ($al eq 'center') {
Branch: 2 branches
BranchTrueFalseTotal
if ($al eq 'center') { } 6 1089 1095
elsif ($al eq 'right') { } 4 1085 1089
280 6 $tx = $cx + ($text_w - $lw) / 2;
281 } elsif ($al eq 'right') {
282 4 $tx = $cx + $text_w - $lw;
283 } else {
284 1085 $tx += $extra_indent;
285 }
286  
287 # Pad support (for TOC dot leaders)
288 1095 my $pad_char = pad $self;
289 1095 ▶ if ($pad_char && length($pad_char)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pad_char and length $pad_char) 4 1091 1095
Condition66%2/3
$pad_char and length $pad_char
!ll&&!rl&&r
109104
MC/DC50%1/2
$pad_char and length $pad_char
$pad_char length $pad_char
Truth table66%2/3
A and B
A= $pad_charB= length $pad_char
ABresult
0X0
100
111
290 4 ▶ my $pad_end_text = pad_end($self) // '';
Condition100%2/2
<custom op object_func_get> // ""
l!l
13
MC/DC100%1/1
<custom op object_func_get> // ""
<custom op object_func_get>
Truth table100%2/2
A
A= <custom op object_func_get>
Aresult
11
00
291 4 my $pad_w = $font->measure_word($pad_char);
292 4 ▶ my $end_w = length($pad_end_text) ? $font->measure_text($pad_end_text) : 0;
Branch: 1 branch
BranchTrueFalseTotal
length $pad_end_text ? : 1 3 4
293 4 my $gap = $text_w - $lw - $end_w;
294 4 ▶ if ($gap > $pad_w) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gap > $pad_w) 4 0 4
295 4 my $num_pads = int($gap / $pad_w);
296 4 $line_text .= ' ' . ($pad_char x $num_pads);
297 4 ▶ $line_text .= $pad_end_text if length($pad_end_text);
Branch: 1 branch
BranchTrueFalseTotal
if length $pad_end_text 1 3 4
298 }
299 }
300  
301 1095 my $show = $font->show_op;
302 1095 $canvas->BT
303 ->rg($cr, $cg, $cb)
304 ->Tf($res_name, $font_size)
305 ->Tm(1, 0, 0, 1, $tx, $baseline_y)
306 ->$show($font->encode($line_text))
307 ->ET;
308  
309 1095 $cy -= $lh;
310 1095 ▶ $cy -= $line_spacing if $idx < $#lines;
Branch: 1 branch
BranchTrueFalseTotal
if $idx < $#lines 674 421 1095
311 }
312  
313 # Update page cursor - spacing applies after the entire block
314 429 my $final_y = $cy - (margin $self) - $line_spacing - $pad;
315 429 $page->y($final_y);
316 429 ▶ if (@lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines) 429 0 429
317 429 end_w $self, $lines[-1][1];
318 429 end_y $self, $cy; # bottom of last line slot
319 }
320  
321 429 return $self;
322 }
323  
324 # Styled-run rendering. Deliberately a separate path from add() above: the
325 # single-font code there produces bytes that existing documents depend on,
326 # and the safest way to keep them is not to touch it. The two agree on where
327 # lines break for single-run input, which t/45-runs.t asserts directly.
328 7 ▶sub _add_runs {
Subroutine _add_runs: called CC=24 SCAR=41.6
329 7 my ($self, $builder) = @_;
330  
331 7 my $page = $builder->page;
332 7 my $canvas = $page->canvas;
333 7 my $rruns = $self->_resolve_runs($builder);
334 7 ▶ return $self unless @$rruns;
Branch: 1 branch
BranchTrueFalseTotal
unless @$rruns 0 7 7
335  
336 7 my $line_spacing = spacing $self;
337 7 ▶ $line_spacing = 0 if !defined($line_spacing) || $line_spacing < 0;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $line_spacing or $line_spacing < 0 0 7 7
Condition33%1/3
not defined $line_spacing or $line_spacing < 0
l!l&&r!l&&!r
007
MC/DC0%0/2
not defined $line_spacing or $line_spacing < 0
not defined $line_spacing $line_spacing < 0
Truth table33%1/3
not(A) or B
A= not defined $line_spacingB= $line_spacing < 0
ABresult
1X1
011
000
338  
339 7 my $pad = padding $self;
340 7 ▶ $pad = 0 if !defined($pad) || $pad < 0;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $pad or $pad < 0 0 7 7
Condition33%1/3
not defined $pad or $pad < 0
l!l&&r!l&&!r
007
MC/DC0%0/2
not defined $pad or $pad < 0
not defined $pad $pad < 0
Truth table33%1/3
not(A) or B
A= not defined $padB= $pad < 0
ABresult
1X1
011
000
341  
342 7 ▶ my $text_w = ($self->w // $page->width) - (2 * $pad);
Condition33%1/3
$self->w // $page->width
l!l&&r!l&&!r
070
MC/DC0%0/2
$self->w // $page->width
$self->w $page->width
Truth table33%1/3
A // B
A= $self->wB= $page->width
ABresult
1X1
011
000
343 7 ▶ $text_w = 1 if $text_w < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $text_w < 1 0 7 7
344 7 ▶ my $cx = ($self->x // $page->content_x) + $pad;
Condition33%1/3
$self->x // $page->content_x
l!l&&r!l&&!r
070
MC/DC0%0/2
$self->x // $page->content_x
$self->x $page->content_x
Truth table33%1/3
A // B
A= $self->xB= $page->content_x
ABresult
1X1
011
000
345 7 my $cy = $self->y;
346 7 ▶ $cy = $page->cursor_y if !defined $cy;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cy 7 0 7
347 7 $cy -= $pad;
348  
349 7 my $indent_w = 0;
350 7 my $ind = indent $self;
351 7 ▶ if ($ind > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ind > 0) 0 7 7
352 0 $indent_w = $rruns->[0]{font}->space_width * $ind;
353 }
354  
355 7 my $lines = PDF::Make::Builder::Runs->layout(
356 runs => $rruns,
357 width => $text_w,
358 indent_w => $indent_w,
359 );
360 7 ▶ return $self unless @$lines;
Branch: 1 branch
BranchTrueFalseTotal
unless @$lines 0 7 7
361  
362 7 my $al = align $self;
363 7 my $can_overflow = overflow $self;
364 7 my %ensured;
365  
366 7 for my $idx (0 .. $#$lines) {
367 7 my $line = $lines->[$idx];
368 7 my $lh = $line->{lh};
369  
370 7 ▶ if ($cy - $lh < $page->bottom_y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cy - $lh < $page->bottom_y) 0 7 7
371 0 ▶ if ($page->has_next_column) {
Branch: 2 branches
BranchTrueFalseTotal
if ($page->has_next_column) { } 0 0 0
elsif ($can_overflow) { } 0 0 0
372 0 $page->next_column;
373 0 $cx = $page->content_x + $pad;
374 0 $cy = $page->cursor_y - $pad;
375 0 $text_w = $page->width - (2 * $pad);
376 0 ▶ $text_w = 1 if $text_w < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $text_w < 1 0 0 0
377 } elsif ($can_overflow) {
378 0 $builder->add_page(
379 page_size => $page->page_size,
380 padding => $page->padding,
381 columns => $page->columns,
382 background => $page->background,
383 );
384 0 $page = $builder->page;
385 0 $canvas = $page->canvas;
386 0 %ensured = (); # font resources are per page
387 0 $cx = $page->content_x + $pad;
388 0 $cy = $page->cursor_y - $pad;
389 0 $text_w = $page->width - (2 * $pad);
390 0 ▶ $text_w = 1 if $text_w < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $text_w < 1 0 0 0
391 } else {
392 0 last;
393 }
394 }
395  
396 # One baseline per line, set by the tallest run on it, so a larger or
397 # bold segment sits on the same line as its neighbours instead of
398 # drifting.
399 7 my $baseline_y = $cy - $line->{size};
400  
401 7 my $tx = $cx;
402 7 ▶ if ($al eq 'center') {
Branch: 3 branches
BranchTrueFalseTotal
if ($al eq 'center') { } 0 7 7
elsif ($al eq 'right') { } 0 7 7
elsif ($line->{'is_first'}) { } 7 0 7
403 0 $tx = $cx + ($text_w - $line->{w}) / 2;
404 } elsif ($al eq 'right') {
405 0 $tx = $cx + $text_w - $line->{w};
406 } elsif ($line->{is_first}) {
407 7 $tx += $indent_w;
408 }
409  
410 7 $canvas->BT;
411 7 for my $seg (@{ $line->{segs} }) {
412 18 my $font = $seg->{font};
413 18 my $key = "$font";
414 18 ▶ my $res = $ensured{$key} ||= $font->ensure_loaded($page->xs_page);
Condition66%2/3
$ensured{$key} ||= $font->ensure_loaded($page->xs_page)
l!l&&r!l&&!r
1170
MC/DC0%0/2
$ensured{$key} ||= $font->ensure_loaded($page->xs_page)
$ensured{$key} $font->ensure_loaded($page->xs_page)
Truth table66%2/3
A ||= B
A= $ensured{$key}B= $font->ensure_loaded($page->xs_page)
ABresult
1X1
011
000
415 18 my ($r, $g, $b) = $font->hex_to_rgb($font->colour);
416 18 my $show = $font->show_op;
417 $canvas->rg($r, $g, $b)
418 ->Tf($res, $font->size)
419 ->Tm(1, 0, 0, 1, $tx, $baseline_y)
420 18 ->$show($font->encode($seg->{text}));
421 18 $tx += $seg->{w};
422 }
423 7 $canvas->ET;
424  
425 7 $cy -= $lh;
426 7 ▶ $cy -= $line_spacing if $idx < $#$lines;
Branch: 1 branch
BranchTrueFalseTotal
if $idx < $lines->$#* 0 7 7
427 }
428  
429 7 my $final_y = $cy - (margin $self) - $line_spacing - $pad;
430 7 $page->y($final_y);
431 7 end_w $self, $lines->[-1]{w};
432 7 end_y $self, $cy;
433  
434 7 return $self;
435 }
436  
437 53 1;
438  
439 __END__
« blib/lib/PDF/Make/Signature.pm ↑ Summary blib/lib/PDF/Make/Builder/SignatureAppearance.pm »