From 344eb98b3cc0043a6f9fc331f727ce883e0d6959 Mon Sep 17 00:00:00 2001 From: MacDue Date: Sun, 3 Mar 2024 20:33:40 +0000 Subject: [PATCH] LibWeb: Add helper to convert CSS LengthPercentage to SVGLength --- Userland/Libraries/LibWeb/SVG/SVGLength.cpp | 48 +++++++++++++++++++++ Userland/Libraries/LibWeb/SVG/SVGLength.h | 2 + 2 files changed, 50 insertions(+) diff --git a/Userland/Libraries/LibWeb/SVG/SVGLength.cpp b/Userland/Libraries/LibWeb/SVG/SVGLength.cpp index d5671444e3..7ec091617d 100644 --- a/Userland/Libraries/LibWeb/SVG/SVGLength.cpp +++ b/Userland/Libraries/LibWeb/SVG/SVGLength.cpp @@ -5,17 +5,65 @@ */ #include +#include #include namespace Web::SVG { JS_DEFINE_ALLOCATOR(SVGLength); +// Same as SVGLength.idl +constexpr unsigned short SVG_LENGTHTYPE_UNKNOWN = 0; +constexpr unsigned short SVG_LENGTHTYPE_NUMBER = 1; +constexpr unsigned short SVG_LENGTHTYPE_PERCENTAGE = 2; +constexpr unsigned short SVG_LENGTHTYPE_EMS = 3; +constexpr unsigned short SVG_LENGTHTYPE_EXS = 4; +constexpr unsigned short SVG_LENGTHTYPE_PX = 5; +constexpr unsigned short SVG_LENGTHTYPE_CM = 6; +constexpr unsigned short SVG_LENGTHTYPE_MM = 7; +constexpr unsigned short SVG_LENGTHTYPE_IN = 8; +constexpr unsigned short SVG_LENGTHTYPE_PT = 9; +constexpr unsigned short SVG_LENGTHTYPE_PC = 10; + JS::NonnullGCPtr SVGLength::create(JS::Realm& realm, u8 unit_type, float value) { return realm.heap().allocate(realm, realm, unit_type, value); } +JS::NonnullGCPtr SVGLength::from_length_percentage(JS::Realm& realm, CSS::LengthPercentage const& length_percentage) +{ + // FIXME: We can't tell if a CSS::LengthPercentage was a unitless length. + (void)SVG_LENGTHTYPE_NUMBER; + if (length_percentage.is_percentage()) + return SVGLength::create(realm, SVG_LENGTHTYPE_PERCENTAGE, length_percentage.percentage().value()); + if (length_percentage.is_length()) + return SVGLength::create( + realm, [&] { + switch (length_percentage.length().type()) { + case CSS::Length::Type::Em: + return SVG_LENGTHTYPE_EMS; + case CSS::Length::Type::Ex: + return SVG_LENGTHTYPE_EXS; + case CSS::Length::Type::Px: + return SVG_LENGTHTYPE_PX; + case CSS::Length::Type::Cm: + return SVG_LENGTHTYPE_CM; + case CSS::Length::Type::Mm: + return SVG_LENGTHTYPE_MM; + case CSS::Length::Type::In: + return SVG_LENGTHTYPE_IN; + case CSS::Length::Type::Pt: + return SVG_LENGTHTYPE_PT; + case CSS::Length::Type::Pc: + return SVG_LENGTHTYPE_PC; + default: + return SVG_LENGTHTYPE_UNKNOWN; + } + }(), + length_percentage.length().raw_value()); + return SVGLength::create(realm, SVG_LENGTHTYPE_UNKNOWN, 0); +} + SVGLength::SVGLength(JS::Realm& realm, u8 unit_type, float value) : PlatformObject(realm) , m_unit_type(unit_type) diff --git a/Userland/Libraries/LibWeb/SVG/SVGLength.h b/Userland/Libraries/LibWeb/SVG/SVGLength.h index b942654f4f..dc295a2d4f 100644 --- a/Userland/Libraries/LibWeb/SVG/SVGLength.h +++ b/Userland/Libraries/LibWeb/SVG/SVGLength.h @@ -25,6 +25,8 @@ public: float value() const { return m_value; } WebIDL::ExceptionOr set_value(float value); + [[nodiscard]] static JS::NonnullGCPtr from_length_percentage(JS::Realm&, CSS::LengthPercentage const&); + private: SVGLength(JS::Realm&, u8 unit_type, float value);