/src/serenity/Userland/Libraries/LibWeb/Layout/InlineLevelIterator.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2022, Andreas Kling <kling@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <AK/Noncopyable.h> |
10 | | #include <LibWeb/Layout/BlockContainer.h> |
11 | | #include <LibWeb/Layout/InlineNode.h> |
12 | | #include <LibWeb/Layout/LayoutState.h> |
13 | | #include <LibWeb/Layout/TextNode.h> |
14 | | |
15 | | namespace Web::Layout { |
16 | | |
17 | | // This class iterates over all the inline-level objects within an inline formatting context. |
18 | | // By repeatedly calling next() with the remaining available width on the current line, |
19 | | // it returns an "Item" representing the next piece of inline-level content to be placed on the line. |
20 | | class InlineLevelIterator { |
21 | | AK_MAKE_NONCOPYABLE(InlineLevelIterator); |
22 | | AK_MAKE_NONMOVABLE(InlineLevelIterator); |
23 | | |
24 | | public: |
25 | | struct Item { |
26 | | enum class Type { |
27 | | Text, |
28 | | Element, |
29 | | ForcedBreak, |
30 | | AbsolutelyPositionedElement, |
31 | | FloatingElement, |
32 | | }; |
33 | | Type type {}; |
34 | | JS::GCPtr<Layout::Node const> node {}; |
35 | | RefPtr<Gfx::GlyphRun> glyph_run {}; |
36 | | size_t offset_in_node { 0 }; |
37 | | size_t length_in_node { 0 }; |
38 | | CSSPixels width { 0.0f }; |
39 | | CSSPixels padding_start { 0.0f }; |
40 | | CSSPixels padding_end { 0.0f }; |
41 | | CSSPixels border_start { 0.0f }; |
42 | | CSSPixels border_end { 0.0f }; |
43 | | CSSPixels margin_start { 0.0f }; |
44 | | CSSPixels margin_end { 0.0f }; |
45 | | bool is_collapsible_whitespace { false }; |
46 | | |
47 | | CSSPixels border_box_width() const |
48 | 0 | { |
49 | 0 | return border_start + padding_start + width + padding_end + border_end; |
50 | 0 | } |
51 | | }; |
52 | | |
53 | | InlineLevelIterator(Layout::InlineFormattingContext&, LayoutState&, Layout::BlockContainer const& containing_block, LayoutState::UsedValues const& containing_block_used_values, LayoutMode); |
54 | | |
55 | | Optional<Item> next(); |
56 | | CSSPixels next_non_whitespace_sequence_width(); |
57 | | |
58 | | private: |
59 | | Optional<Item> next_without_lookahead(); |
60 | | Gfx::GlyphRun::TextType resolve_text_direction_from_context(); |
61 | | void skip_to_next(); |
62 | | void compute_next(); |
63 | | |
64 | | void enter_text_node(Layout::TextNode const&); |
65 | | |
66 | | void enter_node_with_box_model_metrics(Layout::NodeWithStyleAndBoxModelMetrics const&); |
67 | | void exit_node_with_box_model_metrics(); |
68 | | |
69 | | void add_extra_box_model_metrics_to_item(Item&, bool add_leading_metrics, bool add_trailing_metrics); |
70 | | |
71 | | Layout::Node const* next_inline_node_in_pre_order(Layout::Node const& current, Layout::Node const* stay_within); |
72 | | |
73 | | Layout::InlineFormattingContext& m_inline_formatting_context; |
74 | | Layout::LayoutState& m_layout_state; |
75 | | JS::NonnullGCPtr<BlockContainer const> m_containing_block; |
76 | | LayoutState::UsedValues const& m_containing_block_used_values; |
77 | | JS::GCPtr<Layout::Node const> m_current_node; |
78 | | JS::GCPtr<Layout::Node const> m_next_node; |
79 | | LayoutMode const m_layout_mode; |
80 | | |
81 | | struct TextNodeContext { |
82 | | bool do_collapse {}; |
83 | | bool do_wrap_lines {}; |
84 | | bool do_respect_linebreaks {}; |
85 | | bool is_first_chunk {}; |
86 | | bool is_last_chunk {}; |
87 | | TextNode::ChunkIterator chunk_iterator; |
88 | | Optional<Gfx::GlyphRun::TextType> last_known_direction {}; |
89 | | }; |
90 | | |
91 | | Optional<TextNodeContext> m_text_node_context; |
92 | | |
93 | | struct ExtraBoxMetrics { |
94 | | CSSPixels margin { 0 }; |
95 | | CSSPixels border { 0 }; |
96 | | CSSPixels padding { 0 }; |
97 | | }; |
98 | | |
99 | | Optional<ExtraBoxMetrics> m_extra_leading_metrics; |
100 | | Optional<ExtraBoxMetrics> m_extra_trailing_metrics; |
101 | | |
102 | | Vector<JS::NonnullGCPtr<NodeWithStyleAndBoxModelMetrics const>> m_box_model_node_stack; |
103 | | Queue<InlineLevelIterator::Item> m_lookahead_items; |
104 | | }; |
105 | | |
106 | | } |