InlineLevelIterator.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Noncopyable.h>
  8. #include <LibWeb/Layout/Box.h>
  9. #include <LibWeb/Layout/TextNode.h>
  10. namespace Web::Layout {
  11. // This class iterates over all the inline-level objects within an inline formatting context.
  12. // By repeatedly calling next() with the remaining available width on the current line,
  13. // it returns an "Item" representing the next piece of inline-level content to be placed on the line.
  14. class InlineLevelIterator {
  15. AK_MAKE_NONCOPYABLE(InlineLevelIterator);
  16. AK_MAKE_NONMOVABLE(InlineLevelIterator);
  17. public:
  18. struct Item {
  19. enum class Type {
  20. Text,
  21. Element,
  22. ForcedBreak,
  23. };
  24. Type type {};
  25. Layout::Node* node { nullptr };
  26. size_t offset_in_node { 0 };
  27. size_t length_in_node { 0 };
  28. float width { 0.0f };
  29. bool should_force_break { false };
  30. };
  31. explicit InlineLevelIterator(Layout::Box& container, LayoutMode layout_mode)
  32. : m_container(container)
  33. , m_current_node(container.first_child())
  34. , m_layout_mode(layout_mode)
  35. {
  36. }
  37. Optional<Item> next(float available_width);
  38. private:
  39. void skip_to_next();
  40. void enter_text_node(Layout::TextNode&);
  41. Layout::Box& m_container;
  42. Layout::Node* m_current_node { nullptr };
  43. LayoutMode const m_layout_mode;
  44. struct TextNodeContext {
  45. bool do_collapse {};
  46. bool do_wrap_lines {};
  47. bool do_respect_linebreaks {};
  48. TextNode::ChunkIterator chunk_iterator;
  49. };
  50. Optional<TextNodeContext> m_text_node_context;
  51. };
  52. }