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_constraint_space_builder.h" | 8 #include "core/layout/ng/ng_constraint_space_builder.h" |
9 #include "core/layout/ng/ng_fragment.h" | 9 #include "core/layout/ng/ng_fragment.h" |
10 #include "core/style/ComputedStyle.h" | 10 #include "core/style/ComputedStyle.h" |
11 #include "platform/LayoutUnit.h" | 11 #include "platform/LayoutUnit.h" |
12 #include "platform/Length.h" | 12 #include "platform/Length.h" |
13 #include "wtf/Optional.h" | 13 #include "wtf/Optional.h" |
14 | 14 |
15 namespace blink { | 15 namespace blink { |
16 // TODO(layout-ng): | 16 // TODO(layout-ng): |
17 // - replaced calculations | 17 // - replaced calculations |
18 // - Take scrollbars into account | 18 // - Take scrollbars into account |
19 | 19 |
20 bool NeedMinMaxContentSize(const NGConstraintSpace& constraint_space, | 20 bool NeedMinAndMaxContentSizes(const NGConstraintSpace& constraint_space, |
21 const ComputedStyle& style) { | 21 const ComputedStyle& style) { |
22 // This check is technically too broad (fill-available does not need intrinsic | 22 // This check is technically too broad (fill-available does not need intrinsic |
23 // size computation) but that's a rare case and only affects performance, not | 23 // size computation) but that's a rare case and only affects performance, not |
24 // correctness. | 24 // correctness. |
25 return constraint_space.IsShrinkToFit() || | 25 return constraint_space.IsShrinkToFit() || |
26 style.logicalWidth().isIntrinsic() || | 26 style.logicalWidth().isIntrinsic() || |
27 style.logicalMinWidth().isIntrinsic() || | 27 style.logicalMinWidth().isIntrinsic() || |
28 style.logicalMaxWidth().isIntrinsic(); | 28 style.logicalMaxWidth().isIntrinsic(); |
29 } | 29 } |
30 | 30 |
31 bool NeedMinMaxContentSizeForContentContribution(const ComputedStyle& style) { | 31 bool NeedMinAndMaxContentSizesForContentContribution( |
| 32 const ComputedStyle& style) { |
32 return style.logicalWidth().isIntrinsicOrAuto() || | 33 return style.logicalWidth().isIntrinsicOrAuto() || |
33 style.logicalMinWidth().isIntrinsic() || | 34 style.logicalMinWidth().isIntrinsic() || |
34 style.logicalMaxWidth().isIntrinsic(); | 35 style.logicalMaxWidth().isIntrinsic(); |
35 } | 36 } |
36 | 37 |
37 LayoutUnit ResolveInlineLength( | 38 LayoutUnit ResolveInlineLength( |
38 const NGConstraintSpace& constraint_space, | 39 const NGConstraintSpace& constraint_space, |
39 const ComputedStyle& style, | 40 const ComputedStyle& style, |
40 const WTF::Optional<MinMaxContentSize>& min_and_max, | 41 const WTF::Optional<MinAndMaxContentSizes>& min_and_max, |
41 const Length& length, | 42 const Length& length, |
42 LengthResolveType type) { | 43 LengthResolveType type) { |
43 DCHECK(!length.isMaxSizeNone()); | 44 DCHECK(!length.isMaxSizeNone()); |
44 DCHECK_GE(constraint_space.AvailableSize().inline_size, LayoutUnit()); | 45 DCHECK_GE(constraint_space.AvailableSize().inline_size, LayoutUnit()); |
45 DCHECK_GE(constraint_space.PercentageResolutionSize().inline_size, | 46 DCHECK_GE(constraint_space.PercentageResolutionSize().inline_size, |
46 LayoutUnit()); | 47 LayoutUnit()); |
47 | 48 |
48 if (type == LengthResolveType::kMinSize && length.isAuto()) | 49 if (type == LengthResolveType::kMinSize && length.isAuto()) |
49 return LayoutUnit(); | 50 return LayoutUnit(); |
50 | 51 |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 case DeviceHeight: | 176 case DeviceHeight: |
176 case ExtendToZoom: | 177 case ExtendToZoom: |
177 NOTREACHED() << "These should only be used for viewport definitions"; | 178 NOTREACHED() << "These should only be used for viewport definitions"; |
178 case MaxSizeNone: | 179 case MaxSizeNone: |
179 default: | 180 default: |
180 NOTREACHED(); | 181 NOTREACHED(); |
181 return border_and_padding.BlockSum(); | 182 return border_and_padding.BlockSum(); |
182 } | 183 } |
183 } | 184 } |
184 | 185 |
185 MinMaxContentSize ComputeMinAndMaxContentContribution( | 186 MinAndMaxContentSizes ComputeMinAndMaxContentContribution( |
186 const ComputedStyle& style, | 187 const ComputedStyle& style, |
187 const WTF::Optional<MinMaxContentSize>& min_and_max) { | 188 const WTF::Optional<MinAndMaxContentSizes>& min_and_max) { |
188 // Synthesize a zero-sized constraint space for passing to | 189 // Synthesize a zero-sized constraint space for passing to |
189 // ResolveInlineLength. | 190 // ResolveInlineLength. |
190 NGWritingMode writing_mode = FromPlatformWritingMode(style.getWritingMode()); | 191 NGWritingMode writing_mode = FromPlatformWritingMode(style.getWritingMode()); |
191 NGConstraintSpaceBuilder builder(writing_mode); | 192 NGConstraintSpaceBuilder builder(writing_mode); |
192 builder.SetInitialContainingBlockSize( | 193 builder.SetInitialContainingBlockSize( |
193 NGPhysicalSize{LayoutUnit(), LayoutUnit()}); | 194 NGPhysicalSize{LayoutUnit(), LayoutUnit()}); |
194 NGConstraintSpace* space = builder.ToConstraintSpace(writing_mode); | 195 NGConstraintSpace* space = builder.ToConstraintSpace(writing_mode); |
195 | 196 |
196 MinMaxContentSize computed_sizes; | 197 MinAndMaxContentSizes computed_sizes; |
197 Length inline_size = style.logicalWidth(); | 198 Length inline_size = style.logicalWidth(); |
198 if (inline_size.isAuto()) { | 199 if (inline_size.isAuto()) { |
199 CHECK(min_and_max.has_value()); | 200 CHECK(min_and_max.has_value()); |
200 NGBoxStrut border_and_padding = | 201 NGBoxStrut border_and_padding = |
201 ComputeBorders(*space, style) + ComputePadding(*space, style); | 202 ComputeBorders(*space, style) + ComputePadding(*space, style); |
202 computed_sizes.min_content = | 203 computed_sizes.min_content = |
203 min_and_max->min_content + border_and_padding.InlineSum(); | 204 min_and_max->min_content + border_and_padding.InlineSum(); |
204 computed_sizes.max_content = | 205 computed_sizes.max_content = |
205 min_and_max->max_content + border_and_padding.InlineSum(); | 206 min_and_max->max_content + border_and_padding.InlineSum(); |
206 } else { | 207 } else { |
(...skipping 19 matching lines...) Expand all Loading... |
226 NGBoxStrut margins = | 227 NGBoxStrut margins = |
227 ComputeMargins(*space, style, writing_mode, style.direction()); | 228 ComputeMargins(*space, style, writing_mode, style.direction()); |
228 computed_sizes.min_content += margins.InlineSum(); | 229 computed_sizes.min_content += margins.InlineSum(); |
229 computed_sizes.max_content += margins.InlineSum(); | 230 computed_sizes.max_content += margins.InlineSum(); |
230 return computed_sizes; | 231 return computed_sizes; |
231 } | 232 } |
232 | 233 |
233 LayoutUnit ComputeInlineSizeForFragment( | 234 LayoutUnit ComputeInlineSizeForFragment( |
234 const NGConstraintSpace& space, | 235 const NGConstraintSpace& space, |
235 const ComputedStyle& style, | 236 const ComputedStyle& style, |
236 const WTF::Optional<MinMaxContentSize>& min_and_max) { | 237 const WTF::Optional<MinAndMaxContentSizes>& min_and_max) { |
237 if (space.IsFixedSizeInline()) | 238 if (space.IsFixedSizeInline()) |
238 return space.AvailableSize().inline_size; | 239 return space.AvailableSize().inline_size; |
239 | 240 |
240 Length logicalWidth = style.logicalWidth(); | 241 Length logicalWidth = style.logicalWidth(); |
241 if (logicalWidth.isAuto() && space.IsShrinkToFit()) | 242 if (logicalWidth.isAuto() && space.IsShrinkToFit()) |
242 logicalWidth = Length(FitContent); | 243 logicalWidth = Length(FitContent); |
243 | 244 |
244 LayoutUnit extent = ResolveInlineLength( | 245 LayoutUnit extent = ResolveInlineLength( |
245 space, style, min_and_max, logicalWidth, LengthResolveType::kContentSize); | 246 space, style, min_and_max, logicalWidth, LengthResolveType::kContentSize); |
246 | 247 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 if (style.hasNormalColumnGap()) | 328 if (style.hasNormalColumnGap()) |
328 return LayoutUnit(style.getFontDescription().computedPixelSize()); | 329 return LayoutUnit(style.getFontDescription().computedPixelSize()); |
329 return LayoutUnit(style.columnGap()); | 330 return LayoutUnit(style.columnGap()); |
330 } | 331 } |
331 | 332 |
332 NGBoxStrut ComputeMargins(const NGConstraintSpace& constraint_space, | 333 NGBoxStrut ComputeMargins(const NGConstraintSpace& constraint_space, |
333 const ComputedStyle& style, | 334 const ComputedStyle& style, |
334 const NGWritingMode writing_mode, | 335 const NGWritingMode writing_mode, |
335 const TextDirection direction) { | 336 const TextDirection direction) { |
336 // We don't need these for margin computations | 337 // We don't need these for margin computations |
337 MinMaxContentSize empty_sizes; | 338 MinAndMaxContentSizes empty_sizes; |
338 // Margins always get computed relative to the inline size: | 339 // Margins always get computed relative to the inline size: |
339 // https://www.w3.org/TR/CSS2/box.html#value-def-margin-width | 340 // https://www.w3.org/TR/CSS2/box.html#value-def-margin-width |
340 NGPhysicalBoxStrut physical_dim; | 341 NGPhysicalBoxStrut physical_dim; |
341 physical_dim.left = ResolveInlineLength( | 342 physical_dim.left = ResolveInlineLength( |
342 constraint_space, style, empty_sizes, style.marginLeft(), | 343 constraint_space, style, empty_sizes, style.marginLeft(), |
343 LengthResolveType::kMarginBorderPaddingSize); | 344 LengthResolveType::kMarginBorderPaddingSize); |
344 physical_dim.right = ResolveInlineLength( | 345 physical_dim.right = ResolveInlineLength( |
345 constraint_space, style, empty_sizes, style.marginRight(), | 346 constraint_space, style, empty_sizes, style.marginRight(), |
346 LengthResolveType::kMarginBorderPaddingSize); | 347 LengthResolveType::kMarginBorderPaddingSize); |
347 physical_dim.top = ResolveInlineLength( | 348 physical_dim.top = ResolveInlineLength( |
(...skipping 21 matching lines...) Expand all Loading... |
369 } | 370 } |
370 | 371 |
371 NGBoxStrut ComputePadding(const NGConstraintSpace& constraint_space, | 372 NGBoxStrut ComputePadding(const NGConstraintSpace& constraint_space, |
372 const ComputedStyle& style) { | 373 const ComputedStyle& style) { |
373 // If we are producing an anonymous fragment (e.g. a column) we shouldn't | 374 // If we are producing an anonymous fragment (e.g. a column) we shouldn't |
374 // have any padding. | 375 // have any padding. |
375 if (constraint_space.IsAnonymous()) | 376 if (constraint_space.IsAnonymous()) |
376 return NGBoxStrut(); | 377 return NGBoxStrut(); |
377 | 378 |
378 // We don't need these for padding computations | 379 // We don't need these for padding computations |
379 MinMaxContentSize empty_sizes; | 380 MinAndMaxContentSizes empty_sizes; |
380 // Padding always gets computed relative to the inline size: | 381 // Padding always gets computed relative to the inline size: |
381 // https://www.w3.org/TR/CSS2/box.html#value-def-padding-width | 382 // https://www.w3.org/TR/CSS2/box.html#value-def-padding-width |
382 NGBoxStrut padding; | 383 NGBoxStrut padding; |
383 padding.inline_start = ResolveInlineLength( | 384 padding.inline_start = ResolveInlineLength( |
384 constraint_space, style, empty_sizes, style.paddingStart(), | 385 constraint_space, style, empty_sizes, style.paddingStart(), |
385 LengthResolveType::kMarginBorderPaddingSize); | 386 LengthResolveType::kMarginBorderPaddingSize); |
386 padding.inline_end = ResolveInlineLength( | 387 padding.inline_end = ResolveInlineLength( |
387 constraint_space, style, empty_sizes, style.paddingEnd(), | 388 constraint_space, style, empty_sizes, style.paddingEnd(), |
388 LengthResolveType::kMarginBorderPaddingSize); | 389 LengthResolveType::kMarginBorderPaddingSize); |
389 padding.block_start = ResolveInlineLength( | 390 padding.block_start = ResolveInlineLength( |
(...skipping 29 matching lines...) Expand all Loading... |
419 Optional<LayoutUnit> min, | 420 Optional<LayoutUnit> min, |
420 Optional<LayoutUnit> max) { | 421 Optional<LayoutUnit> max) { |
421 if (max && length > max.value()) | 422 if (max && length > max.value()) |
422 length = max.value(); | 423 length = max.value(); |
423 if (min && length < min.value()) | 424 if (min && length < min.value()) |
424 length = min.value(); | 425 length = min.value(); |
425 return length; | 426 return length; |
426 } | 427 } |
427 | 428 |
428 } // namespace blink | 429 } // namespace blink |
OLD | NEW |