Skip to content

Commit

Permalink
add .shape(:!kern) option
Browse files Browse the repository at this point in the history
  • Loading branch information
dwarring committed Feb 26, 2024
1 parent 07ae1e2 commit e05646b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 28 deletions.
2 changes: 1 addition & 1 deletion lib/PDF/Font/Loader.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class PDF::Font::Loader:ver<0.8.1> {
|| do {
note "unable to locate font. Falling back to mono-spaced font"
unless $quiet;
%?RESOURCES<font/FreeMono.ttf>.absolute;
%?RESOURCES<font/FreeMono.ttf>.IO.absolute;
}

my PDF::Font::Loader::FontObj:D $font := $class.load-font: :$file, :$dict, |c;
Expand Down
15 changes: 8 additions & 7 deletions lib/PDF/Font/Loader/FontObj.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -488,14 +488,15 @@ method kern(Str $text) {
}
has $!harfbuzz-font;
method !harfbuzz-font {
method !harfbuzz-font(:@features) {
$!harfbuzz-font //= $!face.font-format ~~ 'TrueType'|'OpenType'
?? HarfBuzz::Font.COERCE: %( :blob($!font-buf), )
!! HarfBuzz::Font::FreeType.COERCE: %( :ft-face($!face), );
?? HarfBuzz::Font.COERCE: %( :blob($!font-buf), :@features)
!! HarfBuzz::Font::FreeType.COERCE: %( :ft-face($!face), :@features);
}
multi method shape(Str $text where $!face.font-format ~~ 'TrueType'|'OpenType') {
my HarfBuzz::Font $font = self!harfbuzz-font;
multi method shape(Str $text where $!face.font-format ~~ 'TrueType'|'OpenType', Bool :$kern = True) {
my HarfBuzz::Feature() @features = $kern ?? <kern> !! <-kern>;
my HarfBuzz::Font $font = self!harfbuzz-font: :@features;
my HarfBuzz::Shaper $shaper .= new: :buf{ :$text, :direction(HB_DIRECTION_LTR) }, :$font;
my uint32 @ords = $text.ords;
my @shaped;
Expand Down Expand Up @@ -551,12 +552,12 @@ multi method shape(Str $text where $!face.font-format ~~ 'TrueType'|'OpenType')
@shaped, $width;
}
multi method shape(Str $text is copy) {
multi method shape(Str $text is copy, Bool :$kern = $!face.has-kerning) {
my Numeric $width = 0.0;
my @shaped;
$text = .ligature-subs($text) with self!metrics;
if $!face.has-kerning {
if $kern {
my FT_UInt $prev-gid = 0;
my FT_Vector $kerning .= new;
my FT_Face $face-struct = $!face.raw;
Expand Down
Binary file modified t/pdf-text-align.pdf
Binary file not shown.
59 changes: 39 additions & 20 deletions t/pdf-text-align.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,63 @@ use Test;
plan 2;
use PDF::Lite;
use PDF::Content;
use PDF::Content::Color :&color;
use PDF::Content::FontObj;
use PDF::Font::Loader;

sub draw-rect($gfx, @rect) {
$gfx.tag: 'Artifact', {
$gfx.StrokeAlpha = .5;
$gfx.StrokeColor = color .5, .01, .01;
$gfx.paint: :stroke, { .Rectangle(@rect[0], @rect[1], @rect[2] - @rect[0], @rect[3] - @rect[1]); }
}
}

sub draw-cross($gfx, $x, $y) {
$gfx.tag: 'Artifact', {
$gfx.StrokeAlpha = .75;
$gfx.StrokeColor = color .01, .7, 0.1;
$gfx.paint: :stroke, { .MoveTo($x-5, $y); .LineTo($x+5, $y); }
$gfx.paint: :stroke, { .MoveTo($x, $y-5); .LineTo($x, $y+5); }
}
}

my PDF::Lite $pdf .= new();
my PDF::Lite::Page $page = $pdf.add-page;
my PDF::Content $gfx = $page.gfx;
my $width = 100;
my $height = 80;
my $x = 110;
my $width = 120;
my $x = 125;

my PDF::Content::FontObj $font = PDF::Font::Loader.load-font: :file<t/fonts/Vera.ttf>, :!subset, :prefix<ABCDEF>;

is-deeply $font.encode("Abc€√b").ords, (0,36, 0,69, 0,70, 1,2, 0,165, 0,69), 'encode (identity-h)';
$gfx.Save;
$gfx.font = $font, 10;

$gfx.text: -> $gfx {
$gfx.font = $font, 10;

my $sample = q:to"--ENOUGH!!--";
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed
do eiusmod tempor incididunt ut labore et dolore magna aliqua.
--ENOUGH!!--
my $baseline = 'top';
my $sample = q:to"--ENOUGH!!--";
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed
do eiusmod tempor incididunt ut labore et dolore magna aliqua.
--ENOUGH!!--
for <top center bottom> -> $valign {
for <top center bottom> -> $valign {
my $y = 700;
my $y = 700;
for <left center right justify> -> $align {
$gfx.text-position = ($x, $y);
$gfx.say( "*** $valign $align*** " ~ $sample, :$width, :$height, :$valign, :$align, :$baseline );
$y -= 170;
for <left center right justify> -> $align {
my @rect[4];
$gfx.&draw-cross($x, $y);
$gfx.text: {
.text-position = ($x, $y);
@rect = .print( "*** $valign $align*** " ~ $sample, :$width, :$valign, :$align, );
}

$x += 125;
draw-rect $gfx, @rect;
$y -= 170;
}

$x += 125;
}

$gfx.Restore;
# ensure consistant document ID generation
$pdf.id = $*PROGRAM-NAME.fmt('%-16.16s');

Expand Down
Binary file modified t/reuse-cid.pdf
Binary file not shown.

0 comments on commit e05646b

Please sign in to comment.