initial import
[vuplus_webkit] / Source / WebCore / rendering / mathml / RenderMathMLRoot.cpp
1 /*
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.
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
29 #if ENABLE(MATHML)
30
31 #include "RenderMathMLRoot.h"
32
33 #include "GraphicsContext.h"
34 #include "MathMLNames.h"
35 #include "PaintInfo.h"
36
37 namespace WebCore {
38     
39 using namespace MathMLNames;
40
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;
47 // Radical width (%)
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;
61     
62 // Radical line thickness (%)
63 const float gRadicalLineThickness = 0.02f;
64 // Radical thick line thickness (%)
65 const float gRadicalThickLineThickness = 0.1f;
66     
67 RenderMathMLRoot::RenderMathMLRoot(Node *expression) 
68 : RenderMathMLBlock(expression) 
69 {
70 }
71
72 void RenderMathMLRoot::addChild(RenderObject* child, RenderObject* )
73 {
74     if (isEmpty()) {
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);
81         
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);
91     } else {
92         // always add to the index
93         firstChild()->addChild(child);
94     }
95 }
96     
97 void RenderMathMLRoot::paint(PaintInfo& info, const LayoutPoint& paintOffset)
98 {
99     RenderMathMLBlock::paint(info, paintOffset);
100     
101     if (info.context->paintingDisabled())
102         return;
103
104     if (!firstChild() || !lastChild())
105         return;
106
107     LayoutPoint adjustedPaintOffset = paintOffset + location();
108     
109     RenderBoxModelObject* indexBox = toRenderBoxModelObject(lastChild());
110     
111     LayoutUnit maxHeight = indexBox->offsetHeight();
112     // default to the font size in pixels if we're empty
113     if (!maxHeight)
114         maxHeight = style()->fontSize();
115     LayoutUnit width = indexBox->offsetWidth();
116     
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();
123         }
124         current = current->nextSibling();
125     }
126     
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());
131     
132     if (maxHeight > thresholdHeight && thresholdHeight) {
133         float shift = (maxHeight - thresholdHeight) / static_cast<float>(thresholdHeight);
134         if (shift > 1.)
135             shift = 1.0f;
136         topStartShift = static_cast<int>(gRadicalBottomPointXPos * frontWidth * shift);
137     }
138     
139     width += topStartShift;
140     
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);
144     
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());
149     
150     GraphicsContextStateSaver stateSaver(*info.context);
151     
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());
157     
158     Path root;
159     
160     root.moveTo(FloatPoint(topStart.x() + width, adjustedPaintOffset.y()));
161     // draw top
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);
167     // draw to end
168     root.addLineTo(leftEnd);
169     
170     info.context->strokePath(root);
171     
172     GraphicsContextStateSaver maskStateSaver(*info.context);
173     
174     // Build a mask to draw the thick part of the root.
175     Path mask;
176     
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()));
181     
182     info.context->clip(mask);
183     
184     // Draw the thick part of the root.
185     info.context->setStrokeThickness(gRadicalThickLineThickness * style()->fontSize());
186     info.context->setLineCap(SquareCap);
187     
188     Path line;
189     line.moveTo(bottomLeft);
190     line.addLineTo(topLeft);
191
192     info.context->strokePath(line);
193 }
194
195 void RenderMathMLRoot::layout()
196 {
197     RenderBlock::layout();
198
199     if (!firstChild() || !lastChild())
200         return;
201
202     LayoutUnit maxHeight = toRenderBoxModelObject(lastChild())->offsetHeight();
203     
204     RenderObject* current = lastChild()->firstChild();
205     if (current)
206         current->style()->setVerticalAlign(BASELINE);
207     
208     if (!maxHeight)
209         maxHeight = style()->fontSize();
210     
211     // Base height above which the shape of the root changes
212     LayoutUnit thresholdHeight = static_cast<LayoutUnit>(gThresholdBaseHeight * style()->fontSize());
213     LayoutUnit topStartShift = 0;
214     
215     if (maxHeight > thresholdHeight && thresholdHeight) {
216         float shift = (maxHeight - thresholdHeight) / static_cast<float>(thresholdHeight);
217         if (shift > 1.)
218             shift = 1.0f;
219         LayoutUnit frontWidth = static_cast<LayoutUnit>(style()->fontSize() * gRadicalWidth);
220         topStartShift = static_cast<LayoutUnit>(gRadicalBottomPointXPos * frontWidth * shift);
221         
222         style()->setPaddingBottom(Length(static_cast<LayoutUnit>(gRootBottomPadding * style()->fontSize()), Fixed));
223     }
224     
225     // Positioning of the index
226     RenderObject* possibleIndex = firstChild()->firstChild();
227     while (possibleIndex && !possibleIndex->isBoxModelObject())
228         possibleIndex = possibleIndex->nextSibling();
229     RenderBoxModelObject* indexBox = toRenderBoxModelObject(possibleIndex);
230     if (!indexBox)
231         return;
232     
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()));
236     
237     style()->setPaddingLeft(Length(indexShift, Fixed));
238     if (rootMarginTop > 0)
239         style()->setPaddingTop(Length(rootMarginTop + static_cast<LayoutUnit>(gRootPadding * style()->fontSize()), Fixed));
240     
241     setNeedsLayout(true);
242     setPreferredLogicalWidthsDirty(true, false);
243     RenderBlock::layout();
244
245     indexBox->style()->setBottom(Length(radicalHeight + style()->paddingBottom().value(), Fixed));
246
247     // Now that we've potentially changed its position, we need layout the index again.
248     indexBox->setNeedsLayout(true);
249     indexBox->layout();
250 }
251     
252 }
253
254 #endif // ENABLE(MATHML)
255
256