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 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
|
package PDF::API2::Outline;
use base 'PDF::API2::Basic::PDF::Dict';
use strict;
use warnings;
our $VERSION = '2.047'; # VERSION
use Carp qw(croak);
use PDF::API2::Basic::PDF::Utils;
use Scalar::Util qw(weaken);
=head1 NAME
PDF::API2::Outline - Manage PDF outlines (a.k.a. bookmarks)
=head1 SYNOPSIS
# Get/create the top-level outline tree
my $outline = $pdf->outline();
# Add an entry
my $item = $outline->outline();
$item->title('First Page');
$item->destination($pdf->open_page(1));
=head1 METHODS
=cut
sub new {
my ($class, $api, $parent, $prev) = @_;
my $self = $class->SUPER::new();
$self->{'Parent'} = $parent if defined $parent;
$self->{'Prev'} = $prev if defined $prev;
$self->{' api'} = $api;
weaken $self->{' api'};
weaken $self->{'Parent'};
return $self;
}
=head2 Examine the Outline Tree
=head3 has_children
my $boolean = $outline->has_children();
Return true if the current outline item has child items.
=cut
sub has_children {
my $self = shift();
# Opened by PDF::API2
return 1 if exists $self->{'First'};
# Created by PDF::API2
return @{$self->{' children'}} > 0 if exists $self->{' children'};
return;
}
=head3 count
my $integer = $outline->count();
Return the number of descendants that are visible when the current outline item
is open (expanded).
=cut
sub count {
my $self = shift();
# Set count to the number of descendant items that will be visible when the
# current item is open.
my $count = 0;
if ($self->has_children()) {
$self->_load_children() unless exists $self->{' children'};
$count += @{$self->{' children'}};
foreach my $child (@{$self->{' children'}}) {
next unless $child->has_children();
next unless $child->is_open();
$count += $child->count();
}
}
if ($count) {
$self->{'Count'} = PDFNum($self->is_open() ? $count : -$count);
}
return $count;
}
sub _load_children {
my $self = shift();
my $item = $self->{'First'};
return unless $item;
$item->realise();
bless $item, __PACKAGE__;
push @{$self->{' children'}}, $item;
while ($item->next()) {
$item = $item->next();
$item->realise();
bless $item, __PACKAGE__;
push @{$self->{' children'}}, $item;
}
return $self;
}
=head3 first
my $child = $outline->first();
Return the first child of the current outline level, if one exists.
=cut
sub first {
my $self = shift();
if (defined $self->{' children'} and defined $self->{' children'}->[0]) {
$self->{'First'} = $self->{' children'}->[0];
}
return $self->{'First'};
}
=head3 last
my $child = $outline->last();
Return the last child of the current outline level, if one exists.
=cut
sub last {
my $self = shift();
if (defined $self->{' children'} and defined $self->{' children'}->[-1]) {
$self->{'Last'} = $self->{' children'}->[-1];
}
return $self->{'Last'};
}
=head3 parent
my $parent = $outline->parent();
Return the parent of the current item, if not at the top level of the outline
tree.
=cut
sub parent {
my $self = shift();
$self->{'Parent'} = shift() if defined $_[0];
return $self->{'Parent'};
}
=head3 prev
my $sibling = $outline->prev();
Return the previous item of the current level of the outline tree.
=cut
sub prev {
my $self = shift();
$self->{'Prev'} = shift() if defined $_[0];
return $self->{'Prev'};
}
=head3 next
my $sibling = $outline->next();
Return the next item of the current level of the outline tree.
=cut
sub next {
my $self = shift();
$self->{'Next'} = shift() if defined $_[0];
return $self->{'Next'};
}
=head2 Modify the Outline Tree
=head3 outline
my $child = $outline->outline();
Add an outline item at the end of the current outline's list of children.
=cut
sub outline {
my $self = shift();
my $child = PDF::API2::Outline->new($self->{' api'}, $self);
$self->{' children'} //= [];
$child->prev($self->{' children'}->[-1]) if @{$self->{' children'}};
$self->{' children'}->[-1]->next($child) if @{$self->{' children'}};
push @{$self->{' children'}}, $child;
unless ($child->is_obj($self->{' api'}->{'pdf'})) {
$self->{' api'}->{'pdf'}->new_obj($child);
}
return $child;
}
=head3 insert_after
my $sibling = $outline->insert_after();
Add an outline item immediately following the current item.
=cut
sub insert_after {
my $self = shift();
my $sibling = PDF::API2::Outline->new($self->{' api'}, $self->parent());
$sibling->next($self->next());
$self->next->prev($sibling) if $self->next();
$self->next($sibling);
$sibling->prev($self);
unless ($sibling->is_obj($self->{' api'}->{'pdf'})) {
$self->{' api'}->{'pdf'}->new_obj($sibling);
}
$self->parent->_reset_children();
return $sibling;
}
=head3 insert_before
$sibling = $outline->insert_before();
Add an outline item immediately preceding the current item.
=cut
sub insert_before {
my $self = shift();
my $sibling = PDF::API2::Outline->new($self->{' api'}, $self->parent());
$sibling->prev($self->prev());
$self->prev->next($sibling) if $self->prev();
$self->prev($sibling);
$sibling->next($self);
unless ($sibling->is_obj($self->{' api'}->{'pdf'})) {
$self->{' api'}->{'pdf'}->new_obj($sibling);
}
$self->parent->_reset_children();
return $sibling;
}
sub _reset_children {
my $self = shift();
my $item = $self->first();
$self->{' children'} = [];
return unless $item;
push @{$self->{' children'}}, $item;
while ($item->next()) {
$item = $item->next();
push @{$self->{' children'}}, $item;
}
return $self;
}
=head3 delete
$outline->delete();
Remove the current outline item from the outline tree. If the item has any
children, they will effectively be deleted as well since they will no longer be
linked.
=cut
sub delete {
my $self = shift();
my $prev = $self->prev();
my $next = $self->next();
$prev->next($next) if defined $prev;
$next->prev($prev) if defined $next;
my $siblings = $self->parent->{' children'};
@$siblings = grep { $_ ne $self } @$siblings;
delete $self->parent->{' children'} unless $self->parent->has_children();
return;
}
=head3 is_open
# Get
my $boolean = $outline->is_open();
# Set
my $outline = $outline->is_open($boolean);
Get/set whether the outline is expanded or collapsed.
=cut
sub is_open {
my $self = shift();
# Get
unless (@_) {
# Created by PDF::API2
return $self->{' closed'} ? 0 : 1 if exists $self->{' closed'};
# Opened by PDF::API2
return $self->{'Count'}->val() > 0 if exists $self->{'Count'};
# Default
return 1;
}
# Set
my $is_open = shift();
$self->{' closed'} = (not $is_open);
return $self;
}
# Deprecated
sub open {
my $self = shift();
return $self->is_open(1);
}
# Deprecated
sub closed {
my $self = shift();
return $self->is_open(0);
}
=head2 Set Outline Attributes
=head3 title
# Get
my $title = $outline->title();
# Set
$outline = $outline->title($text);
Get/set the title of the outline item.
=cut
sub title {
my $self = shift();
# Get
unless (@_) {
return unless $self->{'Title'};
return $self->{'Title'}->val();
}
# Set
my $text = shift();
$self->{'Title'} = PDFStr($text);
return $self;
}
=head3 destination
$outline = $outline->destination($destination, $location, @args);
Set the destination page and optional position of the outline. C<$location> and
C<@args> are as defined in L<PDF::API2::NamedDestination/"destination">.
C<$destination> can optionally be the name of a named destination defined
elsewhere.
=cut
sub _destination {
require PDF::API2::NamedDestination;
return PDF::API2::NamedDestination::_destination(@_);
}
sub destination {
my ($self, $destination, $location, @args) = @_;
# Remove an existing action dictionary
delete $self->{'A'};
if (ref($destination)) {
# Page Destination
$self->{'Dest'} = _destination($destination, $location, @args);
}
else {
# Named Destination
$self->{'Dest'} = PDFStr($destination);
}
return $self;
}
# Deprecated: Use destination with the indicated changes
sub dest {
my ($self, $destination, $location, @args) = @_;
# Replace -fit => 1 or -fitb => 1 with just the location
if (defined $location) {
@args = () if $location eq '-fit' or $location eq '-fitb';
}
# Convert args from arrayref to array
@args = @{$args[0]} if @args and ref($args[0]) eq 'ARRAY';
# Remove hyphen prefix from location
$location =~ s/^-// if defined $location;
return $self->destination($destination, $location, @args);
}
=head3 uri
$outline = $outline->uri($uri);
Launch a URI -- typically a web page -- when the outline item is activated.
=cut
# Deprecated (renamed)
sub url { return uri(@_) }
sub uri {
my ($self, $uri) = @_;
delete $self->{'Dest'};
$self->{'A'} = PDFDict();
$self->{'A'}->{'S'} = PDFName('URI');
$self->{'A'}->{'URI'} = PDFStr($uri);
return $self;
}
=head3 launch
$outline->launch($file);
Launch an application or file when the outline item is activated.
=cut
# Deprecated (renamed)
sub file { return launch(@_) }
sub launch {
my ($self, $file) = @_;
delete $self->{'Dest'};
$self->{'A'} = PDFDict();
$self->{'A'}->{'S'} = PDFName('Launch');
$self->{'A'}->{'F'} = PDFStr($file);
return $self;
}
=head3 pdf
$outline = $outline->pdf($filename, $page_number, $location, @args);
Open another PDF file to a particular page number (first page is zero, which is
also the default). The page can optionally be positioned at a particular
location if C<$location> and C<@args> are set -- see
L<PDF::API2::NamedDestination/"destination"> for possible settings.
=cut
# Deprecated (renamed)
sub pdfile { return pdf_file(@_) }
# Deprecated; use pdf instead, with the indicated changes
sub pdf_file {
my ($self, $file, $page_number, $location, @args);
# Replace -fit => 1 or -fitb => 1 with just the location
if (defined $location) {
@args = () if $location eq '-fit' or $location eq '-fitb';
}
# Convert args from arrayref to array
@args = @{$args[0]} if @args and ref($args[0]) eq 'ARRAY';
# Remove hyphen prefix from location
$location =~ s/^-// if defined $location;
return $self->pdf($file, $page_number, $location, @args);
}
sub pdf {
my ($self, $file, $page_number, $location, @args) = @_;
$page_number //= 0;
delete $self->{'Dest'};
$self->{'A'} = PDFDict();
$self->{'A'}->{'S'} = PDFName('GoToR');
$self->{'A'}->{'F'} = PDFStr($file);
$self->{'A'}->{'D'} = _destination(PDFNum($page_number), $location, @args);
return $self;
}
sub fix_outline {
my $self = shift();
$self->first();
$self->last();
$self->count();
}
sub outobjdeep {
my $self = shift();
$self->fix_outline();
return $self->SUPER::outobjdeep(@_);
}
1;
|