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_length_utils.h" | 5 #include "core/layout/ng/ng_length_utils.h" |
6 | 6 |
7 #include "core/layout/ng/ng_constraint_space.h" | 7 #include "core/layout/ng/ng_constraint_space.h" |
8 #include "core/layout/ng/ng_fragment.h" | 8 #include "core/layout/ng/ng_fragment.h" |
9 #include "core/style/ComputedStyle.h" | 9 #include "core/style/ComputedStyle.h" |
10 #include "platform/LayoutUnit.h" | 10 #include "platform/LayoutUnit.h" |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 } | 55 } |
56 | 56 |
57 } // namespace | 57 } // namespace |
58 | 58 |
59 LayoutUnit ResolveInlineLength(const NGConstraintSpace& constraintSpace, | 59 LayoutUnit ResolveInlineLength(const NGConstraintSpace& constraintSpace, |
60 const ComputedStyle& style, | 60 const ComputedStyle& style, |
61 const Length& length, | 61 const Length& length, |
62 LengthResolveType type) { | 62 LengthResolveType type) { |
63 // TODO(layout-ng): Handle min/max/fit-content | 63 // TODO(layout-ng): Handle min/max/fit-content |
64 DCHECK(!length.isMaxSizeNone()); | 64 DCHECK(!length.isMaxSizeNone()); |
65 DCHECK_GE(constraintSpace.ContainerSize().inline_size, LayoutUnit()); | 65 DCHECK_GE(constraintSpace.AvailableSize().inline_size, LayoutUnit()); |
66 | 66 |
67 if (type == LengthResolveType::MinSize && length.isAuto()) | 67 if (type == LengthResolveType::MinSize && length.isAuto()) |
68 return LayoutUnit(); | 68 return LayoutUnit(); |
69 | 69 |
70 if (type == LengthResolveType::MarginBorderPaddingSize && length.isAuto()) | 70 if (type == LengthResolveType::MarginBorderPaddingSize && length.isAuto()) |
71 return LayoutUnit(); | 71 return LayoutUnit(); |
72 | 72 |
73 // We don't need this when we're resolving margin/border/padding; skip | 73 // We don't need this when we're resolving margin/border/padding; skip |
74 // computing it as an optimization and to simplify the code below. | 74 // computing it as an optimization and to simplify the code below. |
75 NGBoxStrut border_and_padding; | 75 NGBoxStrut border_and_padding; |
76 if (type != LengthResolveType::MarginBorderPaddingSize) { | 76 if (type != LengthResolveType::MarginBorderPaddingSize) { |
77 border_and_padding = | 77 border_and_padding = |
78 ComputeBorders(style) + ComputePadding(constraintSpace, style); | 78 ComputeBorders(style) + ComputePadding(constraintSpace, style); |
79 } | 79 } |
80 LayoutUnit container_size = constraintSpace.ContainerSize().inline_size; | |
81 switch (length.type()) { | 80 switch (length.type()) { |
82 case Auto: | 81 case Auto: |
83 case FillAvailable: { | 82 case FillAvailable: { |
| 83 LayoutUnit content_size = constraintSpace.AvailableSize().inline_size; |
84 NGBoxStrut margins = | 84 NGBoxStrut margins = |
85 ComputeMargins(constraintSpace, style, | 85 ComputeMargins(constraintSpace, style, |
86 FromPlatformWritingMode(style.getWritingMode()), | 86 FromPlatformWritingMode(style.getWritingMode()), |
87 FromPlatformDirection(style.direction())); | 87 FromPlatformDirection(style.direction())); |
88 return std::max(border_and_padding.InlineSum(), | 88 return std::max(border_and_padding.InlineSum(), |
89 container_size - margins.InlineSum()); | 89 content_size - margins.InlineSum()); |
90 } | 90 } |
91 case Percent: | 91 case Percent: |
92 case Fixed: | 92 case Fixed: |
93 case Calculated: { | 93 case Calculated: { |
94 LayoutUnit value = valueForLength(length, container_size); | 94 LayoutUnit percentage_resolution_size = |
| 95 constraintSpace.PercentageResolutionSize().inline_size; |
| 96 LayoutUnit value = valueForLength(length, percentage_resolution_size); |
95 if (style.boxSizing() == BoxSizingContentBox) { | 97 if (style.boxSizing() == BoxSizingContentBox) { |
96 value += border_and_padding.InlineSum(); | 98 value += border_and_padding.InlineSum(); |
97 } else { | 99 } else { |
98 value = std::max(border_and_padding.InlineSum(), value); | 100 value = std::max(border_and_padding.InlineSum(), value); |
99 } | 101 } |
100 return value; | 102 return value; |
101 } | 103 } |
102 case MinContent: | 104 case MinContent: |
103 case MaxContent: | 105 case MaxContent: |
104 case FitContent: | 106 case FitContent: |
(...skipping 17 matching lines...) Expand all Loading... |
122 LengthResolveType type) { | 124 LengthResolveType type) { |
123 DCHECK(!length.isMaxSizeNone()); | 125 DCHECK(!length.isMaxSizeNone()); |
124 DCHECK(type != LengthResolveType::MarginBorderPaddingSize); | 126 DCHECK(type != LengthResolveType::MarginBorderPaddingSize); |
125 | 127 |
126 if (type == LengthResolveType::MinSize && length.isAuto()) | 128 if (type == LengthResolveType::MinSize && length.isAuto()) |
127 return LayoutUnit(); | 129 return LayoutUnit(); |
128 | 130 |
129 // Make sure that indefinite percentages resolve to NGSizeIndefinite, not to | 131 // Make sure that indefinite percentages resolve to NGSizeIndefinite, not to |
130 // a random negative number. | 132 // a random negative number. |
131 if (length.isPercentOrCalc() && | 133 if (length.isPercentOrCalc() && |
132 constraintSpace.ContainerSize().block_size == NGSizeIndefinite) | 134 constraintSpace.PercentageResolutionSize().block_size == NGSizeIndefinite) |
133 return contentSize; | 135 return contentSize; |
134 | 136 |
135 // We don't need this when we're resolving margin/border/padding; skip | 137 // We don't need this when we're resolving margin/border/padding; skip |
136 // computing it as an optimization and to simplify the code below. | 138 // computing it as an optimization and to simplify the code below. |
137 NGBoxStrut border_and_padding; | 139 NGBoxStrut border_and_padding; |
138 if (type != LengthResolveType::MarginBorderPaddingSize) { | 140 if (type != LengthResolveType::MarginBorderPaddingSize) { |
139 border_and_padding = | 141 border_and_padding = |
140 ComputeBorders(style) + ComputePadding(constraintSpace, style); | 142 ComputeBorders(style) + ComputePadding(constraintSpace, style); |
141 } | 143 } |
142 LayoutUnit container_size = constraintSpace.ContainerSize().block_size; | |
143 switch (length.type()) { | 144 switch (length.type()) { |
144 case FillAvailable: { | 145 case FillAvailable: { |
| 146 LayoutUnit content_size = constraintSpace.AvailableSize().block_size; |
145 NGBoxStrut margins = | 147 NGBoxStrut margins = |
146 ComputeMargins(constraintSpace, style, | 148 ComputeMargins(constraintSpace, style, |
147 FromPlatformWritingMode(style.getWritingMode()), | 149 FromPlatformWritingMode(style.getWritingMode()), |
148 FromPlatformDirection(style.direction())); | 150 FromPlatformDirection(style.direction())); |
149 return std::max(border_and_padding.BlockSum(), | 151 return std::max(border_and_padding.BlockSum(), |
150 container_size - margins.BlockSum()); | 152 content_size - margins.BlockSum()); |
151 } | 153 } |
152 case Percent: | 154 case Percent: |
153 case Fixed: | 155 case Fixed: |
154 case Calculated: { | 156 case Calculated: { |
155 LayoutUnit value = valueForLength(length, container_size); | 157 LayoutUnit percentage_resolution_size = |
| 158 constraintSpace.PercentageResolutionSize().block_size; |
| 159 LayoutUnit value = valueForLength(length, percentage_resolution_size); |
156 if (style.boxSizing() == BoxSizingContentBox) { | 160 if (style.boxSizing() == BoxSizingContentBox) { |
157 value += border_and_padding.BlockSum(); | 161 value += border_and_padding.BlockSum(); |
158 } else { | 162 } else { |
159 value = std::max(border_and_padding.BlockSum(), value); | 163 value = std::max(border_and_padding.BlockSum(), value); |
160 } | 164 } |
161 return value; | 165 return value; |
162 } | 166 } |
163 case Auto: | 167 case Auto: |
164 case MinContent: | 168 case MinContent: |
165 case MaxContent: | 169 case MaxContent: |
(...skipping 11 matching lines...) Expand all Loading... |
177 default: | 181 default: |
178 NOTREACHED(); | 182 NOTREACHED(); |
179 return border_and_padding.BlockSum(); | 183 return border_and_padding.BlockSum(); |
180 } | 184 } |
181 } | 185 } |
182 | 186 |
183 LayoutUnit ComputeInlineSizeForFragment( | 187 LayoutUnit ComputeInlineSizeForFragment( |
184 const NGConstraintSpace& constraintSpace, | 188 const NGConstraintSpace& constraintSpace, |
185 const ComputedStyle& style) { | 189 const ComputedStyle& style) { |
186 if (constraintSpace.FixedInlineSize()) | 190 if (constraintSpace.FixedInlineSize()) |
187 return constraintSpace.ContainerSize().inline_size; | 191 return constraintSpace.AvailableSize().inline_size; |
188 | 192 |
189 LayoutUnit extent = | 193 LayoutUnit extent = |
190 ResolveInlineLength(constraintSpace, style, style.logicalWidth(), | 194 ResolveInlineLength(constraintSpace, style, style.logicalWidth(), |
191 LengthResolveType::ContentSize); | 195 LengthResolveType::ContentSize); |
192 | 196 |
193 Length maxLength = style.logicalMaxWidth(); | 197 Length maxLength = style.logicalMaxWidth(); |
194 if (!maxLength.isMaxSizeNone()) { | 198 if (!maxLength.isMaxSizeNone()) { |
195 LayoutUnit max = ResolveInlineLength(constraintSpace, style, maxLength, | 199 LayoutUnit max = ResolveInlineLength(constraintSpace, style, maxLength, |
196 LengthResolveType::MaxSize); | 200 LengthResolveType::MaxSize); |
197 extent = std::min(extent, max); | 201 extent = std::min(extent, max); |
198 } | 202 } |
199 | 203 |
200 LayoutUnit min = | 204 LayoutUnit min = |
201 ResolveInlineLength(constraintSpace, style, style.logicalMinWidth(), | 205 ResolveInlineLength(constraintSpace, style, style.logicalMinWidth(), |
202 LengthResolveType::MinSize); | 206 LengthResolveType::MinSize); |
203 extent = std::max(extent, min); | 207 extent = std::max(extent, min); |
204 return extent; | 208 return extent; |
205 } | 209 } |
206 | 210 |
207 LayoutUnit ComputeBlockSizeForFragment(const NGConstraintSpace& constraintSpace, | 211 LayoutUnit ComputeBlockSizeForFragment(const NGConstraintSpace& constraintSpace, |
208 const ComputedStyle& style, | 212 const ComputedStyle& style, |
209 LayoutUnit contentSize) { | 213 LayoutUnit contentSize) { |
210 if (constraintSpace.FixedBlockSize()) | 214 if (constraintSpace.FixedBlockSize()) |
211 return constraintSpace.ContainerSize().block_size; | 215 return constraintSpace.AvailableSize().block_size; |
212 | 216 |
213 LayoutUnit extent = | 217 LayoutUnit extent = |
214 ResolveBlockLength(constraintSpace, style, style.logicalHeight(), | 218 ResolveBlockLength(constraintSpace, style, style.logicalHeight(), |
215 contentSize, LengthResolveType::ContentSize); | 219 contentSize, LengthResolveType::ContentSize); |
216 if (extent == NGSizeIndefinite) { | 220 if (extent == NGSizeIndefinite) { |
217 DCHECK_EQ(contentSize, NGSizeIndefinite); | 221 DCHECK_EQ(contentSize, NGSizeIndefinite); |
218 return extent; | 222 return extent; |
219 } | 223 } |
220 | 224 |
221 Length maxLength = style.logicalMaxHeight(); | 225 Length maxLength = style.logicalMaxHeight(); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 return padding; | 288 return padding; |
285 } | 289 } |
286 | 290 |
287 void ApplyAutoMargins(const NGConstraintSpace& constraint_space, | 291 void ApplyAutoMargins(const NGConstraintSpace& constraint_space, |
288 const ComputedStyle& style, | 292 const ComputedStyle& style, |
289 const NGFragment& fragment, | 293 const NGFragment& fragment, |
290 NGBoxStrut* margins) { | 294 NGBoxStrut* margins) { |
291 DCHECK(margins) << "Margins cannot be NULL here"; | 295 DCHECK(margins) << "Margins cannot be NULL here"; |
292 const LayoutUnit used_space = fragment.InlineSize() + margins->InlineSum(); | 296 const LayoutUnit used_space = fragment.InlineSize() + margins->InlineSum(); |
293 const LayoutUnit available_space = | 297 const LayoutUnit available_space = |
294 constraint_space.ContainerSize().inline_size - used_space; | 298 constraint_space.AvailableSize().inline_size - used_space; |
295 if (available_space < LayoutUnit()) | 299 if (available_space < LayoutUnit()) |
296 return; | 300 return; |
297 if (style.marginStart().isAuto() && style.marginEnd().isAuto()) { | 301 if (style.marginStart().isAuto() && style.marginEnd().isAuto()) { |
298 margins->inline_start = available_space / 2; | 302 margins->inline_start = available_space / 2; |
299 margins->inline_end = available_space - margins->inline_start; | 303 margins->inline_end = available_space - margins->inline_start; |
300 } else if (style.marginStart().isAuto()) { | 304 } else if (style.marginStart().isAuto()) { |
301 margins->inline_start = available_space; | 305 margins->inline_start = available_space; |
302 } else if (style.marginEnd().isAuto()) { | 306 } else if (style.marginEnd().isAuto()) { |
303 margins->inline_end = available_space; | 307 margins->inline_end = available_space; |
304 } | 308 } |
305 } | 309 } |
306 | 310 |
307 } // namespace blink | 311 } // namespace blink |
OLD | NEW |