diff --git a/Libraries/LibWeb/CSS/Angle.cpp b/Libraries/LibWeb/CSS/Angle.cpp index 24701a5a522..11c26f25206 100644 --- a/Libraries/LibWeb/CSS/Angle.cpp +++ b/Libraries/LibWeb/CSS/Angle.cpp @@ -12,20 +12,20 @@ namespace Web::CSS { -Angle::Angle(double value, Type type) - : m_type(type) +Angle::Angle(double value, AngleUnit unit) + : m_unit(unit) , m_value(value) { } Angle Angle::make_degrees(double value) { - return { value, Type::Deg }; + return { value, AngleUnit::Deg }; } Angle Angle::percentage_of(Percentage const& percentage) const { - return Angle { percentage.as_fraction() * m_value, m_type }; + return Angle { percentage.as_fraction() * m_value, m_unit }; } String Angle::to_string(SerializationMode serialization_mode) const @@ -48,14 +48,14 @@ String Angle::to_string(SerializationMode serialization_mode) const double Angle::to_degrees() const { - switch (m_type) { - case Type::Deg: + switch (m_unit) { + case AngleUnit::Deg: return m_value; - case Type::Grad: + case AngleUnit::Grad: return m_value * (360.0 / 400.0); - case Type::Rad: + case AngleUnit::Rad: return AK::to_degrees(m_value); - case Type::Turn: + case AngleUnit::Turn: return m_value * 360.0; } VERIFY_NOT_REACHED(); @@ -66,38 +66,6 @@ double Angle::to_radians() const return AK::to_radians(to_degrees()); } -StringView Angle::unit_name() const -{ - switch (m_type) { - case Type::Deg: - return "deg"sv; - case Type::Grad: - return "grad"sv; - case Type::Rad: - return "rad"sv; - case Type::Turn: - return "turn"sv; - } - VERIFY_NOT_REACHED(); -} - -Optional Angle::unit_from_name(StringView name) -{ - if (name.equals_ignoring_ascii_case("deg"sv)) { - return Type::Deg; - } - if (name.equals_ignoring_ascii_case("grad"sv)) { - return Type::Grad; - } - if (name.equals_ignoring_ascii_case("rad"sv)) { - return Type::Rad; - } - if (name.equals_ignoring_ascii_case("turn"sv)) { - return Type::Turn; - } - return {}; -} - Angle Angle::resolve_calculated(NonnullRefPtr const& calculated, Layout::Node const& layout_node, Angle const& reference_value) { CalculationResolutionContext context { diff --git a/Libraries/LibWeb/CSS/Angle.h b/Libraries/LibWeb/CSS/Angle.h index 339558287d8..7ae99753492 100644 --- a/Libraries/LibWeb/CSS/Angle.h +++ b/Libraries/LibWeb/CSS/Angle.h @@ -15,16 +15,7 @@ namespace Web::CSS { class Angle { public: - enum class Type : u8 { - Deg, - Grad, - Rad, - Turn, - }; - - static Optional unit_from_name(StringView); - - Angle(double value, Type type); + Angle(double value, AngleUnit unit); static Angle make_degrees(double); Angle percentage_of(Percentage const&) const; @@ -33,13 +24,13 @@ public: double to_degrees() const; double to_radians() const; - Type type() const { return m_type; } double raw_value() const { return m_value; } - StringView unit_name() const; + AngleUnit unit() const { return m_unit; } + StringView unit_name() const { return CSS::to_string(m_unit); } bool operator==(Angle const& other) const { - return m_type == other.m_type && m_value == other.m_value; + return m_unit == other.m_unit && m_value == other.m_value; } int operator<=>(Angle const& other) const @@ -57,7 +48,7 @@ public: static Angle resolve_calculated(NonnullRefPtr const&, Layout::Node const&, Angle const& reference_value); private: - Type m_type; + AngleUnit m_unit; double m_value { 0 }; }; diff --git a/Libraries/LibWeb/CSS/ComputedProperties.cpp b/Libraries/LibWeb/CSS/ComputedProperties.cpp index 164d8f0b559..11232e0bd50 100644 --- a/Libraries/LibWeb/CSS/ComputedProperties.cpp +++ b/Libraries/LibWeb/CSS/ComputedProperties.cpp @@ -378,17 +378,17 @@ CSSPixels ComputedProperties::compute_line_height(CSSPixelRect const& viewport_r return line_height.as_length().length().to_px(viewport_rect, font_metrics, root_font_metrics); if (line_height.is_number()) - return Length(line_height.as_number().number(), Length::Type::Em).to_px(viewport_rect, font_metrics, root_font_metrics); + return Length(line_height.as_number().number(), LengthUnit::Em).to_px(viewport_rect, font_metrics, root_font_metrics); if (line_height.is_percentage()) { // Percentages are relative to 1em. https://www.w3.org/TR/css-inline-3/#valdef-line-height-percentage auto const& percentage = line_height.as_percentage().percentage(); - return Length(percentage.as_fraction(), Length::Type::Em).to_px(viewport_rect, font_metrics, root_font_metrics); + return Length(percentage.as_fraction(), LengthUnit::Em).to_px(viewport_rect, font_metrics, root_font_metrics); } if (line_height.is_calculated()) { CalculationResolutionContext context { - .percentage_basis = Length(1, Length::Type::Em), + .percentage_basis = Length(1, LengthUnit::Em), .length_resolution_context = Length::ResolutionContext { viewport_rect, font_metrics, root_font_metrics }, }; if (line_height.as_calculated().resolves_to_number()) { @@ -397,7 +397,7 @@ CSSPixels ComputedProperties::compute_line_height(CSSPixelRect const& viewport_r dbgln("FIXME: Failed to resolve calc() line-height (number): {}", line_height.as_calculated().to_string(SerializationMode::Normal)); return CSSPixels::nearest_value_for(m_font_list->first().pixel_metrics().line_spacing()); } - return Length(resolved.value(), Length::Type::Em).to_px(viewport_rect, font_metrics, root_font_metrics); + return Length(resolved.value(), LengthUnit::Em).to_px(viewport_rect, font_metrics, root_font_metrics); } auto resolved = line_height.as_calculated().resolve_length_deprecated(context); @@ -565,7 +565,7 @@ Length ComputedProperties::border_spacing_horizontal(Layout::Node const& layout_ if (style_value.is_length()) return style_value.as_length().length(); if (style_value.is_calculated()) - return style_value.as_calculated().resolve_length_deprecated({ .length_resolution_context = Length::ResolutionContext::for_layout_node(layout_node) }).value_or(Length(0, Length::Type::Px)); + return style_value.as_calculated().resolve_length_deprecated({ .length_resolution_context = Length::ResolutionContext::for_layout_node(layout_node) }).value_or(Length::make_px(0)); return {}; }; @@ -587,7 +587,7 @@ Length ComputedProperties::border_spacing_vertical(Layout::Node const& layout_no if (style_value.is_length()) return style_value.as_length().length(); if (style_value.is_calculated()) - return style_value.as_calculated().resolve_length_deprecated({ .length_resolution_context = Length::ResolutionContext::for_layout_node(layout_node) }).value_or(Length(0, Length::Type::Px)); + return style_value.as_calculated().resolve_length_deprecated({ .length_resolution_context = Length::ResolutionContext::for_layout_node(layout_node) }).value_or(Length::make_px(0)); return {}; }; diff --git a/Libraries/LibWeb/CSS/Flex.cpp b/Libraries/LibWeb/CSS/Flex.cpp index 06f958e0c64..a0c26354604 100644 --- a/Libraries/LibWeb/CSS/Flex.cpp +++ b/Libraries/LibWeb/CSS/Flex.cpp @@ -10,20 +10,20 @@ namespace Web::CSS { -Flex::Flex(double value, Type type) - : m_type(type) +Flex::Flex(double value, FlexUnit unit) + : m_unit(unit) , m_value(value) { } Flex Flex::make_fr(double value) { - return { value, Type::Fr }; + return { value, FlexUnit::Fr }; } Flex Flex::percentage_of(Percentage const& percentage) const { - return Flex { percentage.as_fraction() * m_value, m_type }; + return Flex { percentage.as_fraction() * m_value, m_unit }; } String Flex::to_string(SerializationMode serialization_mode) const @@ -44,28 +44,11 @@ String Flex::to_string(SerializationMode serialization_mode) const double Flex::to_fr() const { - switch (m_type) { - case Type::Fr: + switch (m_unit) { + case FlexUnit::Fr: return m_value; } VERIFY_NOT_REACHED(); } -StringView Flex::unit_name() const -{ - switch (m_type) { - case Type::Fr: - return "fr"sv; - } - VERIFY_NOT_REACHED(); -} - -Optional Flex::unit_from_name(StringView name) -{ - if (name.equals_ignoring_ascii_case("fr"sv)) - return Type::Fr; - - return {}; -} - } diff --git a/Libraries/LibWeb/CSS/Flex.h b/Libraries/LibWeb/CSS/Flex.h index e8f8dcb9b2b..a2627b7f334 100644 --- a/Libraries/LibWeb/CSS/Flex.h +++ b/Libraries/LibWeb/CSS/Flex.h @@ -6,9 +6,9 @@ #pragma once -#include #include #include +#include #include namespace Web::CSS { @@ -16,26 +16,20 @@ namespace Web::CSS { // https://drafts.csswg.org/css-grid-2/#typedef-flex class Flex { public: - enum class Type : u8 { - Fr, - }; - - static Optional unit_from_name(StringView); - - Flex(double value, Type type); + Flex(double value, FlexUnit unit); static Flex make_fr(double); Flex percentage_of(Percentage const&) const; String to_string(SerializationMode = SerializationMode::Normal) const; double to_fr() const; - Type type() const { return m_type; } double raw_value() const { return m_value; } - StringView unit_name() const; + FlexUnit unit() const { return m_unit; } + StringView unit_name() const { return CSS::to_string(m_unit); } bool operator==(Flex const& other) const { - return m_type == other.m_type && m_value == other.m_value; + return m_unit == other.m_unit && m_value == other.m_value; } int operator<=>(Flex const& other) const @@ -51,7 +45,7 @@ public: } private: - Type m_type; + FlexUnit m_unit; double m_value { 0 }; }; diff --git a/Libraries/LibWeb/CSS/Frequency.cpp b/Libraries/LibWeb/CSS/Frequency.cpp index c58bb71ec1b..9a0b7f4f33b 100644 --- a/Libraries/LibWeb/CSS/Frequency.cpp +++ b/Libraries/LibWeb/CSS/Frequency.cpp @@ -11,20 +11,20 @@ namespace Web::CSS { -Frequency::Frequency(double value, Type type) - : m_type(type) +Frequency::Frequency(double value, FrequencyUnit unit) + : m_unit(unit) , m_value(value) { } Frequency Frequency::make_hertz(double value) { - return { value, Type::Hz }; + return { value, FrequencyUnit::Hz }; } Frequency Frequency::percentage_of(Percentage const& percentage) const { - return Frequency { percentage.as_fraction() * m_value, m_type }; + return Frequency { percentage.as_fraction() * m_value, m_unit }; } String Frequency::to_string(SerializationMode serialization_mode) const @@ -47,36 +47,15 @@ String Frequency::to_string(SerializationMode serialization_mode) const double Frequency::to_hertz() const { - switch (m_type) { - case Type::Hz: + switch (m_unit) { + case FrequencyUnit::Hz: return m_value; - case Type::kHz: + case FrequencyUnit::KHz: return m_value * 1000; } VERIFY_NOT_REACHED(); } -StringView Frequency::unit_name() const -{ - switch (m_type) { - case Type::Hz: - return "hz"sv; - case Type::kHz: - return "khz"sv; - } - VERIFY_NOT_REACHED(); -} - -Optional Frequency::unit_from_name(StringView name) -{ - if (name.equals_ignoring_ascii_case("hz"sv)) { - return Type::Hz; - } else if (name.equals_ignoring_ascii_case("khz"sv)) { - return Type::kHz; - } - return {}; -} - Frequency Frequency::resolve_calculated(NonnullRefPtr const& calculated, Layout::Node const& layout_node, Frequency const& reference_value) { CalculationResolutionContext context { diff --git a/Libraries/LibWeb/CSS/Frequency.h b/Libraries/LibWeb/CSS/Frequency.h index f4a50550842..75cf784a8bd 100644 --- a/Libraries/LibWeb/CSS/Frequency.h +++ b/Libraries/LibWeb/CSS/Frequency.h @@ -8,33 +8,27 @@ #include #include +#include #include namespace Web::CSS { class Frequency { public: - enum class Type : u8 { - Hz, - kHz - }; - - static Optional unit_from_name(StringView); - - Frequency(double value, Type type); + Frequency(double value, FrequencyUnit unit); static Frequency make_hertz(double); Frequency percentage_of(Percentage const&) const; String to_string(SerializationMode = SerializationMode::Normal) const; double to_hertz() const; - Type type() const { return m_type; } double raw_value() const { return m_value; } - StringView unit_name() const; + FrequencyUnit unit() const { return m_unit; } + StringView unit_name() const { return CSS::to_string(m_unit); } bool operator==(Frequency const& other) const { - return m_type == other.m_type && m_value == other.m_value; + return m_unit == other.m_unit && m_value == other.m_value; } int operator<=>(Frequency const& other) const @@ -52,7 +46,7 @@ public: static Frequency resolve_calculated(NonnullRefPtr const&, Layout::Node const&, Frequency const& reference_value); private: - Type m_type; + FrequencyUnit m_unit; double m_value { 0 }; }; diff --git a/Libraries/LibWeb/CSS/Interpolation.cpp b/Libraries/LibWeb/CSS/Interpolation.cpp index 56e5d1a5ab2..07b6d458825 100644 --- a/Libraries/LibWeb/CSS/Interpolation.cpp +++ b/Libraries/LibWeb/CSS/Interpolation.cpp @@ -1235,7 +1235,7 @@ static RefPtr interpolate_value_impl(DOM::Element& element, Ca auto const& from_length = from.as_length().length(); auto const& to_length = to.as_length().length(); auto interpolated_value = interpolate_raw(from_length.raw_value(), to_length.raw_value(), delta, calculation_context.accepted_type_ranges.get(ValueType::Length)); - return LengthStyleValue::create(Length(interpolated_value, from_length.type())); + return LengthStyleValue::create(Length(interpolated_value, from_length.unit())); } case StyleValue::Type::Number: { auto interpolated_value = interpolate_raw(from.as_number().number(), to.as_number().number(), delta, calculation_context.accepted_type_ranges.get(ValueType::Number)); @@ -1287,7 +1287,7 @@ static RefPtr interpolate_value_impl(DOM::Element& element, Ca return LengthOrAuto::make_auto(); // FIXME: Actually handle the units not matching. auto interpolated_value = interpolate_raw(from.length().raw_value(), to.length().raw_value(), delta, calculation_context.accepted_type_ranges.get(ValueType::Rect)); - return LengthOrAuto { Length { interpolated_value, from.length().type() } }; + return LengthOrAuto { Length { interpolated_value, from.length().unit() } }; }; return RectStyleValue::create({ diff --git a/Libraries/LibWeb/CSS/Length.cpp b/Libraries/LibWeb/CSS/Length.cpp index 56133937b6d..d751371287d 100644 --- a/Libraries/LibWeb/CSS/Length.cpp +++ b/Libraries/LibWeb/CSS/Length.cpp @@ -31,8 +31,8 @@ Length::FontMetrics::FontMetrics(CSSPixels font_size, Gfx::FontPixelMetrics cons { } -Length::Length(double value, Type type) - : m_type(type) +Length::Length(double value, LengthUnit unit) + : m_unit(unit) , m_value(value) { } @@ -40,7 +40,7 @@ Length::~Length() = default; Length Length::make_px(double value) { - return Length(value, Type::Px); + return Length(value, LengthUnit::Px); } Length Length::make_px(CSSPixels value) @@ -50,37 +50,37 @@ Length Length::make_px(CSSPixels value) Length Length::percentage_of(Percentage const& percentage) const { - return Length { percentage.as_fraction() * raw_value(), m_type }; + return Length { percentage.as_fraction() * raw_value(), m_unit }; } CSSPixels Length::font_relative_length_to_px(Length::FontMetrics const& font_metrics, Length::FontMetrics const& root_font_metrics) const { - switch (m_type) { - case Type::Em: + switch (m_unit) { + case LengthUnit::Em: return CSSPixels::nearest_value_for(m_value * font_metrics.font_size.to_double()); - case Type::Rem: + case LengthUnit::Rem: return CSSPixels::nearest_value_for(m_value * root_font_metrics.font_size.to_double()); - case Type::Ex: + case LengthUnit::Ex: return CSSPixels::nearest_value_for(m_value * font_metrics.x_height.to_double()); - case Type::Rex: + case LengthUnit::Rex: return CSSPixels::nearest_value_for(m_value * root_font_metrics.x_height.to_double()); - case Type::Cap: + case LengthUnit::Cap: return CSSPixels::nearest_value_for(m_value * font_metrics.cap_height.to_double()); - case Type::Rcap: + case LengthUnit::Rcap: return CSSPixels::nearest_value_for(m_value * root_font_metrics.cap_height.to_double()); - case Type::Ch: + case LengthUnit::Ch: return CSSPixels::nearest_value_for(m_value * font_metrics.zero_advance.to_double()); - case Type::Rch: + case LengthUnit::Rch: return CSSPixels::nearest_value_for(m_value * root_font_metrics.zero_advance.to_double()); - case Type::Ic: + case LengthUnit::Ic: // FIXME: Use the "advance measure of the “水” (CJK water ideograph, U+6C34) glyph" return CSSPixels::nearest_value_for(m_value * font_metrics.font_size.to_double()); - case Type::Ric: + case LengthUnit::Ric: // FIXME: Use the "advance measure of the “水” (CJK water ideograph, U+6C34) glyph" return CSSPixels::nearest_value_for(m_value * root_font_metrics.font_size.to_double()); - case Type::Lh: + case LengthUnit::Lh: return CSSPixels::nearest_value_for(m_value * font_metrics.line_height.to_double()); - case Type::Rlh: + case LengthUnit::Rlh: return CSSPixels::nearest_value_for(m_value * root_font_metrics.line_height.to_double()); default: VERIFY_NOT_REACHED(); @@ -89,38 +89,38 @@ CSSPixels Length::font_relative_length_to_px(Length::FontMetrics const& font_met CSSPixels Length::viewport_relative_length_to_px(CSSPixelRect const& viewport_rect) const { - switch (m_type) { - case Type::Vw: - case Type::Svw: - case Type::Lvw: - case Type::Dvw: + switch (m_unit) { + case LengthUnit::Vw: + case LengthUnit::Svw: + case LengthUnit::Lvw: + case LengthUnit::Dvw: return viewport_rect.width() * (CSSPixels::nearest_value_for(m_value) / 100); - case Type::Vh: - case Type::Svh: - case Type::Lvh: - case Type::Dvh: + case LengthUnit::Vh: + case LengthUnit::Svh: + case LengthUnit::Lvh: + case LengthUnit::Dvh: return viewport_rect.height() * (CSSPixels::nearest_value_for(m_value) / 100); - case Type::Vi: - case Type::Svi: - case Type::Lvi: - case Type::Dvi: + case LengthUnit::Vi: + case LengthUnit::Svi: + case LengthUnit::Lvi: + case LengthUnit::Dvi: // FIXME: Select the width or height based on which is the inline axis. return viewport_rect.width() * (CSSPixels::nearest_value_for(m_value) / 100); - case Type::Vb: - case Type::Svb: - case Type::Lvb: - case Type::Dvb: + case LengthUnit::Vb: + case LengthUnit::Svb: + case LengthUnit::Lvb: + case LengthUnit::Dvb: // FIXME: Select the width or height based on which is the block axis. return viewport_rect.height() * (CSSPixels::nearest_value_for(m_value) / 100); - case Type::Vmin: - case Type::Svmin: - case Type::Lvmin: - case Type::Dvmin: + case LengthUnit::Vmin: + case LengthUnit::Svmin: + case LengthUnit::Lvmin: + case LengthUnit::Dvmin: return min(viewport_rect.width(), viewport_rect.height()) * (CSSPixels::nearest_value_for(m_value) / 100); - case Type::Vmax: - case Type::Svmax: - case Type::Lvmax: - case Type::Dvmax: + case LengthUnit::Vmax: + case LengthUnit::Svmax: + case LengthUnit::Lvmax: + case LengthUnit::Dvmax: return max(viewport_rect.width(), viewport_rect.height()) * (CSSPixels::nearest_value_for(m_value) / 100); default: VERIFY_NOT_REACHED(); @@ -215,7 +215,7 @@ String Length::to_string(SerializationMode serialization_mode) const // FIXME: Manually skip this for px so we avoid rounding errors in absolute_length_to_px. // Maybe provide alternative functions that don't produce CSSPixels? - if (serialization_mode == SerializationMode::ResolvedValue && is_absolute() && m_type != Type::Px) { + if (serialization_mode == SerializationMode::ResolvedValue && is_absolute() && m_unit != LengthUnit::Px) { StringBuilder builder; serialize_a_number(builder, absolute_length_to_px().to_double()); builder.append("px"sv); @@ -227,192 +227,6 @@ String Length::to_string(SerializationMode serialization_mode) const return builder.to_string_without_validation(); } -StringView Length::unit_name() const -{ - switch (m_type) { - case Type::Em: - return "em"sv; - case Type::Rem: - return "rem"sv; - case Type::Ex: - return "ex"sv; - case Type::Rex: - return "rex"sv; - case Type::Cap: - return "cap"sv; - case Type::Rcap: - return "rcap"sv; - case Type::Ch: - return "ch"sv; - case Type::Rch: - return "rch"sv; - case Type::Ic: - return "ic"sv; - case Type::Ric: - return "ric"sv; - case Type::Lh: - return "lh"sv; - case Type::Rlh: - return "rlh"sv; - case Type::Vw: - return "vw"sv; - case Type::Svw: - return "svw"sv; - case Type::Lvw: - return "lvw"sv; - case Type::Dvw: - return "dvw"sv; - case Type::Vh: - return "vh"sv; - case Type::Svh: - return "svh"sv; - case Type::Lvh: - return "lvh"sv; - case Type::Dvh: - return "dvh"sv; - case Type::Vi: - return "vi"sv; - case Type::Svi: - return "svi"sv; - case Type::Lvi: - return "lvi"sv; - case Type::Dvi: - return "dvi"sv; - case Type::Vb: - return "vb"sv; - case Type::Svb: - return "svb"sv; - case Type::Lvb: - return "lvb"sv; - case Type::Dvb: - return "dvb"sv; - case Type::Vmin: - return "vmin"sv; - case Type::Svmin: - return "svmin"sv; - case Type::Lvmin: - return "lvmin"sv; - case Type::Dvmin: - return "dvmin"sv; - case Type::Vmax: - return "vmax"sv; - case Type::Svmax: - return "svmax"sv; - case Type::Lvmax: - return "lvmax"sv; - case Type::Dvmax: - return "dvmax"sv; - case Type::Cm: - return "cm"sv; - case Type::Mm: - return "mm"sv; - case Type::Q: - return "Q"sv; - case Type::In: - return "in"sv; - case Type::Pt: - return "pt"sv; - case Type::Pc: - return "pc"sv; - case Type::Px: - return "px"sv; - } - VERIFY_NOT_REACHED(); -} - -Optional Length::unit_from_name(StringView name) -{ - if (name.equals_ignoring_ascii_case("em"sv)) { - return Length::Type::Em; - } else if (name.equals_ignoring_ascii_case("rem"sv)) { - return Length::Type::Rem; - } else if (name.equals_ignoring_ascii_case("ex"sv)) { - return Length::Type::Ex; - } else if (name.equals_ignoring_ascii_case("rex"sv)) { - return Length::Type::Rex; - } else if (name.equals_ignoring_ascii_case("cap"sv)) { - return Length::Type::Cap; - } else if (name.equals_ignoring_ascii_case("rcap"sv)) { - return Length::Type::Rcap; - } else if (name.equals_ignoring_ascii_case("ch"sv)) { - return Length::Type::Ch; - } else if (name.equals_ignoring_ascii_case("rch"sv)) { - return Length::Type::Rch; - } else if (name.equals_ignoring_ascii_case("ic"sv)) { - return Length::Type::Ic; - } else if (name.equals_ignoring_ascii_case("ric"sv)) { - return Length::Type::Ric; - } else if (name.equals_ignoring_ascii_case("lh"sv)) { - return Length::Type::Lh; - } else if (name.equals_ignoring_ascii_case("rlh"sv)) { - return Length::Type::Rlh; - } else if (name.equals_ignoring_ascii_case("vw"sv)) { - return Length::Type::Vw; - } else if (name.equals_ignoring_ascii_case("svw"sv)) { - return Length::Type::Svw; - } else if (name.equals_ignoring_ascii_case("lvw"sv)) { - return Length::Type::Lvw; - } else if (name.equals_ignoring_ascii_case("dvw"sv)) { - return Length::Type::Dvw; - } else if (name.equals_ignoring_ascii_case("vh"sv)) { - return Length::Type::Vh; - } else if (name.equals_ignoring_ascii_case("svh"sv)) { - return Length::Type::Svh; - } else if (name.equals_ignoring_ascii_case("lvh"sv)) { - return Length::Type::Lvh; - } else if (name.equals_ignoring_ascii_case("dvh"sv)) { - return Length::Type::Dvh; - } else if (name.equals_ignoring_ascii_case("vi"sv)) { - return Length::Type::Vi; - } else if (name.equals_ignoring_ascii_case("svi"sv)) { - return Length::Type::Svi; - } else if (name.equals_ignoring_ascii_case("lvi"sv)) { - return Length::Type::Lvi; - } else if (name.equals_ignoring_ascii_case("dvi"sv)) { - return Length::Type::Dvi; - } else if (name.equals_ignoring_ascii_case("vb"sv)) { - return Length::Type::Vb; - } else if (name.equals_ignoring_ascii_case("svb"sv)) { - return Length::Type::Svb; - } else if (name.equals_ignoring_ascii_case("lvb"sv)) { - return Length::Type::Lvb; - } else if (name.equals_ignoring_ascii_case("dvb"sv)) { - return Length::Type::Dvb; - } else if (name.equals_ignoring_ascii_case("vmin"sv)) { - return Length::Type::Vmin; - } else if (name.equals_ignoring_ascii_case("svmin"sv)) { - return Length::Type::Svmin; - } else if (name.equals_ignoring_ascii_case("lvmin"sv)) { - return Length::Type::Lvmin; - } else if (name.equals_ignoring_ascii_case("dvmin"sv)) { - return Length::Type::Dvmin; - } else if (name.equals_ignoring_ascii_case("vmax"sv)) { - return Length::Type::Vmax; - } else if (name.equals_ignoring_ascii_case("svmax"sv)) { - return Length::Type::Svmax; - } else if (name.equals_ignoring_ascii_case("lvmax"sv)) { - return Length::Type::Lvmax; - } else if (name.equals_ignoring_ascii_case("dvmax"sv)) { - return Length::Type::Dvmax; - } else if (name.equals_ignoring_ascii_case("cm"sv)) { - return Length::Type::Cm; - } else if (name.equals_ignoring_ascii_case("mm"sv)) { - return Length::Type::Mm; - } else if (name.equals_ignoring_ascii_case("Q"sv)) { - return Length::Type::Q; - } else if (name.equals_ignoring_ascii_case("in"sv)) { - return Length::Type::In; - } else if (name.equals_ignoring_ascii_case("pt"sv)) { - return Length::Type::Pt; - } else if (name.equals_ignoring_ascii_case("pc"sv)) { - return Length::Type::Pc; - } else if (name.equals_ignoring_ascii_case("px"sv)) { - return Length::Type::Px; - } - - return {}; -} - Optional Length::absolutize(CSSPixelRect const& viewport_rect, FontMetrics const& font_metrics, FontMetrics const& root_font_metrics) const { if (is_px()) diff --git a/Libraries/LibWeb/CSS/Length.h b/Libraries/LibWeb/CSS/Length.h index e0c535b99ae..288c0800755 100644 --- a/Libraries/LibWeb/CSS/Length.h +++ b/Libraries/LibWeb/CSS/Length.h @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -19,57 +20,6 @@ namespace Web::CSS { class WEB_API Length { public: - enum class Type : u8 { - // Font-relative - Em, - Rem, - Ex, - Rex, - Cap, - Rcap, - Ch, - Rch, - Ic, - Ric, - Lh, - Rlh, - - // Viewport-relative - Vw, - Svw, - Lvw, - Dvw, - Vh, - Svh, - Lvh, - Dvh, - Vi, - Svi, - Lvi, - Dvi, - Vb, - Svb, - Lvb, - Dvb, - Vmin, - Svmin, - Lvmin, - Dvmin, - Vmax, - Svmax, - Lvmax, - Dvmax, - - // Absolute - Cm, - Mm, - Q, - In, - Pt, - Pc, - Px, - }; - struct FontMetrics { FontMetrics(CSSPixels font_size, Gfx::FontPixelMetrics const&); @@ -82,80 +32,23 @@ public: bool operator==(FontMetrics const&) const = default; }; - static Optional unit_from_name(StringView); - - Length(double value, Type type); + Length(double value, LengthUnit unit); ~Length(); static Length make_px(double value); static Length make_px(CSSPixels value); Length percentage_of(Percentage const&) const; - bool is_px() const { return m_type == Type::Px; } + bool is_px() const { return m_unit == LengthUnit::Px; } - bool is_absolute() const - { - return m_type == Type::Cm - || m_type == Type::Mm - || m_type == Type::Q - || m_type == Type::In - || m_type == Type::Pt - || m_type == Type::Pc - || m_type == Type::Px; - } + bool is_absolute() const { return CSS::is_absolute(m_unit); } + bool is_font_relative() const { return CSS::is_font_relative(m_unit); } + bool is_viewport_relative() const { return CSS::is_viewport_relative(m_unit); } + bool is_relative() const { return CSS::is_relative(m_unit); } - bool is_font_relative() const - { - return m_type == Type::Em - || m_type == Type::Rem - || m_type == Type::Ex - || m_type == Type::Rex - || m_type == Type::Cap - || m_type == Type::Rcap - || m_type == Type::Ch - || m_type == Type::Rch - || m_type == Type::Ic - || m_type == Type::Ric - || m_type == Type::Lh - || m_type == Type::Rlh; - } - - bool is_viewport_relative() const - { - return m_type == Type::Vw - || m_type == Type::Svw - || m_type == Type::Lvw - || m_type == Type::Dvw - || m_type == Type::Vh - || m_type == Type::Svh - || m_type == Type::Lvh - || m_type == Type::Dvh - || m_type == Type::Vi - || m_type == Type::Svi - || m_type == Type::Lvi - || m_type == Type::Dvi - || m_type == Type::Vb - || m_type == Type::Svb - || m_type == Type::Lvb - || m_type == Type::Dvb - || m_type == Type::Vmin - || m_type == Type::Svmin - || m_type == Type::Lvmin - || m_type == Type::Dvmin - || m_type == Type::Vmax - || m_type == Type::Svmax - || m_type == Type::Lvmax - || m_type == Type::Dvmax; - } - - bool is_relative() const - { - return is_font_relative() || is_viewport_relative(); - } - - Type type() const { return m_type; } double raw_value() const { return m_value; } - StringView unit_name() const; + LengthUnit unit() const { return m_unit; } + StringView unit_name() const { return CSS::to_string(m_unit); } struct ResolutionContext { [[nodiscard]] static ResolutionContext for_element(DOM::AbstractElement const&); @@ -200,20 +93,20 @@ public: constexpr double inch_pixels = 96.0; constexpr double centimeter_pixels = (inch_pixels / 2.54); - switch (m_type) { - case Type::Cm: + switch (m_unit) { + case LengthUnit::Cm: return m_value * centimeter_pixels; // 1cm = 96px/2.54 - case Type::In: + case LengthUnit::In: return m_value * inch_pixels; // 1in = 2.54 cm = 96px - case Type::Px: + case LengthUnit::Px: return m_value; // 1px = 1/96th of 1in - case Type::Pt: + case LengthUnit::Pt: return m_value * ((1.0 / 72.0) * inch_pixels); // 1pt = 1/72th of 1in - case Type::Pc: + case LengthUnit::Pc: return m_value * ((1.0 / 6.0) * inch_pixels); // 1pc = 1/6th of 1in - case Type::Mm: + case LengthUnit::Mm: return m_value * ((1.0 / 10.0) * centimeter_pixels); // 1mm = 1/10th of 1cm - case Type::Q: + case LengthUnit::Q: return m_value * ((1.0 / 40.0) * centimeter_pixels); // 1Q = 1/40th of 1cm default: VERIFY_NOT_REACHED(); @@ -224,7 +117,7 @@ public: bool operator==(Length const& other) const { - return m_type == other.m_type && m_value == other.m_value; + return m_unit == other.m_unit && m_value == other.m_value; } CSSPixels font_relative_length_to_px(FontMetrics const& font_metrics, FontMetrics const& root_font_metrics) const; @@ -240,7 +133,7 @@ public: private: [[nodiscard]] CSSPixels to_px_slow_case(Layout::Node const&) const; - Type m_type; + LengthUnit m_unit; double m_value { 0 }; }; diff --git a/Libraries/LibWeb/CSS/NumericType.cpp b/Libraries/LibWeb/CSS/NumericType.cpp index 2ccd618cdb9..0e2f0ef585b 100644 --- a/Libraries/LibWeb/CSS/NumericType.cpp +++ b/Libraries/LibWeb/CSS/NumericType.cpp @@ -56,37 +56,37 @@ Optional NumericType::create_from_unit(StringView unit) } // unit is a unit - if (Length::unit_from_name(unit).has_value()) { + if (string_to_length_unit(unit).has_value()) { // Return «[ "length" → 1 ]» return NumericType { BaseType::Length, 1 }; } // unit is an unit - if (Angle::unit_from_name(unit).has_value()) { + if (string_to_angle_unit(unit).has_value()) { // Return «[ "angle" → 1 ]» return NumericType { BaseType::Angle, 1 }; } // unit is a