File: NamedDestination.pm

package info (click to toggle)
libpdf-api2-perl 2.047-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,228 kB
  • sloc: perl: 42,227; makefile: 11
file content (319 lines) | stat: -rw-r--r-- 9,094 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package PDF::API2::NamedDestination;

use base 'PDF::API2::Basic::PDF::Dict';

use strict;
use warnings;

our $VERSION = '2.047'; # VERSION

use Carp;
use Encode qw(:all);

use PDF::API2::Util;
use PDF::API2::Basic::PDF::Utils;

=head1 NAME

PDF::API2::NamedDestination - Add named destinations (views) to a PDF

=head1 METHODS

=head2 new

    $destination = PDF::API2::NamedDestination->new($pdf, ...);

Creates a new named destination object.  If any additional arguments are
present, they will be passed to C<destination()>.

=cut

sub new {
    my $class = shift();
    my $pdf = shift();
    $pdf = $pdf->{'pdf'} if $pdf->isa('PDF::API2');

    my $self = $class->SUPER::new($pdf);
    $pdf->new_obj($self);

    if (@_) {
        return $self->destination(@_);
    }

    return $self;
}

# Deprecated (warning added in 2.031)
sub new_api {
    my ($class, $api2) = @_;
    warnings::warnif('deprecated',
                     'Call to deprecated method new_api; replace with new');

    my $destination = $class->new($api2);
    return $destination;
}

=head2 destination

    $destination = $destination->destination($page, $location, @args);

A destination is a particular view of a PDF, consisting of a page object, the
location of the window on that page, and possible coordinate and zoom arguments.

    # The XYZ location takes three arguments
    my $dest1 = PDF::API2::NamedDestination->new($pdf);
    $dest->destination($pdf->open_page(1), 'xyz' => ($x, $y, $zoom));

    # The Fit location doesn't require any arguments
    my $dest2 = PDF::API2::NamedDestination->new($pdf);
    $dest->destination($pdf->open_page(2), 'fit');

The following locations are available:

=over

=item * xyz ($left, $top, $zoom)

Display the page with the coordinates (C<$left>, C<$top>) positioned at the
upper-left corner of the window and the contents of the page magnified by the
factor C<$zoom>. An C<undef> value for any of the arguments specifies that the
current value of that argument shall be retained unchanged.  A zoom factor of 0
has the same meaning as C<undef>.

=item * fit

Display the page with its contents magnified just enough to fit the entire page
within the window both horizontally and vertically. If the required horizontal
and vertical magnification factors are different, use the smaller of the two,
centering the page within the window in the other dimension.

=item * fith ($top)

Display the page with the vertical coordinate C<$top> positioned at the top edge
of the window and the contents of the page magnified just enough to fit the
entire width of the page within the window.  An C<undef> value for C<$top>
specifies that the current value of that argument shall be retained unchanged.

=item * fitv ($left)

Display the page with the horizontal coordinate C<$left> positioned at the left
edge of the window and the contents of the page magnified just enough to fit the
entire height of the page within the window.  An C<undef> value for C<$left>
specifies that the current value of that argument shall be retained unchanged.

=item * fitr ($left, $bottom, $right, $top)

Display the page with its contents magnified just enough to fit the rectangle
specified by the coordinates C<$left>, C<$bottom>, C<$right>, and C<$top>
entirely within the window both horizontally and vertically. If the required
horizontal and vertical magnification factors are different, use the smaller of
the two, centering the rectangle within the window in the other dimension.

=item * fitb

Display the page with its contents magnified just enough to fit its bounding box
entirely within the window both horizontally and vertically. If the required
horizontal and vertical magnification factors are different, use the smaller of
the two, centering the bounding box within the window in the other dimension.

=item * fitbh ($top)

Display the page with the vertical coordinate C<$top> positioned at the top edge
of the window and the contents of the page magnified just enough to fit the
entire width of its bounding box within the window.  An C<undef> value for
C<$top> specifies that the current value of that argument shall be retained
unchanged.

=item * fitbv ($left)

Display the page with the horizontal coordinate C<$left> positioned at the left
edge of the window and the contents of the page magnified just enough to fit the
entire height of its bounding box within the window.  An C<undef> value for
C<$left> specifies that the current value of that argument shall be retained
unchanged.

=back

=cut

sub _array {
    my $page = shift();
    my $location = shift();
    return PDFArray($page, PDFName($location),
                    map { defined($_) ? PDFNum($_) : PDFNull() } @_);
}

sub _destination {
    my ($page, $location, @args) = @_;
    return _array($page, 'XYZ', undef, undef, undef) unless $location;

    my %arg_counts = (
        xyz   => 3,
        fit   => 0,
        fith  => 1,
        fitv  => 1,
        fitr  => 4,
        fitb  => 0,
        fitbh => 1,
        fitbv => 1,
    );
    my $arg_count = $arg_counts{$location};
    croak "Invalid location $location" unless defined $arg_count;

    if ($arg_count == 0 and @args) {
        croak "$location doesn't take any arguments";
    }
    elsif ($arg_count == 1 and @args != 1) {
        croak "$location requires one argument";
    }
    elsif ($arg_count == 3 and @args != 3) {
        croak "$location requires three arguments";
    }
    elsif ($arg_count == 4 and @args != 4) {
        croak "$location requires four arguments";
    }

    return _array($page, 'XYZ', @args) if $location eq 'xyz';
    $location =~ s/^fit(.*)$/'Fit' . uc($1 or '')/e;
    return _array($page, $location, @args);
}

sub destination {
    my ($self, $page, $location, @args) = @_;
    $self->{'D'} = _destination($page, $location, @args);
    return $self;
}

# Deprecated; use destination instead, removing hyphen from location name
sub dest {
    my ($self, $page, %opts) = @_;

    if (ref($page)) {
        $opts{'-xyz'} = [undef, undef, undef] unless keys %opts;

        if (defined $opts{'-fit'}) {
            $self->{'D'} = _destination($page, 'fit');
        }
        elsif (defined $opts{'-fith'}) {
            $self->{'D'} = _destination($page, 'fith', $opts{'-fith'});
        }
        elsif (defined $opts{'-fitb'}) {
            $self->{'D'} = _destination($page, 'fitb');
        }
        elsif (defined $opts{'-fitbh'}) {
            $self->{'D'} = _destination($page, 'fitbh', $opts{'-fitbh'});
        }
        elsif (defined $opts{'-fitv'}) {
            $self->{'D'} = _destination($page, 'fitv', $opts{'-fitv'});
        }
        elsif (defined $opts{'-fitbv'}) {
            $self->{'D'} = _destination($page, 'fitbv', $opts{'-fitbv'});
        }
        elsif (defined $opts{'-fitr'}) {
            $self->{'D'} = _destination($page, 'fitr', @{$opts{'-fitr'}});
        }
        elsif (defined $opts{'-xyz'}) {
            $self->{'D'} = _destination($page, 'xyz', @{$opts{'-xyz'}});
        }
    }

    return $self;
}

=head2 goto

    $destination = $destination->goto($page, $location, @args);

A go-to action changes the view to a specified destination (page, location, and
magnification factor).

Parameters are as described in C<destination>.

=cut

sub goto {
    my $self = shift();
    $self->{'S'} = PDFName('GoTo');
    return $self->destination(@_);
}

# Deprecated; use goto instead, removing hyphen from location name
sub link {
    my $self = shift();
    $self->{'S'} = PDFName('GoTo');
    return $self->dest(@_);
}

=head2 uri

    $destination = $destination->uri($uri);

A URI action indicates that a URI -- typically a web page -- should be launched.

=cut

# Deprecated (renamed)
sub url { return uri(@_) }

sub uri {
    my ($self, $uri) = @_;

    $self->{'S'} = PDFName('URI');
    $self->{'URI'} = PDFStr($uri);

    return $self;
}

=head2 launch

    $destination = $destination->launch($file);

A launch action runs an application or opens or prints a document.

C<$file> contains the path to the application to be launched or the document to
be opened or printed.

=cut

# Deprecated (renamed)
sub file { return launch(@_) }

sub launch {
    my ($self, $file) = @_;

    $self->{'S'} = PDFName('Launch');
    $self->{'F'} = PDFStr($file);

    return $self;
}

=head2 pdf

    $destination = $destination->pdf($file, $page_number, $location, @args);

Similar to C<goto>, but the destination is in a different PDF file located at
C<$file>.  C<$page_number> is an integer rather than a page object, and the
other parameters are as described in C<destination>.

=cut

sub pdf {
    my ($self, $file, $page_number, $location, @args) = @_;

    $self->{'S'} = PDFName('GoToR');
    $self->{'F'} = PDFStr($file);

    return $self->destination(PDFNum($page_number), $location, @args);
}

# Deprecated; use pdf instead, removing hyphen from location name
sub pdfile {
    my ($self, $file, $page_number, @args) = @_;

    $self->{'S'} = PDFName('GoToR');
    $self->{'F'} = PDFStr($file);

    return $self->dest(PDFNum($page_number), @args);
}

1;