Skip to content

Commit

Permalink
rebase BBox on Array
Browse files Browse the repository at this point in the history
  • Loading branch information
dwarring committed Dec 13, 2023
1 parent 73a4357 commit d14c27f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 36 deletions.
52 changes: 24 additions & 28 deletions lib/Font/FreeType/BBox.rakumod
Original file line number Diff line number Diff line change
@@ -1,44 +1,40 @@
#| A generic font or glyph bounding box
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);
has Numeric $.x-scale = 1 / Dot6;
has Numeric $.y-scale = 1 / Dot6;

method x-min { $!x-min * $!x-scale }
method y-min { $!y-min * $!y-scale }
method x-max { $!x-max * $!x-scale }
method y-max { $!y-max * $!y-scale }
method width { ($!x-max - $!x-min) * $!x-scale }
method height { ($!y-max - $!y-min) * $!y-scale }
multi method AT-POS(0) { self.x-min }
multi method AT-POS(1) { self.y-min }
multi method AT-POS(2) { self.x-max }
multi method AT-POS(3) { self.y-max }

multi submethod TWEAK(FT_BBox:D :bbox($_)!, |c) {
$!x-min = .x-min;
$!y-min = .y-min;
$!x-max = .x-max;
$!y-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] }

multi method new(FT_BBox:D :bbox($_)!, |c) {
my \bbox = self.bless: | c;
bbox[0] = .x-min * bbox.x-scale;
bbox[1] = .y-min * bbox.y-scale;
bbox[2] = .x-max * bbox.x-scale;
bbox[3] = .y-max * bbox.y-scale;
bbox;
}

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

method bounding-box {
[floor(self.x-min), floor(self.y-min),
ceiling(self.x-max), ceiling(self.y-max)]
}
multi method new(|c) { fail }
}

=begin pod
Expand Down
2 changes: 1 addition & 1 deletion lib/Font/FreeType/Outline.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class Font::FreeType::Outline {
$shape;
}

method bbox returns Font::FreeType::BBox:D handles<bounding-box> {
method bbox returns Font::FreeType::BBox:D is also<bounding-box> {
my FT_BBox $bbox .= new;
ft-try { $!raw.FT_Outline_Get_BBox($bbox); };
Font::FreeType::BBox.new: :$bbox;
Expand Down
14 changes: 7 additions & 7 deletions t/10metrics_verasans.t
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ sub scaled-metrics-tests($scaled-metrics) {
is $scaled-metrics.max-advance, 6.0, '.max-advance';
is-approx $scaled-metrics.underline-position, -1.664063, '.underline-position';
is-approx $scaled-metrics.underline-thickness, 0.837891, '.underline-thickness';
my $bbox = $scaled-metrics.bounding-box;
my @bbox := $scaled-metrics.bounding-box;
enum <x-min y-min x-max y-max>;
is-approx $bbox[x-min], -2.197266, '@bbox[x-min]';
is-approx $bbox[y-min], -2.830078, '@bbox[y-min]';
is-approx $bbox[x-max], 15.445313, '@bbox[x-max]';
is-approx $bbox[y-max], 11.138672, '@bbox[y-max]';
is-approx @bbox[x-min], -2.197266, '@bbox[x-min]';
is-approx @bbox[y-min], -2.830078, '@bbox[y-min]';
is-approx @bbox[x-max], 15.445313, '@bbox[x-max]';
is-approx @bbox[y-max], 11.138672, '@bbox[y-max]';
}

subtest 'scaled-metrics', {
Expand Down Expand Up @@ -180,7 +180,7 @@ my %glyph-metrics = (
'g' => { name => 'g', advance => 1300,
LBearing => 113, RBearing => 186 },
'|' => { name => 'bar', advance => 690,
LBearing => 260, RBearing => 260, Height => 2048, BBox => [260, -483, 430, 1565] },
LBearing => 260, RBearing => 260, Height => 2048, BBox => [260.0, -483.0, 430.0, 1565.0] },
);

# 5*2 tests.
Expand All @@ -200,7 +200,7 @@ $vera.for-glyphs: $chars, -> $glyph {
"width of glyph '$char'";
is $glyph.height, $_, "height of '$char'"
with .<Height>;
is-deeply $glyph.outline.bounding-box, $_, "bbox of '$char'"
is-deeply $glyph.outline.bounding-box.Array, $_, "bbox of '$char'"
with .<BBox>;

}
Expand Down

0 comments on commit d14c27f

Please sign in to comment.