use fontdue layout

This commit is contained in:
griffi-gh 2023-12-02 19:27:21 +01:00
parent f19031662f
commit 6b33e9cdc9
2 changed files with 21 additions and 8 deletions

View file

@ -1,6 +1,7 @@
use crate::{IfModified, text::{TextRenderer, FontHandle}}; use crate::{IfModified, text::{TextRenderer, FontHandle}};
use std::borrow::Cow; use std::borrow::Cow;
use fontdue::layout::{Layout, CoordinateSystem, TextStyle};
use glam::{Vec2, Vec4, vec2}; use glam::{Vec2, Vec4, vec2};
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
@ -155,11 +156,19 @@ impl UiDrawPlan {
]); ]);
}, },
UiDrawCommand::Text { position, size, color, text, font } => { UiDrawCommand::Text { position, size, color, text, font } => {
let mut rpos_x = 0.; //XXX: should we be doing this every time?
for char in text.chars() { 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 vidx = swapper.current().vertices.len() as u32;
let glyph = tr.glyph(*font, char, *size); let glyph = tr.glyph(*font, layout_glyph.parent, layout_glyph.key.px as u8);
rpos_x += glyph.metrics.advance_width;//glyph.metrics.advance_width; //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]); swapper.current_mut().indices.extend([vidx, vidx + 1, vidx + 2, vidx, vidx + 2, vidx + 3]);
let p0x = glyph.position.x as f32 / 1024.; let p0x = glyph.position.x as f32 / 1024.;
let p1x = (glyph.position.x + glyph.size.x as i32) 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.; let p1y = (glyph.position.y + glyph.size.y as i32) as f32 / 1024.;
swapper.current_mut().vertices.extend([ swapper.current_mut().vertices.extend([
UiVertex { UiVertex {
position: *position + vec2(rpos_x, 0.0), position: *position + vec2(layout_glyph.x, layout_glyph.y),
color: *color, color: *color,
uv: vec2(p0x, p0y), uv: vec2(p0x, p0y),
}, },
UiVertex { 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, color: *color,
uv: vec2(p1x, p0y), uv: vec2(p1x, p0y),
}, },
UiVertex { 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, color: *color,
uv: vec2(p1x, p1y), uv: vec2(p1x, p1y),
}, },
UiVertex { 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, color: *color,
uv: vec2(p0x, p1y), uv: vec2(p0x, p1y),
}, },

View file

@ -37,6 +37,10 @@ impl TextRenderer {
pub fn glyph(&mut self, font_handle: FontHandle, character: char, size: u8) -> Arc<GlyphCacheEntry> { pub fn glyph(&mut self, font_handle: FontHandle, character: char, size: u8) -> Arc<GlyphCacheEntry> {
self.ftm.glyph(&self.fm, font_handle, character, size) 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 { impl Default for TextRenderer {