summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/layout/ng/ng_space_utils.cc
blob: 3ae73180868dfdb8ad1311e558d5f6f195a43501 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/core/layout/ng/ng_space_utils.h"

#include "third_party/blink/renderer/core/layout/ng/geometry/ng_bfc_offset.h"
#include "third_party/blink/renderer/core/layout/ng/ng_constraint_space.h"
#include "third_party/blink/renderer/core/layout/ng/ng_constraint_space_builder.h"
#include "third_party/blink/renderer/core/layout/ng/ng_length_utils.h"
#include "third_party/blink/renderer/core/style/computed_style.h"
#include "third_party/blink/renderer/platform/text/writing_mode.h"

namespace blink {

bool AdjustToClearance(LayoutUnit clearance_offset, NGBfcOffset* offset) {
  DCHECK(offset);
  if (clearance_offset > offset->block_offset) {
    offset->block_offset = clearance_offset;
    return true;
  }

  return false;
}

NGConstraintSpace CreateIndefiniteConstraintSpaceForChild(
    const ComputedStyle& container_style,
    NGLayoutInputNode child) {
  WritingMode parent_writing_mode = container_style.GetWritingMode();
  WritingMode child_writing_mode = child.Style().GetWritingMode();
  DCHECK(!IsParallelWritingMode(parent_writing_mode, child_writing_mode));

  LogicalSize indefinite_size(kIndefiniteSize, kIndefiniteSize);
  NGConstraintSpaceBuilder builder(parent_writing_mode, child_writing_mode,
                                   child.CreatesNewFormattingContext());
  SetOrthogonalFallbackInlineSizeIfNeeded(container_style, child, &builder);

  builder.SetAvailableSize(indefinite_size);
  builder.SetPercentageResolutionSize(indefinite_size);
  builder.SetReplacedPercentageResolutionSize(indefinite_size);
  return builder.ToConstraintSpace();
}

void SetOrthogonalFallbackInlineSizeIfNeeded(
    const ComputedStyle& parent_style,
    const NGLayoutInputNode child,
    NGConstraintSpaceBuilder* builder) {
  if (IsParallelWritingMode(parent_style.GetWritingMode(),
                            child.Style().GetWritingMode()))
    return;

  PhysicalSize orthogonal_children_containing_block_size =
      child.InitialContainingBlockSize();

  LayoutUnit fallback_size;
  if (IsHorizontalWritingMode(parent_style.GetWritingMode()))
    fallback_size = orthogonal_children_containing_block_size.height;
  else
    fallback_size = orthogonal_children_containing_block_size.width;

  LayoutUnit size(LayoutUnit::Max());
  if (parent_style.LogicalHeight().IsFixed()) {
    // Note that during layout, fixed size will already be taken care of (and
    // set in the constraint space), but when calculating intrinsic sizes of
    // orthogonal children, that won't be the case.
    size = LayoutUnit(parent_style.LogicalHeight().GetFloatValue());
  }
  if (parent_style.LogicalMaxHeight().IsFixed()) {
    size = std::min(
        size, LayoutUnit(parent_style.LogicalMaxHeight().GetFloatValue()));
  }
  if (parent_style.LogicalMinHeight().IsFixed()) {
    size = std::max(
        size, LayoutUnit(parent_style.LogicalMinHeight().GetFloatValue()));
  }
  // Calculate the content-box size.
  if (parent_style.BoxSizing() == EBoxSizing::kBorderBox) {
    // We're unable to resolve percentages at this point, so make sure we're
    // only dealing with fixed-size values.
    if (!parent_style.PaddingBefore().IsFixed() ||
        !parent_style.PaddingAfter().IsFixed()) {
      builder->SetOrthogonalFallbackInlineSize(fallback_size);
      return;
    }

    LayoutUnit border_padding(parent_style.BorderBefore().Width() +
                              parent_style.BorderAfter().Width() +
                              parent_style.PaddingBefore().GetFloatValue() +
                              parent_style.PaddingAfter().GetFloatValue());

    size -= border_padding;
    size = size.ClampNegativeToZero();
  }

  fallback_size = std::min(fallback_size, size);
  builder->SetOrthogonalFallbackInlineSize(fallback_size);
}

}  // namespace blink