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

+20 -15
View File
@@ -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,
}
+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());