diff --git a/Libraries/LibWeb/CSS/Angle.cpp b/Libraries/LibWeb/CSS/Angle.cpp index 11c26f25206..90974be3e95 100644 --- a/Libraries/LibWeb/CSS/Angle.cpp +++ b/Libraries/LibWeb/CSS/Angle.cpp @@ -48,22 +48,12 @@ String Angle::to_string(SerializationMode serialization_mode) const double Angle::to_degrees() const { - switch (m_unit) { - case AngleUnit::Deg: - return m_value; - case AngleUnit::Grad: - return m_value * (360.0 / 400.0); - case AngleUnit::Rad: - return AK::to_degrees(m_value); - case AngleUnit::Turn: - return m_value * 360.0; - } - VERIFY_NOT_REACHED(); + return ratio_between_units(m_unit, AngleUnit::Deg) * m_value; } double Angle::to_radians() const { - return AK::to_radians(to_degrees()); + return ratio_between_units(m_unit, AngleUnit::Rad) * m_value; } Angle Angle::resolve_calculated(NonnullRefPtr const& calculated, Layout::Node const& layout_node, Angle const& reference_value) diff --git a/Libraries/LibWeb/CSS/Flex.cpp b/Libraries/LibWeb/CSS/Flex.cpp index a0c26354604..bab0ac5a983 100644 --- a/Libraries/LibWeb/CSS/Flex.cpp +++ b/Libraries/LibWeb/CSS/Flex.cpp @@ -44,11 +44,7 @@ String Flex::to_string(SerializationMode serialization_mode) const double Flex::to_fr() const { - switch (m_unit) { - case FlexUnit::Fr: - return m_value; - } - VERIFY_NOT_REACHED(); + return ratio_between_units(m_unit, FlexUnit::Fr) * m_value; } } diff --git a/Libraries/LibWeb/CSS/Frequency.cpp b/Libraries/LibWeb/CSS/Frequency.cpp index 9a0b7f4f33b..f7eee1bb8da 100644 --- a/Libraries/LibWeb/CSS/Frequency.cpp +++ b/Libraries/LibWeb/CSS/Frequency.cpp @@ -47,13 +47,7 @@ String Frequency::to_string(SerializationMode serialization_mode) const double Frequency::to_hertz() const { - switch (m_unit) { - case FrequencyUnit::Hz: - return m_value; - case FrequencyUnit::KHz: - return m_value * 1000; - } - VERIFY_NOT_REACHED(); + return ratio_between_units(m_unit, FrequencyUnit::Hz) * m_value; } Frequency Frequency::resolve_calculated(NonnullRefPtr const& calculated, Layout::Node const& layout_node, Frequency const& reference_value) diff --git a/Libraries/LibWeb/CSS/Length.h b/Libraries/LibWeb/CSS/Length.h index 288c0800755..a1eca954307 100644 --- a/Libraries/LibWeb/CSS/Length.h +++ b/Libraries/LibWeb/CSS/Length.h @@ -90,27 +90,7 @@ public: ALWAYS_INLINE double absolute_length_to_px_without_rounding() const { - constexpr double inch_pixels = 96.0; - constexpr double centimeter_pixels = (inch_pixels / 2.54); - - switch (m_unit) { - case LengthUnit::Cm: - return m_value * centimeter_pixels; // 1cm = 96px/2.54 - case LengthUnit::In: - return m_value * inch_pixels; // 1in = 2.54 cm = 96px - case LengthUnit::Px: - return m_value; // 1px = 1/96th of 1in - case LengthUnit::Pt: - return m_value * ((1.0 / 72.0) * inch_pixels); // 1pt = 1/72th of 1in - case LengthUnit::Pc: - return m_value * ((1.0 / 6.0) * inch_pixels); // 1pc = 1/6th of 1in - case LengthUnit::Mm: - return m_value * ((1.0 / 10.0) * centimeter_pixels); // 1mm = 1/10th of 1cm - case LengthUnit::Q: - return m_value * ((1.0 / 40.0) * centimeter_pixels); // 1Q = 1/40th of 1cm - default: - VERIFY_NOT_REACHED(); - } + return ratio_between_units(m_unit, LengthUnit::Px) * m_value; } String to_string(SerializationMode = SerializationMode::Normal) const; diff --git a/Libraries/LibWeb/CSS/Resolution.cpp b/Libraries/LibWeb/CSS/Resolution.cpp index 3900e3d378c..191c3556655 100644 --- a/Libraries/LibWeb/CSS/Resolution.cpp +++ b/Libraries/LibWeb/CSS/Resolution.cpp @@ -42,16 +42,7 @@ String Resolution::to_string(SerializationMode serialization_mode) const double Resolution::to_dots_per_pixel() const { - switch (m_unit) { - case ResolutionUnit::Dpi: - return m_value / 96; // 1in = 2.54cm = 96px - case ResolutionUnit::Dpcm: - return m_value / (96.0 / 2.54); // 1cm = 96px/2.54 - case ResolutionUnit::Dppx: - case ResolutionUnit::X: - return m_value; - } - VERIFY_NOT_REACHED(); + return ratio_between_units(m_unit, ResolutionUnit::Dppx) * m_value; } } diff --git a/Libraries/LibWeb/CSS/Time.cpp b/Libraries/LibWeb/CSS/Time.cpp index 2fb4caec8ff..d82ab9c5b61 100644 --- a/Libraries/LibWeb/CSS/Time.cpp +++ b/Libraries/LibWeb/CSS/Time.cpp @@ -47,24 +47,12 @@ String Time::to_string(SerializationMode serialization_mode) const double Time::to_seconds() const { - switch (m_unit) { - case TimeUnit::S: - return m_value; - case TimeUnit::Ms: - return m_value / 1000.0; - } - VERIFY_NOT_REACHED(); + return ratio_between_units(m_unit, TimeUnit::S) * m_value; } double Time::to_milliseconds() const { - switch (m_unit) { - case TimeUnit::S: - return m_value * 1000.0; - case TimeUnit::Ms: - return m_value; - } - VERIFY_NOT_REACHED(); + return ratio_between_units(m_unit, TimeUnit::Ms) * m_value; } Time Time::resolve_calculated(NonnullRefPtr const& calculated, Layout::Node const& layout_node, Time const& reference_value)