diff --git a/core/src/orientation/pos.rs b/core/src/orientation/pos.rs index 9d73394..e9afd75 100644 --- a/core/src/orientation/pos.rs +++ b/core/src/orientation/pos.rs @@ -28,6 +28,14 @@ impl UiVec2 { } } + /// From lengths already on the grid, with no fraction of a box. + pub const fn from_px(px: PxVec2) -> Self { + Self { + x: UiScalar::from_parts(Rel::ZERO, px.x), + y: UiScalar::from_parts(Rel::ZERO, px.y), + } + } + pub const fn rel(rel: impl const Into) -> Self { let rel = rel.into(); Self { diff --git a/core/src/primitive/text.rs b/core/src/primitive/text.rs index 941f123..1fa9f92 100644 --- a/core/src/primitive/text.rs +++ b/core/src/primitive/text.rs @@ -1,7 +1,8 @@ #[cfg(feature = "layout-diagnostics")] use crate::layout_diagnostics::{self as diag, Counter, TimerKind}; use crate::{ - Align, GlyphAtlas, GlyphEntry, GlyphKey, PlacedGlyph, RegionAlign, UiColor, util::Vec2, + Align, GlyphAtlas, GlyphEntry, GlyphKey, PlacedGlyph, Px, PxVec2, RegionAlign, UiColor, + util::Vec2, }; use parley::{ Alignment, AlignmentOptions, FontContext, FontFamily, FontFamilyName, GenericFamily, Layout, @@ -303,9 +304,9 @@ impl TextData { }; placed.push(PlacedGlyph { entry, - offset: Vec2::new( - glyph.x.floor() + entry.left as f32, - glyph.y.floor() - entry.top as f32, + offset: PxVec2::new( + Px::from_int(glyph.x.floor() as i32 + entry.left), + Px::from_int(glyph.y.floor() as i32 - entry.top), ), }); } diff --git a/core/src/render/atlas.rs b/core/src/render/atlas.rs index 2325f4a..170bdeb 100644 --- a/core/src/render/atlas.rs +++ b/core/src/render/atlas.rs @@ -1,5 +1,5 @@ use crate::{ - PatchRect, + PatchRect, PxVec2, util::{HashMap, Vec2}, }; use image::RgbaImage; @@ -241,5 +241,7 @@ fn write_glyph(page: &mut RgbaImage, image: &Image, x: u32, y: u32) { #[derive(Clone, Copy)] pub struct PlacedGlyph { pub entry: GlyphEntry, - pub offset: Vec2, + /// Whole pixels from the origin of the text to this glyph's top-left, + /// on the grid once here rather than on every frame that draws it. + pub offset: PxVec2, } diff --git a/core/src/ui/painter.rs b/core/src/ui/painter.rs index 653c36c..9df8484 100644 --- a/core/src/ui/painter.rs +++ b/core/src/ui/painter.rs @@ -326,9 +326,13 @@ impl<'a> Painter<'a> { let mut region = origin; region.x.end = region.x.start; region.y.end = region.y.start; - let mut region = region.offset(UiVec2::px(glyph.offset)); - region.x.end = region.x.start + UiScalar::px(glyph.entry.width as f32); - region.y.end = region.y.start + UiScalar::px(glyph.entry.height as f32); + let mut region = region.offset(UiVec2::from_px(glyph.offset)); + let size = PxVec2::new( + Px::from_int(glyph.entry.width as i32), + Px::from_int(glyph.entry.height as i32), + ); + region.x.end = region.x.start.offset(size.x); + region.y.end = region.y.start.offset(size.y); self.write( kind, GlyphPrimitive {