API2/Content.pm

filter: Statementstmt 74.6% 776 / 1040 Branchbran 58.2% 233 / 400 Conditioncond 35.2% 55 / 156 MC/DCmcdc 14.5% 14 / 96 Subroutinesub 87.6% 128 / 146 Podpod 78.2% 79 / 101 totaltotal 66.2% 1285 / 1939 CC 268 SCAR 38.1
CC
268
Cov
67%
textlabel
306.0
_makecolor
264.6
textstate
110.0
CRAP 45.1
« API2/Basic/PDF/Filter.pm ↑ Summary API2/Basic/PDF/Dict.pm »
1 package PDF::API2::Content;
2  
3 39 ▶use base 'PDF::API2::Basic::PDF::Dict';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 39 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 39 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 39 our $VERSION = '2.049'; # VERSION
9  
10 39 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 39 ▶use Compress::Zlib ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 39 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 39 ▶use Math::Trig;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 39 ▶use PDF::API2::Matrix;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 39 ▶use PDF::API2::Basic::PDF::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 39 ▶use PDF::API2::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 =head1 NAME
20  
21 PDF::API2::Content - Methods for adding graphics and text to a PDF
22  
23 =head1 SYNOPSIS
24  
25 # Start with a PDF page (new or opened)
26 my $pdf = PDF::API2->new();
27 my $page = $pdf->page();
28  
29 # Add a new content object
30 my $content = $page->graphics();
31 my $content = $page->text();
32  
33 # Then call the methods below to add graphics and text to the page.
34  
35 =cut
36  
37 122 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
38 122 my $class = shift();
39 122 my $self = $class->SUPER::new();
40  
41 122 $self->{' stream'} = '';
42 122 $self->{' poststream'} = '';
43 122 $self->{' font'} = undef;
44 122 $self->{' fontset'} = 0;
45 122 $self->{' fontsize'} = 0;
46 122 $self->{' charspace'} = 0;
47 122 $self->{' hscale'} = 100;
48 122 $self->{' wordspace'} = 0;
49 122 $self->{' leading'} = 0;
50 122 $self->{' rise'} = 0;
51 122 $self->{' render'} = 0;
52 122 $self->{' matrix'} = [1, 0, 0, 1, 0, 0];
53 122 $self->{' textmatrix'} = [1, 0, 0, 1, 0, 0];
54 122 $self->{' textlinematrix'} = [0, 0];
55 122 $self->{' textlinestart'} = 0;
56 122 $self->{' fillcolor'} = [0];
57 122 $self->{' strokecolor'} = [0];
58 122 $self->{' translate'} = [0, 0];
59 122 $self->{' scale'} = [1, 1];
60 122 $self->{' skew'} = [0, 0];
61 122 $self->{' rotate'} = 0;
62 122 $self->{' apiistext'} = 0;
63  
64 122 return $self;
65 }
66  
67 114 ▶sub outobjdeep {
Subroutine outobjdeep: called CC=3 SCAR=11.0
Pod documented
68 114 my $self = shift();
69 114 $self->textend();
70 114 ▶ if ($self->{'-docompress'} and $self->{'Filter'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"-docompress"} and $self->{"Filter"}) 6 108 114
Condition66%2/3
$self->{"-docompress"} and $self->{"Filter"}
!ll&&!rl&&r
10806
MC/DC50%1/2
$self->{"-docompress"} and $self->{"Filter"}
$self->{"-docompress"} $self->{"Filter"}
Truth table66%2/3
A and B
A= $self->{"-docompress"}B= $self->{"Filter"}
ABresult
0X0
100
111
71 6 $self->{' stream'} = Compress::Zlib::compress($self->{' stream'});
72 6 $self->{' nofilt'} = 1;
73 6 delete $self->{'-docompress'};
74 }
75 114 return $self->SUPER::outobjdeep(@_);
76 }
77  
78 =head1 COORDINATE TRANSFORMATIONS
79  
80 The methods in this section change the coordinate system for the current content
81 object relative to the rest of the document.
82  
83 Changes to the coordinate system only affect subsequent paths or text.
84  
85 A call to any of the methods in this section resets the coordinate system before
86 applying its changes, unless the C<relative> option is set.
87  
88 =head2 translate
89  
90 $content = $content->translate($x, $y);
91  
92 Moves the origin along the x and y axes.
93  
94 =cut
95  
96 12 ▶sub _translate {
Subroutine _translate: called CC=1 SCAR=0.0
97 12 my ($x, $y) = @_;
98 12 return (1, 0, 0, 1, $x, $y);
99 }
100  
101 2 ▶sub translate {
Subroutine translate: called CC=1 SCAR=0.0
Pod documented
102 2 my ($self, $x, $y) = @_;
103 2 $self->transform(translate => [$x, $y]);
104 }
105  
106 =head2 rotate
107  
108 $content = $content->rotate($degrees);
109  
110 Rotates the coordinate system counter-clockwise.
111  
112 Use a negative argument to rotate clockwise.
113  
114 =cut
115  
116 9 ▶sub _rotate {
Subroutine _rotate: called CC=1 SCAR=0.0
117 9 my $a = deg2rad(shift());
118 9 return (cos($a), sin($a), -sin($a), cos($a), 0, 0);
119 }
120  
121 1 ▶sub rotate {
Subroutine rotate: called CC=1 SCAR=0.0
Pod documented
122 1 my ($self, $a) = @_;
123 1 $self->transform(rotate => $a);
124 }
125  
126 =head2 scale
127  
128 $content = $content->scale($x, $y);
129  
130 Scales (stretches) the coordinate systems along the x and y axes. A value of 1
131 for either C<$x> or C<$y> represents 100% scale (i.e. no change).
132  
133 =cut
134  
135 9 ▶sub _scale {
Subroutine _scale: called CC=1 SCAR=0.0
136 9 my ($x, $y) = @_;
137 9 return ($x, 0, 0, $y, 0, 0);
138 }
139  
140 1 ▶sub scale {
Subroutine scale: called CC=1 SCAR=0.0
Pod documented
141 1 my ($self, $sx, $sy) = @_;
142 1 $self->transform(scale => [$sx, $sy]);
143 }
144  
145 =head2 skew
146  
147 $content = $content->skew($a, $b);
148  
149 Skews the coordinate system by C<$a> degrees (counter-clockwise) from the x axis
150 and C<$b> degrees (clockwise) from the y axis.
151  
152 =cut
153  
154 9 ▶sub _skew {
Subroutine _skew: called CC=1 SCAR=0.0
155 9 my $a = deg2rad(shift());
156 9 my $b = deg2rad(shift());
157 9 return (1, tan($a), tan($b), 1, 0, 0);
158 }
159  
160 1 ▶sub skew {
Subroutine skew: called CC=1 SCAR=0.0
Pod documented
161 1 my ($self, $a, $b) = @_;
162 1 $self->transform(skew => [$a, $b]);
163 }
164  
165 =head2 transform
166  
167 $content = $content->transform(
168 translate => [$x, $y],
169 rotate => $degrees,
170 scale => [$x, $y],
171 skew => [$a, $b],
172 repeat => $boolean,
173 );
174  
175 Performs multiple coordinate transformations, in the order recommended by the
176 PDF specification (translate, rotate, scale, then skew). Omitted options will
177 be unchanged.
178  
179 If C<repeat> is true and if this is not the first call to a transformation
180 method, the previous transformation will be performed again, modified by any
181 other provided arguments.
182  
183 =cut
184  
185 39 ▶sub _to_matrix {
Subroutine _to_matrix: called CC=1 SCAR=0.0
186 39 my @array = @_;
187 39 return PDF::API2::Matrix->new([$array[0], $array[1], 0],
188 [$array[2], $array[3], 0],
189 [$array[4], $array[5], 1]);
190 }
191  
192 15 ▶sub _transform {
Subroutine _transform: called CC=7 SCAR=20.1
193 15 my %opts = @_;
194 15 my $m = PDF::API2::Matrix->new([1, 0, 0], [0, 1, 0], [0, 0, 1]);
195  
196 # Undocumented; only used by textpos()
197 15 ▶ if (defined $opts{'-matrix'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"-matrix"}) 0 15 15
198 0 $m = $m->multiply(_to_matrix(@{$opts{'-matrix'}}));
199 }
200  
201 # Note that the transformations are applied in reverse order. See PDF 1.7
202 # specification section 8.3.4: Transformation Matrices.
203 15 ▶ if (defined $opts{'skew'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"skew"}) 9 6 15
204 9 $m = $m->multiply(_to_matrix(_skew(@{$opts{'skew'}})));
205 }
206 15 ▶ if (defined $opts{'scale'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"scale"}) 9 6 15
207 9 $m = $m->multiply(_to_matrix(_scale(@{$opts{'scale'}})));
208 }
209 15 ▶ if (defined $opts{'rotate'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"rotate"}) 9 6 15
210 9 $m = $m->multiply(_to_matrix(_rotate($opts{'rotate'})));
211 }
212 15 ▶ if (defined $opts{'translate'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"translate"}) 12 3 15
213 12 $m = $m->multiply(_to_matrix(_translate(@{$opts{'translate'}})));
214 }
215  
216 # Undocumented; only used by textpos()
217 15 ▶ if ($opts{'-point'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"-point"}) 0 15 15
218 my $mp = PDF::API2::Matrix->new([$opts{'-point'}->[0],
219 0 $opts{'-point'}->[1], 1]);
220 0 $mp = $mp->multiply($m);
221 0 return ($mp->[0][0], $mp->[0][1]);
222 }
223  
224 return (
225 15 $m->[0][0], $m->[0][1],
226 $m->[1][0], $m->[1][1],
227 $m->[2][0], $m->[2][1]
228 );
229 }
230  
231 # Transformations are described in the PDF 1.7 specification, section 8.3.3:
232 # Common Transformations.
233 16 ▶sub transform {
Subroutine transform: called CC=10 SCAR=23.1
Pod documented
234 16 my ($self, %options) = @_;
235 16 ▶ return $self->transform_rel(%options) if $options{'repeat'};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"repeat"} 0 16 16
236  
237 # Deprecated (renamed to 'repeat' to avoid confusion)
238 16 ▶ return $self->transform_rel(%options) if $options{'relative'};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"relative"} 1 15 16
239  
240 # Deprecated options (remove hyphens)
241 15 foreach my $option (qw(translate rotate scale skew)) {
242 60 ▶ if (exists $options{'-' . $option}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options{'-' . $option}) 14 46 60
243 14 ▶ $options{$option} //= delete $options{'-' . $option};
Condition33%1/3
$options{$option} //= delete $options{'-' . $option}
l!l&&r!l&&!r
0140
MC/DC0%0/2
$options{$option} //= delete $options{'-' . $option}
$options{$option} delete $options{'-' . $option}
Truth table33%1/3
A //= B
A= $options{$option}B= delete $options{'-' . $option}
ABresult
1X1
011
000
244 }
245 }
246  
247 # Apply the transformations
248 15 $self->matrix(_transform(%options));
249  
250 # Store the transformations for lookup or future relative transformations
251 15 ▶ $self->{' translate'} = $options{'translate'} // [0, 0];
Condition100%2/2
$options{"translate"} // [0, 0]
l!l
123
MC/DC100%1/1
$options{"translate"} // [0, 0]
$options{"translate"}
Truth table100%2/2
A
A= $options{"translate"}
Aresult
11
00
252 15 ▶ $self->{' rotate'} = $options{'rotate'} // 0;
Condition100%2/2
$options{"rotate"} // 0
l!l
96
MC/DC100%1/1
$options{"rotate"} // 0
$options{"rotate"}
Truth table100%2/2
A
A= $options{"rotate"}
Aresult
11
00
253 15 ▶ $self->{' scale'} = $options{'scale'} // [1, 1];
Condition100%2/2
$options{"scale"} // [1, 1]
l!l
96
MC/DC100%1/1
$options{"scale"} // [1, 1]
$options{"scale"}
Truth table100%2/2
A
A= $options{"scale"}
Aresult
11
00
254 15 ▶ $self->{' skew'} = $options{'skew'} // [0, 0];
Condition100%2/2
$options{"skew"} // [0, 0]
l!l
96
MC/DC100%1/1
$options{"skew"} // [0, 0]
$options{"skew"}
Truth table100%2/2
A
A= $options{"skew"}
Aresult
11
00
255  
256 15 return $self;
257 }
258  
259 2 ▶sub transform_rel {
Subroutine transform_rel: called CC=8 SCAR=21.3
Pod documented
260 2 my ($self, %options) = @_;
261  
262 # Deprecated options (remove hyphens)
263 2 foreach my $option (qw(translate rotate scale skew)) {
264 8 ▶ if (exists $options{'-' . $option}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options{'-' . $option}) 4 4 8
265 4 ▶ $options{$option} //= delete $options{'-' . $option};
Condition33%1/3
$options{$option} //= delete $options{'-' . $option}
l!l&&r!l&&!r
040
MC/DC0%0/2
$options{$option} //= delete $options{'-' . $option}
$options{$option} delete $options{'-' . $option}
Truth table33%1/3
A //= B
A= $options{$option}B= delete $options{'-' . $option}
ABresult
1X1
011
000
266 }
267 }
268  
269 2 ▶ my ($sa1, $sb1) = @{$options{'skew'} ? $options{'skew'} : [0, 0]};
Branch: 1 branch
BranchTrueFalseTotal
$options{'skew'} ? : 2 0 2
270 2 my ($sa0, $sb0) = @{$self->{' skew'}};
271  
272 2 ▶ my ($sx1, $sy1) = @{$options{'scale'} ? $options{'scale'} : [1, 1]};
Branch: 1 branch
BranchTrueFalseTotal
$options{'scale'} ? : 2 0 2
273 2 my ($sx0, $sy0) = @{$self->{' scale'}};
274  
275 2 ▶ my $r1 = $options{'rotate'} // 0;
Condition50%1/2
$options{"rotate"} // 0
l!l
20
MC/DC0%0/1
$options{"rotate"} // 0
$options{"rotate"}
Truth table50%1/2
A
A= $options{"rotate"}
Aresult
11
00
276 2 my $r0 = $self->{' rotate'};
277  
278 2 ▶ my ($tx1, $ty1) = @{$options{'translate'} ? $options{'translate'} : [0, 0]};
Branch: 1 branch
BranchTrueFalseTotal
$options{'translate'} ? : 2 0 2
279 2 my ($tx0, $ty0) = @{$self->{' translate'}};
280  
281 2 $self->transform(
282 skew => [$sa0 + $sa1, $sb0 + $sb1],
283 scale => [$sx0 * $sx1, $sy0 * $sy1],
284 rotate => $r0 + $r1,
285 translate => [$tx0 + $tx1, $ty0 + $ty1],
286 );
287  
288 2 return $self;
289 }
290  
291 =head2 matrix
292  
293 $graphics = $graphics->matrix($a, $b, $c, $d, $e, $f);
294  
295 ($a, $b, $c, $d, $e, $f) = $text->matrix($a, $b, $c, $d, $e, $f);
296  
297 Sets the current transformation matrix manually. Unless you have a particular
298 need to enter transformations manually, you should use the C<transform> method
299 instead.
300  
301 The return value differs based on whether the caller is a graphics content
302 object or a text content object.
303  
304 =cut
305  
306 3 ▶sub _matrix_text {
Subroutine _matrix_text: called CC=1 SCAR=0.0
307 3 my ($a, $b, $c, $d, $e, $f) = @_;
308 3 return (floats($a, $b, $c, $d, $e, $f), 'Tm');
309 }
310  
311 23 ▶sub _matrix_gfx {
Subroutine _matrix_gfx: called CC=1 SCAR=0.0
312 23 my ($a, $b, $c, $d, $e, $f) = @_;
313 23 return (floats($a, $b, $c, $d, $e, $f), 'cm');
314 }
315  
316 26 ▶sub matrix {
Subroutine matrix: called CC=4 SCAR=13.9
Pod documented
317 26 my $self = shift();
318 26 ▶ if (scalar(@_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 26 0 26
319 26 my ($a, $b, $c, $d, $e, $f) = @_;
320 26 ▶ if ($self->_in_text_object()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_in_text_object) { } 3 23 26
321 3 $self->add(_matrix_text($a, $b, $c, $d, $e, $f));
322 3 $self->{' textmatrix'} = [$a, $b, $c, $d, $e, $f];
323 3 $self->{' textlinematrix'} = [0, 0];
324 }
325 else {
326 23 $self->add(_matrix_gfx($a, $b, $c, $d, $e, $f));
327 }
328 }
329  
330 26 ▶ if ($self->_in_text_object()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_in_text_object) { } 3 23 26
331 3 return @{$self->{' textmatrix'}};
332 }
333 else {
334 23 return $self;
335 }
336 }
337  
338 70 ▶sub matrix_update {
Subroutine matrix_update: called CC=1 SCAR=0.0
Pod undocumented
339 70 my ($self, $tx, $ty) = @_;
340 70 $self->{' textlinematrix'}->[0] += $tx;
341 70 $self->{' textlinematrix'}->[1] += $ty;
342 70 return $self;
343 }
344  
345 =head1 GRAPHICS STATE
346  
347 =head2 save
348  
349 $content = $content->save();
350  
351 Saves the current graphics state on a stack.
352  
353 =cut
354  
355 11 ▶sub _save {
Subroutine _save: called CC=1 SCAR=0.0
356 11 return 'q';
357 }
358  
359 11 ▶sub save {
Subroutine save: called CC=2 SCAR=7.9
Pod documented
360 11 my $self = shift;
361 11 ▶ if ($self->_in_text_object()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_in_text_object) 0 11 11
362 0 carp 'Calling save from a text content object has no effect';
363 0 return;
364 }
365  
366 11 $self->add(_save());
367  
368 11 return $self;
369 }
370  
371 =head2 restore
372  
373 $content = $content->restore();
374  
375 Restores the most recently saved graphics state, removing it from the stack.
376  
377 =cut
378  
379 11 ▶sub _restore {
Subroutine _restore: called CC=1 SCAR=0.0
380 11 return 'Q';
381 }
382  
383 11 ▶sub restore {
Subroutine restore: called CC=2 SCAR=7.9
Pod documented
384 11 my $self = shift;
385 11 ▶ if ($self->_in_text_object()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_in_text_object) 0 11 11
386 0 carp 'Calling restore from a text content object has no effect';
387 0 return;
388 }
389  
390 11 $self->add(_restore());
391  
392 11 return $self;
393 }
394  
395 =head2 line_width
396  
397 $content = $content->line_width($points);
398  
399 Sets the width of the stroke in points.
400  
401 =cut
402  
403 88 ▶sub _linewidth {
Subroutine _linewidth: called CC=1 SCAR=0.0
404 88 my $linewidth = shift();
405 88 return ($linewidth, 'w');
406 }
407  
408 # Deprecated (renamed)
409 87 ▶sub linewidth { return line_width(@_) }
Subroutine linewidth: called CC=1 SCAR=0.0
Pod documented
410  
411 88 ▶sub line_width {
Subroutine line_width: called CC=1 SCAR=0.0
Pod documented
412 88 my ($self, $line_width) = @_;
413  
414 88 $self->add(_linewidth($line_width));
415  
416 88 return $self;
417 }
418  
419 =head2 line_cap
420  
421 $content = $content->line_cap($style);
422  
423 Sets the shape that will be used at the ends of open subpaths (and dashes, if
424 any) when they are stroked.
425  
426 =over
427  
428 =item * "butt" or 0 = Butt Cap, default
429  
430 The stroke ends at the end of the path, with no projection.
431  
432 =item * "round" or 1 = Round Cap)
433  
434 An arc is drawn around the end of the path with a diameter equal to the line
435 width, and is filled in.
436  
437 =item * "square" or 2 = Projecting Square Cap
438  
439 The stroke continues past the end of the path for half the line width.
440  
441 =back
442  
443 =cut
444  
445 2 ▶sub _linecap {
Subroutine _linecap: called CC=1 SCAR=0.0
446 2 my $linecap = shift();
447 2 return ($linecap, 'J');
448 }
449  
450 # Deprecated (renamed)
451 1 ▶sub linecap { return line_cap(@_) }
Subroutine linecap: called CC=1 SCAR=0.0
Pod documented
452  
453 2 ▶sub line_cap {
Subroutine line_cap: called CC=10 SCAR=28.7
Pod documented
454 2 my $self = shift();
455  
456 2 ▶ if ($self->{' graphics'} and not @_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{" graphics"} and not @_) 0 2 2
Condition66%2/3
$self->{" graphics"} and not @_
!ll&&!rl&&r
110
MC/DC0%0/2
$self->{" graphics"} and not @_
$self->{" graphics"} not @_
Truth table66%2/3
A and not(B)
A= $self->{" graphics"}B= not @_
ABresult
0X0
100
111
457 0 croak "Missing argument to line_cap";
458 }
459  
460 2 ▶ my $style = shift() // 0;
Condition50%1/2
shift() // 0
l!l
20
MC/DC0%0/1
shift() // 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
461 2 ▶ $style = 0 if $style eq 'butt';
Branch: 1 branch
BranchTrueFalseTotal
if $style eq "butt" 0 2 2
462 2 ▶ $style = 1 if $style eq 'round';
Branch: 1 branch
BranchTrueFalseTotal
if $style eq "round" 1 1 2
463 2 ▶ $style = 2 if $style eq 'square';
Branch: 1 branch
BranchTrueFalseTotal
if $style eq "square" 0 2 2
464  
465 2 ▶ unless ($style >= 0 and $style <= 2) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($style >= 0 and $style <= 2) 0 2 2
Condition33%1/3
$style >= 0 and $style <= 2
!ll&&!rl&&r
002
MC/DC0%0/2
$style >= 0 and $style <= 2
$style >= 0 $style <= 2
Truth table33%1/3
A and B
A= $style >= 0B= $style <= 2
ABresult
0X0
100
111
466 0 ▶ if ($self->{' graphics'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{' graphics'}) { } 0 0 0
467 0 croak "Unknown line cap style \"$style\"";
468 }
469 else {
470 0 confess "Unknown line cap style \"$style\"";
471 }
472 }
473  
474 2 $self->add(_linecap($style));
475  
476 2 return $self;
477 }
478  
479 =head2 line_join
480  
481 $content = $content->line_join($style);
482  
483 Sets the style of join to be used at corners of a path.
484  
485 =over
486  
487 =item * "miter" or 0 = Miter Join, default
488  
489 The outer edges of the stroke extend until they meet, up to the limit specified
490 below. If the limit would be surpassed, a bevel join is used instead.
491  
492 =item * "round" or 1 = Round Join
493  
494 A circle with a diameter equal to the linewidth is drawn around the corner
495 point, producing a rounded corner.
496  
497 =item * "bevel" or 2 = Bevel Join
498  
499 A triangle is drawn to fill in the notch between the two strokes.
500  
501 =back
502  
503 =cut
504  
505 2 ▶sub _linejoin {
Subroutine _linejoin: called CC=1 SCAR=0.0
506 2 my $linejoin = shift();
507 2 return ($linejoin, 'j');
508 }
509  
510 # Deprecated (renamed)
511 1 ▶sub linejoin { return line_join(@_) }
Subroutine linejoin: called CC=1 SCAR=0.0
Pod documented
512  
513 2 ▶sub line_join {
Subroutine line_join: called CC=10 SCAR=28.7
Pod documented
514 2 my $self = shift();
515  
516 2 ▶ if ($self->{' graphics'} and not @_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{" graphics"} and not @_) 0 2 2
Condition66%2/3
$self->{" graphics"} and not @_
!ll&&!rl&&r
110
MC/DC0%0/2
$self->{" graphics"} and not @_
$self->{" graphics"} not @_
Truth table66%2/3
A and not(B)
A= $self->{" graphics"}B= not @_
ABresult
0X0
100
111
517 0 croak "Missing argument to line_join";
518 }
519  
520 2 ▶ my $style = shift() // 0;
Condition50%1/2
shift() // 0
l!l
20
MC/DC0%0/1
shift() // 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
521 2 ▶ $style = 0 if $style eq 'miter';
Branch: 1 branch
BranchTrueFalseTotal
if $style eq "miter" 0 2 2
522 2 ▶ $style = 1 if $style eq 'round';
Branch: 1 branch
BranchTrueFalseTotal
if $style eq "round" 0 2 2
523 2 ▶ $style = 2 if $style eq 'bevel';
Branch: 1 branch
BranchTrueFalseTotal
if $style eq "bevel" 1 1 2
524  
525 2 ▶ unless ($style >= 0 and $style <= 2) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($style >= 0 and $style <= 2) 0 2 2
Condition33%1/3
$style >= 0 and $style <= 2
!ll&&!rl&&r
002
MC/DC0%0/2
$style >= 0 and $style <= 2
$style >= 0 $style <= 2
Truth table33%1/3
A and B
A= $style >= 0B= $style <= 2
ABresult
0X0
100
111
526 0 ▶ if ($self->{' graphics'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{' graphics'}) { } 0 0 0
527 0 croak "Unknown line join style \"$style\"";
528 }
529 else {
530 0 confess "Unknown line join style \"$style\"";
531 }
532 }
533  
534 2 $self->add(_linejoin($style));
535  
536 2 return $self;
537 }
538  
539 =head2 miter_limit
540  
541 $content = $content->miter_limit($ratio);
542  
543 Sets the miter limit when the line join style is a miter join.
544  
545 The C<$ratio> is the maximum length of the miter (inner to outer corner) divided
546 by the line width. Any miter above this ratio will be converted to a bevel
547 join. The practical effect is that lines meeting at shallow angles are chopped
548 off instead of producing long pointed corners.
549  
550 There is no documented default miter limit.
551  
552 =cut
553  
554 3 ▶sub _miterlimit {
Subroutine _miterlimit: called CC=1 SCAR=0.0
555 3 my $limit = shift();
556 3 return ($limit, 'M');
557 }
558  
559 # Deprecated; miterlimit was originally named incorrectly
560 1 ▶sub meterlimit { return miter_limit(@_) }
Subroutine meterlimit: called CC=1 SCAR=0.0
Pod documented
561  
562 # Deprecated (renamed)
563 1 ▶sub miterlimit { return miter_limit(@_) }
Subroutine miterlimit: called CC=1 SCAR=0.0
Pod documented
564  
565 3 ▶sub miter_limit {
Subroutine miter_limit: called CC=1 SCAR=0.0
Pod documented
566 3 my ($self, $limit) = @_;
567  
568 3 $self->add(_miterlimit($limit));
569  
570 3 return $self;
571 }
572  
573 =head2 line_dash_pattern
574  
575 # Solid line
576 $content = $content->line_dash_pattern();
577  
578 # Equal length lines and gaps
579 $content = $content->line_dash_pattern($length);
580  
581 # Specified line and gap lengths
582 $content = $content->line_dash_pattern($line1, $gap1, $line2, $gap2, ...);
583  
584 # Offset the starting point
585 $content = $content->line_dash_pattern(
586 pattern => [$line1, $gap1, $line2, $gap2, ...],
587 offset => $points,
588 );
589  
590 Sets the line dash pattern.
591  
592 If called without any arguments, a solid line will be drawn.
593  
594 If called with one argument, the dashes and gaps will have equal lengths.
595  
596 If called with two or more arguments, the arguments represent alternating dash
597 and gap lengths.
598  
599 If called with a hash of arguments, a dash phase may be set, which specifies the
600 distance into the pattern at which to start the dash.
601  
602 =cut
603  
604 10 ▶sub _linedash {
Subroutine _linedash: called CC=13 SCAR=46.2
605 10 my @options = @_;
606  
607 10 ▶ unless (@options) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@options) 7 3 10
608 7 return ('[', ']', '0', 'd');
609 }
610  
611 3 ▶ if ($options[0] =~ /^\d/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options[0] =~ /^\d/) 3 0 3
612 3 return ('[', floats(@options), '] 0 d');
613 }
614  
615 0 my %options = @options;
616  
617 # Deprecated option names
618 0 ▶ if ($options{'-pattern'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"-pattern"}) 0 0 0
619 0 ▶ $options{'pattern'} //= delete $options{'-pattern'};
Condition0%0/3
$options{"pattern"} //= delete $options{"-pattern"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"pattern"} //= delete $options{"-pattern"}
$options{"pattern"} delete $options{"-pattern"}
Truth table0%0/3
A //= B
A= $options{"pattern"}B= delete $options{"-pattern"}
ABresult
1X1
011
000
620 }
621 0 ▶ if ($options{'-shift'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"-shift"}) 0 0 0
622 0 ▶ $options{'offset'} //= delete $options{'-shift'};
Condition0%0/3
$options{"offset"} //= delete $options{"-shift"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"offset"} //= delete $options{"-shift"}
$options{"offset"} delete $options{"-shift"}
Truth table0%0/3
A //= B
A= $options{"offset"}B= delete $options{"-shift"}
ABresult
1X1
011
000
623 }
624  
625 # Deprecated: the -full and -clear options will be removed in a future
626 # release
627 0 ▶ if (exists $options{'-full'} or exists $options{'-clear'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options{"-full"} or exists $options{"-clear"}) 0 0 0
Condition0%0/3
exists $options{"-full"} or exists $options{"-clear"}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $options{"-full"} or exists $options{"-clear"}
exists $options{"-full"} exists $options{"-clear"}
Truth table0%0/3
A or B
A= exists $options{"-full"}B= exists $options{"-clear"}
ABresult
1X1
011
000
628 0 ▶ $options{'pattern'} //= [$options{'-full'} // 0, $options{'-clear'} // 0];
Condition0%0/6
$options{"pattern"} //= [$options{"-full"} // 0, $options{"-clear"} // 0]
l!l
00
$options{"-full"} // 0
l!l
00
$options{"-clear"} // 0
l!l
00
MC/DC0%0/1
$options{"pattern"} //= [$options{"-full"} // 0, $options{"-clear"} // 0]
$options{"pattern"}
MC/DC0%0/1
$options{"-full"} // 0
$options{"-full"}
MC/DC0%0/1
$options{"-clear"} // 0
$options{"-clear"}
Truth table0%0/2
A
A= $options{"pattern"}
Aresult
11
00
Truth table0%0/2
A
A= $options{"-full"}
Aresult
11
00
Truth table0%0/2
A
A= $options{"-clear"}
Aresult
11
00
629 }
630  
631 0 ▶ return ('[', floats(@{$options{'pattern'}}), ']',
Condition0%0/2
$options{'offset'} || 0
l!l
00
MC/DC0%0/1
$options{'offset'} || 0
$options{'offset'}
Truth table0%0/2
A
A= $options{'offset'}
Aresult
11
00
632 0 ($options{'offset'} || 0), 'd');
633 }
634  
635 # Deprecated (renamed)
636 7 ▶sub linedash { return line_dash_pattern(@_) }
Subroutine linedash: called CC=1 SCAR=0.0
Pod documented
637  
638 10 ▶sub line_dash_pattern {
Subroutine line_dash_pattern: called CC=1 SCAR=0.0
Pod documented
639 10 my ($self, @a) = @_;
640  
641 10 $self->add(_linedash(@a));
642  
643 10 return $self;
644 }
645  
646 =head2 flatness_tolerance
647  
648 $content = $content->flatness_tolerance($tolerance);
649  
650 Sets the maximum distance in device pixels between the mathematically correct
651 path for a curve and an approximation constructed from straight line segments.
652  
653 C<$tolerance> is an integer between 0 and 100, where 0 represents the device's
654 default flatness tolerance.
655  
656 =cut
657  
658 2 ▶sub _flatness {
Subroutine _flatness: called CC=1 SCAR=0.0
659 2 my $flatness = shift();
660 2 return ($flatness, 'i');
661 }
662  
663 # Deprecated (renamed)
664 1 ▶sub flatness { return flatness_tolerance(@_) }
Subroutine flatness: called CC=1 SCAR=0.0
Pod documented
665  
666 2 ▶sub flatness_tolerance {
Subroutine flatness_tolerance: called CC=1 SCAR=0.0
Pod documented
667 2 my ($self, $flatness) = @_;
668  
669 2 $self->add(_flatness($flatness));
670  
671 2 return $self;
672 }
673  
674 =head2 egstate
675  
676 $content = $content->egstate($object);
677  
678 Adds a L<PDF::API2::Resource::ExtGState> object containing a set of graphics
679 state parameters.
680  
681 =cut
682  
683 0 ▶sub egstate {
Subroutine egstate: not called CC=1 SCAR=6.9
Pod documented
684 0 my ($self, $egstate) = @_;
685 0 $self->add('/' . $egstate->name(), 'gs');
686 0 $self->resource('ExtGState', $egstate->name(), $egstate);
687 0 return $self;
688 }
689  
690 =head1 PATH CONSTRUCTION (DRAWING)
691  
692 Note that paths will not appear until a path painting method is called
693 (L</"stroke">, L</"fill">, or L</"paint">).
694  
695 =head2 move
696  
697 $content = $content->move($x, $y);
698  
699 Starts a new path at the specified coordinates.
700  
701 =cut
702  
703 0 ▶sub _move {
Subroutine _move: not called CC=1 SCAR=6.9
704 0 my ($x, $y) =@_;
705 0 return (floats($x, $y), 'm');
706 }
707  
708 107 ▶sub move {
Subroutine move: called CC=3 SCAR=11.1
Pod documented
709 107 my $self = shift();
710 107 my ($x, $y);
711 107 ▶ while (defined($x = shift())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($x = shift())) 107 107 214
712 107 $y = shift();
713 107 ▶ if ($self->_in_text_object()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_in_text_object) { } 0 107 107
714 0 $self->add_post(floats($x, $y), 'm');
715 }
716 else {
717 107 $self->add(floats($x, $y), 'm');
718 }
719 107 $self->{' x'} = $self->{' mx'} = $x;
720 107 $self->{' y'} = $self->{' my'} = $y;
721 }
722 107 return $self;
723 }
724  
725 =head2 line
726  
727 $content = $content->line($x, $y);
728  
729 Extends the path in a line from the current coordinates to the specified
730 coordinates.
731  
732 =cut
733  
734 0 ▶sub _line {
Subroutine _line: not called CC=1 SCAR=6.9
735 0 my ($x, $y) = @_;
736 0 return (floats($x, $y), 'l');
737 }
738  
739 95 ▶sub line {
Subroutine line: called CC=3 SCAR=11.1
Pod documented
740 95 my $self = shift();
741 95 my ($x, $y);
742 95 ▶ while (defined($x = shift())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($x = shift())) 96 95 191
743 96 $y = shift();
744 96 ▶ if ($self->_in_text_object()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_in_text_object) { } 0 96 96
745 0 $self->add_post(floats($x, $y), 'l');
746 }
747 else {
748 96 $self->add(floats($x, $y), 'l');
749 }
750 96 $self->{' x'} = $x;
751 96 $self->{' y'} = $y;
752 }
753 95 return $self;
754 }
755  
756 =head2 hline
757  
758 $content = $content->hline($x);
759  
760 Extends the path in a horizontal line from the current position to the specified
761 x coordinate.
762  
763 =cut
764  
765 2 ▶sub hline {
Subroutine hline: called CC=2 SCAR=7.2
Pod documented
766 2 my ($self, $x) = @_;
767 2 $self->{' x'} = $x;
768 2 ▶ if ($self->_in_text_object()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_in_text_object) { } 0 2 2
769 0 $self->add_post(floats($x, $self->{' y'}), 'l');
770 }
771 else {
772 2 $self->add(floats($x, $self->{' y'}), 'l');
773 }
774 2 return $self;
775 }
776  
777 =head2 vline
778  
779 $content = $content->vline($x);
780  
781 Extends the path in a vertical line from the current position to the specified y
782 coordinate.
783  
784 =cut
785  
786 1 ▶sub vline {
Subroutine vline: called CC=2 SCAR=7.2
Pod documented
787 1 my ($self, $y) = @_;
788 1 ▶ if ($self->_in_text_object()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_in_text_object) { } 0 1 1
789 0 $self->add_post(floats($self->{' x'}, $y), 'l');
790 }
791 else {
792 1 $self->add(floats($self->{' x'}, $y), 'l');
793 }
794 1 $self->{' y'} = $y;
795 1 return $self;
796 }
797  
798 =head2 polyline
799  
800 $content = $content->polyline($x1, $y1, $x2, $y2, ...);
801  
802 Extends the path from the current position in one or more straight lines.
803  
804 =cut
805  
806 2 ▶sub polyline {
Subroutine polyline: called CC=3 SCAR=11.1
Pod documented
807 2 my $self = shift();
808 2 ▶ unless (@_ % 2 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ % 2 == 0) 0 2 2
809 0 croak 'polyline requires pairs of coordinates';
810 }
811  
812 2 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 2 5
813 3 my $x = shift();
814 3 my $y = shift();
815 3 $self->line($x, $y);
816 }
817  
818 2 return $self;
819 }
820  
821 # Deprecated; replace with move and polyline. Deprecated because poly breaks
822 # the convention followed by every other path-drawing method (other than
823 # enclosed shapes) of extending the path from the current position.
824 2 ▶sub poly {
Subroutine poly: called CC=1 SCAR=0.0
Pod documented
825 2 my $self = shift();
826 2 my $x = shift();
827 2 my $y = shift();
828 2 $self->move($x, $y);
829 2 $self->line(@_);
830 2 return $self;
831 }
832  
833 =head2 curve
834  
835 $content = $content->curve($cx1, $cy1, $cx2, $cy2, $x, $y);
836  
837 Extends the path in a curve from the current point to C<($x, $y)>, using the two
838 specified points to create a cubic Bezier curve.
839  
840 =cut
841  
842 78 ▶sub curve {
Subroutine curve: called CC=3 SCAR=11.0
Pod documented
843 78 my $self = shift();
844 78 my ($x1, $y1, $x2, $y2, $x3, $y3);
845 78 ▶ while (defined($x1 = shift())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($x1 = shift())) 78 78 156
846 78 $y1 = shift();
847 78 $x2 = shift();
848 78 $y2 = shift();
849 78 $x3 = shift();
850 78 $y3 = shift();
851 78 ▶ if ($self->_in_text_object()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_in_text_object) { } 0 78 78
852 0 $self->add_post(floats($x1, $y1, $x2, $y2, $x3, $y3), 'c');
853 }
854 else {
855 78 $self->add(floats($x1, $y1, $x2, $y2, $x3, $y3), 'c');
856 }
857 78 $self->{' x'} = $x3;
858 78 $self->{' y'} = $y3;
859 }
860 78 return $self;
861 }
862  
863 =head2 spline
864  
865 $content = $content->spline($cx1, $cy1, $x, $y);
866  
867 Extends the path in a curve from the current point to C<($x, $y)>, using the two
868 specified points to create a spline.
869  
870 =cut
871  
872 1 ▶sub spline {
Subroutine spline: called CC=2 SCAR=6.9
Pod documented
873 1 my $self = shift();
874  
875 1 ▶ while (scalar @_ >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ >= 4) 1 1 2
876 1 my $cx = shift();
877 1 my $cy = shift();
878 1 my $x = shift();
879 1 my $y = shift();
880 1 my $c1x = (2 * $cx + $self->{' x'}) / 3;
881 1 my $c1y = (2 * $cy + $self->{' y'}) / 3;
882 1 my $c2x = (2 * $cx + $x) / 3;
883 1 my $c2y = (2 * $cy + $y) / 3;
884 1 $self->curve($c1x, $c1y, $c2x, $c2y, $x, $y);
885 }
886 }
887  
888 =head2 arc
889  
890 $content = $content->arc($x, $y, $major, $minor, $a, $b);
891  
892 Extends the path along an arc of an ellipse centered at C<[$x, $y]>. C<$major>
893 and C<$minor> represent the axes of the ellipse, and the arc moves from C<$a>
894 degrees to C<$b> degrees.
895  
896 =cut
897  
898 # Private
899 144 ▶sub arctocurve {
Subroutine arctocurve: called CC=2 SCAR=6.9
Pod undocumented
900 144 my ($a, $b, $alpha, $beta) = @_;
901 144 ▶ if (abs($beta - $alpha) > 30) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $beta - $alpha > 30) { } 68 76 144
902 return (
903 68 arctocurve($a, $b, $alpha, ($beta + $alpha) / 2),
904 arctocurve($a, $b, ($beta + $alpha) / 2, $beta)
905 );
906 }
907 else {
908 76 $alpha = ($alpha * pi / 180);
909 76 $beta = ($beta * pi / 180);
910  
911 76 my $bcp = (4.0 / 3 * (1 - cos(($beta - $alpha) / 2)) / sin(($beta - $alpha) / 2));
912 76 my $sin_alpha = sin($alpha);
913 76 my $sin_beta = sin($beta);
914 76 my $cos_alpha = cos($alpha);
915 76 my $cos_beta = cos($beta);
916  
917 76 my $p0_x = $a * $cos_alpha;
918 76 my $p0_y = $b * $sin_alpha;
919 76 my $p1_x = $a * ($cos_alpha - $bcp * $sin_alpha);
920 76 my $p1_y = $b * ($sin_alpha + $bcp * $cos_alpha);
921 76 my $p2_x = $a * ($cos_beta + $bcp * $sin_beta);
922 76 my $p2_y = $b * ($sin_beta - $bcp * $cos_beta);
923 76 my $p3_x = $a * $cos_beta;
924 76 my $p3_y = $b * $sin_beta;
925  
926 76 return ($p0_x, $p0_y, $p1_x, $p1_y, $p2_x, $p2_y, $p3_x, $p3_y);
927 }
928 }
929  
930 4 ▶sub arc {
Subroutine arc: called CC=3 SCAR=11.0
Pod documented
931 4 my ($self, $x, $y, $a, $b, $alpha, $beta, $move) = @_;
932 4 my @points = arctocurve($a, $b, $alpha, $beta);
933 4 my ($p0_x, $p0_y, $p1_x, $p1_y, $p2_x, $p2_y, $p3_x, $p3_y);
934  
935 4 $p0_x = $x + shift(@points);
936 4 $p0_y = $y + shift(@points);
937  
938 # Deprecated
939 4 ▶ $self->move($p0_x, $p0_y) if $move;
Branch: 1 branch
BranchTrueFalseTotal
if $move 3 1 4
940  
941 4 ▶ while (scalar @points) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @points) 40 4 44
942 40 $p1_x = $x + shift(@points);
943 40 $p1_y = $y + shift(@points);
944 40 $p2_x = $x + shift(@points);
945 40 $p2_y = $y + shift(@points);
946 40 $p3_x = $x + shift(@points);
947 40 $p3_y = $y + shift(@points);
948 40 $self->curve($p1_x, $p1_y, $p2_x, $p2_y, $p3_x, $p3_y);
949 40 shift(@points);
950 40 shift(@points);
951 40 $self->{' x'} = $p3_x;
952 40 $self->{' y'} = $p3_y;
953 }
954 4 return $self;
955 }
956  
957 # Extends the path along an arc of a circle of the specified radius from
958 # C<[x1,y1]> to C<[x2,y2]>.
959 #
960 # Set C<$move> to a true value if this arc is the beginning of a new path
961 # instead of the continuation of an existing path.
962 #
963 # Set C<$outer> to a true value to draw the larger arc between the two points
964 # instead of the smaller one.
965 #
966 # Set C<$reverse> to a true value to draw the mirror image of the specified arc.
967 #
968 # C<$radius * 2> cannot be smaller than the distance from C<[x1,y1]> to
969 # C<[x2,y2]>.
970  
971 # Deprecated; recreate using arc (Bogen is German for arc)
972 4 ▶sub bogen {
Subroutine bogen: called CC=15 SCAR=27.3
Pod undocumented
973 4 my ($self, $x1, $y1, $x2, $y2, $r, $move, $larc, $spf) = @_;
974 4 my ($p0_x, $p0_y, $p1_x, $p1_y, $p2_x, $p2_y, $p3_x, $p3_y);
975 4 my $x = $x2 - $x1;
976 4 my $y = $y2 - $y1;
977 4 my $z = sqrt($x ** 2 + $y ** 2);
978 4 my $alpha_rad = asin($y / $z);
979  
980 4 ▶ $alpha_rad += pi / 2 if $x < 0 and $y > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $x < 0 and $y > 0 0 4 4
Condition33%1/3
$x < 0 and $y > 0
!ll&&!rl&&r
400
MC/DC0%0/2
$x < 0 and $y > 0
$x < 0 $y > 0
Truth table33%1/3
A and B
A= $x < 0B= $y > 0
ABresult
0X0
100
111
981 4 ▶ $alpha_rad -= pi / 2 if $x < 0 and $y < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $x < 0 and $y < 0 0 4 4
Condition33%1/3
$x < 0 and $y < 0
!ll&&!rl&&r
400
MC/DC0%0/2
$x < 0 and $y < 0
$x < 0 $y < 0
Truth table33%1/3
A and B
A= $x < 0B= $y < 0
ABresult
0X0
100
111
982  
983 4 my $alpha = rad2deg($alpha_rad);
984 # use the complement angle for span
985 4 ▶ $alpha -= 180 if $spf and $spf > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $spf and $spf > 0 1 3 4
Condition66%2/3
$spf and $spf > 0
!ll&&!rl&&r
301
MC/DC50%1/2
$spf and $spf > 0
$spf $spf > 0
Truth table66%2/3
A and B
A= $spfB= $spf > 0
ABresult
0X0
100
111
986  
987 4 my $d = 2 * $r;
988 4 my ($beta, $beta_rad, @points);
989  
990 4 $beta = rad2deg(2 * asin($z / $d));
991 4 ▶ $beta = 360 - $beta if $larc and $larc > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $larc and $larc > 0 1 3 4
Condition66%2/3
$larc and $larc > 0
!ll&&!rl&&r
301
MC/DC50%1/2
$larc and $larc > 0
$larc $larc > 0
Truth table66%2/3
A and B
A= $larcB= $larc > 0
ABresult
0X0
100
111
992  
993 4 $beta_rad = deg2rad($beta);
994  
995 4 @points = arctocurve($r, $r, 90 + $alpha + $beta / 2, 90 + $alpha - $beta / 2);
996  
997 4 ▶ if ($spf and $spf > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spf and $spf > 0) 1 3 4
Condition66%2/3
$spf and $spf > 0
!ll&&!rl&&r
301
MC/DC50%1/2
$spf and $spf > 0
$spf $spf > 0
Truth table66%2/3
A and B
A= $spfB= $spf > 0
ABresult
0X0
100
111
998 1 my @pts = @points;
999 1 @points = ();
1000 1 ▶ while ($y = pop(@pts)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y = pop @pts) 15 1 16
1001 15 $x = pop(@pts);
1002 15 push(@points, $x, $y);
1003 }
1004 }
1005  
1006 4 $p0_x = shift(@points);
1007 4 $p0_y = shift(@points);
1008 4 $x = $x1 - $p0_x;
1009 4 $y = $y1 - $p0_y;
1010  
1011 4 ▶ $self->move($x1, $y1) if $move;
Branch: 1 branch
BranchTrueFalseTotal
if $move 1 3 4
1012  
1013 4 ▶ while (scalar @points) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @points) 36 4 40
1014 36 $p1_x = $x + shift(@points);
1015 36 $p1_y = $y + shift(@points);
1016 36 $p2_x = $x + shift(@points);
1017 36 $p2_y = $y + shift(@points);
1018 # if we run out of data points, use the end point instead
1019 36 ▶ if (scalar @points == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @points == 0) { } 1 35 36
1020 1 $p3_x = $x2;
1021 1 $p3_y = $y2;
1022 }
1023 else {
1024 35 $p3_x = $x + shift(@points);
1025 35 $p3_y = $y + shift(@points);
1026 }
1027 36 $self->curve($p1_x, $p1_y, $p2_x, $p2_y, $p3_x, $p3_y);
1028 36 shift(@points);
1029 36 shift(@points);
1030 }
1031 4 return $self;
1032 }
1033  
1034 =head2 close
1035  
1036 $content = $content->close();
1037  
1038 Closes the current path by extending a line from the current position to the
1039 starting position.
1040  
1041 =cut
1042  
1043 13 ▶sub close {
Subroutine close: called CC=1 SCAR=0.0
Pod documented
1044 13 my $self = shift();
1045 13 $self->add('h');
1046 13 $self->{' x'} = $self->{' mx'};
1047 13 $self->{' y'} = $self->{' my'};
1048 13 return $self;
1049 }
1050  
1051 =head1 SHAPE CONSTRUCTION (DRAWING)
1052  
1053 The following are convenience methods for drawing closed paths.
1054  
1055 Note that shapes will not appear until a path painting method is called
1056 (L</"stroke">, L</"fill">, or L</"paint">).
1057  
1058 =head2 rectangle
1059  
1060 $content = $content->rectangle($x1, $y1, $x2, $y2);
1061  
1062 Creates a new rectangle-shaped path, between the two points C<[$x1, $y1]>
1063 and C<[$x2, $y2]>.
1064  
1065 =cut
1066  
1067 2 ▶sub rectangle {
Subroutine rectangle: called CC=3 SCAR=11.4
Pod documented
1068 2 my ($self, $x1, $y1, $x2, $y2) = @_;
1069  
1070 # Ensure that x1,y1 is lower-left and x2,y2 is upper-right
1071 2 ▶ if ($x2 < $x1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x2 < $x1) 1 1 2
1072 1 my $x = $x1;
1073 1 $x1 = $x2;
1074 1 $x2 = $x;
1075 }
1076 2 ▶ if ($y2 < $y1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y2 < $y1) 0 2 2
1077 0 my $y = $y1;
1078 0 $y1 = $y2;
1079 0 $y2 = $y;
1080 }
1081  
1082 2 $self->add(floats($x1, $y1, ($x2 - $x1), ($y2 - $y1)), 're');
1083 2 $self->{' x'} = $x1;
1084 2 $self->{' y'} = $y1;
1085  
1086 2 return $self;
1087 }
1088  
1089 # Deprecated; replace with individual calls to rectangle
1090 5 ▶sub rect {
Subroutine rect: called CC=2 SCAR=6.9
Pod documented
1091 5 my $self = shift();
1092 5 my ($x, $y, $w, $h);
1093 5 ▶ while (defined($x = shift())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($x = shift())) 6 5 11
1094 6 $y = shift();
1095 6 $w = shift();
1096 6 $h = shift();
1097 6 $self->add(floats($x, $y, $w, $h), 're');
1098 }
1099 5 $self->{' x'} = $x;
1100 5 $self->{' y'} = $y;
1101 5 return $self;
1102 }
1103  
1104 # Deprecated; replace with rectangle, converting x2/y2 to w/h.
1105 2 ▶sub rectxy {
Subroutine rectxy: called CC=1 SCAR=0.0
Pod documented
1106 2 my ($self, $x, $y, $x2, $y2) = @_;
1107 2 $self->rect($x, $y, ($x2 - $x), ($y2 - $y));
1108 2 return $self;
1109 }
1110  
1111 =head2 circle
1112  
1113 $content = $content->circle($x, $y, $radius);
1114  
1115 Creates a new circular path centered on C<[$x, $y]> with the specified radius.
1116  
1117 =cut
1118  
1119 1 ▶sub circle {
Subroutine circle: called CC=1 SCAR=0.0
Pod documented
1120 1 my ($self, $x, $y, $r) = @_;
1121 1 $self->arc($x, $y, $r, $r, 0, 360, 1);
1122 1 $self->close();
1123 1 return $self;
1124 }
1125  
1126 =head2 ellipse
1127  
1128 $content = $content->ellipse($x, $y, $major, $minor);
1129  
1130 Creates a new elliptical path centered on C<[$x, $y]> with the specified major
1131 and minor axes.
1132  
1133 =cut
1134  
1135 1 ▶sub ellipse {
Subroutine ellipse: called CC=1 SCAR=0.0
Pod documented
1136 1 my ($self, $x, $y, $a, $b) = @_;
1137 1 $self->arc($x, $y, $a, $b, 0, 360, 1);
1138 1 $self->close();
1139 1 return $self;
1140 }
1141  
1142 =head2 pie
1143  
1144 $content = $content->pie($x, $y, $major, $minor, $a, $b);
1145  
1146 Creates a new wedge-shaped path from an ellipse centered on C<[$x, $y]> with the
1147 specified major and minor axes, extending from C<$a> degrees to C<$b> degrees.
1148  
1149 =cut
1150  
1151 0 ▶sub pie {
Subroutine pie: not called CC=1 SCAR=6.9
Pod documented
1152 0 my $self = shift();
1153 0 my ($x, $y, $a, $b, $alpha, $beta) = @_;
1154 0 my ($p0_x, $p0_y) = arctocurve($a, $b, $alpha, $beta);
1155 0 $self->move($x, $y);
1156 0 $self->line($p0_x + $x, $p0_y + $y);
1157 0 $self->arc($x, $y, $a, $b, $alpha, $beta);
1158 0 $self->close();
1159 }
1160  
1161 =head1 PATH PAINTING (DRAWING)
1162  
1163 =head2 stroke_color
1164  
1165 $content->stroke_color($color, @arguments);
1166  
1167 Sets the stroke color, which is black by default.
1168  
1169 # Use a named color
1170 $content->stroke_color('blue');
1171  
1172 # Use an RGB color (start with '#')
1173 $content->stroke_color('#FF0000');
1174  
1175 # Use a CMYK color (start with '%')
1176 $content->stroke_color('%FF000000');
1177  
1178 # Use a spot color with 100% coverage.
1179 my $spot = $pdf->colorspace('spot', 'PANTONE Red 032 C', '#EF3340');
1180 $content->stroke_color($spot, 1.0);
1181  
1182 RGB and CMYK colors can have one-byte, two-byte, three-byte, or four-byte values
1183 for each color, depending on the level of precision needed. For instance, cyan
1184 can be given as C<%F000> or C<%FFFF000000000000>.
1185  
1186 =head2 fill_color
1187  
1188 $content->fill_color($color, @arguments);
1189  
1190 Sets the fill color, which is black by default. Arguments are the same as in
1191 L</"stroke_color">.
1192  
1193 =cut
1194  
1195 # default colorspaces: rgb/hsv/named cmyk/hsl lab
1196 # ... only one text string
1197 #
1198 # pattern or shading space
1199 # ... only one object
1200 #
1201 # legacy greylevel
1202 # ... only one value
1203 26 ▶sub _makecolor {
Subroutine _makecolor: called CC=26 SCAR=55.8
1204 26 my ($self, $sf, @clr) = @_;
1205  
1206 26 ▶ if ($clr[0] =~ /^[a-z\#\!]+/) {
Branch: 9 branches
BranchTrueFalseTotal
if ($clr[0] =~ /^[a-z\#\!]+/) { } 23 3 26
elsif ($clr[0] =~ /^[\%]+/) { } 2 1 3
elsif ($clr[0] =~ /^[\$\&]/) { } 0 1 1
elsif (scalar @clr == 1 and ref $clr[0]) { } 0 1 1
elsif (scalar @clr == 1) { } 0 1 1
elsif (scalar @clr > 1 and ref $clr[0]) { } 1 0 1
elsif (scalar @clr == 2) { } 0 0 0
elsif (scalar @clr == 3) { } 0 0 0
elsif (scalar @clr == 4) { } 0 0 0
1207 # colorname or #! specifier
1208 # with rgb target colorspace
1209 # namecolor returns always a RGB
1210 23 ▶ return namecolor($clr[0]), ($sf ? 'rg' : 'RG');
Branch: 1 branch
BranchTrueFalseTotal
$sf ? : 14 9 23
1211 }
1212 elsif ($clr[0] =~ /^[\%]+/) {
1213 # % specifier
1214 # with cmyk target colorspace
1215 2 ▶ return namecolor_cmyk($clr[0]), ($sf ? 'k' : 'K');
Branch: 1 branch
BranchTrueFalseTotal
$sf ? : 1 1 2
1216 }
1217 elsif ($clr[0] =~ /^[\$\&]/) {
1218 # &$ specifier
1219 # with L*a*b target colorspace
1220 0 ▶ if (!defined $self->resource('ColorSpace', 'LabS')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->resource("ColorSpace", "LabS")) 0 0 0
1221 0 my $dc = PDFDict();
1222 0 my $cs = PDFArray(PDFName('Lab'), $dc);
1223 0 $dc->{'WhitePoint'} = PDFArray(map { PDFNum($_) } qw(1 1 1));
1224 0 $dc->{'Range'} = PDFArray(map { PDFNum($_) } qw(-128 127 -128 127));
1225 0 $dc->{'Gamma'} = PDFArray(map { PDFNum($_) } qw(2.2 2.2 2.2));
1226 0 $self->resource('ColorSpace', 'LabS', $cs);
1227 }
1228 0 ▶ return '/LabS', ($sf ? 'cs' : 'CS'), namecolor_lab($clr[0]), ($sf ? 'sc' : 'SC');
Branch: 2 branches
BranchTrueFalseTotal
$sf ? : 0 0 0
$sf ? : 0 0 0
1229 }
1230 ▶ elsif (scalar @clr == 1 and ref($clr[0])) {
Condition33%1/3
scalar @clr == 1 and ref $clr[0]
!ll&&!rl&&r
100
MC/DC0%0/2
scalar @clr == 1 and ref $clr[0]
scalar @clr == 1 ref $clr[0]
Truth table33%1/3
A and B
A= scalar @clr == 1B= ref $clr[0]
ABresult
0X0
100
111
1231 # pattern or shading space
1232 0 ▶ return '/Pattern', ($sf ? 'cs' : 'CS'), '/' . ($clr[0]->name()), ($sf ? 'scn' : 'SCN');
Branch: 2 branches
BranchTrueFalseTotal
$sf ? : 0 0 0
$sf ? : 0 0 0
1233 }
1234 elsif (scalar @clr == 1) {
1235 # grey color spec.
1236 0 ▶ return $clr[0], $sf ? 'g' : 'G';
Branch: 1 branch
BranchTrueFalseTotal
$sf ? : 0 0 0
1237 }
1238 ▶ elsif (scalar @clr > 1 and ref($clr[0])) {
Condition33%1/3
scalar @clr > 1 and ref $clr[0]
!ll&&!rl&&r
001
MC/DC0%0/2
scalar @clr > 1 and ref $clr[0]
scalar @clr > 1 ref $clr[0]
Truth table33%1/3
A and B
A= scalar @clr > 1B= ref $clr[0]
ABresult
0X0
100
111
1239 # indexed colorspace plus color-index
1240 # or custom colorspace plus param
1241 1 my $cs = shift(@clr);
1242 1 ▶ return '/' . $cs->name(), ($sf ? 'cs' : 'CS'), $cs->param(@clr), ($sf ? 'sc' : 'SC');
Branch: 2 branches
BranchTrueFalseTotal
$sf ? : 0 1 1
$sf ? : 0 1 1
1243 }
1244 elsif (scalar @clr == 2) {
1245 # indexed colorspace plus color-index
1246 # or custom colorspace plus param
1247 0 ▶ return '/' . $clr[0]->name(), ($sf ? 'cs' : 'CS'), $clr[0]->param($clr[1]), ($sf ? 'sc' : 'SC');
Branch: 2 branches
BranchTrueFalseTotal
$sf ? : 0 0 0
$sf ? : 0 0 0
1248 }
1249 elsif (scalar @clr == 3) {
1250 # legacy rgb color-spec (0 <= x <= 1)
1251 0 ▶ return floats($clr[0], $clr[1], $clr[2]), ($sf ? 'rg' : 'RG');
Branch: 1 branch
BranchTrueFalseTotal
$sf ? : 0 0 0
1252 }
1253 elsif (scalar @clr == 4) {
1254 # legacy cmyk color-spec (0 <= x <= 1)
1255 0 ▶ return floats($clr[0], $clr[1], $clr[2], $clr[3]), ($sf ? 'k' : 'K');
Branch: 1 branch
BranchTrueFalseTotal
$sf ? : 0 0 0
1256 }
1257 else {
1258 0 die 'invalid color specification.';
1259 }
1260 }
1261  
1262 15 ▶sub _fillcolor {
Subroutine _fillcolor: called CC=3 SCAR=13.3
1263 15 my ($self, @clrs) = @_;
1264 15 ▶ if (ref($clrs[0]) =~ m|^PDF::API2::Resource::ColorSpace|) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref($clrs[0]) =~ /^PDF::API2::Resource::ColorSpace/) { } 0 15 15
elsif (ref($clrs[0]) =~ /^PDF::API2::Resource::Pattern/) { } 0 15 15
1265 0 $self->resource('ColorSpace', $clrs[0]->name(), $clrs[0]);
1266 }
1267 elsif (ref($clrs[0]) =~ m|^PDF::API2::Resource::Pattern|) {
1268 0 $self->resource('Pattern', $clrs[0]->name(), $clrs[0]);
1269 }
1270  
1271 15 return $self->_makecolor(1, @clrs);
1272 }
1273  
1274 # Deprecated
1275 13 ▶sub fillcolor { return fill_color(@_) }
Subroutine fillcolor: called CC=1 SCAR=0.0
Pod undocumented
1276  
1277 15 ▶sub fill_color {
Subroutine fill_color: called CC=2 SCAR=7.0
Pod documented
1278 15 my $self = shift();
1279 15 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 15 0 15
1280 15 @{$self->{' fillcolor'}} = @_;
1281 15 $self->add($self->_fillcolor(@_));
1282 }
1283 15 return @{$self->{' fillcolor'}};
1284 }
1285  
1286 11 ▶sub _strokecolor {
Subroutine _strokecolor: called CC=3 SCAR=11.3
1287 11 my ($self, @clrs) = @_;
1288 11 ▶ if (ref($clrs[0]) =~ m|^PDF::API2::Resource::ColorSpace|) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref($clrs[0]) =~ /^PDF::API2::Resource::ColorSpace/) { } 1 10 11
elsif (ref($clrs[0]) =~ /^PDF::API2::Resource::Pattern/) { } 0 10 10
1289 1 $self->resource('ColorSpace', $clrs[0]->name(), $clrs[0]);
1290 }
1291 elsif (ref($clrs[0]) =~ m|^PDF::API2::Resource::Pattern|) {
1292 0 $self->resource('Pattern', $clrs[0]->name(), $clrs[0]);
1293 }
1294 11 return $self->_makecolor(0, @clrs);
1295 }
1296  
1297 # Deprecated
1298 8 ▶sub strokecolor { return stroke_color(@_) }
Subroutine strokecolor: called CC=1 SCAR=0.0
Pod undocumented
1299  
1300 11 ▶sub stroke_color {
Subroutine stroke_color: called CC=2 SCAR=7.0
Pod documented
1301 11 my $self = shift();
1302 11 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 11 0 11
1303 11 @{$self->{' strokecolor'}} = @_;
1304 11 $self->add($self->_strokecolor(@_));
1305 }
1306 11 return @{$self->{' strokecolor'}};
1307 }
1308  
1309 =head2 stroke
1310  
1311 $content = $content->stroke();
1312  
1313 Strokes the current path.
1314  
1315 =cut
1316  
1317 121 ▶sub _stroke {
Subroutine _stroke: called CC=1 SCAR=0.0
1318 121 return 'S';
1319 }
1320  
1321 121 ▶sub stroke {
Subroutine stroke: called CC=1 SCAR=0.0
Pod documented
1322 121 my $self = shift();
1323 121 $self->add(_stroke());
1324 121 return $self;
1325 }
1326  
1327 =head2 fill
1328  
1329 $content = $content->fill(rule => $rule);
1330  
1331 Fills the current path.
1332  
1333 C<$rule> describes which areas are filled in when the path intersects with itself.
1334  
1335 =over
1336  
1337 =item * nonzero (default)
1338  
1339 Use the nonzero winding number rule. This tends to mean that the entire area
1340 enclosed by the path is filled in, with some exceptions depending on the
1341 direction of the path.
1342  
1343 =item * even-odd
1344  
1345 Use the even-odd rule. This tends to mean that the presence of fill alternates
1346 each time the path is intersected.
1347  
1348 =back
1349  
1350 See PDF specification 1.7 section 8.5.3.3, Filling, for more details.
1351  
1352 =cut
1353  
1354 3 ▶sub fill {
Subroutine fill: called CC=5 SCAR=16.2
Pod documented
1355 3 my $self = shift();
1356  
1357 3 my $even_odd;
1358 3 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) { } 1 2 3
1359 1 my %options = @_;
1360 1 ▶ if (($options{'rule'} // 'nonzero') eq 'even-odd') {
Branch: 1 branch
BranchTrueFalseTotal
if (($options{"rule"} // "nonzero") eq "even-odd") 1 0 1
Condition50%1/2
$options{"rule"} // "nonzero"
l!l
10
MC/DC0%0/1
$options{"rule"} // "nonzero"
$options{"rule"}
Truth table50%1/2
A
A= $options{"rule"}
Aresult
11
00
1361 1 $even_odd = 1;
1362 }
1363 }
1364 else {
1365 # Deprecated
1366 2 $even_odd = shift();
1367 }
1368  
1369 3 ▶ $self->add($even_odd ? 'f*' : 'f');
Branch: 1 branch
BranchTrueFalseTotal
$even_odd ? : 2 1 3
1370  
1371 3 return $self;
1372 }
1373  
1374 =head2 paint
1375  
1376 $content = $content->paint(rule => $rule);
1377  
1378 Fills and strokes the current path. C<$rule> is as described in L</"fill">.
1379  
1380 =cut
1381  
1382 # Deprecated (renamed)
1383 2 ▶sub fillstroke { return paint(@_) }
Subroutine fillstroke: called CC=1 SCAR=0.0
Pod documented
1384  
1385 4 ▶sub paint {
Subroutine paint: called CC=5 SCAR=16.2
Pod documented
1386 4 my $self = shift();
1387  
1388 4 my $even_odd;
1389 4 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) { } 1 3 4
1390 1 my %options = @_;
1391 1 ▶ if (($options{'rule'} // 'nonzero') eq 'even-odd') {
Branch: 1 branch
BranchTrueFalseTotal
if (($options{"rule"} // "nonzero") eq "even-odd") 1 0 1
Condition50%1/2
$options{"rule"} // "nonzero"
l!l
10
MC/DC0%0/1
$options{"rule"} // "nonzero"
$options{"rule"}
Truth table50%1/2
A
A= $options{"rule"}
Aresult
11
00
1392 1 $even_odd = 1;
1393 }
1394 }
1395 else {
1396 # Deprecated
1397 3 $even_odd = shift();
1398 }
1399  
1400 4 ▶ $self->add($even_odd ? 'B*' : 'B');
Branch: 1 branch
BranchTrueFalseTotal
$even_odd ? : 2 2 4
1401  
1402 4 return $self;
1403 }
1404  
1405 =head2 clip
1406  
1407 $content = $content->clip(rule => $rule);
1408  
1409 Modifies the current clipping path (initially the entire page) by intersecting
1410 it with the current path following the next path-painting command. C<$rule> is
1411 as described in L</"fill">.
1412  
1413 =cut
1414  
1415 3 ▶sub clip {
Subroutine clip: called CC=5 SCAR=16.2
Pod documented
1416 3 my $self = shift();
1417  
1418 3 my $even_odd;
1419 3 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) { } 1 2 3
1420 1 my %options = @_;
1421 1 ▶ if (($options{'rule'} // 'nonzero') eq 'even-odd') {
Branch: 1 branch
BranchTrueFalseTotal
if (($options{"rule"} // "nonzero") eq "even-odd") 1 0 1
Condition50%1/2
$options{"rule"} // "nonzero"
l!l
10
MC/DC0%0/1
$options{"rule"} // "nonzero"
$options{"rule"}
Truth table50%1/2
A
A= $options{"rule"}
Aresult
11
00
1422 1 $even_odd = 1;
1423 }
1424 }
1425 else {
1426 # Deprecated
1427 2 $even_odd = shift();
1428 }
1429  
1430 3 ▶ $self->add($even_odd ? 'W*' : 'W');
Branch: 1 branch
BranchTrueFalseTotal
$even_odd ? : 2 1 3
1431  
1432 3 return $self;
1433 }
1434  
1435 =head2 end
1436  
1437 $content = $content->end();
1438  
1439 Ends the current path without filling or stroking. This is used primarily for
1440 the side effect of changing the current clipping path.
1441  
1442 =cut
1443  
1444 # Deprecated (renamed)
1445 1 ▶sub endpath { return end(@_) }
Subroutine endpath: called CC=1 SCAR=0.0
Pod documented
1446  
1447 2 ▶sub end {
Subroutine end: called CC=1 SCAR=0.0
Pod documented
1448 2 my $self = shift();
1449 2 $self->add('n');
1450 2 return $self;
1451 }
1452  
1453 0 ▶sub shade {
Subroutine shade: not called CC=1 SCAR=6.9
Pod undocumented
1454 0 my ($self, $shade, @cord) = @_;
1455  
1456 0 my @tm = (
1457 $cord[2] - $cord[0], 0,
1458 0 , $cord[3] - $cord[1],
1459 $cord[0] , $cord[1],
1460 );
1461  
1462 0 $self->save();
1463 0 $self->matrix(@tm);
1464 0 $self->add('/' . $shade->name(), 'sh');
1465 0 $self->resource('Shading', $shade->name(), $shade);
1466 0 $self->restore();
1467  
1468 0 return $self;
1469 }
1470  
1471 =head1 EXTERNAL OBJECTS
1472  
1473 =head2 object
1474  
1475 $content = $content->object($object, $x, $y, $scale_x, $scale_y);
1476  
1477 Places an image or other external object (a.k.a. XObject) on the page in the
1478 specified location.
1479  
1480 If C<$x> and C<$y> are omitted, the object will be placed at C<[0, 0]>.
1481  
1482 For images, C<$scale_x> and C<$scale_y> represent the width and height of the
1483 image on the page in points. If C<$scale_x> is omitted, it will default to 72
1484 pixels per inch. If C<$scale_y> is omitted, the image will be scaled
1485 proportionally based on the image dimensions.
1486  
1487 For other external objects, the scale is a multiplier, where 1 (the default)
1488 represents 100% (i.e. no change).
1489  
1490 If coordinate transformations have been made (see Coordinate Transformations
1491 above), the position and scale will be relative to the updated coordinates.
1492  
1493 If no coordinate transformations are needed, this method can be called directly
1494 from the L<PDF::API2::Page> object instead.
1495  
1496 =cut
1497  
1498 # Behavior based on argument count
1499 # 0: Place at 0, 0, 100%
1500 # 2: Place at X, Y, 100%
1501 # 3: Place at X, Y, scaled
1502 # 4: Place at X, Y, scale_w, scale_h
1503 0 ▶sub object {
Subroutine object: not called CC=8 SCAR=42.8
Pod documented
1504 0 my ($self, $object, $x, $y, $scale_x, $scale_y) = @_;
1505 0 ▶ $x //= 0;
Condition0%0/2
$x //= 0
l!l
00
MC/DC0%0/1
$x //= 0
$x
Truth table0%0/2
A
A= $x
Aresult
11
00
1506 0 ▶ $y //= 0;
Condition0%0/2
$y //= 0
l!l
00
MC/DC0%0/1
$y //= 0
$y
Truth table0%0/2
A
A= $y
Aresult
11
00
1507 0 ▶ if ($object->isa('PDF::API2::Resource::XObject::Image')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object->isa('PDF::API2::Resource::XObject::Image')) { } 0 0 0
1508 0 ▶ $scale_x //= $object->width();
Condition0%0/3
$scale_x //= $object->width
l!l&&r!l&&!r
000
MC/DC0%0/2
$scale_x //= $object->width
$scale_x $object->width
Truth table0%0/3
A //= B
A= $scale_xB= $object->width
ABresult
1X1
011
000
1509 0 ▶ $scale_y //= $object->height() * $scale_x / $object->width();
Condition0%0/2
$scale_y //= $object->height * $scale_x / $object->width
l!l
00
MC/DC0%0/1
$scale_y //= $object->height * $scale_x / $object->width
$scale_y
Truth table0%0/2
A
A= $scale_y
Aresult
11
00
1510 }
1511 else {
1512 0 ▶ $scale_x //= 1;
Condition0%0/2
$scale_x //= 1
l!l
00
MC/DC0%0/1
$scale_x //= 1
$scale_x
Truth table0%0/2
A
A= $scale_x
Aresult
11
00
1513 0 ▶ $scale_y //= $scale_x;
Condition0%0/3
$scale_y //= $scale_x
l!l&&r!l&&!r
000
MC/DC0%0/2
$scale_y //= $scale_x
$scale_y $scale_x
Truth table0%0/3
A //= B
A= $scale_yB= $scale_x
ABresult
1X1
011
000
1514 }
1515  
1516 0 $self->save();
1517 0 $self->matrix($scale_x, 0, 0, $scale_y, $x, $y);
1518 0 $self->add('/' . $object->name(), 'Do');
1519 0 $self->restore();
1520  
1521 0 $self->resource('XObject', $object->name(), $object);
1522  
1523 0 return $self;
1524 }
1525  
1526 # Deprecated
1527 8 ▶sub image {
Subroutine image: called CC=5 SCAR=18.5
Pod documented
1528 8 my $self = shift;
1529 8 my $img = shift;
1530 8 my ($x, $y, $w, $h) = @_;
1531 8 ▶ if (defined $img->{'Metadata'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $img->{"Metadata"}) 0 8 8
1532 0 $self->metaStart('PPAM:PlacedImage', $img->{'Metadata'});
1533 }
1534 8 $self->save();
1535 8 ▶ unless (defined $w) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $w) { } 8 0 8
if (do { not defined $h }) 0 8 8
1536 0 $h = $img->height();
1537 0 $w = $img->width();
1538 }
1539 0 elsif (not defined $h) {
1540 0 $h = $img->height() * $w;
1541 0 $w = $img->width() * $w;
1542 }
1543 8 $self->matrix($w, 0, 0, $h, $x, $y);
1544 8 $self->add('/' . $img->name(), 'Do');
1545 8 $self->restore();
1546 8 $self->{' x'} = $x;
1547 8 $self->{' y'} = $y;
1548 8 $self->resource('XObject', $img->name(), $img);
1549 8 ▶ if (defined $img->{'Metadata'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $img->{"Metadata"}) 0 8 8
1550 0 $self->metaEnd();
1551 }
1552 8 return $self;
1553 }
1554  
1555 # Deprecated
1556 2 ▶sub formimage {
Subroutine formimage: called CC=2 SCAR=7.1
Pod documented
1557 2 my ($self, $img, $x, $y, $s) = @_;
1558 2 $self->save();
1559 2 ▶ if (defined $s) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s) { } 2 0 2
1560 2 $self->matrix($s, 0, 0, $s, $x, $y);
1561 }
1562 else {
1563 0 $self->matrix(1, 0, 0, 1, $x, $y);
1564 }
1565 2 $self->add('/' . $img->name(), 'Do');
1566 2 $self->restore();
1567 2 $self->resource('XObject', $img->name(), $img);
1568 2 return $self;
1569 }
1570  
1571 =head1 TEXT STATE
1572  
1573 All of the following parameters that take a size are applied before any scaling
1574 takes place, so you don't need to adjust values to counteract scaling.
1575  
1576 =head2 font
1577  
1578 $content = $content->font($font, $size);
1579  
1580 Sets the font and font size. C<$font> is an object created by calling
1581 L<PDF::API2/"font"> to add the font to the document.
1582  
1583 my $pdf = PDF::API2->new();
1584 my $page = $pdf->page();
1585 my $text = $page->text();
1586  
1587 my $font = $pdf->font('Helvetica');
1588 $text->font($font, 24);
1589 $text->position(72, 720);
1590 $text->text('Hello, World!');
1591  
1592 $pdf->save('sample.pdf');
1593  
1594 =cut
1595  
1596 18 ▶sub _font {
Subroutine _font: called CC=2 SCAR=6.9
1597 18 my ($font, $size) = @_;
1598 18 ▶ if ($font->isvirtual()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($font->isvirtual) { } 1 17 18
1599 1 return('/' . $font->fontlist->[0]->name() . ' ' . float($size) . ' Tf');
1600 }
1601 else {
1602 17 return('/' . $font->name() . ' ' . float($size) . ' Tf');
1603 }
1604 }
1605 19 ▶sub font {
Subroutine font: called CC=2 SCAR=6.9
Pod documented
1606 19 my ($self, $font, $size) = @_;
1607 19 ▶ unless ($size) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($size) 1 18 19
1608 1 croak q{A font size is required};
1609 }
1610 18 $self->fontset($font, $size);
1611 18 $self->add(_font($font, $size));
1612 18 $self->{' fontset'} = 1;
1613 18 return $self;
1614 }
1615  
1616 18 ▶sub fontset {
Subroutine fontset: called CC=3 SCAR=11.0
Pod undocumented
1617 18 my ($self, $font, $size) = @_;
1618 18 $self->{' font'} = $font;
1619 18 $self->{' fontsize'} = $size;
1620 18 $self->{' fontset'} = 0;
1621  
1622 18 ▶ if ($font->isvirtual()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($font->isvirtual) { } 1 17 18
1623 1 foreach my $f (@{$font->fontlist()}) {
1624 2 $self->resource('Font', $f->name(), $f);
1625 }
1626 }
1627 else {
1628 17 $self->resource('Font', $font->name(), $font);
1629 }
1630  
1631 18 return $self;
1632 }
1633  
1634 =head2 character_spacing
1635  
1636 $spacing = $content->character_spacing($spacing);
1637  
1638 Sets the spacing between characters. This is initially zero.
1639  
1640 =cut
1641  
1642 3 ▶sub _charspace {
Subroutine _charspace: called CC=1 SCAR=0.0
1643 3 my $spacing = shift();
1644 3 return float($spacing, 6) . ' Tc';
1645 }
1646  
1647 # Deprecated (renamed)
1648 3 ▶sub charspace { return character_spacing(@_) }
Subroutine charspace: called CC=1 SCAR=0.0
Pod documented
1649  
1650 4 ▶sub character_spacing {
Subroutine character_spacing: called CC=2 SCAR=6.9
Pod documented
1651 4 my ($self, $spacing) = @_;
1652 4 ▶ if (defined $spacing) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $spacing) 3 1 4
1653 3 $self->{' charspace'} = $spacing;
1654 3 $self->add(_charspace($spacing));
1655 }
1656 4 return $self->{' charspace'};
1657 }
1658  
1659 =head2 word_spacing
1660  
1661 $spacing = $content->word_spacing($spacing);
1662  
1663 Sets the spacing between words. This is initially zero (i.e. just the width of
1664 the space).
1665  
1666 Word spacing might only affect simple fonts and composite fonts where the space
1667 character is a single-byte code. This is a limitation of the PDF specification
1668 at least as of version 1.7 (see section 9.3.3). It's possible that a later
1669 version of the specification will support word spacing in fonts that use
1670 multi-byte codes.
1671  
1672 =cut
1673  
1674 15 ▶sub _wordspace {
Subroutine _wordspace: called CC=1 SCAR=0.0
1675 15 my $spacing = shift();
1676 15 return float($spacing, 6) . ' Tw';
1677 }
1678  
1679 # Deprecated (renamed)
1680 24 ▶sub wordspace { return word_spacing(@_) }
Subroutine wordspace: called CC=1 SCAR=0.0
Pod documented
1681  
1682 25 ▶sub word_spacing {
Subroutine word_spacing: called CC=2 SCAR=6.9
Pod documented
1683 25 my ($self, $spacing) = @_;
1684 25 ▶ if (defined $spacing) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $spacing) 15 10 25
1685 15 $self->{' wordspace'} = $spacing;
1686 15 $self->add(_wordspace($spacing));
1687 }
1688 25 return $self->{' wordspace'};
1689 }
1690  
1691 =head2 hscale
1692  
1693 $scale = $content->hscale($scale);
1694  
1695 Sets/gets the percentage of horizontal text scaling. Enter a scale greater than
1696 100 to stretch text, less than 100 to squeeze text, or 100 to disable any
1697 existing scaling.
1698  
1699 =cut
1700  
1701 2 ▶sub _hscale {
Subroutine _hscale: called CC=1 SCAR=0.0
1702 2 my $scale = shift();
1703 2 return float($scale, 6) . ' Tz';
1704 }
1705  
1706 14 ▶sub hscale {
Subroutine hscale: called CC=2 SCAR=6.9
Pod documented
1707 14 my ($self, $scale) = @_;
1708 14 ▶ if (defined $scale) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $scale) 2 12 14
1709 2 $self->{' hscale'} = $scale;
1710 2 $self->add(_hscale($scale));
1711 }
1712 14 return $self->{' hscale'};
1713 }
1714  
1715 # Deprecated: hscale was originally named incorrectly (as hspace)
1716 1 ▶sub hspace { return hscale(@_) }
Subroutine hspace: called CC=1 SCAR=0.0
Pod documented
1717 0 ▶sub _hspace { return _hscale(@_) }
Subroutine _hspace: not called CC=1 SCAR=6.9
1718  
1719 =head2 leading
1720  
1721 $leading = $content->leading($leading);
1722  
1723 Sets/gets the text leading, which is the distance between baselines. This is
1724 initially zero (i.e. the lines will be printed on top of each other).
1725  
1726 =cut
1727  
1728 # Deprecated: leading is the correct name for this operator
1729 0 ▶sub _lead { return _leading(@_) }
Subroutine _lead: not called CC=1 SCAR=6.9
1730 1 ▶sub lead { return leading(@_) }
Subroutine lead: called CC=1 SCAR=0.0
Pod documented
1731  
1732 11 ▶sub _leading {
Subroutine _leading: called CC=1 SCAR=0.0
1733 11 my $leading = shift();
1734 11 return float($leading) . ' TL';
1735 }
1736 46 ▶sub leading {
Subroutine leading: called CC=2 SCAR=6.9
Pod documented
1737 46 my ($self, $leading) = @_;
1738 46 ▶ if (defined ($leading)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $leading) 11 35 46
1739 11 $self->{' leading'} = $leading;
1740 11 $self->add(_leading($leading));
1741 }
1742 46 return $self->{' leading'};
1743 }
1744  
1745 =head2 render
1746  
1747 $mode = $content->render($mode);
1748  
1749 Sets the text rendering mode.
1750  
1751 =over
1752  
1753 =item * 0 = Fill text
1754  
1755 =item * 1 = Stroke text (outline)
1756  
1757 =item * 2 = Fill, then stroke text
1758  
1759 =item * 3 = Neither fill nor stroke text (invisible)
1760  
1761 =item * 4 = Fill text and add to path for clipping
1762  
1763 =item * 5 = Stroke text and add to path for clipping
1764  
1765 =item * 6 = Fill, then stroke text and add to path for clipping
1766  
1767 =item * 7 = Add text to path for clipping
1768  
1769 =back
1770  
1771 =cut
1772  
1773 1 ▶sub _render {
Subroutine _render: called CC=1 SCAR=0.0
1774 1 my $mode = shift();
1775 1 return intg($mode) . ' Tr';
1776 }
1777  
1778 1 ▶sub render {
Subroutine render: called CC=2 SCAR=7.0
Pod documented
1779 1 my ($self, $mode) = @_;
1780 1 ▶ if (defined ($mode)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mode) 1 0 1
1781 1 $self->{' render'} = $mode;
1782 1 $self->add(_render($mode));
1783 }
1784 1 return $self->{' render'};
1785 }
1786  
1787 =head2 rise
1788  
1789 $distance = $content->rise($distance);
1790  
1791 Adjusts the baseline up or down from its current location. This is initially
1792 zero.
1793  
1794 Use this to create superscripts or subscripts (usually with an adjustment to the
1795 font size as well).
1796  
1797 =cut
1798  
1799 1 ▶sub _rise {
Subroutine _rise: called CC=1 SCAR=0.0
1800 1 my $distance = shift();
1801 1 return float($distance) . ' Ts';
1802 }
1803  
1804 1 ▶sub rise {
Subroutine rise: called CC=2 SCAR=7.0
Pod documented
1805 1 my ($self, $distance) = @_;
1806 1 ▶ if (defined ($distance)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $distance) 1 0 1
1807 1 $self->{' rise'} = $distance;
1808 1 $self->add(_rise($distance));
1809 }
1810 1 return $self->{' rise'};
1811 }
1812  
1813 # Formerly documented; still used internally
1814 0 ▶sub textstate {
Subroutine textstate: not called CC=10 SCAR=47.0
Pod undocumented
1815 0 my $self = shift();
1816 0 my %state;
1817 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
1818 0 %state = @_;
1819 0 foreach my $k (qw(charspace hscale wordspace leading rise render)) {
1820 0 ▶ next unless $state{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless $state{$k} 0 0 0
1821 0 $self->can($k)->($self, $state{$k});
1822 }
1823 0 ▶ if ($state{'font'} and $state{'fontsize'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state{"font"} and $state{"fontsize"}) 0 0 0
Condition0%0/3
$state{"font"} and $state{"fontsize"}
!ll&&!rl&&r
000
MC/DC0%0/2
$state{"font"} and $state{"fontsize"}
$state{"font"} $state{"fontsize"}
Truth table0%0/3
A and B
A= $state{"font"}B= $state{"fontsize"}
ABresult
0X0
100
111
1824 0 $self->font($state{'font'}, $state{'fontsize'});
1825 }
1826 0 ▶ if ($state{'textmatrix'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state{"textmatrix"}) 0 0 0
1827 0 $self->matrix(@{$state{'textmatrix'}});
1828 0 @{$self->{' translate'}} = @{$state{'translate'}};
1829 0 $self->{' rotate'} = $state{'rotate'};
1830 0 @{$self->{' scale'}} = @{$state{'scale'}};
1831 0 @{$self->{' skew'}} = @{$state{'skew'}};
1832 }
1833 0 ▶ if ($state{'fillcolor'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state{"fillcolor"}) 0 0 0
1834 0 $self->fillcolor(@{$state{'fillcolor'}});
1835 }
1836 0 ▶ if ($state{'strokecolor'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state{"strokecolor"}) 0 0 0
1837 0 $self->strokecolor(@{$state{'strokecolor'}});
1838 }
1839 0 %state = ();
1840 }
1841 else {
1842 0 foreach my $k (qw(font fontsize charspace hscale wordspace leading rise render)) {
1843 0 $state{$k} = $self->{" $k"};
1844 }
1845 0 $state{'matrix'} = [@{$self->{' matrix'}}];
1846 0 $state{'textmatrix'} = [@{$self->{' textmatrix'}}];
1847 0 $state{'textlinematrix'} = [@{$self->{' textlinematrix'}}];
1848 0 $state{'rotate'} = $self->{' rotate'};
1849 0 $state{'scale'} = [@{$self->{' scale'}}];
1850 0 $state{'skew'} = [@{$self->{' skew'}}];
1851 0 $state{'translate'} = [@{$self->{' translate'}}];
1852 0 $state{'fillcolor'} = [@{$self->{' fillcolor'}}];
1853 0 $state{'strokecolor'} = [@{$self->{' strokecolor'}}];
1854 }
1855 0 return %state;
1856 }
1857  
1858 =head1 TEXT PLACEMENT
1859  
1860 =head2 position
1861  
1862 # Set
1863 $content = $content->position($x, $y);
1864  
1865 # Get
1866 ($x, $y) = $content->position();
1867  
1868 If called with arguments, moves to the start of the current line of text, offset
1869 by C<$x> and C<$y>.
1870  
1871 If called without arguments, returns the current position of the cursor (before
1872 the effects of any coordinate transformation methods).
1873  
1874 =cut
1875  
1876 59 ▶sub position {
Subroutine position: called CC=4 SCAR=14.0
Pod documented
1877 59 my ($self, $x, $y) = @_;
1878  
1879 59 ▶ if (defined $x and not defined $y) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $x and not defined $y) 0 59 59
Condition66%2/3
defined $x and not defined $y
!ll&&!rl&&r
5810
MC/DC0%0/2
defined $x and not defined $y
defined $x not defined $y
Truth table66%2/3
A and not(B)
A= defined $xB= not defined $y
ABresult
0X0
100
111
1880 0 croak 'position requires either 0 or 2 arguments';
1881 }
1882  
1883 59 ▶ if (defined $x) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $x) 1 58 59
1884 1 $self->add(float($x), float($y), 'Td');
1885 1 $self->matrix_update($x, $y);
1886 1 $self->{' textlinematrix'}->[0] = $self->{' textlinestart'} + $x;
1887 1 $self->{' textlinestart'} = $self->{' textlinematrix'}->[0];
1888 1 return $self;
1889 }
1890  
1891 58 return @{$self->{' textlinematrix'}};
1892 }
1893  
1894 # Deprecated; replace with position
1895 1 ▶sub distance {
Subroutine distance: called CC=1 SCAR=0.0
Pod documented
1896 1 my ($self, $dx, $dy) = @_;
1897 1 $self->add(float($dx), float($dy), 'Td');
1898 1 $self->matrix_update($dx, $dy);
1899 1 $self->{' textlinematrix'}->[0] = $self->{' textlinestart'} + $dx;
1900 1 $self->{' textlinestart'} = $self->{' textlinematrix'}->[0];
1901 }
1902  
1903 # Deprecated; use position (ignores leading) or crlf (uses leading) instead
1904 3 ▶sub cr {
Subroutine cr: called CC=2 SCAR=6.9
Pod documented
1905 3 my ($self, $offset) = @_;
1906 3 ▶ if (defined $offset) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $offset) { } 2 1 3
1907 2 $self->add(0, float($offset), 'Td');
1908 2 $self->matrix_update(0, $offset);
1909 }
1910 else {
1911 1 $self->add('T*');
1912 1 $self->matrix_update(0, $self->leading() * -1);
1913 }
1914  
1915 3 $self->{' textlinematrix'}->[0] = $self->{' textlinestart'};
1916 }
1917  
1918 =head2 crlf
1919  
1920 $content = $content->crlf();
1921  
1922 Moves to the start of the next line, based on the L</"leading"> setting.
1923  
1924 If leading isn't set, a default distance of 120% of the font size will be used.
1925  
1926 =cut
1927  
1928 23 ▶sub crlf {
Subroutine crlf: called CC=3 SCAR=11.0
Pod documented
1929 23 my $self = shift();
1930 23 my $leading = $self->leading();
1931 23 ▶ if ($leading or not $self->{' fontsize'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($leading or not $self->{' fontsize'}) { } 22 1 23
Condition100%3/3
$leading or not $self->{" fontsize"}
l!l&&r!l&&!r
2111
MC/DC100%2/2
$leading or not $self->{" fontsize"}
$leading not $self->{" fontsize"}
Truth table100%3/3
A or not(B)
A= $leadingB= not $self->{" fontsize"}
ABresult
1X1
011
000
1932 22 $self->add('T*');
1933 }
1934 else {
1935 1 $leading = $self->{' fontsize'} * 1.2;
1936 1 $self->add(0, float($leading * -1), 'Td');
1937 }
1938  
1939 23 $self->matrix_update(0, $leading * -1);
1940 23 $self->{' textlinematrix'}->[0] = $self->{' textlinestart'};
1941 23 return $self;
1942 }
1943  
1944 # Deprecated; replace with crlf
1945 1 ▶sub nl {
Subroutine nl: called CC=1 SCAR=0.0
Pod documented
1946 1 my $self = shift();
1947 1 $self->add('T*');
1948 1 $self->matrix_update(0, $self->leading() * -1);
1949 1 $self->{' textlinematrix'}->[0] = $self->{' textlinestart'};
1950 }
1951  
1952 0 ▶sub _textpos {
Subroutine _textpos: not called CC=2 SCAR=17.9
1953 0 my ($self, @xy) = @_;
1954  
1955 0 my ($x, $y) = (0, 0);
1956 0 ▶ while (@xy) {
Branch: 1 branch
BranchTrueFalseTotal
if (@xy) 0 0 0
1957 0 $x += shift(@xy);
1958 0 $y += shift(@xy);
1959 }
1960 my (@m) = _transform(
1961 0 -matrix => $self->{' textmatrix'},
1962 -point => [$x, $y],
1963 );
1964  
1965 0 return ($m[0], $m[1]);
1966 }
1967  
1968 # Deprecated
1969 0 ▶sub textpos {
Subroutine textpos: not called CC=1 SCAR=6.9
Pod undocumented
1970 0 my $self = shift();
1971 0 return $self->_textpos(@{$self->{' textlinematrix'}});
1972 }
1973  
1974 # Deprecated; replace with position (without arguments)
1975 0 ▶sub textpos2 {
Subroutine textpos2: not called CC=1 SCAR=6.9
Pod undocumented
1976 0 my $self = shift();
1977 0 return $self->position();
1978 }
1979  
1980 =head2 text
1981  
1982 my $width = $content->text($text, %options);
1983  
1984 Places text on the page. Returns the width of the text in points.
1985  
1986 Options:
1987  
1988 =over
1989  
1990 =item * align
1991  
1992 One of C<left> (default), C<center>, or C<right>. Text will be placed such that
1993 it begins, is centered on, or ends at the current text position, respectively.
1994  
1995 In each case, the position will then be moved to the end of the text.
1996  
1997 =item * indent
1998  
1999 Indents the text by the number of points.
2000  
2001 If C<align> is set to anything other than C<left>, this setting will be ignored.
2002  
2003 =item * underline
2004  
2005 Underlines the text. The value may be one of the following:
2006  
2007 =over
2008  
2009 =item * auto
2010  
2011 Determines the underline distance from the text based on the font and font size.
2012  
2013 =item * $distance
2014  
2015 Manually set the underline distance in points. A positive distance moves the
2016 line downward.
2017  
2018 =item * [$distance, $thickness, ...]
2019  
2020 Manually set both the underline distance and line thickness, both in points.
2021  
2022 Repeat these arguments to include multiple underlines.
2023  
2024 =back
2025  
2026 =back
2027  
2028 =cut
2029  
2030 0 ▶sub _text_underline {
Subroutine _text_underline: not called CC=10 SCAR=47.0
2031 0 my ($self, $xy1, $xy2, $underline, $color) = @_;
2032 0 ▶ $color ||= 'black';
Condition0%0/2
$color ||= "black"
l!l
00
MC/DC0%0/1
$color ||= "black"
$color
Truth table0%0/2
A
A= $color
Aresult
11
00
2033  
2034 0 my @underline;
2035 0 ▶ if (ref($underline) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $underline eq 'ARRAY') { } 0 0 0
2036 0 @underline = @$underline;
2037 }
2038 else {
2039 0 @underline = ($underline, 1);
2040 }
2041 0 ▶ push @underline, 1 if @underline % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @underline % 2 0 0 0
2042  
2043 0 ▶ my $underlineposition = (-$self->{' font'}->underlineposition() * $self->{' fontsize'} / 1000 || 1);
Condition0%0/2
-$self->{' font'}->underlineposition * $self->{' fontsize'} / 1000 || 1
l!l
00
MC/DC0%0/1
-$self->{' font'}->underlineposition * $self->{' fontsize'} / 1000 || 1
-$self->{' font'}->underlineposition * $self->{' fontsize'} / 1000
Truth table0%0/2
A
A= -$self->{' font'}->underlineposition * $self->{' fontsize'} / 1000
Aresult
11
00
2044 0 ▶ my $underlinethickness = ($self->{' font'}->underlinethickness() * $self->{' fontsize'} / 1000 || 1);
Condition0%0/2
$self->{' font'}->underlinethickness * $self->{' fontsize'} / 1000 || 1
l!l
00
MC/DC0%0/1
$self->{' font'}->underlinethickness * $self->{' fontsize'} / 1000 || 1
$self->{' font'}->underlinethickness * $self->{' fontsize'} / 1000
Truth table0%0/2
A
A= $self->{' font'}->underlinethickness * $self->{' fontsize'} / 1000
Aresult
11
00
2045 0 my $pos = 1;
2046  
2047 0 ▶ while (@underline) {
Branch: 1 branch
BranchTrueFalseTotal
if (@underline) 0 0 0
2048 0 $self->add_post(_save());
2049  
2050 0 my $distance = shift(@underline);
2051 0 my $thickness = shift(@underline);
2052 0 my $scolor = $color;
2053 0 ▶ if (ref($thickness)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $thickness) 0 0 0
2054 0 ($thickness, $scolor) = @$thickness;
2055 }
2056 0 ▶ if ($distance eq 'auto') {
Branch: 1 branch
BranchTrueFalseTotal
if ($distance eq "auto") 0 0 0
2057 0 $distance = $pos * $underlineposition;
2058 }
2059 0 ▶ if ($thickness eq 'auto') {
Branch: 1 branch
BranchTrueFalseTotal
if ($thickness eq "auto") 0 0 0
2060 0 $thickness = $underlinethickness;
2061 }
2062  
2063 0 my ($x1, $y1) = $self->_textpos(@$xy1, 0, -($distance + ($thickness / 2)));
2064 0 my ($x2, $y2) = $self->_textpos(@$xy2, 0, -($distance + ($thickness / 2)));
2065  
2066 0 $self->add_post($self->_strokecolor($scolor));
2067 0 $self->add_post(_linewidth($thickness));
2068 0 $self->add_post(_move($x1, $y1));
2069 0 $self->add_post(_line($x2, $y2));
2070 0 $self->add_post(_stroke());
2071  
2072 0 $self->add_post(_restore());
2073 0 $pos++;
2074 }
2075 }
2076  
2077 30 ▶sub text {
Subroutine text: called CC=15 SCAR=29.6
Pod documented
2078 30 my ($self, $text, %opts) = @_;
2079 30 ▶ unless ($self->{' fontset'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{" fontset"}) 1 29 30
2080 1 ▶ unless (defined $self->{' font'} and $self->{' fontsize'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{" font"} and $self->{" fontsize"}) 1 0 1
Condition33%1/3
defined $self->{" font"} and $self->{" fontsize"}
!ll&&!rl&&r
100
MC/DC0%0/2
defined $self->{" font"} and $self->{" fontsize"}
defined $self->{" font"} $self->{" fontsize"}
Truth table33%1/3
A and B
A= defined $self->{" font"}B= $self->{" fontsize"}
ABresult
0X0
100
111
2081 1 croak q{Can't add text without first setting a font and font size};
2082 }
2083 0 $self->font($self->{' font'}, $self->{' fontsize'});
2084 0 $self->{' fontset'} = 1;
2085 }
2086  
2087 # Deprecated options (remove hyphens)
2088 29 ▶ if (exists $opts{'-indent'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"-indent"}) 10 19 29
2089 10 ▶ $opts{'indent'} //= delete $opts{'-indent'};
Condition33%1/3
$opts{"indent"} //= delete $opts{"-indent"}
l!l&&r!l&&!r
0100
MC/DC0%0/2
$opts{"indent"} //= delete $opts{"-indent"}
$opts{"indent"} delete $opts{"-indent"}
Truth table33%1/3
A //= B
A= $opts{"indent"}B= delete $opts{"-indent"}
ABresult
1X1
011
000
2090 }
2091 29 ▶ if (exists $opts{'-underline'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"-underline"}) 0 29 29
2092 0 ▶ $opts{'underline'} //= delete $opts{'-underline'};
Condition0%0/3
$opts{"underline"} //= delete $opts{"-underline"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"underline"} //= delete $opts{"-underline"}
$opts{"underline"} delete $opts{"-underline"}
Truth table0%0/3
A //= B
A= $opts{"underline"}B= delete $opts{"-underline"}
ABresult
1X1
011
000
2093 }
2094  
2095 29 my $width = $self->text_width($text);
2096  
2097 29 ▶ if (defined $opts{'align'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"align"}) 12 17 29
2098 12 ▶ if ($opts{'align'} eq 'left') {
Branch: 3 branches
BranchTrueFalseTotal
if ($opts{'align'} eq 'left') { } 6 6 12
elsif ($opts{'align'} eq 'center') { } 3 3 6
elsif ($opts{'align'} eq 'right') { } 3 0 3
2099 # NOOP
2100 }
2101 elsif ($opts{'align'} eq 'center') {
2102 3 $opts{'indent'} = -($width / 2);
2103 }
2104 elsif ($opts{'align'} eq 'right') {
2105 3 $opts{'indent'} = -$width;
2106 }
2107 else {
2108 0 croak 'Invalid alignment: ' . $opts{'align'};
2109 }
2110 }
2111  
2112 29 ▶ if (defined $opts{'indent'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"indent"}) 12 17 29
2113 12 $self->matrix_update($opts{'indent'}, 0);
2114 }
2115  
2116 29 my $underline_start = [$self->position()];
2117  
2118 29 ▶ if (defined $opts{'indent'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{'indent'}) { } 12 17 29
2119 12 my $indent = -$opts{'indent'};
2120 12 $indent *= (1000 / $self->{' fontsize'}) * (100 / $self->hscale());
2121 12 $self->add($self->{' font'}->text($text, $self->{' fontsize'}, $indent));
2122 }
2123 else {
2124 17 $self->add($self->{' font'}->text($text, $self->{' fontsize'}));
2125 }
2126  
2127 29 $self->matrix_update($width, 0);
2128  
2129 29 my $underline_end = [$self->position()];
2130  
2131 29 ▶ if (defined $opts{'underline'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"underline"}) 0 29 29
2132 $self->_text_underline($underline_start, $underline_end,
2133 $opts{'underline'},
2134 0 $opts{'-strokecolor'});
2135 }
2136  
2137 29 return $width;
2138 }
2139  
2140 # Deprecated; replace with text($line, align => 'center')
2141 6 ▶sub text_center {
Subroutine text_center: called CC=1 SCAR=0.0
Pod documented
2142 6 my ($self, $text, @opts) = @_;
2143 6 my $width = $self->advancewidth($text);
2144 6 return $self->text($text, -indent => -($width / 2), @opts);
2145 }
2146  
2147 # Deprecated; replace with text($line, align => 'right')
2148 3 ▶sub text_right {
Subroutine text_right: called CC=1 SCAR=0.0
Pod documented
2149 3 my ($self, $text, @opts) = @_;
2150 3 my $width = $self->advancewidth($text);
2151 3 return $self->text($text, -indent => -$width, @opts);
2152 }
2153  
2154 =head2 text_justified
2155  
2156 my $width = $content->text_justified($text, $width, %options);
2157  
2158 As C<text>, filling the specified width by adjusting the space between words.
2159  
2160 =cut
2161  
2162 1 ▶sub text_justified {
Subroutine text_justified: called CC=2 SCAR=7.0
Pod documented
2163 1 my ($self, $text, $width, %opts) = @_;
2164 1 my $initial_width = $self->text_width($text);
2165 1 my $space_count = (split /\s/, $text) - 1;
2166 1 my $ws = $self->wordspace();
2167 1 ▶ $self->wordspace(($width - $initial_width) / $space_count) if $space_count;
Branch: 1 branch
BranchTrueFalseTotal
if $space_count 1 0 1
2168 1 $self->text($text, %opts);
2169 1 $self->wordspace($ws);
2170 1 return $width;
2171 }
2172  
2173 19 ▶sub _text_fill_line {
Subroutine _text_fill_line: called CC=5 SCAR=16.1
2174 19 my ($self, $text, $width) = @_;
2175 19 my @words = split(/\x20/, $text);
2176 19 my @line = ();
2177 19 local $" = ' ';
2178 19 ▶ while (@words) {
Branch: 1 branch
BranchTrueFalseTotal
if (@words) 112 7 119
2179 112 push @line, (shift @words);
2180 112 ▶ last if $self->advancewidth("@line") > $width;
Branch: 1 branch
BranchTrueFalseTotal
if $self->advancewidth(join($", @line)) > $width 12 100 112
2181 }
2182 19 ▶ if ((scalar @line > 1) and ($self->advancewidth("@line") > $width)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @line > 1 and $self->advancewidth(join($", @line)) > $width) 12 7 19
Condition66%2/3
scalar @line > 1 and $self->advancewidth(join($", @line)) > $width
!ll&&!rl&&r
0712
MC/DC50%1/2
scalar @line > 1 and $self->advancewidth(join($", @line)) > $width
scalar @line > 1 $self->advancewidth(join($", @line)) > $width
Truth table66%2/3
A and B
A= scalar @line > 1B= $self->advancewidth(join($", @line)) > $width
ABresult
0X0
100
111
2183 12 unshift @words, pop @line;
2184 }
2185 19 my $ret = "@words";
2186 19 my $line = "@line";
2187 19 return $line, $ret;
2188 }
2189  
2190 7 ▶sub text_fill_left {
Subroutine text_fill_left: called CC=1 SCAR=0.0
Pod undocumented
2191 7 my ($self, $text, $width, %opts) = @_;
2192 7 my ($line, $ret) = $self->_text_fill_line($text, $width);
2193 7 $width = $self->text($line, %opts);
2194 7 return $width, $ret;
2195 }
2196  
2197 2 ▶sub text_fill_center {
Subroutine text_fill_center: called CC=1 SCAR=0.0
Pod undocumented
2198 2 my ($self, $text, $width, %opts) = @_;
2199 2 my ($line, $ret) = $self->_text_fill_line($text, $width);
2200 2 $width = $self->text_center($line, %opts);
2201 2 return $width, $ret;
2202 }
2203  
2204 2 ▶sub text_fill_right {
Subroutine text_fill_right: called CC=1 SCAR=0.0
Pod undocumented
2205 2 my ($self, $text, $width, %opts) = @_;
2206 2 my ($line, $ret) = $self->_text_fill_line($text, $width);
2207 2 $width = $self->text_right($line, %opts);
2208 2 return $width, $ret;
2209 }
2210  
2211 8 ▶sub text_fill_justified {
Subroutine text_fill_justified: called CC=10 SCAR=23.1
Pod undocumented
2212 8 my ($self, $text, $width, %opts) = @_;
2213 8 my ($line, $ret) = $self->_text_fill_line($text, $width);
2214 8 my $ws = $self->wordspace();
2215 8 my $w = $self->advancewidth($line);
2216 8 my $space_count = (split /\s/, $line) - 1;
2217  
2218 # Normal Line
2219 8 ▶ if ($ret) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret) 4 4 8
2220 4 ▶ $self->wordspace(($width - $w) / $space_count) if $space_count;
Branch: 1 branch
BranchTrueFalseTotal
if $space_count 4 0 4
2221 4 $width = $self->text($line, %opts, align => 'left');
2222 4 $self->wordspace($ws);
2223 4 return $width, $ret;
2224 }
2225  
2226 # Last Line
2227 4 ▶ if ($opts{'align-last'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"align-last"}) 3 1 4
2228 3 ▶ unless ($opts{'align-last'} =~ /^(left|center|right|justified)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"align-last"} =~ /^(left|center|right|justified)$/) 0 3 3
2229 0 croak 'Invalid align-last (must be left, center, right, or justified)';
2230 }
2231 }
2232 4 ▶ my $align_last = $opts{'align-last'} // 'left';
Condition100%2/2
$opts{"align-last"} // "left"
l!l
31
MC/DC100%1/1
$opts{"align-last"} // "left"
$opts{"align-last"}
Truth table100%2/2
A
A= $opts{"align-last"}
Aresult
11
00
2233 4 ▶ if ($align_last eq 'left') {
Branch: 3 branches
BranchTrueFalseTotal
if ($align_last eq 'left') { } 1 3 4
elsif ($align_last eq 'center') { } 1 2 3
elsif ($align_last eq 'right') { } 1 1 2
2234 1 $width = $self->text($line, %opts, align => 'left');
2235 }
2236 elsif ($align_last eq 'center') {
2237 1 $width = $self->text($line, %opts, align => 'center');
2238 }
2239 elsif ($align_last eq 'right') {
2240 1 $width = $self->text($line, %opts, align => 'right');
2241 }
2242 else {
2243 1 ▶ $self->wordspace(($width - $w) / $space_count) if $space_count;
Branch: 1 branch
BranchTrueFalseTotal
if $space_count 1 0 1
2244 1 $width = $self->text($line, %opts, align => 'left');
2245 1 $self->wordspace($ws);
2246 }
2247 4 return $width, $ret;
2248 }
2249  
2250 =head2 paragraph
2251  
2252 # Scalar context
2253 $overflow_text = $content->paragraph($text, $width, $height, %options);
2254  
2255 # Array context
2256 ($overflow, $height) = $content->paragraph($text, $width, $height, %options);
2257  
2258 Fills the rectangle with as much of the provided text as will fit.
2259  
2260 In array context, returns the remaining text (if any) of the positioned text and
2261 the remaining (unused) height. In scalar context, returns the remaining text
2262 (if any).
2263  
2264 Line spacing follows L</"leading">, if set, or 120% of the font size by default.
2265  
2266 B<Options>
2267  
2268 =over
2269  
2270 =item * align
2271  
2272 Specifies the alignment for each line of text. May be set to C<left> (default),
2273 C<center>, C<right>, or C<justified>.
2274  
2275 =item * align-last
2276  
2277 Specifies the alignment for the last line of justified text. May be set to
2278 C<left> (default), C<center>, C<right>, or C<justified>.
2279  
2280 =item * underline
2281  
2282 As described in L</"text">.
2283  
2284 =back
2285  
2286 =cut
2287  
2288 10 ▶sub paragraph {
Subroutine paragraph: called CC=22 SCAR=32.5
Pod documented
2289 10 my ($self, $text, $width, $height, %opts) = @_;
2290  
2291 # Deprecated options (remove hyphens)
2292 10 ▶ if (exists $opts{'-align'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"-align"}) 6 4 10
2293 6 ▶ $opts{'align'} //= delete $opts{'-align'};
Condition33%1/3
$opts{"align"} //= delete $opts{"-align"}
l!l&&r!l&&!r
060
MC/DC0%0/2
$opts{"align"} //= delete $opts{"-align"}
$opts{"align"} delete $opts{"-align"}
Truth table33%1/3
A //= B
A= $opts{"align"}B= delete $opts{"-align"}
ABresult
1X1
011
000
2294 }
2295 10 ▶ if (exists $opts{'-align-last'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"-align-last"}) 3 7 10
2296 3 ▶ $opts{'align-last'} //= delete $opts{'-align-last'};
Condition33%1/3
$opts{"align-last"} //= delete $opts{"-align-last"}
l!l&&r!l&&!r
030
MC/DC0%0/2
$opts{"align-last"} //= delete $opts{"-align-last"}
$opts{"align-last"} delete $opts{"-align-last"}
Truth table33%1/3
A //= B
A= $opts{"align-last"}B= delete $opts{"-align-last"}
ABresult
1X1
011
000
2297 }
2298 10 ▶ if (exists $opts{'-underline'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"-underline"}) 0 10 10
2299 0 ▶ $opts{'underline'} //= delete $opts{'-underline'};
Condition0%0/3
$opts{"underline"} //= delete $opts{"-underline"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"underline"} //= delete $opts{"-underline"}
$opts{"underline"} delete $opts{"-underline"}
Truth table0%0/3
A //= B
A= $opts{"underline"}B= delete $opts{"-underline"}
ABresult
1X1
011
000
2300 }
2301  
2302 10 my $leading = $self->leading();
2303 10 ▶ unless ($leading) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($leading) 0 10 10
2304 0 $leading = $self->{' fontsize'} * 1.2;
2305 }
2306  
2307 # If the text contains newlines, call paragraph on each line
2308 10 ▶ if ($text =~ /\n/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\n/) 1 9 10
2309 1 my $overflow = '';
2310 1 foreach my $line (split /\n/, $text) {
2311 # If there's overflow, no more text can be placed.
2312 3 ▶ if (length($overflow)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $overflow) 0 3 3
2313 0 $overflow .= "\n" . $line;
2314 0 next;
2315 }
2316  
2317 # Place a blank line if there are consecutive newlines
2318 3 ▶ unless (length($line)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $line) 1 2 3
2319 1 $self->crlf();
2320 1 $height -= $leading;
2321 1 next;
2322 }
2323  
2324 2 ($line, $height) = $self->paragraph($line, $width, $height, %opts);
2325 2 ▶ $overflow .= $line if length($line);
Branch: 1 branch
BranchTrueFalseTotal
if length $line 1 1 2
2326 }
2327  
2328 1 ▶ return ($overflow, $height) if wantarray();
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 1 1
2329 1 return $overflow;
2330 }
2331  
2332 9 my $w;
2333 9 ▶ while (length($text) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $text > 0) 21 7 28
2334 21 $height -= $leading;
2335 21 ▶ last if $height < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $height < 0 2 19 21
2336  
2337 19 ▶ my $align = $opts{'align'} // 'left';
Condition100%2/2
$opts{"align"} // "left"
l!l
127
MC/DC100%1/1
$opts{"align"} // "left"
$opts{"align"}
Truth table100%2/2
A
A= $opts{"align"}
Aresult
11
00
2338 19 ▶ if ($align eq 'justified' or $align eq 'justify') {
Branch: 3 branches
BranchTrueFalseTotal
if ($align eq 'justified' or $align eq 'justify') { } 8 11 19
elsif ($align eq 'right') { } 2 9 11
elsif ($align eq 'center') { } 2 7 9
Condition66%2/3
$align eq "justified" or $align eq "justify"
l!l&&r!l&&!r
8011
MC/DC50%1/2
$align eq "justified" or $align eq "justify"
$align eq "justified" $align eq "justify"
Truth table66%2/3
A or B
A= $align eq "justified"B= $align eq "justify"
ABresult
1X1
011
000
2339 8 ($w, $text) = $self->text_fill_justified($text, $width, %opts);
2340 }
2341 elsif ($align eq 'right') {
2342 2 ($w, $text) = $self->text_fill_right($text, $width, %opts);
2343 }
2344 elsif ($align eq 'center') {
2345 2 ($w, $text) = $self->text_fill_center($text, $width, %opts);
2346 }
2347 else {
2348 7 ($w, $text) = $self->text_fill_left($text, $width, %opts);
2349 }
2350 19 $self->crlf();
2351 }
2352  
2353 9 ▶ return ($text, $height) if wantarray();
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 2 7 9
2354 7 return $text;
2355 }
2356  
2357 # Deprecated former name
2358 0 ▶sub section { return paragraphs(@_) }
Subroutine section: not called CC=1 SCAR=6.9
Pod documented
2359  
2360 # Deprecated; merged into paragraph
2361 1 ▶sub paragraphs { return paragraph(@_) }
Subroutine paragraphs: called CC=1 SCAR=0.0
Pod documented
2362  
2363 0 ▶sub textlabel {
Subroutine textlabel: not called CC=17 SCAR=57.2
Pod undocumented
2364 0 my ($self, $x, $y, $font, $size, $text, %opts, $wht) = @_;
2365 0 my %trans_opts = (-translate => [$x,$y]);
2366 0 my %text_state;
2367 0 ▶ $trans_opts{'-rotate'} = $opts{'-rotate'} if $opts{'-rotate'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"-rotate"} 0 0 0
2368  
2369 0 my $wastext = $self->_in_text_object();
2370 0 ▶ if ($wastext) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wastext) 0 0 0
2371 0 %text_state = $self->textstate();
2372 0 $self->textend();
2373 }
2374 0 $self->save();
2375 0 $self->textstart();
2376  
2377 0 $self->transform(%trans_opts);
2378  
2379 0 ▶ if ($opts{'-color'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"-color"}) 0 0 0
2380 0 ▶ my $color = ref($opts{'-color'}) ? @{$opts{'-color'}} : $opts{'-color'};
Branch: 1 branch
BranchTrueFalseTotal
ref $opts{'-color'} ? : 0 0 0
2381 0 $self->fillcolor($color);
2382 }
2383 0 ▶ if ($opts{'-strokecolor'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"-strokecolor"}) 0 0 0
2384 my $color = (ref($opts{'-strokecolor'})
2385 0 ? @{$opts{'-strokecolor'}}
2386 0 ▶ : $opts{'-strokecolor'});
Branch: 1 branch
BranchTrueFalseTotal
ref $opts{'-strokecolor'} ? : 0 0 0
2387 0 $self->strokecolor($color);
2388 }
2389  
2390 0 $self->font($font, $size);
2391  
2392 0 ▶ $self->charspace($opts{'-charspace'}) if $opts{'-charspace'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"-charspace"} 0 0 0
2393 0 ▶ $self->hscale($opts{'-hscale'}) if $opts{'-hscale'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"-hscale"} 0 0 0
2394 0 ▶ $self->wordspace($opts{'-wordspace'}) if $opts{'-wordspace'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"-wordspace"} 0 0 0
2395 0 ▶ $self->render($opts{'-render'}) if $opts{'-render'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"-render"} 0 0 0
2396  
2397 0 ▶ my $align = $opts{'-align'} // 'left';
Condition0%0/2
$opts{"-align"} // "left"
l!l
00
MC/DC0%0/1
$opts{"-align"} // "left"
$opts{"-align"}
Truth table0%0/2
A
A= $opts{"-align"}
Aresult
11
00
2398 0 ▶ if ($opts{'-right'} or $align =~ /^r/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts{'-right'} or $align =~ /^r/i) { } 0 0 0
elsif ($opts{'-center'} or $align =~ /^c/i) { } 0 0 0
Condition0%0/3
$opts{"-right"} or $align =~ /^r/i
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"-right"} or $align =~ /^r/i
$opts{"-right"} $align =~ /^r/i
Truth table0%0/3
A or B
A= $opts{"-right"}B= $align =~ /^r/i
ABresult
1X1
011
000
2399 0 $wht = $self->text_right($text, %opts);
2400 }
2401 ▶ elsif ($opts{'-center'} or $align =~ /^c/i) {
Condition0%0/3
$opts{"-center"} or $align =~ /^c/i
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"-center"} or $align =~ /^c/i
$opts{"-center"} $align =~ /^c/i
Truth table0%0/3
A or B
A= $opts{"-center"}B= $align =~ /^c/i
ABresult
1X1
011
000
2402 0 $wht = $self->text_center($text, %opts);
2403 }
2404 else {
2405 0 $wht = $self->text($text, %opts);
2406 }
2407  
2408 0 $self->textend();
2409 0 $self->restore();
2410  
2411 0 ▶ if ($wastext) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wastext) 0 0 0
2412 0 $self->textstart();
2413 0 $self->textstate(%text_state);
2414 }
2415 0 return $wht;
2416 }
2417  
2418 =head2 text_width
2419  
2420 my $width = $content->text_width($line, %overrides);
2421  
2422 Returns the width of a line of text based on the current text state attributes.
2423 These can optionally be overridden:
2424  
2425 my $width = $content->text_width($line,
2426 font => $font,
2427 size => $size,
2428 character_spacing => $spacing,
2429 word_spacing => $spacing,
2430 hscale => $scale,
2431 );
2432  
2433 =cut
2434  
2435 # Deprecated (renamed)
2436 153 ▶sub advancewidth { return text_width(@_) }
Subroutine advancewidth: called CC=1 SCAR=0.0
Pod documented
2437  
2438 183 ▶sub text_width {
Subroutine text_width: called CC=8 SCAR=22.8
Pod documented
2439 183 my ($self, $text, %opts) = @_;
2440 183 ▶ return 0 unless defined($text) and length($text);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and length $text 0 183 183
Condition33%1/3
defined $text and length $text
!ll&&!rl&&r
00183
MC/DC0%0/2
defined $text and length $text
defined $text length $text
Truth table33%1/3
A and B
A= defined $textB= length $text
ABresult
0X0
100
111
2441  
2442 # Convert new names to old names
2443 183 ▶ if (exists $opts{'size'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"size"}) 0 183 183
2444 0 $opts{'fontsize'} = delete $opts{'size'};
2445 }
2446 183 ▶ if (exists $opts{'character_spacing'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"character_spacing"}) 0 183 183
2447 0 $opts{'charspace'} = delete $opts{'character_spacing'};
2448 }
2449 183 ▶ if (exists $opts{'word_spacing'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"word_spacing"}) 0 183 183
2450 0 $opts{'charspace'} = delete $opts{'word_spacing'};
2451 }
2452  
2453 183 foreach my $k (qw(font fontsize wordspace charspace hscale)) {
2454 915 ▶ $opts{$k} = $self->{" $k"} unless defined $opts{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{$k} 913 2 915
2455 }
2456  
2457 # Width of glyphs
2458 183 my $width = $opts{'font'}->width($text) * $opts{'fontsize'};
2459  
2460 # Width of space characters
2461 183 my $space_count = $text =~ y/\x20/\x20/;
2462 183 $width += $opts{'wordspace'} * $space_count;
2463  
2464 # Width of space between characters
2465 183 my $char_count = length($text);
2466 183 $width += $opts{'charspace'} * ($char_count - 1);
2467  
2468 # Horizontal scale multiplier
2469 183 $width *= $opts{'hscale'} / 100;
2470  
2471 183 return $width;
2472 }
2473  
2474 0 ▶sub metaStart {
Subroutine metaStart: not called CC=2 SCAR=17.9
Pod undocumented
2475 0 my ($self, $tag, $obj) = @_;
2476 0 $self->add("/$tag");
2477 0 ▶ if (defined $obj) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $obj) { } 0 0 0
2478 0 my $dict = PDFDict();
2479 0 $dict->{'Metadata'} = $obj;
2480 0 $self->resource('Properties', $obj->name(), $dict);
2481 0 $self->add('/' . $obj->name());
2482 0 $self->add('BDC');
2483 }
2484 else {
2485 0 $self->add('BMC');
2486 }
2487 0 return $self;
2488 }
2489  
2490 0 ▶sub metaEnd {
Subroutine metaEnd: not called CC=1 SCAR=6.9
Pod undocumented
2491 0 my $self = shift();
2492 0 $self->add('EMC');
2493 0 return $self;
2494 }
2495  
2496 0 ▶sub add_post {
Subroutine add_post: not called CC=3 SCAR=24.8
Pod undocumented
2497 0 my $self = shift();
2498 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
2499 0 ▶ unless ($self->{' poststream'} =~ /\s$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{" poststream"} =~ /\s$/) 0 0 0
2500 0 $self->{' poststream'} .= ' ';
2501 }
2502 0 $self->{' poststream'} .= join(' ', @_) . ' ';
2503 }
2504 0 return $self;
2505 }
2506  
2507 778 ▶sub add {
Subroutine add: called CC=3 SCAR=11.0
Pod documented
2508 778 my $self = shift();
2509 778 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 778 0 778
2510 778 ▶ unless ($self->{' stream'} =~ /\s$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{" stream"} =~ /\s$/) 129 649 778
2511 129 $self->{' stream'} .= ' ';
2512 }
2513 778 $self->{' stream'} .= encode('iso-8859-1', join(' ', @_) . ' ');
2514 }
2515 778 return $self;
2516 }
2517  
2518 # Shortcut method for determining if we're inside a text object
2519 # (i.e. between BT and ET). See textstart and textend.
2520 500 ▶sub _in_text_object {
Subroutine _in_text_object: called CC=1 SCAR=0.0
2521 500 my $self = shift();
2522 500 return $self->{' apiistext'};
2523 }
2524  
2525 28 ▶sub compressFlate {
Subroutine compressFlate: called CC=1 SCAR=0.0
Pod undocumented
2526 28 my $self = shift();
2527 28 $self->{'Filter'} = PDFArray(PDFName('FlateDecode'));
2528 28 $self->{'-docompress'} = 1;
2529 28 return $self;
2530 }
2531  
2532 19 ▶sub textstart {
Subroutine textstart: called CC=2 SCAR=6.9
Pod undocumented
2533 19 my $self = shift();
2534 19 ▶ unless ($self->_in_text_object()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_in_text_object) 19 0 19
2535 19 $self->add(' BT ');
2536 19 $self->{' apiistext'} = 1;
2537 19 $self->{' font'} = undef;
2538 19 $self->{' fontset'} = 0;
2539 19 $self->{' fontsize'} = 0;
2540 19 $self->{' charspace'} = 0;
2541 19 $self->{' hscale'} = 100;
2542 19 $self->{' wordspace'} = 0;
2543 19 $self->{' leading'} = 0;
2544 19 $self->{' rise'} = 0;
2545 19 $self->{' render'} = 0;
2546 19 $self->{' textlinestart'} = 0;
2547 19 @{$self->{' matrix'}} = (1, 0, 0, 1, 0, 0);
2548 19 @{$self->{' textmatrix'}} = (1, 0, 0, 1, 0, 0);
2549 19 @{$self->{' textlinematrix'}} = (0, 0);
2550 19 @{$self->{' fillcolor'}} = 0;
2551 19 @{$self->{' strokecolor'}} = 0;
2552 19 @{$self->{' translate'}} = (0, 0);
2553 19 @{$self->{' scale'}} = (1, 1);
2554 19 @{$self->{' skew'}} = (0, 0);
2555 19 $self->{' rotate'} = 0;
2556 }
2557 19 return $self;
2558 }
2559  
2560 123 ▶sub textend {
Subroutine textend: called CC=2 SCAR=6.9
Pod undocumented
2561 123 my $self = shift();
2562 123 ▶ if ($self->_in_text_object()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_in_text_object) 15 108 123
2563 15 $self->add(' ET ', $self->{' poststream'});
2564 15 $self->{' apiistext'} = 0;
2565 15 $self->{' poststream'} = '';
2566 }
2567 123 return $self;
2568 }
2569  
2570 31 ▶sub resource {
Subroutine resource: called CC=9 SCAR=25.6
Pod undocumented
2571 31 my ($self, $type, $key, $obj, $force) = @_;
2572 31 ▶ if ($self->{' apipage'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{' apipage'}) { } 29 2 31
2573 # we are a content stream on a page.
2574 29 return $self->{' apipage'}->resource($type, $key, $obj, $force);
2575 }
2576 else {
2577 # we are a self-contained content stream.
2578 2 ▶ $self->{'Resources'} //= PDFDict();
Condition33%1/3
$self->{"Resources"} //= PDFDict()
l!l&&r!l&&!r
200
MC/DC0%0/2
$self->{"Resources"} //= PDFDict()
$self->{"Resources"} PDFDict()
Truth table33%1/3
A //= B
A= $self->{"Resources"}B= PDFDict()
ABresult
1X1
011
000
2579  
2580 2 my $dict = $self->{'Resources'};
2581 2 ▶ $dict->realise() if ref($dict) =~ /Objind$/;
Branch: 1 branch
BranchTrueFalseTotal
if ref($dict) =~ /Objind$/ 0 2 2
2582  
2583 2 ▶ $dict->{$type} ||= PDFDict();
Condition33%1/3
$dict->{$type} ||= PDFDict()
l!l&&r!l&&!r
020
MC/DC0%0/2
$dict->{$type} ||= PDFDict()
$dict->{$type} PDFDict()
Truth table33%1/3
A ||= B
A= $dict->{$type}B= PDFDict()
ABresult
1X1
011
000
2584 2 ▶ $dict->{$type}->realise() if ref($dict->{$type}) =~ /Objind$/;
Branch: 1 branch
BranchTrueFalseTotal
if ref($dict->{$type}) =~ /Objind$/ 0 2 2
2585 2 ▶ unless (defined $obj) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $obj) { } 2 0 2
2586 0 return $dict->{$type}->{$key};
2587 }
2588 else {
2589 2 ▶ if ($force) {
Branch: 1 branch
BranchTrueFalseTotal
if ($force) { } 0 2 2
2590 0 $dict->{$type}->{$key} = $obj;
2591 }
2592 else {
2593 2 ▶ $dict->{$type}->{$key} //= $obj;
Condition33%1/3
$dict->{$type}{$key} //= $obj
l!l&&r!l&&!r
020
MC/DC0%0/2
$dict->{$type}{$key} //= $obj
$dict->{$type}{$key} $obj
Truth table33%1/3
A //= B
A= $dict->{$type}{$key}B= $obj
ABresult
1X1
011
000
2594 }
2595 2 return $dict;
2596 }
2597 }
2598 }
2599  
2600 =head1 MIGRATION
2601  
2602 See L<PDF::API2/"MIGRATION"> for an overview.
2603  
2604 =over
2605  
2606 =item transform(%hyphen_prefixed_options);
2607  
2608 Remove hyphens from option names (C<-translate> becomes C<translate>, etc.).
2609  
2610 =item transform_rel
2611  
2612 Replace with L</"transform">, setting option C<repeat> to true. Remove
2613 hyphens from the names of other options.
2614  
2615 =item linewidth
2616  
2617 Replace with L</"line_width">.
2618  
2619 =item linecap
2620  
2621 Replace with L</"line_cap">.
2622  
2623 =item linejoin
2624  
2625 Replace with L</"line_join">.
2626  
2627 =item meterlimit
2628  
2629 =item miterlimit
2630  
2631 Replace with L</"miter_limit">.
2632  
2633 =item linedash
2634  
2635 Replace with L</"line_dash_pattern">. Remove hyphens from option names. Rename
2636 C<-shift> to C<offset>.
2637  
2638 =item flatness
2639  
2640 Replace with L</"flatness_tolerance">.
2641  
2642 =item poly
2643  
2644 Replace with L</"move"> (first two arguments) and L</"polyline"> (remaining
2645 arguments).
2646  
2647 =item endpath
2648  
2649 Replace with L</"end">.
2650  
2651 =item rect
2652  
2653 Replace with L</"rectangle">, converting the C<$w> (third) and C<$h> (fourth)
2654 arguments to the X and Y values of the upper-right corner:
2655  
2656 # Old
2657 $content->rect($x, $y, $w, $h);
2658  
2659 # New
2660 $content->rectangle($x, $y, $x + $w, $y + $h);
2661  
2662 =item rectxy
2663  
2664 Replace with L</"rectangle">.
2665  
2666 =item fill(1)
2667  
2668 Replace with C<$content-E<gt>fill(rule =E<gt> 'even-odd')>.
2669  
2670 =item fillstroke
2671  
2672 Replace with L</"paint">.
2673  
2674 =item clip(1)
2675  
2676 Replace with C<$content-E<gt>clip(rule =E<gt> 'even-odd')>.
2677  
2678 =item image
2679  
2680 =item formimage
2681  
2682 Replace with L</"object">.
2683  
2684 =item charspace
2685  
2686 Replace with L</"character_spacing">.
2687  
2688 =item wordspace
2689  
2690 Replace with L</"word_spacing">.
2691  
2692 =item hspace
2693  
2694 Replace with L</"hscale">.
2695  
2696 =item lead
2697  
2698 Replace with L</"leading">.
2699  
2700 =item distance
2701  
2702 Replace with L</"position">.
2703  
2704 =item cr
2705  
2706 Replace with either L</"position"> (if called with arguments) or L</"crlf"> (if
2707 called without arguments).
2708  
2709 =item nl
2710  
2711 Replace with L</"crlf">.
2712  
2713 =item text(%hyphen_prefixed_options)
2714  
2715 Remove initial hyphens from option names.
2716  
2717 =item text_center
2718  
2719 Replace with L</"text">, setting C<align> to C<center>.
2720  
2721 =item text_right
2722  
2723 Replace with L</"text">, setting C<align> to C<right>.
2724  
2725 =item paragraph(%hyphen_prefixed_options)
2726  
2727 Remove initial hyphens from option names. C<-align-last> becomes C<align-last>.
2728  
2729 =item section
2730  
2731 =item paragraphs
2732  
2733 Replace with L</"paragraph">.
2734  
2735 =item advancewidth
2736  
2737 Replace with L</"text_width">.
2738  
2739 =back
2740  
2741 =cut
2742  
2743 39 1;
« API2/Basic/PDF/Filter.pm ↑ Summary API2/Basic/PDF/Dict.pm »