InlineLevelIterator.h 3.4 KB

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