File: Annotation.pm

package info (click to toggle)
libpdf-api2-perl 0.41.00-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 20,308 kB
  • ctags: 1,214
  • sloc: perl: 265,979; makefile: 37
file content (466 lines) | stat: -rw-r--r-- 12,821 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#=======================================================================
#    ____  ____  _____              _    ____ ___   ____
#   |  _ \|  _ \|  ___|  _   _     / \  |  _ \_ _| |___ \
#   | |_) | | | | |_    (_) (_)   / _ \ | |_) | |    __) |
#   |  __/| |_| |  _|    _   _   / ___ \|  __/| |   / __/
#   |_|   |____/|_|     (_) (_) /_/   \_\_|  |___| |_____|
#
#   A Perl Module Chain to faciliate the Creation and Modification
#   of High-Quality "Portable Document Format (PDF)" Files.
#
#   Copyright 1999-2005 Alfred Reibenschuh <areibens@cpan.org>.
#
#=======================================================================
#
#   This library is free software; you can redistribute it and/or
#   modify it under the terms of the GNU Lesser General Public
#   License as published by the Free Software Foundation; either
#   version 2 of the License, or (at your option) any later version.
#
#   This library is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#   Lesser General Public License for more details.
#
#   You should have received a copy of the GNU Lesser General Public
#   License along with this library; if not, write to the
#   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
#   Boston, MA 02111-1307, USA.
#
#   $Id: Annotation.pm,v 1.18 2005/03/14 22:01:05 fredo Exp $
#
#=======================================================================
package PDF::API2::Annotation;

BEGIN 
{

    use strict;
    use vars qw(@ISA $VERSION);

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

    @ISA = qw(PDF::API2::Basic::PDF::Dict);

    ( $VERSION ) = '$Revision: 1.18 $' =~ /Revision: (\S+)\s/; # $Date: 2005/03/14 22:01:05 $

    use utf8;
    use Encode qw(:all);
}

no warnings qw[ deprecated recursion uninitialized ];

=head1 NAME

PDF::API2::Annotation - Annotation object for PDF::API2

=head1 SYNOPSIS

 $ant = PDF::API2::Annotation->new

Returns a annotation object (called from $page->annotation).

=cut

sub new 
{
    my ($class,%opts)=@_;
    my $self=$class->SUPER::new;
    $self->{Type}=PDFName('Annot');
    $self->{Border}=PDFArray(PDFNum(0),PDFNum(0),PDFNum(0));
    return($self);
}

sub outobjdeep 
{
    my ($self, @opts) = @_;
    foreach my $k (qw[ api apipdf apipage ]) 
    {
        $self->{" $k"}=undef;
        delete($self->{" $k"});
    }
    $self->SUPER::outobjdeep(@opts);
}

=item $ant->link $page, %opts

Defines the annotation as launch-page with page $page and
options %opts (-rect, -border or 'dest-options').

=cut

sub link 
{
    my ($self,$page,%opts)=@_;
    $self->{Subtype}=PDFName('Link');
    if(ref $page)
    {
        $self->{A}=PDFDict();
        $self->{A}->{S}=PDFName('GoTo');
    }
    $self->dest($page,%opts);
    $self->rect(@{$opts{-rect}}) if(defined $opts{-rect});
    $self->border(@{$opts{-border}}) if(defined $opts{-border});
    return($self);
}

=item $ant->url $url, %opts

Defines the annotation as launch-url with url $url and
options %opts (-rect and/or -border).

=cut

sub url 
{
    my ($self,$url,%opts)=@_;
    $self->{Subtype}=PDFName('Link');
    $self->{A}=PDFDict();
    $self->{A}->{S}=PDFName('URI');
    if(is_utf8($url)) 
    {
        # URI must be 7-bit ascii
        utf8::downgrade($url);
    }
    $self->{A}->{URI}=PDFStr($url);
    # this will come again -- since the utf8 urls are coming !
    # -- fredo
    #if(is_utf8($url) || utf8::valid($url)) {
    #    $self->{A}->{URI}=PDFUtf($url);
    #} else {
    #    $self->{A}->{URI}=PDFStr($url);
    #}
    $self->rect(@{$opts{-rect}}) if(defined $opts{-rect});
    $self->border(@{$opts{-border}}) if(defined $opts{-border});
    return($self);
}

=item $ant->file $file, %opts

Defines the annotation as launch-file with filepath $file and
options %opts (-rect and/or -border).

=cut

sub file 
{
    my ($self,$url,%opts)=@_;
    $self->{Subtype}=PDFName('Link');
    $self->{A}=PDFDict();
    $self->{A}->{S}=PDFName('Launch');
    if(is_utf8($url)) 
    {
        # URI must be 7-bit ascii
        utf8::downgrade($url);
    }
    $self->{A}->{F}=PDFStr($url);
    # this will come again -- since the utf8 urls are coming !
    # -- fredo
    #if(is_utf8($url) || utf8::valid($url)) {
    #    $self->{A}->{F}=PDFUtf($url);
    #} else {
    #    $self->{A}->{F}=PDFStr($url);
    #}
    $self->rect(@{$opts{-rect}}) if(defined $opts{-rect});
    $self->border(@{$opts{-border}}) if(defined $opts{-border});
    return($self);
}

=item $ant->pdfile $pdfile, $pagenum, %opts

Defines the annotation as pdf-file with filepath $pdfile, $pagenum
and options %opts (same as dest).

=cut

sub pdfile 
{
    my ($self,$url,$pnum,%opts)=@_;
    $self->{Subtype}=PDFName('Link');
    $self->{A}=PDFDict();
    $self->{A}->{S}=PDFName('GoToR');
    if(is_utf8($url)) 
    {
        # URI must be 7-bit ascii
        utf8::downgrade($url);
    }
    $self->{A}->{F}=PDFStr($url);
    # this will come again -- since the utf8 urls are coming !
    # -- fredo
    #if(is_utf8($url) || utf8::valid($url)) {
    #    $self->{A}->{F}=PDFUtf($url);
    #} else {
    #    $self->{A}->{F}=PDFStr($url);
    #}

    $self->dest(PDFNum($pnum),%opts);

    $self->rect(@{$opts{-rect}}) if(defined $opts{-rect});
    $self->border(@{$opts{-border}}) if(defined $opts{-border});

    return($self);
}

=item $ant->text $text, %opts

Defines the annotation as textnote with content $text and
options %opts (-rect and/or -open).

=cut

sub text 
{
    my ($self,$text,%opts)=@_;
    $self->{Subtype}=PDFName('Text');
    $self->content($text);
    $self->rect(@{$opts{-rect}}) if(defined $opts{-rect});
    $self->open($opts{-open}) if(defined $opts{-open});
    return($self);
}

=item $ant->rect $llx, $lly, $urx, $ury

Sets the rectangle of the annotation.

=cut

sub rect 
{
    my ($self,@r)=@_;
    die "insufficient parameters to annotation->rect( ) " unless(scalar @r == 4);
    $self->{Rect}=PDFArray( map { PDFNum($_) } $r[0],$r[1],$r[2],$r[3], );
    return($self);
}

=item $ant->border @b

Sets the border-styles of the annotation, if applicable.

=cut

sub border 
{
    my ($self,@r)=@_;
    die "insufficient parameters to annotation->border( ) " unless(scalar @r == 3);
    $self->{Border}=PDFArray( map { PDFNum($_) } $r[0],$r[1],$r[2] );
    return($self);
}

=item $ant->content @lines

Sets the text-content of the annotation, if applicable.

=cut

sub content 
{
    my ($self,@t)=@_;
    my $t=join("\n",@t);
    if(is_utf8($t) || utf8::valid($t)) 
    {
        $self->{Content}=PDFUtf($t);
    } 
    else 
    {
        $self->{Content}=PDFStr($t);
    }
    return($self);
}

sub name 
{
    my ($self,$n)=@_;
    $self->{Name}=PDFName($n);
    return($self);
}

=item $ant->open $bool

Display the annotation either open or closed, if applicable.

=cut

sub open 
{
    my ($self,$n)=@_;
    $self->{Open}=PDFBool( $n ? 1 : 0 );
    return($self);
}

=item $ant->dest( $page, -fit => 1 )

Display the page designated by 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 $ant->dest( $page, -fith => $top )

Display the page designated by page, with the vertical coordinate 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.

=item $ant->dest( $page, -fitv => $left )

Display the page designated by page, with the horizontal coordinate 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.

=item $ant->dest( $page, -fitr => [ $left, $bottom, $right, $top ] )

Display the page designated by page, with its contents magnified just enough to
fit the rectangle specified by the coordinates left, bottom, right, and 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 $ant->dest( $page, -fitb => 1 )

(PDF 1.1) Display the page designated by 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 $ant->dest( $page, -fitbh => $top )

(PDF 1.1) Display the page designated by page, with the vertical coordinate 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.

=item $ant->dest( $page, -fitbv => $left )

(PDF 1.1) Display the page designated by page, with the horizontal coordinate
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.

=item $ant->dest( $page, -xyz => [ $left, $top, $zoom ] )

Display the page designated by page, with the coordinates (left, top) positioned
at the top-left corner of the window and the contents of the page magnified by
the factor zoom. A zero (0) value for any of the parameters left, top, or zoom
specifies that the current value of that parameter is to be retained unchanged.

=item $ant->dest( $name )

(PDF 1.2) Connect the Annotation to a "Named Destination" defined elswere.

=cut

sub dest 
{
    my ($self,$page,%opts)=@_;

    if(ref $page)
    {
        $opts{-xyz}=[undef,undef,undef] if(scalar(keys %opts)<1);

        $self->{A}||=PDFDict();

        if(defined $opts{-fit}) 
        {
            $self->{A}->{D}=PDFArray($page,PDFName('Fit'));
        } 
        elsif(defined $opts{-fith}) 
        {
            $self->{A}->{D}=PDFArray($page,PDFName('FitH'),PDFNum($opts{-fith}));
        } 
        elsif(defined $opts{-fitb}) 
        {
            $self->{A}->{D}=PDFArray($page,PDFName('FitB'));
        } 
        elsif(defined $opts{-fitbh}) 
        {
            $self->{A}->{D}=PDFArray($page,PDFName('FitBH'),PDFNum($opts{-fitbh}));
        } 
        elsif(defined $opts{-fitv}) 
        {
            $self->{A}->{D}=PDFArray($page,PDFName('FitV'),PDFNum($opts{-fitv}));
        } 
        elsif(defined $opts{-fitbv}) 
        {
            $self->{A}->{D}=PDFArray($page,PDFName('FitBV'),PDFNum($opts{-fitbv}));
        } 
        elsif(defined $opts{-fitr}) 
        {
            die "insufficient parameters to ->dest( page, -fitr => [] ) " unless(scalar @{$opts{-fitr}} == 4);
            $self->{A}->{D}=PDFArray($page,PDFName('FitR'),map {PDFNum($_)} @{$opts{-fitr}});
        } 
        elsif(defined $opts{-xyz}) 
        {
            die "insufficient parameters to ->dest( page, -xyz => [] ) " unless(scalar @{$opts{-xyz}} == 3);
            $self->{A}->{D}=PDFArray($page,PDFName('XYZ'),map {defined $_ ? PDFNum($_) : PDFNull()} @{$opts{-xyz}});
        }
    }
    else
    {
        $self->{Dest}=PDFStr($page);
    }

    return($self);
}

1;

__END__

=head1 AUTHOR

alfred reibenschuh

=head1 HISTORY

    $Log: Annotation.pm,v $
    Revision 1.18  2005/03/14 22:01:05  fredo
    upd 2005

    Revision 1.17  2005/01/03 05:00:27  fredo
    changed default behavior of border

    Revision 1.16  2005/01/03 04:17:24  fredo
    fixed link bug

    Revision 1.15  2005/01/03 03:29:31  fredo
    removed code duplication

    Revision 1.14  2005/01/03 00:30:31  fredo
    added named destination support

    Revision 1.13  2004/12/16 00:30:51  fredo
    added no warn for recursion

    Revision 1.12  2004/10/13 18:30:34  fredo
    fixed pdfile method from utf8 back to ascii

    Revision 1.11  2004/10/11 07:54:24  fredo
    fixed file method from utf8 back to ascii

    Revision 1.10  2004/10/01 01:20:35  fredo
    fixed url link annotation to 7-bit ascii as per pdf-spec-1.5

    Revision 1.9  2004/06/15 09:11:37  fredo
    removed cr+lf

    Revision 1.8  2004/06/07 19:43:58  fredo
    cleaned out cr+lf for lf

    Revision 1.7  2004/02/22 23:55:49  fredo
    full utf8 awareness

    Revision 1.6  2004/02/05 13:33:18  fredo
    added unicode handling to strings

    Revision 1.5  2003/12/08 13:05:18  Administrator
    corrected to proper licencing statement

    Revision 1.4  2003/12/08 11:55:17  Administrator
    change for proper module versioning

    Revision 1.3  2003/11/30 17:08:11  Administrator
    merged into default


=cut