From 13b19529292791e96de35902041d820aac190497 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Tue, 8 Nov 2022 16:31:01 +0000 Subject: [PATCH] LibWeb: Convert AbstractImageStyleValue to new pixel units --- Userland/Libraries/LibWeb/CSS/StyleValue.cpp | 58 +++++++++---------- Userland/Libraries/LibWeb/CSS/StyleValue.h | 38 ++++++------ .../LibWeb/Painting/BackgroundPainting.cpp | 4 +- .../LibWeb/Painting/GradientPainting.cpp | 2 +- .../LibWeb/Painting/MarkerPaintable.cpp | 4 +- 5 files changed, 53 insertions(+), 53 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp index bf9e52ac8f..ace462abaa 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp @@ -1,6 +1,6 @@ /* * Copyright (c) 2018-2020, Andreas Kling - * Copyright (c) 2021-2022, Sam Atkins + * Copyright (c) 2021-2023, Sam Atkins * Copyright (c) 2021, Tobias Christiansen * * SPDX-License-Identifier: BSD-2-Clause @@ -1718,24 +1718,24 @@ bool ImageStyleValue::equals(StyleValue const& other) const return m_url == other.as_image().m_url; } -Optional ImageStyleValue::natural_width() const +Optional ImageStyleValue::natural_width() const { if (auto* b = bitmap(0); b != nullptr) return b->width(); return {}; } -Optional ImageStyleValue::natural_height() const +Optional ImageStyleValue::natural_height() const { if (auto* b = bitmap(0); b != nullptr) return b->height(); return {}; } -void ImageStyleValue::paint(PaintContext& context, Gfx::IntRect const& dest_rect, CSS::ImageRendering image_rendering) const +void ImageStyleValue::paint(PaintContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering image_rendering) const { if (auto* b = bitmap(m_current_frame_index); b != nullptr) - context.painter().draw_scaled_bitmap(dest_rect, *b, bitmap(0)->rect(), 1.0f, to_gfx_scaling_mode(image_rendering)); + context.painter().draw_scaled_bitmap(dest_rect.to_type(), *b, bitmap(0)->rect(), 1.0f, to_gfx_scaling_mode(image_rendering)); } static void serialize_color_stop_list(StringBuilder& builder, auto const& color_stop_list) @@ -1827,10 +1827,10 @@ bool LinearGradientStyleValue::equals(StyleValue const& other_) const && m_color_stop_list == other.m_color_stop_list); } -float LinearGradientStyleValue::angle_degrees(Gfx::FloatSize gradient_size) const +float LinearGradientStyleValue::angle_degrees(CSSPixelSize gradient_size) const { auto corner_angle_degrees = [&] { - return static_cast(atan2(gradient_size.height(), gradient_size.width())) * 180 / AK::Pi; + return static_cast(atan2(gradient_size.height().value(), gradient_size.width().value())) * 180 / AK::Pi; }; return m_direction.visit( [&](SideOrCorner side_or_corner) { @@ -1866,24 +1866,24 @@ float LinearGradientStyleValue::angle_degrees(Gfx::FloatSize gradient_size) cons }); } -void LinearGradientStyleValue::resolve_for_size(Layout::Node const& node, Gfx::FloatSize size) const +void LinearGradientStyleValue::resolve_for_size(Layout::Node const& node, CSSPixelSize size) const { if (m_resolved.has_value() && m_resolved->size == size) return; - m_resolved = ResolvedData { Painting::resolve_linear_gradient_data(node, size.to_type(), *this), size }; + m_resolved = ResolvedData { Painting::resolve_linear_gradient_data(node, size, *this), size }; } -void LinearGradientStyleValue::paint(PaintContext& context, Gfx::IntRect const& dest_rect, CSS::ImageRendering) const +void LinearGradientStyleValue::paint(PaintContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering) const { VERIFY(m_resolved.has_value()); - Painting::paint_linear_gradient(context, dest_rect.to_type(), m_resolved->data); + Painting::paint_linear_gradient(context, dest_rect, m_resolved->data); } -Gfx::FloatPoint PositionValue::resolved(Layout::Node const& node, Gfx::FloatRect const& rect) const +CSSPixelPoint PositionValue::resolved(Layout::Node const& node, CSSPixelRect const& rect) const { // Note: A preset + a none default x/y_relative_to is impossible in the syntax (and makes little sense) - float x = horizontal_position.visit( - [&](HorizontalPreset preset) { + CSSPixels x = horizontal_position.visit( + [&](HorizontalPreset preset) -> CSSPixels { return rect.width() * [&] { switch (preset) { case HorizontalPreset::Left: @@ -1897,11 +1897,11 @@ Gfx::FloatPoint PositionValue::resolved(Layout::Node const& node, Gfx::FloatRect } }(); }, - [&](LengthPercentage length_percentage) { + [&](LengthPercentage length_percentage) -> CSSPixels { return length_percentage.resolved(node, Length::make_px(rect.width())).to_px(node); }); - float y = vertical_position.visit( - [&](VerticalPreset preset) { + CSSPixels y = vertical_position.visit( + [&](VerticalPreset preset) -> CSSPixels { return rect.height() * [&] { switch (preset) { case VerticalPreset::Top: @@ -1915,14 +1915,14 @@ Gfx::FloatPoint PositionValue::resolved(Layout::Node const& node, Gfx::FloatRect } }(); }, - [&](LengthPercentage length_percentage) { + [&](LengthPercentage length_percentage) -> CSSPixels { return length_percentage.resolved(node, Length::make_px(rect.height())).to_px(node); }); if (x_relative_to == HorizontalEdge::Right) x = rect.width() - x; if (y_relative_to == VerticalEdge::Bottom) y = rect.height() - y; - return Gfx::FloatPoint { rect.x() + x, rect.y() + y }; + return CSSPixelPoint { rect.x() + x, rect.y() + y }; } void PositionValue::serialize(StringBuilder& builder) const @@ -2150,11 +2150,11 @@ Gfx::FloatSize RadialGradientStyleValue::resolve_size(Layout::Node const& node, return resolved_size; } -void RadialGradientStyleValue::resolve_for_size(Layout::Node const& node, Gfx::FloatSize paint_size) const +void RadialGradientStyleValue::resolve_for_size(Layout::Node const& node, CSSPixelSize paint_size) const { - Gfx::FloatRect gradient_box { { 0, 0 }, paint_size }; - auto center = m_position.resolved(node, gradient_box); - auto gradient_size = resolve_size(node, center, gradient_box); + CSSPixelRect gradient_box { { 0, 0 }, paint_size }; + auto center = m_position.resolved(node, gradient_box).to_type(); + auto gradient_size = resolve_size(node, center, gradient_box.to_type()); if (m_resolved.has_value() && m_resolved->gradient_size == gradient_size) return; m_resolved = ResolvedData { @@ -2175,10 +2175,10 @@ bool RadialGradientStyleValue::equals(StyleValue const& other) const && m_color_stop_list == other_gradient.m_color_stop_list); } -void RadialGradientStyleValue::paint(PaintContext& context, Gfx::IntRect const& dest_rect, CSS::ImageRendering) const +void RadialGradientStyleValue::paint(PaintContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering) const { VERIFY(m_resolved.has_value()); - Painting::paint_radial_gradient(context, dest_rect.to_type(), m_resolved->data, + Painting::paint_radial_gradient(context, dest_rect, m_resolved->data, context.rounded_device_point(m_resolved->center.to_type()), context.rounded_device_size(m_resolved->gradient_size.to_type())); } @@ -2206,17 +2206,17 @@ DeprecatedString ConicGradientStyleValue::to_deprecated_string() const return builder.to_deprecated_string(); } -void ConicGradientStyleValue::resolve_for_size(Layout::Node const& node, Gfx::FloatSize size) const +void ConicGradientStyleValue::resolve_for_size(Layout::Node const& node, CSSPixelSize size) const { if (!m_resolved.has_value()) m_resolved = ResolvedData { Painting::resolve_conic_gradient_data(node, *this), {} }; - m_resolved->position = m_position.resolved(node, Gfx::FloatRect { { 0, 0 }, size }); + m_resolved->position = m_position.resolved(node, CSSPixelRect { { 0, 0 }, size }); } -void ConicGradientStyleValue::paint(PaintContext& context, Gfx::IntRect const& dest_rect, CSS::ImageRendering) const +void ConicGradientStyleValue::paint(PaintContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering) const { VERIFY(m_resolved.has_value()); - Painting::paint_conic_gradient(context, dest_rect.to_type(), m_resolved->data, context.rounded_device_point(m_resolved->position.to_type())); + Painting::paint_conic_gradient(context, dest_rect, m_resolved->data, context.rounded_device_point(m_resolved->position)); } bool ConicGradientStyleValue::equals(StyleValue const& other) const diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.h b/Userland/Libraries/LibWeb/CSS/StyleValue.h index 7e10119021..1a9e1ed552 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValue.h +++ b/Userland/Libraries/LibWeb/CSS/StyleValue.h @@ -1,7 +1,7 @@ /* * Copyright (c) 2018-2020, Andreas Kling * Copyright (c) 2021, Tobias Christiansen - * Copyright (c) 2021-2022, Sam Atkins + * Copyright (c) 2021-2023, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -130,7 +130,7 @@ struct PositionValue { HorizontalEdge x_relative_to { HorizontalEdge::Left }; VerticalEdge y_relative_to { VerticalEdge::Top }; - Gfx::FloatPoint resolved(Layout::Node const&, Gfx::FloatRect const&) const; + CSSPixelPoint resolved(Layout::Node const& node, CSSPixelRect const& rect) const; void serialize(StringBuilder&) const; bool operator==(PositionValue const&) const; }; @@ -1153,14 +1153,14 @@ class AbstractImageStyleValue : public StyleValue { public: using StyleValue::StyleValue; - virtual Optional natural_width() const { return {}; } - virtual Optional natural_height() const { return {}; } + virtual Optional natural_width() const { return {}; } + virtual Optional natural_height() const { return {}; } virtual void load_any_resources(DOM::Document&) {}; - virtual void resolve_for_size(Layout::Node const&, Gfx::FloatSize) const {}; + virtual void resolve_for_size(Layout::Node const&, CSSPixelSize) const {}; virtual bool is_paintable() const = 0; - virtual void paint(PaintContext& context, Gfx::IntRect const& dest_rect, CSS::ImageRendering image_rendering) const = 0; + virtual void paint(PaintContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering image_rendering) const = 0; }; class ImageStyleValue final @@ -1175,11 +1175,11 @@ public: virtual void load_any_resources(DOM::Document&) override; - Optional natural_width() const override; - Optional natural_height() const override; + Optional natural_width() const override; + Optional natural_height() const override; bool is_paintable() const override { return bitmap(0) != nullptr; } - void paint(PaintContext& context, Gfx::IntRect const& dest_rect, CSS::ImageRendering image_rendering) const override; + void paint(PaintContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering image_rendering) const override; Function on_animate; @@ -1240,7 +1240,7 @@ public: virtual DeprecatedString to_deprecated_string() const override; - void paint(PaintContext&, Gfx::IntRect const& dest_rect, CSS::ImageRendering) const override; + void paint(PaintContext&, DevicePixelRect const& dest_rect, CSS::ImageRendering) const override; virtual bool equals(StyleValue const& other) const override; @@ -1251,7 +1251,7 @@ public: bool is_paintable() const override { return true; } - void resolve_for_size(Layout::Node const&, Gfx::FloatSize) const override; + void resolve_for_size(Layout::Node const&, CSSPixelSize) const override; Gfx::FloatSize resolve_size(Layout::Node const&, Gfx::FloatPoint, Gfx::FloatRect const&) const; @@ -1295,7 +1295,7 @@ public: virtual DeprecatedString to_deprecated_string() const override; - void paint(PaintContext&, Gfx::IntRect const& dest_rect, CSS::ImageRendering) const override; + void paint(PaintContext&, DevicePixelRect const& dest_rect, CSS::ImageRendering) const override; virtual bool equals(StyleValue const& other) const override; @@ -1308,11 +1308,11 @@ public: bool is_paintable() const override { return true; } - void resolve_for_size(Layout::Node const&, Gfx::FloatSize) const override; + void resolve_for_size(Layout::Node const&, CSSPixelSize) const override; virtual ~ConicGradientStyleValue() override = default; - Gfx::FloatPoint resolve_position(Layout::Node const&, Gfx::FloatRect const&) const; + CSSPixelPoint resolve_position(Layout::Node const&, CSSPixelRect const&) const; bool is_repeating() const { return m_repeating == GradientRepeating::Yes; } @@ -1334,7 +1334,7 @@ private: struct ResolvedData { Painting::ConicGradientData data; - Gfx::FloatPoint position; + CSSPixelPoint position; }; mutable Optional m_resolved; @@ -1366,12 +1366,12 @@ public: bool is_repeating() const { return m_repeating == GradientRepeating::Yes; } - float angle_degrees(Gfx::FloatSize gradient_size) const; + float angle_degrees(CSSPixelSize gradient_size) const; - void resolve_for_size(Layout::Node const&, Gfx::FloatSize) const override; + void resolve_for_size(Layout::Node const&, CSSPixelSize) const override; bool is_paintable() const override { return true; } - void paint(PaintContext& context, Gfx::IntRect const& dest_rect, CSS::ImageRendering image_rendering) const override; + void paint(PaintContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering image_rendering) const override; private: LinearGradientStyleValue(GradientDirection direction, Vector color_stop_list, GradientType type, GradientRepeating repeating) @@ -1390,7 +1390,7 @@ private: struct ResolvedData { Painting::LinearGradientData data; - Gfx::FloatSize size; + CSSPixelSize size; }; mutable Optional m_resolved; diff --git a/Userland/Libraries/LibWeb/Painting/BackgroundPainting.cpp b/Userland/Libraries/LibWeb/Painting/BackgroundPainting.cpp index d9fc0ba370..62e4e77a70 100644 --- a/Userland/Libraries/LibWeb/Painting/BackgroundPainting.cpp +++ b/Userland/Libraries/LibWeb/Painting/BackgroundPainting.cpp @@ -307,7 +307,7 @@ void paint_background(PaintContext& context, Layout::NodeWithStyleAndBoxModelMet CSSPixels image_y = image_rect.y(); Optional last_image_device_rect; - image.resolve_for_size(layout_node, image_rect.size().to_type()); + image.resolve_for_size(layout_node, image_rect.size()); while (image_y <= css_clip_rect.bottom()) { image_rect.set_y(image_y); @@ -317,7 +317,7 @@ void paint_background(PaintContext& context, Layout::NodeWithStyleAndBoxModelMet image_rect.set_x(image_x); auto image_device_rect = context.rounded_device_rect(image_rect); if (image_device_rect != last_image_device_rect && image_device_rect.intersects(context.device_viewport_rect())) - image.paint(context, image_device_rect.to_type(), image_rendering); + image.paint(context, image_device_rect, image_rendering); last_image_device_rect = image_device_rect; if (!repeat_x) break; diff --git a/Userland/Libraries/LibWeb/Painting/GradientPainting.cpp b/Userland/Libraries/LibWeb/Painting/GradientPainting.cpp index c546ebf6cf..0cc89789be 100644 --- a/Userland/Libraries/LibWeb/Painting/GradientPainting.cpp +++ b/Userland/Libraries/LibWeb/Painting/GradientPainting.cpp @@ -132,7 +132,7 @@ static ColorStopData resolve_color_stop_positions(auto const& color_stop_list, a LinearGradientData resolve_linear_gradient_data(Layout::Node const& node, CSSPixelSize gradient_size, CSS::LinearGradientStyleValue const& linear_gradient) { - auto gradient_angle = linear_gradient.angle_degrees(gradient_size.to_type()); + auto gradient_angle = linear_gradient.angle_degrees(gradient_size); auto gradient_length_px = calculate_gradient_length(gradient_size, gradient_angle); auto gradient_length = CSS::Length::make_px(gradient_length_px); diff --git a/Userland/Libraries/LibWeb/Painting/MarkerPaintable.cpp b/Userland/Libraries/LibWeb/Painting/MarkerPaintable.cpp index 8227a1c755..e16cb39d69 100644 --- a/Userland/Libraries/LibWeb/Painting/MarkerPaintable.cpp +++ b/Userland/Libraries/LibWeb/Painting/MarkerPaintable.cpp @@ -46,8 +46,8 @@ void MarkerPaintable::paint(PaintContext& context, PaintPhase phase) const image_rect.center_within(enclosing); auto device_image_rect = context.enclosing_device_rect(image_rect); - list_style_image->resolve_for_size(layout_box(), device_image_rect.size().to_type().to_type()); - list_style_image->paint(context, device_image_rect.to_type(), computed_values().image_rendering()); + list_style_image->resolve_for_size(layout_box(), image_rect.size()); + list_style_image->paint(context, device_image_rect, computed_values().image_rendering()); return; }