File Coverage

blib/lib/PDF/Builder/Lite.pm
Criterion Covered Total %
statement 46 165 27.8
branch 3 14 21.4
condition 0 9 0.0
subroutine 14 51 27.4
pod 42 42 100.0
total 105 281 37.3


line stmt bran cond sub pod time code
1             package PDF::Builder::Lite;
2              
3 2     2   71960 use strict;
  2         14  
  2         63  
4 2     2   10 use warnings;
  2         5  
  2         124  
5             #no warnings qw[ deprecated recursion uninitialized ];
6              
7             our $VERSION = '3.023'; # VERSION
8             our $LAST_UPDATE = '3.022'; # manually update whenever code is changed
9             # NOTE that this sub-package has not been tested and is not well documented!
10             # It is possible that it will be deprecated and removed.
11              
12 0         0 BEGIN {
13              
14 2     2   740 use PDF::Builder;
  2         7  
  2         68  
15 2     2   13 use PDF::Builder::Util;
  2         4  
  2         296  
16 2     2   14 use PDF::Builder::Basic::PDF::Utils;
  2         5  
  2         195  
17              
18 2     2   16 use POSIX qw( ceil floor );
  2         4  
  2         18  
19 2     2   179 use Scalar::Util qw(blessed);
  2         4  
  2         113  
20              
21 2     2   13 use vars qw( $hasWeakRef );
  2     0   3  
  2         3935  
22              
23             }
24              
25             =head1 NAME
26              
27             PDF::Builder::Lite - Lightweight PDF creation methods
28              
29             =head1 SYNOPSIS
30              
31             $pdf = PDF::Builder::Lite->new();
32             $pdf->page(595,842);
33             $img = $pdf->image('some.jpg');
34             $font = $pdf->corefont('Times-Roman');
35             $font = $pdf->ttfont('TimesNewRoman.ttf');
36              
37             =head1 METHODS
38              
39             =over
40              
41             =item $pdf = PDF::Builder::Lite->new(%opts)
42              
43             =item $pdf = PDF::Builder::Lite->new()
44              
45             =cut
46              
47             sub new {
48 3     3 1 1957 my ($class, %opts) = @_;
49              
50 3         10 my $self = {};
51 3         9 bless($self, $class);
52 3         22 $self->{'api'} = PDF::Builder->new(%opts);
53              
54 3         1297 return $self;
55             }
56              
57             =item $pdf->page()
58              
59             =item $pdf->page($width,$height)
60              
61             =item $pdf->page($llx,$lly, $urx,$ury)
62              
63             Opens a new page.
64              
65             =cut
66              
67             sub page {
68 1     1 1 575 my $self = shift();
69 1         6 $self->{'page'} = $self->{'api'}->page();
70 1 50       4 $self->{'page'}->mediabox(@_) if $_[0];
71 1         4 $self->{'gfx'} = $self->{'page'}->gfx();
72             # $self->{'gfx'}->compressFlate();
73 1         5 return $self;
74             }
75              
76             =item $pdf->mediabox($w,$h)
77              
78             =item $pdf->mediabox($llx,$lly, $urx,$ury)
79              
80             Sets the global mediabox.
81              
82             =cut
83              
84             sub mediabox {
85 1     1 1 4 my ($self, $x1,$y1, $x2,$y2) = @_;
86 1 50       4 if (defined $x2) {
87 0         0 $self->{'api'}->mediabox($x1,$y1, $x2,$y2);
88             } else {
89 1         5 $self->{'api'}->mediabox($x1,$y1);
90             }
91 1         5 return $self;
92             }
93              
94             =item $pdf->saveas($file)
95              
96             Saves the document (may B be modified later) and
97             deallocates the PDF structures.
98              
99             If C<$file> is just a hyphen '-', the stringified copy is returned, otherwise
100             the file is saved, and C<$self> is returned (for chaining calls).
101              
102             =cut
103              
104             sub saveas {
105 1     1 1 4 my ($self, $file) = @_;
106              
107 1 50       4 if ($file eq '-') {
108 1         5 return $self->{'api'}->stringify();
109             } else {
110 0         0 $self->{'api'}->saveas($file);
111 0         0 return $self;
112             }
113             # is the following code ever reached? - Phil
114 0         0 $self->{'api'}->end();
115 0         0 foreach my $k (keys %{$self}) {
  0         0  
116 0 0 0     0 if (blessed($k) and $k->can('release')) {
    0 0        
117 0         0 $k->release(1);
118             } elsif (blessed($k) and $k->can('end')) {
119 0         0 $k->end();
120             }
121 0         0 $self->{$k} = undef;
122 0         0 delete($self->{$k});
123             }
124 0         0 return;
125             }
126              
127              
128             =item $font = $pdf->corefont($fontname)
129              
130             Returns a new or existing Adobe core font object.
131              
132             B
133              
134             $font = $pdf->corefont('Times-Roman');
135             $font = $pdf->corefont('Times-Bold');
136             $font = $pdf->corefont('Helvetica');
137             $font = $pdf->corefont('ZapfDingbats');
138              
139             =cut
140              
141             sub corefont {
142 4     4 1 2400 my ($self, $name, @opts) = @_;
143              
144 4         24 my $obj = $self->{'api'}->corefont($name, @opts);
145 4         25 return $obj;
146             }
147              
148             =item $font = $pdf->ttfont($ttfile)
149              
150             Returns a new or existing TrueType font object.
151              
152             B
153              
154             $font = $pdf->ttfont('TimesNewRoman.ttf');
155             $font = $pdf->ttfont('/fonts/Univers-Bold.ttf');
156             $font = $pdf->ttfont('../Democratica-SmallCaps.ttf');
157              
158             =cut
159              
160             sub ttfont {
161 0     0 1 0 my ($self, $file, @opts) = @_;
162              
163 0         0 return $self->{'api'}->ttfont($file, @opts);
164             }
165              
166             =item $font = $pdf->psfont($ps_file, %options)
167              
168             =item $font = $pdf->psfont($ps_file)
169              
170             Returns a new Type1 (PS) font object.
171              
172             B
173              
174             $font = $pdf->psfont('TimesRoman.pfa', -afmfile => 'TimesRoman.afm', -encode => 'latin1');
175             $font = $pdf->psfont('/fonts/Univers.pfb', -pfmfile => '/fonts/Univers.pfm', -encode => 'latin2');
176              
177             =cut
178              
179             sub psfont {
180 0     0 1 0 my ($self, @args) = @_;
181              
182 0         0 return $self->{'api'}->psfont(@args);
183             }
184              
185             #=item @color = $pdf->color($colornumber [, $lightdark ])
186             #
187             #=item @color = $pdf->color($basecolor [, $lightdark ])
188             #
189             #Returns a color.
190             #
191             #B
192             #
193             # @color = $pdf->color(0); # 50% grey
194             # @color = $pdf->color(0,+4); # 10% grey
195             # @color = $pdf->color(0,-3); # 80% grey
196             # @color = $pdf->color('yellow'); # yellow, fully saturated
197             # @color = $pdf->color('red',+1); # red, +10% white
198             # @color = $pdf->color('green',-2); # green, +20% black
199             #
200             #=cut
201             #
202             #sub color {
203             # my $self = shift();
204             #
205             # return $self->{'api'}->businesscolor(@_);
206             #}
207              
208             =item $egs = $pdf->create_egs()
209              
210             Returns a new extended-graphics-state object.
211              
212             B
213              
214             $egs = $pdf->create_egs();
215              
216             =cut
217              
218             sub create_egs {
219 1     1 1 12 my ($self) = @_;
220              
221 1         9 return $self->{'api'}->egstate();
222             }
223              
224             =item $img = $pdf->image_jpeg($file)
225              
226             Returns a new JPEG image object.
227              
228             =cut
229              
230             sub image_jpeg {
231 0     0 1   my ($self, $file) = @_;
232              
233 0           return $self->{'api'}->image_jpeg($file);
234             }
235              
236             =item $img = $pdf->image_png($file)
237              
238             Returns a new PNG image object.
239              
240             =cut
241              
242             sub image_png {
243 0     0 1   my ($self, $file) = @_;
244              
245 0           return $self->{'api'}->image_png($file);
246             }
247              
248             =item $img = $pdf->image_tiff($file, %opts)
249              
250             =item $img = $pdf->image_tiff($file)
251              
252             Returns a new TIFF image object.
253              
254             =cut
255              
256             sub image_tiff {
257 0     0 1   my ($self, $file, @opts) = @_;
258              
259 0           return $self->{'api'}->image_tiff($file, @opts);
260             }
261              
262             =item $img = $pdf->image_pnm($file)
263              
264             Returns a new PNM image object.
265              
266             =cut
267              
268             sub image_pnm {
269 0     0 1   my ($self, $file) = @_;
270              
271 0           return $self->{'api'}->image_pnm($file);
272             }
273              
274             =item $pdf->savestate()
275              
276             Saves the state of the page.
277              
278             =cut
279              
280             sub savestate {
281 0     0 1   my $self = shift();
282              
283 0           return $self->{'gfx'}->save();
284             }
285              
286             =item $pdf->restorestate()
287              
288             Restores the state of the page.
289              
290             =cut
291              
292             sub restorestate {
293 0     0 1   my $self = shift();
294              
295 0           return $self->{'gfx'}->restore();
296             }
297              
298             =item $pdf->egstate($egs)
299              
300             Sets extended-graphics state.
301              
302             =cut
303              
304             sub egstate {
305 0     0 1   my $self = shift();
306              
307 0           $self->{'gfx'}->egstate(@_);
308 0           return $self;
309             }
310              
311             =item $pdf->fillcolor($color)
312              
313             Sets the fill color. See C for color names and specifications.
314              
315             =cut
316              
317             sub fillcolor {
318 0     0 1   my $self = shift();
319              
320 0           $self->{'gfx'}->fillcolor(@_);
321 0           return $self;
322             }
323              
324             =item $pdf->strokecolor($color)
325              
326             Sets the stroke color.
327              
328             B
329              
330             aliceblue, antiquewhite, aqua, aquamarine, azure, beige, bisque, black, blanchedalmond,
331             blue, blueviolet, brown, burlywood, cadetblue, chartreuse, chocolate, coral, cornflowerblue,
332             cornsilk, crimson, cyan, darkblue, darkcyan, darkgoldenrod, darkgray, darkgreen, darkgrey,
333             darkkhaki, darkmagenta, darkolivegreen, darkorange, darkorchid, darkred, darksalmon,
334             darkseagreen, darkslateblue, darkslategray, darkslategrey, darkturquoise, darkviolet,
335             deeppink, deepskyblue, dimgray, dimgrey, dodgerblue, firebrick, floralwhite, forestgreen,
336             fuchsia, gainsboro, ghostwhite, gold, goldenrod, gray, grey, green, greenyellow, honeydew,
337             hotpink, indianred, indigo, ivory, khaki, lavender, lavenderblush, lawngreen, lemonchiffon,
338             lightblue, lightcoral, lightcyan, lightgoldenrodyellow, lightgray, lightgreen, lightgrey,
339             lightpink, lightsalmon, lightseagreen, lightskyblue, lightslategray, lightslategrey,
340             lightsteelblue, lightyellow, lime, limegreen, linen, magenta, maroon, mediumaquamarine,
341             mediumblue, mediumorchid, mediumpurple, mediumseagreen, mediumslateblue, mediumspringgreen,
342             mediumturquoise, mediumvioletred, midnightblue, mintcream, mistyrose, moccasin, navajowhite,
343             navy, oldlace, olive, olivedrab, orange, orangered, orchid, palegoldenrod, palegreen,
344             paleturquoise, palevioletred, papayawhip, peachpuff, peru, pink, plum, powderblue, purple,
345             red, rosybrown, royalblue, saddlebrown, salmon, sandybrown, seagreen, seashell, sienna,
346             silver, skyblue, slateblue, slategray, slategrey, snow, springgreen, steelblue, tan, teal,
347             thistle, tomato, turquoise, violet, wheat, white, whitesmoke, yellow, yellowgreen
348              
349             or the rgb-hex-notation:
350              
351             #rgb, #rrggbb, #rrrgggbbb and #rrrrggggbbbb
352              
353             or the cmyk-hex-notation:
354              
355             %cmyk, %ccmmyykk, %cccmmmyyykkk and %ccccmmmmyyyykkkk
356              
357             or the hsl-hex-notation:
358              
359             &hsl, &hhssll, &hhhssslll and &hhhhssssllll
360              
361             or the hsv-hex-notation:
362              
363             !hsv, !hhssvv, !hhhsssvvv and !hhhhssssvvvv
364              
365             =cut
366              
367             sub strokecolor {
368 0     0 1   my $self = shift();
369              
370 0           $self->{'gfx'}->strokecolor(@_);
371 0           return $self;
372             }
373              
374             =item $pdf->linedash(@dash)
375              
376             Sets the line dash pattern.
377              
378             =cut
379              
380             sub linedash {
381 0     0 1   my ($self, @a) = @_;
382 0           $self->{'gfx'}->linedash(@a);
383 0           return $self;
384             }
385              
386             =item $pdf->linewidth($width)
387              
388             Sets the line width.
389              
390             =cut
391              
392             sub linewidth {
393 0     0 1   my ($self, $linewidth) = @_;
394              
395 0           $self->{'gfx'}->linewidth($linewidth);
396 0           return $self;
397             }
398              
399             =item $pdf->transform(%opts)
400              
401             Sets transformations (i.e., translate, rotate, scale, skew) in PDF-canonical order.
402              
403             B
404              
405             $pdf->transform(
406             -translate => [$x,$y],
407             -rotate => $rot,
408             -scale => [$sx,$sy],
409             -skew => [$sa,$sb],
410             )
411              
412             =cut
413              
414             sub transform {
415 0     0 1   my ($self, %opt) = @_;
416              
417 0           $self->{'gfx'}->transform(%opt);
418 0           return $self;
419             }
420              
421             =item $pdf->move($x,$y)
422              
423             Move to a new drawing location at C[$x,$y].
424              
425             =cut
426              
427             sub move { # x,y ...
428 0     0 1   my $self = shift();
429              
430 0           $self->{'gfx'}->move(@_);
431 0           return $self;
432             }
433              
434             =item $pdf->line($x,$y)
435              
436             Draw a line to C[$x,$y].
437              
438             =cut
439              
440             sub line { # x,y ...
441 0     0 1   my $self = shift();
442              
443 0           $self->{'gfx'}->line(@_);
444 0           return $self;
445             }
446              
447             =item $pdf->curve($x1,$y1, $x2,$y2, $x3,$y3)
448              
449             Draw a Bezier curve with three control points.
450              
451             =cut
452              
453             sub curve { # x1,y1,x2,y2,x3,y3 ...
454 0     0 1   my $self = shift();
455 0           $self->{'gfx'}->curve(@_);
456 0           return $self;
457             }
458              
459             =item $pdf->arc($xc,$yc, $rx,$ry, $alpha,$beta, $move, $dir)
460              
461             =item $pdf->arc($xc,$yc, $rx,$ry, $alpha,$beta, $move)
462              
463             Draw an arc centered at C[$xc,$yc], with x radius C[$rx] and y radius C[$ry],
464             from C[$alpha] degrees to C[$beta] degrees. If C[$move] is I, do B
465             draw a line to the start of the arc. C[$dir] defaults to 0 for counter-clockwise
466             sweep, and may be set to 1 for a clockwise sweep.
467              
468             =cut
469              
470             sub arc { # xc,yc, rx,ry, alpha,beta ,move [,dir]
471 0     0 1   my $self = shift();
472              
473 0           $self->{'gfx'}->arc(@_);
474 0           return $self;
475             }
476              
477             =item $pdf->ellipse($xc,$yc, $rx,$ry)
478              
479             Draw an ellipse centered at C[$xc,$yc], with x radius C[$rx] and y radius C[$ry].
480              
481             =cut
482              
483             sub ellipse {
484 0     0 1   my $self = shift();
485              
486 0           $self->{'gfx'}->ellipse(@_);
487 0           return $self;
488             }
489              
490             =item $pdf->circle($xc,$yc, $r)
491              
492             Draw a circle centered at C[$xc,$yc], of radius C[$r].
493              
494             =cut
495              
496             sub circle {
497 0     0 1   my $self = shift();
498              
499 0           $self->{'gfx'}->circle(@_);
500 0           return $self;
501             }
502              
503             =item $pdf->rect($x,$y, $w,$h)
504              
505             Draw a rectangle with lower left corner at C[$x,$y], width (+x) C[$w] and
506             height (+y) C[$h].
507              
508             =cut
509              
510             sub rect { # x,y, w,h ...
511 0     0 1   my $self = shift();
512              
513 0           $self->{'gfx'}->rect(@_);
514 0           return $self;
515             }
516              
517             =item $pdf->rectxy($x1,$y1, $x2,$y2)
518              
519             Draw a rectangle with opposite corners C[$x1,$y1] and C[$x2,$y2].
520              
521             =cut
522              
523             sub rectxy {
524 0     0 1   my $self = shift();
525              
526 0           $self->{'gfx'}->rectxy(@_);
527 0           return $self;
528             }
529              
530             =item $pdf->poly($x1,$y1, ..., $xn,$yn)
531              
532             Draw a polyline (multiple line segments) starting at (I to) C[$x1,$y1] and
533             continuing on to C[$x2,$y2], ..., C[$xn,$yn].
534              
535             =cut
536              
537             sub poly {
538 0     0 1   my $self = shift();
539              
540 0           $self->{'gfx'}->poly(@_);
541 0           return $self;
542             }
543              
544             =item $pdf->close()
545              
546             Close a shape (draw a line back to the beginning).
547              
548             =cut
549              
550             sub close {
551 0     0 1   my $self = shift();
552              
553 0           $self->{'gfx'}->close();
554 0           return $self;
555             }
556              
557             =item $pdf->stroke()
558              
559             Stroke (actually draw) a shape whose path has already been laid out, using
560             the requested C.
561              
562             =cut
563              
564             sub stroke {
565 0     0 1   my $self = shift();
566              
567 0           $self->{'gfx'}->stroke();
568 0           return $self;
569             }
570              
571             =item $pdf->fill()
572              
573             Fill in a closed geometry (path), using the requested C.
574             The I is used if the path crosses itself.
575              
576             =cut
577              
578             sub fill { # nonzero winding rule
579 0     0 1   my $self = shift();
580              
581 0           $self->{'gfx'}->fill();
582 0           return $self;
583             }
584              
585             =item $pdf->fillstroke()
586              
587             Fill (using C) I stroke (using C) a closed path.
588             The I is used if the path crosses itself.
589              
590             =cut
591              
592             sub fillstroke { # nonzero winding rule
593 0     0 1   my $self = shift();
594              
595 0           $self->{'gfx'}->fillstroke();
596 0           return $self;
597             }
598              
599             =item $pdf->image($imgobj, $x,$y, $w,$h)
600              
601             =item $pdf->image($imgobj, $x,$y, $scale)
602              
603             =item $pdf->image($imgobj, $x,$y)
604              
605             B The width/height or scale given
606             is in user-space coordinates, which are subject to
607             transformations which may have been specified beforehand.
608              
609             Per default this has a 72dpi resolution, so if you want an
610             image to have a 150 or 300dpi resolution, you should specify
611             a scale of 72/150 (or 72/300) or adjust width/height accordingly.
612              
613             =cut
614              
615             sub image {
616 0     0 1   my $self = shift();
617              
618 0           $self->{'gfx'}->image(@_);
619 0           return $self;
620             }
621              
622             =item $pdf->textstart()
623              
624             Forces the start of text mode while in graphics.
625              
626             =cut
627              
628             sub textstart {
629 0     0 1   my $self = shift();
630              
631 0           $self->{'gfx'}->textstart();
632 0           return $self;
633             }
634              
635             =item $pdf->textfont($fontobj, $size)
636              
637             Define the current font to be an (already defined) font object at the given size.
638              
639             =cut
640              
641             sub textfont {
642 0     0 1   my $self = shift();
643              
644 0           $self->{'gfx'}->font(@_);
645 0           return $self;
646             }
647              
648             =item $txt->textleading($leading)
649              
650             Set the baseline-to-baseline "leading" to be used for text lines.
651              
652             =item $txt->textlead($leading)
653              
654             Set the baseline-to-baseline "leading" to be used for text lines.
655              
656             B will be removed March 2023 or later. Use textleading().
657              
658             =cut
659              
660             # remove on or after March 2023
661             sub textlead {
662 0     0 1   return $_[0]->textleading($_[1]);
663             }
664              
665             sub textleading {
666 0     0 1   my $self = shift();
667              
668 0           $self->{'gfx'}->leading(@_);
669 0           return $self;
670             }
671              
672             =item $pdf->text($string)
673              
674             Applies (writes out) the given text at the current text location, using the
675             already-specified font.
676              
677             =cut
678              
679             sub text {
680 0     0 1   my $self = shift();
681              
682 0   0       return $self->{'gfx'}->text(@_) || $self;
683             }
684              
685             =item $pdf->nl()
686              
687             Write a newline (drop down to the next line).
688              
689             =cut
690              
691             sub nl {
692 0     0 1   my $self = shift();
693              
694 0           $self->{'gfx'}->nl();
695 0           return $self;
696             }
697              
698             =item $pdf->textend()
699              
700             Force an end to text output and return to graphics.
701              
702             =cut
703              
704             sub textend {
705 0     0 1   my $self = shift();
706              
707 0           $self->{'gfx'}->textend();
708 0           return $self;
709             }
710              
711             =item $pdf->print($font, $size, $x,$y, $rot, $just, $text)
712              
713             Convenience wrapper for shortening the textstart..textend sequence.
714              
715             Go into text mode, set the font to the object and size, go to the location,
716             set any rotation, set justification, and write the array of text.
717             Justification is 0 for left, 1 for center, and 2 for right.
718              
719             =cut
720              
721             sub print {
722 0     0 1   my $self = shift();
723 0           my ($font, $size, $x,$y, $rot, $just, @text) = @_;
724              
725 0           my $text = join(' ', @text);
726 0           $self->textstart();
727 0           $self->textfont($font, $size);
728 0           $self->transform(
729             -translate=>[$x, $y],
730             -rotate=> $rot,
731             );
732 0 0         if ($just==1) {
    0          
733 0           $self->{'gfx'}->text_center($text);
734             } elsif ($just==2) {
735 0           $self->{'gfx'}->text_right($text);
736             } else {
737 0           $self->text(@text);
738             }
739 0           $self->textend();
740 0           return $self;
741             }
742              
743             1;
744              
745             __END__