/src/serenity/Userland/Libraries/LibWeb/Layout/TextNode.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <AK/Utf8View.h> |
10 | | #include <LibLocale/Segmenter.h> |
11 | | #include <LibWeb/DOM/Text.h> |
12 | | #include <LibWeb/Layout/Node.h> |
13 | | |
14 | | namespace Web::Layout { |
15 | | |
16 | | class LineBoxFragment; |
17 | | |
18 | | class TextNode final : public Node { |
19 | | JS_CELL(TextNode, Node); |
20 | | JS_DECLARE_ALLOCATOR(TextNode); |
21 | | |
22 | | public: |
23 | | TextNode(DOM::Document&, DOM::Text&); |
24 | | virtual ~TextNode() override; |
25 | | |
26 | 0 | const DOM::Text& dom_node() const { return static_cast<const DOM::Text&>(*Node::dom_node()); } |
27 | | |
28 | | String const& text_for_rendering() const; |
29 | | |
30 | | struct Chunk { |
31 | | Utf8View view; |
32 | | NonnullRefPtr<Gfx::Font> font; |
33 | | size_t start { 0 }; |
34 | | size_t length { 0 }; |
35 | | bool has_breaking_newline { false }; |
36 | | bool is_all_whitespace { false }; |
37 | | Gfx::GlyphRun::TextType text_type; |
38 | | }; |
39 | | |
40 | | class ChunkIterator { |
41 | | public: |
42 | | ChunkIterator(TextNode const&, bool wrap_lines, bool respect_linebreaks); |
43 | | |
44 | | Optional<Chunk> next(); |
45 | | Optional<Chunk> peek(size_t); |
46 | | |
47 | | private: |
48 | | Optional<Chunk> next_without_peek(); |
49 | | Optional<Chunk> try_commit_chunk(size_t start, size_t end, bool has_breaking_newline, Gfx::Font const&, Gfx::GlyphRun::TextType) const; |
50 | | |
51 | | bool const m_wrap_lines; |
52 | | bool const m_respect_linebreaks; |
53 | | Utf8View m_utf8_view; |
54 | | Gfx::FontCascadeList const& m_font_cascade_list; |
55 | | |
56 | | Locale::Segmenter& m_grapheme_segmenter; |
57 | | size_t m_current_index { 0 }; |
58 | | |
59 | | Vector<Chunk> m_peek_queue; |
60 | | }; |
61 | | |
62 | | void invalidate_text_for_rendering(); |
63 | | void compute_text_for_rendering(); |
64 | | |
65 | | Locale::Segmenter& grapheme_segmenter() const; |
66 | | |
67 | | virtual JS::GCPtr<Painting::Paintable> create_paintable() const override; |
68 | | |
69 | | private: |
70 | 0 | virtual bool is_text_node() const final { return true; } |
71 | | |
72 | | Optional<String> m_text_for_rendering; |
73 | | mutable OwnPtr<Locale::Segmenter> m_grapheme_segmenter; |
74 | | }; |
75 | | |
76 | | template<> |
77 | 0 | inline bool Node::fast_is<TextNode>() const { return is_text_node(); } |
78 | | |
79 | | } |