Browse Source

LibHTML: Remove trailing whitespace in line boxes

After the splitting-into-lines pass, remove any trailing whitespace
from all of a block's line boxes.

This improves the appearance of text-align: justify/right :^)
Andreas Kling 5 năm trước cách đây
mục cha
commit
3bd29ad98c

+ 14 - 5
Libraries/LibHTML/Layout/LayoutBlock.cpp

@@ -62,6 +62,10 @@ void LayoutBlock::layout_inline_children()
         child.split_into_lines(*this);
     });
 
+    for (auto& line_box : m_line_boxes) {
+        line_box.trim_trailing_whitespace();
+    }
+
     int min_line_height = style().line_height();
     int content_height = 0;
 
@@ -83,8 +87,8 @@ void LayoutBlock::layout_inline_children()
             max_height = max(max_height, enclosing_int_rect(fragment.rect()).height());
         }
 
-        int x_offset = x();
-        int excess_horizontal_space = width() - line_box.width();
+        float x_offset = x();
+        float excess_horizontal_space = (float)width() - line_box.width();
 
         switch (text_align) {
         case CSS::ValueID::Center:
@@ -99,7 +103,7 @@ void LayoutBlock::layout_inline_children()
             break;
         }
 
-        int excess_horizontal_space_including_whitespace = excess_horizontal_space;
+        float excess_horizontal_space_including_whitespace = excess_horizontal_space;
         int whitespace_count = 0;
         if (text_align == CSS::ValueID::Justify) {
             for (auto& fragment : line_box.fragments()) {
@@ -110,13 +114,13 @@ void LayoutBlock::layout_inline_children()
             }
         }
 
-        float justified_space_width = whitespace_count ? ((float)excess_horizontal_space_including_whitespace / (float)whitespace_count) : 0;
+        float justified_space_width = whitespace_count ? (excess_horizontal_space_including_whitespace / (float)whitespace_count) : 0;
 
         for (int i = 0; i < line_box.fragments().size(); ++i) {
             auto& fragment = line_box.fragments()[i];
             // Vertically align everyone's bottom to the line.
             // FIXME: Support other kinds of vertical alignment.
-            fragment.rect().set_x(x_offset + fragment.rect().x());
+            fragment.rect().set_x(roundf(x_offset + fragment.rect().x()));
             fragment.rect().set_y(y() + content_height + (max_height - fragment.rect().height()));
 
             if (text_align == CSS::ValueID::Justify) {
@@ -134,6 +138,11 @@ void LayoutBlock::layout_inline_children()
 
             if (is<LayoutReplaced>(fragment.layout_node()))
                 const_cast<LayoutReplaced&>(to<LayoutReplaced>(fragment.layout_node())).set_rect(enclosing_int_rect(fragment.rect()));
+
+            float final_line_box_width = 0;
+            for (auto& fragment : line_box.fragments())
+                final_line_box_width += fragment.rect().width();
+            line_box.m_width = final_line_box_width;
         }
 
         content_height += max_height;

+ 25 - 0
Libraries/LibHTML/Layout/LineBox.cpp

@@ -1,5 +1,7 @@
 #include <LibHTML/Layout/LayoutNode.h>
+#include <LibHTML/Layout/LayoutText.h>
 #include <LibHTML/Layout/LineBox.h>
+#include <ctype.h>
 
 void LineBox::add_fragment(const LayoutNode& layout_node, int start, int length, int width, int height)
 {
@@ -14,3 +16,26 @@ void LineBox::add_fragment(const LayoutNode& layout_node, int start, int length,
     }
     m_width += width;
 }
+
+void LineBox::trim_trailing_whitespace()
+{
+    while (!m_fragments.is_empty() && m_fragments.last().is_justifiable_whitespace()) {
+        auto fragment = m_fragments.take_last();
+        m_width -= fragment.width();
+    }
+
+    if (m_fragments.is_empty())
+        return;
+
+    auto last_text = m_fragments.last().text();
+    if (last_text.is_null())
+        return;
+    auto& last_fragment = m_fragments.last();
+
+    int space_width = last_fragment.layout_node().style().font().glyph_width(' ');
+    while (last_fragment.length() && isspace(last_text[last_fragment.length() - 1])) {
+        last_fragment.m_length -= 1;
+        last_fragment.m_rect.set_width(last_fragment.m_rect.width() - space_width);
+        m_width -= space_width;
+    }
+}

+ 2 - 0
Libraries/LibHTML/Layout/LineBox.h

@@ -14,7 +14,9 @@ public:
     const Vector<LineBoxFragment>& fragments() const { return m_fragments; }
     Vector<LineBoxFragment>& fragments() { return m_fragments; }
 
+    void trim_trailing_whitespace();
 private:
+    friend class LayoutBlock;
     Vector<LineBoxFragment> m_fragments;
     float m_width { 0 };
 };

+ 7 - 4
Libraries/LibHTML/Layout/LineBoxFragment.cpp

@@ -16,10 +16,13 @@ void LineBoxFragment::render(RenderingContext& context)
 }
 
 bool LineBoxFragment::is_justifiable_whitespace() const
+{
+    return text() == " ";
+}
+
+StringView LineBoxFragment::text() const
 {
     if (!is<LayoutText>(layout_node()))
-        return false;
-    auto& layout_text = to<LayoutText>(layout_node());
-    auto text = layout_text.node().data().substring_view(m_start, m_length);
-    return text == " ";
+        return {};
+    return to<LayoutText>(layout_node()).node().data().substring_view(m_start, m_length);
 }

+ 3 - 0
Libraries/LibHTML/Layout/LineBoxFragment.h

@@ -22,9 +22,12 @@ public:
     const FloatRect& rect() const { return m_rect; }
     FloatRect& rect() { return m_rect; }
 
+    float width() const { return m_rect.width(); }
+
     void render(RenderingContext&);
 
     bool is_justifiable_whitespace() const;
+    StringView text() const;
 
 private:
     const LayoutNode& m_layout_node;