ladybird/Libraries/LibHTML/Layout/LineBoxFragment.cpp
Andreas Kling eb77e680ed LibHTML: Implement "text-align: justify"
In order for this to work nicely, I made the line box classes use float
instead of int for its geometry information.

Justification works by distributing all of the whitespace on the line
(including the trailing whitespace before the line break) evenly across
the spaces in-between words.

We should probably use floating point (or maybe fixed point?) for all
the layout metrics stuff. But one thing at a time. :^)
2019-10-20 12:55:55 +02:00

25 lines
765 B
C++

#include <LibGUI/GPainter.h>
#include <LibHTML/Layout/LayoutText.h>
#include <LibHTML/Layout/LineBoxFragment.h>
#include <LibHTML/RenderingContext.h>
void LineBoxFragment::render(RenderingContext& context)
{
for (auto* ancestor = layout_node().parent(); ancestor; ancestor = ancestor->parent()) {
if (!ancestor->is_visible())
return;
}
if (is<LayoutText>(layout_node())) {
to<LayoutText>(layout_node()).render_fragment(context, *this);
}
}
bool LineBoxFragment::is_justifiable_whitespace() 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 == " ";
}