InlineLevelIterator.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. RefPtr<Gfx::GlyphRun> 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. Gfx::GlyphRun::TextType resolve_text_direction_from_context();
  52. void skip_to_next();
  53. void compute_next();
  54. void enter_text_node(Layout::TextNode const&);
  55. void enter_node_with_box_model_metrics(Layout::NodeWithStyleAndBoxModelMetrics const&);
  56. void exit_node_with_box_model_metrics();
  57. void add_extra_box_model_metrics_to_item(Item&, bool add_leading_metrics, bool add_trailing_metrics);
  58. Layout::Node const* next_inline_node_in_pre_order(Layout::Node const& current, Layout::Node const* stay_within);
  59. Layout::InlineFormattingContext& m_inline_formatting_context;
  60. Layout::LayoutState& m_layout_state;
  61. JS::NonnullGCPtr<BlockContainer const> m_containing_block;
  62. LayoutState::UsedValues const& m_containing_block_used_values;
  63. JS::GCPtr<Layout::Node const> m_current_node;
  64. JS::GCPtr<Layout::Node const> m_next_node;
  65. LayoutMode const m_layout_mode;
  66. struct TextNodeContext {
  67. bool do_collapse {};
  68. bool do_wrap_lines {};
  69. bool do_respect_linebreaks {};
  70. bool is_first_chunk {};
  71. bool is_last_chunk {};
  72. TextNode::ChunkIterator chunk_iterator;
  73. Optional<Gfx::GlyphRun::TextType> last_known_direction {};
  74. };
  75. Optional<TextNodeContext> m_text_node_context;
  76. struct ExtraBoxMetrics {
  77. CSSPixels margin { 0 };
  78. CSSPixels border { 0 };
  79. CSSPixels padding { 0 };
  80. };
  81. Optional<ExtraBoxMetrics> m_extra_leading_metrics;
  82. Optional<ExtraBoxMetrics> m_extra_trailing_metrics;
  83. Vector<JS::NonnullGCPtr<NodeWithStyleAndBoxModelMetrics const>> m_box_model_node_stack;
  84. Queue<InlineLevelIterator::Item> m_lookahead_items;
  85. };
  86. }