diff --git a/Libraries/LibHTML/Layout/LayoutBlock.cpp b/Libraries/LibHTML/Layout/LayoutBlock.cpp
index fc53dc6493..3ba37a9088 100644
--- a/Libraries/LibHTML/Layout/LayoutBlock.cpp
+++ b/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(fragment.layout_node()))
const_cast(to(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;
diff --git a/Libraries/LibHTML/Layout/LineBox.cpp b/Libraries/LibHTML/Layout/LineBox.cpp
index 426368e1df..2a44d62193 100644
--- a/Libraries/LibHTML/Layout/LineBox.cpp
+++ b/Libraries/LibHTML/Layout/LineBox.cpp
@@ -1,5 +1,7 @@
#include
+#include
#include
+#include
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;
+ }
+}
diff --git a/Libraries/LibHTML/Layout/LineBox.h b/Libraries/LibHTML/Layout/LineBox.h
index 48d3d1c434..682f5e7a78 100644
--- a/Libraries/LibHTML/Layout/LineBox.h
+++ b/Libraries/LibHTML/Layout/LineBox.h
@@ -14,7 +14,9 @@ public:
const Vector& fragments() const { return m_fragments; }
Vector& fragments() { return m_fragments; }
+ void trim_trailing_whitespace();
private:
+ friend class LayoutBlock;
Vector m_fragments;
float m_width { 0 };
};
diff --git a/Libraries/LibHTML/Layout/LineBoxFragment.cpp b/Libraries/LibHTML/Layout/LineBoxFragment.cpp
index 027c23ebe5..c987483b2b 100644
--- a/Libraries/LibHTML/Layout/LineBoxFragment.cpp
+++ b/Libraries/LibHTML/Layout/LineBoxFragment.cpp
@@ -17,9 +17,12 @@ void LineBoxFragment::render(RenderingContext& context)
bool LineBoxFragment::is_justifiable_whitespace() const
{
- if (!is(layout_node()))
- return false;
- auto& layout_text = to(layout_node());
- auto text = layout_text.node().data().substring_view(m_start, m_length);
- return text == " ";
+ return text() == " ";
+}
+
+StringView LineBoxFragment::text() const
+{
+ if (!is(layout_node()))
+ return {};
+ return to(layout_node()).node().data().substring_view(m_start, m_length);
}
diff --git a/Libraries/LibHTML/Layout/LineBoxFragment.h b/Libraries/LibHTML/Layout/LineBoxFragment.h
index ed9a13a2cb..0b12cce81f 100644
--- a/Libraries/LibHTML/Layout/LineBoxFragment.h
+++ b/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;