summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/layout/ng/mathml/ng_math_radical_layout_algorithm.cc
blob: 3a5cfd1e7d99127c7c035cb3dc407c401cdca573 (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
// 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_radical_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/platform/fonts/shaping/stretchy_operator_shaper.h"

namespace blink {

namespace {

bool HasBaseGlyphForRadical(const ComputedStyle& style) {
  const SimpleFontData* font_data = style.GetFont().PrimaryFont();
  return font_data && font_data->GlyphForCharacter(kSquareRootCharacter);
}

}  // namespace

NGMathRadicalLayoutAlgorithm::NGMathRadicalLayoutAlgorithm(
    const NGLayoutAlgorithmParams& params)
    : NGLayoutAlgorithm(params) {
  DCHECK(params.space.IsNewFormattingContext());
}

void NGMathRadicalLayoutAlgorithm::GatherChildren(
    NGBlockNode* base,
    NGBlockNode* index,
    NGBoxFragmentBuilder* container_builder) const {
  for (NGLayoutInputNode child = Node().FirstChild(); child;
       child = child.NextSibling()) {
    NGBlockNode block_child = To<NGBlockNode>(child);
    if (child.IsOutOfFlowPositioned()) {
      if (container_builder) {
        container_builder->AddOutOfFlowChildCandidate(
            block_child, BorderScrollbarPadding().StartOffset());
      }
      continue;
    }
    if (!*base) {
      *base = block_child;
      continue;
    }
    if (!*index) {
      *index = block_child;
      continue;
    }

    NOTREACHED();
  }

  if (Node().HasIndex()) {
    DCHECK(*base);
    DCHECK(*index);
  }
}

scoped_refptr<const NGLayoutResult> NGMathRadicalLayoutAlgorithm::Layout() {
  DCHECK(!BreakToken());
  DCHECK(IsValidMathMLRadical(Node()));

  auto vertical = GetRadicalVerticalParameters(Style(), Node().HasIndex());
  scoped_refptr<const NGPhysicalBoxFragment> index_fragment, base_fragment;
  LayoutUnit index_inline_size, index_ascent, index_descent, base_ascent,
      base_descent;
  RadicalHorizontalParameters horizontal;
  NGBoxStrut index_margins, base_margins;
  NGBlockNode base = nullptr;
  NGBlockNode index = nullptr;
  GatherChildren(&base, &index, &container_builder_);

  if (base) {
    // Handle layout of base child. For <msqrt> the base is anonymous and uses
    // the row layout algorithm.
    NGConstraintSpace constraint_space = CreateConstraintSpaceForMathChild(
        Node(), ChildAvailableSize(), ConstraintSpace(), base);
    scoped_refptr<const NGLayoutResult> base_layout_result =
        base.Layout(constraint_space);
    base_fragment =
        &To<NGPhysicalBoxFragment>(base_layout_result->PhysicalFragment());
    base_margins =
        ComputeMarginsFor(constraint_space, base.Style(), ConstraintSpace());
    NGBoxFragment fragment(ConstraintSpace().GetWritingDirection(),
                           *base_fragment);
    base_ascent = base_margins.block_start + fragment.BaselineOrSynthesize();
    base_descent = fragment.BlockSize() + base_margins.BlockSum() - base_ascent;
  }
  if (index) {
    // Handle layout of index child.
    // (https://mathml-refresh.github.io/mathml-core/#root-with-index).
    NGConstraintSpace constraint_space = CreateConstraintSpaceForMathChild(
        Node(), ChildAvailableSize(), ConstraintSpace(), index);
    scoped_refptr<const NGLayoutResult> index_layout_result =
        index.Layout(constraint_space);
    index_fragment =
        &To<NGPhysicalBoxFragment>(index_layout_result->PhysicalFragment());
    index_margins =
        ComputeMarginsFor(constraint_space, index.Style(), ConstraintSpace());
    NGBoxFragment fragment(ConstraintSpace().GetWritingDirection(),
                           *index_fragment);
    index_inline_size = fragment.InlineSize() + index_margins.InlineSum();
    index_ascent = index_margins.block_start + fragment.BaselineOrSynthesize();
    index_descent =
        fragment.BlockSize() + index_margins.BlockSum() - index_ascent;
    horizontal = GetRadicalHorizontalParameters(Style());
    horizontal.kern_before_degree =
        std::max(horizontal.kern_before_degree, LayoutUnit());
    horizontal.kern_after_degree =
        std::max(horizontal.kern_after_degree, -index_inline_size);
  }

  StretchyOperatorShaper::Metrics surd_metrics;
  if (HasBaseGlyphForRadical(Style())) {
    // Stretch the radical operator to cover the base height.
    StretchyOperatorShaper shaper(kSquareRootCharacter,
                                  OpenTypeMathStretchData::Vertical);
    float target_size = base_ascent + base_descent + vertical.vertical_gap +
                        vertical.rule_thickness;
    scoped_refptr<ShapeResult> shape_result =
        shaper.Shape(&Style().GetFont(), target_size, &surd_metrics);
    scoped_refptr<ShapeResultView> shape_result_view =
        ShapeResultView::Create(shape_result.get());
    LayoutUnit operator_inline_offset = index_inline_size +
                                        horizontal.kern_before_degree +
                                        horizontal.kern_after_degree;
    container_builder_.SetMathMLPaintInfo(
        std::move(shape_result_view), LayoutUnit(surd_metrics.advance),
        LayoutUnit(surd_metrics.ascent), LayoutUnit(surd_metrics.descent),
        operator_inline_offset, base_margins);
  }

  // Determine the metrics of the radical operator + the base.
  LayoutUnit radical_operator_block_size =
      LayoutUnit(surd_metrics.ascent + surd_metrics.descent);

  LayoutUnit index_bottom_raise =
      LayoutUnit(vertical.degree_bottom_raise_percent) *
      radical_operator_block_size;
  LayoutUnit radical_ascent = base_ascent + vertical.vertical_gap +
                              vertical.rule_thickness + vertical.extra_ascender;
  LayoutUnit ascent = radical_ascent;
  LayoutUnit descent =
      std::max(base_descent,
               radical_operator_block_size + vertical.extra_ascender - ascent);
  if (index) {
    ascent = std::max(
        ascent, -descent + index_bottom_raise + index_descent + index_ascent);
    descent = std::max(
        descent, descent - index_bottom_raise + index_descent + index_ascent);
  }
  ascent += BorderScrollbarPadding().block_start;

  if (base) {
    LogicalOffset base_offset = {
        BorderScrollbarPadding().inline_start +
            LayoutUnit(surd_metrics.advance) + index_inline_size +
            horizontal.kern_before_degree + horizontal.kern_after_degree +
            base_margins.inline_start,
        base_margins.block_start - base_ascent + ascent};
    container_builder_.AddChild(*base_fragment, base_offset);
    base.StoreMargins(ConstraintSpace(), base_margins);
  }
  if (index) {
    LogicalOffset index_offset = {
        BorderScrollbarPadding().inline_start + index_margins.inline_start +
            horizontal.kern_before_degree,
        index_margins.block_start + ascent + descent - index_bottom_raise -
            index_descent - index_ascent};
    container_builder_.AddChild(*index_fragment, index_offset);
    index.StoreMargins(ConstraintSpace(), index_margins);
  }

  container_builder_.SetBaseline(ascent);

  auto total_block_size = ascent + descent + BorderScrollbarPadding().block_end;
  LayoutUnit block_size = ComputeBlockSizeForFragment(
      ConstraintSpace(), Style(), BorderPadding(), total_block_size,
      container_builder_.InitialBorderBoxSize().inline_size);

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

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

  return container_builder_.ToBoxFragment();
}

MinMaxSizesResult NGMathRadicalLayoutAlgorithm::ComputeMinMaxSizes(
    const MinMaxSizesFloatInput&) const {
  DCHECK(IsValidMathMLRadical(Node()));

  NGBlockNode base = nullptr;
  NGBlockNode index = nullptr;
  GatherChildren(&base, &index);

  MinMaxSizes sizes;
  bool depends_on_block_constraints = false;
  if (index) {
    const auto horizontal = GetRadicalHorizontalParameters(Style());
    sizes += horizontal.kern_before_degree.ClampNegativeToZero();

    const auto index_result = ComputeMinAndMaxContentContributionForMathChild(
        Style(), ConstraintSpace(), index, ChildAvailableSize().block_size);
    depends_on_block_constraints |= index_result.depends_on_block_constraints;
    sizes += index_result.sizes;

    // kern_after_degree decreases the inline size, but is capped by the index
    // content inline size.
    sizes.min_size +=
        std::max(-index_result.sizes.min_size, horizontal.kern_after_degree);
    sizes.max_size +=
        std::max(index_result.sizes.max_size, horizontal.kern_after_degree);
  }
  if (base) {
    if (HasBaseGlyphForRadical(Style())) {
      sizes += GetMinMaxSizesForVerticalStretchyOperator(Style(),
                                                         kSquareRootCharacter);
    }
    const auto base_result = ComputeMinAndMaxContentContributionForMathChild(
        Style(), ConstraintSpace(), base, ChildAvailableSize().block_size);
    depends_on_block_constraints |= base_result.depends_on_block_constraints;
    sizes += base_result.sizes;
  }

  sizes += BorderScrollbarPadding().InlineSum();
  return MinMaxSizesResult(sizes, depends_on_block_constraints);
}

}  // namespace blink