From 6b30847120df541ed5851700f198d83cc27ed50c Mon Sep 17 00:00:00 2001 From: circl Date: Sat, 28 Oct 2023 15:39:57 +0200 Subject: [PATCH] LibWeb: Don't add an extra glyph spacing to width of TextNode When calculating the width of text using a bitmap font, a glyph spacing is added at the end of each fragment, including the last one. This meant that everything was 1 pixel too long. This bug did not affect vector fonts. --- .../expected/textnode-width-bitmap-font.txt | 17 +++++++++++++++++ .../input/textnode-width-bitmap-font.html | 1 + .../LibWeb/Layout/InlineLevelIterator.cpp | 8 +++++++- 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 Tests/LibWeb/Layout/expected/textnode-width-bitmap-font.txt create mode 100644 Tests/LibWeb/Layout/input/textnode-width-bitmap-font.html diff --git a/Tests/LibWeb/Layout/expected/textnode-width-bitmap-font.txt b/Tests/LibWeb/Layout/expected/textnode-width-bitmap-font.txt new file mode 100644 index 00000000000..26596af345d --- /dev/null +++ b/Tests/LibWeb/Layout/expected/textnode-width-bitmap-font.txt @@ -0,0 +1,17 @@ +Viewport <#document> at (0,0) content-size 800x600 children: not-inline + BlockContainer at (0,0) content-size 800x38 [BFC] children: not-inline + BlockContainer at (8,13) content-size 784x17 children: not-inline + BlockContainer

at (8,13) content-size 784x17 children: inline + line 0 width: 102, height: 17, bottom: 17, baseline: 11 + frag 0 from TextNode start: 0, length: 18, rect: [8,13 102x17] + "This is some text." + TextNode <#text> + BlockContainer <(anonymous)> at (8,43) content-size 784x0 children: inline + TextNode <#text> + +ViewportPaintable (Viewport<#document>) [0,0 800x600] + PaintableWithLines (BlockContainer) [0,0 800x38] overflow: [0,0 800x43] + PaintableWithLines (BlockContainer) [8,13 784x17] overflow: [8,13 784x30] + PaintableWithLines (BlockContainer

) [8,13 784x17] + TextPaintable (TextNode<#text>) + PaintableWithLines (BlockContainer(anonymous)) [8,43 784x0] diff --git a/Tests/LibWeb/Layout/input/textnode-width-bitmap-font.html b/Tests/LibWeb/Layout/input/textnode-width-bitmap-font.html new file mode 100644 index 00000000000..764edb496ba --- /dev/null +++ b/Tests/LibWeb/Layout/input/textnode-width-bitmap-font.html @@ -0,0 +1 @@ +

This is some text.

diff --git a/Userland/Libraries/LibWeb/Layout/InlineLevelIterator.cpp b/Userland/Libraries/LibWeb/Layout/InlineLevelIterator.cpp index bb9353d59c8..be7f5a5e684 100644 --- a/Userland/Libraries/LibWeb/Layout/InlineLevelIterator.cpp +++ b/Userland/Libraries/LibWeb/Layout/InlineLevelIterator.cpp @@ -180,7 +180,6 @@ Optional InlineLevelIterator::next_without_lookahead( m_text_node_context->is_last_chunk = true; auto& chunk = chunk_opt.value(); - CSSPixels chunk_width = CSSPixels::nearest_value_for(text_node.font().width(chunk.view) + text_node.font().glyph_spacing()); if (m_text_node_context->do_respect_linebreaks && chunk.has_breaking_newline) { return Item { @@ -188,6 +187,13 @@ Optional InlineLevelIterator::next_without_lookahead( }; } + CSSPixels chunk_width; + + if (m_text_node_context->is_last_chunk) + chunk_width = CSSPixels::nearest_value_for(text_node.font().width(chunk.view)); + else + chunk_width = CSSPixels::nearest_value_for(text_node.font().width(chunk.view) + text_node.font().glyph_spacing()); + // NOTE: We never consider `content: ""` to be collapsible whitespace. bool is_generated_empty_string = text_node.is_generated() && chunk.length == 0;