Handle negative glyph positions correctly

This commit is contained in:
grovesNL 2022-11-29 14:33:12 -03:30
parent ca27ec508c
commit 2a01a1b2e0
4 changed files with 19 additions and 19 deletions

View file

@ -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],

View file

@ -1,6 +1,6 @@
struct VertexInput {
@builtin(vertex_index) vertex_idx: u32,
@location(0) pos: vec2<u32>,
@location(0) pos: vec2<i32>,
@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: {}

View file

@ -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,
},

View file

@ -196,25 +196,25 @@ impl TextRenderer {
let mut glyphs_added = 0;
for (layout, overflow) in layouts.iter() {
let layout = layout.borrow();
let layout: &Layout<C> = 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],