summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/layout/ng/mathml/ng_math_under_over_layout_algorithm.cc
blob: 872196de21a3c8c18e60220557f425818273629d (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Copyright 2020 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/mathml/ng_math_under_over_layout_algorithm.h"

#include "third_party/blink/renderer/core/layout/ng/mathml/ng_math_layout_utils.h"
#include "third_party/blink/renderer/core/layout/ng/ng_block_break_token.h"
#include "third_party/blink/renderer/core/layout/ng/ng_box_fragment.h"
#include "third_party/blink/renderer/core/layout/ng/ng_length_utils.h"
#include "third_party/blink/renderer/core/layout/ng/ng_out_of_flow_layout_part.h"
#include "third_party/blink/renderer/core/layout/ng/ng_physical_box_fragment.h"
#include "third_party/blink/renderer/core/mathml/mathml_under_over_element.h"

namespace blink {
namespace {

// Describes the amount to shift to apply to the under/over boxes.
// Data is populated from the OpenType MATH table.
// If the OpenType MATH table is not present fallback values are used.
// https://mathml-refresh.github.io/mathml-core/#base-with-underscript
// https://mathml-refresh.github.io/mathml-core/#base-with-overscript
struct UnderOverVerticalParameters {
  bool use_under_over_bar_fallback;
  LayoutUnit under_gap_min;
  LayoutUnit over_gap_min;
  LayoutUnit under_shift_min;
  LayoutUnit over_shift_min;
  LayoutUnit under_extra_descender;
  LayoutUnit over_extra_ascender;
  LayoutUnit accent_base_height;
};

UnderOverVerticalParameters GetUnderOverVerticalParameters(
    const ComputedStyle& style) {
  UnderOverVerticalParameters parameters;

  if (!OpenTypeMathSupport::HasMathData(
          style.GetFont().PrimaryFont()->PlatformData().GetHarfBuzzFace())) {
    // The MATH table specification does not really provide any suggestions,
    // except for some underbar/overbar values and AccentBaseHeight.
    LayoutUnit default_line_thickness =
        LayoutUnit(RuleThicknessFallback(style));
    parameters.under_gap_min = 3 * default_line_thickness;
    parameters.over_gap_min = 3 * default_line_thickness;
    parameters.under_extra_descender = default_line_thickness;
    parameters.over_extra_ascender = default_line_thickness;
    parameters.accent_base_height =
        LayoutUnit(style.GetFont().PrimaryFont()->GetFontMetrics().XHeight());
    parameters.use_under_over_bar_fallback = true;
    return parameters;
  }

  // The base is a large operator so we read UpperLimit/LowerLimit constants
  // from the MATH table.
  parameters.under_gap_min = LayoutUnit(
      MathConstant(style, OpenTypeMathSupport::MathConstants::kLowerLimitGapMin)
          .value_or(0));
  parameters.over_gap_min = LayoutUnit(
      MathConstant(style, OpenTypeMathSupport::MathConstants::kUpperLimitGapMin)
          .value_or(0));
  parameters.under_shift_min = LayoutUnit(
      MathConstant(
          style, OpenTypeMathSupport::MathConstants::kLowerLimitBaselineDropMin)
          .value_or(0));
  parameters.over_shift_min = LayoutUnit(
      MathConstant(
          style, OpenTypeMathSupport::MathConstants::kUpperLimitBaselineRiseMin)
          .value_or(0));
  parameters.use_under_over_bar_fallback = false;
  return parameters;
}

}  // namespace

NGMathUnderOverLayoutAlgorithm::NGMathUnderOverLayoutAlgorithm(
    const NGLayoutAlgorithmParams& params)
    : NGLayoutAlgorithm(params) {
  DCHECK(params.space.IsNewFormattingContext());
  container_builder_.SetIsNewFormattingContext(
      params.space.IsNewFormattingContext());
  container_builder_.SetInitialFragmentGeometry(params.fragment_geometry);
}

void NGMathUnderOverLayoutAlgorithm::GatherChildren(NGBlockNode* base,
                                                    NGBlockNode* over,
                                                    NGBlockNode* under) {
  auto script_type = Node().ScriptType();
  for (NGLayoutInputNode child = Node().FirstChild(); child;
       child = child.NextSibling()) {
    NGBlockNode block_child = To<NGBlockNode>(child);
    if (child.IsOutOfFlowPositioned()) {
      container_builder_.AddOutOfFlowChildCandidate(
          block_child, BorderScrollbarPadding().StartOffset());
      continue;
    }
    if (!*base) {
      *base = block_child;
      continue;
    }
    switch (script_type) {
      case MathScriptType::kUnder:
        DCHECK(!*under);
        *under = block_child;
        break;
      case MathScriptType::kOver:
        DCHECK(!*over);
        *over = block_child;
        break;
      case MathScriptType::kUnderOver:
        if (!*under) {
          *under = block_child;
          continue;
        }
        DCHECK(!*over);
        *over = block_child;
        break;
      default:
        NOTREACHED();
    }
  }
}

scoped_refptr<const NGLayoutResult> NGMathUnderOverLayoutAlgorithm::Layout() {
  DCHECK(!BreakToken());
  DCHECK(IsValidMathMLScript(Node()));

  NGBlockNode base = nullptr;
  NGBlockNode over = nullptr;
  NGBlockNode under = nullptr;
  GatherChildren(&base, &over, &under);

  const LogicalSize border_box_size = container_builder_.InitialBorderBoxSize();

  const LogicalOffset content_start_offset =
      BorderScrollbarPadding().StartOffset();

  LayoutUnit block_offset = content_start_offset.block_offset;
  UnderOverVerticalParameters parameters =
      GetUnderOverVerticalParameters(Style());
  // TODO(rbuis): handle stretchy operators.
  // TODO(rbuis): handle accent.

  // All children are positioned centered relative to the container (and
  // therefore centered relative to themselves).
  if (over) {
    auto over_space = CreateConstraintSpaceForMathChild(
        Node(), ChildAvailableSize(), ConstraintSpace(), over);
    scoped_refptr<const NGLayoutResult> over_layout_result =
        over.Layout(over_space);
    NGBoxStrut over_margins =
        ComputeMarginsFor(over_space, over.Style(), ConstraintSpace());
    NGBoxFragment over_fragment(
        ConstraintSpace().GetWritingMode(), ConstraintSpace().Direction(),
        To<NGPhysicalBoxFragment>(over_layout_result->PhysicalFragment()));
    block_offset += parameters.over_extra_ascender + over_margins.block_start;
    LogicalOffset over_offset = {
        content_start_offset.inline_offset + over_margins.inline_start +
            (ChildAvailableSize().inline_size -
             (over_fragment.InlineSize() + over_margins.InlineSum())) /
                2,
        block_offset};
    container_builder_.AddChild(over_layout_result->PhysicalFragment(),
                                over_offset);
    over.StoreMargins(ConstraintSpace(), over_margins);
    if (parameters.use_under_over_bar_fallback) {
      block_offset += over_fragment.BlockSize();
      block_offset += parameters.over_gap_min;
    } else {
      LayoutUnit over_ascent =
          over_fragment.Baseline().value_or(over_fragment.BlockSize());
      block_offset +=
          std::max(over_fragment.BlockSize() + parameters.over_gap_min,
                   over_ascent + parameters.over_shift_min);
    }
    block_offset += over_margins.block_end;
  }

  auto base_space = CreateConstraintSpaceForMathChild(
      Node(), ChildAvailableSize(), ConstraintSpace(), base);
  auto base_layout_result = base.Layout(base_space);
  auto base_margins =
      ComputeMarginsFor(base_space, base.Style(), ConstraintSpace());

  NGBoxFragment base_fragment(
      ConstraintSpace().GetWritingMode(), ConstraintSpace().Direction(),
      To<NGPhysicalBoxFragment>(base_layout_result->PhysicalFragment()));

  block_offset += base_margins.block_start;
  LogicalOffset base_offset = {
      content_start_offset.inline_offset + base_margins.inline_start +
          (ChildAvailableSize().inline_size -
           (base_fragment.InlineSize() + base_margins.InlineSum())) /
              2,
      block_offset};
  container_builder_.AddChild(base_layout_result->PhysicalFragment(),
                              base_offset);
  base.StoreMargins(ConstraintSpace(), base_margins);
  block_offset += base_fragment.BlockSize() + base_margins.block_end;

  if (under) {
    auto under_space = CreateConstraintSpaceForMathChild(
        Node(), ChildAvailableSize(), ConstraintSpace(), under);
    scoped_refptr<const NGLayoutResult> under_layout_result =
        under.Layout(under_space);
    NGBoxStrut under_margins =
        ComputeMarginsFor(under_space, under.Style(), ConstraintSpace());
    NGBoxFragment under_fragment(
        ConstraintSpace().GetWritingMode(), ConstraintSpace().Direction(),
        To<NGPhysicalBoxFragment>(under_layout_result->PhysicalFragment()));
    block_offset += under_margins.block_start;
    if (parameters.use_under_over_bar_fallback) {
      block_offset += parameters.under_gap_min;
    } else {
      LayoutUnit under_ascent =
          under_fragment.Baseline().value_or(under_fragment.BlockSize());
      block_offset += std::max(parameters.under_gap_min,
                               parameters.under_shift_min - under_ascent);
    }
    LogicalOffset under_offset = {
        content_start_offset.inline_offset + under_margins.inline_start +
            (ChildAvailableSize().inline_size -
             (under_fragment.InlineSize() + under_margins.InlineSum())) /
                2,
        block_offset};
    block_offset += under_fragment.BlockSize();
    block_offset += parameters.under_extra_descender;
    container_builder_.AddChild(under_layout_result->PhysicalFragment(),
                                under_offset);
    under.StoreMargins(ConstraintSpace(), under_margins);
    block_offset += under_margins.block_end;
  }

  LayoutUnit base_ascent =
      base_fragment.Baseline().value_or(base_fragment.BlockSize());
  container_builder_.SetBaseline(base_offset.block_offset + base_ascent);

  block_offset += BorderScrollbarPadding().block_end;

  LayoutUnit block_size =
      ComputeBlockSizeForFragment(ConstraintSpace(), Style(), BorderPadding(),
                                  block_offset, border_box_size.inline_size);

  container_builder_.SetIntrinsicBlockSize(block_offset);
  container_builder_.SetFragmentsTotalBlockSize(block_size);

  NGOutOfFlowLayoutPart(Node(), ConstraintSpace(), container_builder_.Borders(),
                        &container_builder_)
      .Run();

  return container_builder_.ToBoxFragment();
}

MinMaxSizesResult NGMathUnderOverLayoutAlgorithm::ComputeMinMaxSizes(
    const MinMaxSizesInput& child_input) const {
  DCHECK(IsValidMathMLScript(Node()));

  if (auto result = CalculateMinMaxSizesIgnoringChildren(
          Node(), BorderScrollbarPadding()))
    return *result;

  MinMaxSizes sizes;
  bool depends_on_percentage_block_size = false;

  for (NGLayoutInputNode child = Node().FirstChild(); child;
       child = child.NextSibling()) {
    if (child.IsOutOfFlowPositioned())
      continue;
    auto child_result =
        ComputeMinAndMaxContentContribution(Style(), child, child_input);
    NGBoxStrut margins = ComputeMinMaxMargins(Style(), child);
    child_result.sizes += margins.InlineSum();

    sizes.Encompass(child_result.sizes);
    depends_on_percentage_block_size |=
        child_result.depends_on_percentage_block_size;
  }

  sizes += BorderScrollbarPadding().InlineSum();
  return {sizes, depends_on_percentage_block_size};
}

}  // namespace blink