lib/PDF/Kit.pm

filter: Statementstmt 11.0% 36 / 325 Branchbran 0.0% 0 / 108 Conditioncond 0.0% 0 / 59 MC/DCmcdc 0.0% 0 / 34 Subroutinesub 39.1% 9 / 23 Podpod 100% 13 / 13 totaltotal 10.3% 58 / 562 CC 94 SCAR 58.2
CC
94
Cov
7%
format_paragraph
930.0
flatten
380.0
print_paragraph
210.0
CRAP 337.7
↑ Summary script/pdf_kit_eg.pl »
1 #!/
2 # --------------------------------------
3 #
4 # Title: PDF Kit
5 # Purpose: A collection of subroutines for PDF::API2.
6 #
7 # Name: PDF::Kit
8 # File: Kit.pm
9 # Created: June 2, 2009
10 #
11 # Copyright: Copyright 2009 by Shawn H. Corey. All rights reserved.
12 #
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation, version 3 of the License, or
16 # (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26  
27 # --------------------------------------
28 # Package
29 package PDF::Kit;
30  
31 # --------------------------------------
32 # Pragmas
33  
34 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 # --------------------------------------
38 # Version
39 2 ▶use version; our $VERSION = qv(v1.0.5);
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 # --------------------------------------
42 # Exports
43 2 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
44 2 our @EXPORT = qw(
45 in2pts
46 cm2pts
47 mm2pts
48 baselines
49 column_blocks
50 small_caps
51 flatten
52 as_text
53 format_paragraph
54 align_lines
55 justify_lines
56 print_lines
57 print_paragraph
58 );
59 2 our @EXPORT_OK = qw(
60 add_fonts
61 );
62 2 our %EXPORT_TAGS = (
63 all => [ @EXPORT, @EXPORT_OK ],
64 );
65  
66 # --------------------------------------
67 # Modules
68 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 2 ▶use English qw( -no_match_vars ) ; # Avoids regex performance penalty
Subroutine BEGIN: called CC=1 SCAR=0.0
71 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 2 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
73  
74 # --------------------------------------
75 # Configuration Parameters
76  
77 2 my $SPACE = "\x20";
78  
79 # Make Data::Dumper pretty
80 2 $Data::Dumper::Sortkeys = 1;
81 2 $Data::Dumper::Indent = 1;
82 2 $Data::Dumper::Maxdepth = 0;
83  
84 # --------------------------------------
85 # Variables
86  
87 # --------------------------------------
88 # Subroutines
89  
90 # --------------------------------------
91 # Name: in2pts
92 # Usage: @points = in2pts( @inches );
93 # $points = in2pts( @inches );
94 # Purpose: Convert inches to points.
95 # Parameters: @inches -- List of numbers in inches.
96 # Returns: $points -- Single value in points.
97 # @points -- List of values in points.
98 #
99 0 ▶sub in2pts {
Subroutine in2pts: not called CC=3 SCAR=24.8
Pod documented
100 0 my @points = @_;
101  
102 0 $_ *= 72 for @points;
103 0 ▶ return wantarray ? @points : $points[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
104 }
105  
106 # --------------------------------------
107 # Name: cm2pts
108 # Usage: @points = cm2pts( @cm );
109 # $points = cm2pts( @cm );
110 # Purpose: Convert centimetres to points.
111 # Parameters: @cm -- List of numbers in centimetres.
112 # Returns: $points -- Single value in points.
113 # @points -- List of values in points.
114 #
115 0 ▶sub cm2pts {
Subroutine cm2pts: not called CC=3 SCAR=24.8
Pod documented
116 0 my @points = @_;
117  
118 0 $_ *= 72/2.54 for @points;
119 0 ▶ return wantarray ? @points : $points[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
120 }
121  
122 # --------------------------------------
123 # Name: mm2pts
124 # Usage: @points = mm2pts( @mm );
125 # $points = mm2pts( @mm );
126 # Purpose: Convert millimetres to points.
127 # Parameters: @mm -- List of numbers in millimetres.
128 # Returns: $points -- Single value in points.
129 # @points -- List of values in points.
130 #
131 0 ▶sub mm2pts {
Subroutine mm2pts: not called CC=3 SCAR=24.8
Pod documented
132 0 my @points = @_;
133  
134 0 $_ *= 72/25.4 for @points;
135 0 ▶ return wantarray ? @points : $points[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
136 }
137  
138 # --------------------------------------
139 # Name: baselines
140 # Usage: @y_values = baselines( $height, $size; $spacing, $bottom );
141 # Purpose: Compute the Y values for the baselines.
142 # Parameters: $height -- The height of the box.
143 # $size -- Size of the font.
144 # $spacing -- Optional line spacing. Typical values are 1.0, 1.5, 2.0. Default is 1.0.
145 # $bottom -- Optional bottom margin; will be added to the Y values.
146 # Returns: @y_values -- List of Y values.
147 #
148 0 ▶sub baselines {
Subroutine baselines: not called CC=4 SCAR=30.0
Pod documented
149 0 my $height = shift @_;
150 0 my $size = shift @_;
151 0 ▶ my $spacing = shift @_ || 1;
Condition0%0/2
shift @_ || 1
l!l
00
MC/DC0%0/1
shift @_ || 1
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
152 0 ▶ my $bottom = shift @_ || 0;
Condition0%0/2
shift @_ || 0
l!l
00
MC/DC0%0/1
shift @_ || 0
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
153 0 my $leading = $size * $spacing;
154  
155 0 my @y_values = ();
156 0 my $y = $height - $size;
157 0 ▶ while( $y >= 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($y >= 0) 0 0 0
158 0 push @y_values, $y + $bottom;
159 0 $y -= $leading;
160 }
161  
162 0 return @y_values;
163 }
164  
165 # --------------------------------------
166 # Name: column_blocks
167 # Usage: \@blocks = column_blocks( \@block, $columns; $gap );
168 # Purpose: Calculate the block of the column that fit in the given block
169 # Parameters: \@block -- [ min_x, min_y, max_x, max_y ]
170 # $columns -- Number of columns
171 # $gap -- Gap between columns; optional, default is 0 (zero).
172 # Returns: \@blocks -- List of columns
173 #
174 0 ▶sub column_blocks {
Subroutine column_blocks: not called CC=3 SCAR=24.8
Pod documented
175 0 my $block = shift @_;
176 0 my $columns = shift @_;
177 0 ▶ my $gap = shift @_ || 0;
Condition0%0/2
shift @_ || 0
l!l
00
MC/DC0%0/1
shift @_ || 0
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
178 0 my $blocks = [];
179  
180 0 my $width = ( $block->[2] - $block->[0] - $gap * ( $columns - 1 ) ) / $columns;
181  
182 0 my $offset = $block->[0];
183 0 for my $i ( 1 .. $columns ){
184 0 push @$blocks, [ @$block ]; # make a copy
185 0 $blocks->[-1][0] = $offset;
186 0 $offset += $width;
187 0 $blocks->[-1][2] = $offset;
188 0 $offset += $gap;
189 }
190  
191 0 return $blocks;
192 }
193  
194 # --------------------------------------
195 # Name: small_caps
196 # Usage: \@mut = small_caps( $size, $factor, @text );
197 # Purpose: Convert the text to small caps.
198 # Parameters: $size -- Size of resulting text.
199 # $factor -- The relative size of the lowercase characters.
200 # Recommended factors are from 0.65 to 0.75.
201 # @text -- List of text items.
202 # Returns: \@mut -- L<Mark-up Text>.
203 #
204 0 ▶sub small_caps {
Subroutine small_caps: not called CC=6 SCAR=37.4
Pod documented
205 0 my $size = shift @_;
206 0 my $factor = shift @_;
207 0 my @text = @_;
208 0 my $mut = [];
209 0 my $lc_size = $size * $factor;
210 0 my $prev = '';
211  
212 0 for my $text ( @text ){
213 0 for my $c ( split( //, $text ) ){
214 0 ▶ if( isupper( $c ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (isupper($c)) { } 0 0 0
215 0 ▶ if( $prev ne 'u' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($prev ne "u") 0 0 0
216 0 push @$mut, { -size=>$size };
217 0 $prev = 'u';
218 }
219 0 push @$mut, $c;
220 }else{
221 0 ▶ if( $prev ne 'l' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($prev ne "l") 0 0 0
222 0 push @$mut, { -size=>$lc_size };
223 0 $prev = 'l';
224 }
225 0 push @$mut, uc( $c );
226 }
227 }
228 }
229  
230 0 return [ $mut ];
231 }
232  
233 # --------------------------------------
234 # Name: flatten
235 # Usage: \@flattened = flatten( @attributed_text );
236 # Purpose: Change nested attributed text into flatten attributed text.
237 # Parameters: @attributed_text -- nested attributed text
238 # Returns: \@flattened -- flatten attributed text
239 #
240 0 ▶sub flatten {
Subroutine flatten: not called CC=19 SCAR=59.4
Pod documented
241 0 my $list = [];
242 0 my @context = (
243 {
244 -opts => {},
245 -items => [ @_ ],
246 }
247 );
248  
249 0 ▶ while( @context ){
Branch: 1 branch
BranchTrueFalseTotal
if (@context) 0 0 0
250 0 my %opts = %{ $context[-1]{-opts} };
251 0 my $items = $context[-1]{-items};
252 0 pop @context;
253  
254 0 ▶ if( @$list && ref( $list->[-1] ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (@$list and ref $list->[-1]) { } 0 0 0
Condition0%0/3
@$list and ref $list->[-1]
!ll&&!rl&&r
000
MC/DC0%0/2
@$list and ref $list->[-1]
@$list ref $list->[-1]
Truth table0%0/3
A and B
A= @$listB= ref $list->[-1]
ABresult
0X0
100
111
255 0 $list->[-1] = { %opts };
256 }else{
257 0 push @$list, { %opts };
258 }
259  
260 0 ▶ while( @$items ){
Branch: 1 branch
BranchTrueFalseTotal
if (@$items) 0 0 0
261 0 my $item = shift @$items;
262  
263 0 ▶ if( my $ref = ref( $item ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $item) { } 0 0 0
264 0 ▶ if( $ref eq 'ARRAY' ){
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 0 0 0
elsif ($ref eq 'HASH') { } 0 0 0
265 0 push @context, {
266 -opts => { %opts },
267 -items => [ @$items ],
268 };
269 0 $items = $item;
270 }elsif( $ref eq 'HASH' ){
271 0 @opts{ keys %$item } = values %$item;
272 0 ▶ if( @$list && ref( $list->[-1] ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (@$list and ref $list->[-1]) { } 0 0 0
Condition0%0/3
@$list and ref $list->[-1]
!ll&&!rl&&r
000
MC/DC0%0/2
@$list and ref $list->[-1]
@$list ref $list->[-1]
Truth table0%0/3
A and B
A= @$listB= ref $list->[-1]
ABresult
0X0
100
111
273 0 $list->[-1] = { %opts };
274 }else{
275 0 push @$list, { %opts };
276 }
277 }
278 }else{
279 0 ▶ unless( @$list && ! ref( $list->[-1] ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (@$list and not ref $list->[-1]) 0 0 0
Condition0%0/3
@$list and not ref $list->[-1]
!ll&&!rl&&r
000
MC/DC0%0/2
@$list and not ref $list->[-1]
@$list not ref $list->[-1]
Truth table0%0/3
A and not(B)
A= @$listB= not ref $list->[-1]
ABresult
0X0
100
111
280 0 push @$list, '';
281 }
282 0 ▶ $list->[-1] .= $SPACE if $opts{-space_before};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"-space_before"} 0 0 0
283 0 $list->[-1] .= $item;
284 0 ▶ $list->[-1] .= $SPACE if $opts{-space_after};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"-space_after"} 0 0 0
285 }
286 }
287 }
288  
289 0 ▶ while( @$list && ref( $list->[-1] ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (@$list and ref $list->[-1]) 0 0 0
Condition0%0/3
@$list and ref $list->[-1]
!ll&&!rl&&r
000
MC/DC0%0/2
@$list and ref $list->[-1]
@$list ref $list->[-1]
Truth table0%0/3
A and B
A= @$listB= ref $list->[-1]
ABresult
0X0
100
111
290 0 pop @$list;
291 }
292  
293 0 my $flattened = [];
294 0 ▶ while( @$list ){
Branch: 1 branch
BranchTrueFalseTotal
if (@$list) 0 0 0
295 0 my %opts = %{ shift @$list };
296 0 ▶ delete $opts{-space_before} if exists $opts{-space_before};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"-space_before"} 0 0 0
297 0 ▶ delete $opts{-space_after} if exists $opts{-space_after};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"-space_after"} 0 0 0
298 0 push @$flattened, [ { %opts }, shift @$list ];
299 }
300  
301 0 return $flattened;
302 }
303  
304 # --------------------------------------
305 # Name: as_text
306 # Usage: $text = as_text( @mut );
307 # Purpose: Convert nested attributed text to regular text.
308 # Parameters: @mut -- List of L<Mark-up Text> items.
309 # Returns: $text -- Its text.
310 #
311 0 ▶sub as_text {
Subroutine as_text: not called CC=3 SCAR=24.8
Pod documented
312 0 my $mut = flatten( @_ );
313 0 my $text = '';
314  
315 0 for my $item ( @$mut ){
316 0 ▶ $text .= $item unless ref( $item );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $item 0 0 0
317 }
318  
319 0 return $text;
320 }
321  
322 # --------------------------------------
323 # Name: format_paragraph
324 # Usage: ( \@lines, \@mut ) = format_paragraph( \%paragraph_options, @mut );
325 #
326 # Purpose: Format the L<Mark-up Text> to fit into a paragraph.
327 # Items may be text, sub-lists, or mark-up options.
328 # Text is broken into words via whitespace, C<m{ \s+ }msx>.
329 # Use the UTF character C<\x{a0}> for non-breaking spaces.
330 # Leading whitespace in the first text item is ignored.
331 #
332 # Text will formatted with a single space character between each word
333 # unless the C<-two_spaces> option is used.
334 #
335 # See POD for more details.
336 #
337 #
338 # Parameters: \%paragraph_options -- Mark-up options for the paragraph.
339 # See Mark-up Text in the POD for details.
340 # @mut -- Mark-up Text to format.
341 # Returns: \@lines -- A list of formatted lines.
342 # \@mut -- Leftover mut that did not fit into the paragraph.
343 # These may be used, as is, in another L<format_paragraph()> call.
344 #
345 0 ▶sub format_paragraph {
Subroutine format_paragraph: not called CC=30 SCAR=68.4
Pod documented
346  
347 # flatten makes things easier
348 0 my $mut = flatten( @_ );
349  
350 # remove leading spaces in first text item
351 0 ▶ $mut->[1] =~ s{ \A \s+ }{}msx if @$mut > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$mut > 1 0 0 0
352  
353 # save the indent
354 0 my $indent = 0;
355 0 ▶ if( @$mut && exists( $mut->[0][0]{-indent} ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (@$mut and exists $mut->[0][0]{"-indent"}) 0 0 0
Condition0%0/3
@$mut and exists $mut->[0][0]{"-indent"}
!ll&&!rl&&r
000
MC/DC0%0/2
@$mut and exists $mut->[0][0]{"-indent"}
@$mut exists $mut->[0][0]{"-indent"}
Truth table0%0/3
A and B
A= @$mutB= exists $mut->[0][0]{"-indent"}
ABresult
0X0
100
111
356 0 ▶ $indent = $mut->[0][0]{-indent} || 0;
Condition0%0/2
$mut->[0][0]{'-indent'} || 0
l!l
00
MC/DC0%0/1
$mut->[0][0]{'-indent'} || 0
$mut->[0][0]{'-indent'}
Truth table0%0/2
A
A= $mut->[0][0]{'-indent'}
Aresult
11
00
357 }
358  
359 0 my $lines = [];
360 0 my $space_pending = 0;
361 0 my $space_width = 0;
362 0 my $trailing_width = 0;
363 0 my $trailing_spaces = '';
364 0 my $two_spaces = 0;
365  
366 0 my $add_p_end = 1;
367  
368 MUT_LOOP:
369 0 ▶ while( @$mut ){
Branch: 1 branch
BranchTrueFalseTotal
if (@$mut) 0 0 0
370 0 my %opts = %{ $mut->[0][0] };
371 0 my $text = $mut->[0][1];
372 0 shift @$mut;
373  
374 # add a new segment
375 0 ▶ if( @$lines ){
Branch: 1 branch
BranchTrueFalseTotal
if (@$lines) { } 0 0 0
376 0 push @{ $lines->[-1]{-segments} }, {
377 %opts,
378 -offset => $lines->[-1]{-segments}[-1]{-offset} + $lines->[-1]{-segments}[-1]{-length},
379 0 -length => 0,
380 -text => '',
381 };
382 }else{
383 0 push @{ $lines->[0]{-segments} }, {
384 %opts,
385 -offset => $indent,
386 -length => 0,
387 -text => '',
388 };
389 }
390  
391 0 ▶ if( $space_pending ){
Branch: 1 branch
BranchTrueFalseTotal
if ($space_pending) 0 0 0
392 0 $trailing_spaces = $SPACE;
393 0 $trailing_width = $space_width;
394 0 ▶ if( $two_spaces ){
Branch: 1 branch
BranchTrueFalseTotal
if ($two_spaces) 0 0 0
395 0 $trailing_spaces .= $SPACE;
396 0 $trailing_width .= $space_width;
397 }
398 }
399  
400 0 $space_width = &{$opts{-compute_length}}( { %opts, -print=>0, }, $SPACE ); # must redo every time since might have changed from previous
401  
402 # process the text
403 0 my @text = split m{ ( \s+ ) }msx, $text;
404 0 ▶ while( @text ){
Branch: 1 branch
BranchTrueFalseTotal
if (@text) 0 0 0
405 0 my $word = shift @text;
406  
407 0 ▶ if( $word =~ m{ \A \s }msx ){
Branch: 1 branch
BranchTrueFalseTotal
if ($word =~ / \A \s /msx) 0 0 0
408 0 $space_pending = 1;
409 0 next;
410 }
411  
412 0 my $word_length = &{$opts{-compute_length}}( { %opts, -print=>0, }, $word );
413 0 my $extended_length = $word_length;
414 # a word at end of text may be joined to next
415 0 ▶ unless( @text ){
Branch: 1 branch
BranchTrueFalseTotal
unless (@text) 0 0 0
416 0 for my $item ( @$mut ){
417 0 ▶ last if $item->[1] =~ m{ \A \s }msx;
Branch: 1 branch
BranchTrueFalseTotal
if $item->[1] =~ / \A \s /msx 0 0 0
418 0 ▶ if( $item->[1] =~ m{ \A (\S+) (?=\s) }msx ){
Branch: 1 branch
BranchTrueFalseTotal
if ($item->[1] =~ / \A (\S+) (?=\s) /msx) 0 0 0
419 0 my $look_ahead = $1;
420 0 $extended_length += &{$opts{-compute_length}}( { %opts, -print=>0, }, $look_ahead );
421 0 last;
422 }
423 0 $extended_length += &{$opts{-compute_length}}( { %opts, -print=>0, }, $item->[1] );
424 }
425 }
426 0 my $right = $lines->[-1]{-segments}[-1]{-offset} + $lines->[-1]{-segments}[-1]{-length} + $extended_length;
427 0 my $new_line = 0;
428  
429 0 ▶ if( $trailing_width ){
Branch: 2 branches
BranchTrueFalseTotal
if ($trailing_width) { } 0 0 0
elsif ($space_pending) { } 0 0 0
430 0 ▶ if( $trailing_width + $right > $lines->[-1]{-segments}[-1]{-width} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($trailing_width + $right > $lines->[-1]{"-segments"}[-1]{"-width"}) 0 0 0
431 0 $new_line = 1;
432 }
433 }elsif( $space_pending ){
434 0 my $spw = $space_width;
435 0 ▶ $spw += $space_width if $two_spaces;
Branch: 1 branch
BranchTrueFalseTotal
if $two_spaces 0 0 0
436 0 ▶ if( $spw + $right > $lines->[-1]{-segments}[-1]{-width} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($spw + $right > $lines->[-1]{"-segments"}[-1]{"-width"}) 0 0 0
437 0 $new_line = 1;
438 }
439 }
440  
441 # add a new line
442 0 ▶ if( $new_line ){
Branch: 1 branch
BranchTrueFalseTotal
if ($new_line) 0 0 0
443 0 $lines->[-1]{-width} = $lines->[-1]{-segments}[-1]{-width};
444 0 ▶ if( exists( $opts{-max_lines} ) && @$lines >= $opts{-max_lines} ){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"-max_lines"} and @$lines >= $opts{"-max_lines"}) 0 0 0
Condition0%0/3
exists $opts{"-max_lines"} and @$lines >= $opts{"-max_lines"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opts{"-max_lines"} and @$lines >= $opts{"-max_lines"}
exists $opts{"-max_lines"} @$lines >= $opts{"-max_lines"}
Truth table0%0/3
A and B
A= exists $opts{"-max_lines"}B= @$lines >= $opts{"-max_lines"}
ABresult
0X0
100
111
445 0 delete $opts{-offset};
446 0 delete $opts{-length};
447 0 delete $opts{-text};
448 0 $opts{-indent} = 0; # everything left over is still part of this paragraph, so it's indent must be zero.
449 0 unshift @$mut, [ { %opts }, join( '', $word, @text ) ];
450 0 $add_p_end = 0;
451 0 last MUT_LOOP;
452 }
453 0 push @$lines, {
454 -length => 0,
455 -segments =>[{
456 %opts,
457 -offset => 0,
458 -length => 0,
459 -text => '',
460 }],
461 };
462 0 $space_pending = 0;
463 0 $trailing_width = 0;
464 }
465  
466 0 ▶ if( $trailing_width ){
Branch: 2 branches
BranchTrueFalseTotal
if ($trailing_width) { } 0 0 0
elsif ($space_pending) { } 0 0 0
467 0 $lines->[-1]{-length} += $trailing_width;
468 0 $lines->[-1]{-segments}[-1]{-offset} += $trailing_width;
469 0 $lines->[-1]{-segments}[-2]{-length} += $trailing_width;
470 0 $lines->[-1]{-segments}[-2]{-text} .= $trailing_spaces;
471 0 $trailing_width = 0;
472 }elsif( $space_pending ){
473 0 $lines->[-1]{-length} += $space_width;
474 0 $lines->[-1]{-segments}[-1]{-length} += $space_width;
475 0 $lines->[-1]{-segments}[-1]{-text} .= $SPACE;
476 0 ▶ if( $two_spaces ){
Branch: 1 branch
BranchTrueFalseTotal
if ($two_spaces) 0 0 0
477 0 $lines->[-1]{-length} += $space_width;
478 0 $lines->[-1]{-segments}[-1]{-length} += $space_width;
479 0 $lines->[-1]{-segments}[-1]{-text} .= $SPACE;
480 }
481 }
482  
483 0 $lines->[-1]{-length} += $word_length;
484 0 $lines->[-1]{-segments}[-1]{-length} += $word_length;
485 0 $lines->[-1]{-segments}[-1]{-text} .= $word;
486  
487 0 $space_pending = 0;
488 0 $trailing_width = 0;
489  
490 # check for two_spaces after word
491 0 $two_spaces = 0;
492 0 ▶ if( $opts{-two_spaces} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"-two_spaces"}) 0 0 0
493 0 ▶ $two_spaces = $word =~ $opts{-two_spaces} || 0;
Condition0%0/2
$word =~ /$opts{'-two_spaces'}/ || 0
l!l
00
MC/DC0%0/1
$word =~ /$opts{'-two_spaces'}/ || 0
$word =~ /$opts{'-two_spaces'}/
Truth table0%0/2
A
A= $word =~ /$opts{'-two_spaces'}/
Aresult
11
00
494 }
495 }
496 }
497 0 $lines->[-1]{-width} = $lines->[-1]{-segments}[-1]{-width};
498 0 ▶ if( @$lines && $add_p_end ){
Branch: 1 branch
BranchTrueFalseTotal
if (@$lines and $add_p_end) 0 0 0
Condition0%0/3
@$lines and $add_p_end
!ll&&!rl&&r
000
MC/DC0%0/2
@$lines and $add_p_end
@$lines $add_p_end
Truth table0%0/3
A and B
A= @$linesB= $add_p_end
ABresult
0X0
100
111
499 0 $lines->[-1]{-last_line} = 1;
500 }
501  
502 0 return ( $lines, $mut );
503 }
504  
505 # --------------------------------------
506 # Name: align_lines
507 # Usage: align_lines( $alignment, $lines );
508 # Purpose: Change the offsets in the lines outputted by L<format_paragraph()> to align the paragraph.
509 # Parameters: $alignment -- A value of 0.0 will left align;
510 # a value of 0.5 will center align;
511 # a value of 1.0 will right align.
512 # Other values will create weird, special effects.
513 # $lines -- Output from L<format_paragraph()>.
514 # Returns: none
515 #
516 0 ▶sub align_lines {
Subroutine align_lines: not called CC=5 SCAR=34.0
Pod documented
517 0 my $alignment = shift @_;
518 0 my $lines = shift @_;
519  
520 # first line may have indent, so do it separate
521 0 ▶ my $indent = $lines->[0]{-segments}[0]{-indent} || 0;
Condition0%0/2
$lines->[0]{'-segments'}[0]{'-indent'} || 0
l!l
00
MC/DC0%0/1
$lines->[0]{'-segments'}[0]{'-indent'} || 0
$lines->[0]{'-segments'}[0]{'-indent'}
Truth table0%0/2
A
A= $lines->[0]{'-segments'}[0]{'-indent'}
Aresult
11
00
522 0 my $gap = $lines->[0]{-width} - $lines->[0]{-length};
523 0 my $offset = $gap * $alignment;
524 0 for my $segment ( @{ $lines->[0]{-segments} } ){
525 0 $segment->{-offset} = $offset;
526 0 $offset += $segment->{-length};
527 }
528  
529 # do the rest
530 0 for my $line ( @{ $lines }[ 1 .. $#$lines ] ){
531 0 $gap = $line->{-width} - $line->{-length};
532 0 $offset = $gap * $alignment;
533 0 for my $segment ( @{ $line->{-segments} } ){
534 0 $segment->{-offset} = $offset;
535 0 $offset += $segment->{-length};
536 }
537 }
538  
539 0 return;
540 }
541  
542 # --------------------------------------
543 # Name: _justify_line
544 # Usage: _justify_line( $word_spacing_weight, $character_spacing_weight, $horizontal_scaling_weight, $line; $indent );
545 # Purpose: Calculate the amount to adjust the spacing and scaling to justify the line.
546 # Parameters: $word_spacing_weight -- How much attributed to spaces between words.
547 # $character_spacing_weight -- How much attributed to spaces between characters.
548 # $horizontal_scaling_weight -- How much attributed to scaling the glyphs horizontally.
549 # $line -- The line to adjust.
550 # $indent -- Possible indentation of the line.
551 # Returns: none
552 #
553 0 ▶sub _justify_line {
Subroutine _justify_line: not called CC=6 SCAR=37.4
554 0 my $word_spacing_weight = shift @_;
555 0 my $character_spacing_weight = shift @_;
556 0 my $horizontal_scaling_weight = shift @_;
557 0 my $line = shift @_;
558 0 ▶ my $indent = shift @_ || 0;
Condition0%0/2
shift @_ || 0
l!l
00
MC/DC0%0/1
shift @_ || 0
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
559  
560 0 my $gap = $line->{-width} - $line->{-length} - $indent;
561  
562 0 my $char = 0;
563 0 my $sp = 0;
564 0 for my $segment ( @{ $line->{-segments} } ){
565 0 $char += length( $segment->{-text} );
566 0 $sp += $segment->{-text} =~ tr/\x20/\x20/;
567 }
568  
569 # calculate character spacing
570 0 ▶ if( $sp <= 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($sp <= 0) { } 0 0 0
571 # no spaces if narrow width or non-breaking spaces
572 0 $word_spacing_weight = 0;
573 0 $line->{-wordspace} = 0;
574  
575 0 my $sum = $character_spacing_weight + $horizontal_scaling_weight;
576 0 ▶ if( $sum == 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($sum == 0) { } 0 0 0
577 0 $character_spacing_weight = 0.5;
578 0 $horizontal_scaling_weight = 0.5;
579 }else{
580 0 $character_spacing_weight /= $sum;
581 0 $horizontal_scaling_weight /= $sum;
582 }
583 }else{
584 0 $line->{-wordspace} = $gap * $word_spacing_weight / $sp;
585 }
586  
587 # calculate word spacing
588 0 $line->{-charspace} = $gap * $character_spacing_weight / $char;
589  
590 # calculate horizontal scaling
591 0 $line->{-hspace} = ( $line->{-width} - $indent ) / ( $line->{-length} + $gap * ( 1 - $horizontal_scaling_weight ) ) * 100;
592  
593 # calculate justified offsets
594 0 my $joffset = $indent;
595 0 for my $segment ( @{ $line->{-segments} } ){
596 0 $segment->{-joffset} = $joffset;
597 0 $segment->{-wordspace} = $line->{-wordspace};
598 0 $segment->{-charspace} = $line->{-charspace};
599 0 $segment->{-hspace} = $line->{-hspace};
600  
601 0 $char = length( $segment->{-text} );
602 0 $sp = $segment->{-text} =~ tr/\x20/\x20/;
603 $joffset += $line->{-wordspace} * $sp
604 + $line->{-charspace} * $char
605 0 + $line->{-hspace} * $segment->{-length} / 100;
606 }
607  
608 0 return;
609 }
610  
611 # --------------------------------------
612 # Name: justify_lines
613 # Usage: justify_lines( $word_spacing_weight, $character_spacing_weight, $horizontal_scaling_weight, $lines );
614 # Purpose: Modify the output of format_paragraph() so that the lines are fully justified.
615 # See the POD for details.
616 # Parameters: $word_spacing_weight -- The weight of the adjustment for word spacing.
617 # $character_spacing_weight -- The weight of the adjustment for character spacing.
618 # $horizontal_scaling_weight -- The weight of the adjustment for horizontal scaling.
619 # $lines -- The output of L<format_paragraph()>.
620 # Returns: none
621 #
622 0 ▶sub justify_lines {
Subroutine justify_lines: not called CC=3 SCAR=24.8
Pod documented
623 0 my $word_spacing_weight = abs( shift @_ );
624 0 my $character_spacing_weight = abs( shift @_ );
625 0 my $horizontal_scaling_weight = abs( shift @_ );
626 0 my $lines = shift @_;
627  
628 # normalize the weights
629 0 my $sum = $word_spacing_weight + $character_spacing_weight + $horizontal_scaling_weight;
630 0 $word_spacing_weight /= $sum;
631 0 $character_spacing_weight /= $sum;
632 0 $horizontal_scaling_weight /= $sum;
633  
634 # first line may have an indent, so do it separately
635 0 _justify_line( $word_spacing_weight, $character_spacing_weight, $horizontal_scaling_weight, $lines->[0], $lines->[0]{-segments}[0]{-indent} );
636  
637 0 for my $line ( @{ $lines }[ 1 .. $#$lines ] ){
638 # don't do the last line.
639 0 ▶ next if $line->{-last_line};
Branch: 1 branch
BranchTrueFalseTotal
if $line->{"-last_line"} 0 0 0
640  
641 0 _justify_line( $word_spacing_weight, $character_spacing_weight, $horizontal_scaling_weight, $line );
642 }
643  
644 0 return;
645 }
646  
647 # --------------------------------------
648 # Name: print_lines
649 # Usage: ( \@y_values, $lines ) = print_lines( $left, \@y_values, $lines );
650 # Purpose: Print the lines created by L<format_paragraph()>.
651 # Parameters: $left -- Left offset for the lines.
652 # \@y_values -- A list of baselines for the lines.
653 # $lines -- Formatted lines.
654 # Returns: \@y_values -- Left over baselines.
655 # $lines -- Left over lines.
656 #
657 0 ▶sub print_lines {
Subroutine print_lines: not called CC=5 SCAR=34.0
Pod documented
658 0 my $left = shift @_;
659 0 my $y_values = shift @_;
660 0 my $lines = shift @_;
661  
662 0 ▶ while( @$y_values && @$lines ){
Branch: 1 branch
BranchTrueFalseTotal
if (@$y_values and @$lines) 0 0 0
Condition0%0/3
@$y_values and @$lines
!ll&&!rl&&r
000
MC/DC0%0/2
@$y_values and @$lines
@$y_values @$lines
Truth table0%0/3
A and B
A= @$y_valuesB= @$lines
ABresult
0X0
100
111
663 0 my $line = shift @$lines;
664 0 my $y = shift @$y_values;
665 0 for my $segment ( @{ $line->{-segments} } ){
666 0 my %opts = %$segment;
667 0 $opts{-print} = 1;
668  
669 0 my $x = $segment->{-offset};
670 0 ▶ if( exists( $segment->{-joffset} ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $segment->{"-joffset"}) 0 0 0
671 0 $x = $segment->{-joffset};
672 }
673  
674 0 $opts{-x} = $x + $left;
675 0 $opts{-y} = $y;
676 0 &{ $segment->{-print_text} }( \%opts, $segment->{-text} );
677 }
678 }
679  
680 0 return ( $y_values, $lines );
681 }
682  
683 # --------------------------------------
684 # Name: print_paragraph
685 # Usage: ( $bottom, \@mut ) = print_paragraph( \%print_options, \%paragraph_options, @mut );
686 # Purpose: Print the paragraph.
687 # This subroutine uses L<format_paragraph()> to format the paragraph
688 # and then determines where each segment of text should go.
689 # It uses the application specified print routine to print it.
690 #
691 # See the POD for details.
692 #
693 # Parameters: \%print_options -- See the POD for details.
694 # \%paragraph_options -- Mark-up Text for the paragraph.
695 # Same as format_paragraph().
696 # @mut -- A list of mut to print with L<Mark-up Text>.
697 # Returns: $bottom -- The bottom of the paragraph.
698 # Can be used as the top of the next.
699 # \@mut -- A list of mut that did not fit into the paragraph.
700 #
701 0 ▶sub print_paragraph {
Subroutine print_paragraph: not called CC=14 SCAR=53.5
Pod documented
702 0 my %opts = ();
703 0 ▶ while( @_ && ref( $_[0] ) eq 'HASH' ){
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and ref $_[0] eq "HASH") 0 0 0
Condition0%0/3
@_ and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and ref $_[0] eq "HASH"
@_ ref $_[0] eq "HASH"
Truth table0%0/3
A and B
A= @_B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
704 0 my %hash = %{ shift @_ };
705 0 @opts{keys %hash} = values %hash;
706 }
707  
708 0 my $height = $opts{-block}[3] - $opts{-block}[1];
709 0 ▶ my @y_values = baselines( $height, $opts{-size}, ( $opts{-spacing} || 1 ), $opts{-block}[1] );
Condition0%0/2
$opts{'-spacing'} || 1
l!l
00
MC/DC0%0/1
$opts{'-spacing'} || 1
$opts{'-spacing'}
Truth table0%0/2
A
A= $opts{'-spacing'}
Aresult
11
00
710 0 ▶ my $bottom = $y_values[-1] - $opts{-size} * (( $opts{-spacing} || 1 ) - 1 );
Condition0%0/2
$opts{'-spacing'} || 1
l!l
00
MC/DC0%0/1
$opts{'-spacing'} || 1
$opts{'-spacing'}
Truth table0%0/2
A
A= $opts{'-spacing'}
Aresult
11
00
711  
712 0 my $width = $opts{-block}[2] - $opts{-block}[0];
713 0 my ( $lines, $mut ) = format_paragraph( { %opts, -width=>$width, -max_lines=>scalar( @y_values ) }, @_ );
714  
715 0 ▶ if( $opts{-justify_word} || $opts{-justify_char} || $opts{-justify_scale} ){
Branch: 2 branches
BranchTrueFalseTotal
if ($opts{'-justify_word'} or $opts{'-justify_char'} or $opts{'-justify_scale'}) { } 0 0 0
elsif ($opts{'-alignment'}) { } 0 0 0
Condition0%0/6
$opts{"-justify_word"} or $opts{"-justify_char"} or $opts{"-justify_scale"}
l!l&&r!l&&!r
000
$opts{"-justify_word"} or $opts{"-justify_char"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$opts{"-justify_word"} or $opts{"-justify_char"} or $opts{"-justify_scale"}
$opts{"-justify_word"} $opts{"-justify_char"} $opts{"-justify_scale"}
Truth table0%0/4
A or B or C
A= $opts{"-justify_word"}B= $opts{"-justify_char"}C= $opts{"-justify_scale"}
ABCresult
1XX1
01X1
0011
0000
716 0 ▶ justify_lines( $opts{-justify_word} || 0, $opts{-justify_char} || 0, $opts{-justify_scale} || 0, $lines );
Condition0%0/6
$opts{'-justify_word'} || 0
l!l
00
$opts{'-justify_char'} || 0
l!l
00
$opts{'-justify_scale'} || 0
l!l
00
MC/DC0%0/1
$opts{'-justify_word'} || 0
$opts{'-justify_word'}
MC/DC0%0/1
$opts{'-justify_char'} || 0
$opts{'-justify_char'}
MC/DC0%0/1
$opts{'-justify_scale'} || 0
$opts{'-justify_scale'}
Truth table0%0/2
A
A= $opts{'-justify_word'}
Aresult
11
00
Truth table0%0/2
A
A= $opts{'-justify_char'}
Aresult
11
00
Truth table0%0/2
A
A= $opts{'-justify_scale'}
Aresult
11
00
717 }elsif( $opts{-alignment} ){
718 0 align_lines( $opts{-alignment}, $lines );
719 }
720  
721 0 my ( $y_values, undef ) = print_lines( $opts{-block}[0], \@y_values, $lines );
722 0 ▶ if( @$y_values ){
Branch: 1 branch
BranchTrueFalseTotal
if (@$y_values) 0 0 0
723 0 ▶ $bottom = $y_values->[0] - $opts{-size} * (( $opts{-spacing} || 1 ) - 1 );
Condition0%0/2
$opts{'-spacing'} || 1
l!l
00
MC/DC0%0/1
$opts{'-spacing'} || 1
$opts{'-spacing'}
Truth table0%0/2
A
A= $opts{'-spacing'}
Aresult
11
00
724 }
725  
726 0 return $bottom, $mut;
727 }
728  
729 2 1;
730 __DATA__
↑ Summary script/pdf_kit_eg.pl »