TextNode.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. String const& 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) const;
  31. const LayoutMode m_layout_mode;
  32. bool const m_wrap_lines;
  33. bool const m_respect_linebreaks;
  34. Utf8View m_utf8_view;
  35. Utf8View::Iterator m_iterator;
  36. };
  37. void compute_text_for_rendering(bool collapse);
  38. virtual RefPtr<Painting::Paintable> create_paintable() const override;
  39. private:
  40. virtual bool is_text_node() const final { return true; }
  41. String m_text_for_rendering;
  42. };
  43. template<>
  44. inline bool Node::fast_is<TextNode>() const { return is_text_node(); }
  45. }