📄 api2.pm
字号:
$self->{pdf}->new_obj($obj) unless($obj->is_obj($self->{pdf}));
$obj->{' apiname'}=$key;
$obj->{' apipdf'}=$self->{pdf};
$obj->{' api'}=$self;
$self->resource('XObject',$key,$obj,1);
$self->{pdf}->out_obj($self->{pages});
return($obj);
}
sub extgstate {
my ($self)=@_;
my $key='XTGSx'.pdfkey('extgstate'.time().rand(0x7fffff));
my $obj=PDF::API2::ExtGState->new($self->{pdf},$key);
$self->{pdf}->new_obj($obj) unless($obj->is_obj($self->{pdf}));
$self->resource('ExtGState',$key,$obj,1);
$obj->{' api'}=$self;
return($obj);
}
=item $otls = $pdf->outlines
Returns a new or existing outlines object.
=cut
sub outlines {
my ($self)=@_;
$self->{pdf}->{Root}->{Outlines}=$self->{pdf}->{Root}->{Outlines}
|| PDF::API2::Outlines->new($self);
my $obj=$self->{pdf}->{Root}->{Outlines};
$self->{pdf}->new_obj($obj) if(!$obj->is_obj($self->{pdf}));
return($obj);
}
=item $page->resource $type, $key, $obj, $force
Adds a resource to the global page-inheritance tree.
B<Example:>
$pdf->resource('Font',$fontkey,$fontobj);
$pdf->resource('XObject',$imagekey,$imageobj);
$pdf->resource('Shading',$shadekey,$shadeobj);
$pdf->resource('ColorSpace',$spacekey,$speceobj);
B<Note:> You only have to add the required resources, if
they are NOT handled by the *font*, *image*, *shade* or *space*
methods.
=cut
sub resource {
my ($self, $type, $key, $obj, $force) = @_;
$self->{pages}->{Resources}= $self->{pages}->{Resources} || PDFDict();
my $dict=$self->{pages}->{Resources};
$dict->realise if(ref($dict)=~/Objind$/);
$dict->{$type}=$dict->{$type} || PDFDict();
$dict->{$type}->realise if(ref($dict->{$type})=~/Objind$/);
if($force) {
$dict->{$type}->{$key}=$obj;
} else {
$dict->{$type}->{$key}=$dict->{$type}->{$key} || $obj;
}
$self->{pdf}->out_obj($dict)
if($dict->is_obj($self->{pdf}));
$self->{pdf}->out_obj($dict->{$type})
if($dict->{$type}->is_obj($self->{pdf}));
$self->{pdf}->out_obj($obj)
if($obj->is_obj($self->{pdf}));
$self->{pdf}->out_obj($self->{pages});
return($dict);
}
#==================================================================
# PDF::API2::Pattern
#==================================================================
package PDF::API2::Pattern;
use strict;
use vars qw(@ISA);
@ISA = qw(Text::PDF::Dict);
use Text::PDF::Utils;
use Text::PDF::Dict;
use PDF::API2::Util;
=head2 PDF::API2::Pattern
Subclassed from Text::PDF::Dict.
=item $otls = PDF::API2::Pattern->new
Returns a new pattern object (called from $pdf->pattern).
=cut
sub new {
my ($class,%opts)=@_;
my $self = $class->SUPER::new;
my $key='PTx'.pdfkey(%opts || 'pattern'.localtime() );
$self->{' apiname'}=$key;
$self->{Type}=PDFName('Pattern');
return($self);
}
#==================================================================
# PDF::API2::Outlines
#==================================================================
package PDF::API2::Outlines;
use strict;
use vars qw(@ISA);
@ISA = qw(PDF::API2::Outline);
use Text::PDF::Utils;
use PDF::API2::Util;
=head2 PDF::API2::Outlines
Subclassed from PDF::API2::Outline.
=item $otls = PDF::API2::Outlines->new $api
Returns a new outlines object (called from $pdf->outlines).
=cut
sub new {
my ($class,$api)=@_;
my $self = $class->SUPER::new($api);
$self->{Type}=PDFName('Outlines');
return($self);
}
#==================================================================
# PDF::API2::Outline
#==================================================================
package PDF::API2::Outline;
use strict;
use vars qw(@ISA);
@ISA = qw(Text::PDF::Dict);
use Text::PDF::Dict;
use Text::PDF::Utils;
use PDF::API2::Util;
=head2 PDF::API2::Outline
Subclassed from Text::PDF::Dict.
=over 4
=item $otl = PDF::API2::Outline->new $api,$parent,$prev
Returns a new outline object (called from $otls->outline).
=cut
sub new {
my ($class,$api,$parent,$prev)=@_;
my $self = $class->SUPER::new;
$self->{' apipdf'}=$api->{pdf};
$self->{' api'}=$api;
$self->{Parent}=$parent if(defined $parent);
$self->{Prev}=$prev if(defined $prev);
return($self);
}
sub parent {
my $self=shift @_;
if(defined $_[0]) {
$self->{Parent}=shift @_;
}
return $self->{Parent};
}
sub prev {
my $self=shift @_;
if(defined $_[0]) {
$self->{Prev}=shift @_;
}
return $self->{Prev};
}
sub next {
my $self=shift @_;
if(defined $_[0]) {
$self->{Next}=shift @_;
}
return $self->{Next};
}
sub first {
my $self=shift @_;
$self->{First}=$self->{' childs'}->[0] if(defined $self->{' childs'});
return $self->{First} ;
}
sub last {
my $self=shift @_;
$self->{Last}=$self->{' childs'}->[-1] if(defined $self->{' childs'});
return $self->{Last};
}
sub count {
my $self=shift @_;
my $cnt=scalar @{$self->{' childs'}||[]};
map { $cnt+=$_->count();} @{$self->{' childs'}};
$self->{Count}=PDFNum($cnt) if($cnt>0);
return $cnt;
}
sub fix_outline {
my ($self)=@_;
$self->first;
$self->last;
$self->count;
}
=item $otl->title $text
Set the title of the outline.
=cut
sub title {
my ($self,$txt)=@_;
$self->{Title}=PDFStr($txt);
return($self);
}
=item $sotl=$otl->outline
Returns a new sub-outline.
=cut
sub outline {
my $self=shift @_;
my $obj=PDF::API2::Outline->new($self->{' api'},$self);
$obj->prev($self->{' childs'}->[-1]) if(defined $self->{' childs'});
$self->{' childs'}->[-1]->next($obj) if(defined $self->{' childs'});
push(@{$self->{' childs'}},$obj);
$self->{' api'}->{pdf}->new_obj($obj) if(!$obj->is_obj($self->{' api'}->{pdf}));
return $obj;
}
=item $otl->dest $pageobj [, %opts]
Sets the destination page of the outline.
=item $otl->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 $otl->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 $otl->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 $otl->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 $otl->dest( $page, -fitb => 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 $otl->dest( $page, -fitbh => $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 its bounding box within the window.
=item $otl->dest( $page, -fitbv => $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 its bounding box within the
window.
=item $otl->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.
=cut
sub dest {
my ($self,$page,%opts)=@_;
die "no valid page '$page' specified." if(!ref($page));
$opts{-fit}=1 if(scalar(keys %opts)<1);
if(defined $opts{-fit}) {
$self->{Dest}=PDFArray($page,PDFName('Fit'));
} elsif(defined $opts{-fith}) {
$self->{Dest}=PDFArray($page,PDFName('FitH'),PDFNum($opts{-fith}));
} elsif(defined $opts{-fitb}) {
$self->{Dest}=PDFArray($page,PDFName('FitB'));
} elsif(defined $opts{-fitbh}) {
$self->{Dest}=PDFArray($page,PDFName('FitBH'),PDFNum($opts{-fitbh}));
} elsif(defined $opts{-fitv}) {
$self->{Dest}=PDFArray($page,PDFName('FitV'),PDFNum($opts{-fitv}));
} elsif(defined $opts{-fitbv}) {
$self->{Dest}=PDFArray($page,PDFName('FitBV'),PDFNum($opts{-fitbv}));
} elsif(defined $opts{-fitr}) {
die "insufficient parameters to ->dest( page, -fitr => [] ) " unless(scalar @{$opts{-fitr}} == 4);
$self->{Dest}=PDFArray($page,PDFName('FitR'),map {PDFNum($_)} @{$opts{-fitr}});
} elsif(defined $opts{-xyz}) {
die "insufficient parameters to ->dest( page, -xyz => [] ) " unless(scalar @{$opts{-fitr}} == 3);
$self->{Dest}=PDFArray($page,PDFName('XYZ'),map {PDFNum($_)} @{$opts{-xyz}});
}
return($self);
}
sub out_obj {
my ($self,@param)=@_;
$self->fix_outline;
return $self->SUPER::out_obj(@param);
}
sub outobjdeep {
my ($self,@param)=@_;
$self->fix_outline;
return $self->SUPER::outobjdeep(@param);
}
#==================================================================
# PDF::API2::ColorSpace
#==================================================================
package PDF::API2::ColorSpace;
use strict;
use vars qw(@ISA);
@ISA = qw(Text::PDF::Array);
use Text::PDF::Utils;
use PDF::API2::Util;
use Math::Trig;
=back
=head2 PDF::API2::ColorSpace
Subclassed from Text::PDF::Array.
=item $cs = PDF::API2::ColorSpace->new $pdf, $key, %parameters
Returns a new colorspace object (called from $pdf->colorspace).
=cut
sub new {
my ($class,$pdf,$key,%opts)=@_;
my $self = $class->SUPER::new();
$self->{' apiname'}=$key;
# $self->{' apipdf'}=$pdf;
if($opts{-type} eq 'CalRGB') {
my $csd=PDFDict();
$opts{-whitepoint}=$opts{-whitepoint} || [ 0.95049, 1, 1.08897 ];
$opts{-blackpoint}=$opts{-blackpoint} || [ 0, 0, 0 ];
$opts{-gamma}=$opts{-gamma} || [ 2.22218, 2.22218, 2.22218 ];
$opts{-matrix}=$opts{-matrix} || [
0.41238, 0.21259, 0.01929,
0.35757, 0.71519, 0.11919,
0.1805, 0.07217, 0.95049
];
$csd->{WhitePoint}=PDFArray(map {PDFNum($_)} @{$opts{-whitepoint}});
$csd->{BlackPoint}=PDFArray(map {PDFNum($_)} @{$opts{-blackpoint}});
$csd->{Gamma}=PDFArray(map {PDFNum($_)} @{$opts{-gamma}});
$csd->{Matrix}=PDFArray(map {PDFNum($_)} @{$opts{-matrix}});
$self->add_elements(PDFName($opts{-type}),$csd);
$self->{' type'}='rgb';
} elsif($opts{-type} eq 'CalGray') {
my $csd=PDFDict();
$opts{-whitepoint}=$opts{-whitepoint} || [ 0.95049, 1, 1.08897 ];
$opts{-blackpoint}=$opts{-blackpoint} || [ 0, 0, 0 ];
$opts{-gamma}=$opts{-gamma} || 2.22218;
$csd->{WhitePoint}=PDFArray(map {PDFNum($_)} @{$opts{-whitepoint}});
$csd->{BlackPoint}=PDFArray(map {PDFNum($_)} @{$opts{-blackpoint}});
$csd->{Gamma}=PDFNum($opts{-gamma});
$self->add_elements(PDFName($opts{-type}),$csd);
$self->{' type'}='gray';
} elsif($opts{-type} eq 'Lab') {
my $csd=PDFDict();
$opts{-whitepoint}=$opts{-whitepoint} || [ 0.95049, 1, 1.08897 ];
$opts{-blackpoint}=$opts{-blackpoint} || [ 0, 0, 0 ];
$opts{-range}=$opts{-range} || [ -200, 200, -200, 200 ];
$opts{-gamma}=$opts{-gamma} || [ 2.22218, 2.22218, 2.22218 ];
$csd->{WhitePoint}=PDFArray(map {PDFNum($_)} @{$opts{-whitepoint}});
$csd->{BlackPoint}=PDFArray(map {PDFNum($_)} @{$opts{-blackpoint}});
$csd->{Gamma}=PDFArray(map {PDFNum($_)} @{$opts{-gamma}});
$csd->{Range}=PDFArray(map {PDFNum($_)} @{$opts{-range}});
$self->add_elements(PDFName($opts{-type}),$csd);
$self->{' type'}='lab';
} elsif($opts{-type} eq 'Indexed') {
my $csd=PDFDict();
$opts{-base}=$opts{-base} || 'DeviceRGB';
$opts{-maxindex}=$opts{-maxindex} || scalar(@{$opts{-colors}})-1;
$opts{-whitepoint}=$opts{-whitepoint} || [ 0.95049, 1, 1.08897 ];
$opts{-blackpoint}=$opts{-blackpoint} || [ 0, 0, 0 ];
$opts{-gamma}=$opts{-gamma} || [ 2.22218, 2.22218, 2.22218 ];
$csd->{WhitePoint}=PDFArray(map {PDFNum($_)} @{$opts{-whitepoint}});
$csd->{BlackPoint}=PDFArray(map {PDFNum($_)} @{$opts{-blackpoint}});
$csd->{Gamma}=PDFArray(map {PDFNum($_)} @{$opts{-gamma}});
foreach my $col (@{$opts{-colors}}) {
map { $csd->{' stream'}.=pack('C',$_); } @{$col};
}
$pdf->new_obj($csd);
$csd->{Filter}=PDFArray(PDFName('FlateDecode'));
$self->add_elements(PDFName($opts{-type}),PDFName($opts{-base}),PDFNum($opts{-maxindex}),$csd);
$self->{' type'}='index';
} elsif($opts{-type} eq 'ICCBased') {
my $csd=PDFDict();
$csd->{Filter}=PDFArray(PDFName('FlateDecode'));
$csd->{Alternate}=PDFName($opts{-base}) if(defined $opts{-base});
$csd->{N}=PDFNum($opts{-components});
$csd->{' streamfile'}=$opts{-iccfile};
$pdf->new_obj($csd);
$self->add_elements(PDFName($opts{-type}),$csd);
$self->{' type'} =
$opts{-base}=~/RGB/i ? 'rgb' :
$opts{-base}=~/CMYK/i ? 'cmyk' :
$opts{-base}=~/Lab/i ? 'lab' :
$opts{-base}=~/Gr[ae]y/i ? 'gray' :
$opts{-base}=~/Index/i ? 'index' : 'other'
;
}
return($self);
}
sub isRGB { $_[0]->{' type'}=~/rgb/ ? 1 : 0 ; }
sub isCMYK { $_[0]->{' type'}=~/cmyk/ ? 1 : 0 ; }
sub isLab { $_[0]->{' type'}=~/lab/ ? 1 : 0 ; }
sub isGray { $_[0]->{' type'}=~/gray/ ? 1 : 0 ; }
sub isIndexed { $_[0]->{' type'}=~/index/ ? 1 : 0 ; }
#==================================================================
# PDF::API2::ExtGState
#==================================================================
package PDF::API2::ExtGState;
use strict;
use vars qw(@ISA);
@ISA = qw(Text::PDF::Dict);
use Text::PDF::Dict;
use Text::PDF::Utils;
use Math::Trig;
use PDF::API2::Util;
=head2 PDF::API2::ExtGState
Subclassed from Text::PDF::Dict.
=item $egs = PDF::API2::ExtGState->new @parameters
Returns a new extgstate object (called from $pdf->extgstate).
=cut
sub new {
my ($class,$pdf,$key)=@_;
my $self = $class->SUPER::new;
$self->{' apiname'}=$key;
$self->{' apipdf'}=$pdf;
$self->{Type}=PDFName('ExtGState');
return($self);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -