1/*
2 * Copyright (C) 2010 Alex Milowski (alex@milowski.com). All rights reserved.
3 * Copyright (C) 2016 Igalia S.L.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28#include "RenderMathMLRow.h"
29
30#if ENABLE(MATHML)
31
32#include "MathMLNames.h"
33#include "MathMLRowElement.h"
34#include "RenderIterator.h"
35#include "RenderMathMLOperator.h"
36#include "RenderMathMLRoot.h"
37#include <wtf/IsoMallocInlines.h>
38
39namespace WebCore {
40
41using namespace MathMLNames;
42
43WTF_MAKE_ISO_ALLOCATED_IMPL(RenderMathMLRow);
44
45RenderMathMLRow::RenderMathMLRow(MathMLRowElement& element, RenderStyle&& style)
46 : RenderMathMLBlock(element, WTFMove(style))
47{
48}
49
50MathMLRowElement& RenderMathMLRow::element() const
51{
52 return static_cast<MathMLRowElement&>(nodeForNonAnonymous());
53}
54
55Optional<int> RenderMathMLRow::firstLineBaseline() const
56{
57 auto* baselineChild = firstChildBox();
58 if (!baselineChild)
59 return Optional<int>();
60
61 return Optional<int>(static_cast<int>(lroundf(ascentForChild(*baselineChild) + baselineChild->logicalTop())));
62}
63
64static RenderMathMLOperator* toVerticalStretchyOperator(RenderBox* box)
65{
66 if (is<RenderMathMLBlock>(box)) {
67 auto* renderOperator = downcast<RenderMathMLBlock>(*box).unembellishedOperator();
68 if (renderOperator && renderOperator->isStretchy() && renderOperator->isVertical())
69 return renderOperator;
70 }
71 return nullptr;
72}
73
74void RenderMathMLRow::stretchVerticalOperatorsAndLayoutChildren()
75{
76 // First calculate stretch ascent and descent.
77 LayoutUnit stretchAscent, stretchDescent;
78 for (auto* child = firstChildBox(); child; child = child->nextSiblingBox()) {
79 if (child->isOutOfFlowPositioned()) {
80 child->containingBlock()->insertPositionedObject(*child);
81 continue;
82 }
83 if (toVerticalStretchyOperator(child))
84 continue;
85 child->layoutIfNeeded();
86 LayoutUnit childAscent = ascentForChild(*child);
87 LayoutUnit childDescent = child->logicalHeight() - childAscent;
88 stretchAscent = std::max(stretchAscent, childAscent);
89 stretchDescent = std::max(stretchDescent, childDescent);
90 }
91 if (stretchAscent + stretchDescent <= 0) {
92 // We ensure a minimal stretch size.
93 stretchAscent = style().computedFontPixelSize();
94 stretchDescent = 0;
95 }
96
97 // Next, we stretch the vertical operators.
98 for (auto* child = firstChildBox(); child; child = child->nextSiblingBox()) {
99 if (child->isOutOfFlowPositioned())
100 continue;
101 if (auto renderOperator = toVerticalStretchyOperator(child)) {
102 renderOperator->stretchTo(stretchAscent, stretchDescent);
103 renderOperator->layoutIfNeeded();
104 }
105 }
106}
107
108void RenderMathMLRow::getContentBoundingBox(LayoutUnit& width, LayoutUnit& ascent, LayoutUnit& descent) const
109{
110 ascent = 0;
111 descent = 0;
112 width = borderAndPaddingStart();
113 for (auto* child = firstChildBox(); child; child = child->nextSiblingBox()) {
114 if (child->isOutOfFlowPositioned())
115 continue;
116
117 width += child->marginStart() + child->logicalWidth() + child->marginEnd();
118 LayoutUnit childAscent = ascentForChild(*child);
119 LayoutUnit childDescent = child->logicalHeight() - childAscent;
120 ascent = std::max(ascent, childAscent + child->marginTop());
121 descent = std::max(descent, childDescent + child->marginBottom());
122 }
123 width += borderEnd() + paddingEnd();
124}
125
126void RenderMathMLRow::computePreferredLogicalWidths()
127{
128 ASSERT(preferredLogicalWidthsDirty());
129
130 m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = 0;
131
132 LayoutUnit preferredWidth;
133 for (auto* child = firstChildBox(); child; child = child->nextSiblingBox()) {
134 if (child->isOutOfFlowPositioned())
135 continue;
136 preferredWidth += child->maxPreferredLogicalWidth() + child->marginLogicalWidth();
137 }
138
139 m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = preferredWidth + borderAndPaddingLogicalWidth();
140
141 setPreferredLogicalWidthsDirty(false);
142}
143
144void RenderMathMLRow::layoutRowItems(LayoutUnit width, LayoutUnit ascent)
145{
146 LayoutUnit horizontalOffset = borderAndPaddingStart();
147 for (auto* child = firstChildBox(); child; child = child->nextSiblingBox()) {
148 if (child->isOutOfFlowPositioned())
149 continue;
150 horizontalOffset += child->marginStart();
151 LayoutUnit childAscent = ascentForChild(*child);
152 LayoutUnit childVerticalOffset = borderTop() + paddingTop() + child->marginTop() + ascent - childAscent;
153 LayoutUnit childWidth = child->logicalWidth();
154 LayoutUnit childHorizontalOffset = style().isLeftToRightDirection() ? horizontalOffset : width - horizontalOffset - childWidth;
155 child->setLocation(LayoutPoint(childHorizontalOffset, childVerticalOffset));
156 horizontalOffset += childWidth + child->marginEnd();
157 }
158}
159
160void RenderMathMLRow::layoutBlock(bool relayoutChildren, LayoutUnit)
161{
162 ASSERT(needsLayout());
163
164 if (!relayoutChildren && simplifiedLayout())
165 return;
166
167 recomputeLogicalWidth();
168
169 setLogicalHeight(borderAndPaddingLogicalHeight() + scrollbarLogicalHeight());
170
171 LayoutUnit width, ascent, descent;
172 stretchVerticalOperatorsAndLayoutChildren();
173 getContentBoundingBox(width, ascent, descent);
174 layoutRowItems(width, ascent);
175 setLogicalWidth(width);
176 setLogicalHeight(borderTop() + paddingTop() + ascent + descent + borderBottom() + paddingBottom() + horizontalScrollbarHeight());
177 updateLogicalHeight();
178
179 layoutPositionedObjects(relayoutChildren);
180
181 updateScrollInfoAfterLayout();
182
183 clearNeedsLayout();
184}
185
186}
187
188#endif // ENABLE(MATHML)
189