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

LibWeb: Refactor Layout::TextNode splitting into a chunk iterator

Creating a ChunkIterator allows you to iterate over the text in a
Layout::TextNode at your leisure by calling next() when you want
another chunk.

This is one of many steps towards improving inline layout.
This commit is contained in:
Andreas Kling 2021-04-27 13:05:50 +02:00
parent 03d8ee1082
commit 5074aaea69
2 changed files with 115 additions and 77 deletions

View file

@ -6,6 +6,7 @@
#pragma once
#include <AK/Utf8View.h>
#include <LibWeb/DOM/Text.h>
#include <LibWeb/Layout/Node.h>
@ -26,6 +27,32 @@ public:
virtual void split_into_lines(InlineFormattingContext&, LayoutMode) override;
struct Chunk {
Utf8View view;
size_t start { 0 };
size_t length { 0 };
bool has_breaking_newline { false };
bool is_all_whitespace { false };
};
class ChunkIterator {
public:
ChunkIterator(StringView const& text, LayoutMode, bool wrap_lines, bool wrap_breaks);
Optional<Chunk> next();
private:
Optional<Chunk> try_commit_chunk(Utf8View::Iterator const&, bool has_breaking_newline, bool must_commit = false);
const LayoutMode m_layout_mode;
const bool m_wrap_lines;
const bool m_wrap_breaks;
bool m_last_was_space { false };
bool m_last_was_newline { false };
Utf8View m_utf8_view;
Utf8View::Iterator m_start_of_chunk;
Utf8View::Iterator m_iterator;
};
private:
virtual bool is_text_node() const final { return true; }
virtual bool wants_mouse_events() const override;
@ -35,9 +62,6 @@ private:
void split_into_lines_by_rules(InlineFormattingContext&, LayoutMode, bool do_collapse, bool do_wrap_lines, bool do_wrap_breaks);
void paint_cursor_if_needed(PaintContext&, const LineBoxFragment&) const;
template<typename Callback>
void for_each_chunk(Callback, LayoutMode, bool do_wrap_lines, bool do_wrap_breaks) const;
String m_text_for_rendering;
};