LibWeb: Avoid unnecessary Vector copying when generating line boxes

Carry the same Vector<Gfx::DrawGlyphOrEmoji> all the way from the inline
level iterator to the final line box fragment.
This commit is contained in:
Andreas Kling 2024-03-24 16:51:04 +01:00
parent f48024c2d1
commit 9af966f87d
Notes: sideshowbarker 2024-07-17 12:02:22 +09:00
5 changed files with 8 additions and 9 deletions

View file

@ -348,7 +348,7 @@ void InlineFormattingContext::generate_line_boxes(LayoutMode layout_mode)
item.margin_end, item.margin_end,
item.width, item.width,
text_node.computed_values().line_height(), text_node.computed_values().line_height(),
item.glyph_run); move(item.glyph_run));
break; break;
} }
} }

View file

@ -15,7 +15,7 @@
namespace Web::Layout { namespace Web::Layout {
void LineBox::add_fragment(Node const& layout_node, int start, int length, CSSPixels leading_size, CSSPixels trailing_size, CSSPixels leading_margin, CSSPixels trailing_margin, CSSPixels content_width, CSSPixels content_height, CSSPixels border_box_top, CSSPixels border_box_bottom, Span<Gfx::DrawGlyphOrEmoji const> glyph_run) void LineBox::add_fragment(Node const& layout_node, int start, int length, CSSPixels leading_size, CSSPixels trailing_size, CSSPixels leading_margin, CSSPixels trailing_margin, CSSPixels content_width, CSSPixels content_height, CSSPixels border_box_top, CSSPixels border_box_bottom, Vector<Gfx::DrawGlyphOrEmoji> glyph_run)
{ {
bool text_align_is_justify = layout_node.computed_values().text_align() == CSS::TextAlign::Justify; bool text_align_is_justify = layout_node.computed_values().text_align() == CSS::TextAlign::Justify;
if (!text_align_is_justify && !m_fragments.is_empty() && &m_fragments.last().layout_node() == &layout_node) { if (!text_align_is_justify && !m_fragments.is_empty() && &m_fragments.last().layout_node() == &layout_node) {
@ -24,15 +24,14 @@ void LineBox::add_fragment(Node const& layout_node, int start, int length, CSSPi
// Expand the last fragment instead of adding a new one with the same Layout::Node. // Expand the last fragment instead of adding a new one with the same Layout::Node.
m_fragments.last().m_length = (start - m_fragments.last().m_start) + length; m_fragments.last().m_length = (start - m_fragments.last().m_start) + length;
m_fragments.last().set_width(m_fragments.last().width() + content_width); m_fragments.last().set_width(m_fragments.last().width() + content_width);
for (auto glyph : glyph_run) { for (auto& glyph : glyph_run) {
glyph.visit([&](auto& glyph) { glyph.position.translate_by(fragment_width.to_float(), 0); }); glyph.visit([&](auto& glyph) { glyph.position.translate_by(fragment_width.to_float(), 0); });
m_fragments.last().m_glyph_run->append(glyph); m_fragments.last().m_glyph_run->append(glyph);
} }
} else { } else {
Vector<Gfx::DrawGlyphOrEmoji> glyph_run_copy { glyph_run };
CSSPixels x_offset = leading_margin + leading_size + m_width; CSSPixels x_offset = leading_margin + leading_size + m_width;
CSSPixels y_offset = 0; CSSPixels y_offset = 0;
m_fragments.append(LineBoxFragment { layout_node, start, length, CSSPixelPoint(x_offset, y_offset), CSSPixelSize(content_width, content_height), border_box_top, move(glyph_run_copy) }); m_fragments.append(LineBoxFragment { layout_node, start, length, CSSPixelPoint(x_offset, y_offset), CSSPixelSize(content_width, content_height), border_box_top, move(glyph_run) });
} }
m_width += leading_margin + leading_size + content_width + trailing_size + trailing_margin; m_width += leading_margin + leading_size + content_width + trailing_size + trailing_margin;
m_height = max(m_height, content_height + border_box_top + border_box_bottom); m_height = max(m_height, content_height + border_box_top + border_box_bottom);

View file

@ -20,7 +20,7 @@ public:
CSSPixels bottom() const { return m_bottom; } CSSPixels bottom() const { return m_bottom; }
CSSPixels baseline() const { return m_baseline; } CSSPixels baseline() const { return m_baseline; }
void add_fragment(Node const& layout_node, int start, int length, CSSPixels leading_size, CSSPixels trailing_size, CSSPixels leading_margin, CSSPixels trailing_margin, CSSPixels content_width, CSSPixels content_height, CSSPixels border_box_top, CSSPixels border_box_bottom, Span<Gfx::DrawGlyphOrEmoji const> = {}); void add_fragment(Node const& layout_node, int start, int length, CSSPixels leading_size, CSSPixels trailing_size, CSSPixels leading_margin, CSSPixels trailing_margin, CSSPixels content_width, CSSPixels content_height, CSSPixels border_box_top, CSSPixels border_box_bottom, Vector<Gfx::DrawGlyphOrEmoji> = {});
Vector<LineBoxFragment> const& fragments() const { return m_fragments; } Vector<LineBoxFragment> const& fragments() const { return m_fragments; }
Vector<LineBoxFragment>& fragments() { return m_fragments; } Vector<LineBoxFragment>& fragments() { return m_fragments; }

View file

@ -97,9 +97,9 @@ void LineBuilder::append_box(Box const& box, CSSPixels leading_size, CSSPixels t
}; };
} }
void LineBuilder::append_text_chunk(TextNode const& text_node, size_t offset_in_node, size_t length_in_node, CSSPixels leading_size, CSSPixels trailing_size, CSSPixels leading_margin, CSSPixels trailing_margin, CSSPixels content_width, CSSPixels content_height, Span<Gfx::DrawGlyphOrEmoji> glyph_run) void LineBuilder::append_text_chunk(TextNode const& text_node, size_t offset_in_node, size_t length_in_node, CSSPixels leading_size, CSSPixels trailing_size, CSSPixels leading_margin, CSSPixels trailing_margin, CSSPixels content_width, CSSPixels content_height, Vector<Gfx::DrawGlyphOrEmoji> glyph_run)
{ {
ensure_last_line_box().add_fragment(text_node, offset_in_node, length_in_node, leading_size, trailing_size, leading_margin, trailing_margin, content_width, content_height, 0, 0, glyph_run); ensure_last_line_box().add_fragment(text_node, offset_in_node, length_in_node, leading_size, trailing_size, leading_margin, trailing_margin, content_width, content_height, 0, 0, move(glyph_run));
m_max_height_on_current_line = max(m_max_height_on_current_line, content_height); m_max_height_on_current_line = max(m_max_height_on_current_line, content_height);
} }

View file

@ -25,7 +25,7 @@ public:
void break_line(ForcedBreak, Optional<CSSPixels> next_item_width = {}); void break_line(ForcedBreak, Optional<CSSPixels> next_item_width = {});
void append_box(Box const&, CSSPixels leading_size, CSSPixels trailing_size, CSSPixels leading_margin, CSSPixels trailing_margin); void append_box(Box const&, CSSPixels leading_size, CSSPixels trailing_size, CSSPixels leading_margin, CSSPixels trailing_margin);
void append_text_chunk(TextNode const&, size_t offset_in_node, size_t length_in_node, CSSPixels leading_size, CSSPixels trailing_size, CSSPixels leading_margin, CSSPixels trailing_margin, CSSPixels content_width, CSSPixels content_height, Span<Gfx::DrawGlyphOrEmoji>); void append_text_chunk(TextNode const&, size_t offset_in_node, size_t length_in_node, CSSPixels leading_size, CSSPixels trailing_size, CSSPixels leading_margin, CSSPixels trailing_margin, CSSPixels content_width, CSSPixels content_height, Vector<Gfx::DrawGlyphOrEmoji>);
// Returns whether a line break occurred. // Returns whether a line break occurred.
bool break_if_needed(CSSPixels next_item_width) bool break_if_needed(CSSPixels next_item_width)