Pass scale factor to layout builder.

This means `parley::Layout`'s internal coordinates are now in physical
space.
This commit is contained in:
meehl committed 2026-08-26 17:34:11 +02:00
1 parent fe9434e4f0
commit d217af94c3
4 files changed
+38 -45

No files matched your search

+4 -12
View File
@@ -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<f32>,
}
impl GlyphKey {
pub fn from_position(
position: LayoutPoint<f32>,
position: PhysicalPoint<f32>,
glyph_id: u32,
style_id: StyleId,
scale_factor: f32,
) -> (Self, PhysicalPoint<i32>) {
// 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),
)
+6 -5
View File
@@ -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<T> = Point<T, LayoutSpace>;
pub type LayoutVector<T> = Vector<T, LayoutSpace>;
pub type LayoutRect<T> = Rect<T, LayoutSpace>;
pub type LayoutToLogical = Translation<f32, LayoutSpace, LogicalScreenSpace>;
@@ -123,7 +121,7 @@ pub struct PositionedGlyph {
pub struct Layout {
pub glyphs: Vec<PositionedGlyph>,
pub width: f32,
// kept for parley's cursor helper
// kept for parley's cursor helper.
pub parley_layout: parley::Layout<Srgba>,
}
@@ -133,6 +131,9 @@ impl Layout {
}
pub fn cursor_index_at(&self, point: LogicalPoint<f32>) -> 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()
}
}
+8 -13
View File
@@ -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<f32>,
layout_offset: PhysicalVector<f32>,
scale_factor: ScaleFactor<f32>,
) -> impl Iterator<Item = Option<RasterizedGlyph>> + 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());