#include "config.h"
#include "RootInlineBox.h"
#include "BidiResolver.h"
#include "Chrome.h"
#include "ChromeClient.h"
#include "Document.h"
#include "EllipsisBox.h"
#include "Frame.h"
#include "GraphicsContext.h"
#include "HitTestResult.h"
#include "InlineTextBox.h"
#include "Page.h"
#include "PaintInfo.h"
#include "RenderArena.h"
#include "RenderBlock.h"
#include "RenderView.h"
#include "VerticalPositionCache.h"
#include <wtf/unicode/Unicode.h>
using namespace std;
namespace WebCore {
typedef WTF::HashMap<const RootInlineBox*, EllipsisBox*> EllipsisBoxMap;
static EllipsisBoxMap* gEllipsisBoxMap = 0;
RootInlineBox::RootInlineBox(RenderBlock* block)
: InlineFlowBox(block)
, m_lineBreakPos(0)
, m_lineBreakObj(0)
, m_lineTop(0)
, m_lineBottom(0)
, m_lineTopWithLeading(0)
, m_lineBottomWithLeading(0)
, m_paginationStrut(0)
, m_paginatedLineWidth(0)
{
setIsHorizontal(block->isHorizontalWritingMode());
}
void RootInlineBox::destroy(RenderArena* arena)
{
detachEllipsisBox(arena);
InlineFlowBox::destroy(arena);
}
void RootInlineBox::detachEllipsisBox(RenderArena* arena)
{
if (hasEllipsisBox()) {
EllipsisBox* box = gEllipsisBoxMap->take(this);
box->setParent(0);
box->destroy(arena);
setHasEllipsisBox(false);
}
}
RenderLineBoxList* RootInlineBox::rendererLineBoxes() const
{
return block()->lineBoxes();
}
void RootInlineBox::clearTruncation()
{
if (hasEllipsisBox()) {
detachEllipsisBox(renderer()->renderArena());
InlineFlowBox::clearTruncation();
}
}
bool RootInlineBox::isHyphenated() const
{
for (InlineBox* box = firstLeafChild(); box; box = box->nextLeafChild()) {
if (box->isInlineTextBox()) {
if (toInlineTextBox(box)->hasHyphen())
return true;
}
}
return false;
}
LayoutUnit RootInlineBox::baselinePosition(FontBaseline baselineType) const
{
return boxModelObject()->baselinePosition(baselineType, isFirstLineStyle(), isHorizontal() ? HorizontalLine : VerticalLine, PositionOfInteriorLineBoxes);
}
LayoutUnit RootInlineBox::lineHeight() const
{
return boxModelObject()->lineHeight(isFirstLineStyle(), isHorizontal() ? HorizontalLine : VerticalLine, PositionOfInteriorLineBoxes);
}
bool RootInlineBox::lineCanAccommodateEllipsis(bool ltr, int blockEdge, int lineBoxEdge, int ellipsisWidth)
{
int delta = ltr ? lineBoxEdge - blockEdge : blockEdge - lineBoxEdge;
if (logicalWidth() - delta < ellipsisWidth)
return false;
return InlineFlowBox::canAccommodateEllipsis(ltr, blockEdge, ellipsisWidth);
}
float RootInlineBox::placeEllipsis(const AtomicString& ellipsisStr, bool ltr, float blockLeftEdge, float blockRightEdge, float ellipsisWidth,
InlineBox* markupBox)
{
EllipsisBox* ellipsisBox = new (renderer()->renderArena()) EllipsisBox(renderer(), ellipsisStr, this,
ellipsisWidth - (markupBox ? markupBox->logicalWidth() : 0), logicalHeight(),
y(), !prevRootBox(), isHorizontal(), markupBox);
if (!gEllipsisBoxMap)
gEllipsisBoxMap = new EllipsisBoxMap();
gEllipsisBoxMap->add(this, ellipsisBox);
setHasEllipsisBox(true);
if (ltr && (x() + logicalWidth() + ellipsisWidth) <= blockRightEdge) {
ellipsisBox->setX(x() + logicalWidth());
return logicalWidth() + ellipsisWidth;
}
bool foundBox = false;
float truncatedWidth = 0;
float position = placeEllipsisBox(ltr, blockLeftEdge, blockRightEdge, ellipsisWidth, truncatedWidth, foundBox);
ellipsisBox->setX(position);
return truncatedWidth;
}
float RootInlineBox::placeEllipsisBox(bool ltr, float blockLeftEdge, float blockRightEdge, float ellipsisWidth, float &truncatedWidth, bool& foundBox)
{
float result = InlineFlowBox::placeEllipsisBox(ltr, blockLeftEdge, blockRightEdge, ellipsisWidth, truncatedWidth, foundBox);
if (result == -1) {
result = ltr ? blockRightEdge - ellipsisWidth : blockLeftEdge;
truncatedWidth = blockRightEdge - blockLeftEdge;
}
return result;
}
void RootInlineBox::paintEllipsisBox(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit lineTop, LayoutUnit lineBottom) const
{
if (hasEllipsisBox() && paintInfo.shouldPaintWithinRoot(renderer()) && renderer()->style()->visibility() == VISIBLE
&& paintInfo.phase == PaintPhaseForeground)
ellipsisBox()->paint(paintInfo, paintOffset, lineTop, lineBottom);
}
#if PLATFORM(MAC)
void RootInlineBox::addHighlightOverflow()
{
Frame* frame = renderer()->frame();
if (!frame)
return;
Page* page = frame->page();
if (!page)
return;
FloatRect rootRect(0, selectionTop(), logicalWidth(), selectionHeight());
IntRect inflatedRect = enclosingIntRect(page->chrome()->client()->customHighlightRect(renderer()->node(), renderer()->style()->highlight(), rootRect));
setOverflowFromLogicalRects(inflatedRect, inflatedRect, lineTop(), lineBottom());
}
void RootInlineBox::paintCustomHighlight(PaintInfo& paintInfo, const LayoutPoint& paintOffset, const AtomicString& highlightType)
{
if (!paintInfo.shouldPaintWithinRoot(renderer()) || renderer()->style()->visibility() != VISIBLE || paintInfo.phase != PaintPhaseForeground)
return;
Frame* frame = renderer()->frame();
if (!frame)
return;
Page* page = frame->page();
if (!page)
return;
FloatRect rootRect(paintOffset.x() + x(), paintOffset.y() + selectionTop(), logicalWidth(), selectionHeight());
FloatRect inflatedRect = page->chrome()->client()->customHighlightRect(renderer()->node(), highlightType, rootRect);
if (inflatedRect.intersects(paintInfo.rect))
page->chrome()->client()->paintCustomHighlight(renderer()->node(), highlightType, rootRect, rootRect, false, true);
}
#endif
void RootInlineBox::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit lineTop, LayoutUnit lineBottom)
{
InlineFlowBox::paint(paintInfo, paintOffset, lineTop, lineBottom);
paintEllipsisBox(paintInfo, paintOffset, lineTop, lineBottom);
#if PLATFORM(MAC)
RenderStyle* styleToUse = renderer()->style(isFirstLineStyle());
if (styleToUse->highlight() != nullAtom && !paintInfo.context->paintingDisabled())
paintCustomHighlight(paintInfo, paintOffset, styleToUse->highlight());
#endif
}
bool RootInlineBox::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const LayoutPoint& pointInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit lineTop, LayoutUnit lineBottom)
{
if (hasEllipsisBox() && visibleToHitTesting()) {
if (ellipsisBox()->nodeAtPoint(request, result, pointInContainer, accumulatedOffset, lineTop, lineBottom)) {
renderer()->updateHitTestResult(result, pointInContainer - toLayoutSize(accumulatedOffset));
return true;
}
}
return InlineFlowBox::nodeAtPoint(request, result, pointInContainer, accumulatedOffset, lineTop, lineBottom);
}
void RootInlineBox::adjustPosition(float dx, float dy)
{
InlineFlowBox::adjustPosition(dx, dy);
LayoutUnit blockDirectionDelta = isHorizontal() ? dy : dx; m_lineTop += blockDirectionDelta;
m_lineBottom += blockDirectionDelta;
m_lineTopWithLeading += blockDirectionDelta;
m_lineBottomWithLeading += blockDirectionDelta;
if (hasEllipsisBox())
ellipsisBox()->adjustPosition(dx, dy);
}
void RootInlineBox::childRemoved(InlineBox* box)
{
if (box->renderer() == m_lineBreakObj)
setLineBreakInfo(0, 0, BidiStatus());
for (RootInlineBox* prev = prevRootBox(); prev && prev->lineBreakObj() == box->renderer(); prev = prev->prevRootBox()) {
prev->setLineBreakInfo(0, 0, BidiStatus());
prev->markDirty();
}
}
LayoutUnit RootInlineBox::alignBoxesInBlockDirection(LayoutUnit heightOfBlock, GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache)
{
#if ENABLE(SVG)
if (isSVGRootInlineBox())
return 0;
#endif
LayoutUnit maxPositionTop = 0;
LayoutUnit maxPositionBottom = 0;
LayoutUnit maxAscent = 0;
LayoutUnit maxDescent = 0;
bool setMaxAscent = false;
bool setMaxDescent = false;
bool noQuirksMode = renderer()->document()->inNoQuirksMode();
m_baselineType = requiresIdeographicBaseline(textBoxDataMap) ? IdeographicBaseline : AlphabeticBaseline;
computeLogicalBoxHeights(this, maxPositionTop, maxPositionBottom, maxAscent, maxDescent, setMaxAscent, setMaxDescent, noQuirksMode,
textBoxDataMap, baselineType(), verticalPositionCache);
if (maxAscent + maxDescent < max(maxPositionTop, maxPositionBottom))
adjustMaxAscentAndDescent(maxAscent, maxDescent, maxPositionTop, maxPositionBottom);
LayoutUnit maxHeight = maxAscent + maxDescent;
LayoutUnit lineTop = heightOfBlock;
LayoutUnit lineBottom = heightOfBlock;
LayoutUnit lineTopIncludingMargins = heightOfBlock;
LayoutUnit lineBottomIncludingMargins = heightOfBlock;
bool setLineTop = false;
bool hasAnnotationsBefore = false;
bool hasAnnotationsAfter = false;
placeBoxesInBlockDirection(heightOfBlock, maxHeight, maxAscent, noQuirksMode, lineTop, lineBottom, setLineTop,
lineTopIncludingMargins, lineBottomIncludingMargins, hasAnnotationsBefore, hasAnnotationsAfter, baselineType());
m_hasAnnotationsBefore = hasAnnotationsBefore;
m_hasAnnotationsAfter = hasAnnotationsAfter;
maxHeight = max<LayoutUnit>(0, maxHeight);
setLineTopBottomPositions(lineTop, lineBottom, heightOfBlock, heightOfBlock + maxHeight);
setPaginatedLineWidth(block()->availableLogicalWidthForContent(heightOfBlock));
LayoutUnit annotationsAdjustment = beforeAnnotationsAdjustment();
if (annotationsAdjustment) {
adjustBlockDirectionPosition(annotationsAdjustment);
heightOfBlock += annotationsAdjustment;
}
LayoutUnit gridSnapAdjustment = lineSnapAdjustment();
if (gridSnapAdjustment) {
adjustBlockDirectionPosition(gridSnapAdjustment);
heightOfBlock += gridSnapAdjustment;
}
return heightOfBlock + maxHeight;
}
LayoutUnit RootInlineBox::beforeAnnotationsAdjustment() const
{
LayoutUnit result = 0;
if (!renderer()->style()->isFlippedLinesWritingMode()) {
if (prevRootBox() && prevRootBox()->hasAnnotationsAfter())
result = prevRootBox()->computeUnderAnnotationAdjustment(lineTop());
if (!hasAnnotationsBefore())
return result;
LayoutUnit highestAllowedPosition = prevRootBox() ? min(prevRootBox()->lineBottom(), lineTop()) + result : static_cast<LayoutUnit>(block()->borderBefore());
result = computeOverAnnotationAdjustment(highestAllowedPosition);
} else {
if (hasAnnotationsBefore())
result = computeUnderAnnotationAdjustment(prevRootBox() ? prevRootBox()->lineBottom() : static_cast<LayoutUnit>(block()->borderBefore()));
if (!prevRootBox() || !prevRootBox()->hasAnnotationsAfter())
return result;
LayoutUnit lowestAllowedPosition = max(prevRootBox()->lineBottom(), lineTop()) - result;
result = prevRootBox()->computeOverAnnotationAdjustment(lowestAllowedPosition);
}
return result;
}
LayoutUnit RootInlineBox::lineSnapAdjustment(LayoutUnit delta) const
{
if (block()->style()->lineSnap() == LineSnapNone)
return 0;
LayoutState* layoutState = block()->view()->layoutState();
RenderBlock* lineGrid = layoutState->lineGrid();
LayoutSize lineGridOffset = layoutState->lineGridOffset();
if (!lineGrid || lineGrid->style()->writingMode() != block()->style()->writingMode())
return 0;
RootInlineBox* lineGridBox = lineGrid->lineGridBox();
if (!lineGridBox)
return 0;
LayoutUnit lineGridBlockOffset = lineGrid->isHorizontalWritingMode() ? lineGridOffset.height() : lineGridOffset.width();
LayoutUnit blockOffset = block()->isHorizontalWritingMode() ? layoutState->layoutOffset().height() : layoutState->layoutOffset().width();
LayoutUnit gridLineHeight = lineGridBox->lineBottomWithLeading() - lineGridBox->lineTopWithLeading();
if (!gridLineHeight)
return 0;
LayoutUnit lineGridFontAscent = lineGrid->style()->fontMetrics().ascent(baselineType());
LayoutUnit lineGridFontHeight = lineGridBox->logicalHeight();
LayoutUnit firstTextTop = lineGridBlockOffset + lineGridBox->logicalTop();
LayoutUnit firstLineTopWithLeading = lineGridBlockOffset + lineGridBox->lineTopWithLeading();
LayoutUnit firstBaselinePosition = firstTextTop + lineGridFontAscent;
LayoutUnit currentTextTop = blockOffset + logicalTop() + delta;
LayoutUnit currentFontAscent = block()->style()->fontMetrics().ascent(baselineType());
LayoutUnit currentBaselinePosition = currentTextTop + currentFontAscent;
LayoutUnit lineGridPaginationOrigin = isHorizontal() ? layoutState->lineGridPaginationOrigin().height() : layoutState->lineGridPaginationOrigin().width();
LayoutUnit pageLogicalTop = 0;
if (layoutState->isPaginated() && layoutState->pageLogicalHeight()) {
pageLogicalTop = block()->pageLogicalTopForOffset(lineTopWithLeading() + delta);
if (pageLogicalTop > firstLineTopWithLeading)
firstTextTop = pageLogicalTop + lineGridBox->logicalTop() - lineGrid->borderBefore() - lineGrid->paddingBefore() + lineGridPaginationOrigin;
}
if (block()->style()->lineSnap() == LineSnapContain) {
if (logicalHeight() <= lineGridFontHeight)
firstTextTop += (lineGridFontHeight - logicalHeight()) / 2;
else {
LayoutUnit numberOfLinesWithLeading = ceilf(static_cast<float>(logicalHeight() - lineGridFontHeight) / gridLineHeight);
LayoutUnit totalHeight = lineGridFontHeight + numberOfLinesWithLeading * gridLineHeight;
firstTextTop += (totalHeight - logicalHeight()) / 2;
}
firstBaselinePosition = firstTextTop + currentFontAscent;
} else
firstBaselinePosition = firstTextTop + lineGridFontAscent;
if (currentBaselinePosition < firstBaselinePosition)
return delta + firstBaselinePosition - currentBaselinePosition;
LayoutUnit baselineOffset = currentBaselinePosition - firstBaselinePosition;
LayoutUnit remainder = roundToInt(baselineOffset) % roundToInt(gridLineHeight);
LayoutUnit result = delta;
if (remainder)
result += gridLineHeight - remainder;
if (!layoutState->isPaginated() || !layoutState->pageLogicalHeight() || result == delta)
return result;
LayoutUnit newPageLogicalTop = block()->pageLogicalTopForOffset(lineBottomWithLeading() + result);
if (newPageLogicalTop == pageLogicalTop)
return result;
return lineSnapAdjustment(newPageLogicalTop - (blockOffset + lineTopWithLeading()));
}
GapRects RootInlineBox::lineSelectionGap(RenderBlock* rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock,
LayoutUnit selTop, LayoutUnit selHeight, const PaintInfo* paintInfo)
{
RenderObject::SelectionState lineState = selectionState();
bool leftGap, rightGap;
block()->getSelectionGapInfo(lineState, leftGap, rightGap);
GapRects result;
InlineBox* firstBox = firstSelectedBox();
InlineBox* lastBox = lastSelectedBox();
if (leftGap)
result.uniteLeft(block()->logicalLeftSelectionGap(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock,
firstBox->parent()->renderer(), firstBox->logicalLeft(), selTop, selHeight, paintInfo));
if (rightGap)
result.uniteRight(block()->logicalRightSelectionGap(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock,
lastBox->parent()->renderer(), lastBox->logicalRight(), selTop, selHeight, paintInfo));
if (firstBox && firstBox != lastBox) {
LayoutUnit lastLogicalLeft = firstBox->logicalRight();
bool isPreviousBoxSelected = firstBox->selectionState() != RenderObject::SelectionNone;
for (InlineBox* box = firstBox->nextLeafChild(); box; box = box->nextLeafChild()) {
if (box->selectionState() != RenderObject::SelectionNone) {
LayoutRect logicalRect(lastLogicalLeft, selTop, box->logicalLeft() - lastLogicalLeft, selHeight);
logicalRect.move(renderer()->isHorizontalWritingMode() ? offsetFromRootBlock : LayoutSize(offsetFromRootBlock.height(), offsetFromRootBlock.width()));
LayoutRect gapRect = rootBlock->logicalRectToPhysicalRect(rootBlockPhysicalPosition, logicalRect);
if (isPreviousBoxSelected && gapRect.width() > 0 && gapRect.height() > 0) {
if (paintInfo && box->parent()->renderer()->style()->visibility() == VISIBLE)
paintInfo->context->fillRect(gapRect, box->parent()->renderer()->selectionBackgroundColor(), box->parent()->renderer()->style()->colorSpace());
result.uniteCenter(gapRect);
}
lastLogicalLeft = box->logicalRight();
}
if (box == lastBox)
break;
isPreviousBoxSelected = box->selectionState() != RenderObject::SelectionNone;
}
}
return result;
}
RenderObject::SelectionState RootInlineBox::selectionState()
{
RenderObject::SelectionState state = RenderObject::SelectionNone;
for (InlineBox* box = firstLeafChild(); box; box = box->nextLeafChild()) {
RenderObject::SelectionState boxState = box->selectionState();
if ((boxState == RenderObject::SelectionStart && state == RenderObject::SelectionEnd) ||
(boxState == RenderObject::SelectionEnd && state == RenderObject::SelectionStart))
state = RenderObject::SelectionBoth;
else if (state == RenderObject::SelectionNone ||
((boxState == RenderObject::SelectionStart || boxState == RenderObject::SelectionEnd) &&
(state == RenderObject::SelectionNone || state == RenderObject::SelectionInside)))
state = boxState;
if (state == RenderObject::SelectionBoth)
break;
}
return state;
}
InlineBox* RootInlineBox::firstSelectedBox()
{
for (InlineBox* box = firstLeafChild(); box; box = box->nextLeafChild()) {
if (box->selectionState() != RenderObject::SelectionNone)
return box;
}
return 0;
}
InlineBox* RootInlineBox::lastSelectedBox()
{
for (InlineBox* box = lastLeafChild(); box; box = box->prevLeafChild()) {
if (box->selectionState() != RenderObject::SelectionNone)
return box;
}
return 0;
}
LayoutUnit RootInlineBox::selectionTop() const
{
LayoutUnit selectionTop = m_lineTop;
if (m_hasAnnotationsBefore)
selectionTop -= !renderer()->style()->isFlippedLinesWritingMode() ? computeOverAnnotationAdjustment(m_lineTop) : computeUnderAnnotationAdjustment(m_lineTop);
if (renderer()->style()->isFlippedLinesWritingMode())
return selectionTop;
LayoutUnit prevBottom = prevRootBox() ? prevRootBox()->selectionBottom() : block()->borderBefore() + block()->paddingBefore();
if (prevBottom < selectionTop && block()->containsFloats()) {
LayoutUnit prevLeft = block()->logicalLeftOffsetForLine(prevBottom, false);
LayoutUnit prevRight = block()->logicalRightOffsetForLine(prevBottom, false);
LayoutUnit newLeft = block()->logicalLeftOffsetForLine(selectionTop, false);
LayoutUnit newRight = block()->logicalRightOffsetForLine(selectionTop, false);
if (prevLeft > newLeft || prevRight < newRight)
return selectionTop;
}
return prevBottom;
}
LayoutUnit RootInlineBox::selectionTopAdjustedForPrecedingBlock() const
{
LayoutUnit top = selectionTop();
RenderObject::SelectionState blockSelectionState = root()->block()->selectionState();
if (blockSelectionState != RenderObject::SelectionInside && blockSelectionState != RenderObject::SelectionEnd)
return top;
LayoutSize offsetToBlockBefore;
if (RenderBlock* block = root()->block()->blockBeforeWithinSelectionRoot(offsetToBlockBefore)) {
if (RootInlineBox* lastLine = block->lastRootBox()) {
RenderObject::SelectionState lastLineSelectionState = lastLine->selectionState();
if (lastLineSelectionState != RenderObject::SelectionInside && lastLineSelectionState != RenderObject::SelectionStart)
return top;
LayoutUnit lastLineSelectionBottom = lastLine->selectionBottom() + offsetToBlockBefore.height();
top = max(top, lastLineSelectionBottom);
}
}
return top;
}
LayoutUnit RootInlineBox::selectionBottom() const
{
LayoutUnit selectionBottom = m_lineBottom;
if (m_hasAnnotationsAfter)
selectionBottom += !renderer()->style()->isFlippedLinesWritingMode() ? computeUnderAnnotationAdjustment(m_lineBottom) : computeOverAnnotationAdjustment(m_lineBottom);
if (!renderer()->style()->isFlippedLinesWritingMode() || !nextRootBox())
return selectionBottom;
LayoutUnit nextTop = nextRootBox()->selectionTop();
if (nextTop > selectionBottom && block()->containsFloats()) {
LayoutUnit nextLeft = block()->logicalLeftOffsetForLine(nextTop, false);
LayoutUnit nextRight = block()->logicalRightOffsetForLine(nextTop, false);
LayoutUnit newLeft = block()->logicalLeftOffsetForLine(selectionBottom, false);
LayoutUnit newRight = block()->logicalRightOffsetForLine(selectionBottom, false);
if (nextLeft > newLeft || nextRight < newRight)
return selectionBottom;
}
return nextTop;
}
int RootInlineBox::blockDirectionPointInLine() const
{
return !block()->style()->isFlippedBlocksWritingMode() ? max(lineTop(), selectionTop()) : min(lineBottom(), selectionBottom());
}
RenderBlock* RootInlineBox::block() const
{
return toRenderBlock(renderer());
}
static bool isEditableLeaf(InlineBox* leaf)
{
return leaf && leaf->renderer() && leaf->renderer()->node() && leaf->renderer()->node()->rendererIsEditable();
}
InlineBox* RootInlineBox::closestLeafChildForPoint(const IntPoint& pointInContents, bool onlyEditableLeaves)
{
return closestLeafChildForLogicalLeftPosition(block()->isHorizontalWritingMode() ? pointInContents.x() : pointInContents.y(), onlyEditableLeaves);
}
InlineBox* RootInlineBox::closestLeafChildForLogicalLeftPosition(int leftPosition, bool onlyEditableLeaves)
{
InlineBox* firstLeaf = firstLeafChild();
InlineBox* lastLeaf = lastLeafChild();
if (firstLeaf != lastLeaf) {
if (firstLeaf->isLineBreak())
firstLeaf = firstLeaf->nextLeafChildIgnoringLineBreak();
else if (lastLeaf->isLineBreak())
lastLeaf = lastLeaf->prevLeafChildIgnoringLineBreak();
}
if (firstLeaf == lastLeaf && (!onlyEditableLeaves || isEditableLeaf(firstLeaf)))
return firstLeaf;
if (leftPosition <= firstLeaf->logicalLeft() && !firstLeaf->renderer()->isListMarker() && (!onlyEditableLeaves || isEditableLeaf(firstLeaf)))
return firstLeaf;
if (leftPosition >= lastLeaf->logicalRight() && !lastLeaf->renderer()->isListMarker() && (!onlyEditableLeaves || isEditableLeaf(lastLeaf)))
return lastLeaf;
InlineBox* closestLeaf = 0;
for (InlineBox* leaf = firstLeaf; leaf; leaf = leaf->nextLeafChildIgnoringLineBreak()) {
if (!leaf->renderer()->isListMarker() && (!onlyEditableLeaves || isEditableLeaf(leaf))) {
closestLeaf = leaf;
if (leftPosition < leaf->logicalRight())
return leaf;
}
}
return closestLeaf ? closestLeaf : lastLeaf;
}
BidiStatus RootInlineBox::lineBreakBidiStatus() const
{
return BidiStatus(static_cast<WTF::Unicode::Direction>(m_lineBreakBidiStatusEor), static_cast<WTF::Unicode::Direction>(m_lineBreakBidiStatusLastStrong), static_cast<WTF::Unicode::Direction>(m_lineBreakBidiStatusLast), m_lineBreakContext);
}
void RootInlineBox::setLineBreakInfo(RenderObject* obj, unsigned breakPos, const BidiStatus& status)
{
m_lineBreakObj = obj;
m_lineBreakPos = breakPos;
m_lineBreakBidiStatusEor = status.eor;
m_lineBreakBidiStatusLastStrong = status.lastStrong;
m_lineBreakBidiStatusLast = status.last;
m_lineBreakContext = status.context;
}
EllipsisBox* RootInlineBox::ellipsisBox() const
{
if (!hasEllipsisBox())
return 0;
return gEllipsisBoxMap->get(this);
}
void RootInlineBox::removeLineBoxFromRenderObject()
{
block()->lineBoxes()->removeLineBox(this);
}
void RootInlineBox::extractLineBoxFromRenderObject()
{
block()->lineBoxes()->extractLineBox(this);
}
void RootInlineBox::attachLineBoxToRenderObject()
{
block()->lineBoxes()->attachLineBox(this);
}
LayoutRect RootInlineBox::paddedLayoutOverflowRect(LayoutUnit endPadding) const
{
LayoutRect lineLayoutOverflow = layoutOverflowRect(lineTop(), lineBottom());
if (!endPadding)
return lineLayoutOverflow;
if (isHorizontal()) {
if (isLeftToRightDirection())
lineLayoutOverflow.shiftMaxXEdgeTo(max<LayoutUnit>(lineLayoutOverflow.maxX(), pixelSnappedLogicalRight() + endPadding));
else
lineLayoutOverflow.shiftXEdgeTo(min<LayoutUnit>(lineLayoutOverflow.x(), pixelSnappedLogicalLeft() - endPadding));
} else {
if (isLeftToRightDirection())
lineLayoutOverflow.shiftMaxYEdgeTo(max<LayoutUnit>(lineLayoutOverflow.maxY(), pixelSnappedLogicalRight() + endPadding));
else
lineLayoutOverflow.shiftYEdgeTo(min<LayoutUnit>(lineLayoutOverflow.y(), pixelSnappedLogicalLeft() - endPadding));
}
return lineLayoutOverflow;
}
static void setAscentAndDescent(LayoutUnit& ascent, LayoutUnit& descent, LayoutUnit newAscent, LayoutUnit newDescent, bool& ascentDescentSet)
{
if (!ascentDescentSet) {
ascentDescentSet = true;
ascent = newAscent;
descent = newDescent;
} else {
ascent = max(ascent, newAscent);
descent = max(descent, newDescent);
}
}
void RootInlineBox::ascentAndDescentForBox(InlineBox* box, GlyphOverflowAndFallbackFontsMap& textBoxDataMap, LayoutUnit& ascent, LayoutUnit& descent,
bool& affectsAscent, bool& affectsDescent) const
{
bool ascentDescentSet = false;
if (box->renderer()->isReplaced()) {
if (renderer()->style(isFirstLineStyle())->lineBoxContain() & LineBoxContainReplaced) {
ascent = box->baselinePosition(baselineType());
descent = box->lineHeight() - ascent;
affectsAscent = true;
affectsDescent = true;
}
return;
}
Vector<const SimpleFontData*>* usedFonts = 0;
GlyphOverflow* glyphOverflow = 0;
if (box->isText()) {
GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.find(toInlineTextBox(box));
usedFonts = it == textBoxDataMap.end() ? 0 : &it->second.first;
glyphOverflow = it == textBoxDataMap.end() ? 0 : &it->second.second;
}
bool includeLeading = includeLeadingForBox(box);
bool includeFont = includeFontForBox(box);
bool setUsedFont = false;
bool setUsedFontWithLeading = false;
if (usedFonts && !usedFonts->isEmpty() && (includeFont || (box->renderer()->style(isFirstLineStyle())->lineHeight().isNegative() && includeLeading)) && !box->renderer()->document()->settings()->alwaysUseBaselineOfPrimaryFont()) {
usedFonts->append(box->renderer()->style(isFirstLineStyle())->font().primaryFont());
for (size_t i = 0; i < usedFonts->size(); ++i) {
const FontMetrics& fontMetrics = usedFonts->at(i)->fontMetrics();
LayoutUnit usedFontAscent = fontMetrics.ascent(baselineType());
LayoutUnit usedFontDescent = fontMetrics.descent(baselineType());
LayoutUnit halfLeading = (fontMetrics.lineSpacing() - fontMetrics.height()) / 2;
LayoutUnit usedFontAscentAndLeading = usedFontAscent + halfLeading;
LayoutUnit usedFontDescentAndLeading = fontMetrics.lineSpacing() - usedFontAscentAndLeading;
if (includeFont) {
setAscentAndDescent(ascent, descent, usedFontAscent, usedFontDescent, ascentDescentSet);
setUsedFont = true;
}
if (includeLeading) {
setAscentAndDescent(ascent, descent, usedFontAscentAndLeading, usedFontDescentAndLeading, ascentDescentSet);
setUsedFontWithLeading = true;
}
if (!affectsAscent)
affectsAscent = usedFontAscent - box->logicalTop() > 0;
if (!affectsDescent)
affectsDescent = usedFontDescent + box->logicalTop() > 0;
}
}
if (includeLeading && !setUsedFontWithLeading) {
LayoutUnit ascentWithLeading = box->baselinePosition(baselineType());
LayoutUnit descentWithLeading = box->lineHeight() - ascentWithLeading;
setAscentAndDescent(ascent, descent, ascentWithLeading, descentWithLeading, ascentDescentSet);
affectsAscent = ascentWithLeading - box->logicalTop() > 0;
affectsDescent = descentWithLeading + box->logicalTop() > 0;
}
if (includeFontForBox(box) && !setUsedFont) {
LayoutUnit fontAscent = box->renderer()->style(isFirstLineStyle())->fontMetrics().ascent();
LayoutUnit fontDescent = box->renderer()->style(isFirstLineStyle())->fontMetrics().descent();
setAscentAndDescent(ascent, descent, fontAscent, fontDescent, ascentDescentSet);
affectsAscent = fontAscent - box->logicalTop() > 0;
affectsDescent = fontDescent + box->logicalTop() > 0;
}
if (includeGlyphsForBox(box) && glyphOverflow && glyphOverflow->computeBounds) {
setAscentAndDescent(ascent, descent, glyphOverflow->top, glyphOverflow->bottom, ascentDescentSet);
affectsAscent = glyphOverflow->top - box->logicalTop() > 0;
affectsDescent = glyphOverflow->bottom + box->logicalTop() > 0;
glyphOverflow->top = min(glyphOverflow->top, max(0, glyphOverflow->top - box->renderer()->style(isFirstLineStyle())->fontMetrics().ascent()));
glyphOverflow->bottom = min(glyphOverflow->bottom, max(0, glyphOverflow->bottom - box->renderer()->style(isFirstLineStyle())->fontMetrics().descent()));
}
if (includeMarginForBox(box)) {
LayoutUnit ascentWithMargin = box->renderer()->style(isFirstLineStyle())->fontMetrics().ascent();
LayoutUnit descentWithMargin = box->renderer()->style(isFirstLineStyle())->fontMetrics().descent();
if (box->parent() && !box->renderer()->isText()) {
ascentWithMargin += box->boxModelObject()->borderBefore() + box->boxModelObject()->paddingBefore() + box->boxModelObject()->marginBefore();
descentWithMargin += box->boxModelObject()->borderAfter() + box->boxModelObject()->paddingAfter() + box->boxModelObject()->marginAfter();
}
setAscentAndDescent(ascent, descent, ascentWithMargin, descentWithMargin, ascentDescentSet);
affectsAscent = true;
affectsDescent = true;
}
}
LayoutUnit RootInlineBox::verticalPositionForBox(InlineBox* box, VerticalPositionCache& verticalPositionCache)
{
if (box->renderer()->isText())
return box->parent()->logicalTop();
RenderBoxModelObject* renderer = box->boxModelObject();
ASSERT(renderer->isInline());
if (!renderer->isInline())
return 0;
bool firstLine = isFirstLineStyle();
if (firstLine && !renderer->document()->usesFirstLineRules())
firstLine = false;
bool isRenderInline = renderer->isRenderInline();
if (isRenderInline && !firstLine) {
LayoutUnit verticalPosition = verticalPositionCache.get(renderer, baselineType());
if (verticalPosition != PositionUndefined)
return verticalPosition;
}
LayoutUnit verticalPosition = 0;
EVerticalAlign verticalAlign = renderer->style()->verticalAlign();
if (verticalAlign == TOP || verticalAlign == BOTTOM)
return 0;
RenderObject* parent = renderer->parent();
if (parent->isRenderInline() && parent->style()->verticalAlign() != TOP && parent->style()->verticalAlign() != BOTTOM)
verticalPosition = box->parent()->logicalTop();
if (verticalAlign != BASELINE) {
const Font& font = parent->style(firstLine)->font();
const FontMetrics& fontMetrics = font.fontMetrics();
int fontSize = font.pixelSize();
LineDirectionMode lineDirection = parent->isHorizontalWritingMode() ? HorizontalLine : VerticalLine;
if (verticalAlign == SUB)
verticalPosition += fontSize / 5 + 1;
else if (verticalAlign == SUPER)
verticalPosition -= fontSize / 3 + 1;
else if (verticalAlign == TEXT_TOP)
verticalPosition += renderer->baselinePosition(baselineType(), firstLine, lineDirection) - fontMetrics.ascent(baselineType());
else if (verticalAlign == MIDDLE)
verticalPosition += -static_cast<int>(fontMetrics.xHeight() / 2) - renderer->lineHeight(firstLine, lineDirection) / 2 + renderer->baselinePosition(baselineType(), firstLine, lineDirection);
else if (verticalAlign == TEXT_BOTTOM) {
verticalPosition += fontMetrics.descent(baselineType());
if (!renderer->isReplaced() || renderer->isInlineBlockOrInlineTable())
verticalPosition -= (renderer->lineHeight(firstLine, lineDirection) - renderer->baselinePosition(baselineType(), firstLine, lineDirection));
} else if (verticalAlign == BASELINE_MIDDLE)
verticalPosition += -renderer->lineHeight(firstLine, lineDirection) / 2 + renderer->baselinePosition(baselineType(), firstLine, lineDirection);
else if (verticalAlign == LENGTH)
verticalPosition -= valueForLength(renderer->style()->verticalAlignLength(), renderer->lineHeight(firstLine, lineDirection), renderer->view());
}
if (isRenderInline && !firstLine)
verticalPositionCache.set(renderer, baselineType(), verticalPosition);
return verticalPosition;
}
bool RootInlineBox::includeLeadingForBox(InlineBox* box) const
{
if (box->renderer()->isReplaced() || (box->renderer()->isText() && !box->isText()))
return false;
LineBoxContain lineBoxContain = renderer()->style()->lineBoxContain();
return (lineBoxContain & LineBoxContainInline) || (box == this && (lineBoxContain & LineBoxContainBlock));
}
bool RootInlineBox::includeFontForBox(InlineBox* box) const
{
if (box->renderer()->isReplaced() || (box->renderer()->isText() && !box->isText()))
return false;
if (!box->isText() && box->isInlineFlowBox() && !toInlineFlowBox(box)->hasTextChildren())
return false;
LineBoxContain lineBoxContain = renderer()->style()->lineBoxContain();
return (lineBoxContain & LineBoxContainFont) || (!isHorizontal() && (lineBoxContain & LineBoxContainGlyphs));
}
bool RootInlineBox::includeGlyphsForBox(InlineBox* box) const
{
if (box->renderer()->isReplaced() || (box->renderer()->isText() && !box->isText()))
return false;
if (!box->isText() && box->isInlineFlowBox() && !toInlineFlowBox(box)->hasTextChildren())
return false;
LineBoxContain lineBoxContain = renderer()->style()->lineBoxContain();
return isHorizontal() && (lineBoxContain & LineBoxContainGlyphs);
}
bool RootInlineBox::includeMarginForBox(InlineBox* box) const
{
if (box->renderer()->isReplaced() || (box->renderer()->isText() && !box->isText()))
return false;
LineBoxContain lineBoxContain = renderer()->style()->lineBoxContain();
return lineBoxContain & LineBoxContainInlineBox;
}
bool RootInlineBox::fitsToGlyphs() const
{
LineBoxContain lineBoxContain = renderer()->style()->lineBoxContain();
return isHorizontal() && (lineBoxContain & LineBoxContainGlyphs);
}
bool RootInlineBox::includesRootLineBoxFontOrLeading() const
{
LineBoxContain lineBoxContain = renderer()->style()->lineBoxContain();
return (lineBoxContain & LineBoxContainBlock) || (lineBoxContain & LineBoxContainInline) || (lineBoxContain & LineBoxContainFont);
}
Node* RootInlineBox::getLogicalStartBoxWithNode(InlineBox*& startBox) const
{
Vector<InlineBox*> leafBoxesInLogicalOrder;
collectLeafBoxesInLogicalOrder(leafBoxesInLogicalOrder);
for (size_t i = 0; i < leafBoxesInLogicalOrder.size(); ++i) {
if (leafBoxesInLogicalOrder[i]->renderer()->node()) {
startBox = leafBoxesInLogicalOrder[i];
return startBox->renderer()->node();
}
}
startBox = 0;
return 0;
}
Node* RootInlineBox::getLogicalEndBoxWithNode(InlineBox*& endBox) const
{
Vector<InlineBox*> leafBoxesInLogicalOrder;
collectLeafBoxesInLogicalOrder(leafBoxesInLogicalOrder);
for (size_t i = leafBoxesInLogicalOrder.size(); i > 0; --i) {
if (leafBoxesInLogicalOrder[i - 1]->renderer()->node()) {
endBox = leafBoxesInLogicalOrder[i - 1];
return endBox->renderer()->node();
}
}
endBox = 0;
return 0;
}
#ifndef NDEBUG
const char* RootInlineBox::boxName() const
{
return "RootInlineBox";
}
#endif
}