1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 10:57:35 +00:00

LibWeb: Refactor text justification code + only justify below threshold

All the justification-related code is now in
InlineFormattingContext::apply_justification_to_fragments and is
performed after all the line boxes have been added.

Text justification now only happens on the last line if the excess space
including whitespace is below a certain threshold. 10% seemed reasonable
since it prevents the "over-justification" of text. Note that fragments
in line boxes before the last one are always justified.
This commit is contained in:
sin-ack 2022-03-12 17:01:06 +00:00 committed by Andreas Kling
parent f0a1ab6f84
commit 7fe3f2d970
3 changed files with 53 additions and 26 deletions

View file

@ -125,19 +125,6 @@ void LineBuilder::update_last_line()
break;
}
float excess_horizontal_space_including_whitespace = excess_horizontal_space;
size_t whitespace_count = 0;
if (text_align == CSS::TextAlign::Justify) {
for (auto& fragment : line_box.fragments()) {
if (fragment.is_justifiable_whitespace()) {
++whitespace_count;
excess_horizontal_space_including_whitespace += fragment.width();
}
}
}
float justified_space_width = whitespace_count > 0 ? (excess_horizontal_space_including_whitespace / static_cast<float>(whitespace_count)) : 0;
auto fragment_baseline = [&](auto const& fragment) -> float {
if (fragment.layout_node().is_text_node())
return fragment.layout_node().font().baseline();
@ -193,19 +180,6 @@ void LineBuilder::update_last_line()
fragment.set_offset({ new_fragment_x, new_fragment_y });
bottom = max(bottom, new_fragment_y + fragment.height() + fragment.border_box_bottom());
if (text_align == CSS::TextAlign::Justify
&& fragment.is_justifiable_whitespace()
&& fragment.width() != justified_space_width) {
float diff = justified_space_width - fragment.width();
fragment.set_width(justified_space_width);
// Shift subsequent sibling fragments to the right to adjust for change in width.
for (size_t j = i + 1; j < line_box.fragments().size(); ++j) {
auto offset = line_box.fragments()[j].offset();
offset.translate_by(diff, 0);
line_box.fragments()[j].set_offset(offset);
}
}
}
line_box.m_bottom = bottom;