From a50ea4cdaf7c3dfc828065549d1c7d73f48b85fa Mon Sep 17 00:00:00 2001 From: meehl <12648828+meehl@users.noreply.github.com> Date: Tue, 25 Aug 2026 12:11:49 +0200 Subject: [PATCH] Introduce new coordinate space for glyph positions within a layout --- src/fonts.rs | 2 +- src/fonts/layout.rs | 14 ++++++++++---- src/fonts/rasterizer.rs | 10 +++++----- src/installer.rs | 6 ++++-- src/math.rs | 1 + src/pob/api/rendering.rs | 7 +++---- 6 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/fonts.rs b/src/fonts.rs index 6ff8195..f8cc2d2 100644 --- a/src/fonts.rs +++ b/src/fonts.rs @@ -16,7 +16,7 @@ use parley::{ }; use std::{rc::Rc, sync::Arc}; -pub use layout::{Alignment, FontStyle, Layout, LayoutJob}; +pub use layout::{Alignment, FontStyle, Layout, LayoutJob, LayoutToLogical}; mod atlas; mod glyph_key; diff --git a/src/fonts/layout.rs b/src/fonts/layout.rs index e168446..c622079 100644 --- a/src/fonts/layout.rs +++ b/src/fonts/layout.rs @@ -1,11 +1,19 @@ use crate::{ color::Srgba, - dpi::{LogicalPoint, LogicalRect}, + dpi::{LogicalPoint, LogicalScreenSpace}, + math::{Rect, Translation}, uv::UvRect, }; use ordered_float::OrderedFloat; use parley::{FontFamily, FontFamilyName}; +// All coordinates relative to layout origin +pub struct LayoutSpace; + +pub type LayoutRect = Rect; + +pub type LayoutToLogical = Translation; + #[derive(Copy, Clone, Default, Debug, Hash, PartialEq)] pub enum Alignment { #[default] @@ -104,9 +112,7 @@ impl std::hash::Hash for LayoutJob<'_> { } pub struct PositionedGlyph { - // relative to layout origin - // TODO: introduce new "layout space"? - pub rect: LogicalRect, + pub rect: LayoutRect, pub uv: UvRect, pub layer_idx: u32, pub color: Srgba, diff --git a/src/fonts/rasterizer.rs b/src/fonts/rasterizer.rs index b50f170..9a65e6a 100644 --- a/src/fonts/rasterizer.rs +++ b/src/fonts/rasterizer.rs @@ -1,7 +1,7 @@ use crate::{ color::Srgba, - dpi::{LogicalRect, LogicalVector, PhysicalPoint, PhysicalRect, PhysicalVector, ScaleFactor}, - fonts::{atlas::FontAtlas, glyph_key::GlyphKey}, + dpi::{LogicalVector, PhysicalPoint, PhysicalRect, PhysicalVector, ScaleFactor}, + fonts::{atlas::FontAtlas, glyph_key::GlyphKey, layout::LayoutRect}, math::Size, uv::UvRect, }; @@ -57,8 +57,7 @@ pub struct CachedGlyph { } pub struct RasterizedGlyph { - // NOTE: this is relative to the layout origin - pub rect: LogicalRect, + pub rect: LayoutRect, pub uv: UvRect, pub texture_layer_idx: u32, pub color: Srgba, @@ -77,7 +76,8 @@ impl RasterizedGlyph { ); RasterizedGlyph { - rect: glyph_rect.cast() / scale_factor, + // TODO: store sizes instead of rects + rect: (glyph_rect.cast() / scale_factor).cast_unit(), uv: cached.uv, texture_layer_idx: cached.texture_layer_idx, color, diff --git a/src/installer.rs b/src/installer.rs index 94dde08..bcc07f7 100644 --- a/src/installer.rs +++ b/src/installer.rs @@ -3,7 +3,7 @@ use crate::{ color::Srgba, dpi::{LogicalPoint, LogicalRect}, draw_commands::DrawCommand, - fonts::{Alignment, FontStyle, Fonts, LayoutJob}, + fonts::{Alignment, FontStyle, Fonts, LayoutJob, LayoutToLogical}, installer::download::{ DownloadEvent, ExtractionRule, build_client, download_and_extract_tarball, download_file_to_disk, fetch_file_contents, @@ -144,7 +144,9 @@ impl Installer { .layout(job, self.window_state.scale_factor()); for glyph in &layout.glyphs { draw_commands.push(DrawCommand { - positions: glyph.rect.translate(pos.to_vector()).into(), + positions: LayoutToLogical::new(pos.x, pos.y) + .transform_box(&glyph.rect) + .into(), uvs: glyph.uv.into(), color: glyph.color, // font atlas always lives at default texture ID diff --git a/src/math.rs b/src/math.rs index b408f1b..5544b67 100644 --- a/src/math.rs +++ b/src/math.rs @@ -2,6 +2,7 @@ pub use euclid::Box2D as Rect; pub use euclid::Point2D as Point; pub use euclid::Scale; pub use euclid::Size2D as Size; +pub use euclid::Translation2D as Translation; pub use euclid::Vector2D as Vector; pub use quad::Quad; diff --git a/src/pob/api/rendering.rs b/src/pob/api/rendering.rs index 7264a78..9b88a6e 100644 --- a/src/pob/api/rendering.rs +++ b/src/pob/api/rendering.rs @@ -1,10 +1,9 @@ use crate::{ color::Srgba, dpi::LogicalPoint, - fonts::{Alignment, FontStyle, LayoutJob}, + fonts::{Alignment, FontStyle, LayoutJob, LayoutToLogical}, math::{Point, Quad, Rect, Size}, - pob::Context, - pob::api::image_handle::image_handle_texture_id, + pob::{Context, api::image_handle::image_handle_texture_id}, }; use core::ffi::c_int; use mlua::{ @@ -363,7 +362,7 @@ unsafe extern "C-unwind" fn draw_string(state: *mut ffi::lua_State) -> c_int { let layout = ctx.fonts.borrow_mut().layout(job, scale_factor); for glyph in &layout.glyphs { ctx.recorder.draw_glyph( - glyph.rect.translate(position.to_vector()), + LayoutToLogical::new(position.x, position.y).transform_box(&glyph.rect), glyph.uv, glyph.color, glyph.layer_idx,