From 3e065a8838f3e39a9a953a29c3ec0e7ab2e0b383 Mon Sep 17 00:00:00 2001 From: Dodomew <18313775+Dodomew@users.noreply.github.com> Date: Tue, 10 Nov 2026 21:43:49 +0100 Subject: [PATCH] Snap text to pixel grid after applying viewport offset Text inside viewports was blurry with fractional scale factors because it was aligned to the pixel grid before the viewport offset was applied. --- CHANGELOG.md | 4 ++++ src/draw_commands.rs | 4 ++++ src/pob/api/text.rs | 10 ++++++++-- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 97888a8..f15cde6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## [Unreleased] - ReleaseDate +### Fixed + +- Fix blurry text inside viewports with fractional scale factors + ## [0.3.3] - 2026-10-23 ### Fixed diff --git a/src/draw_commands.rs b/src/draw_commands.rs index 1e5e670..b8bb25f 100644 --- a/src/draw_commands.rs +++ b/src/draw_commands.rs @@ -57,6 +57,10 @@ impl DrawCommandRecorder { self.current_viewport = viewport; } + pub fn get_viewport(&self) -> LogicalRect { + self.current_viewport + } + pub fn set_viewport_from_size(&mut self, size: LogicalSize) { self.set_viewport(LogicalRect::from_size(size)); } diff --git a/src/pob/api/text.rs b/src/pob/api/text.rs index 26dbfc7..1b933c9 100644 --- a/src/pob/api/text.rs +++ b/src/pob/api/text.rs @@ -1,6 +1,6 @@ use crate::{ color::Srgba, - dpi::LogicalPoint, + dpi::{LogicalPoint, LogicalVector}, fonts::{Alignment, FontStyle, LayoutJob, LayoutToLogical}, math::Point, pob::Context, @@ -103,7 +103,13 @@ pub unsafe extern "C-unwind" fn draw_string(state: *mut ffi::lua_State) -> c_int // 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 viewport_offset = if is_absolute_position { + LogicalVector::zero() + } else { + ctx.recorder.get_viewport().min.to_vector() + }; + let position = + ((position + viewport_offset) * scale_factor).round() / scale_factor - viewport_offset; for glyph in &layout.glyphs { ctx.recorder.draw_glyph(