lib/PDF/Imposition/Schema.pm

filter: Statementstmt 95.6% 174 / 182 Branchbran 70.8% 51 / 72 Conditioncond 60.8% 14 / 23 MC/DCmcdc 28.5% 4 / 14 Subroutinesub 94.4% 34 / 36 Podpod 100% 9 / 9 totaltotal 85.1% 286 / 336 CC 45 SCAR 19.1
CC
45
Cov
86%
_optimize_signature
19.8
_find_signature
6.6
_build_in_pdf_obj
5.1
CRAP 6.7
« lib/PDF/Imposition/Schema2up.pm ↑ Summary lib/PDF/Imposition/Schema1x8x2.pm »
1 package PDF::Imposition::Schema;
2 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 9 ▶use File::Basename qw/fileparse/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use PDF::API2;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Types::Standard qw/Int Bool Num HashRef Str Maybe Object Enum/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 9 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 requires qw(_do_impose);
15  
16 9 ▶use constant { DEBUG => $ENV{AMW_DEBUG} };
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 =head1 NAME
19  
20 PDF::Imposition::Schema - Role for the imposition schemas.
21  
22 =head1 SYNOPSIS
23  
24 This class provides the shared method for real imposition schemas and
25 can't me called directly.
26  
27 Consuming classes must provide a C<_do_impose> method.
28  
29 use PDF::Imposition;
30 my $imposer = PDF::Imposition->new(file => "test.pdf",
31 # either use
32 outfile => "out.pdf",
33 # or suffix
34 suffix => "-2up"
35 );
36 $imposer->impose;
37 or
38  
39 use PDF::Imposition;
40 my $imposer = PDF::Imposition->new();
41 $imposer->file("test.pdf");
42
43 $imposer->outfile("out.pdf");
44 # or
45 $imposer->suffix("-imp");
46  
47 $imposer->impose;
48
49 =head1 METHODS
50  
51 =head2 Read/write accessors
52  
53 All the following accessors accept an argument, which sets the
54 value.
55  
56 =head3 file
57  
58 Unsurprisingly, the input file, which must exist.
59  
60 =cut
61  
62 has _version => (is => 'ro',
63 isa => Str,
64 0 ▶ default => sub { 'PDF::Imposition' });
Subroutine __ANON__: not called CC=1 SCAR=1.2
65  
66 has file => (is => 'rw',
67 102 ▶ isa => sub { die "$_[0] is not a pdf" unless $_[0] && $_[0] =~ m/\.pdf\z/i });
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] and $_[0] =~ /\.pdf\z/i 3 99 102
Condition100%3/3
$_[0] and $_[0] =~ /\.pdf\z/i
!ll&&!rl&&r
1299
MC/DC100%2/2
$_[0] and $_[0] =~ /\.pdf\z/i
$_[0] $_[0] =~ /\.pdf\z/i
Truth table100%3/3
A and B
A= $_[0]B= $_[0] =~ /\.pdf\z/i
ABresult
0X0
100
111
68  
69 has _tmp_dir => (is => 'ro',
70 118 ▶ default => sub { File::Temp->newdir(CLEANUP => 1); });
Subroutine __ANON__: called CC=1 SCAR=0.0
71  
72 =head3 outfile
73  
74 The destination file of the imposition. You may prefer to use the
75 suffix method below, which takes care of the filename.
76  
77 =head3 suffix
78  
79 The suffix of the file. By default, '-imp', so test.pdf imposed will
80 be saved as 'test-imp.pdf'. If test-imp.pdf already exists, it will be
81 replaced merciless.
82  
83 =cut
84  
85 has outfile => (is => 'rw',
86 123 ▶ isa => sub { die "$_[0] is not a pdf file"
Subroutine __ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] and $_[0] =~ /\.pdf\z/i 0 123 123
Condition33%1/3
$_[0] and $_[0] =~ /\.pdf\z/i
!ll&&!rl&&r
00123
MC/DC0%0/2
$_[0] and $_[0] =~ /\.pdf\z/i
$_[0] $_[0] =~ /\.pdf\z/i
Truth table33%1/3
A and B
A= $_[0]B= $_[0] =~ /\.pdf\z/i
ABresult
0X0
100
111
87 9 unless $_[0] && $_[0] =~ m/\.pdf\z/i });
88  
89 has suffix => (is => 'rw',
90 isa => Str,
91 114 ▶ default => sub { '-imp' });
Subroutine __ANON__: called CC=1 SCAR=0.0
92  
93 =head3 signature($num_or_range)
94  
95 The signature, must be a multiple of the C<pages_per_sheet> option
96 (usually 4 or 8), or a range, like the string "20-100". If a range is
97 selected, the signature is determined heuristically to minimize the
98 white pages left on the last signature. The wider the range, the better
99 the results.
100  
101 =cut
102  
103 has signature => (is => 'rw',
104 isa => Str,
105 107 ▶ default => sub { '0' });
Subroutine __ANON__: called CC=1 SCAR=0.0
106  
107 =head3 pages_per_sheet
108  
109 The number of logical pages which fit on a sheet, recto-verso. Default
110 to 1. Subclasses usually change this and ignore your option unless
111 otherwise specified.
112  
113 =head3 title
114  
115 The title to set in the PDF meta information. Defaults to the basename.
116  
117 =cut
118  
119 has pages_per_sheet => (is => 'ro',
120 113 ▶ default => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
121 9 isa => Enum[qw/1 2 4 8 16 32/]);
122  
123 475 ▶sub _optimize_signature {
Subroutine _optimize_signature: called CC=18 SCAR=29.8
124 475 my ($self, $sig, $total_pages) = @_;
125 475 ▶ unless ($total_pages) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($total_pages) 25 450 475
126 25 $total_pages = $self->total_pages;
127 }
128 470 ▶ return 0 unless $sig;
Branch: 1 branch
BranchTrueFalseTotal
unless $sig 0 470 470
129 470 ▶ my $ppsheet = $self->pages_per_sheet or die;
Branch: 1 branch
BranchTrueFalseTotal
my $ppsheet = $self->pages_per_sheet or die 470 0 470
130 470 print "# pages per sheet is $ppsheet\n" if DEBUG;
131 470 ▶ if ($sig =~ m/^[0-9]+$/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig =~ /^[0-9]+$/s) 17 453 470
132 17 ▶ die "Signature must be a multiple of $ppsheet" if $sig % $ppsheet;
Branch: 1 branch
BranchTrueFalseTotal
if $sig % $ppsheet 0 17 17
133 17 return $sig;
134 }
135 453 my ($min, $max);
136 453 ▶ if ($sig =~ m/^([0-9]+)?-([0-9]+)?$/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig =~ /^([0-9]+)?-([0-9]+)?$/s) { } 453 0 453
137 453 ▶ $min = $1 || $ppsheet;
Condition66%2/3
$1 || $ppsheet
l!l&&r!l&&!r
45030
MC/DC0%0/2
$1 || $ppsheet
$1 $ppsheet
Truth table66%2/3
A || B
A= $1B= $ppsheet
ABresult
1X1
011
000
138 453 ▶ $max = $2 || $total_pages;
Condition33%1/3
$2 || $total_pages
l!l&&r!l&&!r
45300
MC/DC0%0/2
$2 || $total_pages
$2 $total_pages
Truth table33%1/3
A || B
A= $2B= $total_pages
ABresult
1X1
011
000
139 453 $min = $min + (($ppsheet - ($min % $ppsheet)) % $ppsheet);
140 453 $max = $max + (($ppsheet - ($max % $ppsheet)) % $ppsheet);
141 453 ▶ die "Bad range $max - $min" unless $max > $min;
Branch: 1 branch
BranchTrueFalseTotal
unless $max > $min 0 453 453
142 453 ▶ die "bad min $min" if $min % $ppsheet;
Branch: 1 branch
BranchTrueFalseTotal
if $min % $ppsheet 0 453 453
143 453 ▶ die "bad max $max" if $max % $ppsheet;
Branch: 1 branch
BranchTrueFalseTotal
if $max % $ppsheet 0 453 453
144 }
145 else {
146 0 die "Unrecognized range $sig";
147 }
148 453 my $signature = 0;
149 453 my $roundedpages = $total_pages + (($ppsheet - ($total_pages % $ppsheet)) % $ppsheet);
150 453 my $needed = $roundedpages - $total_pages;
151 453 ▶ die "Something is wrong" if $roundedpages % $ppsheet;
Branch: 1 branch
BranchTrueFalseTotal
if $roundedpages % $ppsheet 0 453 453
152 453 ▶ if ($roundedpages <= $min) {
Branch: 1 branch
BranchTrueFalseTotal
if ($roundedpages <= $min) 90 363 453
153 90 ▶ wantarray ? return ($roundedpages, $needed) : return $roundedpages;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 40 50 90
154 }
155 363 $signature = $self->_find_signature($roundedpages, $max);
156 363 ▶ if ($roundedpages > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($roundedpages > $max) 340 23 363
157 340 ▶ while ($signature < $min) {
Branch: 1 branch
BranchTrueFalseTotal
if ($signature < $min) 344 340 684
158 344 $roundedpages += $ppsheet;
159 344 $needed += $ppsheet;
160 344 $signature = $self->_find_signature($roundedpages, $max)
161 }
162 }
163 # warn "Needed $needed blank pages";
164 363 ▶ wantarray ? return ($signature, $needed) : return $signature;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 360 3 363
165 }
166  
167 707 ▶sub _find_signature {
Subroutine _find_signature: called CC=6 SCAR=18.8
168 707 my ($self, $num, $max) = @_;
169 707 ▶ my $ppsheet = $self->pages_per_sheet or die;
Branch: 1 branch
BranchTrueFalseTotal
my $ppsheet = $self->pages_per_sheet or die 707 0 707
170 707 ▶ die "not a multiple of $ppsheet" if $num % $ppsheet;
Branch: 1 branch
BranchTrueFalseTotal
if $num % $ppsheet 0 707 707
171 707 ▶ die "uh?" unless $num;
Branch: 1 branch
BranchTrueFalseTotal
unless $num 0 707 707
172 707 my $i = $max;
173 707 ▶ while ($i > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 5528 0 5528
174 # check if the the pagenumber is divisible by the signature
175 # with modulo 0
176 # warn "trying $i for $num / max $max\n";
177 5528 ▶ if (($num % $i) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num % $i == 0) 707 4821 5528
178 707 return $i;
179 }
180 4821 $i -= $ppsheet;
181 }
182 0 warn "_find_signature loop ended with no result\n";
183 }
184  
185  
186  
187 =head2 Internal methods accessors
188  
189 The following methods are used internally but documented for schema's
190 authors.
191  
192 =head3 dimensions
193  
194 Returns an hashref with the original pdf dimensions in points.
195  
196 { w => 800, h => 600 }
197  
198 =head3 orig_width
199  
200 =head3 orig_height
201  
202 =head3 total_pages
203  
204 Returns the number of pages
205  
206  
207 =cut
208  
209 9 has dimensions => (is => 'lazy',
210 isa => HashRef[Num]);
211  
212 63 ▶sub _build_dimensions {
Subroutine _build_dimensions: called CC=4 SCAR=14.8
213 63 my $self = shift;
214 63 my $pdf = $self->in_pdf_obj;
215 63 my ($x, $y, $w, $h) = $pdf->openpage(1)->get_mediabox; # use the first page
216 63 ▶ warn $self->file . "use x-y offset, cannot proceed safely" if ($x + $y);
Branch: 1 branch
BranchTrueFalseTotal
if $x + $y 0 63 63
217 63 ▶ die "Cannot retrieve paper dimensions" unless $w && $h;
Branch: 1 branch
BranchTrueFalseTotal
unless $w and $h 0 63 63
Condition33%1/3
$w and $h
!ll&&!rl&&r
0063
MC/DC0%0/2
$w and $h
$w $h
Truth table33%1/3
A and B
A= $wB= $h
ABresult
0X0
100
111
218 63 my %dimensions = (
219 w => sprintf('%.2f', $w),
220 h => sprintf('%.2f', $h),
221 );
222 # return a copy
223 63 return \%dimensions;
224 }
225  
226 9 has total_pages => (is => 'lazy',
227 isa => Int);
228  
229 96 ▶sub _build_total_pages {
Subroutine _build_total_pages: called CC=1 SCAR=0.0
230 96 my $self = shift;
231 96 my $count = $self->in_pdf_obj->pages;
232 91 return $count;
233 }
234  
235 483 ▶sub orig_width {
Subroutine orig_width: called CC=1 SCAR=0.0
Pod documented
236 483 return shift->dimensions->{w};
237 }
238  
239 298 ▶sub orig_height {
Subroutine orig_height: called CC=1 SCAR=0.0
Pod documented
240 298 return shift->dimensions->{h};
241 }
242  
243  
244  
245 =head3 in_pdf_obj
246  
247 Internal usage. It's the PDF::API2 object used as source.
248  
249 =head3 out_pdf_obj
250  
251 Internal usage. The PDF::API2 object used as output.
252  
253 =cut
254  
255 9 has in_pdf_obj => (is => 'lazy',
256 isa => Maybe[Object]);
257  
258 9 has _in_pdf_object_is_open => (is => 'rw', isa => Bool);
259  
260 96 ▶sub _build_in_pdf_obj {
Subroutine _build_in_pdf_obj: called CC=5 SCAR=16.4
261 96 my $self = shift;
262 96 my $input;
263 96 ▶ if ($self->file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->file) 91 5 96
264 91 ▶ die "File " . $self->file . " doesn't exists" unless -f $self->file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $self->file 0 91 91
265 91 print $self->file . ": building in_pdf_obj\n" if DEBUG;
266 91 my ($basename, $path, $suff) = fileparse($self->file, qr{\.pdf}i);
267 91 my $tmpfile = File::Spec->catfile($self->_tmp_dir,
268 $basename . $suff);
269 91 ▶ copy($self->file, $tmpfile) or die "copy to $tmpfile failed $!";
Branch: 1 branch
BranchTrueFalseTotal
copy($self->file, $tmpfile) or die "copy to $tmpfile failed $!" 91 0 91
270  
271 91 eval {
272 91 $input = PDF::API2->open($tmpfile);
273 };
274 91 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 91 91
275 0 die "Couldn't open $tmpfile $@";
276 }
277 else {
278 91 print "$tmpfile built\n" if DEBUG;
279 }
280 91 $self->_in_pdf_object_is_open(1);
281 }
282 96 return $input;
283 }
284  
285 9 has out_pdf_obj => (is => 'lazy',
286 isa => Maybe[Object]);
287  
288 9 has _out_pdf_object_is_open => (is => 'rw', isa => Bool);
289  
290 9 has title => (is => 'ro',
291 isa => Maybe[Str]);
292  
293 64 ▶sub _build_out_pdf_obj {
Subroutine _build_out_pdf_obj: called CC=3 SCAR=11.0
294 64 my $self = shift;
295 64 my $pdf;
296 64 ▶ if ($self->file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->file) 64 0 64
297 64 ▶ die "File " . $self->file . " is not a file" unless -f $self->file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $self->file 1 63 64
298 63 print $self->file . ": building out_pdf_object\n" if DEBUG;
299 63 $pdf = PDF::API2->new();
300 63 my %info = (
301 $self->in_pdf_obj->info,
302 Creator => $self->_version,
303 CreationDate => $self->_orig_file_timestamp,
304 ModDate => $self->_now_timestamp,
305 );
306 63 $pdf->info(%info);
307 63 $self->_out_pdf_object_is_open(1);
308 }
309 63 return $pdf;
310 }
311  
312 =head3 get_imported_page($pagenumber)
313  
314 Retrieve the page form object from the input pdf to the output pdf,
315 and return it. The method return undef if the page is out of range.
316  
317 =cut
318  
319  
320 936 ▶sub get_imported_page {
Subroutine get_imported_page: called CC=4 SCAR=14.0
Pod documented
321 936 my ($self, $page) = @_;
322 936 ▶ if ((!defined $page) || ($page <= 0) || ($page > $self->total_pages)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $page or $page <= 0 or $page > $self->total_pages) 101 835 936
Condition66%4/6
not defined $page or $page <= 0 or $page > $self->total_pages
l!l&&r!l&&!r
1010835
not defined $page or $page <= 0
l!l&&r!l&&!r
1010835
MC/DC33%1/3
not defined $page or $page <= 0 or $page > $self->total_pages
not defined $page $page <= 0 $page > $self->total_pages
Truth table50%2/4
not(A) or B or C
A= not defined $pageB= $page <= 0C= $page > $self->total_pages
ABCresult
1XX1
01X1
0011
0000
323 101 return undef;
324 }
325 835 return $self->out_pdf_obj->importPageIntoForm($self->in_pdf_obj, $page)
326 }
327  
328 =head3 impose
329  
330 Do the job and leave the output in C<< $self->outfile >>, cleaning up
331 the internal objects.
332  
333 =cut
334  
335 64 ▶sub impose {
Subroutine impose: called CC=1 SCAR=0.0
Pod documented
336 64 my $self = shift;
337 64 my $out = $self->output_filename;
338 64 $self->_do_impose;
339 63 $self->out_pdf_obj->saveas($out);
340 63 $self->out_pdf_obj->end;
341 63 $self->_out_pdf_object_is_open(0);
342 63 $self->in_pdf_obj->end;
343 63 $self->_in_pdf_object_is_open(0);
344 63 $self->outfile($out);
345 63 return $out;
346 }
347  
348 =head3 output_filename
349  
350 If outfile is not provided, use the suffix provided and return the
351 filename.
352  
353 =cut
354  
355 69 ▶sub output_filename {
Subroutine output_filename: called CC=3 SCAR=11.0
Pod documented
356 69 my $self = shift;
357 69 my $out = $self->outfile;
358 69 ▶ unless ($out) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($out) 43 26 69
359 43 my ($name, $path, $suffix) = fileparse($self->file, qr{\.pdf}i);
360 43 ▶ die $self->file . " has a suffix not recognized" unless $suffix;
Branch: 1 branch
BranchTrueFalseTotal
unless $suffix 0 43 43
361 43 $out = File::Spec->catfile($path, $name . $self->suffix . $suffix);
362 }
363 69 return $out;
364 }
365  
366  
367 63 ▶sub _orig_file_timestamp {
Subroutine _orig_file_timestamp: called CC=1 SCAR=0.0
368 63 my $self = shift;
369 63 my $mtime = (stat($self->file))[9];
370 63 return $self->_format_timestamp($mtime);
371 }
372  
373 63 ▶sub _now_timestamp {
Subroutine _now_timestamp: called CC=1 SCAR=0.0
374 63 return shift->_format_timestamp(time());
375 }
376  
377 126 ▶sub _format_timestamp {
Subroutine _format_timestamp: called CC=1 SCAR=0.0
378 126 my ($self, $epoc) = @_;
379 126 return POSIX::strftime(q{%Y%m%d%H%M%S+00'00'}, localtime($epoc));
380 }
381  
382 =head3 computed_signature
383  
384 Return the actual number of signature, resolving 0 to the nearer
385 signature.
386  
387 =head3 total_output_pages
388  
389 Return the computed number of pages of the output, taking in account
390 the signature handling.
391  
392 =head3 DEMOLISH
393  
394 Object cleanup.
395  
396 =head3 DEBUG
397  
398 Constant picked from AMW_DEBUG environment.
399  
400 =cut
401  
402 131 ▶sub computed_signature {
Subroutine computed_signature: called CC=3 SCAR=11.0
Pod documented
403 131 my $self = shift;
404 131 ▶ my $signature = $self->signature || 0;
Condition100%2/2
$self->signature || 0
l!l
25106
MC/DC100%1/1
$self->signature || 0
$self->signature
Truth table100%2/2
A
A= $self->signature
Aresult
11
00
405 131 ▶ if ($signature) {
Branch: 1 branch
BranchTrueFalseTotal
if ($signature) { } 25 106 131
406 25 return $self->_optimize_signature($self->signature) + 0;
407 }
408 else {
409 106 my $pages = $self->total_pages;
410 106 my $ppsheet = $self->pages_per_sheet;
411 106 return $pages + (($ppsheet - ($pages % $ppsheet)) % $ppsheet);
412 }
413 }
414  
415 45 ▶sub total_output_pages {
Subroutine total_output_pages: called CC=1 SCAR=0.0
Pod documented
416 45 my $self = shift;
417 45 my $pages = $self->total_pages;
418 45 my $signature = $self->computed_signature;
419 45 return $pages + (($signature - ($pages % $signature)) % $signature);
420 }
421  
422 =head3 cropmarks_options
423  
424 By default, cropmarks are centered and twoside is true.
425  
426 =cut
427  
428  
429 0 ▶sub cropmarks_options {
Subroutine cropmarks_options: not called CC=1 SCAR=6.9
Pod documented
430 0 my %options = (
431 top => 1,
432 bottom => 1,
433 inner => 1,
434 outer => 1,
435 twoside => 1,
436 );
437 0 return %options;
438 }
439  
440 118 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=3 SCAR=11.6
Pod documented
441 118 my $self = shift;
442 118 ▶ if ($self->_out_pdf_object_is_open) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_out_pdf_object_is_open) 0 118 118
443 0 print $self->file . ": closing outpdf object\n" if DEBUG;
444 0 $self->out_pdf_obj->end;
445 }
446 118 ▶ if ($self->_in_pdf_object_is_open) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_in_pdf_object_is_open) 28 90 118
447 28 print $self->file . ": closing inpdf object\n" if DEBUG;
448 28 $self->in_pdf_obj->end;
449 }
450 }
451  
452 9 1;
453  
454 =head1 SEE ALSO
455  
456 L<PDF::Imposition>
457  
458 =cut
459  
460  
« lib/PDF/Imposition/Schema2up.pm ↑ Summary lib/PDF/Imposition/Schema1x8x2.pm »