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