#include "config.h"
#include "RenderRubyRun.h"
#include "RenderRuby.h"
#include "RenderRubyBase.h"
#include "RenderRubyText.h"
#include "RenderText.h"
#include "RenderView.h"
#include "StyleInheritedData.h"
#include <wtf/StackStats.h>
namespace WebCore {
using namespace std;
RenderRubyRun::RenderRubyRun(Document& document, RenderStyle&& style)
: RenderBlockFlow(document, WTFMove(style))
, m_lastCharacter(0)
, m_secondToLastCharacter(0)
{
setReplaced(true);
setInline(true);
}
RenderRubyRun::~RenderRubyRun()
{
}
bool RenderRubyRun::hasRubyText() const
{
return firstChild() && firstChild()->isRubyText();
}
bool RenderRubyRun::hasRubyBase() const
{
return lastChild() && lastChild()->isRubyBase();
}
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(RenderTreeMutationIsAllowed)
{
}
bool RenderRubyRun::isChildAllowed(const RenderObject& child, const RenderStyle&) const
{
return child.isInline() || child.isRubyText();
}
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);
RenderElement* ruby = parent();
ASSERT(isRuby(ruby));
RenderBlock* newRun = staticCreateRubyRun(ruby);
ruby->addChild(newRun, nextSibling());
RenderBlockFlow::addChild(child, beforeChild);
RenderBlockFlow::removeChild(*beforeChild);
newRun->addChild(beforeChild);
} else if (hasRubyBase()) {
RenderElement* 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() && !renderTreeBeingDestroyed() && child.isRubyText()) {
RenderRubyBase* base = rubyBase();
RenderObject* rightNeighbour = nextSibling();
if (base && is<RenderRubyRun>(rightNeighbour)) {
RenderRubyRun& rightRun = downcast<RenderRubyRun>(*rightNeighbour);
if (rightRun.hasRubyBase()) {
RenderRubyBase* rightBase = rightRun.rubyBaseSafe();
rightBase->mergeChildrenWithBase(*base);
moveChildTo(&rightRun, base);
rightRun.moveChildTo(this, rightBase);
ASSERT(!rubyBase()->firstChild());
}
}
}
RenderBlockFlow::removeChild(child);
if (!beingDestroyed() && !renderTreeBeingDestroyed()) {
RenderBlock* base = rubyBase();
if (base && !base->firstChild()) {
RenderBlockFlow::removeChild(*base);
base->deleteLines();
base->destroy();
}
if (!hasRubyText() && !hasRubyBase()) {
parent()->removeChild(*this);
deleteLines();
destroy();
}
}
}
RenderRubyBase* RenderRubyRun::createRubyBase() const
{
auto newStyle = RenderStyle::createAnonymousStyleWithDisplay(style(), BLOCK);
newStyle.setTextAlign(CENTER); auto renderer = new RenderRubyBase(document(), WTFMove(newStyle));
renderer->initializeStyle();
return renderer;
}
RenderRubyRun* RenderRubyRun::staticCreateRubyRun(const RenderObject* parentRuby)
{
ASSERT(isRuby(parentRuby));
auto renderer = new RenderRubyRun(parentRuby->document(), RenderStyle::createAnonymousStyleWithDisplay(parentRuby->style(), INLINE_BLOCK));
renderer->initializeStyle();
return renderer;
}
void RenderRubyRun::layoutExcludedChildren(bool relayoutChildren)
{
RenderBlockFlow::layoutExcludedChildren(relayoutChildren);
StackStats::LayoutCheckPoint layoutCheckPoint;
RenderRubyText* rt = rubyText();
if (!rt)
return;
rt->setIsExcludedFromNormalLayout(true);
if (relayoutChildren)
rt->setChildNeedsLayout(MarkOnlyThis);
rt->layoutIfNeeded();
}
void RenderRubyRun::layout()
{
if (RenderRubyBase* base = rubyBase())
base->reset();
RenderBlockFlow::layout();
}
void RenderRubyRun::layoutBlock(bool relayoutChildren, LayoutUnit pageHeight)
{
if (!relayoutChildren) {
relayoutChildren = style().textAlign() == JUSTIFY;
}
RenderBlockFlow::layoutBlock(relayoutChildren, pageHeight);
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 (isHorizontalWritingMode() && rt->style().rubyPosition() == RubyPositionInterCharacter) {
const FontCascade& font = style().fontCascade();
float distanceBetweenBase = max(font.letterSpacing(), 2.0f * rt->style().fontCascade().fontMetrics().height());
setWidth(width() + distanceBetweenBase - font.letterSpacing());
if (RenderRubyBase* rb = rubyBase()) {
LayoutUnit firstLineTop = 0;
LayoutUnit lastLineBottom = logicalHeight();
RootInlineBox* rootBox = rb->firstRootBox();
if (rootBox)
firstLineTop = rootBox->logicalTopLayoutOverflow();
firstLineTop += rb->logicalTop();
if (rootBox)
lastLineBottom = rootBox->logicalBottomLayoutOverflow();
lastLineBottom += rb->logicalTop();
rt->setX(rb->x() + rb->width() - font.letterSpacing());
LayoutUnit extent = lastLineBottom - firstLineTop;
rt->setY(firstLineTop + (extent - rt->height()) / 2);
}
} else 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());
}
static bool shouldOverhang(bool firstLine, const RenderObject* renderer, const RenderRubyBase& rubyBase)
{
if (!renderer || !renderer->isText())
return false;
const RenderStyle& rubyBaseStyle = firstLine ? rubyBase.firstLineStyle() : rubyBase.style();
const RenderStyle& style = firstLine ? renderer->firstLineStyle() : renderer->style();
return style.computedFontPixelSize() <= rubyBaseStyle.computedFontPixelSize();
}
void RenderRubyRun::getOverhang(bool firstLine, RenderObject* startRenderer, RenderObject* endRenderer, float& startOverhang, float& endOverhang) const
{
ASSERT(!needsLayout());
startOverhang = 0;
endOverhang = 0;
RenderRubyBase* rubyBase = this->rubyBase();
RenderRubyText* rubyText = this->rubyText();
if (!rubyBase || !rubyText)
return;
if (!rubyBase->firstRootBox())
return;
LayoutUnit logicalWidth = this->logicalWidth();
float logicalLeftOverhang = std::numeric_limits<float>::max();
float logicalRightOverhang = std::numeric_limits<float>::max();
for (RootInlineBox* rootInlineBox = rubyBase->firstRootBox(); rootInlineBox; rootInlineBox = rootInlineBox->nextRootBox()) {
logicalLeftOverhang = std::min<float>(logicalLeftOverhang, rootInlineBox->logicalLeft());
logicalRightOverhang = std::min<float>(logicalRightOverhang, logicalWidth - rootInlineBox->logicalRight());
}
startOverhang = style().isLeftToRightDirection() ? logicalLeftOverhang : logicalRightOverhang;
endOverhang = style().isLeftToRightDirection() ? logicalRightOverhang : logicalLeftOverhang;
if (!shouldOverhang(firstLine, startRenderer, *rubyBase))
startOverhang = 0;
if (!shouldOverhang(firstLine, endRenderer, *rubyBase))
endOverhang = 0;
const RenderStyle& rubyTextStyle = firstLine ? rubyText->firstLineStyle() : rubyText->style();
float halfWidthOfFontSize = rubyTextStyle.computedFontPixelSize() / 2.;
if (startOverhang)
startOverhang = std::min(startOverhang, std::min(downcast<RenderText>(*startRenderer).minLogicalWidth(), halfWidthOfFontSize));
if (endOverhang)
endOverhang = std::min(endOverhang, std::min(downcast<RenderText>(*endRenderer).minLogicalWidth(), halfWidthOfFontSize));
}
void RenderRubyRun::updatePriorContextFromCachedBreakIterator(LazyLineBreakIterator& iterator) const
{
iterator.setPriorContext(m_lastCharacter, m_secondToLastCharacter);
}
bool RenderRubyRun::canBreakBefore(const LazyLineBreakIterator& iterator) const
{
RenderRubyText* rubyText = this->rubyText();
if (!rubyText)
return true;
return rubyText->canBreakBefore(iterator);
}
}