Skip to content

Commit

Permalink
use BBox for SizeMetrics
Browse files Browse the repository at this point in the history
  • Loading branch information
dwarring committed Dec 9, 2023
1 parent 256333e commit f7e4adb
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 45 deletions.
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ Testing
To checkout and test this module from the Git repository:

$ git checkout https://github.com/pdf-raku/Font-FreeType-raku.git
$ zef build . # -OR- raku Build.pm
$ zef build . # -OR- raku Build.rakumod
$ prove -e'raku -I .' -v t

Authors
Expand Down
41 changes: 29 additions & 12 deletions lib/Font/FreeType/BBox.rakumod
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
class Font::FreeType::BBox {
class Font::FreeType::BBox
is Array {

use Font::FreeType::Raw;
use Font::FreeType::Raw::Defs;

constant Dot6 = Font::FreeType::Raw::Defs::Dot6;

has Numeric ($.x-min, $.y-min);
has Numeric ($.x-max, $.y-max);
method x-min is rw { self[0] }
method y-min is rw { self[1] }
method x-max is rw { self[2] }
method y-max is rw { self[3] }
method width { self[2] - self[0] }
method height { self[3] - self[1] }

submethod TWEAK(FT_BBox :$bbox!) {
with $bbox {
$!x-min = .x-min / Dot6;
$!x-max = .x-max / Dot6;
$!y-min = .y-min / Dot6;
$!y-max = .y-max / Dot6;
}
multi method new(FT_BBox:D :bbox($_)!) {
my ::?CLASS:D \bbox = self.bless;
bbox.x-min = .x-min / Dot6;
bbox.y-min = .y-min / Dot6;
bbox.x-max = .x-max / Dot6;
bbox.y-max = .y-max / Dot6;
bbox;
}

method Array {
[$!x-min, $!y-min, $!x-max, $!y-max];
multi method new(Array:D :bbox($_)! where .elems == 4) {
my ::?CLASS:D \bbox = self.bless;
bbox.x-min = .[0];
bbox.y-min = .[1];
bbox.x-max = .[2];
bbox.y-max = .[3];
bbox;
}

multi method new(|) { fail }

method bounding-box {
[floor(self.x-min), floor(self.y-min),
ceiling(self.x-max), ceiling(self.y-max)]
}
}
13 changes: 9 additions & 4 deletions lib/Font/FreeType/Face.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Font::FreeType::Face {
use Font::FreeType::NamedInfo;
use Font::FreeType::CharMap;
use Font::FreeType::SizeMetrics;
use Font::FreeType::BBox;
use Method::Also;

constant Dot6 = Font::FreeType::Raw::Defs::Dot6;
Expand All @@ -27,10 +28,14 @@ class Font::FreeType::Face {
ft-try { self.raw.FT_Attach_File($filepath); }
}

method bbox returns FT_BBox is DEPRECATED<bounding-box> {
my FT_BBox $bbox = $!raw.bbox.clone
if self.is-scalable;
$bbox;
method bbox returns Font::FreeType::BBox {
if self.is-scalable {
my FT_BBox $bbox = $!raw.bbox;
Font::FreeType::BBox.new: :$bbox;
}
else {
Font::FreeType::BBox;
}
}
class UnscaledMetrics {
method bbox is also<bounding-box> { Array }
Expand Down
8 changes: 1 addition & 7 deletions lib/Font/FreeType/Outline.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,12 @@ class Font::FreeType::Outline {
$shape;
}

method bbox returns Font::FreeType::BBox:D {
method bbox returns Font::FreeType::BBox:D handles<bounding-box> {
my FT_BBox $bbox .= new;
ft-try { $!raw.FT_Outline_Get_BBox($bbox); };
Font::FreeType::BBox.new: :$bbox;
}

method bounding-box is also<Array> {
my Font::FreeType::BBox $bbox = self.bbox;
[floor($bbox.x-min), floor($bbox.y-min),
ceiling($bbox.x-max), ceiling($bbox.y-max)]
}

method postscript returns Str:D {
my Str @lines;
my ft6_shape_t $shape = self.decompose;
Expand Down
2 changes: 1 addition & 1 deletion lib/Font/FreeType/Raw/Defs.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ our $FT-LIB is export = Rakudo::Internals.IS-WIN ?? find-library('freetype') !!
# library bindings

# additional C bindings
our $FT-WRAPPER-LIB is export = Rakudo::Internals.IS-WIN ?? find-library('ft6') !! %?RESOURCES<libraries/ft6>;
our $FT-WRAPPER-LIB is export = Rakudo::Internals.IS-WIN ?? find-library('ft6') !! %?RESOURCES<libraries/ft6>;
our $CLIB = Rakudo::Internals.IS-WIN ?? 'msvcrt' !! Str;

constant FT_Bool is export = uint8;
Expand Down
34 changes: 14 additions & 20 deletions lib/Font/FreeType/SizeMetrics.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ unit class Font::FreeType::SizeMetrics;

use Font::FreeType::Raw;
use Font::FreeType::Raw::Defs;
use Font::FreeType::BBox;
use Method::Also;

constant Dot6 = Font::FreeType::Raw::Defs::Dot6;
Expand Down Expand Up @@ -60,26 +61,19 @@ method max-advance-height {
$!face.max-advance-height * $.y-scale;
}

method Array is also<FontBBox> {
with $!face.bbox {
[
.x-min * $.x-scale,
.y-min * $.y-scale,
.x-max * $.x-scale,
.y-max * $.y-scale,
]
}
else {
Array
}
}

method bounding-box {
self.Array does role {
method x-min { self[0] }
method y-min { self[1] }
method x-max { self[2] }
method y-max { self[3] }
method bbox is also<bounding-box FontBBox> {
with $!face.bbox {
Font::FreeType::BBox.new(
:bbox[
.x-min * $.x-scale,
.y-min * $.y-scale,
.x-max * $.x-scale,
.y-max * $.y-scale,
],
);
}
else {
Font::FreeType::BBox;
}
}

Expand Down

0 comments on commit f7e4adb

Please sign in to comment.