RenderMathMLBlock.cpp [plain text]
#include "config.h"
#if ENABLE(MATHML)
#include "RenderMathMLBlock.h"
#include "GraphicsContext.h"
#include "MathMLNames.h"
#include "RenderView.h"
#include <wtf/text/StringBuilder.h>
#if ENABLE(DEBUG_MATH_LAYOUT)
#include "PaintInfo.h"
#endif
namespace WebCore {
using namespace MathMLNames;
RenderMathMLBlock::RenderMathMLBlock(Element& container, PassRef<RenderStyle> style)
: RenderFlexibleBox(container, WTF::move(style))
{
}
RenderMathMLBlock::RenderMathMLBlock(Document& document, PassRef<RenderStyle> style)
: RenderFlexibleBox(document, WTF::move(style))
{
}
bool RenderMathMLBlock::isChildAllowed(const RenderObject& child, const RenderStyle&) const
{
return child.node() && isElement(*child.node());
}
RenderPtr<RenderMathMLBlock> RenderMathMLBlock::createAnonymousMathMLBlock()
{
RenderPtr<RenderMathMLBlock> newBlock = createRenderer<RenderMathMLBlock>(document(), RenderStyle::createAnonymousStyleWithDisplay(&style(), FLEX));
newBlock->initializeStyle();
return newBlock;
}
int RenderMathMLBlock::baselinePosition(FontBaseline baselineType, bool firstLine, LineDirectionMode direction, LinePositionMode linePositionMode) const
{
if (linePositionMode == PositionOfInteriorLineBoxes)
return 0;
LayoutUnit baseline = firstLineBaseline(); if (baseline != -1)
return baseline;
return RenderFlexibleBox::baselinePosition(baselineType, firstLine, direction, linePositionMode);
}
const char* RenderMathMLBlock::renderName() const
{
EDisplay display = style().display();
if (display == FLEX)
return isAnonymous() ? "RenderMathMLBlock (anonymous, flex)" : "RenderMathMLBlock (flex)";
if (display == INLINE_FLEX)
return isAnonymous() ? "RenderMathMLBlock (anonymous, inline-flex)" : "RenderMathMLBlock (inline-flex)";
ASSERT_NOT_REACHED();
return isAnonymous() ? "RenderMathMLBlock (anonymous)" : "RenderMathMLBlock";
}
#if ENABLE(DEBUG_MATH_LAYOUT)
void RenderMathMLBlock::paint(PaintInfo& info, const LayoutPoint& paintOffset)
{
RenderFlexibleBox::paint(info, paintOffset);
if (info.context->paintingDisabled() || info.phase != PaintPhaseForeground)
return;
IntPoint adjustedPaintOffset = roundedIntPoint(paintOffset + location());
GraphicsContextStateSaver stateSaver(*info.context);
info.context->setStrokeThickness(1.0f);
info.context->setStrokeStyle(SolidStroke);
info.context->setStrokeColor(Color(0, 0, 255), ColorSpaceSRGB);
info.context->drawLine(adjustedPaintOffset, IntPoint(adjustedPaintOffset.x() + pixelSnappedOffsetWidth(), adjustedPaintOffset.y()));
info.context->drawLine(IntPoint(adjustedPaintOffset.x() + pixelSnappedOffsetWidth(), adjustedPaintOffset.y()), IntPoint(adjustedPaintOffset.x() + pixelSnappedOffsetWidth(), adjustedPaintOffset.y() + pixelSnappedOffsetHeight()));
info.context->drawLine(IntPoint(adjustedPaintOffset.x(), adjustedPaintOffset.y() + pixelSnappedOffsetHeight()), IntPoint(adjustedPaintOffset.x() + pixelSnappedOffsetWidth(), adjustedPaintOffset.y() + pixelSnappedOffsetHeight()));
info.context->drawLine(adjustedPaintOffset, IntPoint(adjustedPaintOffset.x(), adjustedPaintOffset.y() + pixelSnappedOffsetHeight()));
int topStart = paddingTop();
info.context->setStrokeColor(Color(0, 255, 0), ColorSpaceSRGB);
info.context->drawLine(IntPoint(adjustedPaintOffset.x(), adjustedPaintOffset.y() + topStart), IntPoint(adjustedPaintOffset.x() + pixelSnappedOffsetWidth(), adjustedPaintOffset.y() + topStart));
int baseline = roundToInt(baselinePosition(AlphabeticBaseline, true, HorizontalLine));
info.context->setStrokeColor(Color(255, 0, 0), ColorSpaceSRGB);
info.context->drawLine(IntPoint(adjustedPaintOffset.x(), adjustedPaintOffset.y() + baseline), IntPoint(adjustedPaintOffset.x() + pixelSnappedOffsetWidth(), adjustedPaintOffset.y() + baseline));
}
#endif // ENABLE(DEBUG_MATH_LAYOUT)
bool parseMathMLLength(const String& string, LayoutUnit& lengthValue, const RenderStyle* style, bool allowNegative)
{
String s = string.simplifyWhiteSpace();
int stringLength = s.length();
if (!stringLength)
return false;
if (parseMathMLNamedSpace(s, lengthValue, style, allowNegative))
return true;
StringBuilder number;
String unit;
int i = 0;
UChar c = s[0];
if (c == '-') {
number.append(c);
i++;
}
bool gotDot = false;
for ( ; i < stringLength; i++) {
c = s[i];
if (gotDot && c == '.')
return false;
if (c == '.')
gotDot = true;
else if (!isASCIIDigit(c)) {
unit = s.substring(i, stringLength - i);
break;
}
number.append(c);
}
bool ok;
float floatValue = number.toString().toFloat(&ok);
if (!ok)
return false;
if (floatValue < 0 && !allowNegative)
return false;
if (unit.isEmpty()) {
lengthValue *= floatValue;
return true;
}
if (unit == "%") {
lengthValue *= floatValue / 100;
return true;
}
if (unit == "em") {
lengthValue = floatValue * style->font().size();
return true;
}
if (unit == "ex") {
lengthValue = floatValue * style->fontMetrics().xHeight();
return true;
}
if (unit == "px") {
lengthValue = floatValue;
return true;
}
if (unit == "pt") {
lengthValue = 4 * (floatValue / 3);
return true;
}
if (unit == "pc") {
lengthValue = 16 * floatValue;
return true;
}
if (unit == "in") {
lengthValue = 96 * floatValue;
return true;
}
if (unit == "cm") {
lengthValue = 96 * (floatValue / 2.54);
return true;
}
if (unit == "mm") {
lengthValue = 96 * (floatValue / 25.4);
return true;
}
return false;
}
bool parseMathMLNamedSpace(const String& string, LayoutUnit& lengthValue, const RenderStyle* style, bool allowNegative)
{
float length = 0;
if (string == "veryverythinmathspace")
length = 1;
else if (string == "verythinmathspace")
length = 2;
else if (string == "thinmathspace")
length = 3;
else if (string == "mediummathspace")
length = 4;
else if (string == "thickmathspace")
length = 5;
else if (string == "verythickmathspace")
length = 6;
else if (string == "veryverythickmathspace")
length = 7;
else if (allowNegative) {
if (string == "negativeveryverythinmathspace")
length = -1;
else if (string == "negativeverythinmathspace")
length = -2;
else if (string == "negativethinmathspace")
length = -3;
else if (string == "negativemediummathspace")
length = -4;
else if (string == "negativethickmathspace")
length = -5;
else if (string == "negativeverythickmathspace")
length = -6;
else if (string == "negativeveryverythickmathspace")
length = -7;
}
if (length) {
lengthValue = length * style->font().size() / 18;
return true;
}
return false;
}
int RenderMathMLTable::firstLineBaseline() const
{
return (logicalHeight() + style().fontMetrics().xHeight()) / 2;
}
}
#endif