1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 00:57:45 +00:00

LibWeb: Convert AbstractImageStyleValue to new pixel units

This commit is contained in:
Sam Atkins 2022-11-08 16:31:01 +00:00 committed by Linus Groh
parent 02cd853eee
commit 13b1952929
5 changed files with 53 additions and 53 deletions

View file

@ -1,6 +1,6 @@
/* /*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org> * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org> * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * 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; return m_url == other.as_image().m_url;
} }
Optional<int> ImageStyleValue::natural_width() const Optional<CSSPixels> ImageStyleValue::natural_width() const
{ {
if (auto* b = bitmap(0); b != nullptr) if (auto* b = bitmap(0); b != nullptr)
return b->width(); return b->width();
return {}; return {};
} }
Optional<int> ImageStyleValue::natural_height() const Optional<CSSPixels> ImageStyleValue::natural_height() const
{ {
if (auto* b = bitmap(0); b != nullptr) if (auto* b = bitmap(0); b != nullptr)
return b->height(); return b->height();
return {}; 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) 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<int>(), *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) 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); && 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 = [&] { auto corner_angle_degrees = [&] {
return static_cast<float>(atan2(gradient_size.height(), gradient_size.width())) * 180 / AK::Pi<float>; return static_cast<float>(atan2(gradient_size.height().value(), gradient_size.width().value())) * 180 / AK::Pi<float>;
}; };
return m_direction.visit( return m_direction.visit(
[&](SideOrCorner side_or_corner) { [&](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) if (m_resolved.has_value() && m_resolved->size == size)
return; return;
m_resolved = ResolvedData { Painting::resolve_linear_gradient_data(node, size.to_type<CSSPixels>(), *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()); VERIFY(m_resolved.has_value());
Painting::paint_linear_gradient(context, dest_rect.to_type<DevicePixels>(), 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) // Note: A preset + a none default x/y_relative_to is impossible in the syntax (and makes little sense)
float x = horizontal_position.visit( CSSPixels x = horizontal_position.visit(
[&](HorizontalPreset preset) { [&](HorizontalPreset preset) -> CSSPixels {
return rect.width() * [&] { return rect.width() * [&] {
switch (preset) { switch (preset) {
case HorizontalPreset::Left: 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); return length_percentage.resolved(node, Length::make_px(rect.width())).to_px(node);
}); });
float y = vertical_position.visit( CSSPixels y = vertical_position.visit(
[&](VerticalPreset preset) { [&](VerticalPreset preset) -> CSSPixels {
return rect.height() * [&] { return rect.height() * [&] {
switch (preset) { switch (preset) {
case VerticalPreset::Top: 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); return length_percentage.resolved(node, Length::make_px(rect.height())).to_px(node);
}); });
if (x_relative_to == HorizontalEdge::Right) if (x_relative_to == HorizontalEdge::Right)
x = rect.width() - x; x = rect.width() - x;
if (y_relative_to == VerticalEdge::Bottom) if (y_relative_to == VerticalEdge::Bottom)
y = rect.height() - y; 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 void PositionValue::serialize(StringBuilder& builder) const
@ -2150,11 +2150,11 @@ Gfx::FloatSize RadialGradientStyleValue::resolve_size(Layout::Node const& node,
return resolved_size; 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 }; CSSPixelRect gradient_box { { 0, 0 }, paint_size };
auto center = m_position.resolved(node, gradient_box); auto center = m_position.resolved(node, gradient_box).to_type<float>();
auto gradient_size = resolve_size(node, center, gradient_box); auto gradient_size = resolve_size(node, center, gradient_box.to_type<float>());
if (m_resolved.has_value() && m_resolved->gradient_size == gradient_size) if (m_resolved.has_value() && m_resolved->gradient_size == gradient_size)
return; return;
m_resolved = ResolvedData { m_resolved = ResolvedData {
@ -2175,10 +2175,10 @@ bool RadialGradientStyleValue::equals(StyleValue const& other) const
&& m_color_stop_list == other_gradient.m_color_stop_list); && 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()); VERIFY(m_resolved.has_value());
Painting::paint_radial_gradient(context, dest_rect.to_type<DevicePixels>(), m_resolved->data, Painting::paint_radial_gradient(context, dest_rect, m_resolved->data,
context.rounded_device_point(m_resolved->center.to_type<CSSPixels>()), context.rounded_device_point(m_resolved->center.to_type<CSSPixels>()),
context.rounded_device_size(m_resolved->gradient_size.to_type<CSSPixels>())); context.rounded_device_size(m_resolved->gradient_size.to_type<CSSPixels>()));
} }
@ -2206,17 +2206,17 @@ DeprecatedString ConicGradientStyleValue::to_deprecated_string() const
return builder.to_deprecated_string(); 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()) if (!m_resolved.has_value())
m_resolved = ResolvedData { Painting::resolve_conic_gradient_data(node, *this), {} }; 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()); VERIFY(m_resolved.has_value());
Painting::paint_conic_gradient(context, dest_rect.to_type<DevicePixels>(), m_resolved->data, context.rounded_device_point(m_resolved->position.to_type<CSSPixels>())); Painting::paint_conic_gradient(context, dest_rect, m_resolved->data, context.rounded_device_point(m_resolved->position));
} }
bool ConicGradientStyleValue::equals(StyleValue const& other) const bool ConicGradientStyleValue::equals(StyleValue const& other) const

View file

@ -1,7 +1,7 @@
/* /*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org> * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
* Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org> * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -130,7 +130,7 @@ struct PositionValue {
HorizontalEdge x_relative_to { HorizontalEdge::Left }; HorizontalEdge x_relative_to { HorizontalEdge::Left };
VerticalEdge y_relative_to { VerticalEdge::Top }; 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; void serialize(StringBuilder&) const;
bool operator==(PositionValue const&) const; bool operator==(PositionValue const&) const;
}; };
@ -1153,14 +1153,14 @@ class AbstractImageStyleValue : public StyleValue {
public: public:
using StyleValue::StyleValue; using StyleValue::StyleValue;
virtual Optional<int> natural_width() const { return {}; } virtual Optional<CSSPixels> natural_width() const { return {}; }
virtual Optional<int> natural_height() const { return {}; } virtual Optional<CSSPixels> natural_height() const { return {}; }
virtual void load_any_resources(DOM::Document&) {}; 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 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 class ImageStyleValue final
@ -1175,11 +1175,11 @@ public:
virtual void load_any_resources(DOM::Document&) override; virtual void load_any_resources(DOM::Document&) override;
Optional<int> natural_width() const override; Optional<CSSPixels> natural_width() const override;
Optional<int> natural_height() const override; Optional<CSSPixels> natural_height() const override;
bool is_paintable() const override { return bitmap(0) != nullptr; } 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<void()> on_animate; Function<void()> on_animate;
@ -1240,7 +1240,7 @@ public:
virtual DeprecatedString to_deprecated_string() const override; 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; virtual bool equals(StyleValue const& other) const override;
@ -1251,7 +1251,7 @@ public:
bool is_paintable() const override { return true; } 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; 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; 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; virtual bool equals(StyleValue const& other) const override;
@ -1308,11 +1308,11 @@ public:
bool is_paintable() const override { return true; } 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; 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; } bool is_repeating() const { return m_repeating == GradientRepeating::Yes; }
@ -1334,7 +1334,7 @@ private:
struct ResolvedData { struct ResolvedData {
Painting::ConicGradientData data; Painting::ConicGradientData data;
Gfx::FloatPoint position; CSSPixelPoint position;
}; };
mutable Optional<ResolvedData> m_resolved; mutable Optional<ResolvedData> m_resolved;
@ -1366,12 +1366,12 @@ public:
bool is_repeating() const { return m_repeating == GradientRepeating::Yes; } 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; } 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: private:
LinearGradientStyleValue(GradientDirection direction, Vector<LinearColorStopListElement> color_stop_list, GradientType type, GradientRepeating repeating) LinearGradientStyleValue(GradientDirection direction, Vector<LinearColorStopListElement> color_stop_list, GradientType type, GradientRepeating repeating)
@ -1390,7 +1390,7 @@ private:
struct ResolvedData { struct ResolvedData {
Painting::LinearGradientData data; Painting::LinearGradientData data;
Gfx::FloatSize size; CSSPixelSize size;
}; };
mutable Optional<ResolvedData> m_resolved; mutable Optional<ResolvedData> m_resolved;

View file

@ -307,7 +307,7 @@ void paint_background(PaintContext& context, Layout::NodeWithStyleAndBoxModelMet
CSSPixels image_y = image_rect.y(); CSSPixels image_y = image_rect.y();
Optional<DevicePixelRect> last_image_device_rect; Optional<DevicePixelRect> last_image_device_rect;
image.resolve_for_size(layout_node, image_rect.size().to_type<float>()); image.resolve_for_size(layout_node, image_rect.size());
while (image_y <= css_clip_rect.bottom()) { while (image_y <= css_clip_rect.bottom()) {
image_rect.set_y(image_y); image_rect.set_y(image_y);
@ -317,7 +317,7 @@ void paint_background(PaintContext& context, Layout::NodeWithStyleAndBoxModelMet
image_rect.set_x(image_x); image_rect.set_x(image_x);
auto image_device_rect = context.rounded_device_rect(image_rect); 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())) if (image_device_rect != last_image_device_rect && image_device_rect.intersects(context.device_viewport_rect()))
image.paint(context, image_device_rect.to_type<int>(), image_rendering); image.paint(context, image_device_rect, image_rendering);
last_image_device_rect = image_device_rect; last_image_device_rect = image_device_rect;
if (!repeat_x) if (!repeat_x)
break; break;

View file

@ -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) 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<float>()); auto gradient_angle = linear_gradient.angle_degrees(gradient_size);
auto gradient_length_px = calculate_gradient_length(gradient_size, gradient_angle); auto gradient_length_px = calculate_gradient_length(gradient_size, gradient_angle);
auto gradient_length = CSS::Length::make_px(gradient_length_px); auto gradient_length = CSS::Length::make_px(gradient_length_px);

View file

@ -46,8 +46,8 @@ void MarkerPaintable::paint(PaintContext& context, PaintPhase phase) const
image_rect.center_within(enclosing); image_rect.center_within(enclosing);
auto device_image_rect = context.enclosing_device_rect(image_rect); auto device_image_rect = context.enclosing_device_rect(image_rect);
list_style_image->resolve_for_size(layout_box(), device_image_rect.size().to_type<int>().to_type<float>()); list_style_image->resolve_for_size(layout_box(), image_rect.size());
list_style_image->paint(context, device_image_rect.to_type<int>(), computed_values().image_rendering()); list_style_image->paint(context, device_image_rect, computed_values().image_rendering());
return; return;
} }