From d217af94c344680c5ae6f47d0b2e291f585dee38 Mon Sep 17 00:00:00 2001 From: meehl <12648828+meehl@users.noreply.github.com> Date: Wed, 26 Aug 2026 17:34:11 +0200 Subject: [PATCH] Pass scale factor to layout builder. This means `parley::Layout`'s internal coordinates are now in physical space. --- src/fonts.rs | 35 ++++++++++++++++++++--------------- src/fonts/glyph_key.rs | 16 ++++------------ src/fonts/layout.rs | 11 ++++++----- src/fonts/rasterizer.rs | 21 ++++++++------------- 4 files changed, 38 insertions(+), 45 deletions(-) diff --git a/src/fonts.rs b/src/fonts.rs index 36de020..ca4a100 100644 --- a/src/fonts.rs +++ b/src/fonts.rs @@ -1,12 +1,9 @@ use crate::{ color::Srgba, - dpi::ScaleFactor, + dpi::{PhysicalVector, ScaleFactor}, fonts::{ - atlas::FontAtlas, - glyph_key::SubpixelBin, - layout::{LayoutVector, PositionedGlyph}, - layout_cache::LayoutCache, - rasterizer::GlyphRasterizer, + atlas::FontAtlas, glyph_key::SubpixelBin, layout::PositionedGlyph, + layout_cache::LayoutCache, rasterizer::GlyphRasterizer, }, renderer::image::ImageDelta, util::calculate_hash, @@ -163,9 +160,12 @@ impl Fonts { font_size, ..Default::default() }; - let mut builder = - self.layout_context - .tree_builder(&mut self.font_context, 1.0, false, &style); + let mut builder = self.layout_context.tree_builder( + &mut self.font_context, + scale_factor.get(), + false, + &style, + ); builder.push_text(text); let (mut parley_layout, _) = builder.build(); @@ -177,7 +177,7 @@ impl Fonts { .rasterize_glyph_run( &mut self.atlas, &run, - LayoutVector::new(horizontal_offset, 0.0), + PhysicalVector::new(horizontal_offset, 0.0), scale_factor, ) .for_each(|_| {}); @@ -213,9 +213,13 @@ impl Fonts { ..default_style }; - let mut builder = - self.layout_context - .tree_builder(&mut self.font_context, 1.0, false, &style); + // scale factor is passed to builder so coordinates in resulting layout will be physical + let mut builder = self.layout_context.tree_builder( + &mut self.font_context, + scale_factor.get(), + false, + &style, + ); for segment in job.segments { let brush_style = StyleProperty::Brush(segment.color); @@ -228,7 +232,7 @@ impl Fonts { parley_layout.break_all_lines(None); // extra offset applied to each glyph to get position relative to layout origin - let mut glyph_offset = LayoutVector::zero(); + let mut glyph_offset = PhysicalVector::zero(); if let Some(alignment) = job.alignment { let alignment = match alignment { Alignment::Min => parley::Alignment::Start, @@ -264,7 +268,8 @@ impl Fonts { }); Layout { - width: parley_layout.full_width(), + // full_width is physical so it needs to be converted back to logical + width: parley_layout.full_width() / scale_factor.get(), glyphs, parley_layout, } diff --git a/src/fonts/glyph_key.rs b/src/fonts/glyph_key.rs index d045859..a988517 100644 --- a/src/fonts/glyph_key.rs +++ b/src/fonts/glyph_key.rs @@ -1,36 +1,28 @@ -use ordered_float::OrderedFloat; - -use crate::{ - dpi::PhysicalPoint, - fonts::{layout::LayoutPoint, style_cache::StyleId}, -}; +use crate::{dpi::PhysicalPoint, fonts::style_cache::StyleId}; #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct GlyphKey { glyph_id: swash::GlyphId, style_id: StyleId, x_bin: SubpixelBin<4>, - scale_factor: OrderedFloat, } impl GlyphKey { pub fn from_position( - position: LayoutPoint, + position: PhysicalPoint, glyph_id: u32, style_id: StyleId, - scale_factor: f32, ) -> (Self, PhysicalPoint) { // x: use subpixel binning so bitmap encodes sub-pixel position - let (x, x_bin) = SubpixelBin::<4>::new(position.x * scale_factor); + let (x, x_bin) = SubpixelBin::<4>::new(position.x); // y: just rounded. not much benefit from sub-pixel vertical positioning - let y = (position.y * scale_factor).round() as i32; + let y = position.y.round() as i32; ( Self { glyph_id: glyph_id as u16, style_id, x_bin, - scale_factor: OrderedFloat(scale_factor), }, PhysicalPoint::new(x, y), ) diff --git a/src/fonts/layout.rs b/src/fonts/layout.rs index 2e6e223..fd3d5b4 100644 --- a/src/fonts/layout.rs +++ b/src/fonts/layout.rs @@ -1,7 +1,7 @@ use crate::{ color::Srgba, dpi::{LogicalPoint, LogicalScreenSpace}, - math::{Point, Rect, Translation, Vector}, + math::{Rect, Translation}, uv::UvRect, }; use ordered_float::OrderedFloat; @@ -10,8 +10,6 @@ use parley::{FontFamily, FontFamilyName}; // All coordinates relative to layout origin pub struct LayoutSpace; -pub type LayoutPoint = Point; -pub type LayoutVector = Vector; pub type LayoutRect = Rect; pub type LayoutToLogical = Translation; @@ -123,7 +121,7 @@ pub struct PositionedGlyph { pub struct Layout { pub glyphs: Vec, pub width: f32, - // kept for parley's cursor helper + // kept for parley's cursor helper. pub parley_layout: parley::Layout, } @@ -133,6 +131,9 @@ impl Layout { } pub fn cursor_index_at(&self, point: LogicalPoint) -> usize { - parley::Cursor::from_point(&self.parley_layout, point.x, point.y).index() + // `parley_layout` was built at the real display scale factor so its internal coordinates + // are physical. Scale the query point to match. + let scale = self.parley_layout.scale(); + parley::Cursor::from_point(&self.parley_layout, point.x * scale, point.y * scale).index() } } diff --git a/src/fonts/rasterizer.rs b/src/fonts/rasterizer.rs index 4db48b6..5c99524 100644 --- a/src/fonts/rasterizer.rs +++ b/src/fonts/rasterizer.rs @@ -1,12 +1,7 @@ use crate::{ color::Srgba, - dpi::{PhysicalRect, PhysicalSize, PhysicalVector, ScaleFactor}, - fonts::{ - atlas::FontAtlas, - glyph_key::GlyphKey, - layout::{LayoutPoint, LayoutRect, LayoutVector}, - style_cache::StyleCache, - }, + dpi::{PhysicalPoint, PhysicalRect, PhysicalSize, PhysicalVector, ScaleFactor}, + fonts::{atlas::FontAtlas, glyph_key::GlyphKey, layout::LayoutRect, style_cache::StyleCache}, uv::UvRect, }; use ahash::HashMap; @@ -82,12 +77,12 @@ impl GlyphRasterizer { atlas: &'atlas mut FontAtlas, glyph_run: &'run GlyphRun<'_, Srgba>, // additional offset relative to layout origin - layout_offset: LayoutVector, + layout_offset: PhysicalVector, scale_factor: ScaleFactor, ) -> impl Iterator> + use<'slf, 'run, 'atlas> { let run = glyph_run.run(); let color = glyph_run.style().brush; - let font_size = run.font_size() * scale_factor.get(); + let font_size = run.font_size(); let normalized_coords = run.normalized_coords(); let skew = run.synthesis().skew(); // skew angle for faux italic @@ -112,10 +107,10 @@ impl GlyphRasterizer { let cached_glyphs = &mut self.cached_glyphs; glyph_run.positioned_glyphs().map(move |glyph| { - let layout_position = LayoutPoint::new(glyph.x, glyph.y) + layout_offset; + let layout_position = PhysicalPoint::new(glyph.x, glyph.y) + layout_offset; - let (glyph_key, pixel_position) = - GlyphKey::from_position(layout_position, glyph.id, style_id, scale_factor.get()); + let (glyph_key, physical_position) = + GlyphKey::from_position(layout_position, glyph.id, style_id); let cached = *cached_glyphs.entry(glyph_key).or_insert_with(|| { rasterize_glyph(&mut scaler, image, atlas, glyph.id, &glyph_key, skew) @@ -123,7 +118,7 @@ impl GlyphRasterizer { cached.map(|cached| { // apply the baseline offset to get the actual bitmap position - let bitmap_top_left = pixel_position + cached.baseline_offset; + let bitmap_top_left = physical_position + cached.baseline_offset; let physical_rect = PhysicalRect::from_origin_and_size(bitmap_top_left, cached.size.cast());