RenderMathMLSubSup.cpp [plain text]
#include "config.h"
#if ENABLE(MATHML)
#include "RenderMathMLSubSup.h"
#include "FontSelector.h"
#include "MathMLNames.h"
#include "RenderInline.h"
#include "RenderTable.h"
#include "RenderTableCell.h"
#include "RenderTableRow.h"
#include "RenderTableSection.h"
#include "RenderText.h"
namespace WebCore {
using namespace MathMLNames;
static const int gTopAdjustDivisor = 3;
static const int gSubsupScriptMargin = 1;
static const float gSubSupStretch = 1.2;
RenderMathMLSubSup::RenderMathMLSubSup(Element* element)
: RenderMathMLBlock(element)
, m_scripts(0)
{
if (element->hasLocalName(MathMLNames::msubTag))
m_kind = Sub;
else if (element->hasLocalName(MathMLNames::msupTag))
m_kind = Sup;
else if (element->hasLocalName(MathMLNames::msubsupTag))
m_kind = SubSup;
else
m_kind = SubSup;
}
void RenderMathMLSubSup::addChild(RenderObject* child, RenderObject* beforeChild)
{
if (firstChild()) {
if (m_kind == SubSup) {
if (!m_scripts) {
m_scripts = new (renderArena()) RenderMathMLBlock(node());
RefPtr<RenderStyle> scriptsStyle = RenderStyle::create();
scriptsStyle->inheritFrom(style());
scriptsStyle->setDisplay(INLINE_BLOCK);
scriptsStyle->setVerticalAlign(MIDDLE);
scriptsStyle->setMarginLeft(Length(gSubsupScriptMargin, Fixed));
scriptsStyle->setTextAlign(LEFT);
m_scripts->setStyle(scriptsStyle.release());
RenderMathMLBlock::addChild(m_scripts, beforeChild);
}
RenderBlock* script = new (renderArena()) RenderMathMLBlock(node());
RefPtr<RenderStyle> scriptStyle = RenderStyle::create();
scriptStyle->inheritFrom(m_scripts->style());
scriptStyle->setDisplay(BLOCK);
script->setStyle(scriptStyle.release());
m_scripts->addChild(script, m_scripts->firstChild());
script->addChild(child);
} else
RenderMathMLBlock::addChild(child, beforeChild);
} else {
RenderMathMLBlock* wrapper = new (renderArena()) RenderMathMLBlock(node());
RefPtr<RenderStyle> wrapperStyle = RenderStyle::create();
wrapperStyle->inheritFrom(style());
wrapperStyle->setDisplay(INLINE_BLOCK);
wrapperStyle->setVerticalAlign(MIDDLE);
wrapper->setStyle(wrapperStyle.release());
RenderMathMLBlock::addChild(wrapper, beforeChild);
wrapper->addChild(child);
}
}
void RenderMathMLSubSup::stretchToHeight(int height)
{
RenderObject* base = firstChild();
if (!base)
return;
if (base->isRenderMathMLBlock()) {
RenderMathMLBlock* block = toRenderMathMLBlock(base);
block->stretchToHeight(static_cast<int>(gSubSupStretch * height));
}
if (height > 0 && m_kind == SubSup && m_scripts) {
RenderObject* script = m_scripts->firstChild();
if (script) {
RenderObject* top = script;
int topHeight = getBoxModelObjectHeight(top->firstChild());
int topAdjust = topHeight / gTopAdjustDivisor;
top->style()->setMarginTop(Length(-topAdjust, Fixed));
top->style()->setMarginBottom(Length(height - topHeight + topAdjust, Fixed));
if (top->isBoxModelObject()) {
RenderBoxModelObject* topBox = toRenderBoxModelObject(top);
topBox->updateBoxModelInfoFromStyle();
}
m_scripts->setNeedsLayoutAndPrefWidthsRecalc();
m_scripts->markContainingBlocksForLayout();
}
}
updateBoxModelInfoFromStyle();
setNeedsLayoutAndPrefWidthsRecalc();
markContainingBlocksForLayout();
}
int RenderMathMLSubSup::nonOperatorHeight() const
{
return 0;
}
void RenderMathMLSubSup::layout()
{
RenderBlock::layout();
if (m_kind == SubSup) {
int width = 0;
RenderObject* current = firstChild();
while (current) {
width += getBoxModelObjectWidth(current);
current = current->nextSibling();
}
width++;
if (m_scripts)
width += gSubsupScriptMargin;
style()->setWidth(Length(width, Fixed));
setNeedsLayoutAndPrefWidthsRecalc();
markContainingBlocksForLayout();
RenderBlock::layout();
}
}
int RenderMathMLSubSup::baselinePosition(bool firstLine, bool isRootLineBox) const
{
RenderObject* base = firstChild();
if (!base)
return offsetHeight();
base = base->firstChild();
if (!base)
return offsetHeight();
int baseline = offsetHeight();
switch (m_kind) {
case SubSup:
if (m_scripts) {
int topAdjust = 0;
if (base->isBoxModelObject()) {
RenderBoxModelObject* box = toRenderBoxModelObject(base);
topAdjust = (m_scripts->offsetHeight() - box->offsetHeight()) / 2;
}
return topAdjust + (base ? base->baselinePosition(firstLine, isRootLineBox) : 0) + 4;
}
break;
case Sup:
if (base) {
baseline = base->baselinePosition(firstLine, isRootLineBox) + 4;
RenderObject* sup = base->nextSibling();
if (sup && sup->isBoxModelObject()) {
RenderBoxModelObject* box = toRenderBoxModelObject(sup);
baseline += static_cast<int>(box->offsetHeight() * 0.5);
}
baseline++;
}
break;
case Sub:
if (base)
baseline = base->baselinePosition(true) + 4;
}
return baseline;
}
}
#endif // ENABLE(MATHML)