Просмотр исходного кода

LibWeb: Add `cap` and `rcap` Length units

As noted, the ascent of the font is not the best heuristic for this, but
it is one that's listed as OK to use by the spec:

> In the cases where it is impossible or impractical to determine the
  cap-height, the font’s ascent must be used.
Sam Atkins 2 лет назад
Родитель
Сommit
03ed37eb14
2 измененных файлов с 20 добавлено и 0 удалено
  1. 15 0
      Userland/Libraries/LibWeb/CSS/Length.cpp
  2. 5 0
      Userland/Libraries/LibWeb/CSS/Length.h

+ 15 - 0
Userland/Libraries/LibWeb/CSS/Length.cpp

@@ -21,6 +21,9 @@ namespace Web::CSS {
 Length::FontMetrics::FontMetrics(CSSPixels font_size, Gfx::FontPixelMetrics const& pixel_metrics, CSSPixels line_height)
     : font_size(font_size)
     , x_height(pixel_metrics.x_height)
+    // FIXME: This is only approximately the cap height. The spec suggests measuring the "O" glyph:
+    //        https://www.w3.org/TR/css-values-4/#cap
+    , cap_height(pixel_metrics.ascent)
     , zero_advance(pixel_metrics.advance_of_ascii_zero + pixel_metrics.glyph_spacing)
     , line_height(line_height)
 {
@@ -78,6 +81,10 @@ CSSPixels Length::relative_length_to_px(CSSPixelRect const& viewport_rect, FontM
         return m_value * font_metrics.x_height;
     case Type::Rex:
         return m_value * root_font_metrics.x_height;
+    case Type::Cap:
+        return m_value * font_metrics.cap_height;
+    case Type::Rcap:
+        return m_value * root_font_metrics.cap_height;
     case Type::Ch:
         return m_value * font_metrics.zero_advance;
     case Type::Rch:
@@ -143,6 +150,10 @@ char const* Length::unit_name() const
         return "ex";
     case Type::Rex:
         return "rex";
+    case Type::Cap:
+        return "cap";
+    case Type::Rcap:
+        return "rcap";
     case Type::Ch:
         return "ch";
     case Type::Rch:
@@ -189,6 +200,10 @@ Optional<Length::Type> Length::unit_from_name(StringView name)
         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)) {

+ 5 - 0
Userland/Libraries/LibWeb/CSS/Length.h

@@ -22,6 +22,8 @@ public:
         Rem,
         Ex,
         Rex,
+        Cap,
+        Rcap,
         Ch,
         Rch,
         Lh,
@@ -51,6 +53,7 @@ public:
 
         CSSPixels font_size;
         CSSPixels x_height;
+        CSSPixels cap_height;
         CSSPixels zero_advance;
         CSSPixels line_height;
     };
@@ -87,6 +90,8 @@ public:
             || 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::Lh