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 :^)
This commit is contained in:
Andreas Kling 2019-10-20 17:18:28 +02:00
commit 3bd29ad98c
Notes: sideshowbarker 2024-07-19 11:36:33 +09:00
5 changed files with 52 additions and 10 deletions

View file

@ -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;
}
}