Skip to content

Commit

Permalink
Avoid subpixel positioning when scale factor is 1
Browse files Browse the repository at this point in the history
  • Loading branch information
as-cii committed Nov 11, 2024
1 parent 67822bc commit bd6ded6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
9 changes: 9 additions & 0 deletions crates/gpui/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 15 additions & 4 deletions crates/gpui/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand Down

0 comments on commit bd6ded6

Please sign in to comment.