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

LibWeb+WebContent: Forbid access to underlying type of CSSPixels

Although DistinctNumeric, which is supposed to abstract the underlying
type, was used to represent CSSPixels, we have a whole bunch of places
in the layout code that assume CSSPixels::value() returns a
floating-point type. This assumption makes it difficult to replace the
underlying type in CSSPixels with a non-floating type.

To make it easier to transition CSSPixels to fixed-point math, one step
we can take is to prevent access to the underlying type using value()
and instead use explicit conversions with the to_float(), to_double(),
and to_int() methods.
This commit is contained in:
Aliaksandr Kalenik 2023-06-12 21:37:35 +03:00 committed by Andreas Kling
parent 5a54c686a7
commit 147c3b3d97
43 changed files with 340 additions and 220 deletions

View file

@ -24,10 +24,10 @@ Gfx::FloatRect EdgeRect::resolved(Layout::Node const& layout_node, Gfx::Rect<dou
// widths for <bottom>, and the same as the used value of the width plus the sum of the
// horizontal padding and border widths for <right>, such that four 'auto' values result in the
// clipping region being the same as the element's border box).
auto left = border_box.left() + (left_edge.is_auto() ? 0 : left_edge.to_px(layout_node)).value();
auto top = border_box.top() + (top_edge.is_auto() ? 0 : top_edge.to_px(layout_node)).value();
auto right = border_box.left() + (right_edge.is_auto() ? border_box.width() : right_edge.to_px(layout_node)).value();
auto bottom = border_box.top() + (bottom_edge.is_auto() ? border_box.height() : bottom_edge.to_px(layout_node)).value();
auto left = border_box.left() + (left_edge.is_auto() ? 0 : left_edge.to_px(layout_node)).to_double();
auto top = border_box.top() + (top_edge.is_auto() ? 0 : top_edge.to_px(layout_node)).to_double();
auto right = border_box.left() + (right_edge.is_auto() ? border_box.width() : right_edge.to_px(layout_node)).to_double();
auto bottom = border_box.top() + (bottom_edge.is_auto() ? border_box.height() : bottom_edge.to_px(layout_node)).to_double();
return Gfx::FloatRect {
left,
top,

View file

@ -49,7 +49,7 @@ Length Length::make_auto()
Length Length::make_px(CSSPixels value)
{
return Length(value.value(), Type::Px);
return Length(value.to_double(), Type::Px);
}
Length Length::percentage_of(Percentage const& percentage) const
@ -74,31 +74,31 @@ CSSPixels Length::font_relative_length_to_px(Length::FontMetrics const& font_met
{
switch (m_type) {
case Type::Em:
return m_value * font_metrics.font_size;
return m_value * font_metrics.font_size.to_double();
case Type::Rem:
return m_value * root_font_metrics.font_size;
return m_value * root_font_metrics.font_size.to_double();
case Type::Ex:
return m_value * font_metrics.x_height;
return m_value * font_metrics.x_height.to_double();
case Type::Rex:
return m_value * root_font_metrics.x_height;
return m_value * root_font_metrics.x_height.to_double();
case Type::Cap:
return m_value * font_metrics.cap_height;
return m_value * font_metrics.cap_height.to_double();
case Type::Rcap:
return m_value * root_font_metrics.cap_height;
return m_value * root_font_metrics.cap_height.to_double();
case Type::Ch:
return m_value * font_metrics.zero_advance;
return m_value * font_metrics.zero_advance.to_double();
case Type::Rch:
return m_value * root_font_metrics.zero_advance;
return m_value * root_font_metrics.zero_advance.to_double();
case Type::Ic:
// FIXME: Use the "advance measure of the “水” (CJK water ideograph, U+6C34) glyph"
return m_value * font_metrics.font_size;
return m_value * font_metrics.font_size.to_double();
case Type::Ric:
// FIXME: Use the "advance measure of the “水” (CJK water ideograph, U+6C34) glyph"
return m_value * root_font_metrics.font_size;
return m_value * root_font_metrics.font_size.to_double();
case Type::Lh:
return m_value * font_metrics.line_height;
return m_value * font_metrics.line_height.to_double();
case Type::Rlh:
return m_value * root_font_metrics.line_height;
return m_value * root_font_metrics.line_height.to_double();
default:
VERIFY_NOT_REACHED();
}

View file

@ -42,10 +42,10 @@ Gfx::IntRect Screen::screen_rect() const
{
auto screen_rect_in_css_pixels = window().page()->web_exposed_screen_area();
return {
screen_rect_in_css_pixels.x().value(),
screen_rect_in_css_pixels.y().value(),
screen_rect_in_css_pixels.width().value(),
screen_rect_in_css_pixels.height().value()
screen_rect_in_css_pixels.x().to_int(),
screen_rect_in_css_pixels.y().to_int(),
screen_rect_in_css_pixels.width().to_int(),
screen_rect_in_css_pixels.height().to_int()
};
}

View file

@ -1888,7 +1888,7 @@ void StyleComputer::compute_font(StyleProperties& style, DOM::Element const* ele
Optional<Length> maybe_length;
if (font_size->is_percentage()) {
// Percentages refer to parent element's font size
maybe_length = Length::make_px(static_cast<double>(font_size->as_percentage().percentage().as_fraction()) * parent_font_size());
maybe_length = Length::make_px(font_size->as_percentage().percentage().as_fraction() * parent_font_size().to_double());
} else if (font_size->is_length()) {
maybe_length = font_size->as_length().length();
@ -1896,7 +1896,7 @@ void StyleComputer::compute_font(StyleProperties& style, DOM::Element const* ele
maybe_length = font_size->as_calculated().resolve_length(length_resolution_context);
}
if (maybe_length.has_value()) {
auto px = maybe_length.value().to_px(length_resolution_context).value();
auto px = maybe_length.value().to_px(length_resolution_context).to_int();
if (px != 0)
font_size_in_px = px;
}

View file

@ -30,7 +30,7 @@ static double resolve_value(CalculatedStyleValue::CalculationResult::Value value
[](Number const& number) { return number.value(); },
[](Angle const& angle) { return angle.to_degrees(); },
[](Frequency const& frequency) { return frequency.to_hertz(); },
[&context](Length const& length) { return length.to_px(*context).value(); },
[&context](Length const& length) { return length.to_px(*context).to_double(); },
[](Percentage const& percentage) { return percentage.value(); },
[](Time const& time) { return time.to_seconds(); });
};

View file

@ -18,7 +18,7 @@ float Filter::Blur::resolved_radius(Layout::Node const& node) const
// Default value when omitted is 0px.
auto sigma = 0;
if (radius.has_value())
sigma = radius->to_px(node).value();
sigma = radius->to_px(node).to_int();
// Note: The radius/sigma of the blur needs to be doubled for LibGfx's blur functions.
return sigma * 2;
}
@ -28,9 +28,9 @@ Filter::DropShadow::Resolved Filter::DropShadow::resolved(Layout::Node const& no
// The default value for omitted values is missing length values set to 0
// and the missing used color is taken from the color property.
return Resolved {
offset_x.to_px(node).value(),
offset_y.to_px(node).value(),
radius.has_value() ? radius->to_px(node).value() : 0.0,
offset_x.to_px(node).to_double(),
offset_y.to_px(node).to_double(),
radius.has_value() ? radius->to_px(node).to_double() : 0.0,
color.has_value() ? *color : node.computed_values().color()
};
}

View file

@ -66,7 +66,7 @@ bool LinearGradientStyleValue::equals(StyleValue const& other_) const
float LinearGradientStyleValue::angle_degrees(CSSPixelSize gradient_size) const
{
auto corner_angle_degrees = [&] {
return atan2(gradient_size.height().value(), gradient_size.width().value()) * 180 / AK::Pi<double>;
return atan2(gradient_size.height().to_double(), gradient_size.width().to_double()) * 180 / AK::Pi<double>;
};
return m_properties.direction.visit(
[&](SideOrCorner side_or_corner) {

View file

@ -146,12 +146,12 @@ Gfx::FloatSize RadialGradientStyleValue::resolve_size(Layout::Node const& node,
},
[&](CircleSize const& circle_size) {
auto radius = circle_size.radius.to_px(node);
return Gfx::FloatSize { radius.value(), radius.value() };
return Gfx::FloatSize { radius.to_float(), radius.to_float() };
},
[&](EllipseSize const& ellipse_size) {
auto radius_a = ellipse_size.radius_a.resolved(node, CSS::Length::make_px(size.width())).to_px(node);
auto radius_b = ellipse_size.radius_b.resolved(node, CSS::Length::make_px(size.height())).to_px(node);
return Gfx::FloatSize { radius_a.value(), radius_b.value() };
return Gfx::FloatSize { radius_a.to_float(), radius_b.to_float() };
});
// Handle degenerate cases