RenderMathMLFenced.cpp [plain text]
#include "config.h"
#if ENABLE(MATHML)
#include "RenderMathMLFenced.h"
#include "FontSelector.h"
#include "MathMLNames.h"
#include "RenderInline.h"
#include "RenderMathMLOperator.h"
#include "RenderText.h"
#include <wtf/text/StringBuilder.h>
namespace WebCore {
using namespace MathMLNames;
enum Braces { OpeningBraceChar = 0x28, ClosingBraceChar = 0x29 };
static const float gSeparatorMarginEndEms = 0.25f;
static const float gFenceMarginEms = 0.1f;
RenderMathMLFenced::RenderMathMLFenced(Element* element)
: RenderMathMLRow(element)
, m_open(OpeningBraceChar)
, m_close(ClosingBraceChar)
, m_closeFenceRenderer(0)
{
}
void RenderMathMLFenced::updateFromElement()
{
Element* fenced = toElement(node());
AtomicString openValue = fenced->getAttribute(MathMLNames::openAttr);
if (openValue.length() > 0)
m_open = openValue[0];
AtomicString closeValue = fenced->getAttribute(MathMLNames::closeAttr);
if (closeValue.length() > 0)
m_close = closeValue[0];
AtomicString separators = fenced->getAttribute(MathMLNames::separatorsAttr);
if (!separators.isNull()) {
StringBuilder characters;
for (unsigned int i = 0; i < separators.length(); i++) {
if (!isSpaceOrNewline(separators[i]))
characters.append(separators[i]);
}
m_separators = !characters.length() ? 0 : characters.toString().impl();
} else {
m_separators = StringImpl::create(",");
}
if (isEmpty())
makeFences();
}
RenderMathMLOperator* RenderMathMLFenced::createMathMLOperator(UChar uChar, RenderMathMLOperator::OperatorType operatorType)
{
RefPtr<RenderStyle> newStyle = RenderStyle::createAnonymousStyleWithDisplay(style(), FLEX);
newStyle->setFlexDirection(FlowColumn);
newStyle->setMarginEnd(Length((operatorType == RenderMathMLOperator::Fence ? gFenceMarginEms : gSeparatorMarginEndEms) * style()->fontSize(), Fixed));
if (operatorType == RenderMathMLOperator::Fence)
newStyle->setMarginStart(Length(gFenceMarginEms * style()->fontSize(), Fixed));
RenderMathMLOperator* newOperator = new (renderArena()) RenderMathMLOperator(toElement(node()), uChar);
newOperator->setOperatorType(operatorType);
newOperator->setStyle(newStyle.release());
return newOperator;
}
void RenderMathMLFenced::makeFences()
{
RenderMathMLRow::addChild(createMathMLOperator(m_open, RenderMathMLOperator::Fence), firstChild());
m_closeFenceRenderer = createMathMLOperator(m_close, RenderMathMLOperator::Fence);
RenderMathMLRow::addChild(m_closeFenceRenderer);
}
void RenderMathMLFenced::addChild(RenderObject* child, RenderObject* beforeChild)
{
if (isEmpty())
updateFromElement();
RenderObject* separatorRenderer = 0;
if (m_separators.get()) {
unsigned int count = 0;
for (Node* position = child->node(); position; position = position->previousSibling()) {
if (position->isElementNode())
count++;
}
if (!beforeChild) {
--count;
}
if (count > 0) {
UChar separator;
if (count > m_separators.get()->length())
separator = (*m_separators.get())[m_separators.get()->length() - 1];
else
separator = (*m_separators.get())[count - 1];
separatorRenderer = createMathMLOperator(separator, RenderMathMLOperator::Separator);
}
}
if (beforeChild) {
RenderMathMLRow::addChild(child, beforeChild);
if (separatorRenderer)
RenderMathMLRow::addChild(separatorRenderer, beforeChild);
} else {
if (separatorRenderer)
RenderMathMLRow::addChild(separatorRenderer, m_closeFenceRenderer);
RenderMathMLRow::addChild(child, m_closeFenceRenderer);
}
}
void RenderMathMLFenced::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
{
RenderMathMLBlock::styleDidChange(diff, oldStyle);
for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
if (child->node() == node()) {
ASSERT(child->style()->refCount() == 1);
child->style()->inheritFrom(style());
bool isFence = child == firstChild() || child == lastChild();
child->style()->setMarginEnd(Length((isFence ? gFenceMarginEms : gSeparatorMarginEndEms) * style()->fontSize(), Fixed));
if (isFence)
child->style()->setMarginStart(Length(gFenceMarginEms * style()->fontSize(), Fixed));
}
}
}
}
#endif