text update for more code reuse + much better caching

This commit is contained in:
2025-09-29 13:45:48 -04:00
parent c98a43f94d
commit 628840d5cd
9 changed files with 319 additions and 273 deletions

View File

@@ -2,7 +2,7 @@ use cosmic_text::{Attrs, AttrsList, Buffer, Family, FontSystem, Metrics, SwashCa
use image::{Rgba, RgbaImage};
use crate::{
layout::{TextureHandle, Textures, UiColor, Vec2},
layout::{Align, TextureHandle, Textures, UiColor, Vec2},
util::HashMap,
};
@@ -27,6 +27,8 @@ pub struct TextAttrs {
pub line_height: f32,
pub family: Family<'static>,
pub wrap: bool,
/// inner alignment of text region (within where its drawn)
pub align: Align,
}
impl TextAttrs {
@@ -56,6 +58,7 @@ impl Default for TextAttrs {
line_height: size * 1.2,
family: Family::SansSerif,
wrap: false,
align: Align::Center,
}
}
}
@@ -66,7 +69,11 @@ impl TextData {
buffer: &mut TextBuffer,
attrs: &TextAttrs,
textures: &mut Textures,
) -> (TextureHandle, TextOffset) {
) -> TextTexture {
// TODO: either this or the layout stuff (or both) is super slow,
// should probably do texture packing and things if possible.
// very visible if you add just a couple of wrapping texts and resize window
// should also be timed to figure out exactly what points need to be sped up
let mut pixels = HashMap::default();
let mut min_x = 0;
let mut min_y = 0;
@@ -113,22 +120,23 @@ impl TextData {
let y = (y - min_y) as u32;
image.put_pixel(x, y, color);
}
let offset = TextOffset {
TextTexture {
handle: textures.add(image),
top_left: Vec2::new(min_x as f32, min_y as f32),
bot_right: Vec2::new(max_width - max_x as f32, height - max_y as f32),
};
(textures.add(image), offset)
}
}
}
#[derive(Clone, Copy)]
pub struct TextOffset {
#[derive(Clone)]
pub struct TextTexture {
pub handle: TextureHandle,
pub top_left: Vec2,
pub bot_right: Vec2,
}
impl TextOffset {
pub fn size(&self, handle: &TextureHandle) -> Vec2 {
handle.size() - self.top_left + self.bot_right
impl TextTexture {
pub fn size(&self) -> Vec2 {
self.handle.size() - self.top_left + self.bot_right
}
}