mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-22 09:18:55 +00:00
LibWeb/CSS: Use generated code to convert between dimension units
This commit is contained in:
parent
82f5be871a
commit
5534ed6715
Notes:
github-actions[bot]
2025-09-11 16:07:38 +00:00
Author: https://github.com/AtkinsSJ
Commit: 5534ed6715
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/6071
6 changed files with 8 additions and 69 deletions
|
@ -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<CalculatedStyleValue const> const& calculated, Layout::Node const& layout_node, Angle const& reference_value)
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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<CalculatedStyleValue const> const& calculated, Layout::Node const& layout_node, Frequency const& reference_value)
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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<CalculatedStyleValue const> const& calculated, Layout::Node const& layout_node, Time const& reference_value)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue