From 21ba485fd334212ba0dc1251fb164dceafdbca8d Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Thu, 30 Sep 2021 22:57:35 +0200 Subject: [PATCH] LibWeb: Resolve cyclic dependency: Length and CalculatedStyleValue Previously: Length (and all nearly all of its inline method definitions) depended on the definition of class CalculatedStyleValue. Meanwhile, CalculatedStyleValue (and nearly all of its namespaced structs) depended on the definition of class Length. Thus, a compilation unit that (for example) only contains #include would fail to compile. This patch resolves this issue by pushing the inline definition of various Web::CSS::Length methods into a different file. --- Userland/Libraries/LibWeb/CSS/Length.cpp | 49 ++++++++++++++++++++++++ Userland/Libraries/LibWeb/CSS/Length.h | 47 ++++++----------------- 2 files changed, 60 insertions(+), 36 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/Length.cpp b/Userland/Libraries/LibWeb/CSS/Length.cpp index db59cdccce..c1c1ae9624 100644 --- a/Userland/Libraries/LibWeb/CSS/Length.cpp +++ b/Userland/Libraries/LibWeb/CSS/Length.cpp @@ -16,6 +16,55 @@ namespace Web::CSS { +Length::Length() = default; +Length::Length(int value, Type type) + : m_type(type) + , m_value(value) +{ +} +Length::Length(float value, Type type) + : m_type(type) + , m_value(value) +{ +} + +Length Length::make_auto() +{ + return Length(0, Type::Auto); +} +Length Length::make_px(float value) +{ + return Length(value, Type::Px); +} + +Length Length::resolved(const Length& fallback_for_undefined, const Layout::Node& layout_node, float reference_for_percent) const +{ + if (is_undefined()) + return fallback_for_undefined; + if (is_calculated()) + return Length(resolve_calculated_value(layout_node, reference_for_percent), Type::Px); + if (is_percentage()) + return make_px(raw_value() / 100.0f * reference_for_percent); + if (is_relative()) + return make_px(to_px(layout_node)); + return *this; +} + +Length Length::resolved_or_auto(const Layout::Node& layout_node, float reference_for_percent) const +{ + return resolved(make_auto(), layout_node, reference_for_percent); +} + +Length Length::resolved_or_zero(const Layout::Node& layout_node, float reference_for_percent) const +{ + return resolved(make_px(0), layout_node, reference_for_percent); +} + +void Length::set_calculated_style(CalculatedStyleValue* value) +{ + m_calculated_style = value; +} + float Length::relative_length_to_px(Gfx::IntRect const& viewport_rect, Gfx::FontMetrics const& font_metrics, float root_font_size) const { switch (m_type) { diff --git a/Userland/Libraries/LibWeb/CSS/Length.h b/Userland/Libraries/LibWeb/CSS/Length.h index 0e05727ba0..2fa50e4527 100644 --- a/Userland/Libraries/LibWeb/CSS/Length.h +++ b/Userland/Libraries/LibWeb/CSS/Length.h @@ -36,43 +36,18 @@ public: Vmin, }; - Length() = default; - Length(int value, Type type) - : m_type(type) - , m_value(value) - { - } - Length(float value, Type type) - : m_type(type) - , m_value(value) - { - } + // We have a RefPtr member, but can't include the header StyleValue.h as it includes + // this file already. To break the cyclic dependency, we must move all method definitions out. + Length(); + Length(int value, Type type); + Length(float value, Type type); - static Length make_auto() { return Length(0, Type::Auto); } - static Length make_px(float value) { return Length(value, Type::Px); } + static Length make_auto(); + static Length make_px(float value); - Length resolved(const Length& fallback_for_undefined, const Layout::Node& layout_node, float reference_for_percent) const - { - if (is_undefined()) - return fallback_for_undefined; - if (is_calculated()) - return Length(resolve_calculated_value(layout_node, reference_for_percent), Type::Px); - if (is_percentage()) - return make_px(raw_value() / 100.0f * reference_for_percent); - if (is_relative()) - return make_px(to_px(layout_node)); - return *this; - } - - Length resolved_or_auto(const Layout::Node& layout_node, float reference_for_percent) const - { - return resolved(make_auto(), layout_node, reference_for_percent); - } - - Length resolved_or_zero(const Layout::Node& layout_node, float reference_for_percent) const - { - return resolved(make_px(0), layout_node, reference_for_percent); - } + Length resolved(const Length& fallback_for_undefined, const Layout::Node& layout_node, float reference_for_percent) const; + Length resolved_or_auto(const Layout::Node& layout_node, float reference_for_percent) const; + Length resolved_or_zero(const Layout::Node& layout_node, float reference_for_percent) const; bool is_undefined_or_auto() const { return m_type == Type::Undefined || m_type == Type::Auto; } bool is_undefined() const { return m_type == Type::Undefined; } @@ -157,7 +132,7 @@ public: return !(*this == other); } - void set_calculated_style(CalculatedStyleValue* value) { m_calculated_style = value; } + void set_calculated_style(CalculatedStyleValue* value); float relative_length_to_px(Gfx::IntRect const& viewport_rect, Gfx::FontMetrics const& font_metrics, float root_font_size) const;