diff --git a/Userland/Libraries/LibGfx/Rect.h b/Userland/Libraries/LibGfx/Rect.h index 293ebb592d..e52319f42e 100644 --- a/Userland/Libraries/LibGfx/Rect.h +++ b/Userland/Libraries/LibGfx/Rect.h @@ -1,6 +1,7 @@ /* * Copyright (c) 2018-2021, Andreas Kling * Copyright (c) 2021, Sam Atkins + * Copyright (c) 2022, Jelle Raaijmakers * * SPDX-License-Identifier: BSD-2-Clause */ @@ -696,6 +697,17 @@ public: return Rect(*this); } + template + [[nodiscard]] ALWAYS_INLINE Rect to_rounded() const + { + return { + static_cast(llroundd(x())), + static_cast(llroundd(y())), + static_cast(llroundd(width())), + static_cast(llroundd(height())), + }; + } + [[nodiscard]] String to_string() const; private: @@ -715,11 +727,6 @@ using FloatRect = Rect; return Gfx::IntRect::from_two_points({ x1, y1 }, { x2, y2 }); } -[[nodiscard]] ALWAYS_INLINE IntRect rounded_int_rect(FloatRect const& float_rect) -{ - return IntRect { floorf(float_rect.x()), floorf(float_rect.y()), roundf(float_rect.width()), roundf(float_rect.height()) }; -} - } namespace AK { diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp index 1d293da46f..76ed693fd0 100644 --- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp +++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp @@ -180,7 +180,7 @@ DOM::ExceptionOr CanvasRenderingContext2D::draw_image(CanvasImageSource co if (!painter) return {}; auto transformed_destination_rect = m_drawing_state.transform.map(destination_rect); - painter->draw_scaled_bitmap(rounded_int_rect(transformed_destination_rect), *bitmap, source_rect, 1.0f, Gfx::Painter::ScalingMode::BilinearBlend); + painter->draw_scaled_bitmap(transformed_destination_rect.to_rounded(), *bitmap, source_rect, 1.0f, Gfx::Painter::ScalingMode::BilinearBlend); // 7. If image is not origin-clean, then set the CanvasRenderingContext2D's origin-clean flag to false. if (image_is_not_origin_clean(image)) diff --git a/Userland/Libraries/LibWeb/Painting/CanvasPaintable.cpp b/Userland/Libraries/LibWeb/Painting/CanvasPaintable.cpp index 3ce31c55e9..3464f136b1 100644 --- a/Userland/Libraries/LibWeb/Painting/CanvasPaintable.cpp +++ b/Userland/Libraries/LibWeb/Painting/CanvasPaintable.cpp @@ -36,7 +36,7 @@ void CanvasPaintable::paint(PaintContext& context, PaintPhase phase) const return; if (layout_box().dom_node().bitmap()) - context.painter().draw_scaled_bitmap(rounded_int_rect(absolute_rect()), *layout_box().dom_node().bitmap(), layout_box().dom_node().bitmap()->rect(), 1.0f, to_gfx_scaling_mode(computed_values().image_rendering())); + context.painter().draw_scaled_bitmap(absolute_rect().to_rounded(), *layout_box().dom_node().bitmap(), layout_box().dom_node().bitmap()->rect(), 1.0f, to_gfx_scaling_mode(computed_values().image_rendering())); } } diff --git a/Userland/Libraries/LibWeb/Painting/ImagePaintable.cpp b/Userland/Libraries/LibWeb/Painting/ImagePaintable.cpp index 659d0445ff..8fde403e76 100644 --- a/Userland/Libraries/LibWeb/Painting/ImagePaintable.cpp +++ b/Userland/Libraries/LibWeb/Painting/ImagePaintable.cpp @@ -47,7 +47,7 @@ void ImagePaintable::paint(PaintContext& context, PaintPhase phase) const alt = image_element.src(); context.painter().draw_text(enclosing_int_rect(absolute_rect()), alt, Gfx::TextAlignment::Center, computed_values().color(), Gfx::TextElision::Right); } else if (auto bitmap = layout_box().image_loader().bitmap(layout_box().image_loader().current_frame_index())) { - context.painter().draw_scaled_bitmap(rounded_int_rect(absolute_rect()), *bitmap, bitmap->rect(), 1.0f, to_gfx_scaling_mode(computed_values().image_rendering())); + context.painter().draw_scaled_bitmap(absolute_rect().to_rounded(), *bitmap, bitmap->rect(), 1.0f, to_gfx_scaling_mode(computed_values().image_rendering())); } } } diff --git a/Userland/Libraries/LibWeb/Painting/StackingContext.cpp b/Userland/Libraries/LibWeb/Painting/StackingContext.cpp index ab85977616..98e42a4556 100644 --- a/Userland/Libraries/LibWeb/Painting/StackingContext.cpp +++ b/Userland/Libraries/LibWeb/Painting/StackingContext.cpp @@ -270,7 +270,7 @@ void StackingContext::paint(PaintContext& context) const auto transformed_destination_rect = affine_transform.map(source_rect).translated(transform_origin); source_rect.translate_by(transform_origin); - context.painter().draw_scaled_bitmap(Gfx::rounded_int_rect(transformed_destination_rect), *bitmap, source_rect, opacity, Gfx::Painter::ScalingMode::BilinearBlend); + context.painter().draw_scaled_bitmap(transformed_destination_rect.to_rounded(), *bitmap, source_rect, opacity, Gfx::Painter::ScalingMode::BilinearBlend); } else { paint_internal(context); }