Put a glyph's offset on the grid where it is placed

A placed glyph's offset is whole pixels by construction -- a floored pen
position plus the entry's integer bearing -- and `Painter::glyphs` was
converting it, and the entry's width and height, from `f32` on every frame
that drew the glyph. It is a `PxVec2` now, converted once when the text is
placed, and the size is two integer shifts.

Measured with `perf stat -e instructions:u`, since the difference is smaller
than this machine's clock: the `many` phase went from 2,013,099,594
instructions to 1,938,264,572 over 500 frames, 3.7% less. `scroll` and
`repaint` are unchanged to within noise, which is right -- they do not redraw
glyphs.

Checked: fmt, clippy, 105 tests, the reorder fuzzer at 300 seeds, and `tabs`,
`text` and `random` byte-identical.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
iris-aiandClaude Opus 5 committed 2026-09-16 04:11:07 -04:00
1 parent f11f5f4825
commit 11c55bcef9
4 files changed
+24 -9

No files matched your search

+8
View File
@@ -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<Vec2>) -> Self { pub const fn rel(rel: impl const Into<Vec2>) -> Self {
let rel = rel.into(); let rel = rel.into();
Self { Self {
+5 -4
View File
@@ -1,7 +1,8 @@
#[cfg(feature = "layout-diagnostics")] #[cfg(feature = "layout-diagnostics")]
use crate::layout_diagnostics::{self as diag, Counter, TimerKind}; use crate::layout_diagnostics::{self as diag, Counter, TimerKind};
use crate::{ use crate::{
Align, GlyphAtlas, GlyphEntry, GlyphKey, PlacedGlyph, RegionAlign, UiColor, util::Vec2, Align, GlyphAtlas, GlyphEntry, GlyphKey, PlacedGlyph, Px, PxVec2, RegionAlign, UiColor,
util::Vec2,
}; };
use parley::{ use parley::{
Alignment, AlignmentOptions, FontContext, FontFamily, FontFamilyName, GenericFamily, Layout, Alignment, AlignmentOptions, FontContext, FontFamily, FontFamilyName, GenericFamily, Layout,
@@ -303,9 +304,9 @@ impl TextData {
}; };
placed.push(PlacedGlyph { placed.push(PlacedGlyph {
entry, entry,
offset: Vec2::new( offset: PxVec2::new(
glyph.x.floor() + entry.left as f32, Px::from_int(glyph.x.floor() as i32 + entry.left),
glyph.y.floor() - entry.top as f32, Px::from_int(glyph.y.floor() as i32 - entry.top),
), ),
}); });
} }
+4 -2
View File
@@ -1,5 +1,5 @@
use crate::{ use crate::{
PatchRect, PatchRect, PxVec2,
util::{HashMap, Vec2}, util::{HashMap, Vec2},
}; };
use image::RgbaImage; use image::RgbaImage;
@@ -241,5 +241,7 @@ fn write_glyph(page: &mut RgbaImage, image: &Image, x: u32, y: u32) {
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub struct PlacedGlyph { pub struct PlacedGlyph {
pub entry: GlyphEntry, 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,
} }
+7 -3
View File
@@ -326,9 +326,13 @@ impl<'a> Painter<'a> {
let mut region = origin; let mut region = origin;
region.x.end = region.x.start; region.x.end = region.x.start;
region.y.end = region.y.start; region.y.end = region.y.start;
let mut region = region.offset(UiVec2::px(glyph.offset)); let mut region = region.offset(UiVec2::from_px(glyph.offset));
region.x.end = region.x.start + UiScalar::px(glyph.entry.width as f32); let size = PxVec2::new(
region.y.end = region.y.start + UiScalar::px(glyph.entry.height as f32); 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( self.write(
kind, kind,
GlyphPrimitive { GlyphPrimitive {