TextNode.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Utf8View.h>
  8. #include <LibWeb/DOM/Text.h>
  9. #include <LibWeb/Layout/Node.h>
  10. namespace Web::Layout {
  11. class LineBoxFragment;
  12. class TextNode : public Node {
  13. public:
  14. TextNode(DOM::Document&, DOM::Text&);
  15. virtual ~TextNode() override;
  16. const DOM::Text& dom_node() const { return static_cast<const DOM::Text&>(*Node::dom_node()); }
  17. const String& text_for_rendering() const { return m_text_for_rendering; }
  18. struct Chunk {
  19. Utf8View view;
  20. size_t start { 0 };
  21. size_t length { 0 };
  22. bool has_breaking_newline { false };
  23. bool is_all_whitespace { false };
  24. };
  25. class ChunkIterator {
  26. public:
  27. ChunkIterator(StringView text, LayoutMode, bool wrap_lines, bool respect_linebreaks);
  28. Optional<Chunk> next();
  29. private:
  30. Optional<Chunk> try_commit_chunk(Utf8View::Iterator const& start, Utf8View::Iterator const& end, bool has_breaking_newline, bool must_commit = false) const;
  31. const LayoutMode m_layout_mode;
  32. const bool m_wrap_lines;
  33. const bool m_respect_linebreaks;
  34. bool m_last_was_space { false };
  35. bool m_last_was_newline { false };
  36. Utf8View m_utf8_view;
  37. Utf8View::Iterator m_iterator;
  38. };
  39. void compute_text_for_rendering(bool collapse, bool previous_is_empty_or_ends_in_whitespace);
  40. virtual RefPtr<Painting::Paintable> create_paintable() const override;
  41. private:
  42. virtual bool is_text_node() const final { return true; }
  43. void paint_cursor_if_needed(PaintContext&, const LineBoxFragment&) const;
  44. void paint_text_decoration(Gfx::Painter&, LineBoxFragment const&) const;
  45. String m_text_for_rendering;
  46. };
  47. template<>
  48. inline bool Node::fast_is<TextNode>() const { return is_text_node(); }
  49. }