snap text on shader

This commit is contained in:
2025-08-23 22:16:00 -04:00
parent 5ce6fca275
commit 50ccf7393d
10 changed files with 101 additions and 64 deletions

View File

@@ -3,26 +3,48 @@ use image::{Rgba, RgbaImage};
use crate::{
HashMap,
layout::{TextureHandle, Textures},
layout::{TextureHandle, Textures, UiColor},
};
pub(crate) struct TextData {
font_system: FontSystem,
cache: SwashCache,
swash_cache: SwashCache,
}
impl Default for TextData {
fn default() -> Self {
Self {
font_system: FontSystem::new(),
cache: SwashCache::new(),
swash_cache: SwashCache::new(),
}
}
}
pub struct TextAttrs {
pub color: UiColor,
pub size: f32,
pub line_height: f32,
}
impl Default for TextAttrs {
fn default() -> Self {
let size = 14.0;
Self {
color: UiColor::WHITE,
size,
line_height: size * 1.2,
}
}
}
impl TextData {
pub fn draw(&mut self, content: &str, textures: &mut Textures) -> TextureHandle {
let metrics = Metrics::new(30.0, 30.0);
pub fn draw(
&mut self,
content: &str,
attrs: &TextAttrs,
textures: &mut Textures,
) -> TextureHandle {
let metrics = Metrics::new(attrs.size, attrs.line_height);
let mut buffer = Buffer::new(&mut self.font_system, metrics);
let mut buffer = buffer.borrow_with(&mut self.font_system);
buffer.set_text(content, &Attrs::new(), Shaping::Advanced);
@@ -33,9 +55,10 @@ impl TextData {
let mut min_y = 0;
let mut max_x = 0;
let mut max_y = 0;
let c = attrs.color;
buffer.draw(
&mut self.cache,
cosmic_text::Color::rgb(0xff, 0xff, 0xff),
&mut self.swash_cache,
cosmic_text::Color::rgba(c.r, c.g, c.b, c.a),
|x, y, _, _, color| {
min_x = min_x.min(x);
min_y = min_y.min(y);