From 0be479dcfb766b7e931d266989b1556db577b5c8 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Wed, 26 Oct 2022 20:53:41 +0100 Subject: [PATCH] LibWeb: Make PaintContext aware of CSS and DevicePixels Store the ratio between device and CSS pixels on the PaintContext, so that it can convert between the two. Co-authored-by: MacDue --- .../LibWeb/Painting/PaintContext.cpp | 76 ++++++++++++++++++- .../Libraries/LibWeb/Painting/PaintContext.h | 18 ++++- Userland/Services/WebContent/PageHost.cpp | 2 +- Userland/Utilities/headless-browser.cpp | 2 +- 4 files changed, 93 insertions(+), 5 deletions(-) diff --git a/Userland/Libraries/LibWeb/Painting/PaintContext.cpp b/Userland/Libraries/LibWeb/Painting/PaintContext.cpp index c907a5c8de..9dd49be010 100644 --- a/Userland/Libraries/LibWeb/Painting/PaintContext.cpp +++ b/Userland/Libraries/LibWeb/Painting/PaintContext.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2018-2022, Andreas Kling + * Copyright (c) 2022, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -8,9 +9,10 @@ namespace Web { -PaintContext::PaintContext(Gfx::Painter& painter, Palette const& palette) +PaintContext::PaintContext(Gfx::Painter& painter, Palette const& palette, float device_pixels_per_css_pixel) : m_painter(painter) , m_palette(palette) + , m_device_pixels_per_css_pixel(device_pixels_per_css_pixel) { } @@ -32,4 +34,76 @@ void PaintContext::clear_svg_context() m_svg_context.clear(); } +DevicePixels PaintContext::rounded_device_pixels(CSSPixels css_pixels) const +{ + return roundf(css_pixels.value() * m_device_pixels_per_css_pixel); +} + +DevicePixels PaintContext::enclosing_device_pixels(CSSPixels css_pixels) const +{ + return ceilf(css_pixels.value() * m_device_pixels_per_css_pixel); +} + +DevicePixels PaintContext::floored_device_pixels(CSSPixels css_pixels) const +{ + return floorf(css_pixels.value() * m_device_pixels_per_css_pixel); +} + +DevicePixelPoint PaintContext::rounded_device_point(CSSPixelPoint point) const +{ + return { + roundf(point.x().value() * m_device_pixels_per_css_pixel), + roundf(point.y().value() * m_device_pixels_per_css_pixel) + }; +} + +DevicePixelRect PaintContext::enclosing_device_rect(CSSPixelRect rect) const +{ + return { + floorf(rect.x().value() * m_device_pixels_per_css_pixel), + floorf(rect.y().value() * m_device_pixels_per_css_pixel), + ceilf(rect.width().value() * m_device_pixels_per_css_pixel), + ceilf(rect.height().value() * m_device_pixels_per_css_pixel) + }; +} + +DevicePixelRect PaintContext::rounded_device_rect(CSSPixelRect rect) const +{ + return { + roundf(rect.x().value() * m_device_pixels_per_css_pixel), + roundf(rect.y().value() * m_device_pixels_per_css_pixel), + roundf(rect.width().value() * m_device_pixels_per_css_pixel), + roundf(rect.height().value() * m_device_pixels_per_css_pixel) + }; +} + +DevicePixelSize PaintContext::enclosing_device_size(CSSPixelSize size) const +{ + return { + ceilf(size.width().value() * m_device_pixels_per_css_pixel), + ceilf(size.height().value() * m_device_pixels_per_css_pixel) + }; +} + +DevicePixelSize PaintContext::rounded_device_size(CSSPixelSize size) const +{ + return { + roundf(size.width().value() * m_device_pixels_per_css_pixel), + roundf(size.height().value() * m_device_pixels_per_css_pixel) + }; +} + +CSSPixels PaintContext::scale_to_css_pixels(DevicePixels device_pixels) const +{ + return device_pixels.value() / m_device_pixels_per_css_pixel; +} + +CSSPixelPoint PaintContext::scale_to_css_point(DevicePixelPoint point) const +{ + return { + scale_to_css_pixels(point.x()), + scale_to_css_pixels(point.y()) + }; +} + } diff --git a/Userland/Libraries/LibWeb/Painting/PaintContext.h b/Userland/Libraries/LibWeb/Painting/PaintContext.h index 5024eebb16..511a789287 100644 --- a/Userland/Libraries/LibWeb/Painting/PaintContext.h +++ b/Userland/Libraries/LibWeb/Painting/PaintContext.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2018-2022, Andreas Kling + * Copyright (c) 2022, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -10,13 +11,14 @@ #include #include #include +#include #include namespace Web { class PaintContext { public: - PaintContext(Gfx::Painter& painter, Palette const& palette); + PaintContext(Gfx::Painter& painter, Palette const& palette, float device_pixels_per_css_pixel); Gfx::Painter& painter() const { return m_painter; } Palette const& palette() const { return m_palette; } @@ -35,9 +37,20 @@ public: bool has_focus() const { return m_focus; } void set_has_focus(bool focus) { m_focus = focus; } + DevicePixels enclosing_device_pixels(CSSPixels css_pixels) const; + DevicePixels floored_device_pixels(CSSPixels css_pixels) const; + DevicePixels rounded_device_pixels(CSSPixels css_pixels) const; + DevicePixelPoint rounded_device_point(CSSPixelPoint) const; + DevicePixelRect enclosing_device_rect(CSSPixelRect) const; + DevicePixelRect rounded_device_rect(CSSPixelRect) const; + DevicePixelSize enclosing_device_size(CSSPixelSize) const; + DevicePixelSize rounded_device_size(CSSPixelSize) const; + CSSPixels scale_to_css_pixels(DevicePixels) const; + CSSPixelPoint scale_to_css_point(DevicePixelPoint) const; + PaintContext clone(Gfx::Painter& painter) const { - auto clone = PaintContext(painter, m_palette); + auto clone = PaintContext(painter, m_palette, m_device_pixels_per_css_pixel); clone.m_viewport_rect = m_viewport_rect; clone.m_should_show_line_box_borders = m_should_show_line_box_borders; clone.m_focus = m_focus; @@ -49,6 +62,7 @@ private: Gfx::Painter& m_painter; Palette m_palette; Optional m_svg_context; + float m_device_pixels_per_css_pixel; Gfx::IntRect m_viewport_rect; bool m_should_show_line_box_borders { false }; bool m_focus { false }; diff --git a/Userland/Services/WebContent/PageHost.cpp b/Userland/Services/WebContent/PageHost.cpp index 7d634bb66b..be54d02a85 100644 --- a/Userland/Services/WebContent/PageHost.cpp +++ b/Userland/Services/WebContent/PageHost.cpp @@ -118,7 +118,7 @@ void PageHost::paint(Web::DevicePixelRect const& content_rect, Gfx::Bitmap& targ return; } - Web::PaintContext context(painter, palette()); + Web::PaintContext context(painter, palette(), device_pixels_per_css_pixel()); context.set_should_show_line_box_borders(m_should_show_line_box_borders); context.set_viewport_rect(content_rect.to_type()); context.set_has_focus(m_has_focus); diff --git a/Userland/Utilities/headless-browser.cpp b/Userland/Utilities/headless-browser.cpp index 1310932f85..162df70b6b 100644 --- a/Userland/Utilities/headless-browser.cpp +++ b/Userland/Utilities/headless-browser.cpp @@ -87,7 +87,7 @@ public: return; } - Web::PaintContext context(painter, palette()); + Web::PaintContext context(painter, palette(), device_pixels_per_css_pixel()); context.set_should_show_line_box_borders(false); context.set_viewport_rect(content_rect.to_type()); context.set_has_focus(true);