-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
RenderMathMLUnderOver.cpp
370 lines (314 loc) · 15.4 KB
/
RenderMathMLUnderOver.cpp
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
* Copyright (C) 2009 Alex Milowski (alex@milowski.com). All rights reserved.
* Copyright (C) 2016 Igalia S.L.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "RenderMathMLUnderOver.h"
#if ENABLE(MATHML)
#include "MathMLElement.h"
#include "MathMLOperatorDictionary.h"
#include "MathMLUnderOverElement.h"
#include "RenderIterator.h"
#include "RenderMathMLOperator.h"
#include <wtf/IsoMallocInlines.h>
namespace WebCore {
WTF_MAKE_ISO_ALLOCATED_IMPL(RenderMathMLUnderOver);
RenderMathMLUnderOver::RenderMathMLUnderOver(MathMLUnderOverElement& element, RenderStyle&& style)
: RenderMathMLScripts(element, WTFMove(style))
{
}
MathMLUnderOverElement& RenderMathMLUnderOver::element() const
{
return static_cast<MathMLUnderOverElement&>(nodeForNonAnonymous());
}
static RenderMathMLOperator* horizontalStretchyOperator(const RenderBox& box)
{
if (!is<RenderMathMLBlock>(box))
return nullptr;
auto* renderOperator = downcast<RenderMathMLBlock>(box).unembellishedOperator();
if (!renderOperator)
return nullptr;
if (!renderOperator->isStretchy() || renderOperator->isVertical() || renderOperator->isStretchWidthLocked())
return nullptr;
return renderOperator;
}
static void fixLayoutAfterStretch(RenderBox& ancestor, RenderMathMLOperator& stretchyOperator)
{
stretchyOperator.setStretchWidthLocked(true);
stretchyOperator.setNeedsLayout();
ancestor.layoutIfNeeded();
stretchyOperator.setStretchWidthLocked(false);
}
void RenderMathMLUnderOver::stretchHorizontalOperatorsAndLayoutChildren()
{
ASSERT(isValid());
ASSERT(needsLayout());
// We apply horizontal stretchy rules from the MathML spec (sections 3.2.5.8.3 and 3.2.5.8.4), which
// can be roughly summarized as "stretching opersators to the maximum widths of all children" and
// minor variations of that algorithm do not affect the result. However, the spec is a bit ambiguous
// for embellished operators (section 3.2.5.7.3) and different approaches can lead to significant
// stretch size differences. We made the following decisions:
// - The unstretched size is the embellished operator width with the <mo> at the core unstretched.
// - In general, the target size is just the maximum widths of non-stretchy children because the
// embellishments could make widths significantly larger.
// - In the edge case when all operators of stretchy, we follow the specification and take the
// maximum of all unstretched sizes.
// - The <mo> at the core is stretched to cover the target size, even if the embellished operator
// might become much wider.
Vector<RenderBox*, 3> embellishedOperators;
Vector<RenderMathMLOperator*, 3> stretchyOperators;
bool isAllStretchyOperators = true;
LayoutUnit stretchWidth;
for (auto* child = firstChildBox(); child; child = child->nextSiblingBox()) {
if (auto* stretchyOperator = horizontalStretchyOperator(*child)) {
embellishedOperators.append(child);
stretchyOperators.append(stretchyOperator);
} else {
isAllStretchyOperators = false;
child->layoutIfNeeded();
stretchWidth = std::max(stretchWidth, child->logicalWidth());
}
}
if (isAllStretchyOperators) {
for (size_t i = 0; i < embellishedOperators.size(); i++) {
stretchyOperators[i]->resetStretchSize();
fixLayoutAfterStretch(*embellishedOperators[i], *stretchyOperators[i]);
stretchWidth = std::max(stretchWidth, embellishedOperators[i]->logicalWidth());
}
}
for (size_t i = 0; i < embellishedOperators.size(); i++) {
stretchyOperators[i]->stretchTo(stretchWidth);
fixLayoutAfterStretch(*embellishedOperators[i], *stretchyOperators[i]);
}
}
bool RenderMathMLUnderOver::isValid() const
{
// Verify whether the list of children is valid:
// <munder> base under </munder>
// <mover> base over </mover>
// <munderover> base under over </munderover>
auto* child = firstChildBox();
if (!child)
return false;
child = child->nextSiblingBox();
if (!child)
return false;
child = child->nextSiblingBox();
switch (scriptType()) {
case MathMLScriptsElement::ScriptType::Over:
case MathMLScriptsElement::ScriptType::Under:
return !child;
case MathMLScriptsElement::ScriptType::UnderOver:
return child && !child->nextSiblingBox();
default:
ASSERT_NOT_REACHED();
return false;
}
}
bool RenderMathMLUnderOver::shouldMoveLimits() const
{
if (style().mathStyle() == MathStyle::Normal)
return false;
if (auto* renderOperator = unembellishedOperator())
return renderOperator->shouldMoveLimits();
return false;
}
RenderBox& RenderMathMLUnderOver::base() const
{
ASSERT(isValid());
return *firstChildBox();
}
RenderBox& RenderMathMLUnderOver::under() const
{
ASSERT(isValid());
ASSERT(scriptType() == MathMLScriptsElement::ScriptType::Under || scriptType() == MathMLScriptsElement::ScriptType::UnderOver);
return *firstChildBox()->nextSiblingBox();
}
RenderBox& RenderMathMLUnderOver::over() const
{
ASSERT(isValid());
ASSERT(scriptType() == MathMLScriptsElement::ScriptType::Over || scriptType() == MathMLScriptsElement::ScriptType::UnderOver);
auto* secondChild = firstChildBox()->nextSiblingBox();
return scriptType() == MathMLScriptsElement::ScriptType::Over ? *secondChild : *secondChild->nextSiblingBox();
}
void RenderMathMLUnderOver::computePreferredLogicalWidths()
{
ASSERT(preferredLogicalWidthsDirty());
if (!isValid()) {
m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = 0;
setPreferredLogicalWidthsDirty(false);
return;
}
if (shouldMoveLimits()) {
RenderMathMLScripts::computePreferredLogicalWidths();
return;
}
LayoutUnit preferredWidth = base().maxPreferredLogicalWidth();
if (scriptType() == MathMLScriptsElement::ScriptType::Under || scriptType() == MathMLScriptsElement::ScriptType::UnderOver)
preferredWidth = std::max(preferredWidth, under().maxPreferredLogicalWidth());
if (scriptType() == MathMLScriptsElement::ScriptType::Over || scriptType() == MathMLScriptsElement::ScriptType::UnderOver)
preferredWidth = std::max(preferredWidth, over().maxPreferredLogicalWidth());
m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = preferredWidth;
setPreferredLogicalWidthsDirty(false);
}
LayoutUnit RenderMathMLUnderOver::horizontalOffset(const RenderBox& child) const
{
return (logicalWidth() - child.logicalWidth()) / 2;
}
bool RenderMathMLUnderOver::hasAccent(bool accentUnder) const
{
ASSERT(scriptType() == MathMLScriptsElement::ScriptType::UnderOver || (accentUnder && scriptType() == MathMLScriptsElement::ScriptType::Under) || (!accentUnder && scriptType() == MathMLScriptsElement::ScriptType::Over));
const MathMLElement::BooleanValue& attributeValue = accentUnder ? element().accentUnder() : element().accent();
if (attributeValue == MathMLElement::BooleanValue::True)
return true;
if (attributeValue == MathMLElement::BooleanValue::False)
return false;
RenderBox& script = accentUnder ? under() : over();
if (!is<RenderMathMLBlock>(script))
return false;
auto* scriptOperator = downcast<RenderMathMLBlock>(script).unembellishedOperator();
return scriptOperator && scriptOperator->hasOperatorFlag(MathMLOperatorDictionary::Accent);
}
RenderMathMLUnderOver::VerticalParameters RenderMathMLUnderOver::verticalParameters() const
{
VerticalParameters parameters;
// By default, we set all values to zero.
parameters.underGapMin = 0;
parameters.overGapMin = 0;
parameters.underShiftMin = 0;
parameters.overShiftMin = 0;
parameters.underExtraDescender = 0;
parameters.overExtraAscender = 0;
parameters.accentBaseHeight = 0;
const auto& primaryFont = style().fontCascade().primaryFont();
auto* mathData = primaryFont.mathData();
if (!mathData) {
// The MATH table specification does not really provide any suggestions, except for some underbar/overbar values and AccentBaseHeight.
LayoutUnit defaultLineThickness = ruleThicknessFallback();
parameters.underGapMin = 3 * defaultLineThickness;
parameters.overGapMin = 3 * defaultLineThickness;
parameters.underExtraDescender = defaultLineThickness;
parameters.overExtraAscender = defaultLineThickness;
parameters.accentBaseHeight = style().metricsOfPrimaryFont().xHeight();
parameters.useUnderOverBarFallBack = true;
return parameters;
}
if (is<RenderMathMLBlock>(base())) {
if (auto* baseOperator = downcast<RenderMathMLBlock>(base()).unembellishedOperator()) {
if (baseOperator->hasOperatorFlag(MathMLOperatorDictionary::LargeOp)) {
// The base is a large operator so we read UpperLimit/LowerLimit constants from the MATH table.
parameters.underGapMin = mathData->getMathConstant(primaryFont, OpenTypeMathData::LowerLimitGapMin);
parameters.overGapMin = mathData->getMathConstant(primaryFont, OpenTypeMathData::UpperLimitGapMin);
parameters.underShiftMin = mathData->getMathConstant(primaryFont, OpenTypeMathData::LowerLimitBaselineDropMin);
parameters.overShiftMin = mathData->getMathConstant(primaryFont, OpenTypeMathData::UpperLimitBaselineRiseMin);
parameters.useUnderOverBarFallBack = false;
return parameters;
}
if (baseOperator->isStretchy() && !baseOperator->isVertical()) {
// The base is a horizontal stretchy operator, so we read StretchStack constants from the MATH table.
parameters.underGapMin = mathData->getMathConstant(primaryFont, OpenTypeMathData::StretchStackGapBelowMin);
parameters.overGapMin = mathData->getMathConstant(primaryFont, OpenTypeMathData::StretchStackGapAboveMin);
parameters.underShiftMin = mathData->getMathConstant(primaryFont, OpenTypeMathData::StretchStackBottomShiftDown);
parameters.overShiftMin = mathData->getMathConstant(primaryFont, OpenTypeMathData::StretchStackTopShiftUp);
parameters.useUnderOverBarFallBack = false;
return parameters;
}
}
}
// By default, we just use the underbar/overbar constants.
parameters.underGapMin = mathData->getMathConstant(primaryFont, OpenTypeMathData::UnderbarVerticalGap);
parameters.overGapMin = mathData->getMathConstant(primaryFont, OpenTypeMathData::OverbarVerticalGap);
parameters.underExtraDescender = mathData->getMathConstant(primaryFont, OpenTypeMathData::UnderbarExtraDescender);
parameters.overExtraAscender = mathData->getMathConstant(primaryFont, OpenTypeMathData::OverbarExtraAscender);
parameters.accentBaseHeight = mathData->getMathConstant(primaryFont, OpenTypeMathData::AccentBaseHeight);
parameters.useUnderOverBarFallBack = true;
return parameters;
}
void RenderMathMLUnderOver::layoutBlock(bool relayoutChildren, LayoutUnit pageLogicalHeight)
{
ASSERT(needsLayout());
for (auto& box : childrenOfType<RenderBox>(*this)) {
if (box.isOutOfFlowPositioned())
box.containingBlock()->insertPositionedObject(box);
}
if (!relayoutChildren && simplifiedLayout())
return;
if (!isValid()) {
layoutInvalidMarkup(relayoutChildren);
return;
}
if (shouldMoveLimits()) {
RenderMathMLScripts::layoutBlock(relayoutChildren, pageLogicalHeight);
return;
}
recomputeLogicalWidth();
stretchHorizontalOperatorsAndLayoutChildren();
ASSERT(!base().needsLayout());
ASSERT(scriptType() == MathMLScriptsElement::ScriptType::Over || !under().needsLayout());
ASSERT(scriptType() == MathMLScriptsElement::ScriptType::Under || !over().needsLayout());
LayoutUnit logicalWidth = base().logicalWidth();
if (scriptType() == MathMLScriptsElement::ScriptType::Under || scriptType() == MathMLScriptsElement::ScriptType::UnderOver)
logicalWidth = std::max(logicalWidth, under().logicalWidth());
if (scriptType() == MathMLScriptsElement::ScriptType::Over || scriptType() == MathMLScriptsElement::ScriptType::UnderOver)
logicalWidth = std::max(logicalWidth, over().logicalWidth());
setLogicalWidth(logicalWidth);
VerticalParameters parameters = verticalParameters();
LayoutUnit verticalOffset;
if (scriptType() == MathMLScriptsElement::ScriptType::Over || scriptType() == MathMLScriptsElement::ScriptType::UnderOver) {
verticalOffset += parameters.overExtraAscender;
over().setLocation(LayoutPoint(horizontalOffset(over()), verticalOffset));
if (parameters.useUnderOverBarFallBack) {
verticalOffset += over().logicalHeight();
if (hasAccent()) {
LayoutUnit baseAscent = ascentForChild(base());
if (baseAscent < parameters.accentBaseHeight)
verticalOffset += parameters.accentBaseHeight - baseAscent;
} else
verticalOffset += parameters.overGapMin;
} else {
LayoutUnit overAscent = ascentForChild(over());
verticalOffset += std::max(over().logicalHeight() + parameters.overGapMin, overAscent + parameters.overShiftMin);
}
}
base().setLocation(LayoutPoint(horizontalOffset(base()), verticalOffset));
verticalOffset += base().logicalHeight();
if (scriptType() == MathMLScriptsElement::ScriptType::Under || scriptType() == MathMLScriptsElement::ScriptType::UnderOver) {
if (parameters.useUnderOverBarFallBack) {
if (!hasAccentUnder())
verticalOffset += parameters.underGapMin;
} else {
LayoutUnit underAscent = ascentForChild(under());
verticalOffset += std::max(parameters.underGapMin, parameters.underShiftMin - underAscent);
}
under().setLocation(LayoutPoint(horizontalOffset(under()), verticalOffset));
verticalOffset += under().logicalHeight();
verticalOffset += parameters.underExtraDescender;
}
setLogicalHeight(verticalOffset);
layoutPositionedObjects(relayoutChildren);
updateScrollInfoAfterLayout();
clearNeedsLayout();
}
}
#endif // ENABLE(MATHML)