This source file includes following definitions.
- hasRubyText
- hasRubyBase
- isEmpty
- rubyText
- rubyBase
- rubyBaseSafe
- firstLineBlock
- updateFirstLetter
- isChildAllowed
- addChild
- removeChild
- createRubyBase
- staticCreateRubyRun
- layoutSpecialExcludedChild
- layout
- getOverhang
#include "config.h"
#include "core/rendering/RenderRubyRun.h"
#include "core/rendering/LayoutRectRecorder.h"
#include "core/rendering/RenderRubyBase.h"
#include "core/rendering/RenderRubyText.h"
#include "core/rendering/RenderText.h"
using namespace std;
namespace WebCore {
RenderRubyRun::RenderRubyRun()
: RenderBlockFlow(0)
{
setReplaced(true);
setInline(true);
}
RenderRubyRun::~RenderRubyRun()
{
}
bool RenderRubyRun::hasRubyText() const
{
return firstChild() && firstChild()->isRubyText();
}
bool RenderRubyRun::hasRubyBase() const
{
return lastChild() && lastChild()->isRubyBase();
}
bool RenderRubyRun::isEmpty() const
{
return !hasRubyText() && !hasRubyBase();
}
RenderRubyText* RenderRubyRun::rubyText() const
{
RenderObject* child = firstChild();
ASSERT(!child || !child->isRubyText() || !child->isFloatingOrOutOfFlowPositioned());
return child && child->isRubyText() ? static_cast<RenderRubyText*>(child) : 0;
}
RenderRubyBase* RenderRubyRun::rubyBase() const
{
RenderObject* child = lastChild();
return child && child->isRubyBase() ? static_cast<RenderRubyBase*>(child) : 0;
}
RenderRubyBase* RenderRubyRun::rubyBaseSafe()
{
RenderRubyBase* base = rubyBase();
if (!base) {
base = createRubyBase();
RenderBlockFlow::addChild(base);
}
return base;
}
RenderBlock* RenderRubyRun::firstLineBlock() const
{
return 0;
}
void RenderRubyRun::updateFirstLetter()
{
}
bool RenderRubyRun::isChildAllowed(RenderObject* child, RenderStyle*) const
{
return child->isRubyText() || child->isInline();
}
void RenderRubyRun::addChild(RenderObject* child, RenderObject* beforeChild)
{
ASSERT(child);
if (child->isRubyText()) {
if (!beforeChild) {
ASSERT(!hasRubyText());
RenderBlockFlow::addChild(child, firstChild());
} else if (beforeChild->isRubyText()) {
ASSERT(beforeChild->parent() == this);
RenderObject* ruby = parent();
ASSERT(ruby->isRuby());
RenderBlock* newRun = staticCreateRubyRun(ruby);
ruby->addChild(newRun, nextSibling());
RenderBlockFlow::addChild(child, beforeChild);
RenderBlockFlow::removeChild(beforeChild);
newRun->addChild(beforeChild);
} else if (hasRubyBase()) {
RenderObject* ruby = parent();
RenderRubyRun* newRun = staticCreateRubyRun(ruby);
ruby->addChild(newRun, this);
newRun->addChild(child);
rubyBaseSafe()->moveChildren(newRun->rubyBaseSafe(), beforeChild);
}
} else {
if (beforeChild && beforeChild->isRubyText())
beforeChild = 0;
rubyBaseSafe()->addChild(child, beforeChild);
}
}
void RenderRubyRun::removeChild(RenderObject* child)
{
if (!beingDestroyed() && !documentBeingDestroyed() && child->isRubyText()) {
RenderRubyBase* base = rubyBase();
RenderObject* rightNeighbour = nextSibling();
if (base && rightNeighbour && rightNeighbour->isRubyRun()) {
RenderRubyRun* rightRun = toRenderRubyRun(rightNeighbour);
if (rightRun->hasRubyBase()) {
RenderRubyBase* rightBase = rightRun->rubyBaseSafe();
rightBase->moveChildren(base);
moveChildTo(rightRun, base);
rightRun->moveChildTo(this, rightBase);
ASSERT(!rubyBase()->firstChild());
}
}
}
RenderBlockFlow::removeChild(child);
if (!beingDestroyed() && !documentBeingDestroyed()) {
RenderBlock* base = rubyBase();
if (base && !base->firstChild()) {
RenderBlockFlow::removeChild(base);
base->deleteLineBoxTree();
base->destroy();
}
if (isEmpty()) {
parent()->removeChild(this);
deleteLineBoxTree();
destroy();
}
}
}
RenderRubyBase* RenderRubyRun::createRubyBase() const
{
RenderRubyBase* renderer = RenderRubyBase::createAnonymous(&document());
RefPtr<RenderStyle> newStyle = RenderStyle::createAnonymousStyleWithDisplay(style(), BLOCK);
newStyle->setTextAlign(CENTER);
renderer->setStyle(newStyle.release());
return renderer;
}
RenderRubyRun* RenderRubyRun::staticCreateRubyRun(const RenderObject* parentRuby)
{
ASSERT(parentRuby && parentRuby->isRuby());
RenderRubyRun* rr = new RenderRubyRun();
rr->setDocumentForAnonymous(&parentRuby->document());
RefPtr<RenderStyle> newStyle = RenderStyle::createAnonymousStyleWithDisplay(parentRuby->style(), INLINE_BLOCK);
rr->setStyle(newStyle.release());
return rr;
}
RenderObject* RenderRubyRun::layoutSpecialExcludedChild(bool relayoutChildren, SubtreeLayoutScope& layoutScope)
{
RenderRubyText* rt = rubyText();
if (!rt)
return 0;
if (relayoutChildren)
layoutScope.setChildNeedsLayout(rt);
rt->layoutIfNeeded();
return rt;
}
void RenderRubyRun::layout()
{
LayoutRectRecorder recorder(*this);
RenderBlockFlow::layout();
RenderRubyText* rt = rubyText();
if (!rt)
return;
rt->setLogicalLeft(0);
LayoutUnit lastLineRubyTextBottom = rt->logicalHeight();
LayoutUnit firstLineRubyTextTop = 0;
RootInlineBox* rootBox = rt->lastRootBox();
if (rootBox) {
firstLineRubyTextTop = rt->firstRootBox()->logicalTopLayoutOverflow();
lastLineRubyTextBottom = rootBox->logicalBottomLayoutOverflow();
}
if (style()->isFlippedLinesWritingMode() == (style()->rubyPosition() == RubyPositionAfter)) {
LayoutUnit firstLineTop = 0;
if (RenderRubyBase* rb = rubyBase()) {
RootInlineBox* rootBox = rb->firstRootBox();
if (rootBox)
firstLineTop = rootBox->logicalTopLayoutOverflow();
firstLineTop += rb->logicalTop();
}
rt->setLogicalTop(-lastLineRubyTextBottom + firstLineTop);
} else {
LayoutUnit lastLineBottom = logicalHeight();
if (RenderRubyBase* rb = rubyBase()) {
RootInlineBox* rootBox = rb->lastRootBox();
if (rootBox)
lastLineBottom = rootBox->logicalBottomLayoutOverflow();
lastLineBottom += rb->logicalTop();
}
rt->setLogicalTop(-firstLineRubyTextTop + lastLineBottom);
}
computeOverflow(clientLogicalBottom());
}
void RenderRubyRun::getOverhang(bool firstLine, RenderObject* startRenderer, RenderObject* endRenderer, int& startOverhang, int& endOverhang) const
{
ASSERT(!needsLayout());
startOverhang = 0;
endOverhang = 0;
RenderRubyBase* rubyBase = this->rubyBase();
RenderRubyText* rubyText = this->rubyText();
if (!rubyBase || !rubyText)
return;
if (!rubyBase->firstRootBox())
return;
int logicalWidth = this->logicalWidth();
int logicalLeftOverhang = numeric_limits<int>::max();
int logicalRightOverhang = numeric_limits<int>::max();
for (RootInlineBox* rootInlineBox = rubyBase->firstRootBox(); rootInlineBox; rootInlineBox = rootInlineBox->nextRootBox()) {
logicalLeftOverhang = min<int>(logicalLeftOverhang, rootInlineBox->logicalLeft());
logicalRightOverhang = min<int>(logicalRightOverhang, logicalWidth - rootInlineBox->logicalRight());
}
startOverhang = style()->isLeftToRightDirection() ? logicalLeftOverhang : logicalRightOverhang;
endOverhang = style()->isLeftToRightDirection() ? logicalRightOverhang : logicalLeftOverhang;
if (!startRenderer || !startRenderer->isText() || startRenderer->style(firstLine)->fontSize() > rubyBase->style(firstLine)->fontSize())
startOverhang = 0;
if (!endRenderer || !endRenderer->isText() || endRenderer->style(firstLine)->fontSize() > rubyBase->style(firstLine)->fontSize())
endOverhang = 0;
int halfWidthOfFontSize = rubyText->style(firstLine)->fontSize() / 2;
if (startOverhang)
startOverhang = min<int>(startOverhang, min<int>(toRenderText(startRenderer)->minLogicalWidth(), halfWidthOfFontSize));
if (endOverhang)
endOverhang = min<int>(endOverhang, min<int>(toRenderText(endRenderer)->minLogicalWidth(), halfWidthOfFontSize));
}
}