From bd6ded6ba57de4c4ba1367fbf1d781d5f82d4c3a Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Mon, 11 Nov 2024 16:51:52 +0100 Subject: [PATCH] Avoid subpixel positioning when scale factor is 1 --- crates/gpui/src/geometry.rs | 9 +++++++++ crates/gpui/src/window.rs | 19 +++++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/crates/gpui/src/geometry.rs b/crates/gpui/src/geometry.rs index 9e0b9b90140391..97fac6b87bb040 100644 --- a/crates/gpui/src/geometry.rs +++ b/crates/gpui/src/geometry.rs @@ -2572,6 +2572,15 @@ impl ScaledPixels { Self(self.0.floor()) } + /// Rounds the `ScaledPixels` value to the nearest whole number. + /// + /// # Returns + /// + /// Returns a new `ScaledPixels` instance with the rounded value. + pub fn round(&self) -> Self { + Self(self.0.round()) + } + /// Rounds the `ScaledPixels` value to the nearest whole number. /// /// # Returns diff --git a/crates/gpui/src/window.rs b/crates/gpui/src/window.rs index dcda69723b5d86..b1fe23bf2cefbe 100644 --- a/crates/gpui/src/window.rs +++ b/crates/gpui/src/window.rs @@ -647,6 +647,7 @@ impl Window { let modifiers = platform_window.modifiers(); let content_size = platform_window.content_size(); let scale_factor = platform_window.scale_factor(); + log::info!("!!!!!! SCALE FACTOR {:?}", scale_factor); let appearance = platform_window.appearance(); let text_system = Arc::new(WindowTextSystem::new(cx.text_system().clone())); let dirty = Rc::new(Cell::new(true)); @@ -1187,6 +1188,7 @@ impl<'a> WindowContext<'a> { fn bounds_changed(&mut self) { self.window.scale_factor = self.window.platform_window.scale_factor(); + log::info!("!!!!!! SCALE FACTOR {:?}", self.window.scale_factor); self.window.viewport_size = self.window.platform_window.content_size(); self.window.display_id = self .window @@ -2436,9 +2438,13 @@ impl<'a> WindowContext<'a> { let element_opacity = self.element_opacity(); let scale_factor = self.scale_factor(); let glyph_origin = origin.scale(scale_factor); - let subpixel_variant = Point { - x: (glyph_origin.x.0.fract() * SUBPIXEL_VARIANTS as f32).floor() as u8, - y: (glyph_origin.y.0.fract() * SUBPIXEL_VARIANTS as f32).floor() as u8, + let subpixel_variant = if scale_factor == 1. { + Point::default() + } else { + Point { + x: (glyph_origin.x.0.fract() * SUBPIXEL_VARIANTS as f32).floor() as u8, + y: (glyph_origin.y.0.fract() * SUBPIXEL_VARIANTS as f32).floor() as u8, + } }; let params = RenderGlyphParams { font_id, @@ -2460,8 +2466,13 @@ impl<'a> WindowContext<'a> { Ok(Some((size, Cow::Owned(bytes)))) })? .expect("Callback above only errors or returns Some"); + let origin = if scale_factor == 1. { + glyph_origin.map(|px| px.round()) + } else { + glyph_origin.map(|px| px.floor()) + }; let bounds = Bounds { - origin: glyph_origin.map(|px| px.floor()) + raster_bounds.origin.map(Into::into), + origin: origin + raster_bounds.origin.map(Into::into), size: tile.bounds.size.map(Into::into), }; let content_mask = self.content_mask().scale(scale_factor);