From fe3542a28d5405e80d94003d86ad68bb049b266f Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Thu, 17 Jan 2019 07:07:02 -0600 Subject: [PATCH] Allow floating point font sizes --- emigui/src/font.rs | 20 ++++++++++---------- emigui/src/fonts.rs | 7 +++++-- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/emigui/src/font.rs b/emigui/src/font.rs index 72b04c06..f7a2b759 100644 --- a/emigui/src/font.rs +++ b/emigui/src/font.rs @@ -57,19 +57,19 @@ const LAST_ASCII: usize = 126; pub struct Font { font: rusttype::Font<'static>, /// Maximum character height - scale: usize, + scale: f32, /// NUM_CHARS big glyph_infos: Vec, atlas: Arc>, } impl Font { - pub fn new(atlas: Arc>, font_data: &'static [u8], scale: usize) -> Font { + pub fn new(atlas: Arc>, font_data: &'static [u8], scale: f32) -> Font { let font = rusttype::Font::from_bytes(font_data).expect("Error constructing Font"); // println!( // "font.v_metrics: {:?}", - // font.v_metrics(Scale::uniform(scale as f32)) + // font.v_metrics(Scale::uniform(scale)) // ); let glyphs: Vec<_> = Self::supported_characters() @@ -81,7 +81,7 @@ impl Font { "Failed to find a glyph for the character '{}'", c ); - let glyph = glyph.scaled(Scale::uniform(scale as f32)); + let glyph = glyph.scaled(Scale::uniform(scale)); glyph.positioned(point(0.0, 0.0)) }) .collect(); @@ -141,7 +141,7 @@ impl Font { } pub fn line_spacing(&self) -> f32 { - self.scale as f32 + self.scale } pub fn supported_characters() -> impl Iterator { @@ -168,7 +168,7 @@ impl Font { /// Returns the a single line of characters separated into words pub fn layout_single_line(&self, text: &str) -> Vec { - let scale = Scale::uniform(self.scale as f32); + let scale = Scale::uniform(self.scale); let mut current_fragment = TextFragment { x_offsets: vec![0.0], @@ -276,8 +276,8 @@ impl Font { let texture_mut = atlas_lock.texture_mut(); let max_width = 160; - let scale = Scale::uniform(self.scale as f32); - let mut pixel_rows = vec![vec![0; max_width]; self.scale]; + let scale = Scale::uniform(self.scale); + let mut pixel_rows = vec![vec![0; max_width]; self.scale.ceil() as usize]; let mut cursor_x = 0.0; let cursor_y = 0; let mut last_glyph_id = None; @@ -291,7 +291,7 @@ impl Font { for row in pixel_rows { println!("{}", as_ascii(&row)); } - pixel_rows = vec![vec![0; max_width]; self.scale]; + pixel_rows = vec![vec![0; max_width]; self.scale.ceil() as usize]; cursor_x = 0.0; } if let Some(uv) = glyph.uv { @@ -343,7 +343,7 @@ mod tests { let atlas = TextureAtlas::new(128, 8); let atlas = Arc::new(Mutex::new(atlas)); let font_data = include_bytes!("../fonts/Roboto-Regular.ttf"); - let font = Font::new(atlas, font_data, 13); + let font = Font::new(atlas, font_data, 13.0); font.debug_print_all_chars(); } } diff --git a/emigui/src/fonts.rs b/emigui/src/fonts.rs index cb4cbfcb..88f5d061 100644 --- a/emigui/src/fonts.rs +++ b/emigui/src/fonts.rs @@ -38,11 +38,14 @@ impl Fonts { let typeface_data = include_bytes!("../fonts/Roboto-Regular.ttf"); let mut fonts = BTreeMap::new(); - fonts.insert(TextStyle::Body, Font::new(atlas.clone(), typeface_data, 20)); + fonts.insert( + TextStyle::Body, + Font::new(atlas.clone(), typeface_data, 20.0), + ); fonts.insert(TextStyle::Button, fonts[&TextStyle::Body].clone()); fonts.insert( TextStyle::Heading, - Font::new(atlas.clone(), typeface_data, 30), + Font::new(atlas.clone(), typeface_data, 30.0), ); let texture = atlas.lock().unwrap().clone().texture().clone();