TextBlock.pm

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