From 64b7849c66abfef13ccb771328b0e451fa764f96 Mon Sep 17 00:00:00 2001 From: Erin Date: Tue, 15 Aug 2017 14:20:31 -0500 Subject: [PATCH] textium: tweak GlyphMetadata, remove FontPacker, FontTexture --- textium/src/lib.rs | 79 +++++++----------------------------------------------- 1 file changed, 9 insertions(+), 70 deletions(-) diff --git a/textium/src/lib.rs b/textium/src/lib.rs index bfc69a5..f0d5e88 100644 --- a/textium/src/lib.rs +++ b/textium/src/lib.rs @@ -21,23 +21,19 @@ pub use geometry::Rect; /// Information used to render a glyph in a font. #[derive(Debug)] pub struct GlyphMetadata { - /// Coordinates of the top left of this glyph in the font's texture - texture_coord: (f32, f32), - - /// Width and height of the glyph's texture. - texture_size: (f32, f32), - - /// Size of the glyph, in ems. - em_size: (f32, f32), + /// Bounding box in the packed texture + bounding_box: Rect, + /// Scale that the character was rendered at + scale: f32, /// Distance between the bottom of the glyph and the baseline, in ems. - baseline_distance: f32, + // baseline_distance: f32, - /// Left padding on the glyph, in ems. - left_padding: f32, + /// Distance to advance in layout after this character + advance_width: f32, - /// Left padding on the glyph, in ems. - right_padding: f32, + /// Horizontal offset between glyph origin and leftmost point of the glyph + left_bearing: f32, } pub struct FontAtlas { @@ -52,60 +48,3 @@ pub struct FontTexture { atlas: FontAtlas, } -pub trait FontPacker { - fn pack(&self, font: Font, characters: C, font_size: u32) -> Result<(Texture2dData, HashMap), Error> - where C: Iterator; -} - -pub struct NaiveFontPacker; -impl FontPacker for NaiveFontPacker { - fn pack(&self, font: Font, characters: C, font_size: u32) -> Result<(Texture2dData, HashMap), Error> - where C: Iterator - { - // Margin around each character in the texture - const MARGIN: u32 = 2; - - let invalid_char_width = font_size / 2; - - let estimated_glyphs = characters.size_hint().1.unwrap_or(0); - - let mut texture_data: Vec = Vec::with_capacity( - estimated_glyphs * (font_size as usize).pow(2)); - - let texture_width = get_nearest_pow2(std::cmp::max(font_size * 2, - (((estimated_glyphs as u32) * font_size.pow(2)) as f32).sqrt() as u32)); - - let mut cursor = (0u32, 0u32); - let mut skip_rows = 0u32; - - // let glyph_metadata = characters.map(|character| { - // let scaled_glyph = font.glyph(character) - // .ok_or_else(|| Error::NotAGlyph(character))? - // .scaled(rusttype::Scale::uniform(font_size as f32)); - - // // let h_metrics = - // }).collect::, Error>>()?; - - - Err(Error::PackingError) - } -} - -// TODO: make this an actual From once TryFrom is stabilized. -impl FontTexture { - /// Load a font from a Rusttype font into a Textium font texture, and upload it to Glium. - pub fn from_font(font: Font, font_size: u32, facade: &Facade, packer: T) -> Result - where T: FontPacker - { - let characters: Vec = (0..255).filter_map(|c| ::std::char::from_u32(c)).collect(); - - let (texture_data, meta) = packer.pack(font, characters.into_iter(), font_size)?; - - let texture = Texture2d::new(facade, &texture_data)?; - - Ok(FontTexture { - texture: texture, - glyph_metadata: meta, - }) - } -}