LibHTML: Divide the "line spacing" evenly between lines of text

Before this patch, all of the excess spacing caused by line-height was
"padding" the line boxes below the text.

To fix this, we make line box fragments use the font height as their
height, and then let the inline layout algorithm adjust the Y positions
to distribute the vertical space.
This commit is contained in:
Andreas Kling 2019-11-25 18:12:12 +01:00
parent 10d67879c3
commit 847b232680
Notes: sideshowbarker 2024-07-19 11:05:01 +09:00
2 changed files with 5 additions and 5 deletions

View File

@ -67,6 +67,7 @@ void LayoutBlock::layout_inline_children()
}
float min_line_height = style().line_height();
float line_spacing = min_line_height - style().font().glyph_height();
float content_height = 0;
// FIXME: This should be done by the CSS parser!
@ -121,7 +122,7 @@ void LayoutBlock::layout_inline_children()
// Vertically align everyone's bottom to the line.
// FIXME: Support other kinds of vertical alignment.
fragment.rect().set_x(roundf(x_offset + fragment.rect().x()));
fragment.rect().set_y(y() + content_height + (max_height - fragment.rect().height()));
fragment.rect().set_y(y() + content_height + (max_height - fragment.rect().height()) - (line_spacing / 2));
if (text_align == CSS::ValueID::Justify) {
if (fragment.is_justifiable_whitespace()) {

View File

@ -54,7 +54,7 @@ void LayoutText::render_fragment(RenderingContext& context, const LineBoxFragmen
bool is_underline = text_decoration == "underline";
if (is_underline)
painter.draw_line(enclosing_int_rect(fragment.rect()).bottom_left().translated(0, -1), enclosing_int_rect(fragment.rect()).bottom_right().translated(0, -1), color);
painter.draw_line(enclosing_int_rect(fragment.rect()).bottom_left().translated(0, 1), enclosing_int_rect(fragment.rect()).bottom_right().translated(0, 1), color);
painter.draw_text(enclosing_int_rect(fragment.rect()), m_text_for_rendering.substring_view(fragment.start(), fragment.length()), TextAlignment::TopLeft, color);
}
@ -131,7 +131,6 @@ void LayoutText::split_into_lines(LayoutBlock& container)
{
auto& font = style().font();
float space_width = font.glyph_width(' ') + font.glyph_spacing();
float line_height = style().line_height();
auto& line_boxes = container.line_boxes();
if (line_boxes.is_empty())
@ -142,7 +141,7 @@ void LayoutText::split_into_lines(LayoutBlock& container)
if (is_preformatted) {
m_text_for_rendering = node().data();
for_each_source_line([&](const Utf8View& view, int start, int length) {
line_boxes.last().add_fragment(*this, start, length, font.width(view), line_height);
line_boxes.last().add_fragment(*this, start, length, font.width(view), font.glyph_height());
line_boxes.append(LineBox());
});
return;
@ -196,7 +195,7 @@ void LayoutText::split_into_lines(LayoutBlock& container)
if (is_whitespace && line_boxes.last().fragments().is_empty())
continue;
line_boxes.last().add_fragment(*this, word.start, is_whitespace ? 1 : word.length, word_width, line_height);
line_boxes.last().add_fragment(*this, word.start, is_whitespace ? 1 : word.length, word_width, font.glyph_height());
available_width -= word_width;
if (available_width < 0) {