summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/layout/ng/mathml/ng_math_fraction_layout_algorithm.cc
blob: 9deb2e3c49f7528496df4f6b195b3575802b0d0f (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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// 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_fraction_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_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/platform/fonts/opentype/open_type_math_support.h"

namespace blink {
namespace {

// Describes the amount to shift the numerator/denominator of the fraction when
// a fraction bar is present. 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/#fraction-with-nonzero-line-thickness
struct FractionParameters {
  LayoutUnit numerator_gap_min;
  LayoutUnit denominator_gap_min;
  LayoutUnit numerator_min_shift_up;
  LayoutUnit denominator_min_shift_down;
};

FractionParameters GetFractionParameters(const ComputedStyle& style) {
  FractionParameters parameters;

  bool has_display_style = HasDisplayStyle(style);

  // We try and read constants to draw the fraction from the OpenType MATH and
  // use fallback values otherwise.
  // The MATH table specification suggests default rule thickness or (in
  // displaystyle) 3 times default rule thickness for the gaps.
  parameters.numerator_gap_min = LayoutUnit(
      MathConstant(
          style,
          has_display_style
              ? OpenTypeMathSupport::MathConstants::
                    kFractionNumDisplayStyleGapMin
              : OpenTypeMathSupport::MathConstants::kFractionNumeratorGapMin)
          .value_or((has_display_style ? 3 : 1) *
                    RuleThicknessFallback(style)));
  parameters.denominator_gap_min = LayoutUnit(
      MathConstant(
          style,
          has_display_style
              ? OpenTypeMathSupport::MathConstants::
                    kFractionDenomDisplayStyleGapMin
              : OpenTypeMathSupport::MathConstants::kFractionDenominatorGapMin)
          .value_or(parameters.numerator_gap_min));

  // TODO(crbug.com/1058369): The MATH table specification does not suggest
  // any values for shifts, so we leave them at zero for now.
  parameters.numerator_min_shift_up = LayoutUnit(
      MathConstant(
          style,
          has_display_style
              ? OpenTypeMathSupport::MathConstants::
                    kFractionNumeratorDisplayStyleShiftUp
              : OpenTypeMathSupport::MathConstants::kFractionNumeratorShiftUp)
          .value_or(0));
  parameters.denominator_min_shift_down = LayoutUnit(
      MathConstant(style, has_display_style
                              ? OpenTypeMathSupport::MathConstants::
                                    kFractionDenominatorDisplayStyleShiftDown
                              : OpenTypeMathSupport::MathConstants::
                                    kFractionDenominatorShiftDown)
          .value_or(0));

  return parameters;
}

// Describes the amount to shift the numerator/denominator of the fraction when
// a fraction bar is not present. 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/#fraction-with-zero-line-thickness
struct FractionStackParameters {
  LayoutUnit gap_min;
  LayoutUnit top_shift_up;
  LayoutUnit bottom_shift_down;
};

FractionStackParameters GetFractionStackParameters(const ComputedStyle& style) {
  FractionStackParameters parameters;

  bool has_display_style = HasDisplayStyle(style);

  // We try and read constants to draw the stack from the OpenType MATH and use
  // fallback values otherwise.
  // We use the fallback values suggested in the MATH table specification.
  parameters.gap_min = LayoutUnit(
      MathConstant(
          style,
          has_display_style
              ? OpenTypeMathSupport::MathConstants::kStackDisplayStyleGapMin
              : OpenTypeMathSupport::MathConstants::kStackGapMin)
          .value_or((has_display_style ? 7 : 3) *
                    RuleThicknessFallback(style)));
  // The MATH table specification does not suggest any values for shifts, so
  // we leave them at zero.
  parameters.top_shift_up = LayoutUnit(
      MathConstant(
          style,
          has_display_style
              ? OpenTypeMathSupport::MathConstants::kStackTopDisplayStyleShiftUp
              : OpenTypeMathSupport::MathConstants::kStackTopShiftUp)
          .value_or(0));
  parameters.bottom_shift_down = LayoutUnit(
      MathConstant(
          style,
          has_display_style
              ? OpenTypeMathSupport::MathConstants::
                    kStackBottomDisplayStyleShiftDown
              : OpenTypeMathSupport::MathConstants::kStackBottomShiftDown)
          .value_or(0));

  return parameters;
}

}  // namespace

NGMathFractionLayoutAlgorithm::NGMathFractionLayoutAlgorithm(
    const NGLayoutAlgorithmParams& params)
    : NGLayoutAlgorithm(params),
      border_scrollbar_padding_(params.fragment_geometry.border +
                                params.fragment_geometry.padding +
                                params.fragment_geometry.scrollbar) {
  DCHECK(params.space.IsNewFormattingContext());
  container_builder_.SetIsNewFormattingContext(
      params.space.IsNewFormattingContext());
  container_builder_.SetInitialFragmentGeometry(params.fragment_geometry);
  container_builder_.SetIsMathMLFraction();
}

void NGMathFractionLayoutAlgorithm::GatherChildren(NGBlockNode* numerator,
                                                   NGBlockNode* denominator) {
  for (NGLayoutInputNode child = Node().FirstChild(); child;
       child = child.NextSibling()) {
    NGBlockNode block_child = To<NGBlockNode>(child);
    if (child.IsOutOfFlowPositioned()) {
      container_builder_.AddOutOfFlowChildCandidate(
          block_child, {border_scrollbar_padding_.inline_start,
                        border_scrollbar_padding_.block_start});
      continue;
    }
    if (!*numerator) {
      *numerator = block_child;
      continue;
    }
    if (!*denominator) {
      *denominator = block_child;
      continue;
    }

    NOTREACHED();
  }

  DCHECK(*numerator);
  DCHECK(*denominator);
}

scoped_refptr<const NGLayoutResult> NGMathFractionLayoutAlgorithm::Layout() {
  DCHECK(!BreakToken());

  NGBlockNode numerator = nullptr;
  NGBlockNode denominator = nullptr;
  GatherChildren(&numerator, &denominator);

  const LogicalSize border_box_size = container_builder_.InitialBorderBoxSize();
  auto child_available_size =
      ShrinkAvailableSize(border_box_size, border_scrollbar_padding_);
  auto numerator_space = CreateConstraintSpaceForMathChild(
      Node(), child_available_size, ConstraintSpace(), numerator);
  scoped_refptr<const NGLayoutResult> numerator_layout_result =
      numerator.Layout(numerator_space);
  auto numerator_margins =
      ComputeMarginsFor(numerator_space, numerator.Style(), ConstraintSpace());
  auto denominator_space = CreateConstraintSpaceForMathChild(
      Node(), child_available_size, ConstraintSpace(), denominator);
  scoped_refptr<const NGLayoutResult> denominator_layout_result =
      denominator.Layout(denominator_space);
  auto denominator_margins = ComputeMarginsFor(
      denominator_space, denominator.Style(), ConstraintSpace());

  NGBoxFragment numerator_fragment(
      ConstraintSpace().GetWritingMode(), ConstraintSpace().Direction(),
      To<NGPhysicalBoxFragment>(numerator_layout_result->PhysicalFragment()));
  NGBoxFragment denominator_fragment(
      ConstraintSpace().GetWritingMode(), ConstraintSpace().Direction(),
      To<NGPhysicalBoxFragment>(denominator_layout_result->PhysicalFragment()));

  LayoutUnit content_inline_size = std::max(
      numerator_fragment.InlineSize() + numerator_margins.InlineSum(),
      denominator_fragment.InlineSize() + denominator_margins.InlineSum());

  LayoutUnit numerator_ascent =
      numerator_margins.block_start +
      numerator_fragment.Baseline().value_or(numerator_fragment.BlockSize());
  LayoutUnit numerator_descent = numerator_fragment.BlockSize() +
                                 numerator_margins.BlockSum() -
                                 numerator_ascent;
  LayoutUnit denominator_ascent = denominator_margins.block_start +
                                  denominator_fragment.Baseline().value_or(
                                      denominator_fragment.BlockSize());
  LayoutUnit denominator_descent = denominator_fragment.BlockSize() +
                                   denominator_margins.BlockSum() -
                                   denominator_ascent;

  LayoutUnit numerator_shift, denominator_shift;
  LayoutUnit thickness = FractionLineThickness(Style());
  if (thickness) {
    LayoutUnit axis_height = MathAxisHeight(Style());
    FractionParameters parameters = GetFractionParameters(Style());
    numerator_shift =
        std::max(parameters.numerator_min_shift_up,
                 axis_height + thickness / 2 + parameters.numerator_gap_min +
                     numerator_descent);
    denominator_shift =
        std::max(parameters.denominator_min_shift_down,
                 thickness / 2 + parameters.denominator_gap_min +
                     denominator_ascent - axis_height);
  } else {
    FractionStackParameters parameters = GetFractionStackParameters(Style());
    numerator_shift = parameters.top_shift_up;
    denominator_shift = parameters.bottom_shift_down;
    LayoutUnit gap = denominator_shift - denominator_ascent + numerator_shift -
                     numerator_descent;
    if (gap < parameters.gap_min) {
      LayoutUnit diff = parameters.gap_min - gap;
      LayoutUnit delta = diff / 2;
      numerator_shift += delta;
      denominator_shift += diff - delta;
    }
  }

  LayoutUnit fraction_ascent =
      std::max(numerator_shift + numerator_ascent,
               -denominator_shift + denominator_ascent);
  LayoutUnit fraction_descent =
      std::max(-numerator_shift + numerator_descent,
               denominator_shift + denominator_descent);
  fraction_ascent += border_scrollbar_padding_.block_start;
  fraction_descent += border_scrollbar_padding_.block_end;
  LayoutUnit total_block_size = fraction_ascent + fraction_descent;

  container_builder_.SetBaseline(fraction_ascent);

  LogicalOffset numerator_offset;
  LogicalOffset denominator_offset;
  numerator_offset.inline_offset =
      border_scrollbar_padding_.inline_start + numerator_margins.inline_start +
      (content_inline_size -
       (numerator_fragment.InlineSize() + numerator_margins.InlineSum())) /
          2;
  denominator_offset.inline_offset =
      border_scrollbar_padding_.inline_start +
      denominator_margins.inline_start +
      (content_inline_size -
       (denominator_fragment.InlineSize() + denominator_margins.InlineSum())) /
          2;

  numerator_offset.block_offset = numerator_margins.block_start +
                                  fraction_ascent - numerator_shift -
                                  numerator_ascent;
  denominator_offset.block_offset = denominator_margins.block_start +
                                    fraction_ascent + denominator_shift -
                                    denominator_ascent;

  container_builder_.AddChild(numerator_layout_result->PhysicalFragment(),
                              numerator_offset);
  container_builder_.AddChild(denominator_layout_result->PhysicalFragment(),
                              denominator_offset);

  numerator.StoreMargins(ConstraintSpace(), numerator_margins);
  denominator.StoreMargins(ConstraintSpace(), denominator_margins);

  LayoutUnit block_size = ComputeBlockSizeForFragment(
      ConstraintSpace(), Style(), border_scrollbar_padding_, total_block_size);

  container_builder_.SetIntrinsicBlockSize(total_block_size);
  container_builder_.SetBlockSize(block_size);

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

  return container_builder_.ToBoxFragment();
}

base::Optional<MinMaxSizes> NGMathFractionLayoutAlgorithm::ComputeMinMaxSizes(
    const MinMaxSizesInput& input) const {
  base::Optional<MinMaxSizes> sizes =
      CalculateMinMaxSizesIgnoringChildren(Node(), border_scrollbar_padding_);
  if (sizes)
    return sizes;

  sizes.emplace();
  LayoutUnit child_percentage_resolution_block_size =
      CalculateChildPercentageBlockSizeForMinMax(
          ConstraintSpace(), Node(), border_scrollbar_padding_,
          input.percentage_resolution_block_size);

  MinMaxSizesInput child_input(child_percentage_resolution_block_size);

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

  *sizes += border_scrollbar_padding_.InlineSum();
  return sizes;
}

}  // namespace blink