#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/IsoMallocInlines.h>
#include <wtf/StackStats.h>
namespace WebCore {
WTF_MAKE_ISO_ALLOCATED_IMPL(RenderRubyRun);
RenderRubyRun::RenderRubyRun(Document& document, RenderStyle&& style)
: RenderBlockFlow(document, WTFMove(style))
, m_lastCharacter(0)
, m_secondToLastCharacter(0)
{
setReplaced(true);
setInline(true);
}
RenderRubyRun::~RenderRubyRun() = default;
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) : nullptr;
}
RenderRubyBase* RenderRubyRun::rubyBase() const
{
RenderObject* child = lastChild();
return child && child->isRubyBase() ? static_cast<RenderRubyBase*>(child) : nullptr;
}
RenderBlock* RenderRubyRun::firstLineBlock() const
{
return nullptr;
}
bool RenderRubyRun::isChildAllowed(const RenderObject& child, const RenderStyle&) const
{
return child.isInline() || child.isRubyText();
}
RenderPtr<RenderRubyBase> RenderRubyRun::createRubyBase() const
{
auto newStyle = RenderStyle::createAnonymousStyleWithDisplay(style(), DisplayType::Block);
newStyle.setTextAlign(TextAlignMode::Center); auto renderer = createRenderer<RenderRubyBase>(document(), WTFMove(newStyle));
renderer->initializeStyle();
return renderer;
}
RenderPtr<RenderRubyRun> RenderRubyRun::staticCreateRubyRun(const RenderObject* parentRuby)
{
ASSERT(isRuby(parentRuby));
auto renderer = createRenderer<RenderRubyRun>(parentRuby->document(), RenderStyle::createAnonymousStyleWithDisplay(parentRuby->style(), DisplayType::InlineBlock));
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() == TextAlignMode::Justify;
}
RenderBlockFlow::layoutBlock(relayoutChildren, pageHeight);
RenderRubyText* rt = rubyText();
if (!rt)
return;
rt->setLogicalLeft(0);
LayoutUnit lastLineRubyTextBottom = rt->logicalHeight();
LayoutUnit firstLineRubyTextTop;
RootInlineBox* rootBox = rt->lastRootBox();
if (rootBox) {
firstLineRubyTextTop = rt->firstRootBox()->logicalTopLayoutOverflow();
lastLineRubyTextBottom = rootBox->logicalBottomLayoutOverflow();
}
if (isHorizontalWritingMode() && rt->style().rubyPosition() == RubyPosition::InterCharacter) {
const FontCascade& font = style().fontCascade();
float distanceBetweenBase = std::max(font.letterSpacing(), 2.0f * rt->style().fontCascade().fontMetrics().height());
setWidth(width() + distanceBetweenBase - font.letterSpacing());
if (RenderRubyBase* rb = rubyBase()) {
LayoutUnit firstLineTop;
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() == RubyPosition::After)) {
LayoutUnit firstLineTop;
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);
}
}