📄 api2.pm
字号:
=item $wd = $font->width_utf8 $text
Returns the width of $text as if it were at size 1,
but requires $text to be in UTF8.
=cut
sub width_utf8 {
my ($self,$text)=@_;
$text=utf8_to_ucs2($text);
my ($width);
foreach my $x (0..(length($text)>>1)-1) {
$width += $self->{' uniwidth'}{vec($text,$x,16)};
}
$width/=1000;
return($width);
}
=item $font->encode $encoding
Changes the encoding of the font object. Since encodings are one virtual
in ::API2 for truetype fonts you DONT have to use 'clone'.
=cut
sub encode {
my ($self,$enc)=@_;
$self->{' apipdf'}->{' encoding'}=$self->{' apipdf'}->{' encoding'} || {};
$self->{' apipdf'}->{' encoding'}->{$enc}=$self->{' apipdf'}->{' encoding'}->{$enc} || PDF::API2::UniMap->new($enc);
my $map=$self->{' apipdf'}->{' encoding'}->{$enc};
my $ttf=$self->{' font'};
my $upem = $ttf->{'head'}->read->{'unitsPerEm'};
$self->{' encoding'}=$enc;
$self->{' chrcid'}->{$enc}=$self->{' chrcid'}->{$enc}||{};
$self->{' chrwidth'}->{$enc}=$self->{' chrwidth'}->{$enc}||{};
if(scalar keys(%{$self->{' chrcid'}->{$enc}}) < 1) {
foreach my $x (0..255) {
$self->{' chrcid'}->{$enc}{$x}=
$self->{' unicid'}{$map->{'c2u'}->{$x}||32}||$self->{' unicid'}{32};
$self->{' chrwidth'}->{$enc}{$x}=
$ttf->{'hmtx'}{'advance'}[$self->{' unicid'}{$map->{'c2u'}->{$x}||32}||$self->{' unicid'}{32}]*1000/$upem;
}
}
return($self);
}
#==================================================================
# PDF::API2::Page
#==================================================================
package PDF::API2::Page;
use strict;
use vars qw(@ISA);
@ISA = qw(Text::PDF::Pages);
use Text::PDF::Pages;
use Text::PDF::Utils;
use PDF::API2::Util;
use Math::Trig;
=head2 PDF::API2::Page
Subclassed from Text::PDF::Pages
=item $page = PDF::API2::Page->new $pdf, $parent, $index
Returns a page object (called from $pdf->page).
=cut
sub new {
my ($class, $pdf, $parent, $index) = @_;
my ($self) = {};
$class = ref $class if ref $class;
$self = $class->SUPER::new($pdf, $parent);
$self->{'Type'} = PDFName('Page');
delete $self->{'Count'};
delete $self->{'Kids'};
$parent->add_page($self, $index);
$self;
}
=item $page = PDF::API2::Page->coerce $pdf, $pdfpage
Returns a page object converted from $pdfpage (called from $pdf->openpage).
=cut
sub coerce {
my ($class, $pdf, $page) = @_;
my ($self) = {};
bless($self,$class);
foreach my $k (keys %{$page}) {
$self->{$k}=$page->{$k};
}
$self->{' apipdf'}=$pdf;
return($self);
}
=item $page->update
Marks a page to be updated (by $pdf->update).
=cut
sub update {
my ($self) = @_;
$self->{' apipdf'}->out_obj($self);
$self;
}
=item $page->mediabox $w, $h
=item $page->mediabox $llx, $lly, $urx, $ury
Sets the mediabox.
=cut
sub mediabox {
my ($self,$x1,$y1,$x2,$y2) = @_;
if(defined $x2) {
$self->{'MediaBox'}=PDFArray(
map { PDFNum(float($_)) } ($x1,$y1,$x2,$y2)
);
} else {
$self->{'MediaBox'}=PDFArray(
map { PDFNum(float($_)) } (0,0,$x1,$y1)
);
}
$self;
}
=item $page->cropbox $w, $h
=item $page->cropbox $llx, $lly, $urx, $ury
Sets the cropbox.
=cut
sub cropbox {
my ($self,$x1,$y1,$x2,$y2) = @_;
if(defined $x2) {
$self->{'CropBox'}=PDFArray(
map { PDFNum(float($_)) } ($x1,$y1,$x2,$y2)
);
} else {
$self->{'CropBox'}=PDFArray(
map { PDFNum(float($_)) } (0,0,$x1,$y1)
);
}
$self;
}
=item $page->bleedbox $w, $h
=item $page->bleedbox $llx, $lly, $urx, $ury
Sets the bleedbox.
=cut
sub bleedbox {
my ($self,$x1,$y1,$x2,$y2) = @_;
if(defined $x2) {
$self->{'BleedBox'}=PDFArray(
map { PDFNum(float($_)) } ($x1,$y1,$x2,$y2)
);
} else {
$self->{'BleedBox'}=PDFArray(
map { PDFNum(float($_)) } (0,0,$x1,$y1)
);
}
$self;
}
=item $page->trimbox $w, $h
=item $page->trimbox $llx, $lly, $urx, $ury
Sets the trimbox.
=cut
sub trimbox {
my ($self,$x1,$y1,$x2,$y2) = @_;
if(defined $x2) {
$self->{'TrimBox'}=PDFArray(
map { PDFNum(float($_)) } ($x1,$y1,$x2,$y2)
);
} else {
$self->{'TrimBox'}=PDFArray(
map { PDFNum(float($_)) } (0,0,$x1,$y1)
);
}
$self;
}
=item $page->artbox $w, $h
=item $page->artbox $llx, $lly, $urx, $ury
Sets the artbox.
=cut
sub artbox {
my ($self,$x1,$y1,$x2,$y2) = @_;
if(defined $x2) {
$self->{'ArtBox'}=PDFArray(
map { PDFNum(float($_)) } ($x1,$y1,$x2,$y2)
);
} else {
$self->{'ArtBox'}=PDFArray(
map { PDFNum(float($_)) } (0,0,$x1,$y1)
);
}
$self;
}
=item $gfx = $page->gfx
Returns a graphics content object.
=cut
sub fixcontents {
my ($self) = @_;
$self->{'Contents'} = $self->{'Contents'} || PDFArray();
if(ref($self->{'Contents'})=~/Objind$/) {
$self->{'Contents'}->realise;
}
if(ref($self->{'Contents'})!~/Array$/) {
$self->{'Contents'} = PDFArray($self->{'Contents'});
}
}
sub addcontent {
my ($self,@objs) = @_;
$self->fixcontents;
$self->{'Contents'}->add_elements(@objs);
}
sub gfx {
my ($self) = @_;
my $gfx=PDF::API2::Gfx->new();
$self->addcontent($gfx);
$self->{' apipdf'}->new_obj($gfx);
$gfx->{' apipdf'}=$self->{' apipdf'};
$gfx->{' apipage'}=$self;
return($gfx);
}
=item $txt = $page->text
Returns a text content object.
=cut
sub text {
my ($self) = @_;
my $text=PDF::API2::Text->new();
$self->addcontent($text);
$self->{' apipdf'}->new_obj($text);
$text->{' apipdf'}=$self->{' apipdf'};
$text->{' apipage'}=$self;
return($text);
}
=item $hyb = $page->hybrid
Returns a hybrid content object.
=cut
sub hybrid {
my ($self) = @_;
my $hyb=PDF::API2::Hybrid->new();
$self->addcontent($hyb);
$self->{' apipdf'}->new_obj($hyb);
$hyb->{' apipdf'}=$self->{' apipdf'};
$hyb->{' apipage'}=$self;
return($hyb);
}
=item $ant = $page->annotation
Returns a annotation object.
=cut
sub annotation {
my ($self, $type, $key, $obj) = @_;
$self->{'Annots'} = $self->{'Annots'} || PDFArray();
my $ant=PDF::API2::Annotation->new;
$self->{'Annots'}->add_elements($ant);
$self->{' apipdf'}->new_obj($ant);
$ant->{' apipdf'}=$self->{' apipdf'};
$ant->{' apipage'}=$self;
return($ant);
}
=item $page->resource $type, $key, $obj
Adds a resource to the page-inheritance tree.
B<Example:>
$co->resource('Font',$fontkey,$fontobj);
$co->resource('XObject',$imagekey,$imageobj);
$co->resource('Shading',$shadekey,$shadeobj);
$co->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) = @_;
my ($dict) = $self->find_prop('Resources');
$dict= $dict || $self->{Resources} || PDFDict();
$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->{' apipdf'}->out_obj($dict)
if($dict->is_obj($self->{' apipdf'}));
$self->{' apipdf'}->out_obj($dict->{$type})
if($dict->{$type}->is_obj($self->{' apipdf'}));
$self->{' apipdf'}->out_obj($obj)
if($obj->is_obj($self->{' apipdf'}));
$self->{' apipdf'}->out_obj($self);
return($dict);
}
sub content {
my ($self,$obj) = @_;
$self->fixcontents;
$self->{'Contents'}->add_elements($obj);
## $self->{' apipdf'}->new_obj($obj);
$obj->{' apipdf'}=$self->{' apipdf'};
$obj->{' apipage'}=$self;
return($obj);
}
sub ship_out
{
my ($self, $pdf) = @_;
$pdf->ship_out($self);
if (defined $self->{'Contents'})
{ $pdf->ship_out($self->{'Contents'}->elementsof); }
$self;
}
#==================================================================
# PDF::API2::Annotation
#==================================================================
package PDF::API2::Annotation;
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::Annotation
Subclassed from Text::PDF::Dict.
=item $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');
return($self);
}
=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');
$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');
$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,$file,%opts)=@_;
$self->{Subtype}=PDFName('Link');
$self->{A}=PDFDict();
$self->{A}->{S}=PDFName('Launch');
$self->{A}->{F}=PDFStr($file);
$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..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..2] );
return($self);
}
=item $ant->content @lines
Sets the text-content of the annotation, if applicable.
=cut
sub content {
my ($self,@t)=@_;
$self->{Content}=PDFStr(join("\n",@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 ma
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -