From 2a01a1b2e03084379faa3248f1ae9568a868131e Mon Sep 17 00:00:00 2001 From: grovesNL Date: Tue, 29 Nov 2022 14:33:12 -0330 Subject: [PATCH] Handle negative glyph positions correctly --- src/lib.rs | 2 +- src/shader.wgsl | 10 +++++----- src/text_atlas.rs | 2 +- src/text_render.rs | 24 ++++++++++++------------ 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 683be88..86d70e0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -46,7 +46,7 @@ pub(crate) struct GlyphDetails { #[repr(C)] #[derive(Clone, Copy, Debug)] pub(crate) struct GlyphToRender { - pos: [u32; 2], + pos: [i32; 2], dim: [u16; 2], uv: [u16; 2], color: [u8; 4], diff --git a/src/shader.wgsl b/src/shader.wgsl index b806c59..88a1320 100644 --- a/src/shader.wgsl +++ b/src/shader.wgsl @@ -1,6 +1,6 @@ struct VertexInput { @builtin(vertex_index) vertex_idx: u32, - @location(0) pos: vec2, + @location(0) pos: vec2, @location(1) dim: u32, @location(2) uv: u32, @location(3) color: u32, @@ -37,17 +37,17 @@ fn vs_main(in_vert: VertexInput) -> VertexOutput { switch v { case 1u: { - pos.x += width; + pos.x += i32(width); uv.x += width; } case 2u: { - pos.x += width; - pos.y += height; + pos.x += i32(width); + pos.y += i32(height); uv.x += width; uv.y += height; } case 3u: { - pos.y += height; + pos.y += i32(height); uv.y += height; } default: {} diff --git a/src/text_atlas.rs b/src/text_atlas.rs index 982ebbf..e6804b6 100644 --- a/src/text_atlas.rs +++ b/src/text_atlas.rs @@ -74,7 +74,7 @@ impl TextAtlas { step_mode: wgpu::VertexStepMode::Vertex, attributes: &[ wgpu::VertexAttribute { - format: VertexFormat::Uint32x2, + format: VertexFormat::Sint32x2, offset: 0, shader_location: 0, }, diff --git a/src/text_render.rs b/src/text_render.rs index 60c480b..59a6c91 100644 --- a/src/text_render.rs +++ b/src/text_render.rs @@ -196,25 +196,25 @@ impl TextRenderer { let mut glyphs_added = 0; for (layout, overflow) in layouts.iter() { - let layout = layout.borrow(); + let layout: &Layout = layout.borrow(); let settings = layout.settings(); // Note: subpixel positioning is not currently handled, so we always truncate down to // the nearest pixel. - let bounds_min_x = settings.x.trunc() as u32; + let bounds_min_x = settings.x.trunc(); let bounds_max_x = settings .max_width - .map(|w| bounds_min_x + w.trunc() as u32) - .unwrap_or(u32::MAX); - let bounds_min_y = settings.y.trunc() as u32; + .map(|w| bounds_min_x + w.trunc()) + .unwrap_or(f32::MAX); + let bounds_min_y = settings.y.trunc(); let bounds_max_y = settings .max_height - .map(|h| bounds_min_y + h.trunc() as u32) - .unwrap_or(u32::MAX); + .map(|h| bounds_min_y + h.trunc()) + .unwrap_or(f32::MAX); for glyph in layout.glyphs() { - let mut x = glyph.x.trunc() as u32; - let mut y = glyph.y.trunc() as u32; + let mut x = glyph.x; + let mut y = glyph.y; let details = atlas.glyph_cache.get(&glyph.key).unwrap(); let (mut atlas_x, mut atlas_y) = match details.gpu_cache { @@ -222,8 +222,8 @@ impl TextRenderer { GpuCache::SkipRasterization => continue, }; - let mut width = details.width as u32; - let mut height = details.height as u32; + let mut width = details.width as f32; + let mut height = details.height as f32; match overflow { TextOverflow::Overflow => {} @@ -274,7 +274,7 @@ impl TextRenderer { glyph_vertices.extend( iter::repeat(GlyphToRender { - pos: [x, y], + pos: [x as i32, y as i32], dim: [width as u16, height as u16], uv: [atlas_x, atlas_y], color: [color.r, color.g, color.b, color.a],