File Coverage

blib/lib/PDF/API3/Compat/API2/Annotation.pm
Criterion Covered Total %
statement 26 149 17.4
branch 0 64 0.0
condition 0 6 0.0
subroutine 9 23 39.1
pod 13 14 92.8
total 48 256 18.7


line stmt bran cond sub pod time code
1             #=======================================================================
2             # ____ ____ _____ _ ____ ___ ____
3             # | _ \| _ \| ___| _ _ / \ | _ \_ _| |___ \
4             # | |_) | | | | |_ (_) (_) / _ \ | |_) | | __) |
5             # | __/| |_| | _| _ _ / ___ \| __/| | / __/
6             # |_| |____/|_| (_) (_) /_/ \_\_| |___| |_____|
7             #
8             # A Perl Module Chain to faciliate the Creation and Modification
9             # of High-Quality "Portable Document Format (PDF)" Files.
10             #
11             # Copyright 1999-2005 Alfred Reibenschuh .
12             #
13             #=======================================================================
14             #
15             # This library is free software; you can redistribute it and/or
16             # modify it under the terms of the GNU Lesser General Public
17             # License as published by the Free Software Foundation; either
18             # version 2 of the License, or (at your option) any later version.
19             #
20             # This library is distributed in the hope that it will be useful,
21             # but WITHOUT ANY WARRANTY; without even the implied warranty of
22             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23             # Lesser General Public License for more details.
24             #
25             # You should have received a copy of the GNU Lesser General Public
26             # License along with this library; if not, write to the
27             # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28             # Boston, MA 02111-1307, USA.
29             #
30             # $Id: Annotation.pm,v 2.1 2007/10/02 19:59:37 areibens Exp $
31             #
32             #=======================================================================
33             package PDF::API3::Compat::API2::Annotation;
34            
35             BEGIN
36             {
37            
38 1     1   5 use strict;
  1         3  
  1         37  
39 1     1   5 use vars qw(@ISA $VERSION);
  1         3  
  1         49  
40            
41 1     1   7 use PDF::API3::Compat::API2::Basic::PDF::Dict;
  1         2  
  1         27  
42 1     1   5 use PDF::API3::Compat::API2::Basic::PDF::Utils;
  1         3  
  1         124  
43 1     1   6 use PDF::API3::Compat::API2::Util;
  1         1  
  1         241  
44            
45 1     1   23 @ISA = qw(PDF::API3::Compat::API2::Basic::PDF::Dict);
46            
47 1         39 ( $VERSION ) = sprintf '%i.%03i', split(/\./,('$Revision: 2.1 $' =~ /Revision: (\S+)\s/)[0]); # $Date: 2007/10/02 19:59:37 $
48            
49 1     1   7 use utf8;
  1         2  
  1         11  
50 1     1   30 use Encode qw(:all);
  1         3  
  1         419  
51             }
52            
53 1     1   109 no warnings qw[ deprecated recursion uninitialized ];
  1         2  
  1         2047  
54            
55             =head1 $ant = PDF::API3::Compat::API2::Annotation->new
56            
57             Returns a annotation object (called from $page->annotation).
58            
59             =cut
60            
61             sub new
62             {
63 0     0 1   my ($class,%opts)=@_;
64 0           my $self=$class->SUPER::new;
65 0           $self->{Type}=PDFName('Annot');
66 0           $self->{Border}=PDFArray(PDFNum(0),PDFNum(0),PDFNum(0));
67 0           return($self);
68             }
69            
70             sub outobjdeep
71             {
72 0     0 1   my ($self, @opts) = @_;
73 0           foreach my $k (qw[ api apipdf apipage ])
74             {
75 0           $self->{" $k"}=undef;
76 0           delete($self->{" $k"});
77             }
78 0           $self->SUPER::outobjdeep(@opts);
79             }
80            
81             =item $ant->link $page, %opts
82            
83             Defines the annotation as launch-page with page $page and
84             options %opts (-rect, -border or 'dest-options').
85            
86             =cut
87            
88             sub link
89             {
90 0     0 1   my ($self,$page,%opts)=@_;
91 0           $self->{Subtype}=PDFName('Link');
92 0 0         if(ref $page)
93             {
94 0           $self->{A}=PDFDict();
95 0           $self->{A}->{S}=PDFName('GoTo');
96             }
97 0           $self->dest($page,%opts);
98 0 0         $self->rect(@{$opts{-rect}}) if(defined $opts{-rect});
  0            
99 0 0         $self->border(@{$opts{-border}}) if(defined $opts{-border});
  0            
100 0           return($self);
101             }
102            
103             =item $ant->url $url, %opts
104            
105             Defines the annotation as launch-url with url $url and
106             options %opts (-rect and/or -border).
107            
108             =cut
109            
110             sub url
111             {
112 0     0 1   my ($self,$url,%opts)=@_;
113 0           $self->{Subtype}=PDFName('Link');
114 0           $self->{A}=PDFDict();
115 0           $self->{A}->{S}=PDFName('URI');
116 0 0         if(is_utf8($url))
117             {
118             # URI must be 7-bit ascii
119 0           utf8::downgrade($url);
120             }
121 0           $self->{A}->{URI}=PDFStr($url);
122             # this will come again -- since the utf8 urls are coming !
123             # -- fredo
124             #if(is_utf8($url) || utf8::valid($url)) {
125             # $self->{A}->{URI}=PDFUtf($url);
126             #} else {
127             # $self->{A}->{URI}=PDFStr($url);
128             #}
129 0 0         $self->rect(@{$opts{-rect}}) if(defined $opts{-rect});
  0            
130 0 0         $self->border(@{$opts{-border}}) if(defined $opts{-border});
  0            
131 0           return($self);
132             }
133            
134             =item $ant->file $file, %opts
135            
136             Defines the annotation as launch-file with filepath $file and
137             options %opts (-rect and/or -border).
138            
139             =cut
140            
141             sub file
142             {
143 0     0 1   my ($self,$url,%opts)=@_;
144 0           $self->{Subtype}=PDFName('Link');
145 0           $self->{A}=PDFDict();
146 0           $self->{A}->{S}=PDFName('Launch');
147 0 0         if(is_utf8($url))
148             {
149             # URI must be 7-bit ascii
150 0           utf8::downgrade($url);
151             }
152 0           $self->{A}->{F}=PDFStr($url);
153             # this will come again -- since the utf8 urls are coming !
154             # -- fredo
155             #if(is_utf8($url) || utf8::valid($url)) {
156             # $self->{A}->{F}=PDFUtf($url);
157             #} else {
158             # $self->{A}->{F}=PDFStr($url);
159             #}
160 0 0         $self->rect(@{$opts{-rect}}) if(defined $opts{-rect});
  0            
161 0 0         $self->border(@{$opts{-border}}) if(defined $opts{-border});
  0            
162 0           return($self);
163             }
164            
165             =item $ant->pdfile $pdfile, $pagenum, %opts
166            
167             Defines the annotation as pdf-file with filepath $pdfile, $pagenum
168             and options %opts (same as dest).
169            
170             =cut
171            
172             sub pdfile
173             {
174 0     0 1   my ($self,$url,$pnum,%opts)=@_;
175 0           $self->{Subtype}=PDFName('Link');
176 0           $self->{A}=PDFDict();
177 0           $self->{A}->{S}=PDFName('GoToR');
178 0 0         if(is_utf8($url))
179             {
180             # URI must be 7-bit ascii
181 0           utf8::downgrade($url);
182             }
183 0           $self->{A}->{F}=PDFStr($url);
184             # this will come again -- since the utf8 urls are coming !
185             # -- fredo
186             #if(is_utf8($url) || utf8::valid($url)) {
187             # $self->{A}->{F}=PDFUtf($url);
188             #} else {
189             # $self->{A}->{F}=PDFStr($url);
190             #}
191            
192 0           $self->dest(PDFNum($pnum),%opts);
193            
194 0 0         $self->rect(@{$opts{-rect}}) if(defined $opts{-rect});
  0            
195 0 0         $self->border(@{$opts{-border}}) if(defined $opts{-border});
  0            
196            
197 0           return($self);
198             }
199            
200             =item $ant->text $text, %opts
201            
202             Defines the annotation as textnote with content $text and
203             options %opts (-rect and/or -open).
204            
205             =cut
206            
207             sub text
208             {
209 0     0 1   my ($self,$text,%opts)=@_;
210 0           $self->{Subtype}=PDFName('Text');
211 0           $self->content($text);
212 0 0         $self->rect(@{$opts{-rect}}) if(defined $opts{-rect});
  0            
213 0 0         $self->open($opts{-open}) if(defined $opts{-open});
214 0           return($self);
215             }
216            
217             =item $ant->movie $file, $contentype, %opts
218            
219             Defines the annotation as a movie from $file with $contentype and
220             options %opts (-rect).
221            
222             =cut
223            
224             sub movie
225             {
226 0     0 1   my ($self,$file,$contentype,%opts)=@_;
227 0           $self->{Subtype}=PDFName('Movie');
228 0           $self->{A}=PDFBool(1);
229 0           $self->{Movie}=PDFDict();
230 0           $self->{Movie}->{F}=PDFDict();
231 0           $self->{' apipdf'}->new_obj($self->{Movie}->{F});
232 0           my $f=$self->{Movie}->{F};
233 0           $f->{Type}=PDFName('EmbeddedFile');
234 0           $f->{Subtype}=PDFName($contentype);
235 0           $f->{' streamfile'}=$file;
236 0 0         $self->rect(@{$opts{-rect}}) if(defined $opts{-rect});
  0            
237 0           return($self);
238             }
239            
240             =item $ant->rect $llx, $lly, $urx, $ury
241            
242             Sets the rectangle of the annotation.
243            
244             =cut
245            
246             sub rect
247             {
248 0     0 1   my ($self,@r)=@_;
249 0 0         die "insufficient parameters to annotation->rect( ) " unless(scalar @r == 4);
250 0           $self->{Rect}=PDFArray( map { PDFNum($_) } $r[0],$r[1],$r[2],$r[3], );
  0            
251 0           return($self);
252             }
253            
254             =item $ant->border @b
255            
256             Sets the border-styles of the annotation, if applicable.
257            
258             =cut
259            
260             sub border
261             {
262 0     0 1   my ($self,@r)=@_;
263 0 0         die "insufficient parameters to annotation->border( ) " unless(scalar @r == 3);
264 0           $self->{Border}=PDFArray( map { PDFNum($_) } $r[0],$r[1],$r[2] );
  0            
265 0           return($self);
266             }
267            
268             =item $ant->content @lines
269            
270             Sets the text-content of the annotation, if applicable.
271            
272             =cut
273            
274             sub content
275             {
276 0     0 1   my ($self,@t)=@_;
277 0           my $t=join("\n",@t);
278 0 0 0       if(is_utf8($t) || utf8::valid($t))
279             {
280 0           $self->{Contents}=PDFUtf($t);
281             }
282             else
283             {
284 0           $self->{Contents}=PDFStr($t);
285             }
286 0           return($self);
287             }
288            
289             sub name
290             {
291 0     0 0   my ($self,$n)=@_;
292 0           $self->{Name}=PDFName($n);
293 0           return($self);
294             }
295            
296             =item $ant->open $bool
297            
298             Display the annotation either open or closed, if applicable.
299            
300             =cut
301            
302             sub open
303             {
304 0     0 1   my ($self,$n)=@_;
305 0 0         $self->{Open}=PDFBool( $n ? 1 : 0 );
306 0           return($self);
307             }
308            
309             =item $ant->dest( $page, -fit => 1 )
310            
311             Display the page designated by page, with its contents magnified just enough to
312             fit the entire page within the window both horizontally and vertically. If the
313             required horizontal and vertical magnification factors are different, use the
314             smaller of the two, centering the page within the window in the other dimension.
315            
316             =item $ant->dest( $page, -fith => $top )
317            
318             Display the page designated by page, with the vertical coordinate top positioned
319             at the top edge of the window and the contents of the page magnified just enough
320             to fit the entire width of the page within the window.
321            
322             =item $ant->dest( $page, -fitv => $left )
323            
324             Display the page designated by page, with the horizontal coordinate left positioned
325             at the left edge of the window and the contents of the page magnified just enough
326             to fit the entire height of the page within the window.
327            
328             =item $ant->dest( $page, -fitr => [ $left, $bottom, $right, $top ] )
329            
330             Display the page designated by page, with its contents magnified just enough to
331             fit the rectangle specified by the coordinates left, bottom, right, and top
332             entirely within the window both horizontally and vertically. If the required
333             horizontal and vertical magnification factors are different, use the smaller of
334             the two, centering the rectangle within the window in the other dimension.
335            
336             =item $ant->dest( $page, -fitb => 1 )
337            
338             (PDF 1.1) Display the page designated by page, with its contents magnified just
339             enough to fit its bounding box entirely within the window both horizontally and
340             vertically. If the required horizontal and vertical magnification factors are
341             different, use the smaller of the two, centering the bounding box within the
342             window in the other dimension.
343            
344             =item $ant->dest( $page, -fitbh => $top )
345            
346             (PDF 1.1) Display the page designated by page, with the vertical coordinate top
347             positioned at the top edge of the window and the contents of the page magnified
348             just enough to fit the entire width of its bounding box within the window.
349            
350             =item $ant->dest( $page, -fitbv => $left )
351            
352             (PDF 1.1) Display the page designated by page, with the horizontal coordinate
353             left positioned at the left edge of the window and the contents of the page
354             magnified just enough to fit the entire height of its bounding box within the
355             window.
356            
357             =item $ant->dest( $page, -xyz => [ $left, $top, $zoom ] )
358            
359             Display the page designated by page, with the coordinates (left, top) positioned
360             at the top-left corner of the window and the contents of the page magnified by
361             the factor zoom. A zero (0) value for any of the parameters left, top, or zoom
362             specifies that the current value of that parameter is to be retained unchanged.
363            
364             =item $ant->dest( $name )
365            
366             (PDF 1.2) Connect the Annotation to a "Named Destination" defined elswere.
367            
368             =cut
369            
370             sub dest
371             {
372 0     0 1   my ($self,$page,%opts)=@_;
373            
374 0 0         if(ref $page)
375             {
376 0 0         $opts{-xyz}=[undef,undef,undef] if(scalar(keys %opts)<1);
377            
378 0   0       $self->{A}||=PDFDict();
379            
380 0 0         if(defined $opts{-fit})
    0          
    0          
    0          
    0          
    0          
    0          
    0          
381             {
382 0           $self->{A}->{D}=PDFArray($page,PDFName('Fit'));
383             }
384             elsif(defined $opts{-fith})
385             {
386 0           $self->{A}->{D}=PDFArray($page,PDFName('FitH'),PDFNum($opts{-fith}));
387             }
388             elsif(defined $opts{-fitb})
389             {
390 0           $self->{A}->{D}=PDFArray($page,PDFName('FitB'));
391             }
392             elsif(defined $opts{-fitbh})
393             {
394 0           $self->{A}->{D}=PDFArray($page,PDFName('FitBH'),PDFNum($opts{-fitbh}));
395             }
396             elsif(defined $opts{-fitv})
397             {
398 0           $self->{A}->{D}=PDFArray($page,PDFName('FitV'),PDFNum($opts{-fitv}));
399             }
400             elsif(defined $opts{-fitbv})
401             {
402 0           $self->{A}->{D}=PDFArray($page,PDFName('FitBV'),PDFNum($opts{-fitbv}));
403             }
404             elsif(defined $opts{-fitr})
405             {
406 0 0         die "insufficient parameters to ->dest( page, -fitr => [] ) " unless(scalar @{$opts{-fitr}} == 4);
  0            
407 0           $self->{A}->{D}=PDFArray($page,PDFName('FitR'),map {PDFNum($_)} @{$opts{-fitr}});
  0            
  0            
408             }
409             elsif(defined $opts{-xyz})
410             {
411 0 0         die "insufficient parameters to ->dest( page, -xyz => [] ) " unless(scalar @{$opts{-xyz}} == 3);
  0            
412 0 0         $self->{A}->{D}=PDFArray($page,PDFName('XYZ'),map {defined $_ ? PDFNum($_) : PDFNull()} @{$opts{-xyz}});
  0            
  0            
413             }
414             }
415             else
416             {
417 0           $self->{Dest}=PDFStr($page);
418             }
419            
420 0           return($self);
421             }
422            
423             1;
424            
425             __END__