Skip to content

Commit

Permalink
Reduce aliasing when painting thin box outlines (#5484)
Browse files Browse the repository at this point in the history
* Part of #5164
  • Loading branch information
emilk authored Dec 16, 2024
1 parent 450c624 commit e802917
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions crates/epaint/src/tessellator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,11 @@ impl Tessellator {
((point * self.pixels_per_point - 0.5).round() + 0.5) / self.pixels_per_point
}

#[inline(always)]
pub fn round_pos_to_pixel(&self, pos: Pos2) -> Pos2 {
pos2(self.round_to_pixel(pos.x), self.round_to_pixel(pos.y))
}

#[inline(always)]
pub fn round_pos_to_pixel_center(&self, pos: Pos2) -> Pos2 {
pos2(
Expand Down Expand Up @@ -1702,6 +1707,20 @@ impl Tessellator {
self.tessellate_line(line, stroke, out); // …and forth
}
} else {
let rect = if !stroke.is_empty() && stroke.width < self.feathering {
// Very thin rectangle strokes create extreme aliasing when they move around.
// We can fix that by rounding the rectangle corners to pixel centers.
// TODO(#5164): maybe do this for all shapes and stroke sizes
// TODO(emilk): since we use StrokeKind::Outside, we should probably round the
// corners after offsetting them with half the stroke width (see `translate_stroke_point`).
Rect {
min: self.round_pos_to_pixel_center(rect.min),
max: self.round_pos_to_pixel_center(rect.max),
}
} else {
rect
};

let path = &mut self.scratchpad_path;
path.clear();
path::rounded_rectangle(&mut self.scratchpad_points, rect, rounding);
Expand Down

0 comments on commit e802917

Please sign in to comment.