Skip to content

Commit

Permalink
Expose slice and vec of raw data (#98)
Browse files Browse the repository at this point in the history
* Expose slice and vec of raw data

* Fix formatting

* Restore methods directly on FontVec

* Format

* Remove methods from Font, add a new trait and just use as_slice

* Fix merged trait usage

* Use a default and implement per type data impls

* Fix docs

* Restore blank

* Fixes and feedback

* Fix fmt

---------

Co-authored-by: Alex Parlett <alexparlett@Alexs-MacBook-Air.local>
  • Loading branch information
alexparlett and Alex Parlett authored Apr 3, 2024
1 parent a255264 commit 675ed3e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
22 changes: 22 additions & 0 deletions glyph/src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,23 @@ pub trait Font {
scale: scale.into(),
}
}

/// Extracts a slice containing the data passed into e.g. [`FontArc::try_from_slice`].
///
/// # Example
/// ```
/// # use ab_glyph::*;
/// # fn main() -> Result<(), InvalidFont> {
/// # let owned_font_data = include_bytes!("../../dev/fonts/Exo2-Light.otf");
/// let font = FontArc::try_from_slice(owned_font_data)?;
/// assert_eq!(font.font_data(), owned_font_data);
/// # Ok(()) }
/// ```
#[inline]
fn font_data(&self) -> &[u8] {
// panic impl prevents this method from breaking external Font impls
unimplemented!()
}
}

impl<F: Font> Font for &F {
Expand Down Expand Up @@ -315,4 +332,9 @@ impl<F: Font> Font for &F {
fn glyph_raster_image2(&self, id: GlyphId, size: u16) -> Option<v2::GlyphImage> {
(*self).glyph_raster_image2(id, size)
}

#[inline]
fn font_data(&self) -> &[u8] {
(*self).font_data()
}
}
5 changes: 5 additions & 0 deletions glyph/src/font_arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ impl Font for FontArc {
fn glyph_raster_image2(&self, id: GlyphId, size: u16) -> Option<v2::GlyphImage> {
self.0.glyph_raster_image2(id, size)
}

#[inline]
fn font_data(&self) -> &[u8] {
self.0.font_data()
}
}

impl From<FontVec> for FontArc {
Expand Down
6 changes: 6 additions & 0 deletions glyph/src/ttfp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ impl FontVec {
/// assert_eq!(font.into_vec(), font_data_clone);
/// # Ok(()) }
/// ```
#[inline]
pub fn into_vec(self) -> Vec<u8> {
self.0.face.into_vec()
}
Expand Down Expand Up @@ -341,6 +342,11 @@ macro_rules! impl_font {
},
})
}

#[inline]
fn font_data(&self) -> &[u8] {
self.0.face.as_face_ref().raw_face().data
}
}
};
}
Expand Down

0 comments on commit 675ed3e

Please sign in to comment.