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 20 matching lines...) Expand all Loading... |
31 LayoutUnit ResolveInlineLength( | 31 LayoutUnit ResolveInlineLength( |
32 const NGConstraintSpace& constraint_space, | 32 const NGConstraintSpace& constraint_space, |
33 const ComputedStyle& style, | 33 const ComputedStyle& style, |
34 const WTF::Optional<MinAndMaxContentSizes>& min_and_max, | 34 const WTF::Optional<MinAndMaxContentSizes>& min_and_max, |
35 const Length& length, | 35 const Length& length, |
36 LengthResolveType type) { | 36 LengthResolveType type) { |
37 // TODO(layout-ng): Handle min/max/fit-content | 37 // TODO(layout-ng): Handle min/max/fit-content |
38 DCHECK(!length.isMaxSizeNone()); | 38 DCHECK(!length.isMaxSizeNone()); |
39 DCHECK_GE(constraint_space.AvailableSize().inline_size, LayoutUnit()); | 39 DCHECK_GE(constraint_space.AvailableSize().inline_size, LayoutUnit()); |
40 | 40 |
41 if (type == LengthResolveType::MinSize && length.isAuto()) | 41 if (type == LengthResolveType::kMinSize && length.isAuto()) |
42 return LayoutUnit(); | 42 return LayoutUnit(); |
43 | 43 |
44 if (type == LengthResolveType::MarginBorderPaddingSize && length.isAuto()) | 44 if (type == LengthResolveType::kMarginBorderPaddingSize && length.isAuto()) |
45 return LayoutUnit(); | 45 return LayoutUnit(); |
46 | 46 |
47 // We don't need this when we're resolving margin/border/padding; skip | 47 // We don't need this when we're resolving margin/border/padding; skip |
48 // computing it as an optimization and to simplify the code below. | 48 // computing it as an optimization and to simplify the code below. |
49 NGBoxStrut border_and_padding; | 49 NGBoxStrut border_and_padding; |
50 if (type != LengthResolveType::MarginBorderPaddingSize) { | 50 if (type != LengthResolveType::kMarginBorderPaddingSize) { |
51 border_and_padding = | 51 border_and_padding = |
52 ComputeBorders(style) + ComputePadding(constraint_space, style); | 52 ComputeBorders(style) + ComputePadding(constraint_space, style); |
53 } | 53 } |
54 switch (length.type()) { | 54 switch (length.type()) { |
55 case Auto: | 55 case Auto: |
56 case FillAvailable: { | 56 case FillAvailable: { |
57 LayoutUnit content_size = constraint_space.AvailableSize().inline_size; | 57 LayoutUnit content_size = constraint_space.AvailableSize().inline_size; |
58 NGBoxStrut margins = ComputeMargins( | 58 NGBoxStrut margins = ComputeMargins( |
59 constraint_space, style, | 59 constraint_space, style, |
60 FromPlatformWritingMode(style.getWritingMode()), style.direction()); | 60 FromPlatformWritingMode(style.getWritingMode()), style.direction()); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 return border_and_padding.InlineSum(); | 107 return border_and_padding.InlineSum(); |
108 } | 108 } |
109 } | 109 } |
110 | 110 |
111 LayoutUnit ResolveBlockLength(const NGConstraintSpace& constraint_space, | 111 LayoutUnit ResolveBlockLength(const NGConstraintSpace& constraint_space, |
112 const ComputedStyle& style, | 112 const ComputedStyle& style, |
113 const Length& length, | 113 const Length& length, |
114 LayoutUnit content_size, | 114 LayoutUnit content_size, |
115 LengthResolveType type) { | 115 LengthResolveType type) { |
116 DCHECK(!length.isMaxSizeNone()); | 116 DCHECK(!length.isMaxSizeNone()); |
117 DCHECK(type != LengthResolveType::MarginBorderPaddingSize); | 117 DCHECK(type != LengthResolveType::kMarginBorderPaddingSize); |
118 | 118 |
119 if (type == LengthResolveType::MinSize && length.isAuto()) | 119 if (type == LengthResolveType::kMinSize && length.isAuto()) |
120 return LayoutUnit(); | 120 return LayoutUnit(); |
121 | 121 |
122 // Make sure that indefinite percentages resolve to NGSizeIndefinite, not to | 122 // Make sure that indefinite percentages resolve to NGSizeIndefinite, not to |
123 // a random negative number. | 123 // a random negative number. |
124 if (length.isPercentOrCalc() && | 124 if (length.isPercentOrCalc() && |
125 constraint_space.PercentageResolutionSize().block_size == | 125 constraint_space.PercentageResolutionSize().block_size == |
126 NGSizeIndefinite) | 126 NGSizeIndefinite) |
127 return content_size; | 127 return content_size; |
128 | 128 |
129 // We don't need this when we're resolving margin/border/padding; skip | 129 // We don't need this when we're resolving margin/border/padding; skip |
130 // computing it as an optimization and to simplify the code below. | 130 // computing it as an optimization and to simplify the code below. |
131 NGBoxStrut border_and_padding; | 131 NGBoxStrut border_and_padding; |
132 if (type != LengthResolveType::MarginBorderPaddingSize) { | 132 if (type != LengthResolveType::kMarginBorderPaddingSize) { |
133 border_and_padding = | 133 border_and_padding = |
134 ComputeBorders(style) + ComputePadding(constraint_space, style); | 134 ComputeBorders(style) + ComputePadding(constraint_space, style); |
135 } | 135 } |
136 switch (length.type()) { | 136 switch (length.type()) { |
137 case FillAvailable: { | 137 case FillAvailable: { |
138 LayoutUnit content_size = constraint_space.AvailableSize().block_size; | 138 LayoutUnit content_size = constraint_space.AvailableSize().block_size; |
139 NGBoxStrut margins = ComputeMargins( | 139 NGBoxStrut margins = ComputeMargins( |
140 constraint_space, style, | 140 constraint_space, style, |
141 FromPlatformWritingMode(style.getWritingMode()), style.direction()); | 141 FromPlatformWritingMode(style.getWritingMode()), style.direction()); |
142 return std::max(border_and_padding.BlockSum(), | 142 return std::max(border_and_padding.BlockSum(), |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 } | 175 } |
176 } | 176 } |
177 | 177 |
178 LayoutUnit ComputeInlineSizeForFragment( | 178 LayoutUnit ComputeInlineSizeForFragment( |
179 const NGConstraintSpace& constraint_space, | 179 const NGConstraintSpace& constraint_space, |
180 const ComputedStyle& style, | 180 const ComputedStyle& style, |
181 const WTF::Optional<MinAndMaxContentSizes>& min_and_max) { | 181 const WTF::Optional<MinAndMaxContentSizes>& min_and_max) { |
182 if (constraint_space.FixedInlineSize()) | 182 if (constraint_space.FixedInlineSize()) |
183 return constraint_space.AvailableSize().inline_size; | 183 return constraint_space.AvailableSize().inline_size; |
184 | 184 |
185 LayoutUnit extent = | 185 LayoutUnit extent = ResolveInlineLength(constraint_space, style, min_and_max, |
186 ResolveInlineLength(constraint_space, style, min_and_max, | 186 style.logicalWidth(), |
187 style.logicalWidth(), LengthResolveType::ContentSize); | 187 LengthResolveType::kContentSize); |
188 | 188 |
189 Length max_length = style.logicalMaxWidth(); | 189 Length max_length = style.logicalMaxWidth(); |
190 if (!max_length.isMaxSizeNone()) { | 190 if (!max_length.isMaxSizeNone()) { |
191 LayoutUnit max = | 191 LayoutUnit max = |
192 ResolveInlineLength(constraint_space, style, min_and_max, max_length, | 192 ResolveInlineLength(constraint_space, style, min_and_max, max_length, |
193 LengthResolveType::MaxSize); | 193 LengthResolveType::kMaxSize); |
194 extent = std::min(extent, max); | 194 extent = std::min(extent, max); |
195 } | 195 } |
196 | 196 |
197 LayoutUnit min = | 197 LayoutUnit min = |
198 ResolveInlineLength(constraint_space, style, min_and_max, | 198 ResolveInlineLength(constraint_space, style, min_and_max, |
199 style.logicalMinWidth(), LengthResolveType::MinSize); | 199 style.logicalMinWidth(), LengthResolveType::kMinSize); |
200 extent = std::max(extent, min); | 200 extent = std::max(extent, min); |
201 return extent; | 201 return extent; |
202 } | 202 } |
203 | 203 |
204 LayoutUnit ComputeBlockSizeForFragment( | 204 LayoutUnit ComputeBlockSizeForFragment( |
205 const NGConstraintSpace& constraint_space, | 205 const NGConstraintSpace& constraint_space, |
206 const ComputedStyle& style, | 206 const ComputedStyle& style, |
207 LayoutUnit content_size) { | 207 LayoutUnit content_size) { |
208 if (constraint_space.FixedBlockSize()) | 208 if (constraint_space.FixedBlockSize()) |
209 return constraint_space.AvailableSize().block_size; | 209 return constraint_space.AvailableSize().block_size; |
210 | 210 |
211 LayoutUnit extent = | 211 LayoutUnit extent = |
212 ResolveBlockLength(constraint_space, style, style.logicalHeight(), | 212 ResolveBlockLength(constraint_space, style, style.logicalHeight(), |
213 content_size, LengthResolveType::ContentSize); | 213 content_size, LengthResolveType::kContentSize); |
214 if (extent == NGSizeIndefinite) { | 214 if (extent == NGSizeIndefinite) { |
215 DCHECK_EQ(content_size, NGSizeIndefinite); | 215 DCHECK_EQ(content_size, NGSizeIndefinite); |
216 return extent; | 216 return extent; |
217 } | 217 } |
218 | 218 |
219 Length max_length = style.logicalMaxHeight(); | 219 Length max_length = style.logicalMaxHeight(); |
220 if (!max_length.isMaxSizeNone()) { | 220 if (!max_length.isMaxSizeNone()) { |
221 LayoutUnit max = | 221 LayoutUnit max = |
222 ResolveBlockLength(constraint_space, style, max_length, content_size, | 222 ResolveBlockLength(constraint_space, style, max_length, content_size, |
223 LengthResolveType::MaxSize); | 223 LengthResolveType::kMaxSize); |
224 extent = std::min(extent, max); | 224 extent = std::min(extent, max); |
225 } | 225 } |
226 | 226 |
227 LayoutUnit min = | 227 LayoutUnit min = |
228 ResolveBlockLength(constraint_space, style, style.logicalMinHeight(), | 228 ResolveBlockLength(constraint_space, style, style.logicalMinHeight(), |
229 content_size, LengthResolveType::MinSize); | 229 content_size, LengthResolveType::kMinSize); |
230 extent = std::max(extent, min); | 230 extent = std::max(extent, min); |
231 return extent; | 231 return extent; |
232 } | 232 } |
233 | 233 |
234 int ResolveUsedColumnCount(int computed_count, | 234 int ResolveUsedColumnCount(int computed_count, |
235 LayoutUnit computed_size, | 235 LayoutUnit computed_size, |
236 LayoutUnit used_gap, | 236 LayoutUnit used_gap, |
237 LayoutUnit available_size) { | 237 LayoutUnit available_size) { |
238 if (computed_size == NGSizeIndefinite) { | 238 if (computed_size == NGSizeIndefinite) { |
239 DCHECK(computed_count); | 239 DCHECK(computed_count); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 const ComputedStyle& style, | 282 const ComputedStyle& style, |
283 const NGWritingMode writing_mode, | 283 const NGWritingMode writing_mode, |
284 const TextDirection direction) { | 284 const TextDirection direction) { |
285 // We don't need these for margin computations | 285 // We don't need these for margin computations |
286 MinAndMaxContentSizes empty_sizes; | 286 MinAndMaxContentSizes empty_sizes; |
287 // Margins always get computed relative to the inline size: | 287 // Margins always get computed relative to the inline size: |
288 // https://www.w3.org/TR/CSS2/box.html#value-def-margin-width | 288 // https://www.w3.org/TR/CSS2/box.html#value-def-margin-width |
289 NGPhysicalBoxStrut physical_dim; | 289 NGPhysicalBoxStrut physical_dim; |
290 physical_dim.left = ResolveInlineLength( | 290 physical_dim.left = ResolveInlineLength( |
291 constraint_space, style, empty_sizes, style.marginLeft(), | 291 constraint_space, style, empty_sizes, style.marginLeft(), |
292 LengthResolveType::MarginBorderPaddingSize); | 292 LengthResolveType::kMarginBorderPaddingSize); |
293 physical_dim.right = ResolveInlineLength( | 293 physical_dim.right = ResolveInlineLength( |
294 constraint_space, style, empty_sizes, style.marginRight(), | 294 constraint_space, style, empty_sizes, style.marginRight(), |
295 LengthResolveType::MarginBorderPaddingSize); | 295 LengthResolveType::kMarginBorderPaddingSize); |
296 physical_dim.top = ResolveInlineLength( | 296 physical_dim.top = ResolveInlineLength( |
297 constraint_space, style, empty_sizes, style.marginTop(), | 297 constraint_space, style, empty_sizes, style.marginTop(), |
298 LengthResolveType::MarginBorderPaddingSize); | 298 LengthResolveType::kMarginBorderPaddingSize); |
299 physical_dim.bottom = ResolveInlineLength( | 299 physical_dim.bottom = ResolveInlineLength( |
300 constraint_space, style, empty_sizes, style.marginBottom(), | 300 constraint_space, style, empty_sizes, style.marginBottom(), |
301 LengthResolveType::MarginBorderPaddingSize); | 301 LengthResolveType::kMarginBorderPaddingSize); |
302 return physical_dim.ConvertToLogical(writing_mode, direction); | 302 return physical_dim.ConvertToLogical(writing_mode, direction); |
303 } | 303 } |
304 | 304 |
305 NGBoxStrut ComputeBorders(const ComputedStyle& style) { | 305 NGBoxStrut ComputeBorders(const ComputedStyle& style) { |
306 NGBoxStrut borders; | 306 NGBoxStrut borders; |
307 borders.inline_start = LayoutUnit(style.borderStartWidth()); | 307 borders.inline_start = LayoutUnit(style.borderStartWidth()); |
308 borders.inline_end = LayoutUnit(style.borderEndWidth()); | 308 borders.inline_end = LayoutUnit(style.borderEndWidth()); |
309 borders.block_start = LayoutUnit(style.borderBeforeWidth()); | 309 borders.block_start = LayoutUnit(style.borderBeforeWidth()); |
310 borders.block_end = LayoutUnit(style.borderAfterWidth()); | 310 borders.block_end = LayoutUnit(style.borderAfterWidth()); |
311 return borders; | 311 return borders; |
312 } | 312 } |
313 | 313 |
314 NGBoxStrut ComputePadding(const NGConstraintSpace& constraint_space, | 314 NGBoxStrut ComputePadding(const NGConstraintSpace& constraint_space, |
315 const ComputedStyle& style) { | 315 const ComputedStyle& style) { |
316 // We don't need these for padding computations | 316 // We don't need these for padding computations |
317 MinAndMaxContentSizes empty_sizes; | 317 MinAndMaxContentSizes empty_sizes; |
318 // Padding always gets computed relative to the inline size: | 318 // Padding always gets computed relative to the inline size: |
319 // https://www.w3.org/TR/CSS2/box.html#value-def-padding-width | 319 // https://www.w3.org/TR/CSS2/box.html#value-def-padding-width |
320 NGBoxStrut padding; | 320 NGBoxStrut padding; |
321 padding.inline_start = ResolveInlineLength( | 321 padding.inline_start = ResolveInlineLength( |
322 constraint_space, style, empty_sizes, style.paddingStart(), | 322 constraint_space, style, empty_sizes, style.paddingStart(), |
323 LengthResolveType::MarginBorderPaddingSize); | 323 LengthResolveType::kMarginBorderPaddingSize); |
324 padding.inline_end = ResolveInlineLength( | 324 padding.inline_end = ResolveInlineLength( |
325 constraint_space, style, empty_sizes, style.paddingEnd(), | 325 constraint_space, style, empty_sizes, style.paddingEnd(), |
326 LengthResolveType::MarginBorderPaddingSize); | 326 LengthResolveType::kMarginBorderPaddingSize); |
327 padding.block_start = ResolveInlineLength( | 327 padding.block_start = ResolveInlineLength( |
328 constraint_space, style, empty_sizes, style.paddingBefore(), | 328 constraint_space, style, empty_sizes, style.paddingBefore(), |
329 LengthResolveType::MarginBorderPaddingSize); | 329 LengthResolveType::kMarginBorderPaddingSize); |
330 padding.block_end = ResolveInlineLength( | 330 padding.block_end = ResolveInlineLength( |
331 constraint_space, style, empty_sizes, style.paddingAfter(), | 331 constraint_space, style, empty_sizes, style.paddingAfter(), |
332 LengthResolveType::MarginBorderPaddingSize); | 332 LengthResolveType::kMarginBorderPaddingSize); |
333 return padding; | 333 return padding; |
334 } | 334 } |
335 | 335 |
336 void ApplyAutoMargins(const NGConstraintSpace& constraint_space, | 336 void ApplyAutoMargins(const NGConstraintSpace& constraint_space, |
337 const ComputedStyle& style, | 337 const ComputedStyle& style, |
338 const NGFragmentBase& fragment, | 338 const NGFragmentBase& fragment, |
339 NGBoxStrut* margins) { | 339 NGBoxStrut* margins) { |
340 DCHECK(margins) << "Margins cannot be NULL here"; | 340 DCHECK(margins) << "Margins cannot be NULL here"; |
341 const LayoutUnit used_space = fragment.InlineSize() + margins->InlineSum(); | 341 const LayoutUnit used_space = fragment.InlineSize() + margins->InlineSum(); |
342 const LayoutUnit available_space = | 342 const LayoutUnit available_space = |
343 constraint_space.AvailableSize().inline_size - used_space; | 343 constraint_space.AvailableSize().inline_size - used_space; |
344 if (available_space < LayoutUnit()) | 344 if (available_space < LayoutUnit()) |
345 return; | 345 return; |
346 if (style.marginStart().isAuto() && style.marginEnd().isAuto()) { | 346 if (style.marginStart().isAuto() && style.marginEnd().isAuto()) { |
347 margins->inline_start = available_space / 2; | 347 margins->inline_start = available_space / 2; |
348 margins->inline_end = available_space - margins->inline_start; | 348 margins->inline_end = available_space - margins->inline_start; |
349 } else if (style.marginStart().isAuto()) { | 349 } else if (style.marginStart().isAuto()) { |
350 margins->inline_start = available_space; | 350 margins->inline_start = available_space; |
351 } else if (style.marginEnd().isAuto()) { | 351 } else if (style.marginEnd().isAuto()) { |
352 margins->inline_end = available_space; | 352 margins->inline_end = available_space; |
353 } | 353 } |
354 } | 354 } |
355 | 355 |
356 } // namespace blink | 356 } // namespace blink |
OLD | NEW |