Subject: | The "synthetic fonts" feature is broken (and probably fixed...) |
use strict;
use warnings;
use PDF::API2;
my $pdf = PDF::API2-> new;
my $font = $pdf-> ttfont( 'calibri.ttf' );
my $sft = $pdf-> synfont( $font );
__END__
Can't locate object method "name" via package "PDF::API2::Resource::CIDFont::TrueType=HASH(0x1e067cc)" (perhaps you forgot to load "PDF::API2::Resource::CIDFont::TrueType=HASH(0x1e067cc)"?) at C:/Strawberry/perl/site/lib/PDF/API2/Resource/Font/SynFont.pm line 92.
------------
--- API2.pm.bak Thu Jan 26 22:30:03 2017
+++ API2.pm Tue Jan 31 11:58:13 2017
@@ -1883,13 +1883,13 @@
=cut
sub synfont {
- my ($self, %opts) = @_;
+ my ($self, $font, %opts) = @_;
require PDF::API2::Resource::Font::SynFont;
- my $obj = PDF::API2::Resource::Font::SynFont->new($self->{'pdf'}, %opts);
+ my $obj = PDF::API2::Resource::Font::SynFont->new($self->{'pdf'}, $font, %opts);
$self->{'pdf'}->out_obj($self->{'pages'});
- $obj->tounicodemap() if $opts{-unicodemap} == 1;
+ $obj->tounicodemap unless defined $opts{-unicodemap} and !$opts{-unicodemap};
return $obj;
}
----------
2 lines for a fix, the 3d line to add "ToUnicode" by default (a PDF file without it is a bad idea, I think).
Further, these fonts can't be used with anything but basic latin. The '-encode' option is mentioned in POD, but not implemented in code. See SynFont.diff patch. Tested with TTF fonts and CP1251 (Latin/Cyrillic) encoding, should work for any "simple" (fitting into single byte) encoding.
Subject: | API2.diff |
--- API2.pm.bak Thu Jan 26 22:30:03 2017
+++ API2.pm Tue Jan 31 11:58:13 2017
@@ -1883,13 +1883,13 @@
=cut
sub synfont {
- my ($self, %opts) = @_;
+ my ($self, $font, %opts) = @_;
require PDF::API2::Resource::Font::SynFont;
- my $obj = PDF::API2::Resource::Font::SynFont->new($self->{'pdf'}, %opts);
+ my $obj = PDF::API2::Resource::Font::SynFont->new($self->{'pdf'}, $font, %opts);
$self->{'pdf'}->out_obj($self->{'pages'});
- $obj->tounicodemap() if $opts{-unicodemap} == 1;
+ $obj->tounicodemap unless defined $opts{-unicodemap} and !$opts{-unicodemap};
return $obj;
}
Subject: | SynFont.diff |
--- SynFont.pm.bak Thu Jan 26 22:30:03 2017
+++ SynFont.pm Tue Jan 31 12:20:39 2017
@@ -82,11 +82,7 @@
my $space=$opts{-space}||'0';
my $bold=($opts{-bold}||0)*10; # convert to em
- $self->{' slant'}=$slant;
- $self->{' oblique'}=$oblique;
- $self->{' bold'}=$bold;
- $self->{' boldmove'}=0.001;
- $self->{' space'}=$space;
+ $font-> encodeByName( $opts{ -encode }) if $opts{ -encode };
$class = ref $class if ref $class;
$self = $class->SUPER::new($pdf,
@@ -178,7 +174,10 @@
next;
}
my $char=PDFDict();
- my $wth=int($font->width(chr($w))*1000*$slant+2*$space);
+
+ my $uni = $self->data->{uni}->[$w];
+ my $wth = int($font->width(chr($uni))*1000*$slant+2*$space);
+
$procs->{$font->glyphByEnc($w)}=$char;
#$char->{Filter}=PDFArray(PDFName('FlateDecode'));
$char->{' stream'}=$wth." 0 ".join(' ',map { int($_) } $self->fontbbox)." d1\n";
@@ -196,16 +195,15 @@
$char->{' stream'}.="/FSN 800 Tf\n";
$char->{' stream'}.=($slant*110)." Tz\n";
$char->{' stream'}.=" [ -$space ] TJ\n" if($space);
- my $ch=$self->encByUni(hex($ci->{upper}));
- $wth=int($font->width(chr($ch))*800*$slant*1.1+2*$space);
- $char->{' stream'}.=$font->text(chr($ch));
+ $wth=int($font->width(uc chr($uni))*800*$slant*1.1+2*$space);
+ $char->{' stream'}.=$font->text(uc chr($uni));
}
else
{
$char->{' stream'}.="/FSN 1000 Tf\n";
$char->{' stream'}.=($slant*100)." Tz\n" if($slant!=1);
$char->{' stream'}.=" [ -$space ] TJ\n" if($space);
- $char->{' stream'}.=$font->text(chr($w));
+ $char->{' stream'}.=$font-> text( chr( $uni ));
}
$char->{' stream'}.=" Tj\nET\n";
push @widths,$wth;
Subject: | api2-synfont.pl |
use strict;
use warnings;
use utf8;
use PDF::API2;
use Encode qw/ encode /;
my $pdf = PDF::API2-> new;
my $page = $pdf-> page;
$page-> mediabox( 'A4' );
my $text = $page-> text;
my $font = $pdf-> ttfont( 'calibri.ttf' );
my $str = 'Hello World! ÐÑивеÑ, миÑ!';
my %opts = ( -encode => 'CP1251' );
my $sft = $pdf-> synfont( $font, %opts );
$text-> font( $sft, 20 );
$text-> translate( 100, 700 );
$text-> text( $str );
my $sft1 = $pdf-> synfont( $font, %opts = ( %opts, -caps => 1 ));
$text-> font( $sft1, 20 );
$text-> translate( 100, 650 );
$text-> text( $str );
my $sft2 = $pdf-> synfont( $font, %opts, -bold => 4 );
$text-> font( $sft2, 20 );
$text-> translate( 100, 600 );
$text-> text( $str );
my $sft3 = $pdf-> synfont( $font, %opts, -oblique => 30 );
$text-> font( $sft3, 20 );
$text-> translate( 100, 550 );
$text-> text( $str );
my $sft4 = $pdf-> synfont( $font, %opts, -slant => 0.5, -space => 100 );
$text-> font( $sft4, 20 );
$text-> translate( 100, 500 );
$text-> text( $str );
$pdf-> saveas( 'test.pdf' );