Skip to content

Commit

Permalink
adjust to latest Font::FreeType
Browse files Browse the repository at this point in the history
  • Loading branch information
dwarring committed Dec 30, 2023
1 parent 3af29e1 commit 9241f48
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/PDF/Font/Loader.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class PDF::Font::Loader:ver<0.7.8> {
$class.load-font: :$font-buf, |c;
}

my subset Type1 where .font-format ~~ 'Type 1'|'CFF' && !.is-internally-keyed-cid;
my subset Type1 where .font-format ~~ 'Type 1'|'CFF'|'OpenType' && !.is-internally-keyed-cid;
my subset CIDEncoding of Str where m/^[identity|utf]/;
multi method load-font(
$?: Font::FreeType::Face :$face!,
Expand Down
2 changes: 1 addition & 1 deletion lib/PDF/Font/Loader/Enc.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ method make-to-unicode-cmap(:$to-unicode = self.to-unicode) {
while $cid < last-char && $to-unicode[$cid + 1] && ($cid+1) div 256 == $start-byte {
my $this-ord := $to-unicode[$cid + 1];
if ($this-ord == $last-ord + 1) {
if ++$ord-run-len >= 6 {
if ++$ord-run-len >= 5 {
# We've encountered a run of ascending cids + ords.
# Process them more elegantly on our next loop.
$cid -= $ord-run-len;
Expand Down
21 changes: 13 additions & 8 deletions lib/PDF/Font/Loader/FontObj.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ method encode($text is raw, |c) {
method !font-type-entry returns Str {
given $!face.font-format {
when 'Type 1'|'CFF' { 'Type1' }
when 'TrueType'|'OpenType' { 'TrueType' }
when 'Type 1'|'CFF'|'OpenType' { 'Type1' }
when 'TrueType' { 'TrueType' }
default { fail "unable to handle font type: $_" }
}
}
Expand Down Expand Up @@ -203,13 +203,18 @@ method !make-other-font-file(Blob:D $buf) {
given $!face.font-format {
when 'OpenType' {
%dict<Subtype> = /<CIDFontType0C>;
%dict<Subtype> = $!face.is-internally-keyed-cid
?? /<CIDFontType0C> !! /<OpenType>;
}
when 'CFF' {
# Peek at the buffer to distinguish simple CFF from OpenType/CFF
# See https://learn.microsoft.com/en-us/typography/opentype/spec/otff#organization-of-an-opentype-font
my subset OpenTypeCFF of Blob:D where .subbuf(0,4).decode('latin-1') eq 'OTTO';
%dict<Subtype> = ($buf ~~ OpenTypeCFF) ?? /<OpenType> !! /<Type1C>;
%dict<Subtype> = /<Type1C>;
if Font::FreeType.^ver <= v0.5.4 {
# Peek at the buffer to distinguish simple CFF from OpenType/CFF
# See https://learn.microsoft.com/en-us/typography/opentype/spec/otff#organization-of-an-opentype-font
my subset OpenTypeCFF of Blob:D where .subbuf(0,4).decode('latin-1') eq 'OTTO';
%dict<Subtype> = /<OpenType>
if $buf ~~ OpenTypeCFF;
}
}
}
Expand Down Expand Up @@ -274,7 +279,7 @@ method font-descriptor {
$dict<FontWeight> //= .usWeightClass;
$dict<AvgWidth> //= .xAvgCharWidth;
if $!face.font-format ~~ 'FreeType'|'OpenType' {
if $!face.is-internally-keyed-cid {
# applicable to CID font descriptors
my $buf = .panose.Blob;
$buf.prepend: (.sFamilyClass div 256, .sFamilyClass mod 256);
Expand Down
4 changes: 2 additions & 2 deletions lib/PDF/Font/Loader/FontObj/CID.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ submethod TWEAK {
# /Subtype entry for the descendant CID font
method !cid-font-type-entry {
given $.face.font-format {
when 'CFF' { 'CIDFontType0' }
when 'TrueType'|'OpenType' {'CIDFontType2'}
when 'CFF'|'OpenType' { 'CIDFontType0' }
when 'TrueType' {'CIDFontType2'}
default { fail "unable to handle CID font type: $_" }
}
}
Expand Down
Binary file modified t/fontobj.pdf
Binary file not shown.
Binary file modified t/reuse-type1.pdf
Binary file not shown.
Binary file modified t/reuse-unembedded.pdf
Binary file not shown.
4 changes: 2 additions & 2 deletions t/type1-encoding.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ my PDF::Lite $pdf .= new;
my PDF::Lite::Page $page = $pdf.add-page;
my @differences = 1, 'b', 'c', 10, 'y', 'z';
my PDF::Content::FontObj $times = PDF::Font::Loader.load-font( :file<t/fonts/TimesNewRomPS.pfb>, :@differences );
is-deeply $times.encode('abcdxyz', :cids), buf8.new(97,1,2,100,120,10,11), 'differences encoding';
is-deeply $times.encode('abcdxyz', :cids).list, (97,1,2,100,120,10,11), 'differences encoding';
$page.text: {
.text-position = 10,500;
.font = $times;
Expand All @@ -26,7 +26,7 @@ lives-ok {
$times = PDF::Font::Loader.load-font( :$dict );
}, 'reload font from dict - lives';

is-deeply $times.encode('abcdxyz', :cids), buf8.new(97,1,2,100,120,10,11), 'differences re-encoding';
is-deeply $times.encode('abcdxyz', :cids).list, (97,1,2,100,120,10,11), 'differences re-encoding';

done-testing;

0 comments on commit 9241f48

Please sign in to comment.