OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/layout/ng/ng_out_of_flow_layout_part.h" | 5 #include "core/layout/ng/ng_out_of_flow_layout_part.h" |
6 | 6 |
7 #include "core/layout/ng/ng_absolute_utils.h" | 7 #include "core/layout/ng/ng_absolute_utils.h" |
8 #include "core/layout/ng/ng_block_node.h" | 8 #include "core/layout/ng/ng_block_node.h" |
9 #include "core/layout/ng/ng_constraint_space_builder.h" | 9 #include "core/layout/ng/ng_constraint_space_builder.h" |
10 #include "core/layout/ng/ng_fragment.h" | 10 #include "core/layout/ng/ng_fragment.h" |
11 #include "core/layout/ng/ng_fragment_builder.h" | |
11 #include "core/layout/ng/ng_length_utils.h" | 12 #include "core/layout/ng/ng_length_utils.h" |
12 #include "core/layout/ng/ng_physical_fragment.h" | 13 #include "core/layout/ng/ng_physical_fragment.h" |
13 #include "core/style/ComputedStyle.h" | 14 #include "core/style/ComputedStyle.h" |
14 | 15 |
15 namespace blink { | 16 namespace blink { |
16 | 17 |
18 namespace { | |
19 | |
20 // True if the container will contain an absolute child. | |
21 bool IsContainingBlockForAbsoluteChild(const ComputedStyle& container_style, | |
22 const ComputedStyle& child_style) { | |
23 EPosition position = child_style.position(); | |
24 bool contains_fixed = container_style.canContainFixedPositionObjects(); | |
25 bool contains_absolute = | |
26 container_style.canContainAbsolutePositionObjects() || contains_fixed; | |
27 | |
28 return (contains_absolute && position == AbsolutePosition) || | |
29 (contains_fixed && position == FixedPosition); | |
30 } | |
31 | |
32 } // namespace | |
33 | |
17 NGOutOfFlowLayoutPart::NGOutOfFlowLayoutPart( | 34 NGOutOfFlowLayoutPart::NGOutOfFlowLayoutPart( |
18 PassRefPtr<const ComputedStyle> container_style, | 35 const ComputedStyle& container_style, |
19 NGLogicalSize container_size) { | 36 NGFragmentBuilder* container_builder) |
37 : container_style_(container_style), container_builder_(container_builder) { | |
20 NGWritingMode writing_mode( | 38 NGWritingMode writing_mode( |
21 FromPlatformWritingMode(container_style->getWritingMode())); | 39 FromPlatformWritingMode(container_style_.getWritingMode())); |
22 | 40 |
23 NGBoxStrut borders = ComputeBorders(*container_style); | 41 NGBoxStrut borders = ComputeBorders(container_style_); |
24 parent_border_offset_ = | 42 parent_border_offset_ = |
25 NGLogicalOffset{borders.inline_start, borders.block_start}; | 43 NGLogicalOffset{borders.inline_start, borders.block_start}; |
26 parent_border_physical_offset_ = parent_border_offset_.ConvertToPhysical( | 44 parent_border_physical_offset_ = parent_border_offset_.ConvertToPhysical( |
27 writing_mode, container_style->direction(), | 45 writing_mode, container_style_.direction(), |
28 container_size.ConvertToPhysical(writing_mode), NGPhysicalSize()); | 46 container_builder_->Size().ConvertToPhysical(writing_mode), |
47 NGPhysicalSize()); | |
29 | 48 |
30 NGLogicalSize space_size = container_size; | 49 NGLogicalSize space_size = container_builder_->Size(); |
31 space_size.block_size -= borders.BlockSum(); | 50 space_size.block_size -= borders.BlockSum(); |
32 space_size.inline_size -= borders.InlineSum(); | 51 space_size.inline_size -= borders.InlineSum(); |
33 | 52 |
34 // Initialize ConstraintSpace | 53 // Initialize ConstraintSpace |
35 NGConstraintSpaceBuilder space_builder(writing_mode); | 54 NGConstraintSpaceBuilder space_builder(writing_mode); |
36 space_builder.SetAvailableSize(space_size); | 55 space_builder.SetAvailableSize(space_size); |
37 space_builder.SetPercentageResolutionSize(space_size); | 56 space_builder.SetPercentageResolutionSize(space_size); |
38 space_builder.SetIsNewFormattingContext(true); | 57 space_builder.SetIsNewFormattingContext(true); |
39 space_builder.SetTextDirection(container_style->direction()); | 58 space_builder.SetTextDirection(container_style_.direction()); |
40 parent_space_ = space_builder.ToConstraintSpace(); | 59 container_space_ = space_builder.ToConstraintSpace(); |
41 } | 60 } |
42 | 61 |
43 void NGOutOfFlowLayoutPart::Layout(NGBlockNode& node, | 62 void NGOutOfFlowLayoutPart::Run() { |
ikilpatrick
2017/01/20 23:48:30
plz halp, better name?
atotic
2017/01/23 07:37:56
I like Run(). Kinda like DoIt(), the all-purpose m
| |
44 NGStaticPosition static_position, | 63 HeapLinkedHashSet<WeakMember<NGBlockNode>> out_of_flow_candidates; |
45 NGFragment** fragment_out, | 64 Vector<NGStaticPosition> out_of_flow_candidate_positions; |
46 NGLogicalOffset* offset) { | 65 |
66 container_builder_->GetAndClearOutOfFlowDescendantCandidates( | |
67 &out_of_flow_candidates, &out_of_flow_candidate_positions); | |
68 size_t position_index = 0; | |
69 | |
70 while (!out_of_flow_candidates.isEmpty()) { | |
atotic
2017/01/23 07:37:56
Convert to standard iteration:
for (auto& child :
ikilpatrick
2017/01/24 19:11:32
Done.
| |
71 NGBlockNode* child = out_of_flow_candidates.first(); | |
72 out_of_flow_candidates.removeFirst(); | |
73 | |
74 NGStaticPosition static_position = | |
75 out_of_flow_candidate_positions[position_index++]; | |
76 | |
77 if (IsContainingBlockForAbsoluteChild(container_style_, *child->Style())) { | |
78 NGLogicalOffset offset; | |
79 NGFragment* fragment = LayoutChild(*child, static_position, &offset); | |
80 container_builder_->AddChild(fragment, offset); | |
81 | |
82 container_builder_->GetAndClearOutOfFlowDescendantCandidates( | |
83 &out_of_flow_candidates, &out_of_flow_candidate_positions); | |
84 } else { | |
85 container_builder_->AddOutOfFlowDescendant(child, static_position); | |
86 } | |
87 } | |
88 } | |
89 | |
90 NGFragment* NGOutOfFlowLayoutPart::LayoutChild(NGBlockNode& child, | |
atotic
2017/01/23 07:37:56
Strictly, the routine is laying out a "positioned
ikilpatrick
2017/01/24 19:11:32
Renamed:
child->descendant
parent->container
f
| |
91 NGStaticPosition static_position, | |
92 NGLogicalOffset* offset) { | |
47 // Adjust the static_position origin. The static_position coordinate origin is | 93 // Adjust the static_position origin. The static_position coordinate origin is |
48 // relative to the parent's border box, ng_absolute_utils expects it to be | 94 // relative to the parent's border box, ng_absolute_utils expects it to be |
49 // relative to the parent's padding box. | 95 // relative to the parent's padding box. |
50 static_position.offset -= parent_border_physical_offset_; | 96 static_position.offset -= parent_border_physical_offset_; |
51 | 97 |
52 NGFragment* fragment = nullptr; | 98 NGFragment* fragment = nullptr; |
53 Optional<MinAndMaxContentSizes> inline_estimate; | 99 Optional<MinAndMaxContentSizes> inline_estimate; |
54 Optional<LayoutUnit> block_estimate; | 100 Optional<LayoutUnit> block_estimate; |
55 | 101 |
56 if (AbsoluteNeedsChildInlineSize(*node.Style())) { | 102 if (AbsoluteNeedsChildInlineSize(*child.Style())) { |
57 inline_estimate = node.ComputeMinAndMaxContentSizesSync(); | 103 inline_estimate = child.ComputeMinAndMaxContentSizesSync(); |
58 } | 104 } |
59 | 105 |
60 NGAbsolutePhysicalPosition node_position = | 106 NGAbsolutePhysicalPosition node_position = |
61 ComputePartialAbsoluteWithChildInlineSize( | 107 ComputePartialAbsoluteWithChildInlineSize( |
62 *parent_space_, *node.Style(), static_position, inline_estimate); | 108 *container_space_, *child.Style(), static_position, inline_estimate); |
63 | 109 |
64 if (AbsoluteNeedsChildBlockSize(*node.Style())) { | 110 if (AbsoluteNeedsChildBlockSize(*child.Style())) { |
65 fragment = GenerateFragment(node, block_estimate, node_position); | 111 fragment = GenerateFragment(child, block_estimate, node_position); |
66 block_estimate = fragment->BlockSize(); | 112 block_estimate = fragment->BlockSize(); |
67 } | 113 } |
68 | 114 |
69 ComputeFullAbsoluteWithChildBlockSize(*parent_space_, *node.Style(), | 115 ComputeFullAbsoluteWithChildBlockSize(*container_space_, *child.Style(), |
70 static_position, block_estimate, | 116 static_position, block_estimate, |
71 &node_position); | 117 &node_position); |
72 | 118 |
73 // Skip this step if we produced a fragment when estimating the block size. | 119 // Skip this step if we produced a fragment when estimating the block size. |
74 if (!fragment) { | 120 if (!fragment) { |
75 block_estimate = | 121 block_estimate = |
76 node_position.size.ConvertToLogical(parent_space_->WritingMode()) | 122 node_position.size.ConvertToLogical(container_space_->WritingMode()) |
77 .block_size; | 123 .block_size; |
78 fragment = GenerateFragment(node, block_estimate, node_position); | 124 fragment = GenerateFragment(child, block_estimate, node_position); |
79 } | 125 } |
80 | 126 |
81 *fragment_out = fragment; | |
82 | |
83 // Compute logical offset, NGAbsolutePhysicalPosition is calculated relative | 127 // Compute logical offset, NGAbsolutePhysicalPosition is calculated relative |
84 // to the padding box so add back the parent's borders. | 128 // to the padding box so add back the parent's borders. |
85 NGBoxStrut inset = node_position.inset.ConvertToLogical( | 129 NGBoxStrut inset = node_position.inset.ConvertToLogical( |
86 parent_space_->WritingMode(), parent_space_->Direction()); | 130 container_space_->WritingMode(), container_space_->Direction()); |
87 offset->inline_offset = | 131 offset->inline_offset = |
88 inset.inline_start + parent_border_offset_.inline_offset; | 132 inset.inline_start + parent_border_offset_.inline_offset; |
89 offset->block_offset = inset.block_start + parent_border_offset_.block_offset; | 133 offset->block_offset = inset.block_start + parent_border_offset_.block_offset; |
134 | |
135 return fragment; | |
90 } | 136 } |
91 | 137 |
92 NGFragment* NGOutOfFlowLayoutPart::GenerateFragment( | 138 NGFragment* NGOutOfFlowLayoutPart::GenerateFragment( |
93 NGBlockNode& node, | 139 NGBlockNode& child, |
94 const Optional<LayoutUnit>& block_estimate, | 140 const Optional<LayoutUnit>& block_estimate, |
95 const NGAbsolutePhysicalPosition node_position) { | 141 const NGAbsolutePhysicalPosition node_position) { |
96 // The fragment is generated in one of these two scenarios: | 142 // The fragment is generated in one of these two scenarios: |
97 // 1. To estimate child's block size, in this case block_size is parent's | 143 // 1. To estimate child's block size, in this case block_size is parent's |
98 // available size. | 144 // available size. |
99 // 2. To compute final fragment, when block size is known from the absolute | 145 // 2. To compute final fragment, when block size is known from the absolute |
100 // position calculation. | 146 // position calculation. |
101 LayoutUnit inline_size = | 147 LayoutUnit inline_size = |
102 node_position.size.ConvertToLogical(parent_space_->WritingMode()) | 148 node_position.size.ConvertToLogical(container_space_->WritingMode()) |
103 .inline_size; | 149 .inline_size; |
104 LayoutUnit block_size = block_estimate | 150 LayoutUnit block_size = block_estimate |
105 ? *block_estimate | 151 ? *block_estimate |
106 : parent_space_->AvailableSize().block_size; | 152 : container_space_->AvailableSize().block_size; |
107 | 153 |
108 NGLogicalSize available_size{inline_size, block_size}; | 154 NGLogicalSize available_size{inline_size, block_size}; |
109 | 155 |
110 NGConstraintSpaceBuilder builder(parent_space_->WritingMode()); | 156 NGConstraintSpaceBuilder builder(container_space_->WritingMode()); |
111 builder.SetAvailableSize(available_size); | 157 builder.SetAvailableSize(available_size); |
112 builder.SetPercentageResolutionSize(parent_space_->AvailableSize()); | 158 builder.SetPercentageResolutionSize(container_space_->AvailableSize()); |
113 if (block_estimate) | 159 if (block_estimate) |
114 builder.SetIsFixedSizeBlock(true); | 160 builder.SetIsFixedSizeBlock(true); |
115 builder.SetIsFixedSizeInline(true); | 161 builder.SetIsFixedSizeInline(true); |
116 builder.SetIsNewFormattingContext(true); | 162 builder.SetIsNewFormattingContext(true); |
117 NGConstraintSpace* space = builder.ToConstraintSpace(); | 163 NGConstraintSpace* space = builder.ToConstraintSpace(); |
118 | 164 |
119 NGFragment* fragment; | 165 NGFragment* fragment; |
120 node.LayoutSync(space, &fragment); | 166 child.LayoutSync(space, &fragment); |
121 return fragment; | 167 return fragment; |
122 } | 168 } |
123 | 169 |
124 DEFINE_TRACE(NGOutOfFlowLayoutPart) { | |
125 visitor->trace(parent_space_); | |
126 } | |
127 | |
128 } // namespace blink | 170 } // namespace blink |
OLD | NEW |