Decouple anchor offset from parley alignment

This commit is contained in:
meehl committed 2026-08-26 17:59:01 +02:00
1 parent d217af94c3
commit 1f5e30ae5a
5 files changed
+24 -20

No files matched your search

+1
View File
@@ -4,6 +4,7 @@ pub struct LogicalScreenSpace;
pub struct PhysicalScreenSpace;
pub type LogicalPoint<T> = Point<T, LogicalScreenSpace>;
pub type LogicalVector<T> = Vector<T, LogicalScreenSpace>;
pub type LogicalSize<T> = Size<T, LogicalScreenSpace>;
pub type LogicalRect<T> = Rect<T, LogicalScreenSpace>;
pub type LogicalQuad<T> = Quad<T, LogicalScreenSpace>;
+5 -13
View File
@@ -231,21 +231,13 @@ impl Fonts {
let (mut parley_layout, _) = builder.build();
parley_layout.break_all_lines(None);
// extra offset applied to each glyph to get position relative to layout origin
let mut glyph_offset = PhysicalVector::zero();
if let Some(alignment) = job.alignment {
let alignment = match alignment {
let parley_alignment = match alignment {
Alignment::Min => parley::Alignment::Start,
Alignment::Center => {
glyph_offset.x += -parley_layout.full_width() * 0.5;
parley::Alignment::Center
}
Alignment::Max => {
glyph_offset.x += -parley_layout.full_width();
parley::Alignment::End
}
Alignment::Center => parley::Alignment::Center,
Alignment::Max => parley::Alignment::End,
};
parley_layout.align(alignment, parley::AlignmentOptions::default());
parley_layout.align(parley_alignment, parley::AlignmentOptions::default());
}
let mut glyphs = Vec::new();
@@ -254,7 +246,7 @@ impl Fonts {
for rasterized in self.glyph_rasterizer.rasterize_glyph_run(
&mut self.atlas,
&run,
glyph_offset,
PhysicalVector::zero(),
scale_factor,
) {
let Some(glyph) = rasterized else { continue };
+9 -1
View File
@@ -1,6 +1,6 @@
use crate::{
color::Srgba,
dpi::{LogicalPoint, LogicalScreenSpace},
dpi::{LogicalPoint, LogicalScreenSpace, LogicalVector},
math::{Rect, Translation},
uv::UvRect,
};
@@ -130,6 +130,14 @@ impl Layout {
self.width
}
pub fn anchor_offset(&self, anchor: Alignment) -> LogicalVector<f32> {
match anchor {
Alignment::Min => LogicalVector::zero(),
Alignment::Center => LogicalVector::new(-self.width * 0.5, 0.0),
Alignment::Max => LogicalVector::new(-self.width, 0.0),
}
}
pub fn cursor_index_at(&self, point: LogicalPoint<f32>) -> usize {
// `parley_layout` was built at the real display scale factor so its internal coordinates
// are physical. Scale the query point to match.
+4 -4
View File
@@ -76,8 +76,8 @@ impl GlyphRasterizer {
&'slf mut self,
atlas: &'atlas mut FontAtlas,
glyph_run: &'run GlyphRun<'_, Srgba>,
// additional offset relative to layout origin
layout_offset: PhysicalVector<f32>,
// additional offset applied to every glyph's raw parley position before quantization
offset: PhysicalVector<f32>,
scale_factor: ScaleFactor<f32>,
) -> impl Iterator<Item = Option<RasterizedGlyph>> + use<'slf, 'run, 'atlas> {
let run = glyph_run.run();
@@ -107,10 +107,10 @@ impl GlyphRasterizer {
let cached_glyphs = &mut self.cached_glyphs;
glyph_run.positioned_glyphs().map(move |glyph| {
let layout_position = PhysicalPoint::new(glyph.x, glyph.y) + layout_offset;
let glyph_position = PhysicalPoint::new(glyph.x, glyph.y) + offset;
let (glyph_key, physical_position) =
GlyphKey::from_position(layout_position, glyph.id, style_id);
GlyphKey::from_position(glyph_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)
+5 -2
View File
@@ -355,11 +355,14 @@ unsafe extern "C-unwind" fn draw_string(state: *mut ffi::lua_State) -> c_int {
ctx.recorder.set_draw_color(last_segment.color);
}
// Align layout origin with the physical pixel grid to reduce blurriness
let scale_factor = ctx.window_state.scale_factor();
let layout = ctx.fonts.borrow_mut().layout(job, scale_factor);
// apply offset to account for anchor position
let position = position + layout.anchor_offset(halign);
// align layout anchor with the physical pixel grid to reduce blurriness
let position = (position * scale_factor).round() / scale_factor;
let layout = ctx.fonts.borrow_mut().layout(job, scale_factor);
for glyph in &layout.glyphs {
ctx.recorder.draw_glyph(
LayoutToLogical::new(position.x, position.y).transform_box(&glyph.rect),