TextNode.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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&, PaintPhase) const override;
  19. virtual void split_into_lines(InlineFormattingContext&, LayoutMode) override;
  20. struct Chunk {
  21. Utf8View view;
  22. size_t start { 0 };
  23. size_t length { 0 };
  24. bool has_breaking_newline { false };
  25. bool is_all_whitespace { false };
  26. };
  27. class ChunkIterator {
  28. public:
  29. ChunkIterator(StringView const& text, LayoutMode, bool wrap_lines, bool respect_linebreaks);
  30. Optional<Chunk> next();
  31. private:
  32. Optional<Chunk> try_commit_chunk(Utf8View::Iterator const& start, Utf8View::Iterator const& end, bool has_breaking_newline, bool must_commit = false);
  33. const LayoutMode m_layout_mode;
  34. const bool m_wrap_lines;
  35. const bool m_respect_linebreaks;
  36. bool m_last_was_space { false };
  37. bool m_last_was_newline { false };
  38. Utf8View m_utf8_view;
  39. Utf8View::Iterator m_iterator;
  40. };
  41. void compute_text_for_rendering(bool collapse, bool previous_is_empty_or_ends_in_whitespace);
  42. private:
  43. virtual bool is_text_node() const final { return true; }
  44. virtual bool wants_mouse_events() const override;
  45. virtual void handle_mousedown(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned modifiers) override;
  46. virtual void handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned modifiers) override;
  47. virtual void handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned modifiers) override;
  48. void split_into_lines_by_rules(InlineFormattingContext&, LayoutMode, bool do_collapse, bool do_wrap_lines, bool do_respect_linebreaks);
  49. void paint_cursor_if_needed(PaintContext&, const LineBoxFragment&) const;
  50. void paint_text_decoration(Gfx::Painter&, LineBoxFragment const&) const;
  51. String m_text_for_rendering;
  52. };
  53. template<>
  54. inline bool Node::fast_is<TextNode>() const { return is_text_node(); }
  55. }