From c9ca85900d8308f7c3d6af7e9ce22af6b0d6ca15 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Sat, 2 Dec 2023 19:27:21 +0100 Subject: [PATCH] use fontdue layout --- kubi-ui/src/draw.rs | 25 +++++++++++++++++-------- kubi-ui/src/text.rs | 4 ++++ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/kubi-ui/src/draw.rs b/kubi-ui/src/draw.rs index 8386738..e220b3d 100644 --- a/kubi-ui/src/draw.rs +++ b/kubi-ui/src/draw.rs @@ -1,6 +1,7 @@ use crate::{IfModified, text::{TextRenderer, FontHandle}}; use std::borrow::Cow; +use fontdue::layout::{Layout, CoordinateSystem, TextStyle}; use glam::{Vec2, Vec4, vec2}; #[derive(Clone, Debug, PartialEq)] @@ -155,11 +156,19 @@ impl UiDrawPlan { ]); }, UiDrawCommand::Text { position, size, color, text, font } => { - let mut rpos_x = 0.; - for char in text.chars() { + //XXX: should we be doing this every time? + let mut layout = Layout::new(CoordinateSystem::PositiveYDown); + layout.append( + &[tr.internal_font(*font)], + &TextStyle::new(&text, *size as f32, 0) + ); + let glyphs = layout.glyphs(); + + //let mut rpos_x = 0.; + for layout_glyph in glyphs { let vidx = swapper.current().vertices.len() as u32; - let glyph = tr.glyph(*font, char, *size); - rpos_x += glyph.metrics.advance_width;//glyph.metrics.advance_width; + let glyph = tr.glyph(*font, layout_glyph.parent, layout_glyph.key.px as u8); + //rpos_x += glyph.metrics.advance_width;//glyph.metrics.advance_width; swapper.current_mut().indices.extend([vidx, vidx + 1, vidx + 2, vidx, vidx + 2, vidx + 3]); let p0x = glyph.position.x as f32 / 1024.; let p1x = (glyph.position.x + glyph.size.x as i32) as f32 / 1024.; @@ -167,22 +176,22 @@ impl UiDrawPlan { let p1y = (glyph.position.y + glyph.size.y as i32) as f32 / 1024.; swapper.current_mut().vertices.extend([ UiVertex { - position: *position + vec2(rpos_x, 0.0), + position: *position + vec2(layout_glyph.x, layout_glyph.y), color: *color, uv: vec2(p0x, p0y), }, UiVertex { - position: *position + vec2(rpos_x + glyph.metrics.width as f32, 0.0), + position: *position + vec2(layout_glyph.x + glyph.metrics.width as f32, layout_glyph.y), color: *color, uv: vec2(p1x, p0y), }, UiVertex { - position: *position + vec2(rpos_x + glyph.metrics.width as f32, glyph.metrics.height as f32), + position: *position + vec2(layout_glyph.x + glyph.metrics.width as f32, layout_glyph.y + glyph.metrics.height as f32), color: *color, uv: vec2(p1x, p1y), }, UiVertex { - position: *position + vec2(rpos_x, glyph.metrics.height as f32), + position: *position + vec2(layout_glyph.x, layout_glyph.y + glyph.metrics.height as f32), color: *color, uv: vec2(p0x, p1y), }, diff --git a/kubi-ui/src/text.rs b/kubi-ui/src/text.rs index c0df6fa..5e99e08 100644 --- a/kubi-ui/src/text.rs +++ b/kubi-ui/src/text.rs @@ -37,6 +37,10 @@ impl TextRenderer { pub fn glyph(&mut self, font_handle: FontHandle, character: char, size: u8) -> Arc { self.ftm.glyph(&self.fm, font_handle, character, size) } + + pub(crate) fn internal_font(&self, handle: FontHandle) -> &Font { + self.fm.get(handle).unwrap() + } } impl Default for TextRenderer {