2 * Copyright (C) 2009 Alex Milowski (alex@milowski.com). All rights reserved.
3 * Copyright (C) 2010 François Sausset (sausset@gmail.com). All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
31 #include "RenderMathMLRoot.h"
33 #include "GraphicsContext.h"
34 #include "MathMLNames.h"
35 #include "PaintInfo.h"
39 using namespace MathMLNames;
41 // Left margin of the radical (px)
42 const int gRadicalLeftMargin = 3;
43 // Bottom padding of the radical (px)
44 const int gRadicalBasePad = 3;
45 // Threshold above which the radical shape is modified to look nice with big bases (%)
46 const float gThresholdBaseHeight = 1.5f;
48 const float gRadicalWidth = 0.75f;
49 // Horizontal position of the bottom point of the radical (%)
50 const float gRadicalBottomPointXPos= 0.5f;
51 // Horizontal position of the top left point of the radical (%)
52 const float gRadicalTopLeftPointXPos = 0.8f;
53 // Vertical position of the top left point of the radical (%)
54 const float gRadicalTopLeftPointYPos = 0.625f;
55 // Vertical shift of the left end point of the radical (%)
56 const float gRadicalLeftEndYShift = 0.05f;
57 // Root padding around the base (%)
58 const float gRootPadding = 0.2f;
59 // Additional bottom root padding (%)
60 const float gRootBottomPadding = 0.2f;
62 // Radical line thickness (%)
63 const float gRadicalLineThickness = 0.02f;
64 // Radical thick line thickness (%)
65 const float gRadicalThickLineThickness = 0.1f;
67 RenderMathMLRoot::RenderMathMLRoot(Node *expression)
68 : RenderMathMLBlock(expression)
72 void RenderMathMLRoot::addChild(RenderObject* child, RenderObject* )
75 // Add a block for the index
76 RenderBlock* block = new (renderArena()) RenderBlock(node());
77 RefPtr<RenderStyle> indexStyle = makeBlockStyle();
78 indexStyle->setDisplay(INLINE_BLOCK);
79 block->setStyle(indexStyle.release());
80 RenderBlock::addChild(block);
82 // FIXME: the wrapping does not seem to be needed anymore.
83 // this is the base, so wrap it so we can pad it
84 block = new (renderArena()) RenderBlock(node());
85 RefPtr<RenderStyle> baseStyle = makeBlockStyle();
86 baseStyle->setDisplay(INLINE_BLOCK);
87 baseStyle->setPaddingLeft(Length(5 * gRadicalWidth , Percent));
88 block->setStyle(baseStyle.release());
89 RenderBlock::addChild(block);
90 block->addChild(child);
92 // always add to the index
93 firstChild()->addChild(child);
97 void RenderMathMLRoot::paint(PaintInfo& info, const LayoutPoint& paintOffset)
99 RenderMathMLBlock::paint(info, paintOffset);
101 if (info.context->paintingDisabled())
104 if (!firstChild() || !lastChild())
107 LayoutPoint adjustedPaintOffset = paintOffset + location();
109 RenderBoxModelObject* indexBox = toRenderBoxModelObject(lastChild());
111 LayoutUnit maxHeight = indexBox->offsetHeight();
112 // default to the font size in pixels if we're empty
114 maxHeight = style()->fontSize();
115 LayoutUnit width = indexBox->offsetWidth();
117 LayoutUnit indexWidth = 0;
118 RenderObject* current = firstChild();
119 while (current != lastChild()) {
120 if (current->isBoxModelObject()) {
121 RenderBoxModelObject* box = toRenderBoxModelObject(current);
122 indexWidth += box->offsetWidth();
124 current = current->nextSibling();
127 int frontWidth = static_cast<int>(style()->fontSize() * gRadicalWidth);
128 int topStartShift = 0;
129 // Base height above which the shape of the root changes
130 int thresholdHeight = static_cast<int>(gThresholdBaseHeight * style()->fontSize());
132 if (maxHeight > thresholdHeight && thresholdHeight) {
133 float shift = (maxHeight - thresholdHeight) / static_cast<float>(thresholdHeight);
136 topStartShift = static_cast<int>(gRadicalBottomPointXPos * frontWidth * shift);
139 width += topStartShift;
141 int rootPad = static_cast<int>(gRootPadding * style()->fontSize());
142 LayoutUnit start = adjustedPaintOffset.x() + indexWidth + gRadicalLeftMargin + style()->paddingLeft().value() - rootPad;
143 adjustedPaintOffset.setY(adjustedPaintOffset.y() + style()->paddingTop().value() - rootPad);
145 FloatPoint topStart(start - topStartShift, paintOffset.y());
146 FloatPoint bottomLeft(start - gRadicalBottomPointXPos * frontWidth , adjustedPaintOffset.y() + maxHeight + gRadicalBasePad);
147 FloatPoint topLeft(start - gRadicalTopLeftPointXPos * frontWidth , adjustedPaintOffset.y() + gRadicalTopLeftPointYPos * maxHeight);
148 FloatPoint leftEnd(start - frontWidth , topLeft.y() + gRadicalLeftEndYShift * style()->fontSize());
150 GraphicsContextStateSaver stateSaver(*info.context);
152 info.context->setStrokeThickness(gRadicalLineThickness * style()->fontSize());
153 info.context->setStrokeStyle(SolidStroke);
154 info.context->setStrokeColor(style()->visitedDependentColor(CSSPropertyColor), ColorSpaceDeviceRGB);
155 info.context->setLineJoin(MiterJoin);
156 info.context->setMiterLimit(style()->fontSize());
160 root.moveTo(FloatPoint(topStart.x() + width, adjustedPaintOffset.y()));
162 root.addLineTo(topStart);
163 // draw from top left corner to bottom point of radical
164 root.addLineTo(bottomLeft);
165 // draw from bottom point to top of left part of radical base "pocket"
166 root.addLineTo(topLeft);
168 root.addLineTo(leftEnd);
170 info.context->strokePath(root);
172 GraphicsContextStateSaver maskStateSaver(*info.context);
174 // Build a mask to draw the thick part of the root.
177 mask.moveTo(topStart);
178 mask.addLineTo(bottomLeft);
179 mask.addLineTo(topLeft);
180 mask.addLineTo(FloatPoint(2 * topLeft.x() - leftEnd.x(), 2 * topLeft.y() - leftEnd.y()));
182 info.context->clip(mask);
184 // Draw the thick part of the root.
185 info.context->setStrokeThickness(gRadicalThickLineThickness * style()->fontSize());
186 info.context->setLineCap(SquareCap);
189 line.moveTo(bottomLeft);
190 line.addLineTo(topLeft);
192 info.context->strokePath(line);
195 void RenderMathMLRoot::layout()
197 RenderBlock::layout();
199 if (!firstChild() || !lastChild())
202 LayoutUnit maxHeight = toRenderBoxModelObject(lastChild())->offsetHeight();
204 RenderObject* current = lastChild()->firstChild();
206 current->style()->setVerticalAlign(BASELINE);
209 maxHeight = style()->fontSize();
211 // Base height above which the shape of the root changes
212 LayoutUnit thresholdHeight = static_cast<LayoutUnit>(gThresholdBaseHeight * style()->fontSize());
213 LayoutUnit topStartShift = 0;
215 if (maxHeight > thresholdHeight && thresholdHeight) {
216 float shift = (maxHeight - thresholdHeight) / static_cast<float>(thresholdHeight);
219 LayoutUnit frontWidth = static_cast<LayoutUnit>(style()->fontSize() * gRadicalWidth);
220 topStartShift = static_cast<LayoutUnit>(gRadicalBottomPointXPos * frontWidth * shift);
222 style()->setPaddingBottom(Length(static_cast<LayoutUnit>(gRootBottomPadding * style()->fontSize()), Fixed));
225 // Positioning of the index
226 RenderObject* possibleIndex = firstChild()->firstChild();
227 while (possibleIndex && !possibleIndex->isBoxModelObject())
228 possibleIndex = possibleIndex->nextSibling();
229 RenderBoxModelObject* indexBox = toRenderBoxModelObject(possibleIndex);
233 LayoutUnit indexShift = indexBox->offsetWidth() + topStartShift;
234 LayoutUnit radicalHeight = static_cast<LayoutUnit>((1 - gRadicalTopLeftPointYPos) * maxHeight);
235 LayoutUnit rootMarginTop = radicalHeight + style()->paddingBottom().value() + indexBox->offsetHeight() - (maxHeight + static_cast<LayoutUnit>(gRootPadding * style()->fontSize()));
237 style()->setPaddingLeft(Length(indexShift, Fixed));
238 if (rootMarginTop > 0)
239 style()->setPaddingTop(Length(rootMarginTop + static_cast<LayoutUnit>(gRootPadding * style()->fontSize()), Fixed));
241 setNeedsLayout(true);
242 setPreferredLogicalWidthsDirty(true, false);
243 RenderBlock::layout();
245 indexBox->style()->setBottom(Length(radicalHeight + style()->paddingBottom().value(), Fixed));
247 // Now that we've potentially changed its position, we need layout the index again.
248 indexBox->setNeedsLayout(true);
254 #endif // ENABLE(MATHML)