InlineLevelIterator.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 should_force_break { false };
  39. bool is_collapsible_whitespace { false };
  40. float border_box_width() const
  41. {
  42. return border_start + padding_start + width + padding_end + border_end;
  43. }
  44. };
  45. InlineLevelIterator(Layout::InlineFormattingContext&, FormattingState&, Layout::BlockContainer const&, LayoutMode);
  46. Optional<Item> next(float available_width);
  47. private:
  48. void skip_to_next();
  49. void compute_next();
  50. void enter_text_node(Layout::TextNode const&, bool previous_is_empty_or_ends_in_whitespace);
  51. void enter_node_with_box_model_metrics(Layout::NodeWithStyleAndBoxModelMetrics const&);
  52. void exit_node_with_box_model_metrics();
  53. void add_extra_box_model_metrics_to_item(Item&, bool add_leading_metrics, bool add_trailing_metrics);
  54. Layout::Node const* next_inline_node_in_pre_order(Layout::Node const& current, Layout::Node const* stay_within);
  55. Layout::InlineFormattingContext& m_inline_formatting_context;
  56. Layout::FormattingState& m_formatting_state;
  57. Layout::BlockContainer const& m_container;
  58. Layout::Node const* m_current_node { nullptr };
  59. Layout::Node const* m_next_node { nullptr };
  60. LayoutMode const m_layout_mode;
  61. struct TextNodeContext {
  62. bool do_collapse {};
  63. bool do_wrap_lines {};
  64. bool do_respect_linebreaks {};
  65. bool is_first_chunk {};
  66. bool is_last_chunk {};
  67. TextNode::ChunkIterator chunk_iterator;
  68. Optional<TextNode::Chunk> next_chunk {};
  69. };
  70. Optional<TextNodeContext> m_text_node_context;
  71. struct ExtraBoxMetrics {
  72. float margin { 0 };
  73. float border { 0 };
  74. float padding { 0 };
  75. };
  76. Optional<ExtraBoxMetrics> m_extra_leading_metrics;
  77. Optional<ExtraBoxMetrics> m_extra_trailing_metrics;
  78. Vector<NodeWithStyleAndBoxModelMetrics const&> m_box_model_node_stack;
  79. };
  80. }