Browse Source

LibWeb: Cache the used CSS text-align property on LayoutNodeWithStyle

Andreas Kling 5 years ago
parent
commit
f4ecb5362f

+ 6 - 6
Libraries/LibWeb/CSS/StyleProperties.cpp

@@ -229,19 +229,19 @@ bool StyleProperties::operator==(const StyleProperties& other) const
     return true;
 }
 
-CSS::ValueID StyleProperties::text_align() const
+CSS::TextAlign StyleProperties::text_align() const
 {
     auto string = string_or_fallback(CSS::PropertyID::TextAlign, "left");
     if (string == "center")
-        return CSS::ValueID::Center;
+        return CSS::TextAlign::Center;
     if (string == "right")
-        return CSS::ValueID::Right;
+        return CSS::TextAlign::Right;
     if (string == "justify")
-        return CSS::ValueID::Justify;
+        return CSS::TextAlign::Justify;
     if (string == "-libweb-center")
-        return CSS::ValueID::VendorSpecificCenter;
+        return CSS::TextAlign::VendorSpecificCenter;
     // Otherwise, just assume "left"..
-    return CSS::ValueID::Left;
+    return CSS::TextAlign::Left;
 }
 
 }

+ 1 - 1
Libraries/LibWeb/CSS/StyleProperties.h

@@ -59,7 +59,7 @@ public:
     Length length_or_fallback(CSS::PropertyID, const Length& fallback, float reference_for_percentages) const;
     String string_or_fallback(CSS::PropertyID, const StringView& fallback) const;
     Color color_or_fallback(CSS::PropertyID, const Document&, Color fallback) const;
-    CSS::ValueID text_align() const;
+    CSS::TextAlign text_align() const;
 
     const Gfx::Font& font() const
     {

+ 9 - 0
Libraries/LibWeb/CSS/StyleValue.h

@@ -114,6 +114,15 @@ enum class Position {
     Fixed,
     Sticky,
 };
+
+enum class TextAlign {
+    Left,
+    Center,
+    Right,
+    Justify,
+    VendorSpecificCenter,
+};
+
 }
 
 class StyleValue : public RefCounted<StyleValue> {

+ 9 - 9
Libraries/LibWeb/Layout/LayoutBlock.cpp

@@ -187,7 +187,7 @@ void LayoutBlock::layout_inline_children(LayoutMode layout_mode)
         line_box.trim_trailing_whitespace();
     }
 
-    auto text_align = style().text_align();
+    auto text_align = this->text_align();
     float min_line_height = style().line_height(*this);
     float line_spacing = min_line_height - style().font().glyph_height();
     float content_height = 0;
@@ -203,22 +203,22 @@ void LayoutBlock::layout_inline_children(LayoutMode layout_mode)
         float excess_horizontal_space = (float)width() - line_box.width();
 
         switch (text_align) {
-        case CSS::ValueID::Center:
-        case CSS::ValueID::VendorSpecificCenter:
+        case CSS::TextAlign::Center:
+        case CSS::TextAlign::VendorSpecificCenter:
             x_offset += excess_horizontal_space / 2;
             break;
-        case CSS::ValueID::Right:
+        case CSS::TextAlign::Right:
             x_offset += excess_horizontal_space;
             break;
-        case CSS::ValueID::Left:
-        case CSS::ValueID::Justify:
+        case CSS::TextAlign::Left:
+        case CSS::TextAlign::Justify:
         default:
             break;
         }
 
         float excess_horizontal_space_including_whitespace = excess_horizontal_space;
         int whitespace_count = 0;
-        if (text_align == CSS::ValueID::Justify) {
+        if (text_align == CSS::TextAlign::Justify) {
             for (auto& fragment : line_box.fragments()) {
                 if (fragment.is_justifiable_whitespace()) {
                     ++whitespace_count;
@@ -236,7 +236,7 @@ void LayoutBlock::layout_inline_children(LayoutMode layout_mode)
             // FIXME: Support other kinds of vertical alignment.
             fragment.set_offset({ roundf(x_offset + fragment.offset().x()), content_height + (max_height - fragment.height()) - (line_spacing / 2) });
 
-            if (text_align == CSS::ValueID::Justify) {
+            if (text_align == CSS::TextAlign::Justify) {
                 if (fragment.is_justifiable_whitespace()) {
                     if (fragment.width() != justified_space_width) {
                         float diff = justified_space_width - fragment.width();
@@ -633,7 +633,7 @@ void LayoutBlock::place_block_level_non_replaced_element_in_normal_flow(LayoutBl
         + box.padding().left.to_px(*this)
         + box.offset().left.to_px(*this);
 
-    if (this->style().text_align() == CSS::ValueID::VendorSpecificCenter) {
+    if (text_align() == CSS::TextAlign::VendorSpecificCenter) {
         x = (containing_block.width() / 2) - block.width() / 2;
     }
 

+ 1 - 0
Libraries/LibWeb/Layout/LayoutNode.cpp

@@ -217,6 +217,7 @@ LayoutNodeWithStyle::LayoutNodeWithStyle(const Node* node, NonnullRefPtr<StylePr
 {
     m_has_style = true;
     m_position = m_style->position();
+    m_text_align = m_style->text_align();
 }
 
 }

+ 2 - 0
Libraries/LibWeb/Layout/LayoutNode.h

@@ -259,6 +259,7 @@ public:
     void set_style(const StyleProperties& style) { m_style = style; }
 
     CSS::Position position() const { return m_position; }
+    CSS::TextAlign text_align() const { return m_text_align; }
 
 protected:
     explicit LayoutNodeWithStyle(const Node*, NonnullRefPtr<StyleProperties>);
@@ -266,6 +267,7 @@ protected:
 private:
     NonnullRefPtr<StyleProperties> m_style;
     CSS::Position m_position;
+    CSS::TextAlign m_text_align;
 };
 
 class LayoutNodeWithStyleAndBoxModelMetrics : public LayoutNodeWithStyle {