blib/lib/App/a2pdf.pm untested

filter: Statementstmt 0.0% 0 / 207 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 336 CC 27 SCAR 27.6
CC
27
Cov
0%
new
42.0
write_line
30.0
print_word
30.0
CRAP 15.7
1 # App::a2pdf
2 #
3 # Copyright (C) 2007 Jon Allen <jj@jonallen.info>
4 #
5 # This software is licensed under the terms of the Artistic
6 # License version 2.0.
7 #
8 # For full license details, please read the file 'artistic-2_0.txt'
9 # included with this distribution, or see
10 # http://www.perlfoundation.org/legal/licenses/artistic-2_0.html
11  
12 package App::a2pdf;
13  
14 use strict;
15 use warnings;
16 use PDF::API2;
17 use Switch 'Perl6';
18  
19 BEGIN {
20 }
21  
22  
23 #-----------------------------------------------------------------------
24  
25 sub new {
26 my $invocant = shift;
27 my $class = ref($invocant) || $invocant;;
28  
29 # Set default options
30 my $self = { @_ };
31 bless $self,$class;
32
33 # Define style mapping
34 # This will relate Perl::Tidy's token types to a printing style
35 $self->{stylemap} = {
36 'header' => 'helvetica_bold_10',
37 'footer' => 'helvetica_bold_10',
38 'k' => 'black_bold',
39 '{' => 'black_bold',
40 '}' => 'black_bold',
41 'POD' => 'grey_italic',
42 'POD_START' => 'grey_italic',
43 'POD_END' => 'grey_italic',
44 'END_START' => 'grey_italic',
45 'DATA_START' => 'grey_italic',
46 'DATA' => 'grey_italic',
47 'SYSTEM' => 'grey_italic',
48 '#' => 'grey_italic',
49 'J' => 'red_italic',
50 'j' => 'red_italic',
51 'i' => 'blue',
52 '->' => 'blue',
53 'w' => 'green',
54 'L' => 'brown',
55 'R' => 'brown',
56 'Q' => 'purple',
57 'q' => 'purple',
58 };
59
60 # Define styles
61 # Supports 3 properties, font (e.g. Helvetica, Courier, Times),
62 # color (in hex), and type (Bold, Oblique, or BoldOblique)
63 $self->{stylist} = {
64 'helvetica9' => {font=>'Helvetica',size=>9},
65 'helvetica_bold_10' => {font=>'Helvetica',size=>10,type=>'Bold'},
66 'black_bold' => {color=>'#000000',type=>'Bold'},
67 'grey_italic' => {color=>'#333333',type=>'Oblique'},
68 'red_italic' => {color=>'#cc2222',type=>'Oblique'},
69 'blue' => {color=>'#222288'},
70 'green' => {color=>'#228822'},
71 'brown' => {color=>'#666622'},
72 'purple' => {color=>'#882288'},
73 };
74
75 # Set up first page
76 $self->{page_number} = 0;
77 $self->{line_number} = 1;
78 $self->{line_number_width} = 0;
79 $self->{line_spacing} = $self->{font_size}+2 unless ($self->{line_spacing});
80 $self->{x_position} = $self->{left_margin};
81 $self->{y_position} = $self->{page_height} - $self->{top_margin};
82 $self->{pdf} = PDF::API2->new;
83 $self->{pdf}->mediabox($self->{page_width},$self->{page_height});
84
85 if ($self->{icon}) {
86 # Load required modules to handle images
87 eval "use File::Type;use Image::Size";
88 unless ($@) {
89 if (-e $self->{icon}) {
90 my $type = File::Type->new->checktype_filename($self->{icon});
91 given ($type) {
92 when 'image/jpeg' {$self->{icon_img} = $self->{pdf}->image_jpeg($self->{icon})}
93 when 'image/tiff' {$self->{icon_img} = $self->{pdf}->image_tiff($self->{icon})}
94 when 'image/gif' {$self->{icon_img} = $self->{pdf}->image_gif($self->{icon})}
95 when 'image/x-png' {$self->{icon_img} = $self->{pdf}->image_png($self->{icon})}
96 when 'image/x-pnm' {$self->{icon_img} = $self->{pdf}->image_pnm($self->{icon})}
97 default {warn "[Warning] Unknown image format '$type' for icon ".$self->{icon}."\n"}
98 }
99 if ($self->{icon_img}) {
100 ($self->{icon_width},$self->{icon_height}) = imgsize($self->{icon});
101 }
102 } else {
103 warn("[Warning] Cannot open icon file: ".$self->{icon}."\n");
104 }
105 } else {
106 warn("[Warning] The modules File::Type and Image::Size are required to use icons\n")
107 }
108 }
109  
110 $self->formfeed;
111 $self->set_style;
112 return $self;
113 }
114  
115  
116 #-----------------------------------------------------------------------
117  
118 sub print {
119 my $self = shift;
120 my $line = shift;
121  
122 if ($self->{newline_flag}) {
123 $self->newline;
124 $self->{newline_flag} = 0;
125 }
126  
127 $self->print_line_number if $self->{line_numbers};
128 $self->print_text_with_style($line);
129 $self->{newline_flag} = 1;
130 }
131  
132  
133 #-----------------------------------------------------------------------
134  
135  
136  
137 sub write_line { # This is the write_line method called by Perl::Tidy
138 my $self = shift;
139 my $line = shift;
140 my $line_number = $line->{_line_number};
141 my $line_type = $line->{_line_type};
142 my $line_text = $line->{_line_text};
143 chomp $line_text;
144  
145 if ($self->{newline_flag}) {
146 $self->newline;
147 $self->{newline_flag} = 0;
148 }
149 $self->print_line_number if $self->{line_numbers};
150  
151 if ($line_type eq 'CODE') {
152 $self->print_text_with_style($1) if ($line_text =~ /^(\s+)/);
153 my @rtoken_list = @{$line->{_rtokens}};
154 my @rtoken_types = @{$line->{_rtoken_type}};
155 foreach my $rtoken (@rtoken_list) {
156 my $rtoken_type = shift @rtoken_types;
157 $self->print_text_with_style($rtoken,$rtoken_type);
158 }
159 } else {
160 $self->print_text_with_style($line_text,$line_type);
161 }
162 $self->{newline_flag} = 1;
163 }
164  
165  
166 #-----------------------------------------------------------------------
167  
168 sub newline {
169 my $self = shift;
170
171 $self->linefeed;
172 $self->{line_number}++;
173 $self->{overflow} = 0;
174 }
175  
176  
177 #-----------------------------------------------------------------------
178  
179 sub linefeed {
180 my $self = shift;
181
182 $self->{y_position} -= $self->{line_spacing};
183 $self->{x_position} = $self->{left_margin} + $self->{line_number_width};
184 $self->{overflow} = 1;
185
186 if ($self->{y_position} < ($self->{bottom_margin} + $self->{footer_height})) {
187 my $style = $self->{style};
188 $self->formfeed;
189 $self->set_style($style);
190 }
191 }
192  
193  
194 #-----------------------------------------------------------------------
195  
196 sub formfeed {
197 my $self = shift;
198 $self->{page} = $self->{pdf}->page;
199 $self->{x_position} = $self->{left_margin} + $self->{line_number_width};
200 $self->{page_number}++;
201  
202 delete $self->{text};
203 delete $self->{gfx};
204 $self->{gfx} = $self->{page}->gfx;
205 $self->{text} = $self->{page}->text;
206
207 $self->{y_position} = $self->{page_height} - $self->{top_margin} - $self->{line_spacing};
208 $self->{header_height} = ($self->{header}) ? $self->generate_header : 0;
209 $self->{footer_height} = ($self->{footer}) ? $self->generate_footer : 0;
210 $self->{y_position} -= $self->{header_height};
211 }
212  
213  
214 #-----------------------------------------------------------------------
215  
216 sub generate_header {
217 my $self = shift;
218 $self->set_style('header');
219
220 my $header_padding = 2;
221 my $header_spacing = 3;
222 my $header_height = $self->{text_size} + $header_spacing + $header_padding;
223
224 # Draw header icon
225 if ($self->{icon_img}) {
226 my $icon_height_in_points = $self->{icon_height} * $self->{icon_scale};
227 if ($icon_height_in_points > $self->{text_size}) {
228 $header_height += ($icon_height_in_points - $self->{text_size});
229 }
230 my $ypos = $self->{page_height} - $self->{top_margin} - $icon_height_in_points;
231 $self->{gfx}->image($self->{icon_img},$self->{left_margin},$ypos,$self->{icon_scale});
232 }
233  
234 # Add page title
235 my $x = $self->{page_width} - $self->{right_margin} - $self->{text}->advancewidth($self->{title});
236 my $y = $self->{page_height} - $self->{top_margin} - $header_height + $header_spacing + $header_padding;
237 $self->{text}->textlabel($x,$y,$self->{fontcache}->{$self->{font}},$self->{text_size},$self->{title});
238  
239 # Draw horizontal line
240 $self->{gfx}->move($self->{left_margin},$self->{page_height}-$self->{top_margin}-$header_height + $header_padding);
241 $self->{gfx}->line($self->{page_width}-$self->{right_margin},$self->{page_height}-$self->{top_margin}-$header_height + $header_padding);
242 $self->{gfx}->stroke;
243
244 return $header_height;
245 }
246  
247  
248 #-----------------------------------------------------------------------
249  
250 sub generate_footer {
251 my $self = shift;
252 $self->set_style('footer');
253  
254 # Add page footer
255 my $t = 'Page '.$self->{page_number};
256 my $x = $self->{page_width} - $self->{right_margin} - $self->{text}->advancewidth($t);
257 my $y = $self->{bottom_margin};
258 $self->{text}->textlabel($x,$y,$self->{fontcache}->{$self->{font}},$self->{text_size},$t);
259  
260  
261 $self->{gfx} = $self->{page}->gfx unless (exists $self->{gfx});
262 $self->{gfx}->move($self->{left_margin},$self->{bottom_margin}+10);
263 $self->{gfx}->line($self->{page_width}-$self->{right_margin},$self->{bottom_margin}+10);
264 $self->{gfx}->stroke;
265 return 18; # Footer height in points
266 }
267  
268  
269 #-----------------------------------------------------------------------
270  
271 sub output {
272 my $self = shift;
273 print $self->{pdf}->stringify;
274 #$self->{pdf}->end;
275 }
276  
277  
278 #-----------------------------------------------------------------------
279  
280 sub line_number_chars {
281 my $self = shift;
282 my $line_number_chars = shift;
283 $self->{line_number_chars} = $line_number_chars;
284 $self->{line_number_width} = ($self->{line_numbers}) ? $self->{text}->advancewidth('X' x ($line_number_chars + 2)) : 0;
285 $self->{line_number_template} = '%'.$line_number_chars.'d: %s';
286 $self->{x_position} = $self->{left_margin} + $self->{line_number_width};
287 }
288  
289  
290 #-----------------------------------------------------------------------
291  
292 sub print_line_number {
293 my $self = shift;
294
295 $self->set_style;
296 my $width = $self->{text}->advancewidth($self->{line_number}.':X');
297 my $x_pos = $self->{left_margin} + $self->{line_number_width} - $width;
298 $self->{text}->textlabel($x_pos,$self->{y_position},$self->{fontcache}->{$self->{font}},$self->{text_size},$self->{line_number}.':');
299 }
300  
301  
302 #--print_text_with_style---------------------------------------------------
303  
304 sub print_text_with_style {
305 my $self = shift;
306 my $text = shift;
307 $self->set_style(shift);
308  
309 while ($text =~ /(\f|[^\f]+)/g) {
310 my $block = $1;
311 if ($block =~ /\f/ && !exists $self->{noformfeed}) {
312 $self->formfeed;
313 $self->{x_position} = $self->{left_margin} + $self->{line_number_width};
314 } else {
315 while ($block =~ /(\s+|\S+)/g) {
316 my $word = $1;
317 $self->print_word($word);
318 }
319 }
320 }
321 }
322  
323  
324 #--print_word--------------------------------------------------------------
325 #
326 # Purpose: Adds a single word to the PDF in the current style
327 #
328 # Usage: $self->print_word('word');
329 #
330 #--------------------------------------------------------------------------
331  
332 sub print_word {
333 my $self = shift;
334 my $word = shift;
335
336  
337 my $width = $self->{text}->advancewidth($word);
338 if ($self->{x_position} + $width > $self->{page_width} - $self->{right_margin}) {
339 # If the word will not fit on one line, split it up and recurse the 'print_word' sub
340 if ($width > ($self->{page_width} - $self->{left_margin} - $self->{right_margin})) {
341 my $fit = int(($self->{page_width} - $self->{x_position} - $self->{right_margin}) / $self->{nspace});
342 my @words = (substr($word,0,$fit),substr($word,$fit));
343 $self->print_word($_) foreach @words;
344 return;
345 }
346 $self->linefeed;
347 if ($word =~ /^\s+$/ &&
348 $self->{overflow} &&
349 $self->{x_position} == $self->{left_margin} + $self->{line_number_width}) {
350 return;
351 }
352 }
353 $self->{x_position} += $self->{text}->textlabel($self->{x_position},
354 $self->{y_position},
355 $self->{fontcache}->{$self->{font}},
356 $self->{text_size},
357 $word,
358 -color => $self->{text_color});
359 if ($self->{x_position} > $self->{page_width} - $self->{right_margin}) {
360 $self->linefeed;
361 }
362 }
363  
364  
365 #--set_style---------------------------------------------------------------
366 #
367 # Purpose: Sets current style (font, size, colour)
368 #
369 # Usage: $self->set_style('stylename');
370 #
371 #--------------------------------------------------------------------------
372  
373 sub set_style {
374 my $self = shift;
375 my $style = shift || 'default';
376
377 $style = (exists $self->{stylemap}->{$style}) ? $self->{stylemap}->{$style} : 'default';
378
379 # Create font object if necessary
380 my $font = ($self->{stylist}->{$style}->{font} || $self->{font_face}) .
381 ((exists $self->{stylist}->{$style}->{type}) ? '-'.$self->{stylist}->{$style}->{type} : '');
382 unless (exists $self->{fontcache}->{$font}) {
383 $self->{fontcache}->{$font} = $self->{pdf}->corefont($font);
384 }
385
386 $self->{style} = $style;
387 $self->{font} = $font;
388 $self->{text_color} = $self->{stylist}->{$style}->{color} || '#000000';
389 $self->{text_size} = $self->{stylist}->{$style}->{size} || $self->{font_size};
390
391 $self->{text}->font($self->{fontcache}->{$font},$self->{text_size});
392 $self->{nspace} = $self->{text}->advancewidth('n');
393 }
394  
395  
396 #--------------------------------------------------------------------------
397  
398 sub _MANIFEST {
399 require File::Type;
400 require Image::Size;
401 require PDF::API2::Content;
402 require PDF::API2::Win32;
403 require PDF::API2::Lite;
404 require PDF::API2::UniWrap;
405 }
406  
407 1;