RenderMathMLSquareRoot.cpp [plain text]
#include "config.h"
#if ENABLE(MATHML)
#include "RenderMathMLSquareRoot.h"
#include "GraphicsContext.h"
#include "MathMLNames.h"
#include "PaintInfo.h"
#include "Path.h"
namespace WebCore {
using namespace MathMLNames;
const int gRadicalBasePad = 3;
const float gThresholdBaseHeight = 1.5f;
const float gRadicalWidth = 0.75f;
const float gRadicalBottomPointXPos= 0.5f;
const float gRadicalTopLeftPointXPos = 0.2f;
const float gRadicalTopLeftPointYPos = 0.5f;
const float gRadicalLeftEndYShift = 0.05f;
const float gRootBottomPadding = 0.2f;
const float gRadicalLineThickness = 0.02f;
const float gRadicalThickLineThickness = 0.1f;
RenderMathMLSquareRoot::RenderMathMLSquareRoot(Node *expression)
: RenderMathMLBlock(expression)
{
}
void RenderMathMLSquareRoot::paint(PaintInfo& info, int tx, int ty)
{
RenderMathMLBlock::paint(info, tx, ty);
if (info.context->paintingDisabled())
return;
tx += x();
ty += y();
int maxHeight = 0;
int width = 0;
RenderObject* current = firstChild();
while (current) {
if (current->isBoxModelObject()) {
RenderBoxModelObject* box = toRenderBoxModelObject(current);
if (box->offsetHeight() > maxHeight)
maxHeight = box->offsetHeight();
width += box->offsetWidth();
}
current = current->nextSibling();
}
if (!maxHeight)
maxHeight = style()->fontSize();
int frontWidth = static_cast<int>(style()->fontSize() * gRadicalWidth);
int topStartShift = 0;
int thresholdHeight = static_cast<int>(gThresholdBaseHeight * style()->fontSize());
if (maxHeight > thresholdHeight && thresholdHeight) {
float shift = (maxHeight - thresholdHeight) / static_cast<float>(thresholdHeight);
if (shift > 1.)
shift = 1.0f;
topStartShift = static_cast<int>(gRadicalBottomPointXPos * frontWidth * shift);
}
width += topStartShift;
FloatPoint topStart(tx + frontWidth - topStartShift, ty);
FloatPoint bottomLeft(tx + frontWidth * gRadicalBottomPointXPos , ty + maxHeight + gRadicalBasePad);
FloatPoint topLeft(tx + frontWidth * gRadicalTopLeftPointXPos , ty + gRadicalTopLeftPointYPos * maxHeight);
FloatPoint leftEnd(tx , topLeft.y() + gRadicalLeftEndYShift * style()->fontSize());
GraphicsContextStateSaver stateSaver(*info.context);
info.context->setStrokeThickness(gRadicalLineThickness * style()->fontSize());
info.context->setStrokeStyle(SolidStroke);
info.context->setStrokeColor(style()->visitedDependentColor(CSSPropertyColor), ColorSpaceDeviceRGB);
info.context->setLineJoin(MiterJoin);
info.context->setMiterLimit(style()->fontSize());
Path root;
root.moveTo(FloatPoint(topStart.x() + width , ty));
root.addLineTo(topStart);
root.addLineTo(bottomLeft);
root.addLineTo(topLeft);
root.addLineTo(leftEnd);
info.context->strokePath(root);
GraphicsContextStateSaver maskStateSaver(*info.context);
Path mask;
mask.moveTo(topStart);
mask.addLineTo(bottomLeft);
mask.addLineTo(topLeft);
mask.addLineTo(FloatPoint(2 * topLeft.x() - leftEnd.x(), 2 * topLeft.y() - leftEnd.y()));
info.context->clip(mask);
info.context->setStrokeThickness(gRadicalThickLineThickness * style()->fontSize());
info.context->setLineCap(SquareCap);
Path line;
line.moveTo(bottomLeft);
line.addLineTo(topLeft);
info.context->strokePath(line);
}
void RenderMathMLSquareRoot::layout()
{
int maxHeight = 0;
RenderObject* current = firstChild();
while (current) {
if (current->isBoxModelObject()) {
RenderBoxModelObject* box = toRenderBoxModelObject(current);
if (box->offsetHeight() > maxHeight)
maxHeight = box->offsetHeight();
box->style()->setVerticalAlign(BASELINE);
}
current = current->nextSibling();
}
if (!maxHeight)
maxHeight = style()->fontSize();
if (maxHeight > static_cast<int>(gThresholdBaseHeight * style()->fontSize()))
style()->setPaddingBottom(Length(static_cast<int>(gRootBottomPadding * style()->fontSize()), Fixed));
RenderBlock::layout();
}
}
#endif // ENABLE(MATHML)