text fixing

This commit is contained in:
Jonas H
2026-03-29 10:47:10 +02:00
parent 75a046d92a
commit 909ae8612a
6 changed files with 172 additions and 106 deletions

View File

@@ -10,7 +10,7 @@ const ATLAS_ROWS: u32 = 8;
const FIRST_CHAR: u32 = 32;
const LAST_CHAR: u32 = 126;
const NUM_GLYPHS: u32 = LAST_CHAR - FIRST_CHAR + 1;
const FONT_PX: f32 = 16.0;
const FONT_PX: f32 = 124.0;
struct GlyphMetrics
{
@@ -58,7 +58,10 @@ impl FontAtlas
let cell_y = row * cell_h;
glyphs.push(GlyphMetrics {
uv_min: [cell_x as f32 / atlas_w as f32, cell_y as f32 / atlas_h as f32],
uv_min: [
cell_x as f32 / atlas_w as f32,
cell_y as f32 / atlas_h as f32,
],
uv_max: [
(cell_x + cell_w) as f32 / atlas_w as f32,
(cell_y + cell_h) as f32 / atlas_h as f32,
@@ -129,8 +132,8 @@ impl FontAtlas
label: Some("Font Atlas Sampler"),
address_mode_u: wgpu::AddressMode::ClampToEdge,
address_mode_v: wgpu::AddressMode::ClampToEdge,
mag_filter: wgpu::FilterMode::Nearest,
min_filter: wgpu::FilterMode::Nearest,
mag_filter: wgpu::FilterMode::Linear,
min_filter: wgpu::FilterMode::Linear,
..Default::default()
});
@@ -156,6 +159,24 @@ impl FontAtlas
/// `inner_top_y` y-offset (up-axis) of the top edge of the text area
/// `char_world_h` world-space height of one character cell
/// `line_spacing` extra vertical gap between lines in world units
/// Returns `(n_lines, max_chars_in_any_line)` for the given text wrapped at `inner_half_w`.
pub fn measure_text(&self, text: &str, char_world_h: f32, inner_half_w: f32) -> (usize, usize)
{
let char_w = char_world_h * self.aspect();
let chars_per_line = ((inner_half_w * 2.0) / char_w).floor() as usize;
if chars_per_line == 0
{
return (1, 0);
}
let lines = word_wrap(text, chars_per_line);
let n_lines = lines.len().max(1);
let max_chars = lines.iter().map(|l| l.chars().count()).max().unwrap_or(0);
(n_lines, max_chars)
}
pub fn build_bubble_text(
&self,
text: &str,
@@ -214,12 +235,18 @@ impl FontAtlas
let bl = centre - right * half_char_w - up * half_char_h;
verts.extend_from_slice(&[
TextVertex { position: tl.to_array(), uv: g.uv_min },
TextVertex {
position: tl.to_array(),
uv: g.uv_min,
},
TextVertex {
position: tr.to_array(),
uv: [g.uv_max[0], g.uv_min[1]],
},
TextVertex { position: br.to_array(), uv: g.uv_max },
TextVertex {
position: br.to_array(),
uv: g.uv_max,
},
TextVertex {
position: bl.to_array(),
uv: [g.uv_min[0], g.uv_max[1]],