TextBlock.pm

filter: Statementstmt 88.2% 172 / 195 Branchbran 77.0% 77 / 100 Conditioncond 81.2% 26 / 32 MC/DCmcdc 55.0% 11 / 20 Subroutinesub 92.8% 13 / 14 Podpod 100% 2 / 2 totaltotal 82.9% 301 / 363 SCAR 44.6
CC
68
Cov
84%
apply
83.4
_apply_defaults
5.0
garbledy_gook
4.0
CRAP 86.6
↑ Summary TextBlock/Font.pm »
1 package PDF::TextBlock;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use File::Temp qw(mktemp);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Class::Accessor::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use PDF::TextBlock::Font;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 7 ▶use base qw( Class::Accessor::Fast );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 __PACKAGE__->mk_accessors(qw( pdf page text fonts x y w h lead parspace align hang flindent fpindent indent ));
12  
13 7 ▶use constant mm => 25.4 / 72;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use constant in => 1 / 72;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use constant pt => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 7 my $debug = 0;
18  
19 =head1 NAME
20  
21 PDF::TextBlock - Easier creation of text blocks when using PDF::API2
22  
23 =cut
24  
25 7 our $VERSION = '0.11';
26  
27 =head1 SYNOPSIS
28  
29 use PDF::API2;
30 use PDF::TextBlock;
31  
32 my $pdf = PDF::API2->new( -file => "40-demo.pdf" );
33 my $tb = PDF::TextBlock->new({
34 pdf => $pdf,
35 fonts => {
36 b => PDF::TextBlock::Font->new({
37 pdf => $pdf,
38 font => $pdf->corefont( 'Helvetica-Bold', -encoding => 'latin1' ),
39 }),
40 },
41 });
42 $tb->text(
43 $tb->garbledy_gook .
44 ' <b>This fairly lengthy</b>, rather verbose sentence <b>is tagged</b> to appear ' .
45 'in a <b>different font, specifically the one we tagged b for "bold".</b> ' .
46 $tb->garbledy_gook .
47 ' <href="http://www.omnihotels.com">Click here to visit Omni Hotels.</href> ' .
48 $tb->garbledy_gook . "\n\n" .
49 "New paragraph.\n\n" .
50 "Another paragraph."
51 );
52 $tb->apply;
53 $pdf->save;
54 $pdf->end;
55  
56 =head1 DESCRIPTION
57  
58 Neither Rick Measham's excellent L<PDF::API2> tutorial nor L<PDF::FromHTML> are able to cope with
59 wanting some words inside a text block to be bold. This module makes that task trivial.
60  
61 Simply define whatever tags you want PDF::TextBlock to honor inside the fonts hashref, and
62 then you are free to use HTML-like markup in the text attribute and we'll render those fonts
63 for you.
64  
65 We also honor the HTML-like tag <href>. This means that we add annotation to the PDF for you
66 which makes the word(s) you wrap in <href> clickable, and we underline those words.
67  
68 Note this markup syntax is very rudimentary. We do not support HTML.
69 Tags cannot overlap each other. There is no way to escape tags inside text().
70  
71 The tests in t/ generate .pdf files. You might find those examples helpful.
72 Watch out for 20-demo.pdf. It spits. :)
73  
74 =head1 METHODS
75  
76 =head2 new
77  
78 Our attributes are listed below. They can be set when you call new(),
79 and/or added/changed individually at any time before you call apply().
80  
81 =over
82  
83 =item pdf
84  
85 A L<PDF::API2> object. You must provide this.
86  
87 =item text
88  
89 The text of your TextBlock. Defaults to garbledy_gook().
90  
91 =item x
92  
93 X position from the left of the document. Default is 20/mm.
94  
95 =item y
96  
97 Y position from the bottom of the document. Default is 238/mm.
98  
99 =item w
100  
101 Width of this text block. Default is 175/mm.
102  
103 =item h
104  
105 Height of this text block. Default is 220/mm.
106  
107 =item align
108  
109 Alignment of words in the text block. Default is 'justify'. Legal values:
110  
111 =over
112  
113 =item justify
114  
115 Spreads words out evenly in the text block so that each line ends in the same spot
116 on the right side of the text block. The last line in a paragraph (too short to fill
117 the entire line) will be set to 'left'.
118  
119 =item fulljustify
120  
121 Like justify, except that the last line is also spread across the page. The last
122 line can look very odd with very large gaps.
123  
124 =item left
125  
126 Aligns each line to the left.
127  
128 =item right
129  
130 Aligns each line to the right.
131  
132 =back
133  
134 =item page
135  
136 A L<PDF::API2::Page> object. If you don't set this manually then we create
137 a new page for you when you call apply().
138  
139 If you want multiple PDF::TextBlock objects to all render onto the same
140 page, you could create a PDF::API2 page yourself, and pass it in to each
141 PDF::TextBlock object:
142  
143 my $pdf = PDF::API2->new( -file => "mytest.pdf" );
144 my $page = $pdf->page();
145  
146 my $tb = PDF::TextBlock->new({
147 pdf => $pdf,
148 page => $page, # <---
149 ...
150  
151 Or after your first apply() you could grab $page off of $tb.
152  
153 my $pdf = PDF::API2->new( -file => "mytest.pdf" );
154 my $tb = PDF::TextBlock->new({
155 pdf => $pdf,
156 ...
157 });
158 $tb->apply;
159 my $page = $tb->page; # Use the same page
160  
161 my $tb2 = PDF::TextBlock->new({
162 pdf => $pdf,
163 page => $page, # <---
164 ...
165  
166 =item fonts
167  
168 A hashref of HTML-like markup tags and what font objects you want us to use
169 when we see that tag in text().
170  
171 my $tb = PDF::TextBlock->new({
172 pdf => $pdf,
173 fonts => {
174 # font is a PDF::API2::Resource::Font::CoreFont
175 b => PDF::TextBlock::Font->new({
176 pdf => $pdf,
177 font => $pdf->corefont( 'Helvetica-Bold', -encoding => 'latin1' ),
178 fillcolor => '#ff0000', # red
179 }),
180 },
181 });
182  
183 =back
184  
185 The attributes below came from Rick's text_block(). They do things,
186 but I don't really understand them. POD patches welcome. :)
187  
188 L<http://rick.measham.id.au/pdf-api2/>
189  
190 =over
191  
192 =item lead
193  
194 Default is 15/pt.
195  
196 =item parspace
197  
198 Default is 0/pt.
199  
200 =item hang
201  
202 =item flindent
203  
204 =item fpindent
205  
206 =item indent
207  
208 =back
209  
210 =head2 apply
211  
212 This is where we do all the L<PDF::API2> heavy lifting for you.
213  
214 Returns $endw, $ypos, $overflow.
215  
216 I'm not sure what $endw is good for, it's straight from Ricks' code. :)
217  
218 $ypos is useful when you have multiple TextBlock objects and you want to start
219 the next one wherever the previous one left off.
220  
221 my ($endw, $ypos) = $tb->apply();
222 $tb->y($ypos);
223 $tb->text("a bunch more text");
224 $tb->apply();
225  
226 $overflow is whatever text() didn't fit inside your TextBlock.
227 (Too much text? Your font was too big? You set w and h too small?)
228  
229 The original version of this method was text_block(), which is (c) Rick Measham, 2004-2007.
230 The latest version of text_block() can be found in the tutorial located at L<http://rick.measham.id.au/pdf-api2/>.
231 text_block() is released under the LGPL v2.1.
232  
233 =cut
234  
235 sub apply {
236 27 ▶ my ($self, %args) = @_;
Subroutine apply: called CC=61 SCAR=44.2
Pod documented
237  
238 27 my $pdf = $self->pdf;
239 27 ▶ unless (ref $pdf eq "PDF::API2") {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $pdf eq "PDF::API2") 0 27 27
240 0 croak "pdf attribute (a PDF::API2 object) required";
241 }
242  
243 27 $self->_apply_defaults();
244  
245 27 my $text = $self->text;
246 27 my $page = $self->page;
247  
248 # Build %content_texts. A hash of all PDF::API2::Content::Text objects,
249 # one for each tag (<b> or <i> or whatever) in $text.
250 27 my %content_texts;
251 27 foreach my $tag (($text =~ /<(\w*)[^\/].*?>/g), "default") {
252 106 ▶ next if ($content_texts{$tag});
Branch: 1 branch
BranchTrueFalseTotal
if $content_texts{$tag} 72 34 106
253 34 my $content_text = $page->text; # PDF::API2::Content::Text obj
254 34 my $font;
255 34 ▶ if ($self->fonts && $self->fonts->{$tag}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->fonts and $self->fonts->{$tag}) { } 29 5 34
elsif ($self->fonts and $self->fonts->{'default'}) { } 0 5 5
Condition66%2/3
$self->fonts and $self->fonts->{$tag}
!ll&&!rl&&r
0529
MC/DC50%1/2
$self->fonts and $self->fonts->{$tag}
$self->fonts $self->fonts->{$tag}
Truth table66%2/3
A and B
A= $self->fontsB= $self->fonts->{$tag}
ABresult
0X0
100
111
256 29 ▶ $debug && warn "using the specific font you set for <$tag>";
Branch: 1 branch
BranchTrueFalseTotal
$debug and warn "using the specific font you set for <$tag>" 0 29 29
257 29 $font = $self->fonts->{$tag};
258 ▶ } elsif ($self->fonts && $self->fonts->{default}) {
Condition33%1/3
$self->fonts and $self->fonts->{"default"}
!ll&&!rl&&r
050
MC/DC0%0/2
$self->fonts and $self->fonts->{"default"}
$self->fonts $self->fonts->{"default"}
Truth table33%1/3
A and B
A= $self->fontsB= $self->fonts->{"default"}
ABresult
0X0
100
111
259 0 ▶ $debug && warn "using the default font you set for <$tag>";
Branch: 1 branch
BranchTrueFalseTotal
$debug and warn "using the default font you set for <$tag>" 0 0 0
260 0 $font = $self->fonts->{default};
261 } else {
262 5 ▶ $debug && warn "using PDF::TextBlock::Font default font for <$tag> since you specified neither <$tag> nor a 'default'";
Branch: 1 branch
BranchTrueFalseTotal
$debug and warn "using PDF::TextBlock::Font default font for <$tag> since you specified neither <$tag> nor a 'default'" 0 5 5
263 5 $font = PDF::TextBlock::Font->new({ pdf => $pdf });
264 5 $self->fonts->{$tag} = $font;
265 }
266 34 $font->apply_defaults;
267 34 $content_text->font($font->font, $font->size);
268 34 $content_text->fillcolor($font->fillcolor);
269 34 $content_text->translate($self->x, $self->y);
270 34 $content_texts{$tag} = $content_text;
271 }
272  
273 27 my $content_text = $content_texts{default};
274  
275 27 ▶ if ($self->align eq "text_right") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->align eq "text_right") 1 26 27
276 # Special case... Single line of text that we don't paragraph out...
277 # ... why does this exist? TODO: why can't align 'right' do this?
278 # t/20-demo.t doesn't work align 'right', but I don't know why.
279 1 $content_text->text_right($text);
280 1 return 1;
281 }
282  
283 26 my ($endw, $ypos);
284  
285 # Get the text in paragraphs
286 26 my @paragraphs = split( /\n/, $text );
287  
288 # calculate width of all words
289 26 my $space_width = $content_text->advancewidth(' ');
290  
291 26 my @words = split( /\s+/, $text );
292  
293 # Build a hash of widths we refer back to later.
294 26 my $current_content_text = $content_texts{default};
295 26 my $tag;
296 26 my %width = ();
297 26 foreach my $word (@words) {
298 1268 ▶ next if exists $width{$word};
Branch: 1 branch
BranchTrueFalseTotal
if exists $width{$word} 2 1266 1268
299 1266 ▶ if (($tag) = ($word =~ /<(.*?)>/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (($tag) = $word =~ /<(.*?)>/) 83 1183 1266
300 83 ▶ if ($tag !~ /\//) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tag =~ m[/]) 78 5 83
301 78 ▶ unless ($content_texts{$tag}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($content_texts{$tag}) 0 78 78
302 # Huh. They didn't declare this one, so we'll put default in here for them.
303 0 $content_texts{$tag} = $content_texts{default};
304 }
305 78 $current_content_text = $content_texts{$tag};
306 }
307 }
308
309 1266 my $stripped = $word;
310 1266 $stripped =~ s/<.*?>//g;
311 1266 $width{$word} = $current_content_text->advancewidth($stripped);
312  
313 1266 ▶ if ($tag && $tag =~ /^\//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag and $tag =~ m[^/]) 4 1262 1266
Condition100%3/3
$tag and $tag =~ m[^/]
!ll&&!rl&&r
1183794
MC/DC100%2/2
$tag and $tag =~ m[^/]
$tag $tag =~ m[^/]
Truth table100%3/3
A and B
A= $tagB= $tag =~ m[^/]
ABresult
0X0
100
111
314 4 $current_content_text = $content_texts{default};
315 }
316 }
317  
318 26 $ypos = $self->y;
319 26 my @paragraph = split( / /, shift(@paragraphs) );
320  
321 26 my $first_line = 1;
322 26 my $first_paragraph = 1;
323  
324 26 my ($href);
325 26 $current_content_text = $content_texts{default};
326  
327 # while we can add another line
328 26 ▶ while ( $ypos >= $self->y - $self->h + $self->lead ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ypos >= $self->y - $self->h + $self->lead) 183 2 185
329  
330 183 ▶ unless (@paragraph) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@paragraph) 29 154 183
331 29 ▶ last unless scalar @paragraphs;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @paragraphs 24 5 29
332  
333 5 @paragraph = split( / /, shift(@paragraphs) );
334  
335 5 ▶ $ypos -= $self->parspace if $self->parspace;
Branch: 1 branch
BranchTrueFalseTotal
if $self->parspace 0 5 5
336 5 ▶ last unless $ypos >= $self->y - $self->h;
Branch: 1 branch
BranchTrueFalseTotal
unless $ypos >= $self->y - $self->h 0 5 5
337  
338 5 $first_line = 1;
339 5 $first_paragraph = 0;
340 }
341  
342 159 my $xpos = $self->x;
343  
344 # while there's room on the line, add another word
345 159 my @line = ();
346  
347 159 my $line_width = 0;
348 159 ▶ if ( $first_line && defined $self->hang ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($first_line and defined $self->hang) { } 0 159 159
elsif ($first_line and defined $self->flindent) { } 0 159 159
elsif ($first_paragraph and defined $self->fpindent) { } 0 159 159
elsif (defined $self->indent) { } 0 159 159
Condition66%2/3
$first_line and defined $self->hang
!ll&&!rl&&r
128310
MC/DC0%0/2
$first_line and defined $self->hang
$first_line defined $self->hang
Truth table66%2/3
A and B
A= $first_lineB= defined $self->hang
ABresult
0X0
100
111
349 0 my $hang_width = $content_text->advancewidth( $self->hang );
350  
351 0 $content_text->translate( $xpos, $ypos );
352 0 $content_text->text( $self->hang );
353  
354 0 $xpos += $hang_width;
355 0 $line_width += $hang_width;
356 0 ▶ $self->indent($self->indent + $hang_width) if $first_paragraph;
Branch: 1 branch
BranchTrueFalseTotal
if $first_paragraph 0 0 0
357 ▶ } elsif ( $first_line && defined $self->flindent ) {
Condition66%2/3
$first_line and defined $self->flindent
!ll&&!rl&&r
128310
MC/DC0%0/2
$first_line and defined $self->flindent
$first_line defined $self->flindent
Truth table66%2/3
A and B
A= $first_lineB= defined $self->flindent
ABresult
0X0
100
111
358 0 $xpos += $self->flindent;
359 0 $line_width += $self->flindent;
360 ▶ } elsif ( $first_paragraph && defined $self->fpindent ) {
Condition66%2/3
$first_paragraph and defined $self->fpindent
!ll&&!rl&&r
51540
MC/DC0%0/2
$first_paragraph and defined $self->fpindent
$first_paragraph defined $self->fpindent
Truth table66%2/3
A and B
A= $first_paragraphB= defined $self->fpindent
ABresult
0X0
100
111
361 0 $xpos += $self->fpindent;
362 0 $line_width += $self->fpindent;
363 } elsif ( defined $self->indent ) {
364 0 $xpos += $self->indent;
365 0 $line_width += $self->indent;
366 }
367  
368 159 @paragraph = grep { length($_) } @paragraph;
369 159 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if (@paragraph and $line_width + scalar(@line) * $space_width + $width{$paragraph[0]} < $self->w) 1242 159 1401
Condition100%3/3
@paragraph and $line_width + scalar(@line) * $space_width + $width{$paragraph[0]} < $self->w
!ll&&!rl&&r
291301242
MC/DC100%2/2
@paragraph and $line_width + scalar(@line) * $space_width + $width{$paragraph[0]} < $self->w
@paragraph $line_width + scalar(@line) * $space_width + $width{$paragraph[0]} < $self->w
Truth table100%3/3
A and B
A= @paragraphB= $line_width + scalar(@line) * $space_width + $width{$paragraph[0]} < $self->w
ABresult
0X0
100
111
370 @paragraph &&
371 $line_width +
372 ( scalar(@line) * $space_width ) +
373 $width{ $paragraph[0] }
374 < $self->w
375 ) {
376 1242 $line_width += $width{ $paragraph[0] };
377 1242 push( @line, shift(@paragraph) );
378 }
379  
380 # calculate the space width
381 159 my ( $wordspace, $align );
382 159 ▶ if ( $self->align eq 'fulljustify'
Branch: 1 branch
BranchTrueFalseTotal
if ($self->align eq 'fulljustify' or $self->align eq 'justify' and @paragraph) { } 121 38 159
Condition100%6/6
$self->align eq "fulljustify" or $self->align eq "justify" and @paragraph
l!l&&r!l&&!r
711438
$self->align eq "justify" and @paragraph
!ll&&!rl&&r
2414114
MC/DC100%3/3
$self->align eq "fulljustify" or $self->align eq "justify" and @paragraph
$self->align eq "fulljustify" $self->align eq "justify" @paragraph
Truth table100%4/4
A or B and C
A= $self->align eq "fulljustify"B= $self->align eq "justify"C= @paragraph
ABCresult
1XX1
0111
00X0
0100
383 or ( $self->align eq 'justify' and @paragraph )
384 ) {
385 121 ▶ if ( scalar(@line) == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @line == 1) 0 121 121
386 0 @line = split( //, $line[0] );
387 }
388 121 $wordspace = ( $self->w - $line_width ) / ( scalar(@line) - 1 );
389 121 $align = 'justify';
390 } else {
391 # We've run out of words to fill a full line
392 38 ▶ $align = ( $self->align eq 'justify' ) ? 'left' : $self->align;
Branch: 1 branch
BranchTrueFalseTotal
$self->align eq 'justify' ? : 14 24 38
393 38 $wordspace = $space_width;
394 }
395 159 $line_width += $wordspace * ( scalar(@line) - 1 );
396  
397 # If we want to justify this line, or if there are any markup tags
398 # in here we'll have to split the line up word for word.
399 159 ▶ if ( $align eq 'justify' or (grep /<.*>/, @line) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($align eq 'justify' or grep /<.*>/, @line) { } 131 28 159
Condition100%3/3
$align eq "justify" or grep /<.*>/, @line
l!l&&r!l&&!r
1211028
MC/DC100%2/2
$align eq "justify" or grep /<.*>/, @line
$align eq "justify" grep /<.*>/, @line
Truth table100%3/3
A or B
A= $align eq "justify"B= grep /<.*>/, @line
ABresult
1X1
011
000
400 # TODO: #4 This loop is DOA for align 'right' and 'center' with any tags.
401 # FMCC Fix proposal
402 131 ▶ if ( $align eq 'center' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($align eq 'center') { } 3 128 131
elsif ($align eq 'right') { } 3 125 128
403 # Fix $xpos
404 3 $xpos += ( $self->w / 2 ) - ( $line_width / 2 );
405 } elsif ( $align eq 'right' ) {
406 # Fix $xpos
407 3 $xpos += $self->w - $line_width;;
408 }
409 # END FMCC Fix Proposal
410 131 foreach my $word (@line) {
411 1089 ▶ if (($tag) = ($word =~ /<(.*?)>/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (($tag) = $word =~ /<(.*?)>/) 83 1006 1089
412 # warn "tag is $tag";
413 83 ▶ if ($tag =~ /^href[a-z]?/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tag =~ /^href[a-z]?/) { } 1 82 83
elsif (not $tag =~ m[/]) { } 78 4 82
414 1 ($tag, $href) = ($tag =~ /(href[a-z]?)="(.*?)"/);
415 1 ▶ $current_content_text = $content_texts{$tag} if ref $content_texts{$tag};
Branch: 1 branch
BranchTrueFalseTotal
if ref $content_texts{$tag} 1 0 1
416 } elsif ($tag !~ /\//) {
417 78 $current_content_text = $content_texts{$tag};
418 }
419 }
420
421 1089 my $stripped = $word;
422 1089 $stripped =~ s/<.*?>//g;
423 1089 ▶ $debug && _debug("$tag 1", $xpos, $ypos, $stripped);
Branch: 1 branch
BranchTrueFalseTotal
$debug and _debug("$tag 1", $xpos, $ypos, $stripped) 0 1089 1089
424 1089 $current_content_text->translate( $xpos, $ypos );
425  
426 1089 ▶ if ($href) {
Branch: 1 branch
BranchTrueFalseTotal
if ($href) { } 6 1083 1089
427 6 $current_content_text->text($stripped); # -underline => [2,.5]);
428  
429 # It would be nice if we could use -underline above, but it leaves gaps
430 # between each word, which we don't like. So we'll have to draw our own line
431 # that knows how and when to extend into the space between words.
432 6 my $underline = $page->gfx;
433 # $underline->strokecolor('black');
434 6 $underline->linewidth(.5);
435 6 $underline->move( $xpos, $ypos - 2);
436 6 ▶ if ($word =~ /<\/href[a-z]?/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($word =~ m[</href[a-z]?]) { } 1 5 6
437 1 $underline->line( $xpos + $width{$word}, $ypos - 2);
438 } else {
439 5 $underline->line( $xpos + $width{$word} + $wordspace, $ypos - 2);
440 }
441 6 $underline->stroke;
442  
443 # Add hyperlink
444 6 my $ann = $page->annotation;
445 6 $ann->rect($xpos, $ypos - 3, $xpos + $width{$word} + $wordspace, $ypos + 10);
446 6 $ann->url($href);
447 } else {
448 1083 $current_content_text->text($stripped);
449 }
450  
451 1089 ▶ unless ($width{$word}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($width{$word}) 0 1089 1089
452 0 ▶ $debug && _debug("Can't find \$width{$word}");
Branch: 1 branch
BranchTrueFalseTotal
$debug and _debug("Can't find \$width{$word}") 0 0 0
453 0 $width{$word} = 0;
454 }
455 1089 ▶ $xpos += ( $width{$word} + $wordspace ) if (@line);
Branch: 1 branch
BranchTrueFalseTotal
if @line 1089 0 1089
456  
457 1089 ▶ if ($word =~ /\//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($word =~ m[/]) 80 1009 1089
458 80 ▶ if ($word =~ /\/href[a-z]?/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($word =~ m[/href[a-z]?]) 1 79 80
459 1 undef $href;
460 }
461 80 ▶ unless ($href) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($href) 79 1 80
462 79 $current_content_text = $content_texts{default};
463 }
464 }
465 }
466 131 $endw = $self->w;
467 } else {
468 # calculate the left hand position of the line
469 28 ▶ if ( $align eq 'right' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($align eq 'right') { } 4 24 28
elsif ($align eq 'center') { } 6 18 24
470 4 $xpos += $self->w - $line_width;
471 } elsif ( $align eq 'center' ) {
472 6 $xpos += ( $self->w / 2 ) - ( $line_width / 2 );
473 }
474 # render the line
475 28 ▶ $debug && _debug("default 2", $xpos, $ypos, @line);
Branch: 1 branch
BranchTrueFalseTotal
$debug and _debug("default 2", $xpos, $ypos, @line) 0 28 28
476 28 $content_text->translate( $xpos, $ypos );
477 28 $endw = $content_texts{default}->text( join( ' ', @line ) );
478 }
479 159 $ypos -= $self->lead;
480 159 $first_line = 0;
481 }
482  
483 # Don't yet know why we'd want to return @paragraphs...
484 # unshift( @paragraphs, join( ' ', @paragraph ) ) if scalar(@paragraph);
485 #return ( $endw, $ypos ); # , join( "\n", @paragraphs ) )
486 26 ▶ unshift( @paragraphs, join( ' ', @paragraph ) ) if scalar(@paragraph);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @paragraph 2 24 26
487 26 my $overflow = join("\n",@paragraphs);
488 26 return ( $endw, $ypos, $overflow); #$overflow text returned to script
489 }
490  
491  
492 sub _debug{
493 0 ▶ my ($msg, $xpos, $ypos, @line) = @_;
Subroutine _debug: not called CC=1 SCAR=6.9
494 0 printf("[%s|%d|%d] ", $msg, $xpos, $ypos);
495 0 print join ' ', @line;
496 0 print "\n";
497 }
498  
499  
500 =head2 garbledy_gook
501  
502 Returns a scalar containing a paragraph of jibberish. Used by test scripts for
503 demonstrations.
504  
505 my $jibberish = $tb->garbledy_gook(50);
506  
507 The integer is the numer of jibberish words you want returned. Default is 100.
508  
509 =cut
510  
511 sub garbledy_gook {
512 10 ▶ my ($self, $words) = @_;
Subroutine garbledy_gook: called CC=4 SCAR=13.9
Pod documented
513 10 my $rval;
514 10 ▶ $words ||= 100;
Condition100%2/2
$words ||= 100
l!l
19
MC/DC100%1/1
$words ||= 100
$words
Truth table100%2/2
A
A= $words
Aresult
11
00
515 10 for (1..$words) {
516 930 for (1.. int(rand(10)) + 3) {
517 6867 $rval .= ('a'..'z')[ int(rand(26)) ];
518 }
519 930 $rval .= " ";
520 }
521 10 chop $rval;
522 10 return $rval;
523 }
524  
525  
526 # Applies defaults for you wherever you didn't explicitly set a different value.
527 sub _apply_defaults {
528 27 ▶ my ($self) = @_;
Subroutine _apply_defaults: called CC=5 SCAR=16.1
529 27 my %defaults = (
530 x => 20 / mm,
531 y => 238 / mm,
532 w => 175 / mm,
533 h => 220 / mm,
534 lead => 15 / pt,
535 parspace => 0 / pt,
536 align => 'justify',
537 fonts => {},
538 );
539 27 foreach my $att (keys %defaults) {
540 216 ▶ $self->$att($defaults{$att}) unless defined $self->$att;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->$att 34 182 216
541 }
542  
543 # Create a new page inside our .pdf unless a page was provided.
544 27 ▶ unless (defined $self->page) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->page) 4 23 27
545 4 $self->page($self->pdf->page);
546 }
547  
548 # Use garbledy gook unless text was provided.
549 27 ▶ unless (defined $self->text) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->text) 2 25 27
550 2 $self->text($self->garbledy_gook);
551 }
552 }
553  
554  
555 =head1 AUTHOR
556  
557 Jay Hannah, C<< <jay at jays.net> >>
558  
559 =head1 SUPPORT
560  
561 You can find documentation for this module with the perldoc command.
562  
563 perldoc PDF::TextBlock
564  
565 Source code and bug reports on github: L<http://github.com/jhannah/pdf-textblock>
566  
567 =head1 ACKNOWLEDGEMENTS
568  
569 This module started from, and has grown on top of, Rick Measham's (aka Woosta)
570 "Using PDF::API2" tutorial: http://rick.measham.id.au/pdf-api2/
571  
572 =head1 COPYRIGHT & LICENSE
573  
574 Copyright 2009-2020 Jay Hannah, all rights reserved.
575  
576 This program is free software; you can redistribute it and/or modify it
577 under the same terms as Perl itself.
578  
579 =cut
580  
581 7 1; # End of PDF::TextBlock
↑ Summary TextBlock/Font.pm »