TextNode.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 wrap_breaks);
  30. Optional<Chunk> next();
  31. private:
  32. Optional<Chunk> try_commit_chunk(Utf8View::Iterator const&, bool has_breaking_newline, bool must_commit = false);
  33. const LayoutMode m_layout_mode;
  34. const bool m_wrap_lines;
  35. const bool m_wrap_breaks;
  36. bool m_last_was_space { false };
  37. bool m_last_was_newline { false };
  38. Utf8View m_utf8_view;
  39. Utf8View::Iterator m_start_of_chunk;
  40. Utf8View::Iterator m_iterator;
  41. };
  42. void compute_text_for_rendering(bool collapse, bool previous_is_empty_or_ends_in_whitespace);
  43. private:
  44. virtual bool is_text_node() const final { return true; }
  45. virtual bool wants_mouse_events() const override;
  46. virtual void handle_mousedown(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned modifiers) override;
  47. virtual void handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned modifiers) override;
  48. virtual void handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned modifiers) override;
  49. void split_into_lines_by_rules(InlineFormattingContext&, LayoutMode, bool do_collapse, bool do_wrap_lines, bool do_wrap_breaks);
  50. void paint_cursor_if_needed(PaintContext&, const LineBoxFragment&) const;
  51. void paint_text_decoration(Gfx::Painter&, LineBoxFragment const&) const;
  52. String m_text_for_rendering;
  53. };
  54. template<>
  55. inline bool Node::fast_is<TextNode>() const { return is_text_node(); }
  56. }