LibWeb/CSS: Use generated FooUnit types instead of Foo::Type

I've also renamed the `m_type` and `type()` members to be `m_unit` and
`unit()` instead, to match what they actually are.
This commit is contained in:
Sam Atkins 2025-09-02 13:48:49 +01:00
commit b3e32445d3
Notes: github-actions[bot] 2025-09-11 16:08:15 +00:00
29 changed files with 232 additions and 669 deletions

View file

@ -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::Type> 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> Length::absolutize(CSSPixelRect const& viewport_rect, FontMetrics const& font_metrics, FontMetrics const& root_font_metrics) const
{
if (is_px())