TextNode.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. virtual void paint_fragment(PaintContext&, const LineBoxFragment&, Painting::PaintPhase) const override;
  19. struct Chunk {
  20. Utf8View view;
  21. size_t start { 0 };
  22. size_t length { 0 };
  23. bool has_breaking_newline { false };
  24. bool is_all_whitespace { false };
  25. };
  26. class ChunkIterator {
  27. public:
  28. ChunkIterator(StringView text, LayoutMode, bool wrap_lines, bool respect_linebreaks);
  29. Optional<Chunk> next();
  30. private:
  31. Optional<Chunk> try_commit_chunk(Utf8View::Iterator const& start, Utf8View::Iterator const& end, bool has_breaking_newline, bool must_commit = false) const;
  32. const LayoutMode m_layout_mode;
  33. const bool m_wrap_lines;
  34. const bool m_respect_linebreaks;
  35. bool m_last_was_space { false };
  36. bool m_last_was_newline { false };
  37. Utf8View m_utf8_view;
  38. Utf8View::Iterator m_iterator;
  39. };
  40. void compute_text_for_rendering(bool collapse, bool previous_is_empty_or_ends_in_whitespace);
  41. virtual RefPtr<Painting::Paintable> create_paintable() const override;
  42. private:
  43. virtual bool is_text_node() const final { return true; }
  44. void paint_cursor_if_needed(PaintContext&, const LineBoxFragment&) const;
  45. void paint_text_decoration(Gfx::Painter&, LineBoxFragment const&) const;
  46. String m_text_for_rendering;
  47. };
  48. template<>
  49. inline bool Node::fast_is<TextNode>() const { return is_text_node(); }
  50. }