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