Skip to content

Commit

Permalink
document BBox object
Browse files Browse the repository at this point in the history
  • Loading branch information
dwarring committed Dec 15, 2023
1 parent 48276b9 commit 2782838
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ $(DocLinker) :
Pod-To-Markdown-installed :
@raku -M Pod::To::Markdown -c

doc : $(DocLinker) Pod-To-Markdown-installed docs/index.md docs/Font/FreeType.md docs/Font/FreeType/BitMap.md docs/Font/FreeType/CharMap.md docs/Font/FreeType/Face.md docs/Font/FreeType/Glyph.md docs/Font/FreeType/GlyphImage.md docs/Font/FreeType/Outline.md docs/Font/FreeType/SizeMetrics.md docs/Font/FreeType/NamedInfo.md docs/Font/FreeType/Raw.md docs/Font/FreeType/Raw/Defs.md docs/Font/FreeType/Raw/TT_Sfnt.md
doc : $(DocLinker) Pod-To-Markdown-installed docs/index.md docs/Font/FreeType.md docs/Font/FreeType/BBox.md docs/Font/FreeType/BitMap.md docs/Font/FreeType/CharMap.md docs/Font/FreeType/Face.md docs/Font/FreeType/Glyph.md docs/Font/FreeType/GlyphImage.md docs/Font/FreeType/Outline.md docs/Font/FreeType/SizeMetrics.md docs/Font/FreeType/NamedInfo.md docs/Font/FreeType/Raw.md docs/Font/FreeType/Raw/Defs.md docs/Font/FreeType/Raw/TT_Sfnt.md

test : all
@prove -e"raku -I ." -j $(TEST_JOBS) t
Expand Down
54 changes: 54 additions & 0 deletions docs/Font/FreeType/BBox.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
[[Raku PDF Project]](https://pdf-raku.github.io)
/ [[Font-FreeType Module]](https://pdf-raku.github.io/Font-FreeType-raku)
/ [Font::FreeType](https://pdf-raku.github.io/Font-FreeType-raku/Font/FreeType)
:: [BBox](https://pdf-raku.github.io/Font-FreeType-raku/Font/FreeType/BBox)

class Font::FreeType::BBox
--------------------------

A generic font or glyph bounding box

Synopsis
--------

```raku
use Font::FreeType;
use Font::FreeType::BBox;
use Font::FreeType::Face;
use Font::FreeType::Glyph;

my Font::FreeType $freetype .= new;
my Font::FreeType::Face $vera = $freetype.face('t/fonts/Vera.ttf');
constant FontSize = 12;
# get the font bounding box
$vera.set-font-size(FontSize);

my Font::FreeType::BBox $bbox = $vera.bbox;
say 'font "%s" (%s) %dpt has bbox [%.1f, %.1f, %.1f, %.1f]'.sprintf($vera.family-name, $vera.style-name, FontSize, $bbox.x-min, $bbox.y-min, $bbox.x-max, $bbox.y-max);
# get the bounding box for an individual glyph
$vera.for-glyphs: "X", -> Font::FreeType::Glyph $glyph {
my Font::FreeType::BBox $bbox = $glyph.bounding-box;
say 'glyph "%s" %dpt has bbox [%.1f, %.1f, %.1f, %.1f]'.sprintf($glyph.Str, FontSize, |$bbox);
}
```

Description
-----------

This object is a subclass of Array. It represents a bounding box as 4 elements with accessor aliases `x-min`, `y-min`, `x-max` and `y-max`.

Methods
-------

`x-min`, `y-min`, `x-max`, `y-max`

The bottom-left and top right coordinates of the bounding box. These can also be accessed as a four element array.

`width`, `height`

Aliases for `x-max - x-min` and `y-max - y-min`.

`x-scale`, `y-scale`

The bounding `x` and `y` scale of the bounding box.

6 changes: 3 additions & 3 deletions docs/Font/FreeType/Face.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use Font::FreeType;
use Font::FreeType::Face;

my Font::FreeType $freetype .= new;
my Font::Freetype::face $vera = $freetype.face('Vera.ttf');
my Font::Freetype::Face $vera = $freetype.face('Vera.ttf');
```

Description
Expand Down Expand Up @@ -304,9 +304,9 @@ The current active [Font::FreeType::CharMap](https://pdf-raku.github.io/Font-Fre

An array of the available [Font::FreeType::CharMap](https://pdf-raku.github.io/Font-FreeType-raku/Font/FreeType/CharMap) objects for the face.

### bounding-box()
### bounding-box() [or bbox()]

The outline's bounding box returned as a 4 element array: `($x-min, $y-min, $x-max, $y-max)`.
The outline's bounding box returned as a [Font::FreeType::BBox](https://pdf-raku.github.io/Font-FreeType-raku/Font/FreeType/BBox) array: `($x-min, $y-min, $x-max, $y-max)`.

### raw()

Expand Down
36 changes: 26 additions & 10 deletions lib/Font/FreeType/BBox.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,43 @@ class Font::FreeType::BBox
=begin code :lang<raku>
use Font::FreeType;
use Font::FreeType::Face;
use Font::FreeType::BBox;
use Font::FreeType::Face;
use Font::FreeType::Glyph;
my Font::FreeType $freetype .= new;
my Font::Freetype::Face $vera = $freetype.face('Vera.ttf');
my Font::FreeType::Face $vera = $freetype.face('t/fonts/Vera.ttf');
constant FontSize = 12;
# get the font bounding box
my Font::FreeType::BBox $bbox = $vera.bbox;
say $bbox.x-min;
say $bbox.x-max;
say $bbox.width;
say $bbox.height;
$vera.set-font-size(FontSize);
my Font::FreeType::BBox $bbox = $vera.bbox;
say 'font "%s" (%s) %dpt has bbox [%.1f, %.1f, %.1f, %.1f]'.sprintf($vera.family-name, $vera.style-name, FontSize, $bbox.x-min, $bbox.y-min, $bbox.x-max, $bbox.y-max);
# get the bounding box for an individual glyph
$vera.for-glyphs: "X", -> $gslot {
$vera.for-glyphs: "X", -> Font::FreeType::Glyph $glyph {
my Font::FreeType::BBox $bbox = $glyph.bounding-box;
say 'glyph "%s" %dpt has bbox [%.1f, %.1f, %.1f, %.1f]'.sprintf($glyph.Str, FontSize, |$bbox);
}
=end code
=head2 Description
This object is a subclass of Array. It represents a bounding box as 4 elements
with accessor aliases `x-min`, `y-min`, `x-max` and `y-max`.
=head2 Methods
`x-min`, `y-min`, `x-max`, `y-max`
The bottom-left and top right coordinates of the bounding box. These
can also be accessed as a four element array.
`width`, `height`
Aliases for `x-max - x-min` and `y-max - y-min`.
`x-scale`, `y-scale`
The bounding `x` and `y` scale of the bounding box.
=end pod
2 changes: 1 addition & 1 deletion lib/Font/FreeType/Glyph.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Font::FreeType::Glyph {
method height returns Rat:D { $.metrics.height / $!y-scale }
method format returns UInt:D { FT_GLYPH_FORMAT($!raw.format) }

method glyph-image handles<bitmap outline decompose> returns Font::FreeType::GlyphImage:D {
method glyph-image handles<bitmap decompose> returns Font::FreeType::GlyphImage:D {
my $top = $!raw.bitmap-top;
my $left = $!raw.bitmap-left;
Font::FreeType::GlyphImage.new: :$.face, :glyph($!raw), :$left, :$top, :$.char-code, :$.index, :$.stat;
Expand Down
2 changes: 1 addition & 1 deletion lib/Font/FreeType/_Glyph.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ method is-outline {
.format == FT_GLYPH_FORMAT_OUTLINE with $.raw;
}

method outline handles<decompose> returns Font::FreeType::Outline:D {
method outline handles<decompose bbox bounding-box> returns Font::FreeType::Outline:D {
die "not an outline glyph"
unless self.is-outline;
my FT_Outline:D $outline = $.raw.outline;
Expand Down
15 changes: 9 additions & 6 deletions t/10metrics_verasans.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

use v6;
use Test;
plan 2714;
plan 2715;
use Font::FreeType;
use Font::FreeType::SizeMetrics;
use Font::FreeType::Glyph;
use Font::FreeType::Raw::Defs;

# Load the Vera Sans face.
Expand Down Expand Up @@ -167,7 +168,7 @@ is $i, +@character-list, "we aren't missing any glyphs";

$i = 0;
$vera.forall-char-images: &check-glyph-char;
is $i, +@character-list, "we aren't missing any glyphs";
is $i, +@character-list, "we aren't missing any glyph images";

# Test metrics on some particlar glyphs.
my %glyph-metrics = (
Expand All @@ -185,9 +186,9 @@ my %glyph-metrics = (

# 5*2 tests.
my $chars = %glyph-metrics.keys.sort.join;
$vera.for-glyphs: $chars, -> $glyph {
$vera.for-glyphs: $chars, -> Font::FreeType::Glyph $glyph {
my $char = $glyph.Str;
with %glyph-metrics{$char} {
given %glyph-metrics{$char} {
is $glyph.name, .<name>,
"name of glyph '$char'";
is $glyph.horizontal-advance, .<advance>,
Expand All @@ -200,8 +201,10 @@ $vera.for-glyphs: $chars, -> $glyph {
"width of glyph '$char'";
is $glyph.height, $_, "height of '$char'"
with .<Height>;
is-deeply $glyph.outline.bounding-box.Array, $_, "bbox of '$char'"
with .<BBox>;
with .<BBox> {
is-deeply $glyph.outline.bounding-box.Array, $_, "bbox of '$char'";
is-deeply $glyph.glyph-image.outline.bounding-box.Array, $_, "outline.bbox of '$char'";
}

}
}
Expand Down

0 comments on commit 2782838

Please sign in to comment.