InlineLevelIterator.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/BlockContainer.h>
  9. #include <LibWeb/Layout/InlineNode.h>
  10. #include <LibWeb/Layout/TextNode.h>
  11. namespace Web::Layout {
  12. // This class iterates over all the inline-level objects within an inline formatting context.
  13. // By repeatedly calling next() with the remaining available width on the current line,
  14. // it returns an "Item" representing the next piece of inline-level content to be placed on the line.
  15. class InlineLevelIterator {
  16. AK_MAKE_NONCOPYABLE(InlineLevelIterator);
  17. AK_MAKE_NONMOVABLE(InlineLevelIterator);
  18. public:
  19. struct Item {
  20. enum class Type {
  21. Text,
  22. Element,
  23. ForcedBreak,
  24. AbsolutelyPositionedElement,
  25. FloatingElement,
  26. };
  27. Type type {};
  28. Layout::Node const* node { nullptr };
  29. size_t offset_in_node { 0 };
  30. size_t length_in_node { 0 };
  31. float width { 0.0f };
  32. float padding_start { 0.0f };
  33. float padding_end { 0.0f };
  34. float border_start { 0.0f };
  35. float border_end { 0.0f };
  36. float margin_start { 0.0f };
  37. float margin_end { 0.0f };
  38. bool is_collapsible_whitespace { false };
  39. float border_box_width() const
  40. {
  41. return border_start + padding_start + width + padding_end + border_end;
  42. }
  43. };
  44. InlineLevelIterator(Layout::InlineFormattingContext&, FormattingState&, Layout::BlockContainer const&, LayoutMode);
  45. Optional<Item> next(float available_width);
  46. private:
  47. void skip_to_next();
  48. void compute_next();
  49. void enter_text_node(Layout::TextNode const&);
  50. void enter_node_with_box_model_metrics(Layout::NodeWithStyleAndBoxModelMetrics const&);
  51. void exit_node_with_box_model_metrics();
  52. void add_extra_box_model_metrics_to_item(Item&, bool add_leading_metrics, bool add_trailing_metrics);
  53. Layout::Node const* next_inline_node_in_pre_order(Layout::Node const& current, Layout::Node const* stay_within);
  54. Layout::InlineFormattingContext& m_inline_formatting_context;
  55. Layout::FormattingState& m_formatting_state;
  56. Layout::BlockContainer const& m_container;
  57. Layout::Node const* m_current_node { nullptr };
  58. Layout::Node const* m_next_node { nullptr };
  59. LayoutMode const m_layout_mode;
  60. struct TextNodeContext {
  61. bool do_collapse {};
  62. bool do_wrap_lines {};
  63. bool do_respect_linebreaks {};
  64. bool is_first_chunk {};
  65. bool is_last_chunk {};
  66. TextNode::ChunkIterator chunk_iterator;
  67. Optional<TextNode::Chunk> next_chunk {};
  68. };
  69. Optional<TextNodeContext> m_text_node_context;
  70. struct ExtraBoxMetrics {
  71. float margin { 0 };
  72. float border { 0 };
  73. float padding { 0 };
  74. };
  75. Optional<ExtraBoxMetrics> m_extra_leading_metrics;
  76. Optional<ExtraBoxMetrics> m_extra_trailing_metrics;
  77. Vector<NodeWithStyleAndBoxModelMetrics const&> m_box_model_node_stack;
  78. };
  79. }