Skip to content

Commit

Permalink
Add method for getting the glyph bounding box from the glyf program d…
Browse files Browse the repository at this point in the history
…irectly
  • Loading branch information
LaurenzV committed Jul 11, 2024
1 parent a486aa7 commit 949d179
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/tables/glyf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,25 @@ impl<'a> Table<'a> {
outline_impl(self.loca_table, self.data, glyph_data, 0, &mut b)?
}

/// The bounding box of the glyph. Unlike the `outline` method, this method does not
/// calculate the bounding box manually by outlining the glyph, but instead uses the
/// bounding box in the `glyf` program. As a result, this method will be much faster,
/// but the bounding box could be more inaccurate.
#[inline]
pub fn bbox(&self, glyph_id: GlyphId) -> Option<Rect> {
let glyph_data = self.get(glyph_id)?;

let mut s = Stream::new(glyph_data);
// number of contours
let _ = s.read::<i16>()?;
Some(Rect {
x_min: s.read::<i16>()?,
y_min: s.read::<i16>()?,
x_max: s.read::<i16>()?,
y_max: s.read::<i16>()?,
})
}

#[inline]
pub(crate) fn get(&self, glyph_id: GlyphId) -> Option<&'a [u8]> {
let range = self.loca_table.glyph_range(glyph_id)?;
Expand Down

0 comments on commit 949d179

Please sign in to comment.