File Coverage

blib/lib/PDF/Builder/NamedDestination.pm
Criterion Covered Total %
statement 18 65 27.6
branch 0 30 0.0
condition n/a
subroutine 6 12 50.0
pod 6 6 100.0
total 30 113 26.5


line stmt bran cond sub pod time code
1             package PDF::Builder::NamedDestination;
2              
3 34     34   280 use base 'PDF::Builder::Basic::PDF::Dict';
  34         83  
  34         3506  
4              
5 34     34   236 use strict;
  34         79  
  34         685  
6 34     34   174 use warnings;
  34         78  
  34         1897  
7             #no warnings qw[ recursion uninitialized ];
8              
9             our $VERSION = '3.023'; # VERSION
10             our $LAST_UPDATE = '3.021'; # manually update whenever code is changed
11              
12             # TBD: do -rect and -border apply to Named Destinations (link, url, file)?
13             # There is nothing to implement these options. Perhaps the code was copied
14             # from Annotations and never cleaned up? Disable mention of these options
15             # for now (in the POD). Only link handles the destination page fit option.
16              
17 34     34   250 use Encode qw(:all);
  34         80  
  34         10351  
18              
19 34     34   284 use PDF::Builder::Util;
  34         90  
  34         4726  
20 34     34   272 use PDF::Builder::Basic::PDF::Utils;
  34         94  
  34         27696  
21              
22             =head1 NAME
23              
24             PDF::Builder::NamedDestination - Add named destination shortcuts to a PDF
25              
26             =head1 METHODS
27              
28             =over
29              
30             =item $dest = PDF::Builder::NamedDestination->new($pdf)
31              
32             Returns a named destination object.
33              
34             =back
35              
36             =head2 Destination types
37              
38             =over
39              
40             =cut
41              
42             sub new {
43 0     0 1   my ($class, $pdf) = @_;
44              
45 0           my ($self);
46              
47 0 0         $class = ref $class if ref $class;
48 0           $self = $class->SUPER::new($pdf);
49              
50 0 0         $pdf->new_obj($self) unless $self->is_obj($pdf);
51              
52 0           return $self;
53             }
54              
55             # Note: new_api() removed in favor of new():
56             # new_api($api, ...) replace with new($api->{'pdf'}, ...)
57              
58             =item $dest->link($page, %opts)
59              
60             =item $dest->link($page)
61              
62             Defines the destination as launch-page with page C<$page> and
63             options %opts for target page fit.
64              
65             =cut
66              
67             sub link {
68 0     0 1   my ($self, $page, %opts) = @_;
69              
70 0           $self->{'S'} = PDFName('GoTo');
71 0           $self->dest($page, %opts);
72              
73 0           return $self;
74             }
75              
76             =item $dest->url($url, %opts)
77              
78             =item $dest->url($url)
79              
80             Defines the destination as launch-url with url C<$url> and
81             page-fit options %opts.
82              
83             =cut
84              
85             sub url {
86 0     0 1   my ($self, $url, %opts) = @_;
87              
88 0           $self->{'S'} = PDFName('URI');
89 0           $self->{'URI'} = PDFString($url, 'u');
90              
91 0           return $self;
92             }
93              
94             =item $dest->file($file, %opts)
95              
96             =item $dest->file($file)
97              
98             Defines the destination as launch-file with filepath C<$file> and
99             page-fit options %opts.
100              
101             =cut
102              
103             sub file {
104 0     0 1   my ($self, $url, %opts) = @_;
105              
106 0           $self->{'S'} = PDFName('Launch');
107 0           $self->{'F'} = PDFString($url, 'u');
108              
109 0           return $self;
110             }
111              
112             =item $dest->pdf_file($pdffile, $pagenum, %opts)
113              
114             =item $dest->pdf_file($pdffile, $pagenum)
115              
116             Defines the destination as a PDF-file with filepath C<$pdffile>, on page
117             C<$pagenum>, and options %opts (same as dest()).
118              
119             =cut
120              
121             sub pdf_file {
122 0     0 1   my ($self, $url, $pnum, %opts) = @_;
123              
124 0           $self->{'S'} = PDFName('GoToR');
125 0           $self->{'F'} = PDFString($url, 'u');
126              
127 0           $self->dest(PDFNum($pnum), %opts);
128              
129 0           return $self;
130             }
131              
132             =item $dest->dest($page, -fit => 1)
133              
134             Display the page designated by C<$page>, with its contents magnified just enough
135             to fit the entire page within the window both horizontally and vertically. If
136             the required horizontal and vertical magnification factors are different, use
137             the smaller of the two, centering the page within the window in the other
138             dimension.
139              
140             =item $dest->dest($page, -fith => $top)
141              
142             Display the page designated by C<$page>, with the vertical coordinate C<$top>
143             positioned at the top edge of the window and the contents of the page magnified
144             just enough to fit the entire width of the page within the window.
145              
146             =item $dest->dest($page, -fitv => $left)
147              
148             Display the page designated by C<$page>, with the horizontal coordinate C<$left>
149             positioned at the left edge of the window and the contents of the page magnified
150             just enough to fit the entire height of the page within the window.
151              
152             =item $dest->dest($page, -fitr => [$left, $bottom, $right, $top])
153              
154             Display the page designated by C<$page>, with its contents magnified just enough
155             to fit the rectangle specified by the coordinates C<$left>, C<$bottom>,
156             C<$right>, and C<$top> entirely within the window both horizontally and
157             vertically. If the required horizontal and vertical magnification factors are
158             different, use the smaller of the two, centering the rectangle within the window
159             in the other dimension.
160              
161             =item $dest->dest($page, -fitb => 1)
162              
163             Display the page designated by C<$page>, with its contents magnified
164             just enough to fit its bounding box entirely within the window both horizontally
165             and vertically. If the required horizontal and vertical magnification factors
166             are different, use the smaller of the two, centering the bounding box within the
167             window in the other dimension.
168              
169             =item $dest->dest($page, -fitbh => $top)
170              
171             Display the page designated by C<$page>, with the vertical coordinate
172             C<$top> positioned at the top edge of the window and the contents of the page
173             magnified just enough to fit the entire width of its bounding box within the
174             window.
175              
176             =item $dest->dest($page, -fitbv => $left)
177              
178             Display the page designated by C<$page>, with the horizontal
179             coordinate C<$left> positioned at the left edge of the window and the contents
180             of the page magnified just enough to fit the entire height of its bounding box
181             within the window.
182              
183             =item $dest->dest($page, -xyz => [$left, $top, $zoom])
184              
185             Display the page designated by page, with the coordinates C<[$left, $top]>
186             positioned at the top-left corner of the window and the contents of the page
187             magnified by the factor C<$zoom>. A zero (0) value for any of the parameters
188             C<$left>, C<$top>, or C<$zoom> specifies that the current value of that
189             parameter is to be retained unchanged.
190              
191             This is the B fit setting, with position (left and top) and zoom
192             the same as the calling page's.
193              
194             =cut
195              
196             sub dest {
197 0     0 1   my ($self, $page, %opts) = @_;
198              
199 0 0         if (ref $page) {
200 0 0         if (defined $opts{'-fit'}) {
    0          
    0          
    0          
    0          
    0          
    0          
    0          
201 0           $self->{'D'} = PDFArray($page, PDFName('Fit'));
202             } elsif (defined $opts{'-fith'}) {
203 0           $self->{'D'} = PDFArray($page, PDFName('FitH'), PDFNum($opts{'-fith'}));
204             } elsif (defined $opts{'-fitb'}) {
205 0           $self->{'D'} = PDFArray($page, PDFName('FitB'));
206             } elsif (defined $opts{'-fitbh'}) {
207 0           $self->{'D'} = PDFArray($page, PDFName('FitBH'), PDFNum($opts{'-fitbh'}));
208             } elsif (defined $opts{'-fitv'}) {
209 0           $self->{'D'} = PDFArray($page, PDFName('FitV'), PDFNum($opts{'-fitv'}));
210             } elsif (defined $opts{'-fitbv'}) {
211 0           $self->{'D'} = PDFArray($page, PDFName('FitBV'), PDFNum($opts{'-fitbv'}));
212             } elsif (defined $opts{'-fitr'}) {
213 0 0         die "Insufficient parameters to ->dest(page, -fitr => []) " unless scalar @{$opts{'-fitr'}} == 4;
  0            
214 0           $self->{'D'} = PDFArray($page, PDFName('FitR'), map {PDFNum($_)} @{$opts{'-fitr'}});
  0            
  0            
215             } elsif (defined $opts{'-xyz'}) {
216 0 0         die "Insufficient parameters to ->dest(page, -xyz => []) " unless scalar @{$opts{'-xyz'}} == 3;
  0            
217 0 0         $self->{'D'} = PDFArray($page, PDFName('XYZ'), map {defined $_ ? PDFNum($_) : PDFNull()} @{$opts{'-xyz'}});
  0            
  0            
218             } else {
219             # no "fit" option found. use default.
220 0           $opts{'-xyz'} = [undef,undef,undef];
221 0 0         $self->{'D'} = PDFArray($page, PDFName('XYZ'), map {defined $_ ? PDFNum($_) : PDFNull()} @{$opts{'-xyz'}});
  0            
  0            
222             }
223             }
224              
225 0           return $self;
226             }
227              
228             =back
229              
230             =cut
231              
232             1;