RenderMathMLRoot.cpp [plain text]
#include "config.h"
#if ENABLE(MATHML)
#include "RenderMathMLRoot.h"
#include "GraphicsContext.h"
#include "PaintInfo.h"
#include "RenderMathMLRow.h"
using namespace std;
namespace WebCore {
const float gThresholdBaseHeightEms = 1.5f;
const float gFrontWidthEms = 0.75f;
const float gSpaceAboveEms = 0.2f;
const float gRadicalBottomPointXFront = 0.5f;
const int gRadicalBottomPointLower = 3;
const float gRadicalDipLeftPointXFront = 0.8f;
const float gSqrtRadicalDipLeftPointYPos = 0.5f;
const float gRootRadicalDipLeftPointYPos = 0.625f;
const float gRadicalLeftEndYShiftEms = 0.05f;
const float gBigRootBottomPaddingEms = 0.2f;
const float gRadicalLineThicknessEms = 0.02f;
const float gRadicalThickLineThicknessEms = 0.1f;
RenderMathMLRoot::RenderMathMLRoot(Element* element)
: RenderMathMLBlock(element)
, m_intrinsicPaddingBefore(0)
, m_intrinsicPaddingAfter(0)
, m_intrinsicPaddingStart(0)
, m_intrinsicPaddingEnd(0)
{
}
LayoutUnit RenderMathMLRoot::paddingTop() const
{
LayoutUnit result = computedCSSPaddingTop();
switch (style()->writingMode()) {
case TopToBottomWritingMode:
return result + m_intrinsicPaddingBefore;
case BottomToTopWritingMode:
return result + m_intrinsicPaddingAfter;
case LeftToRightWritingMode:
case RightToLeftWritingMode:
return result + (style()->isLeftToRightDirection() ? m_intrinsicPaddingStart : m_intrinsicPaddingEnd);
}
ASSERT_NOT_REACHED();
return result;
}
LayoutUnit RenderMathMLRoot::paddingBottom() const
{
LayoutUnit result = computedCSSPaddingBottom();
switch (style()->writingMode()) {
case TopToBottomWritingMode:
return result + m_intrinsicPaddingAfter;
case BottomToTopWritingMode:
return result + m_intrinsicPaddingBefore;
case LeftToRightWritingMode:
case RightToLeftWritingMode:
return result + (style()->isLeftToRightDirection() ? m_intrinsicPaddingEnd : m_intrinsicPaddingStart);
}
ASSERT_NOT_REACHED();
return result;
}
LayoutUnit RenderMathMLRoot::paddingLeft() const
{
LayoutUnit result = computedCSSPaddingLeft();
switch (style()->writingMode()) {
case LeftToRightWritingMode:
return result + m_intrinsicPaddingBefore;
case RightToLeftWritingMode:
return result + m_intrinsicPaddingAfter;
case TopToBottomWritingMode:
case BottomToTopWritingMode:
return result + (style()->isLeftToRightDirection() ? m_intrinsicPaddingStart : m_intrinsicPaddingEnd);
}
ASSERT_NOT_REACHED();
return result;
}
LayoutUnit RenderMathMLRoot::paddingRight() const
{
LayoutUnit result = computedCSSPaddingRight();
switch (style()->writingMode()) {
case RightToLeftWritingMode:
return result + m_intrinsicPaddingBefore;
case LeftToRightWritingMode:
return result + m_intrinsicPaddingAfter;
case TopToBottomWritingMode:
case BottomToTopWritingMode:
return result + (style()->isLeftToRightDirection() ? m_intrinsicPaddingEnd : m_intrinsicPaddingStart);
}
ASSERT_NOT_REACHED();
return result;
}
LayoutUnit RenderMathMLRoot::paddingBefore() const
{
return computedCSSPaddingBefore() + m_intrinsicPaddingBefore;
}
LayoutUnit RenderMathMLRoot::paddingAfter() const
{
return computedCSSPaddingAfter() + m_intrinsicPaddingAfter;
}
LayoutUnit RenderMathMLRoot::paddingStart() const
{
return computedCSSPaddingStart() + m_intrinsicPaddingStart;
}
LayoutUnit RenderMathMLRoot::paddingEnd() const
{
return computedCSSPaddingEnd() + m_intrinsicPaddingEnd;
}
void RenderMathMLRoot::addChild(RenderObject* newChild, RenderObject* beforeChild)
{
if (!firstChild())
RenderMathMLBlock::addChild(RenderMathMLRow::createAnonymousWithParentRenderer(this));
if (newChild->style()->position() == AbsolutePosition)
RenderMathMLBlock::addChild(newChild);
else
firstChild()->addChild(newChild, beforeChild && beforeChild->parent() == firstChild() ? beforeChild : 0);
}
RenderBox* RenderMathMLRoot::index() const
{
if (!firstChild())
return 0;
RenderObject* index = firstChild()->nextSibling();
if (!index || !index->isBox())
return 0;
return toRenderBox(index);
}
void RenderMathMLRoot::layout()
{
for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
if (!child->isBox())
continue;
toRenderBox(child)->layoutIfNeeded();
}
int baseHeight = firstChild() && firstChild()->isBox() ? roundToInt(toRenderBox(firstChild())->logicalHeight()) : style()->fontSize();
int frontWidth = lroundf(gFrontWidthEms * style()->fontSize());
float thresholdHeight = gThresholdBaseHeightEms * style()->fontSize();
if (baseHeight > thresholdHeight && thresholdHeight) {
float shift = min<float>((baseHeight - thresholdHeight) / thresholdHeight, 1.0f);
m_overbarLeftPointShift = static_cast<int>(shift * gRadicalBottomPointXFront * frontWidth);
m_intrinsicPaddingAfter = lroundf(gBigRootBottomPaddingEms * style()->fontSize());
} else {
m_overbarLeftPointShift = 0;
m_intrinsicPaddingAfter = 0;
}
int rootPad = lroundf(gSpaceAboveEms * style()->fontSize());
m_intrinsicPaddingBefore = rootPad;
m_indexTop = 0;
if (RenderBox* index = this->index()) {
m_intrinsicPaddingStart = roundToInt(index->maxPreferredLogicalWidth()) + m_overbarLeftPointShift;
int indexHeight = roundToInt(index->logicalHeight());
int partDipHeight = lroundf((1 - gRootRadicalDipLeftPointYPos) * baseHeight);
int rootExtraTop = partDipHeight + indexHeight - (baseHeight + rootPad);
if (rootExtraTop > 0)
m_intrinsicPaddingBefore += rootExtraTop;
else
m_indexTop = - rootExtraTop;
} else
m_intrinsicPaddingStart = frontWidth;
setPreferredLogicalWidthsDirty(true);
RenderMathMLBlock::layout();
if (RenderBox* index = this->index())
index->setLogicalTop(m_indexTop);
}
void RenderMathMLRoot::paint(PaintInfo& info, const LayoutPoint& paintOffset)
{
RenderMathMLBlock::paint(info, paintOffset);
if (info.context->paintingDisabled() || style()->visibility() != VISIBLE)
return;
IntPoint adjustedPaintOffset = roundedIntPoint(paintOffset + location() + contentBoxRect().location());
int startX = adjustedPaintOffset.x();
int frontWidth = lroundf(gFrontWidthEms * style()->fontSize());
int overbarWidth = roundToInt(contentLogicalWidth()) + m_overbarLeftPointShift;
int baseHeight = roundToInt(contentLogicalHeight());
int rootPad = lroundf(gSpaceAboveEms * style()->fontSize());
adjustedPaintOffset.setY(adjustedPaintOffset.y() - rootPad);
float radicalDipLeftPointYPos = (index() ? gRootRadicalDipLeftPointYPos : gSqrtRadicalDipLeftPointYPos) * baseHeight;
FloatPoint overbarLeftPoint(startX - m_overbarLeftPointShift, adjustedPaintOffset.y());
FloatPoint bottomPoint(startX - gRadicalBottomPointXFront * frontWidth, adjustedPaintOffset.y() + baseHeight + gRadicalBottomPointLower);
FloatPoint dipLeftPoint(startX - gRadicalDipLeftPointXFront * frontWidth, adjustedPaintOffset.y() + radicalDipLeftPointYPos);
FloatPoint leftEnd(startX - frontWidth, dipLeftPoint.y() + gRadicalLeftEndYShiftEms * style()->fontSize());
GraphicsContextStateSaver stateSaver(*info.context);
info.context->setStrokeThickness(gRadicalLineThicknessEms * 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(overbarLeftPoint.x() + overbarWidth, adjustedPaintOffset.y()));
root.addLineTo(overbarLeftPoint);
root.addLineTo(bottomPoint);
root.addLineTo(dipLeftPoint);
root.addLineTo(leftEnd);
info.context->strokePath(root);
GraphicsContextStateSaver maskStateSaver(*info.context);
Path mask;
mask.moveTo(overbarLeftPoint);
mask.addLineTo(bottomPoint);
mask.addLineTo(dipLeftPoint);
mask.addLineTo(FloatPoint(2 * dipLeftPoint.x() - leftEnd.x(), 2 * dipLeftPoint.y() - leftEnd.y()));
info.context->clip(mask);
info.context->setStrokeThickness(gRadicalThickLineThicknessEms * style()->fontSize());
info.context->setLineCap(SquareCap);
Path line;
line.moveTo(bottomPoint);
line.addLineTo(dipLeftPoint);
info.context->strokePath(line);
}
}
#endif // ENABLE(MATHML)