2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2019-06-15 22:49:44 +02:00
|
|
|
#pragma once
|
|
|
|
|
2025-07-25 09:34:41 -04:00
|
|
|
#include <AK/Utf16String.h>
|
|
|
|
#include <AK/Utf16View.h>
|
2025-02-02 15:55:26 +01:00
|
|
|
#include <LibGfx/TextLayout.h>
|
2024-09-20 17:14:14 -04:00
|
|
|
#include <LibUnicode/Segmenter.h>
|
2020-03-07 10:32:51 +01:00
|
|
|
#include <LibWeb/DOM/Text.h>
|
2020-11-22 15:53:01 +01:00
|
|
|
#include <LibWeb/Layout/Node.h>
|
2019-06-15 22:49:44 +02:00
|
|
|
|
2020-11-22 15:53:01 +01:00
|
|
|
namespace Web::Layout {
|
2020-03-07 10:27:02 +01:00
|
|
|
|
2019-10-03 15:20:13 +02:00
|
|
|
class LineBoxFragment;
|
2019-09-25 12:36:44 +03:00
|
|
|
|
2023-01-11 14:01:33 +01:00
|
|
|
class TextNode final : public Node {
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_CELL(TextNode, Node);
|
|
|
|
GC_DECLARE_ALLOCATOR(TextNode);
|
2022-10-17 14:41:50 +02:00
|
|
|
|
2019-06-15 22:49:44 +02:00
|
|
|
public:
|
2020-11-22 15:53:01 +01:00
|
|
|
TextNode(DOM::Document&, DOM::Text&);
|
|
|
|
virtual ~TextNode() override;
|
2019-06-15 22:49:44 +02:00
|
|
|
|
2025-08-29 13:02:52 +01:00
|
|
|
DOM::Text const& dom_node() const { return static_cast<DOM::Text const&>(*Node::dom_node()); }
|
2019-06-15 22:49:44 +02:00
|
|
|
|
2025-07-25 09:34:41 -04:00
|
|
|
Utf16String const& text_for_rendering() const;
|
2019-06-15 23:17:08 +02:00
|
|
|
|
2021-04-27 13:05:50 +02:00
|
|
|
struct Chunk {
|
2025-07-25 09:34:41 -04:00
|
|
|
Utf16View view;
|
2025-04-15 15:48:14 -06:00
|
|
|
NonnullRefPtr<Gfx::Font const> font;
|
2021-04-27 13:05:50 +02:00
|
|
|
size_t start { 0 };
|
|
|
|
size_t length { 0 };
|
|
|
|
bool has_breaking_newline { false };
|
2024-10-15 18:21:49 +01:00
|
|
|
bool has_breaking_tab { false };
|
2021-04-27 13:05:50 +02:00
|
|
|
bool is_all_whitespace { false };
|
2024-08-18 17:58:05 +01:00
|
|
|
Gfx::GlyphRun::TextType text_type;
|
2021-04-27 13:05:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class ChunkIterator {
|
|
|
|
public:
|
2025-09-12 10:06:27 +02:00
|
|
|
ChunkIterator(TextNode const&, bool should_wrap_lines, bool should_respect_linebreaks);
|
|
|
|
ChunkIterator(TextNode const&, Utf16View const&, Unicode::Segmenter&, bool should_wrap_lines, bool should_respect_linebreaks);
|
|
|
|
|
|
|
|
bool should_wrap_lines() const { return m_should_wrap_lines; }
|
|
|
|
bool should_respect_linebreaks() const { return m_should_respect_linebreaks; }
|
|
|
|
bool should_collapse_whitespace() const { return m_should_collapse_whitespace; }
|
2024-09-20 17:14:14 -04:00
|
|
|
|
2021-04-27 13:05:50 +02:00
|
|
|
Optional<Chunk> next();
|
2024-08-18 17:58:05 +01:00
|
|
|
Optional<Chunk> peek(size_t);
|
2021-04-27 13:05:50 +02:00
|
|
|
|
2025-08-08 21:17:33 +10:00
|
|
|
Chunk create_empty_chunk();
|
|
|
|
|
2021-04-27 13:05:50 +02:00
|
|
|
private:
|
2024-08-18 17:58:05 +01:00
|
|
|
Optional<Chunk> next_without_peek();
|
2024-10-15 18:21:49 +01:00
|
|
|
Optional<Chunk> try_commit_chunk(size_t start, size_t end, bool has_breaking_newline, bool has_breaking_tab, Gfx::Font const&, Gfx::GlyphRun::TextType) const;
|
2021-04-27 13:05:50 +02:00
|
|
|
|
2025-09-12 10:06:27 +02:00
|
|
|
bool const m_should_wrap_lines;
|
|
|
|
bool const m_should_respect_linebreaks;
|
|
|
|
bool m_should_collapse_whitespace;
|
2025-07-25 09:34:41 -04:00
|
|
|
Utf16View m_view;
|
2024-06-29 17:14:23 +02:00
|
|
|
Gfx::FontCascadeList const& m_font_cascade_list;
|
2024-08-18 17:58:05 +01:00
|
|
|
|
2024-09-22 10:03:23 -04:00
|
|
|
Unicode::Segmenter& m_grapheme_segmenter;
|
2024-09-20 17:14:14 -04:00
|
|
|
size_t m_current_index { 0 };
|
|
|
|
|
2024-08-18 17:58:05 +01:00
|
|
|
Vector<Chunk> m_peek_queue;
|
2021-04-27 13:05:50 +02:00
|
|
|
};
|
|
|
|
|
2023-08-16 11:03:00 +02:00
|
|
|
void invalidate_text_for_rendering();
|
2021-04-29 08:51:31 +02:00
|
|
|
|
2024-09-22 10:03:23 -04:00
|
|
|
Unicode::Segmenter& grapheme_segmenter() const;
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
virtual GC::Ptr<Painting::Paintable> create_paintable() const override;
|
2022-03-10 16:46:44 +01:00
|
|
|
|
2019-06-15 22:49:44 +02:00
|
|
|
private:
|
2021-01-17 09:34:01 +01:00
|
|
|
virtual bool is_text_node() const final { return true; }
|
2019-12-18 12:44:16 +01:00
|
|
|
|
2025-07-25 09:34:41 -04:00
|
|
|
void compute_text_for_rendering();
|
|
|
|
|
|
|
|
Optional<Utf16String> m_text_for_rendering;
|
2024-09-22 10:03:23 -04:00
|
|
|
mutable OwnPtr<Unicode::Segmenter> m_grapheme_segmenter;
|
2019-06-15 22:49:44 +02:00
|
|
|
};
|
2019-10-15 14:19:52 +02:00
|
|
|
|
2021-01-17 09:34:01 +01:00
|
|
|
template<>
|
|
|
|
inline bool Node::fast_is<TextNode>() const { return is_text_node(); }
|
|
|
|
|
2019-10-15 14:19:52 +02:00
|
|
|
}
|