RenderMathMLOperator.cpp [plain text]
#include "config.h"
#if ENABLE(MATHML)
#include "RenderMathMLOperator.h"
#include "FontSelector.h"
#include "MathMLNames.h"
#include "RenderText.h"
namespace WebCore {
using namespace MathMLNames;
RenderMathMLOperator::RenderMathMLOperator(Node* container)
: RenderMathMLBlock(container),
m_stretchHeight(0),
m_operator(0)
{
}
RenderMathMLOperator::RenderMathMLOperator(Node* container, UChar operatorChar)
: RenderMathMLBlock(container),
m_stretchHeight(0),
m_operator(operatorChar)
{
}
bool RenderMathMLOperator::isChildAllowed(RenderObject*, RenderStyle*) const
{
return false;
}
void RenderMathMLOperator::stretchToHeight(int height)
{
if (height == m_stretchHeight)
return;
m_stretchHeight = height;
updateBoxModelInfoFromStyle();
setNeedsLayoutAndPrefWidthsRecalc();
markContainingBlocksForLayout();
}
void RenderMathMLOperator::layout()
{
updateFromElement();
RenderBlock::layout();
}
static struct StretchyCharacter {
UChar character;
UChar topGlyph;
UChar extensionGlyph;
UChar bottomGlyph;
UChar middleGlyph;
} stretchyCharacters[9] = {
{ 0x28 , 0x239b, 0x239c, 0x239d, 0x0 }, { 0x29 , 0x239e, 0x239f, 0x23a0, 0x0 }, { 0x5b , 0x23a1, 0x23a2, 0x23a3, 0x0 }, { 0x5d , 0x23a4, 0x23a5, 0x23a6, 0x0 }, { 0x7b , 0x23a7, 0x23aa, 0x23a9, 0x23a8 }, { 0x7c , 0x23d0, 0x23d0, 0x23d0, 0x0 }, { 0x7d , 0x23ab, 0x23aa, 0x23ad, 0x23ac }, { 0x222b, 0x2320, 0x23ae, 0x2321, 0x0 } };
static const int gGlyphFontSize = 14;
static const int gGlyphLineHeight = 12;
static const int gMinimumStretchHeight = 24;
static const int gGlyphHeight = 10;
static const int gMiddleGlyphTopAdjust = -2;
static const int gBottomGlyphTopAdjust = -4;
void RenderMathMLOperator::updateFromElement()
{
while (firstChild()) {
RenderObject* obj = firstChild();
removeChild(obj);
}
UChar firstChar = m_operator;
bool stretchDisabled = false;
if (node()->nodeType() == Node::ELEMENT_NODE) {
if (Element* mo = static_cast<Element*>(node())) {
AtomicString stretchyAttr = mo->getAttribute(MathMLNames::stretchyAttr);
stretchDisabled = equalIgnoringCase(stretchyAttr, "false");
if (!stretchDisabled && !firstChar) {
String opText = mo->textContent();
for (unsigned int i = 0; !firstChar && i < opText.length(); i++) {
if (!isSpaceOrNewline(opText[i]))
firstChar = opText[i];
}
}
}
}
int index = -1;
bool isStretchy = false;
if (!stretchDisabled && firstChar) {
const int maxIndex = sizeof(stretchyCharacters) / sizeof(stretchyCharacters[0]);
for (index++; index < maxIndex; index++) {
if (stretchyCharacters[index].character == firstChar) {
isStretchy = true;
break;
}
}
}
bool shouldStretch = isStretchy && m_stretchHeight>gMinimumStretchHeight;
if (stretchDisabled || !shouldStretch) {
m_isStacked = false;
RenderBlock* container = new (renderArena()) RenderBlock(node());
RefPtr<RenderStyle> newStyle = RenderStyle::create();
newStyle->inheritFrom(style());
newStyle->setDisplay(BLOCK);
int currentFontSize = style()->fontSize();
if (!stretchDisabled && isStretchy && m_stretchHeight > 0 && m_stretchHeight <= gMinimumStretchHeight && m_stretchHeight > currentFontSize) {
FontDescription* desc = new FontDescription();
desc->setIsAbsoluteSize(true);
desc->setSpecifiedSize(m_stretchHeight);
desc->setComputedSize(m_stretchHeight);
newStyle->setFontDescription(*desc);
newStyle->font().update(newStyle->font().fontSelector());
}
newStyle->setVerticalAlign(BASELINE);
container->setStyle(newStyle.release());
addChild(container);
RenderText* text = 0;
if (m_operator)
text = new (renderArena()) RenderText(node(), StringImpl::create(&m_operator, 1));
else if (node()->nodeType() == Node::ELEMENT_NODE)
if (Element* mo = static_cast<Element*>(node()))
text = new (renderArena()) RenderText(node(), StringImpl::create(mo->textContent().characters(), mo->textContent().length()));
if (text) {
text->setStyle(container->style());
container->addChild(text);
}
} else {
m_isStacked = true;
if (stretchyCharacters[index].middleGlyph) {
int half = (m_stretchHeight - gGlyphHeight) / 2;
if (half <= gGlyphHeight) {
createGlyph(stretchyCharacters[index].topGlyph, half);
createGlyph(stretchyCharacters[index].middleGlyph, gGlyphHeight, gMiddleGlyphTopAdjust);
createGlyph(stretchyCharacters[index].bottomGlyph, 0, gBottomGlyphTopAdjust);
} else {
createGlyph(stretchyCharacters[index].topGlyph, gGlyphHeight);
int remaining = half - gGlyphHeight;
while (remaining > 0) {
if (remaining < gGlyphHeight) {
createGlyph(stretchyCharacters[index].extensionGlyph, remaining);
remaining = 0;
} else {
createGlyph(stretchyCharacters[index].extensionGlyph, gGlyphHeight);
remaining -= gGlyphHeight;
}
}
createGlyph(stretchyCharacters[index].middleGlyph, gGlyphHeight, gMiddleGlyphTopAdjust);
remaining = half - gGlyphHeight;
if (m_stretchHeight % 2 == 1)
remaining++;
while (remaining > 0) {
if (remaining < gGlyphHeight) {
createGlyph(stretchyCharacters[index].extensionGlyph, remaining);
remaining = 0;
} else {
createGlyph(stretchyCharacters[index].extensionGlyph, gGlyphHeight);
remaining -= gGlyphHeight;
}
}
createGlyph(stretchyCharacters[index].bottomGlyph, 0, gBottomGlyphTopAdjust);
}
} else {
int remaining = m_stretchHeight - 2 * gGlyphHeight;
createGlyph(stretchyCharacters[index].topGlyph, gGlyphHeight);
while (remaining > 0) {
if (remaining < gGlyphHeight) {
createGlyph(stretchyCharacters[index].extensionGlyph, remaining);
remaining = 0;
} else {
createGlyph(stretchyCharacters[index].extensionGlyph, gGlyphHeight);
remaining -= gGlyphHeight;
}
}
createGlyph(stretchyCharacters[index].bottomGlyph, 0, gBottomGlyphTopAdjust);
}
}
}
RefPtr<RenderStyle> RenderMathMLOperator::createStackableStyle(int size, int topRelative)
{
RefPtr<RenderStyle> newStyle = RenderStyle::create();
newStyle->inheritFrom(style());
newStyle->setDisplay(BLOCK);
FontDescription* desc = new FontDescription();
desc->setIsAbsoluteSize(true);
desc->setSpecifiedSize(gGlyphFontSize);
desc->setComputedSize(gGlyphFontSize);
newStyle->setFontDescription(*desc);
newStyle->font().update(newStyle->font().fontSelector());
newStyle->setLineHeight(Length(gGlyphLineHeight, Fixed));
newStyle->setVerticalAlign(TOP);
if (size > 0)
newStyle->setMaxHeight(Length(size, Fixed));
newStyle->setOverflowY(OHIDDEN);
newStyle->setOverflowX(OHIDDEN);
if (topRelative) {
newStyle->setTop(Length(topRelative, Fixed));
newStyle->setPosition(RelativePosition);
}
return newStyle;
}
RenderBlock* RenderMathMLOperator::createGlyph(UChar glyph, int size, int charRelative, int topRelative)
{
RenderBlock* container = new (renderArena()) RenderBlock(node());
container->setStyle(createStackableStyle(size, topRelative).release());
addChild(container);
RenderBlock* parent = container;
if (charRelative) {
RenderBlock* charBlock = new (renderArena()) RenderBlock(node());
RefPtr<RenderStyle> charStyle = RenderStyle::create();
charStyle->inheritFrom(container->style());
charStyle->setDisplay(INLINE_BLOCK);
charStyle->setTop(Length(charRelative, Fixed));
charStyle->setPosition(RelativePosition);
charBlock->setStyle(charStyle);
parent->addChild(charBlock);
parent = charBlock;
}
RenderText* text = new (renderArena()) RenderText(node(), StringImpl::create(&glyph, 1));
text->setStyle(container->style());
parent->addChild(text);
return container;
}
int RenderMathMLOperator::baselinePosition(bool firstLine, bool isRootLineBox) const
{
return !m_isStacked && firstChild() ? firstChild()->baselinePosition(firstLine, isRootLineBox) : offsetHeight();
}
}
#endif