lib/PDF/Imposition/Schema2up.pm

filter: Statementstmt 100% 68 / 68 Branchbran 96.1% 25 / 26 Conditioncond 80.0% 12 / 15 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 92.4% 122 / 132 CC 20 SCAR 23.8
CC
20
Cov
96%
page_sequence_for_booklet
17.0
_do_impose
4.0
BEGIN
1.0
CRAP 10.8
« lib/PDF/Imposition/Schema4up.pm ↑ Summary lib/PDF/Imposition/Schema.pm »
1 package PDF::Imposition::Schema2up;
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 Types::Standard qw/Bool/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 9 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 with 'PDF::Imposition::Schema';
10  
11  
12  
13 =head1 NAME
14  
15 PDF::Imposition::Schema2up - Imposition schema 2up (booklet)
16  
17 =head1 SYNOPSIS
18  
19 use PDF::Imposition::Schema2up;
20 my $imposer = PDF::Imposition::Schema2up->new(
21 signature => "10-20",
22 file => "test.pdf",
23 output => "out.pdf",
24 cover => 1,
25 );
26 # or call the methods below to set the values, and then call:
27 $imposer->impose;
28  
29 The output pdf will be in C<$imposer->output>
30  
31 =head1 SCHEMA EXPLANATION
32  
33 This schema is a variable and dynamic method. The signature, i.e., the
34 booklets which compose the document, are not fixed-sized, but can be
35 altered. The purpose is to have 1 or more booklets that you print
36 recto-verso and just fold to have your home-made book (this schema is
37 aimed to DIY people).
38  
39 Say you have a text with 60 pages in A5: you would print it on A4,
40 double-side, take the pile out of the printer, fold it and clip it.
41  
42 The schema looks like (for a signature of 8 pages on 2 sheets):
43  
44 RECTO S.1 VERSO S.1
45 +-----+-----+ +-----+-----+
46 | | | | | |
47 | 8 | 1 | | 2 | 7 |
48 | | | | | |
49 +-----+-----+ +-----+-----+
50  
51 RECTO S.2 VERSO S.2
52 +-----+-----+ +-----+-----+
53 | | | | | |
54 | 6 | 3 | | 4 | 5 |
55 | | | | | |
56 +-----+-----+ +-----+-----+
57  
58 =head1 METHODS
59  
60 =head2 Public methods
61  
62 =head3 signature
63  
64 The signature, must be a multiple of 4, or a range, like the string
65 "20-100". If a range is selected, the signature is determined
66 heuristically to minimize the white pages left on the last signature.
67 The wider the range, the best the results.
68  
69 This is useful if you are doing batch processing, and you don't know
70 the number of page in advance (so you can't tweak the source pdf to
71 have a suitable number of page via text-block dimensions or font
72 changes).
73  
74 Typical case: you define a signature of 60 pages, and your PDF happens
75 to have 61 pages. How unfortunate, and you just can't put out a PDF
76 with 59 blank pages. The manual solution is to change something in the
77 document to get it under 60 pages, but this is not always viable or
78 desirable. So you define a dynamic range for signature, like 20-60,
79 (so the signature will vary between 20 and 60) and the routine will
80 find the best one, which in this particular case happens to be 32 (so
81 the text will have two booklets, and the second will have 3 blank
82 pages).
83  
84 Es.
85  
86 $imposer->signature("20-60");
87  
88 Keep in mind that a signature with more than 100 pages is not suitable
89 to be printed and folded at home (too thick), so to get some
90 acceptable result, the sheets must be cut and glued together by a
91 binder, so in this case you want to go with the single signature for
92 the whole pdf.
93  
94 If no signature is specified, the whole text will be imposed on a
95 single signature, regardeless of its size.
96  
97  
98 =cut
99  
100 1226 ▶sub pages_per_sheet { 4 };
Subroutine pages_per_sheet: called CC=1 SCAR=0.0
Pod documented
101  
102 =head3 cover
103  
104 This schema supports the cover option.
105  
106 =cut
107  
108 9 has cover => (is => 'rw', isa => Bool);
109  
110  
111 =head2 INTERNALS
112  
113 =head3 pages_per_sheet
114  
115 Always return 4.
116  
117 =head3 page_sequence_for_booklet($pages, $signature)
118  
119 Algorithm taken/stolen from C<psbook> (Angus J. C. Duggan 1991-1995).
120 The C<psutils> are still a viable solution if you want to go with the
121 PDF->PS->PDF route.
122  
123 =cut
124  
125 34 ▶sub page_sequence_for_booklet {
Subroutine page_sequence_for_booklet: called CC=17 SCAR=28.4
Pod documented
126 34 my ($self, $pages, $signature) = @_;
127 # if set to 0, get the actual number
128 34 ▶ $signature ||= $self->computed_signature;
Condition66%2/3
$signature ||= $self->computed_signature
l!l&&r!l&&!r
1330
MC/DC0%0/2
$signature ||= $self->computed_signature
$signature $self->computed_signature
Truth table66%2/3
A ||= B
A= $signatureB= $self->computed_signature
ABresult
1X1
011
000
129 34 ▶ $pages ||= $self->total_pages;
Condition66%2/3
$pages ||= $self->total_pages
l!l&&r!l&&!r
2320
MC/DC0%0/2
$pages ||= $self->total_pages
$pages $self->total_pages
Truth table66%2/3
A ||= B
A= $pagesB= $self->total_pages
ABresult
1X1
011
000
130 34 my $maxpage = $self->total_output_pages;
131 34 my @pgs;
132 {
133 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
134 34 ▶ for (my $currentpg = 0; $currentpg < $maxpage; $currentpg++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($currentpg < $maxpage) 484 34 518
135 484 my $actualpg = $currentpg - ($currentpg % $signature);
136 484 my $modulo = $currentpg % 4;
137 484 ▶ if ($modulo == 0 or $modulo == 3) {
Branch: 2 branches
BranchTrueFalseTotal
if ($modulo == 0 or $modulo == 3) { } 242 242 484
elsif ($modulo == 1 or $modulo == 2) { } 242 0 242
Condition100%3/3
$modulo == 0 or $modulo == 3
l!l&&r!l&&!r
121121242
MC/DC100%2/2
$modulo == 0 or $modulo == 3
$modulo == 0 $modulo == 3
Truth table100%3/3
A or B
A= $modulo == 0B= $modulo == 3
ABresult
1X1
011
000
138 242 $actualpg += $signature - 1 - (($currentpg % $signature) / 2);
139 }
140 ▶ elsif ($modulo == 1 or $modulo == 2) {
Condition66%2/3
$modulo == 1 or $modulo == 2
l!l&&r!l&&!r
1211210
MC/DC0%0/2
$modulo == 1 or $modulo == 2
$modulo == 1 $modulo == 2
Truth table66%2/3
A or B
A= $modulo == 1B= $modulo == 2
ABresult
1X1
011
000
141 242 $actualpg += ($currentpg % $signature) / 2;
142 }
143 484 ▶ if ($actualpg < $pages) {
Branch: 1 branch
BranchTrueFalseTotal
if ($actualpg < $pages) { } 447 37 484
144 447 $actualpg++;
145 } else {
146 37 $actualpg = undef;
147 }
148 484 push @pgs, $actualpg;
149 }
150 }
151 34 my @out;
152 # if we want a cover, we need to find the index of the last page,
153 # and the first undef page, which could be at the beginning of the
154 # last signature, so we have to scan the array.
155 34 ▶ if ($self->cover) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cover) 15 19 34
156 15 my $last;
157 my $firstundef;
158  
159 # find the last page
160 15 ▶ for (my $i = 0; $i < @pgs; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @pgs) 220 15 235
161 220 ▶ if ($pgs[$i] and $pgs[$i] == $pages) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pgs[$i] and $pgs[$i] == $pages) 15 205 220
Condition100%3/3
$pgs[$i] and $pgs[$i] == $pages
!ll&&!rl&&r
1918615
MC/DC100%2/2
$pgs[$i] and $pgs[$i] == $pages
$pgs[$i] $pgs[$i] == $pages
Truth table100%3/3
A and B
A= $pgs[$i]B= $pgs[$i] == $pages
ABresult
0X0
100
111
162 15 $last = $i;
163 }
164 }
165  
166 # find the first empty page (inserted by us)
167 15 ▶ for (my $i = 0; $i < @pgs; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @pgs) 130 9 139
168 130 ▶ if (not defined $pgs[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pgs[$i]) 6 124 130
169 6 $firstundef = $i;
170 6 last;
171 }
172 }
173  
174 # if we don't find a white page, there is nothing to do
175 15 ▶ if (defined $firstundef) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $firstundef) 6 9 15
176 # there is an undef, so swap;
177 6 $pgs[$firstundef] = $pgs[$last];
178 6 $pgs[$last] = undef;
179 }
180 }
181 34 ▶ while (@pgs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pgs) 242 34 276
182 242 push @out, [ shift(@pgs), shift(@pgs) ];
183 }
184 34 return \@out;
185 }
186  
187 22 ▶sub _do_impose {
Subroutine _do_impose: called CC=4 SCAR=13.9
188 22 my $self = shift;
189 # prototype
190 22 $self->out_pdf_obj->mediabox(
191 $self->orig_width * 2,
192 $self->orig_height,
193 );
194 21 my $seq = $self->page_sequence_for_booklet;
195 21 foreach my $p (@$seq) {
196 # loop over the pages
197 150 my $left = $self->get_imported_page($p->[0]);
198 150 my $right = $self->get_imported_page($p->[1]);
199 150 my $page = $self->out_pdf_obj->page();
200 150 my $gfx = $page->gfx();
201 150 ▶ if (defined $left) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $left) 138 12 150
202 138 $gfx->formimage($left, 0, 0);
203 }
204 150 ▶ if (defined $right) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $right) 141 9 150
205 141 $gfx->formimage($right, $self->orig_width, 0);
206 }
207 }
208 }
209  
210 =head1 INTERNALS
211  
212 =head2 cropmarks_options
213  
214 Set inner to false.
215  
216 =cut
217  
218  
219 15 ▶sub cropmarks_options {
Subroutine cropmarks_options: called CC=1 SCAR=0.0
Pod documented
220 15 my %options = (
221 top => 1,
222 bottom => 1,
223 inner => 0,
224 outer => 1,
225 twoside => 1,
226 );
227 15 return %options;
228 }
229  
230  
231 9 1;
232  
233 =head1 SEE ALSO
234  
235 L<PDF::Imposition>
236  
237 =cut
238  
« lib/PDF/Imposition/Schema4up.pm ↑ Summary lib/PDF/Imposition/Schema.pm »