blib/lib/PDF/API2/Tweaks.pm

filter: Statementstmt 11.2% 15 / 133 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 22.2% 4 / 18 Podpod 8.3% 1 / 12 totaltotal 7.4% 20 / 269 CC 47 SCAR 34.8
CC
47
Cov
7%
textlist
56.0
texthlist
56.0
textvlist
56.0
CRAP 32.3
↑ Summary
1 #! perl
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use PDF::API2;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 package PDF::API2::Tweaks;
9  
10 =head1 NAME
11  
12 PDF::API2::Tweaks - Assorted handy additions to PDF::API2.
13  
14 =head1 SYNOPSIS
15  
16 PDF::API2::Tweaks provides a number of extensions to PDF::API2.
17  
18 Most of the extensions deal with producing PDF overlays, to fill in
19 forms. For example,
20  
21 # Open an existing PDF file
22 my $pdf = PDF::API2->open($form);
23  
24 # Retrieve an existing page
25 my $page = $pdf->openpage(1);
26  
27 # Add a built-in font to the PDF
28 my $font = $pdf->corefont('Helvetica');
29  
30 # Setup text context.
31 my $text = $page->text();
32 $text->font($font, 10);
33 $text->fillcolor('#000000');
34 $text->strokecolor('#000000');
35  
36 # So far, this is all basic PDF::API2.
37  
38 # The following Tweaks extension will produce a series of lines,
39 # the first one starting at position 100,714 and subsequent lines
40 # spaced 16 apart:
41  
42 $text->textlist( 100, 714, 16, <<'EOD' );
43 Now is the time
44 for all good man
45 to start using Perl
46 EOD
47  
48 # Save to a file.
49 $pdf->saveas("perl.pdf");
50  
51 =cut
52  
53 1 our $VERSION = 0.09;
54  
55 =head1 TEXT FUNCTIONS
56  
57 The following functions operate on PDF::API2::Content::Text objects.
58 In general, these are obtained by a call to the C<text> method on the
59 page object.
60  
61 =cut
62  
63 package PDF::API2::Content::Text;
64  
65 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 0 ▶sub _isnum {
Subroutine _isnum: not called CC=1 SCAR=6.9
68 0 $_[0] =~ /^[-+]?\d+(.\d+)?$/;
69 }
70  
71 1 my %rotation_for; # LEAK!!!
72  
73 0 ▶sub translate { # internal
Subroutine translate: not called CC=3 SCAR=24.8
Pod documented
74 0 my ( $self, $x, $y ) = @_;
75 0 ▶ if ( $rotation_for{$self} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rotation_for{$self}) 0 0 0
76 0 ▶ ($x, $y) = ($y, $x) if $rotation_for{$self} == 90;
Branch: 1 branch
BranchTrueFalseTotal
if $rotation_for{$self} == 90 0 0 0
77 $self->transform( -translate => [ $x, $y ],
78 0 -rotate => $rotation_for{$self},
79 );
80 0 return;
81 }
82  
83 0 $self->SUPER::translate( $x, $y );
84 }
85  
86 0 ▶sub set_rotation { # internal
Subroutine set_rotation: not called CC=2 SCAR=17.9
Pod undocumented
87 0 my ( $self, $rotation ) = @_;
88 0 ▶ $rotation ? $rotation_for{$self} = $rotation : delete($rotation_for{$self});
Branch: 1 branch
BranchTrueFalseTotal
$rotation ? : 0 0 0
89 }
90  
91 =head2 $text->textlist( X, Y, D, items )
92  
93 Writes a list of items starting at the given coordinates and
94 decrementing Y with D for each item. Note that items may contain
95 newlines that will be dwimmed.
96  
97 Returns the coordinates of the last item written; in scalar context
98 the Y coordinate only.
99  
100 =cut
101  
102 0 ▶sub textlist {
Subroutine textlist: not called CC=7 SCAR=40.3
Pod undocumented
103 0 my ( $self, $x, $y, $d, @list ) = @_;
104 0 ▶ croak("textlist: coordinates must be numeric, not ($x,$y)")
Branch: 1 branch
BranchTrueFalseTotal
unless _isnum($x) and _isnum($y) 0 0 0
Condition0%0/3
_isnum($x) and _isnum($y)
!ll&&!rl&&r
000
MC/DC0%0/2
_isnum($x) and _isnum($y)
_isnum($x) _isnum($y)
Truth table0%0/3
A and B
A= _isnum($x)B= _isnum($y)
ABresult
0X0
100
111
105 unless _isnum($x) && _isnum($y);
106 0 ▶ croak("textlist: line spacing must be numeric, not \"$d\"")
Branch: 1 branch
BranchTrueFalseTotal
unless _isnum($d) 0 0 0
107 unless _isnum($d);
108  
109 0 foreach ( @list ) {
110 0 foreach ( split /\n/ ) {
111 0 $self->translate( $x, $y );
112 0 $self->text($_);
113 0 $y -= $d;
114 }
115 }
116 0 ▶ wantarray ? ( $x, $y + $d ) : $y + $d;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
117 }
118  
119 =head2 $text->textline( X, Y, line )
120  
121 Writes a line of text at the given coordinates.
122  
123 Returns the coordinates; in scalar context the Y coordinate only.
124  
125 =cut
126  
127 0 ▶sub textline {
Subroutine textline: not called CC=4 SCAR=30.0
Pod undocumented
128 0 my ( $self, $x, $y, $line ) = @_;
129 0 ▶ croak("textline: coordinates must be numeric, not ($x,$y)")
Branch: 1 branch
BranchTrueFalseTotal
unless _isnum($x) and _isnum($y) 0 0 0
Condition0%0/3
_isnum($x) and _isnum($y)
!ll&&!rl&&r
000
MC/DC0%0/2
_isnum($x) and _isnum($y)
_isnum($x) _isnum($y)
Truth table0%0/3
A and B
A= _isnum($x)B= _isnum($y)
ABresult
0X0
100
111
130 unless _isnum($x) && _isnum($y);
131  
132 0 $self->translate( $x, $y );
133 0 $self->text($line);
134 0 ▶ wantarray ? ( $x, $y ) : $y;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
135 }
136  
137 =head2 $text->textrline( X, Y, line )
138  
139 Writes a line of text at the given coordinates, right aligned.
140  
141 Returns the coordinates; in scalar context the Y coordinate only.
142  
143 =cut
144  
145 0 ▶sub textrline {
Subroutine textrline: not called CC=4 SCAR=30.0
Pod undocumented
146 0 my ( $self, $x, $y, $line ) = @_;
147 0 ▶ croak("textrline: coordinates must be numeric, not ($x,$y)")
Branch: 1 branch
BranchTrueFalseTotal
unless _isnum($x) and _isnum($y) 0 0 0
Condition0%0/3
_isnum($x) and _isnum($y)
!ll&&!rl&&r
000
MC/DC0%0/2
_isnum($x) and _isnum($y)
_isnum($x) _isnum($y)
Truth table0%0/3
A and B
A= _isnum($x)B= _isnum($y)
ABresult
0X0
100
111
148 unless _isnum($x) && _isnum($y);
149  
150 0 $self->translate( $x, $y );
151 0 $self->text_right($line);
152 0 ▶ wantarray ? ( $x, $y ) : $y;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
153 }
154  
155 =head2 $text->textcline( X, Y, line )
156  
157 Writes a line of text at the given coordinates, centered.
158  
159 Returns the coordinates; in scalar context the Y coordinate only.
160  
161 =cut
162  
163 0 ▶sub textcline {
Subroutine textcline: not called CC=4 SCAR=30.0
Pod undocumented
164 0 my ( $self, $x, $y, $line ) = @_;
165 0 ▶ croak("textcline: coordinates must be numeric, not ($x,$y)")
Branch: 1 branch
BranchTrueFalseTotal
unless _isnum($x) and _isnum($y) 0 0 0
Condition0%0/3
_isnum($x) and _isnum($y)
!ll&&!rl&&r
000
MC/DC0%0/2
_isnum($x) and _isnum($y)
_isnum($x) _isnum($y)
Truth table0%0/3
A and B
A= _isnum($x)B= _isnum($y)
ABresult
0X0
100
111
166 unless _isnum($x) && _isnum($y);
167  
168 0 $self->translate( $x, $y );
169 0 $self->text_center($line);
170 0 ▶ wantarray ? ( $x, $y ) : $y;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
171 }
172  
173 =head2 $text->texthlist( X, Y, item, [ disp, item, ... ] )
174  
175 Writes a series of items at the given coordinates, each subsequent
176 item is horizontally offsetted by the displacement that precedes it
177 in the list.
178  
179 Returns the coordinates of the last item written; in scalar context
180 the X coordinate only.
181  
182 =cut
183  
184 0 ▶sub texthlist {
Subroutine texthlist: not called CC=7 SCAR=40.3
Pod undocumented
185 0 my ( $self, $x, $y, @list ) = @_;
186 0 ▶ croak("texthlist: coordinates must be numeric, not ($x,$y)")
Branch: 1 branch
BranchTrueFalseTotal
unless _isnum($x) and _isnum($y) 0 0 0
Condition0%0/3
_isnum($x) and _isnum($y)
!ll&&!rl&&r
000
MC/DC0%0/2
_isnum($x) and _isnum($y)
_isnum($x) _isnum($y)
Truth table0%0/3
A and B
A= _isnum($x)B= _isnum($y)
ABresult
0X0
100
111
187 unless _isnum($x) && _isnum($y);
188  
189 0 $self->translate( $x, $y );
190 0 $self->text( shift(@list) );
191 0 ▶ while ( @list ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@list) 0 0 0
192 0 my $d = shift(@list);
193 0 ▶ croak("texthlist: offset must be a number, not \"$d\"")
Branch: 1 branch
BranchTrueFalseTotal
unless _isnum($d) 0 0 0
194 unless _isnum($d);
195 0 $x += $d;
196 0 ▶ last unless @list;
Branch: 1 branch
BranchTrueFalseTotal
unless @list 0 0 0
197 0 $self->translate( $x, $y );
198 0 $self->text( shift(@list) );
199 }
200 0 ▶ wantarray ? ( $x, $y ) : $x;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
201 }
202  
203 =head2 $text->textvlist( X, Y, item, [ disp, item, ... ] )
204  
205 Writes a series of items at the given coordinates, each subsequent
206 item is vertically offsetted by the displacement that precedes it
207 in the list.
208  
209 Returns the coordinates of the last item written; in scalar context
210 the Y coordinate only.
211  
212 =cut
213  
214 0 ▶sub textvlist {
Subroutine textvlist: not called CC=7 SCAR=40.3
Pod undocumented
215 0 my ( $self, $x, $y, @list ) = @_;
216 0 ▶ croak("textvlist: coordinates must be numeric, not ($x,$y)")
Branch: 1 branch
BranchTrueFalseTotal
unless _isnum($x) and _isnum($y) 0 0 0
Condition0%0/3
_isnum($x) and _isnum($y)
!ll&&!rl&&r
000
MC/DC0%0/2
_isnum($x) and _isnum($y)
_isnum($x) _isnum($y)
Truth table0%0/3
A and B
A= _isnum($x)B= _isnum($y)
ABresult
0X0
100
111
217 unless _isnum($x) && _isnum($y);
218  
219 0 $self->translate( $x, $y );
220 0 $self->text( shift(@list) );
221 0 ▶ while ( @list ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@list) 0 0 0
222 0 my $d = shift(@list);
223 0 ▶ croak("textvlist: offset must be a number, not \"$d\"")
Branch: 1 branch
BranchTrueFalseTotal
unless _isnum($d) 0 0 0
224 unless _isnum($d);
225 0 $y -= $d;
226 0 ▶ last unless @list;
Branch: 1 branch
BranchTrueFalseTotal
unless @list 0 0 0
227 0 $self->translate( $x, $y );
228 0 $self->text( shift(@list) );
229 }
230 0 ▶ wantarray ? ( $x, $y ) : $x;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
231 }
232  
233 =head2 $text->textspread( X, Y, disp, item )
234  
235 Writes a text at the given coordinates, each individual letter
236 is horizontally offsetted by the displacement.
237  
238 Returns the coordinates of the last item written; in scalar context
239 the X coordinate only.
240  
241 =cut
242  
243 0 ▶sub textspread {
Subroutine textspread: not called CC=6 SCAR=37.4
Pod undocumented
244 0 my ( $self, $x, $y, $d, $line ) = @_;
245 0 ▶ croak("textspread: coordinates must be numeric, not ($x,$y)")
Branch: 1 branch
BranchTrueFalseTotal
unless _isnum($x) and _isnum($y) 0 0 0
Condition0%0/3
_isnum($x) and _isnum($y)
!ll&&!rl&&r
000
MC/DC0%0/2
_isnum($x) and _isnum($y)
_isnum($x) _isnum($y)
Truth table0%0/3
A and B
A= _isnum($x)B= _isnum($y)
ABresult
0X0
100
111
246 unless _isnum($x) && _isnum($y);
247 0 ▶ croak("textspread: spread must be numeric, not \"$d\"")
Branch: 1 branch
BranchTrueFalseTotal
unless _isnum($d) 0 0 0
248 unless _isnum($d);
249  
250 0 for ( split( //, $line ) ) {
251 0 $self->translate($x, $y);
252 0 $self->text($_);
253 0 $x += $d;
254 }
255 0 $x -= $d;
256 0 ▶ wantarray ? ( $x, $y ) : $x;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
257 }
258  
259 =head2 $text->textpara( X, Y, W, disp, indent, text )
260  
261 Writes a text in an rectangular area starting at X,Y and W width.
262 Lines are broken at whitespace.
263  
264 Returns the coordinates of the last item written; in scalar context
265 the Y coordinate only.
266  
267 =cut
268  
269 0 ▶sub textpara {
Subroutine textpara: not called CC=5 SCAR=34.0
Pod undocumented
270 0 my ( $self, $x, $y, $w, $d, $indent, $text ) = @_;
271  
272 0 my $t = '';
273 0 my $xx = $x;
274 0 $x += $indent;
275 0 my $l = $indent;
276  
277 # Get rid of trailing spaces.
278 0 $text =~ s/\s+$//;
279  
280 0 my @text = split( /\s+/, $text );
281 0 ▶ while ( @text ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@text) 0 0 0
282 0 my $word = shift(@text);
283 0 ▶ if ( ( $l += $self->advancewidth(" $word")) > $w ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($l += $self->advancewidth(" $word")) > $w) { } 0 0 0
284 0 $self->textline( $x, $y, $t );
285 0 $y -= $d;
286 0 $t = $word;
287 0 $x = $xx;
288 0 $l = $self->advancewidth($word)
289 }
290 else {
291 0 ▶ $t .= " " if length($t);
Branch: 1 branch
BranchTrueFalseTotal
if length $t 0 0 0
292 0 $t .= $word;
293 }
294 }
295  
296 0 $self->textline( $x, $y, $t);
297 0 ▶ wantarray ? ( $xx, $y ) : $y;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
298 }
299  
300 =head2 $text->textparas( X, Y, W, disp, indent, text )
301  
302 Writes a text in an rectangular area starting at X,Y and W width.
303 Lines are broken at whitespace.
304 A newline indicates a new paragraph start.
305  
306 Returns the coordinates of the last item written; in scalar context
307 the Y coordinate only.
308  
309 =cut
310  
311 0 ▶sub textparas {
Subroutine textparas: not called CC=3 SCAR=24.8
Pod undocumented
312 0 my ( $self, $x, $y, $w, $d, $indent, $text ) = @_;
313  
314 0 $text =~ s/\r\n/\n/g;
315 0 my @text = split( /\n/, $text );
316 0 foreach ( @text ) {
317 0 $y = $self->textpara( $x, $y, $w, $d, $indent, $_ );
318 0 $y -= $d;
319 }
320 0 ▶ wantarray ? ( $x, $y + $d ) : $y;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
321 }
322  
323 package PDF::API2::Page;
324  
325 =head1 PAGE FUNCTIONS
326  
327 The following functions operate on PDF::API2::Page objects.
328 In general, these are obtained by a call to the C<page> method on the
329 PDF document object.
330  
331 =cut
332  
333 =head2 $page->grid( [ spacing ] )
334  
335 Draws a grid of coordinates on the page.
336 Lines a black for every 100, blue otherwise.
337  
338 =cut
339  
340 0 ▶sub grid {
Subroutine grid: not called CC=5 SCAR=34.0
Pod undocumented
341 0 my ( $self, $spacing ) = @_;
342  
343 0 my $g = $self->gfx;
344 0 my $t = $self->text;
345  
346 0 $g->fillcolor('#000000');
347 0 $g->strokecolor('#000000');
348 # $g->font( ... );
349 0 ▶ my $colour = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
350 0 ▶ if ( $_[0] % 100 == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] % 100 == 0) { } 0 0 0
351 0 $g->linewidth(0.25);
352 0 $g->strokecolor('#808080');
353 }
354 else {
355 0 $g->linewidth(0.25);
356 0 $g->strokecolor('#aaaaff');
357 }
358 0 };
359  
360 0 $g->save;
361 0 ▶ $spacing ||= 100;
Condition0%0/2
$spacing ||= 100
l!l
00
MC/DC0%0/1
$spacing ||= 100
$spacing
Truth table0%0/2
A
A= $spacing
Aresult
11
00
362 0 ▶ $spacing = 20 if $spacing == 1; # for convenience
Branch: 1 branch
BranchTrueFalseTotal
if $spacing == 1 0 0 0
363  
364 0 my ( $xmax, $ymax ) = ( 600, 900 );
365 0 ▶ for ( my $x = $spacing; $x < $xmax; $x += $spacing ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $xmax) 0 0 0
366 0 $colour->($x);
367 0 $g->move($x, 0);
368 0 $g->line($x, $ymax);
369 0 $g->stroke;
370 }
371 0 ▶ for ( my $y = $spacing; $y < $ymax; $y += $spacing ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < $ymax) 0 0 0
372 0 $colour->($y);
373 0 $g->move(0, $y);
374 0 $g->line($xmax, $y);
375 0 $g->stroke;
376 # $g->move(5, $y+5);
377 # $g->text("$y");
378 }
379 0 $g->restore;
380 }
381  
382 =head1 BUGS
383  
384 There's a small memory leak for every text object that is used. For
385 normal use this can be ignored since you'll probably need just of
386 couple of text objects.
387  
388 =cut
389  
390 1 1;
↑ Summary