diff --git a/src/dpi.rs b/src/dpi.rs index 2f8f453..31a7377 100644 --- a/src/dpi.rs +++ b/src/dpi.rs @@ -4,6 +4,7 @@ pub struct LogicalScreenSpace; pub struct PhysicalScreenSpace; pub type LogicalPoint = Point; +pub type LogicalVector = Vector; pub type LogicalSize = Size; pub type LogicalRect = Rect; pub type LogicalQuad = Quad; diff --git a/src/fonts.rs b/src/fonts.rs index ca4a100..07fd974 100644 --- a/src/fonts.rs +++ b/src/fonts.rs @@ -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 }; diff --git a/src/fonts/layout.rs b/src/fonts/layout.rs index fd3d5b4..9e5992a 100644 --- a/src/fonts/layout.rs +++ b/src/fonts/layout.rs @@ -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 { + 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) -> usize { // `parley_layout` was built at the real display scale factor so its internal coordinates // are physical. Scale the query point to match. diff --git a/src/fonts/rasterizer.rs b/src/fonts/rasterizer.rs index 5c99524..f68e551 100644 --- a/src/fonts/rasterizer.rs +++ b/src/fonts/rasterizer.rs @@ -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, + // additional offset applied to every glyph's raw parley position before quantization + offset: PhysicalVector, scale_factor: ScaleFactor, ) -> impl Iterator> + 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) diff --git a/src/pob/api/rendering.rs b/src/pob/api/rendering.rs index 9b88a6e..5c39591 100644 --- a/src/pob/api/rendering.rs +++ b/src/pob/api/rendering.rs @@ -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),