File Coverage

blib/lib/PDF/API2/NamedDestination.pm
Criterion Covered Total %
statement 45 93 48.3
branch 11 40 27.5
condition 5 14 35.7
subroutine 11 21 52.3
pod 6 12 50.0
total 78 180 43.3


line stmt bran cond sub pod time code
1             package PDF::API2::NamedDestination;
2              
3 38     38   299 use base 'PDF::API2::Basic::PDF::Dict';
  38         116  
  38         3865  
4              
5 38     38   266 use strict;
  38         84  
  38         765  
6 38     38   199 use warnings;
  38         74  
  38         1468  
7              
8             our $VERSION = '2.044'; # VERSION
9              
10 38     38   235 use Carp;
  38         121  
  38         2449  
11 38     38   297 use Encode qw(:all);
  38         124  
  38         11434  
12              
13 38     38   323 use PDF::API2::Util;
  38         93  
  38         4943  
14 38     38   368 use PDF::API2::Basic::PDF::Utils;
  38         86  
  38         49206  
15              
16             =head1 NAME
17              
18             PDF::API2::NamedDestination - Add named destinations (views) to a PDF
19              
20             =head1 METHODS
21              
22             =head2 new
23              
24             $destination = PDF::API2::NamedDestination->new($pdf, ...);
25              
26             Creates a new named destination object. If any additional arguments are
27             present, they will be passed to C.
28              
29             =cut
30              
31             sub new {
32 1     1 1 12 my $class = shift();
33 1         2 my $pdf = shift();
34 1 50       10 $pdf = $pdf->{'pdf'} if $pdf->isa('PDF::API2');
35              
36 1         9 my $self = $class->SUPER::new($pdf);
37 1         5 $pdf->new_obj($self);
38              
39 1 50       4 if (@_) {
40 1         6 return $self->destination(@_);
41             }
42              
43 0         0 return $self;
44             }
45              
46             # Deprecated (warning added in 2.031)
47             sub new_api {
48 0     0 0 0 my ($class, $api2) = @_;
49 0         0 warnings::warnif('deprecated',
50             'Call to deprecated method new_api; replace with new');
51              
52 0         0 my $destination = $class->new($api2);
53 0         0 return $destination;
54             }
55              
56             =head2 destination
57              
58             $destination = $destination->destination($page, $location, @args);
59              
60             A destination is a particular view of a PDF, consisting of a page object, the
61             location of the window on that page, and possible coordinate and zoom arguments.
62              
63             # The XYZ location takes three arguments
64             my $dest1 = PDF::API2::NamedDestination->new($pdf);
65             $dest->destination($pdf->open_page(1), 'xyz' => ($x, $y, $zoom));
66              
67             # The Fit location doesn't require any arguments
68             my $dest2 = PDF::API2::NamedDestination->new($pdf);
69             $dest->destination($pdf->open_page(2), 'fit');
70              
71             The following locations are available:
72              
73             =over
74              
75             =item * xyz ($left, $top, $zoom)
76              
77             Display the page with the coordinates (C<$left>, C<$top>) positioned at the
78             upper-left corner of the window and the contents of the page magnified by the
79             factor C<$zoom>. An C value for any of the arguments specifies that the
80             current value of that argument shall be retained unchanged. A zoom factor of 0
81             has the same meaning as C.
82              
83             =item * fit
84              
85             Display the page with its contents magnified just enough to fit the entire page
86             within the window both horizontally and vertically. If the required horizontal
87             and vertical magnification factors are different, use the smaller of the two,
88             centering the page within the window in the other dimension.
89              
90             =item * fith ($top)
91              
92             Display the page with the vertical coordinate C<$top> positioned at the top edge
93             of the window and the contents of the page magnified just enough to fit the
94             entire width of the page within the window. An C value for C<$top>
95             specifies that the current value of that argument shall be retained unchanged.
96              
97             =item * fitv ($left)
98              
99             Display the page with the horizontal coordinate C<$left> positioned at the left
100             edge of the window and the contents of the page magnified just enough to fit the
101             entire height of the page within the window. An C value for C<$left>
102             specifies that the current value of that argument shall be retained unchanged.
103              
104             =item * fitr ($left, $bottom, $right, $top)
105              
106             Display the page with its contents magnified just enough to fit the rectangle
107             specified by the coordinates C<$left>, C<$bottom>, C<$right>, and C<$top>
108             entirely within the window both horizontally and vertically. If the required
109             horizontal and vertical magnification factors are different, use the smaller of
110             the two, centering the rectangle within the window in the other dimension.
111              
112             =item * fitb
113              
114             Display the page with its contents magnified just enough to fit its bounding box
115             entirely within the window both horizontally and vertically. If the required
116             horizontal and vertical magnification factors are different, use the smaller of
117             the two, centering the bounding box within the window in the other dimension.
118              
119             =item * fitbh ($top)
120              
121             Display the page with the vertical coordinate C<$top> positioned at the top edge
122             of the window and the contents of the page magnified just enough to fit the
123             entire width of its bounding box within the window. An C value for
124             C<$top> specifies that the current value of that argument shall be retained
125             unchanged.
126              
127             =item * fitbv ($left)
128              
129             Display the page with the horizontal coordinate C<$left> positioned at the left
130             edge of the window and the contents of the page magnified just enough to fit the
131             entire height of its bounding box within the window. An C value for
132             C<$left> specifies that the current value of that argument shall be retained
133             unchanged.
134              
135             =back
136              
137             =cut
138              
139             sub _array {
140 7     7   16 my $page = shift();
141 7         14 my $location = shift();
142             return PDFArray($page, PDFName($location),
143 7 50       24 map { defined($_) ? PDFNum($_) : PDFNull() } @_);
  12         35  
144             }
145              
146             sub _destination {
147 7     7   20 my ($page, $location, @args) = @_;
148 7 100       27 return _array($page, 'XYZ', undef, undef, undef) unless $location;
149              
150 3         54 my %arg_counts = (
151             xyz => 3,
152             fit => 0,
153             fith => 1,
154             fitv => 1,
155             fitr => 4,
156             fitb => 0,
157             fitbh => 1,
158             fitbv => 1,
159             );
160 3         13 my $arg_count = $arg_counts{$location};
161 3 50       10 croak "Invalid location $location" unless defined $arg_count;
162              
163 3 50 33     48 if ($arg_count == 0 and @args) {
    50 33        
    50 33        
    50 33        
164 0         0 croak "$location doesn't take any arguments";
165             }
166             elsif ($arg_count == 1 and @args != 1) {
167 0         0 croak "$location requires one argument";
168             }
169             elsif ($arg_count == 3 and @args != 3) {
170 0         0 croak "$location requires three arguments";
171             }
172             elsif ($arg_count == 4 and @args != 4) {
173 0         0 croak "$location requires four arguments";
174             }
175              
176 3 50       17 return _array($page, 'XYZ', @args) if $location eq 'xyz';
177 3   50     32 $location =~ s/^fit(.*)$/'Fit' . uc($1 or '')/e;
  3         25  
178 3         17 return _array($page, $location, @args);
179             }
180              
181             sub destination {
182 1     1 1 5 my ($self, $page, $location, @args) = @_;
183 1         5 $self->{'D'} = _destination($page, $location, @args);
184 1         4 return $self;
185             }
186              
187             # Deprecated; use destination instead, removing hyphen from location name
188             sub dest {
189 0     0 0   my ($self, $page, %opts) = @_;
190              
191 0 0         if (ref($page)) {
192 0 0         $opts{'-xyz'} = [undef, undef, undef] unless keys %opts;
193              
194 0 0         if (defined $opts{'-fit'}) {
    0          
    0          
    0          
    0          
    0          
    0          
    0          
195 0           $self->{'D'} = _destination($page, 'fit');
196             }
197             elsif (defined $opts{'-fith'}) {
198 0           $self->{'D'} = _destination($page, 'fith', $opts{'-fith'});
199             }
200             elsif (defined $opts{'-fitb'}) {
201 0           $self->{'D'} = _destination($page, 'fitb');
202             }
203             elsif (defined $opts{'-fitbh'}) {
204 0           $self->{'D'} = _destination($page, 'fitbh', $opts{'-fitbh'});
205             }
206             elsif (defined $opts{'-fitv'}) {
207 0           $self->{'D'} = _destination($page, 'fitv', $opts{'-fitv'});
208             }
209             elsif (defined $opts{'-fitbv'}) {
210 0           $self->{'D'} = _destination($page, 'fitbv', $opts{'-fitbv'});
211             }
212             elsif (defined $opts{'-fitr'}) {
213 0           $self->{'D'} = _destination($page, 'fitr', @{$opts{'-fitr'}});
  0            
214             }
215             elsif (defined $opts{'-xyz'}) {
216 0           $self->{'D'} = _destination($page, 'xyz', @{$opts{'-xyz'}});
  0            
217             }
218             }
219              
220 0           return $self;
221             }
222              
223             =head2 goto
224              
225             $destination = $destination->goto($page, $location, @args);
226              
227             A go-to action changes the view to a specified destination (page, location, and
228             magnification factor).
229              
230             Parameters are as described in C.
231              
232             =cut
233              
234             sub goto {
235 0     0 1   my $self = shift();
236 0           $self->{'S'} = PDFName('GoTo');
237 0           return $self->destination(@_);
238             }
239              
240             # Deprecated; use goto instead, removing hyphen from location name
241             sub link {
242 0     0 0   my $self = shift();
243 0           $self->{'S'} = PDFName('GoTo');
244 0           return $self->dest(@_);
245             }
246              
247             =head2 uri
248              
249             $destination = $destination->uri($uri);
250              
251             A URI action indicates that a URI -- typically a web page -- should be launched.
252              
253             =cut
254              
255             # Deprecated (renamed)
256 0     0 0   sub url { return uri(@_) }
257              
258             sub uri {
259 0     0 1   my ($self, $uri) = @_;
260              
261 0           $self->{'S'} = PDFName('URI');
262 0           $self->{'URI'} = PDFStr($uri);
263              
264 0           return $self;
265             }
266              
267             =head2 launch
268              
269             $destination = $destination->launch($file);
270              
271             A launch action runs an application or opens or prints a document.
272              
273             C<$file> contains the path to the application to be launched or the document to
274             be opened or printed.
275              
276             =cut
277              
278             # Deprecated (renamed)
279 0     0 0   sub file { return launch(@_) }
280              
281             sub launch {
282 0     0 1   my ($self, $file) = @_;
283              
284 0           $self->{'S'} = PDFName('Launch');
285 0           $self->{'F'} = PDFStr($file);
286              
287 0           return $self;
288             }
289              
290             =head2 pdf
291              
292             $destination = $destination->pdf($file, $page_number, $location, @args);
293              
294             Similar to C, but the destination is in a different PDF file located at
295             C<$file>. C<$page_number> is an integer rather than a page object, and the
296             other parameters are as described in C.
297              
298             =cut
299              
300             sub pdf {
301 0     0 1   my ($self, $file, $page_number, $location, @args) = @_;
302              
303 0           $self->{'S'} = PDFName('GoToR');
304 0           $self->{'F'} = PDFStr($file);
305              
306 0           return $self->destination(PDFNum($page_number), $location, @args);
307             }
308              
309             # Deprecated; use pdf instead, removing hyphen from location name
310             sub pdfile {
311 0     0 0   my ($self, $file, $page_number, @args) = @_;
312              
313 0           $self->{'S'} = PDFName('GoToR');
314 0           $self->{'F'} = PDFStr($file);
315              
316 0           return $self->dest(PDFNum($page_number), @args);
317             }
318              
319             1;