Skip to content

Truly independent web browser

LibWeb: Remove the C++ view of computed sizes

Andreas Kling5437fd0b5183
diff --git a/Libraries/LibWeb/CSS/CSSStyleProperties.cpp b/Libraries/LibWeb/CSS/CSSStyleProperties.cpp
index a4c594ef2b8..7b8f72a6b40 100644
--- a/Libraries/LibWeb/CSS/CSSStyleProperties.cpp
+++ b/Libraries/LibWeb/CSS/CSSStyleProperties.cpp
@@ -328,30 +328,6 @@ static NonnullRefPtr<StyleValue const> style_value_for_transform_origin(Transfor
return StyleValueList::create(move(offsets), StyleValueList::Separator::Space, StyleValueList::Collapsible::No);
}
-static NonnullRefPtr<StyleValue const> style_value_for_size(Size const& size)
-{
- if (size.is_none())
- return KeywordStyleValue::create(Keyword::None);
- if (size.is_percentage())
- return PercentageStyleValue::create(size.percentage());
- if (size.is_length())
- return LengthStyleValue::create(size.length());
- if (size.is_auto())
- return KeywordStyleValue::create(Keyword::Auto);
- if (size.is_calculated())
- return size.calculated();
- if (size.is_min_content())
- return KeywordStyleValue::create(Keyword::MinContent);
- if (size.is_max_content())
- return KeywordStyleValue::create(Keyword::MaxContent);
- if (size.is_fit_content()) {
- if (auto available_space = size.fit_content_available_space(); available_space.has_value())
- return FunctionStyleValue::create("fit-content"_utf16_fly_string, style_value_for_length_percentage(available_space.release_value()));
- return KeywordStyleValue::create(Keyword::FitContent);
- }
- TODO();
-}
-
static RefPtr<StyleValue const> style_value_for_shadow(ShadowStyleValue::ShadowType shadow_type, ReadonlySpan<ShadowData> shadow_data)
{
if (shadow_data.is_empty())
@@ -1199,8 +1175,8 @@ RefPtr<StyleValue const> CSSStyleProperties::style_value_for_computed_property(L
[](auto const& box_layout_node) { return Painting::content_height(box_layout_node); },
[](auto const& box_layout_node) { return Painting::unbroken_border_box_size(box_layout_node).height(); });
if (maybe_used_height.has_value())
- return style_value_for_size(Size::make_px(maybe_used_height.release_value()));
- return style_value_for_size(layout_node.height());
+ return LengthStyleValue::create(Length::make_px(maybe_used_height.release_value()));
+ return get_computed_value(property_id);
}
case PropertyID::MarginBottom:
if (auto maybe_used_value = used_value_for_property([](auto const& box_layout_node) { return Painting::box_model(box_layout_node).margin.bottom; }); maybe_used_value.has_value())
@@ -1239,8 +1215,8 @@ RefPtr<StyleValue const> CSSStyleProperties::style_value_for_computed_property(L
[](auto const& box_layout_node) { return Painting::content_width(box_layout_node); },
[](auto const& box_layout_node) { return Painting::unbroken_border_box_size(box_layout_node).width(); });
if (maybe_used_width.has_value())
- return style_value_for_size(Size::make_px(maybe_used_width.release_value()));
- return style_value_for_size(layout_node.width());
+ return LengthStyleValue::create(Length::make_px(maybe_used_width.release_value()));
+ return get_computed_value(property_id);
}
// -> bottom
@@ -1408,13 +1384,13 @@ RefPtr<StyleValue const> CSSStyleProperties::style_value_for_computed_property(L
// https://drafts.csswg.org/css-backgrounds/#border-width
// NB: We do this adjustment when assigning to ComputedValues, so read from there.
case PropertyID::BorderBottomWidth:
- return style_value_for_size(Size::make_px(layout_node.border_bottom().width));
+ return LengthStyleValue::create(Length::make_px(layout_node.border_bottom().width));
case PropertyID::BorderLeftWidth:
- return style_value_for_size(Size::make_px(layout_node.border_left().width));
+ return LengthStyleValue::create(Length::make_px(layout_node.border_left().width));
case PropertyID::BorderRightWidth:
- return style_value_for_size(Size::make_px(layout_node.border_right().width));
+ return LengthStyleValue::create(Length::make_px(layout_node.border_right().width));
case PropertyID::BorderTopWidth:
- return style_value_for_size(Size::make_px(layout_node.border_top().width));
+ return LengthStyleValue::create(Length::make_px(layout_node.border_top().width));
// -> Any other property
// The resolved value is the computed value.
diff --git a/Libraries/LibWeb/CSS/ComputedValues.cpp b/Libraries/LibWeb/CSS/ComputedValues.cpp
index 93699a97caa..d2490f560b6 100644
--- a/Libraries/LibWeb/CSS/ComputedValues.cpp
+++ b/Libraries/LibWeb/CSS/ComputedValues.cpp
@@ -83,8 +83,6 @@ static_assert(to_underlying(PaintOrder::Stroke) == 1);
static_assert(to_underlying(PaintOrder::Markers) == 2);
static_assert(to_underlying(TextAnchor::Start) == 0);
static_assert(to_underlying(ShapeRendering::Auto) == 0);
-static_assert(sizeof(Size) == sizeof(ComputedValuesFFI::ComputedSize));
-static_assert(alignof(Size) == alignof(ComputedValuesFFI::ComputedSize));
static_assert(sizeof(RustStyleValueHandle) == sizeof(StyleValueFFI::StyleValueData const*));
static_assert(alignof(RustStyleValueHandle) == alignof(StyleValueFFI::StyleValueData const*));
diff --git a/Libraries/LibWeb/CSS/ComputedValues.h b/Libraries/LibWeb/CSS/ComputedValues.h
index 5310a98cdcc..436f26ebe4e 100644
--- a/Libraries/LibWeb/CSS/ComputedValues.h
+++ b/Libraries/LibWeb/CSS/ComputedValues.h
@@ -30,7 +30,6 @@
#include <LibWeb/CSS/PseudoElement.h>
#include <LibWeb/CSS/Ratio.h>
#include <LibWeb/CSS/ResolvedTransform.h>
-#include <LibWeb/CSS/Size.h>
#include <LibWeb/CSS/StyleRecordID.h>
#include <LibWeb/CSS/StyleStructRef.h>
#include <LibWeb/CSS/StyleValues/AbstractImageStyleValue.h>
@@ -525,8 +524,8 @@ public:
ComputedFilterView backdrop_filter() const { return m_noninherited.effects->backdrop_filter_value(); }
ComputedFilterView filter() const { return m_noninherited.effects->filter_value(); }
ReadonlySpan<ShadowData> box_shadow() const { return m_noninherited.effects->box_shadow_span(); }
- Size const& width() const { return Size::view(m_noninherited.sizing->width); }
- Size const& height() const { return Size::view(m_noninherited.sizing->height); }
+ ComputedValuesFFI::ComputedSize const& width() const { return m_noninherited.sizing->width; }
+ ComputedValuesFFI::ComputedSize const& height() const { return m_noninherited.sizing->height; }
Variant<VerticalAlign, LengthPercentage> vertical_align() const
{
auto const& value = m_noninherited.box->vertical_align;
@@ -1091,27 +1090,7 @@ public:
bool operator==(AlignmentValues const& other) const
{
- auto gaps_equal = [](auto const& first, auto const& second) {
- if (first.is_normal || second.is_normal)
- return first.is_normal == second.is_normal;
- return LengthPercentage::view(first.value) == LengthPercentage::view(second.value);
- };
- return webkit_box_orient == other.webkit_box_orient
- && flex_direction == other.flex_direction
- && flex_wrap == other.flex_wrap
- && flex_basis.is_content == other.flex_basis.is_content
- && Size::view(flex_basis.size) == Size::view(other.flex_basis.size)
- && flex_grow == other.flex_grow
- && flex_shrink == other.flex_shrink
- && order == other.order
- && align_content == other.align_content
- && align_items == other.align_items
- && align_self == other.align_self
- && justify_content == other.justify_content
- && justify_items == other.justify_items
- && justify_self == other.justify_self
- && gaps_equal(column_gap, other.column_gap)
- && gaps_equal(row_gap, other.row_gap);
+ return ComputedValuesFFI::rust_style_group_payloads_equal(style_group_index, this, &other);
}
};
@@ -1132,12 +1111,7 @@ public:
bool operator==(SizingValues const& other) const
{
- return Size::view(width) == Size::view(other.width)
- && Size::view(min_width) == Size::view(other.min_width)
- && Size::view(max_width) == Size::view(other.max_width)
- && Size::view(height) == Size::view(other.height)
- && Size::view(min_height) == Size::view(other.min_height)
- && Size::view(max_height) == Size::view(other.max_height);
+ return ComputedValuesFFI::rust_style_group_payloads_equal(style_group_index, this, &other);
}
};
@@ -1316,7 +1290,6 @@ public:
return;
m_values.m_inherited.text.access().letter_spacing = value;
}
- void set_width(Size value) { set_size(&ComputedValuesFFI::SizingValues::width, move(value)); }
void set_display(Display value)
{
if (m_values.display() == value)
@@ -1348,14 +1321,6 @@ public:
TransformValues::set_transformation(m_values.m_noninherited.transform.access().translate, value.ptr());
}
-private:
- void set_size(ComputedValuesFFI::ComputedSize ComputedValuesFFI::SizingValues::* member, Size value)
- {
- if (Size::view(m_values.m_noninherited.sizing.operator->()->*member) == value)
- return;
- Size::replace(m_values.m_noninherited.sizing.access().*member, move(value));
- }
-
ComputedValues& m_values;
};
diff --git a/Libraries/LibWeb/CSS/Size.h b/Libraries/LibWeb/CSS/Size.h
deleted file mode 100644
index b9d92a8b790..00000000000
--- a/Libraries/LibWeb/CSS/Size.h
+++ /dev/null
@@ -1,231 +0,0 @@
-/*
- * Copyright (c) 2022, Andreas Kling <[email protected]>
- * Copyright (c) 2023, Sam Atkins <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <LibWeb/CSS/Length.h>
-#include <LibWeb/CSS/PercentageOr.h>
-#include <LibWeb/CSS/StyleValues/FunctionStyleValue.h>
-#include <LibWeb/CSS/StyleValues/KeywordStyleValue.h>
-#include <LibWeb/ComputedValuesRustFFI.h>
-
-namespace Web::CSS {
-
-class Size : public ComputedValuesFFI::ComputedSize {
-public:
- using Type = ComputedValuesFFI::ComputedSizeKind;
-
- Size(Size const& other)
- : ComputedSize { other.kind, { other.value.pointer ? StyleValueFFI::rust_style_value_retain(static_cast<StyleValueFFI::StyleValueData const*>(other.value.pointer)) : nullptr } }
- {
- }
-
- Size(Size&& other)
- : ComputedSize { other.kind, { exchange(other.value.pointer, nullptr) } }
- {
- }
-
- Size& operator=(Size other)
- {
- swap(kind, other.kind);
- swap(value.pointer, other.value.pointer);
- return *this;
- }
-
- ~Size()
- {
- StyleValueFFI::rust_style_value_release(static_cast<StyleValueFFI::StyleValueData const*>(value.pointer));
- }
-
- static Size make_auto() { return Size { Type::Auto }; }
- static Size make_px(CSSPixels px) { return make_length(Length::make_px(px)); }
- static Size make_length(Length length) { return Size { Type::Length, move(length) }; }
- static Size make_percentage(Percentage percentage) { return Size { Type::Percentage, move(percentage) }; }
- static Size make_calculated(NonnullRefPtr<CalculatedStyleValue const> calculated) { return Size { Type::Calculated, move(calculated) }; }
- static Size make_min_content() { return Size { Type::MinContent }; }
- static Size make_max_content() { return Size { Type::MaxContent }; }
- static Size make_fit_content(LengthPercentage available_space) { return Size { Type::FitContent, move(available_space) }; }
- static Size make_fit_content() { return Size { Type::FitContent }; }
- static Size make_none() { return Size { Type::None }; }
-
- static Size from_style_value(NonnullRefPtr<StyleValue const> const& value)
- {
- if (value->is_keyword()) {
- switch (value->to_keyword()) {
- case Keyword::Auto:
- return make_auto();
- case Keyword::FitContent:
- return make_fit_content();
- case Keyword::MinContent:
- return make_min_content();
- case Keyword::MaxContent:
- return make_max_content();
- case Keyword::None:
- return make_none();
- default:
- VERIFY_NOT_REACHED();
- }
- }
- if (value->is_function() && value->as_function().name() == "fit-content"_utf16_fly_string)
- return make_fit_content(LengthPercentage::from_style_value(value->as_function().value()));
- if (value->is_calculated())
- return make_calculated(value->as_calculated());
- if (value->is_percentage())
- return make_percentage(value->as_percentage().percentage());
- if (value->is_length())
- return make_length(value->as_length().length());
-
- // FIXME: Support `anchor-size(..)`
- if (value->is_anchor_size())
- return make_none();
-
- dbgln("FIXME: Unsupported size value: `{}`, treating as `auto`", value->to_string(SerializationMode::Normal));
- return make_auto();
- }
-
- bool is_auto() const { return kind == Type::Auto; }
- bool is_calculated() const { return kind == Type::Calculated; }
- bool is_length() const { return kind == Type::Length; }
- bool is_percentage() const { return kind == Type::Percentage; }
- bool is_min_content() const { return kind == Type::MinContent; }
- bool is_max_content() const { return kind == Type::MaxContent; }
- bool is_fit_content() const { return kind == Type::FitContent; }
- bool is_none() const { return kind == Type::None; }
- Type type() const { return kind; }
-
- bool is_intrinsic_sizing_constraint() const { return is_min_content() || is_max_content() || is_fit_content(); }
- bool is_length_percentage() const { return is_length() || is_percentage() || is_calculated(); }
-
- [[nodiscard]] CSSPixels to_px(CSSPixels reference_value) const
- {
- if (!value.pointer)
- return 0;
- return length_percentage().to_px(reference_value);
- }
-
- bool contains_percentage() const
- {
- switch (kind) {
- case Type::Auto:
- case Type::MinContent:
- case Type::MaxContent:
- case Type::None:
- return false;
- case Type::FitContent:
- return value.pointer && length_percentage().contains_percentage();
- default:
- return length_percentage().contains_percentage();
- }
- }
-
- ValueComparingNonnullRefPtr<CalculatedStyleValue const> calculated() const
- {
- VERIFY(is_calculated());
- return length_percentage().calculated();
- }
-
- Length length() const
- {
- VERIFY(is_length());
- return length_percentage().length();
- }
-
- Percentage percentage() const
- {
- VERIFY(is_percentage());
- return length_percentage().percentage();
- }
-
- LengthPercentage const& length_percentage() const
- {
- VERIFY(value.pointer);
- return LengthPercentage::view(value);
- }
-
- Optional<LengthPercentage> fit_content_available_space() const
- {
- VERIFY(is_fit_content());
- if (!value.pointer)
- return {};
- return length_percentage();
- }
-
- void serialize(StringBuilder& builder, SerializationMode mode) const
- {
- switch (kind) {
- case Type::Auto:
- builder.append("auto"sv);
- break;
- case Type::Calculated:
- case Type::Length:
- case Type::Percentage:
- length_percentage().serialize(builder, mode);
- break;
- case Type::MinContent:
- builder.append("min-content"sv);
- break;
- case Type::MaxContent:
- builder.append("max-content"sv);
- break;
- case Type::FitContent:
- if (!value.pointer) {
- builder.append("fit-content"sv);
- } else {
- builder.append("fit-content("sv);
- length_percentage().serialize(builder, mode);
- builder.append(")"sv);
- }
- break;
- case Type::None:
- builder.append("none"sv);
- break;
- }
- }
-
- String to_string(SerializationMode mode) const
- {
- StringBuilder builder;
- serialize(builder, mode);
- return MUST(builder.to_string());
- }
- bool operator==(Size const& other) const
- {
- if (kind != other.kind)
- return false;
- if (!value.pointer || !other.value.pointer)
- return value.pointer == other.value.pointer;
- return length_percentage() == other.length_percentage();
- }
-
- static Size const& view(ComputedValuesFFI::ComputedSize const& size)
- {
- static_assert(sizeof(Size) == sizeof(size));
- return reinterpret_cast<Size const&>(size);
- }
-
- static void replace(ComputedValuesFFI::ComputedSize& target, Size replacement)
- {
- swap(target.kind, replacement.kind);
- swap(target.value.pointer, replacement.value.pointer);
- }
-
-private:
- explicit Size(Type type, Optional<LengthPercentage> length_percentage = {})
- : ComputedSize { type, { length_percentage.has_value() ? length_percentage->leak_data() : nullptr } }
- {
- }
-};
-
-}
-
-template<>
-struct AK::Formatter<Web::CSS::Size> : Formatter<StringView> {
- ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Size const& size)
- {
- return Formatter<StringView>::format(builder, size.to_string(Web::CSS::SerializationMode::Normal));
- }
-};
diff --git a/Libraries/LibWeb/Forward.h b/Libraries/LibWeb/Forward.h
index 721d4a55d5f..d515961a148 100644
--- a/Libraries/LibWeb/Forward.h
+++ b/Libraries/LibWeb/Forward.h
@@ -363,7 +363,6 @@ class ScreenOrientation;
class Selector;
class ShadowStyleValue;
class ShorthandStyleValue;
-class Size;
class StringStyleValue;
class StyleComputer;
class StylePropertyMap;
diff --git a/Libraries/LibWeb/Layout/Node.h b/Libraries/LibWeb/Layout/Node.h
index 912e66b1cd2..bbe826b93d6 100644
--- a/Libraries/LibWeb/Layout/Node.h
+++ b/Libraries/LibWeb/Layout/Node.h
@@ -513,12 +513,6 @@ public:
CSSPixels line_height() const { return style_group<CSS::ComputedValues::FontValues>().line_height_used; }
CSSPixels font_size() const { return style_group<CSS::ComputedValues::FontValues>().font_size; }
Gfx::FontCascadeList const& font_list() const { return style_group<CSS::ComputedValues::FontValues>().font_list_value(); }
- CSS::Size const& width() const { return CSS::Size::view(style_group<CSS::ComputedValues::SizingValues>().width); }
- CSS::Size const& min_width() const { return CSS::Size::view(style_group<CSS::ComputedValues::SizingValues>().min_width); }
- CSS::Size const& max_width() const { return CSS::Size::view(style_group<CSS::ComputedValues::SizingValues>().max_width); }
- CSS::Size const& height() const { return CSS::Size::view(style_group<CSS::ComputedValues::SizingValues>().height); }
- CSS::Size const& min_height() const { return CSS::Size::view(style_group<CSS::ComputedValues::SizingValues>().min_height); }
- CSS::Size const& max_height() const { return CSS::Size::view(style_group<CSS::ComputedValues::SizingValues>().max_height); }
CSS::LengthBox inset() const { return length_box(style_group<CSS::ComputedValues::SurroundValues>().inset); }
CSS::LengthBox margin() const { return length_box(style_group<CSS::ComputedValues::SurroundValues>().margin); }
CSS::LengthBox padding() const { return length_box(style_group<CSS::ComputedValues::SurroundValues>().padding); }
diff --git a/Libraries/LibWeb/Page/ElementResizeAction.cpp b/Libraries/LibWeb/Page/ElementResizeAction.cpp
index 2ea554e8894..a78df78da78 100644
--- a/Libraries/LibWeb/Page/ElementResizeAction.cpp
+++ b/Libraries/LibWeb/Page/ElementResizeAction.cpp
@@ -76,18 +76,16 @@ void ElementResizeAction::handle_pointer_move(CSSPixelPoint pointer_position)
auto reference_basis = containing_block_padding_box_size(layout_node);
if (reference_basis.has_value()) {
- if (auto const& min_width = layout_node.min_width(); !min_width.is_auto()) {
- css_width = max(css_width, min_width.to_px(reference_basis->width()));
- }
- if (auto const& max_width = layout_node.max_width(); !max_width.is_none()) {
- css_width = min(css_width, max_width.to_px(reference_basis->width()));
- }
- if (auto const& min_height = layout_node.min_height(); !min_height.is_auto()) {
- css_height = max(css_height, min_height.to_px(reference_basis->height()));
- }
- if (auto const& max_height = layout_node.max_height(); !max_height.is_none()) {
- css_height = min(css_height, max_height.to_px(reference_basis->height()));
- }
+ double width = css_width.to_double();
+ double height = css_height.to_double();
+ CSS::ComputedValuesFFI::rust_sizing_values_clamp_size(
+ &layout_node.style_group<CSS::ComputedValues::SizingValues>(),
+ reference_basis->width().to_double(),
+ reference_basis->height().to_double(),
+ &width,
+ &height);
+ css_width = CSSPixels::nearest_value_for(width);
+ css_height = CSSPixels::nearest_value_for(height);
}
if (layout_node.box_sizing() == CSS::BoxSizing::ContentBox) {
auto const metrics = Painting::box_model(layout_node);
diff --git a/Libraries/LibWeb/Rust/src/css/computed_values.rs b/Libraries/LibWeb/Rust/src/css/computed_values.rs
index a5e90a0713a..eeac18cd1d8 100644
--- a/Libraries/LibWeb/Rust/src/css/computed_values.rs
+++ b/Libraries/LibWeb/Rust/src/css/computed_values.rs
@@ -1946,6 +1946,49 @@ pub unsafe extern "C" fn rust_style_group_payloads_equal(
unsafe { style_group_payloads_equal(group_index, a, b) }
}
+/// Clamps a border box size by the min and max sizes of a sizing group that resolve against the given basis, as
+/// resizing an element by its resizer does.
+#[unsafe(no_mangle)]
+pub extern "C" fn rust_sizing_values_clamp_size(
+ values: &SizingValues,
+ basis_width: f64,
+ basis_height: f64,
+ width: &mut f64,
+ height: &mut f64,
+) {
+ use crate::css::css_pixels::CssPixels;
+ let clamp = |size: f64, basis: f64, minimum: &ComputedSize, maximum: &ComputedSize| {
+ let basis = CssPixels::nearest_value_for(basis);
+ let mut size = size;
+ if minimum.is_length_percentage() {
+ size = size.max(minimum.to_px(basis).to_double());
+ }
+ if maximum.is_length_percentage() {
+ size = size.min(maximum.to_px(basis).to_double());
+ }
+ size
+ };
+ *width = clamp(*width, basis_width, &values.min_width, &values.max_width);
+ *height = clamp(*height, basis_height, &values.min_height, &values.max_height);
+}
+
+/// The pixels of a computed size given as an absolute length, written to `pixels`. Returns false for any other size.
+#[unsafe(no_mangle)]
+pub extern "C" fn rust_computed_size_absolute_length_px(size: &ComputedSize, pixels: &mut f64) -> bool {
+ let Some(StyleValueData::Length { value, unit }) = size.value.style_value() else {
+ return false;
+ };
+ if size.kind != ComputedSizeKind::Length {
+ return false;
+ }
+ let ratio = crate::css::style_compute::LENGTH_UNIT_CANONICAL_PX_RATIOS[*unit as usize];
+ if !ratio.is_finite() {
+ return false;
+ }
+ *pixels = value * ratio;
+ true
+}
+
/// One field of a style group the generic builder can populate or check: a pokeable simple field
/// at `offset` in the group's payload, or a constraint requiring the value to be one that leaves
/// the payload's initial value standing.
diff --git a/Libraries/LibWeb/SVG/SVGDecodedImageData.cpp b/Libraries/LibWeb/SVG/SVGDecodedImageData.cpp
index be481dc833a..23d05f097b7 100644
--- a/Libraries/LibWeb/SVG/SVGDecodedImageData.cpp
+++ b/Libraries/LibWeb/SVG/SVGDecodedImageData.cpp
@@ -383,11 +383,11 @@ CSS::SizeWithAspectRatio const& SVGDecodedImageData::natural_size() const
m_document->update_style();
auto const* sizing_values = m_root_element->style_group<CSS::ComputedValues::SizingValues>();
VERIFY(sizing_values);
- auto absolute_length = [](auto const& size_handle) -> Optional<CSSPixels> {
- auto const& size = CSS::Size::view(size_handle);
- if (size.is_length() && size.length().is_absolute())
- return size.length().absolute_length_to_px();
- return {};
+ auto absolute_length = [](auto const& size) -> Optional<CSSPixels> {
+ double pixels = 0;
+ if (!CSS::ComputedValuesFFI::rust_computed_size_absolute_length_px(&size, &pixels))
+ return {};
+ return CSSPixels::nearest_value_for(pixels);
};
natural_size.width = absolute_length(sizing_values->width);
natural_size.height = absolute_length(sizing_values->height);