Skip to content

Commit

Permalink
Add a file() attribute #38
Browse files Browse the repository at this point in the history
  • Loading branch information
dwarring committed Dec 5, 2024
1 parent 2ccb3ae commit ed7b21a
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 11 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{{$NEXT}}
- Add file attribute to font objects.

0.8.9 2024-11-04T08:54:13+13:00
- Add can-subset() method.
Expand Down
14 changes: 7 additions & 7 deletions lib/PDF/Font/Loader.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,15 @@ use PDF::Font::Loader::Dict :&load-font-opts;
proto method load-font($?: |c) is export(:load-font) {*};

multi sub find-afm(IO:D $file where .extension ~~ 'pfa'|'pfb') {
my $afm-file = $file.Str.subst(/'.pf'[a|b]$/, '.afm');
my $afm-file = $file.path.subst(/'.pf'[a|b]$/, '.afm');
$afm-file.IO.e ?? $afm-file !! Str;
}
multi sub find-afm(IO:D $file where .extension ~~ 'PFA'|'PFB') {
my $afm-file = $file.Str.subs(/'.PF'[A|B]$/, '.AFM');
my $afm-file = $file.path.subs(/'.PF'[A|B]$/, '.AFM');
$afm-file.IO.e ?? $afm-file !! Str;
}
multi sub find-afm($) { Str }

multi method load-font($class = $?CLASS: IO() :$file!, Str :$afm = find-afm($file), |c) {
my Blob $font-buf = $file.slurp: :bin;
$class.load-font: :$font-buf, :$afm, |c;
}

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(
Expand Down Expand Up @@ -59,6 +54,11 @@ multi method load-font($class = $?CLASS: Blob :$font-buf!, Font::FreeType :$ft-l
$class.load-font: :$face, :$font-buf, |c;
}

multi method load-font($class = $?CLASS: IO:D() :$file!, Str :$afm = find-afm($file), |c) {
my Blob $font-buf = $file.slurp: :bin;
$class.load-font: :$font-buf, :$afm, :$file, |c;
}

# core font load
multi method load-font(
$class is copy = $?CLASS:
Expand Down
1 change: 1 addition & 0 deletions lib/PDF/Font/Loader/FontObj.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ has Bool $!build-widths;
has Str $.afm;
has Font::AFM $!metrics;
has uint32 @.unicode-index;
has IO::Path $.file;

sub subsetter { PDF::COS.required("HarfBuzz::Subset") }
sub shaper { PDF::COS.required("PDF::Font::Loader::HarfBuzz") }
Expand Down
1 change: 1 addition & 0 deletions t/fontobj.t
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ my PDF::Content::FontObj $ttc-font = load-font :file<t/fonts/Sitka.ttc>, :!embed

is $vera.underline-position, -284;
is $vera.underline-thickness, 143;
is $vera.file.path, 't/fonts/Vera.ttf';

my $n = 0;
my $vera-chars;
Expand Down
3 changes: 2 additions & 1 deletion t/reuse-cid.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use PDF::Lite;
use PDF::Font::Loader;
use PDF::Content::FontObj;

plan 9;
plan 10;
# see if we can re-load the CID font that we wrote in pdf-text.align.t

class FontLoader {
Expand Down Expand Up @@ -48,6 +48,7 @@ sub font-sanity($font) {
my $enc = $font.encode($text, :str);
is-deeply $enc, [~]("\0\x[24]", "\0\x[45]", "\0\x[46]", "\x[1]\x[2]", "\x[0]\x[a5]", "\0\x[45]"), 'encode';
is $font.decode($enc, :str), $text, "font encode/decode round-trip";
nok $font.file, 'lacks an external file path';
}

my PDF::Lite $pdf .= open: "t/pdf-text-align.pdf";
Expand Down
Binary file modified t/reuse-type1.pdf
Binary file not shown.
3 changes: 1 addition & 2 deletions t/reuse-type1.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use PDF::Content::FontObj;
my PDF::Lite $pdf .= open: "t/fontobj.pdf";
my PDF::Lite::Page $page = $pdf.page(2);


$pdf.page(2).gfx.text: -> $gfx {
my PDF::COS::Dict %fonts = $gfx.resources('Font');
$gfx.text-position = 10, 400;
Expand All @@ -36,7 +35,7 @@ $pdf.page(2).gfx.text: -> $gfx {

# ensure consistant document ID generation
my $basename := "t/reuse-type1";
$pdf.id = "{$basename}.t".fmt('%-16s').substr(0,16);
$pdf.id = "{$basename}.t".fmt('%-16s').substr(0,16);
lives-ok { $pdf.save-as: "{$basename}.pdf"; };

done-testing;
Binary file modified t/type3-basic.pdf
Binary file not shown.
3 changes: 2 additions & 1 deletion t/type3-basic.t
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use Test;
plan 11;
plan 12;
use PDF::COS::Dict;
use PDF::Lite;
use PDF::Font::Loader;
Expand All @@ -25,6 +25,7 @@ $pdf.page(1).gfx.text: -> $gfx {
is $f1.enc, 'std', 'enc';
nok $f1.is-embedded, "isn't embedded";
nok $f1.is-subset, "isn't subset";
ok $f1.file.path, 'has file path';
my @cids = 1, 2, 3;
my @glyphs = $f1.get-glyphs(@cids);
is-deeply @glyphs[0], PDF::Font::Loader::Glyph.new(:name<square>, :cid(1), :gid(1), :ax(1000), :sx(1000) );
Expand Down

0 comments on commit ed7b21a

Please sign in to comment.