RenderBlockLineLayout.cpp [plain text]
#include "config.h"
#include "BidiResolver.h"
#include "Hyphenation.h"
#include "InlineIterator.h"
#include "InlineTextBox.h"
#include "Logging.h"
#include "RenderArena.h"
#include "RenderCombineText.h"
#include "RenderFlowThread.h"
#include "RenderInline.h"
#include "RenderLayer.h"
#include "RenderListMarker.h"
#include "RenderRubyRun.h"
#include "RenderView.h"
#include "Settings.h"
#include "TextBreakIterator.h"
#include "TrailingFloatsRootInlineBox.h"
#include "VerticalPositionCache.h"
#include "break_lines.h"
#include <wtf/AlwaysInline.h>
#include <wtf/RefCountedLeakCounter.h>
#include <wtf/StdLibExtras.h>
#include <wtf/Vector.h>
#include <wtf/unicode/CharacterNames.h>
#if ENABLE(SVG)
#include "RenderSVGInlineText.h"
#include "SVGRootInlineBox.h"
#endif
using namespace std;
using namespace WTF;
using namespace Unicode;
namespace WebCore {
const unsigned cMaxLineDepth = 200;
class LineWidth {
public:
LineWidth(RenderBlock* block, bool isFirstLine)
: m_block(block)
, m_uncommittedWidth(0)
, m_committedWidth(0)
, m_overhangWidth(0)
, m_left(0)
, m_right(0)
, m_availableWidth(0)
, m_isFirstLine(isFirstLine)
{
ASSERT(block);
updateAvailableWidth();
}
bool fitsOnLine() const { return currentWidth() <= m_availableWidth; }
bool fitsOnLine(float extra) const { return currentWidth() + extra <= m_availableWidth; }
float currentWidth() const { return m_committedWidth + m_uncommittedWidth; }
float uncommittedWidth() const { return m_uncommittedWidth; }
float committedWidth() const { return m_committedWidth; }
float availableWidth() const { return m_availableWidth; }
void updateAvailableWidth();
void shrinkAvailableWidthForNewFloatIfNeeded(RenderBlock::FloatingObject*);
void addUncommittedWidth(float delta) { m_uncommittedWidth += delta; }
void commit()
{
m_committedWidth += m_uncommittedWidth;
m_uncommittedWidth = 0;
}
void applyOverhang(RenderRubyRun*, RenderObject* startRenderer, RenderObject* endRenderer);
void fitBelowFloats();
private:
void computeAvailableWidthFromLeftAndRight()
{
m_availableWidth = max(0, m_right - m_left) + m_overhangWidth;
}
private:
RenderBlock* m_block;
float m_uncommittedWidth;
float m_committedWidth;
float m_overhangWidth; int m_left;
int m_right;
float m_availableWidth;
bool m_isFirstLine;
};
inline void LineWidth::updateAvailableWidth()
{
LayoutUnit height = m_block->logicalHeight();
m_left = m_block->pixelSnappedLogicalLeftOffsetForLine(height, m_isFirstLine);
m_right = m_block->pixelSnappedLogicalRightOffsetForLine(height, m_isFirstLine);
computeAvailableWidthFromLeftAndRight();
}
inline void LineWidth::shrinkAvailableWidthForNewFloatIfNeeded(RenderBlock::FloatingObject* newFloat)
{
LayoutUnit height = m_block->logicalHeight();
if (height < m_block->logicalTopForFloat(newFloat) || height >= m_block->logicalBottomForFloat(newFloat))
return;
if (newFloat->type() == RenderBlock::FloatingObject::FloatLeft) {
m_left = m_block->pixelSnappedLogicalRightForFloat(newFloat);
if (m_isFirstLine && m_block->style()->isLeftToRightDirection())
m_left += floorToInt(m_block->textIndentOffset());
} else {
m_right = m_block->pixelSnappedLogicalLeftForFloat(newFloat);
if (m_isFirstLine && !m_block->style()->isLeftToRightDirection())
m_right -= floorToInt(m_block->textIndentOffset());
}
computeAvailableWidthFromLeftAndRight();
}
void LineWidth::applyOverhang(RenderRubyRun* rubyRun, RenderObject* startRenderer, RenderObject* endRenderer)
{
int startOverhang;
int endOverhang;
rubyRun->getOverhang(m_isFirstLine, startRenderer, endRenderer, startOverhang, endOverhang);
startOverhang = min<int>(startOverhang, m_committedWidth);
m_availableWidth += startOverhang;
endOverhang = max(min<int>(endOverhang, m_availableWidth - currentWidth()), 0);
m_availableWidth += endOverhang;
m_overhangWidth += startOverhang + endOverhang;
}
void LineWidth::fitBelowFloats()
{
ASSERT(!m_committedWidth);
ASSERT(!fitsOnLine());
LayoutUnit floatLogicalBottom;
LayoutUnit lastFloatLogicalBottom = m_block->logicalHeight();
float newLineWidth = m_availableWidth;
float newLineLeft = m_left;
float newLineRight = m_right;
while (true) {
floatLogicalBottom = m_block->nextFloatLogicalBottomBelow(lastFloatLogicalBottom);
if (floatLogicalBottom <= lastFloatLogicalBottom)
break;
newLineLeft = m_block->logicalLeftOffsetForLine(floatLogicalBottom, m_isFirstLine);
newLineRight = m_block->logicalRightOffsetForLine(floatLogicalBottom, m_isFirstLine);
newLineWidth = max(0.0f, newLineRight - newLineLeft);
lastFloatLogicalBottom = floatLogicalBottom;
if (newLineWidth >= m_uncommittedWidth)
break;
}
if (newLineWidth > m_availableWidth) {
m_block->setLogicalHeight(lastFloatLogicalBottom);
m_availableWidth = newLineWidth + m_overhangWidth;
m_left = newLineLeft;
m_right = newLineRight;
}
}
class LineInfo {
public:
LineInfo()
: m_isFirstLine(true)
, m_isLastLine(false)
, m_isEmpty(true)
, m_previousLineBrokeCleanly(true)
, m_floatPaginationStrut(0)
{ }
bool isFirstLine() const { return m_isFirstLine; }
bool isLastLine() const { return m_isLastLine; }
bool isEmpty() const { return m_isEmpty; }
bool previousLineBrokeCleanly() const { return m_previousLineBrokeCleanly; }
LayoutUnit floatPaginationStrut() const { return m_floatPaginationStrut; }
void setFirstLine(bool firstLine) { m_isFirstLine = firstLine; }
void setLastLine(bool lastLine) { m_isLastLine = lastLine; }
void setEmpty(bool empty, RenderBlock* block = 0, LineWidth* lineWidth = 0)
{
if (m_isEmpty == empty)
return;
m_isEmpty = empty;
if (!empty && block && floatPaginationStrut()) {
block->setLogicalHeight(block->logicalHeight() + floatPaginationStrut());
setFloatPaginationStrut(0);
lineWidth->updateAvailableWidth();
}
}
void setPreviousLineBrokeCleanly(bool previousLineBrokeCleanly) { m_previousLineBrokeCleanly = previousLineBrokeCleanly; }
void setFloatPaginationStrut(LayoutUnit strut) { m_floatPaginationStrut = strut; }
private:
bool m_isFirstLine;
bool m_isLastLine;
bool m_isEmpty;
bool m_previousLineBrokeCleanly;
LayoutUnit m_floatPaginationStrut;
};
static inline LayoutUnit borderPaddingMarginStart(RenderInline* child)
{
return child->marginStart() + child->paddingStart() + child->borderStart();
}
static inline LayoutUnit borderPaddingMarginEnd(RenderInline* child)
{
return child->marginEnd() + child->paddingEnd() + child->borderEnd();
}
static LayoutUnit inlineLogicalWidth(RenderObject* child, bool start = true, bool end = true)
{
unsigned lineDepth = 1;
LayoutUnit extraWidth = 0;
RenderObject* parent = child->parent();
while (parent->isRenderInline() && lineDepth++ < cMaxLineDepth) {
RenderInline* parentAsRenderInline = toRenderInline(parent);
if (start && !child->previousSibling())
extraWidth += borderPaddingMarginStart(parentAsRenderInline);
if (end && !child->nextSibling())
extraWidth += borderPaddingMarginEnd(parentAsRenderInline);
child = parent;
parent = child->parent();
}
return extraWidth;
}
static void determineDirectionality(TextDirection& dir, InlineIterator iter)
{
while (!iter.atEnd()) {
if (iter.atParagraphSeparator())
return;
if (UChar current = iter.current()) {
Direction charDirection = direction(current);
if (charDirection == LeftToRight) {
dir = LTR;
return;
}
if (charDirection == RightToLeft || charDirection == RightToLeftArabic) {
dir = RTL;
return;
}
}
iter.increment();
}
}
static void checkMidpoints(LineMidpointState& lineMidpointState, InlineIterator& lBreak)
{
if (lBreak.m_obj && lineMidpointState.numMidpoints && !(lineMidpointState.numMidpoints % 2)) {
InlineIterator* midpoints = lineMidpointState.midpoints.data();
InlineIterator& endpoint = midpoints[lineMidpointState.numMidpoints - 2];
const InlineIterator& startpoint = midpoints[lineMidpointState.numMidpoints - 1];
InlineIterator currpoint = endpoint;
while (!currpoint.atEnd() && currpoint != startpoint && currpoint != lBreak)
currpoint.increment();
if (currpoint == lBreak) {
lineMidpointState.numMidpoints--;
if (endpoint.m_obj->style()->collapseWhiteSpace())
endpoint.m_pos--;
}
}
}
static void addMidpoint(LineMidpointState& lineMidpointState, const InlineIterator& midpoint)
{
if (lineMidpointState.midpoints.size() <= lineMidpointState.numMidpoints)
lineMidpointState.midpoints.grow(lineMidpointState.numMidpoints + 10);
InlineIterator* midpoints = lineMidpointState.midpoints.data();
midpoints[lineMidpointState.numMidpoints++] = midpoint;
}
static inline BidiRun* createRun(int start, int end, RenderObject* obj, InlineBidiResolver& resolver)
{
return new (obj->renderArena()) BidiRun(start, end, obj, resolver.context(), resolver.dir());
}
void RenderBlock::appendRunsForObject(BidiRunList<BidiRun>& runs, int start, int end, RenderObject* obj, InlineBidiResolver& resolver)
{
if (start > end || shouldSkipCreatingRunsForObject(obj))
return;
LineMidpointState& lineMidpointState = resolver.midpointState();
bool haveNextMidpoint = (lineMidpointState.currentMidpoint < lineMidpointState.numMidpoints);
InlineIterator nextMidpoint;
if (haveNextMidpoint)
nextMidpoint = lineMidpointState.midpoints[lineMidpointState.currentMidpoint];
if (lineMidpointState.betweenMidpoints) {
if (!(haveNextMidpoint && nextMidpoint.m_obj == obj))
return;
lineMidpointState.betweenMidpoints = false;
start = nextMidpoint.m_pos;
lineMidpointState.currentMidpoint++;
if (start < end)
return appendRunsForObject(runs, start, end, obj, resolver);
} else {
if (!haveNextMidpoint || (obj != nextMidpoint.m_obj)) {
runs.addRun(createRun(start, end, obj, resolver));
return;
}
if (static_cast<int>(nextMidpoint.m_pos + 1) <= end) {
lineMidpointState.betweenMidpoints = true;
lineMidpointState.currentMidpoint++;
if (nextMidpoint.m_pos != UINT_MAX) { if (static_cast<int>(nextMidpoint.m_pos + 1) > start)
runs.addRun(createRun(start, nextMidpoint.m_pos + 1, obj, resolver));
return appendRunsForObject(runs, nextMidpoint.m_pos + 1, end, obj, resolver);
}
} else
runs.addRun(createRun(start, end, obj, resolver));
}
}
static inline InlineBox* createInlineBoxForRenderer(RenderObject* obj, bool isRootLineBox, bool isOnlyRun = false)
{
if (isRootLineBox)
return toRenderBlock(obj)->createAndAppendRootInlineBox();
if (obj->isText()) {
InlineTextBox* textBox = toRenderText(obj)->createInlineTextBox();
if (obj->isBR())
textBox->setIsText(isOnlyRun || obj->document()->inNoQuirksMode());
return textBox;
}
if (obj->isBox())
return toRenderBox(obj)->createInlineBox();
return toRenderInline(obj)->createAndAppendInlineFlowBox();
}
static inline void dirtyLineBoxesForRenderer(RenderObject* o, bool fullLayout)
{
if (o->isText()) {
if (o->preferredLogicalWidthsDirty() && (o->isCounter() || o->isQuote()))
toRenderText(o)->computePreferredLogicalWidths(0); toRenderText(o)->dirtyLineBoxes(fullLayout);
} else
toRenderInline(o)->dirtyLineBoxes(fullLayout);
}
static bool parentIsConstructedOrHaveNext(InlineFlowBox* parentBox)
{
do {
if (parentBox->isConstructed() || parentBox->nextOnLine())
return true;
parentBox = parentBox->parent();
} while (parentBox);
return false;
}
InlineFlowBox* RenderBlock::createLineBoxes(RenderObject* obj, const LineInfo& lineInfo, InlineBox* childBox)
{
unsigned lineDepth = 1;
InlineFlowBox* parentBox = 0;
InlineFlowBox* result = 0;
bool hasDefaultLineBoxContain = style()->lineBoxContain() == RenderStyle::initialLineBoxContain();
do {
ASSERT(obj->isRenderInline() || obj == this);
RenderInline* inlineFlow = (obj != this) ? toRenderInline(obj) : 0;
parentBox = inlineFlow ? inlineFlow->lastLineBox() : toRenderBlock(obj)->lastLineBox();
bool constructedNewBox = false;
bool allowedToConstructNewBox = !hasDefaultLineBoxContain || !inlineFlow || inlineFlow->alwaysCreateLineBoxes();
bool canUseExistingParentBox = parentBox && !parentIsConstructedOrHaveNext(parentBox);
if (allowedToConstructNewBox && !canUseExistingParentBox) {
InlineBox* newBox = createInlineBoxForRenderer(obj, obj == this);
ASSERT(newBox->isInlineFlowBox());
parentBox = toInlineFlowBox(newBox);
parentBox->setFirstLineStyleBit(lineInfo.isFirstLine());
parentBox->setIsHorizontal(isHorizontalWritingMode());
if (!hasDefaultLineBoxContain)
parentBox->clearDescendantsHaveSameLineHeightAndBaseline();
constructedNewBox = true;
}
if (constructedNewBox || canUseExistingParentBox) {
if (!result)
result = parentBox;
if (childBox)
parentBox->addToLine(childBox);
if (!constructedNewBox || obj == this)
break;
childBox = parentBox;
}
obj = (++lineDepth >= cMaxLineDepth) ? this : obj->parent();
} while (true);
return result;
}
static bool reachedEndOfTextRenderer(const BidiRunList<BidiRun>& bidiRuns)
{
BidiRun* run = bidiRuns.logicallyLastRun();
if (!run)
return true;
unsigned int pos = run->stop();
RenderObject* r = run->m_object;
if (!r->isText() || r->isBR())
return false;
RenderText* renderText = toRenderText(r);
if (pos >= renderText->textLength())
return true;
while (isASCIISpace(renderText->characters()[pos])) {
pos++;
if (pos >= renderText->textLength())
return true;
}
return false;
}
RootInlineBox* RenderBlock::constructLine(BidiRunList<BidiRun>& bidiRuns, const LineInfo& lineInfo)
{
ASSERT(bidiRuns.firstRun());
bool rootHasSelectedChildren = false;
InlineFlowBox* parentBox = 0;
for (BidiRun* r = bidiRuns.firstRun(); r; r = r->next()) {
bool isOnlyRun = (bidiRuns.runCount() == 1);
if (bidiRuns.runCount() == 2 && !r->m_object->isListMarker())
isOnlyRun = (!style()->isLeftToRightDirection() ? bidiRuns.lastRun() : bidiRuns.firstRun())->m_object->isListMarker();
if (lineInfo.isEmpty())
continue;
InlineBox* box = createInlineBoxForRenderer(r->m_object, false, isOnlyRun);
r->m_box = box;
ASSERT(box);
if (!box)
continue;
if (!rootHasSelectedChildren && box->renderer()->selectionState() != RenderObject::SelectionNone)
rootHasSelectedChildren = true;
if (!parentBox || parentBox->renderer() != r->m_object->parent())
parentBox = createLineBoxes(r->m_object->parent(), lineInfo, box);
else {
parentBox->addToLine(box);
}
bool visuallyOrdered = r->m_object->style()->rtlOrdering() == VisualOrder;
box->setBidiLevel(r->level());
if (box->isInlineTextBox()) {
InlineTextBox* text = toInlineTextBox(box);
text->setStart(r->m_start);
text->setLen(r->m_stop - r->m_start);
text->setDirOverride(r->dirOverride(visuallyOrdered));
if (r->m_hasHyphen)
text->setHasHyphen(true);
}
}
ASSERT(lastLineBox() && !lastLineBox()->isConstructed());
if (rootHasSelectedChildren)
lastLineBox()->root()->setHasSelectedChildren(true);
bool isLogicallyLastRunWrapped = bidiRuns.logicallyLastRun()->m_object && bidiRuns.logicallyLastRun()->m_object->isText() ? !reachedEndOfTextRenderer(bidiRuns) : true;
lastLineBox()->determineSpacingForFlowBoxes(lineInfo.isLastLine(), isLogicallyLastRunWrapped, bidiRuns.logicallyLastRun()->m_object);
lastLineBox()->setConstructed();
return lastRootBox();
}
ETextAlign RenderBlock::textAlignmentForLine(bool endsWithSoftBreak) const
{
ETextAlign alignment = style()->textAlign();
if (!endsWithSoftBreak && alignment == JUSTIFY)
alignment = TAAUTO;
return alignment;
}
static void updateLogicalWidthForLeftAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
{
if (isLeftToRightDirection) {
if (totalLogicalWidth > availableLogicalWidth && trailingSpaceRun)
trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceRun->m_box->logicalWidth() - totalLogicalWidth + availableLogicalWidth));
return;
}
if (trailingSpaceRun)
trailingSpaceRun->m_box->setLogicalWidth(0);
else if (totalLogicalWidth > availableLogicalWidth)
logicalLeft -= (totalLogicalWidth - availableLogicalWidth);
}
static void updateLogicalWidthForRightAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
{
if (isLeftToRightDirection) {
if (trailingSpaceRun) {
totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
trailingSpaceRun->m_box->setLogicalWidth(0);
}
if (totalLogicalWidth < availableLogicalWidth)
logicalLeft += availableLogicalWidth - totalLogicalWidth;
return;
}
if (totalLogicalWidth > availableLogicalWidth && trailingSpaceRun) {
trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceRun->m_box->logicalWidth() - totalLogicalWidth + availableLogicalWidth));
totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
} else
logicalLeft += availableLogicalWidth - totalLogicalWidth;
}
static void updateLogicalWidthForCenterAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
{
float trailingSpaceWidth = 0;
if (trailingSpaceRun) {
totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
trailingSpaceWidth = min(trailingSpaceRun->m_box->logicalWidth(), (availableLogicalWidth - totalLogicalWidth + 1) / 2);
trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceWidth));
}
if (isLeftToRightDirection)
logicalLeft += max<float>((availableLogicalWidth - totalLogicalWidth) / 2, 0);
else
logicalLeft += totalLogicalWidth > availableLogicalWidth ? (availableLogicalWidth - totalLogicalWidth) : (availableLogicalWidth - totalLogicalWidth) / 2 - trailingSpaceWidth;
}
void RenderBlock::setMarginsForRubyRun(BidiRun* run, RenderRubyRun* renderer, RenderObject* previousObject, const LineInfo& lineInfo)
{
int startOverhang;
int endOverhang;
RenderObject* nextObject = 0;
for (BidiRun* runWithNextObject = run->next(); runWithNextObject; runWithNextObject = runWithNextObject->next()) {
if (!runWithNextObject->m_object->isPositioned() && !runWithNextObject->m_box->isLineBreak()) {
nextObject = runWithNextObject->m_object;
break;
}
}
renderer->getOverhang(lineInfo.isFirstLine(), renderer->style()->isLeftToRightDirection() ? previousObject : nextObject, renderer->style()->isLeftToRightDirection() ? nextObject : previousObject, startOverhang, endOverhang);
setMarginStartForChild(renderer, -startOverhang);
setMarginEndForChild(renderer, -endOverhang);
}
static inline float measureHyphenWidth(RenderText* renderer, const Font& font)
{
RenderStyle* style = renderer->style();
return font.width(RenderBlock::constructTextRun(renderer, font, style->hyphenString().string(), style));
}
static inline void setLogicalWidthForTextRun(RootInlineBox* lineBox, BidiRun* run, RenderText* renderer, float xPos, const LineInfo& lineInfo,
GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache)
{
HashSet<const SimpleFontData*> fallbackFonts;
GlyphOverflow glyphOverflow;
if (lineBox->fitsToGlyphs()) {
bool includeRootLine = lineBox->includesRootLineBoxFontOrLeading();
int baselineShift = lineBox->verticalPositionForBox(run->m_box, verticalPositionCache);
int rootDescent = includeRootLine ? lineBox->renderer()->style(lineInfo.isFirstLine())->font().fontMetrics().descent() : 0;
int rootAscent = includeRootLine ? lineBox->renderer()->style(lineInfo.isFirstLine())->font().fontMetrics().ascent() : 0;
int boxAscent = renderer->style(lineInfo.isFirstLine())->font().fontMetrics().ascent() - baselineShift;
int boxDescent = renderer->style(lineInfo.isFirstLine())->font().fontMetrics().descent() + baselineShift;
if (boxAscent > rootDescent || boxDescent > rootAscent)
glyphOverflow.computeBounds = true;
}
LayoutUnit hyphenWidth = 0;
if (toInlineTextBox(run->m_box)->hasHyphen()) {
const Font& font = renderer->style(lineInfo.isFirstLine())->font();
hyphenWidth = measureHyphenWidth(renderer, font);
}
run->m_box->setLogicalWidth(renderer->width(run->m_start, run->m_stop - run->m_start, xPos, lineInfo.isFirstLine(), &fallbackFonts, &glyphOverflow) + hyphenWidth);
if (!fallbackFonts.isEmpty()) {
ASSERT(run->m_box->isText());
GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.add(toInlineTextBox(run->m_box), make_pair(Vector<const SimpleFontData*>(), GlyphOverflow())).iterator;
ASSERT(it->second.first.isEmpty());
copyToVector(fallbackFonts, it->second.first);
run->m_box->parent()->clearDescendantsHaveSameLineHeightAndBaseline();
}
if ((glyphOverflow.top || glyphOverflow.bottom || glyphOverflow.left || glyphOverflow.right)) {
ASSERT(run->m_box->isText());
GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.add(toInlineTextBox(run->m_box), make_pair(Vector<const SimpleFontData*>(), GlyphOverflow())).iterator;
it->second.second = glyphOverflow;
run->m_box->clearKnownToHaveNoOverflow();
}
}
static inline void computeExpansionForJustifiedText(BidiRun* firstRun, BidiRun* trailingSpaceRun, Vector<unsigned, 16>& expansionOpportunities, unsigned expansionOpportunityCount, float& totalLogicalWidth, float availableLogicalWidth)
{
if (!expansionOpportunityCount || availableLogicalWidth <= totalLogicalWidth)
return;
size_t i = 0;
for (BidiRun* r = firstRun; r; r = r->next()) {
if (!r->m_box || r == trailingSpaceRun)
continue;
if (r->m_object->isText()) {
unsigned opportunitiesInRun = expansionOpportunities[i++];
ASSERT(opportunitiesInRun <= expansionOpportunityCount);
if (r->m_object->style()->collapseWhiteSpace()) {
InlineTextBox* textBox = toInlineTextBox(r->m_box);
int expansion = (availableLogicalWidth - totalLogicalWidth) * opportunitiesInRun / expansionOpportunityCount;
textBox->setExpansion(expansion);
totalLogicalWidth += expansion;
}
expansionOpportunityCount -= opportunitiesInRun;
if (!expansionOpportunityCount)
break;
}
}
}
void RenderBlock::computeInlineDirectionPositionsForLine(RootInlineBox* lineBox, const LineInfo& lineInfo, BidiRun* firstRun, BidiRun* trailingSpaceRun, bool reachedEnd,
GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache)
{
ETextAlign textAlign = textAlignmentForLine(!reachedEnd && !lineBox->endsWithBreak());
float logicalLeft = pixelSnappedLogicalLeftOffsetForLine(logicalHeight(), lineInfo.isFirstLine());
float availableLogicalWidth = pixelSnappedLogicalRightOffsetForLine(logicalHeight(), lineInfo.isFirstLine()) - logicalLeft;
bool needsWordSpacing = false;
float totalLogicalWidth = lineBox->getFlowSpacingLogicalWidth();
unsigned expansionOpportunityCount = 0;
bool isAfterExpansion = true;
Vector<unsigned, 16> expansionOpportunities;
RenderObject* previousObject = 0;
for (BidiRun* r = firstRun; r; r = r->next()) {
if (!r->m_box || r->m_object->isPositioned() || r->m_box->isLineBreak())
continue; if (r->m_object->isText()) {
RenderText* rt = toRenderText(r->m_object);
if (textAlign == JUSTIFY && r != trailingSpaceRun) {
if (!isAfterExpansion)
toInlineTextBox(r->m_box)->setCanHaveLeadingExpansion(true);
unsigned opportunitiesInRun = Font::expansionOpportunityCount(rt->characters() + r->m_start, r->m_stop - r->m_start, r->m_box->direction(), isAfterExpansion);
expansionOpportunities.append(opportunitiesInRun);
expansionOpportunityCount += opportunitiesInRun;
}
if (int length = rt->textLength()) {
if (!r->m_start && needsWordSpacing && isSpaceOrNewline(rt->characters()[r->m_start]))
totalLogicalWidth += rt->style(lineInfo.isFirstLine())->font().wordSpacing();
needsWordSpacing = !isSpaceOrNewline(rt->characters()[r->m_stop - 1]) && r->m_stop == length;
}
setLogicalWidthForTextRun(lineBox, r, rt, totalLogicalWidth, lineInfo, textBoxDataMap, verticalPositionCache);
} else {
isAfterExpansion = false;
if (!r->m_object->isRenderInline()) {
RenderBox* renderBox = toRenderBox(r->m_object);
if (renderBox->isRubyRun())
setMarginsForRubyRun(r, toRenderRubyRun(renderBox), previousObject, lineInfo);
r->m_box->setLogicalWidth(logicalWidthForChild(renderBox));
totalLogicalWidth += marginStartForChild(renderBox) + marginEndForChild(renderBox);
}
}
totalLogicalWidth += r->m_box->logicalWidth();
previousObject = r->m_object;
}
if (isAfterExpansion && !expansionOpportunities.isEmpty()) {
expansionOpportunities.last()--;
expansionOpportunityCount--;
}
switch (textAlign) {
case LEFT:
case WEBKIT_LEFT:
updateLogicalWidthForLeftAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
break;
case JUSTIFY:
adjustInlineDirectionLineBounds(expansionOpportunityCount, logicalLeft, availableLogicalWidth);
if (expansionOpportunityCount) {
if (trailingSpaceRun) {
totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
trailingSpaceRun->m_box->setLogicalWidth(0);
}
break;
}
case TAAUTO:
if (style()->isLeftToRightDirection()) {
if (totalLogicalWidth > availableLogicalWidth && trailingSpaceRun)
trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceRun->m_box->logicalWidth() - totalLogicalWidth + availableLogicalWidth));
break;
}
case RIGHT:
case WEBKIT_RIGHT:
updateLogicalWidthForRightAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
break;
case CENTER:
case WEBKIT_CENTER:
updateLogicalWidthForCenterAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
break;
case TASTART:
if (style()->isLeftToRightDirection())
updateLogicalWidthForLeftAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
else
updateLogicalWidthForRightAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
break;
case TAEND:
if (style()->isLeftToRightDirection())
updateLogicalWidthForRightAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
else
updateLogicalWidthForLeftAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
break;
}
computeExpansionForJustifiedText(firstRun, trailingSpaceRun, expansionOpportunities, expansionOpportunityCount, totalLogicalWidth, availableLogicalWidth);
needsWordSpacing = false;
lineBox->placeBoxesInInlineDirection(logicalLeft, needsWordSpacing, textBoxDataMap);
}
void RenderBlock::computeBlockDirectionPositionsForLine(RootInlineBox* lineBox, BidiRun* firstRun, GlyphOverflowAndFallbackFontsMap& textBoxDataMap,
VerticalPositionCache& verticalPositionCache)
{
setLogicalHeight(lineBox->alignBoxesInBlockDirection(logicalHeight(), textBoxDataMap, verticalPositionCache));
for (BidiRun* r = firstRun; r; r = r->next()) {
ASSERT(r->m_box);
if (!r->m_box)
continue;
if (r->m_object->isPositioned())
r->m_box->setLogicalTop(logicalHeight());
if (r->m_object->isText())
toRenderText(r->m_object)->positionLineBox(r->m_box);
else if (r->m_object->isBox())
toRenderBox(r->m_object)->positionLineBox(r->m_box);
}
lineBox->markDirty(false);
}
static inline bool isCollapsibleSpace(UChar character, RenderText* renderer)
{
if (character == ' ' || character == '\t' || character == softHyphen)
return true;
if (character == '\n')
return !renderer->style()->preserveNewline();
if (character == noBreakSpace)
return renderer->style()->nbspMode() == SPACE;
return false;
}
static void setStaticPositions(RenderBlock* block, RenderBox* child)
{
RenderObject* containerBlock = child->container();
LayoutUnit blockHeight = block->logicalHeight();
if (containerBlock->isRenderInline()) {
toRenderInline(containerBlock)->layer()->setStaticInlinePosition(block->startOffsetForLine(blockHeight, false));
toRenderInline(containerBlock)->layer()->setStaticBlockPosition(blockHeight);
}
if (child->style()->isOriginalDisplayInlineType())
child->layer()->setStaticInlinePosition(block->startOffsetForLine(blockHeight, false));
else
block->setStaticInlinePositionForChild(child, blockHeight, block->startOffsetForContent(blockHeight));
child->layer()->setStaticBlockPosition(blockHeight);
}
inline BidiRun* RenderBlock::handleTrailingSpaces(BidiRunList<BidiRun>& bidiRuns, BidiContext* currentContext)
{
if (!bidiRuns.runCount()
|| !bidiRuns.logicallyLastRun()->m_object->style()->breakOnlyAfterWhiteSpace()
|| !bidiRuns.logicallyLastRun()->m_object->style()->autoWrap())
return 0;
BidiRun* trailingSpaceRun = bidiRuns.logicallyLastRun();
RenderObject* lastObject = trailingSpaceRun->m_object;
if (!lastObject->isText())
return 0;
RenderText* lastText = toRenderText(lastObject);
const UChar* characters = lastText->characters();
int firstSpace = trailingSpaceRun->stop();
while (firstSpace > trailingSpaceRun->start()) {
UChar current = characters[firstSpace - 1];
if (!isCollapsibleSpace(current, lastText))
break;
firstSpace--;
}
if (firstSpace == trailingSpaceRun->stop())
return 0;
TextDirection direction = style()->direction();
bool shouldReorder = trailingSpaceRun != (direction == LTR ? bidiRuns.lastRun() : bidiRuns.firstRun());
if (firstSpace != trailingSpaceRun->start()) {
BidiContext* baseContext = currentContext;
while (BidiContext* parent = baseContext->parent())
baseContext = parent;
BidiRun* newTrailingRun = new (renderArena()) BidiRun(firstSpace, trailingSpaceRun->m_stop, trailingSpaceRun->m_object, baseContext, OtherNeutral);
trailingSpaceRun->m_stop = firstSpace;
if (direction == LTR)
bidiRuns.addRun(newTrailingRun);
else
bidiRuns.prependRun(newTrailingRun);
trailingSpaceRun = newTrailingRun;
return trailingSpaceRun;
}
if (!shouldReorder)
return trailingSpaceRun;
if (direction == LTR) {
bidiRuns.moveRunToEnd(trailingSpaceRun);
trailingSpaceRun->m_level = 0;
} else {
bidiRuns.moveRunToBeginning(trailingSpaceRun);
trailingSpaceRun->m_level = 1;
}
return trailingSpaceRun;
}
void RenderBlock::appendFloatingObjectToLastLine(FloatingObject* floatingObject)
{
ASSERT(!floatingObject->m_originatingLine);
floatingObject->m_originatingLine = lastRootBox();
lastRootBox()->appendFloat(floatingObject->renderer());
}
static inline BidiStatus statusWithDirection(TextDirection textDirection, bool isOverride)
{
WTF::Unicode::Direction direction = textDirection == LTR ? LeftToRight : RightToLeft;
RefPtr<BidiContext> context = BidiContext::create(textDirection == LTR ? 0 : 1, direction, isOverride, FromStyleOrDOM);
return BidiStatus(direction, direction, direction, context.release());
}
static inline void constructBidiRuns(InlineBidiResolver& topResolver, BidiRunList<BidiRun>& bidiRuns, const InlineIterator& endOfLine, VisualDirectionOverride override, bool previousLineBrokeCleanly)
{
ASSERT(&topResolver.runs() == &bidiRuns);
RenderObject* currentRoot = topResolver.position().root();
topResolver.createBidiRunsForLine(endOfLine, override, previousLineBrokeCleanly);
while (!topResolver.isolatedRuns().isEmpty()) {
BidiRun* isolatedRun = topResolver.isolatedRuns().last();
topResolver.isolatedRuns().removeLast();
RenderObject* startObj = isolatedRun->object();
RenderInline* isolatedInline = toRenderInline(containingIsolate(startObj, currentRoot));
InlineBidiResolver isolatedResolver;
EUnicodeBidi unicodeBidi = isolatedInline->style()->unicodeBidi();
TextDirection direction;
if (unicodeBidi == Plaintext)
determineDirectionality(direction, InlineIterator(isolatedInline, isolatedRun->object(), 0));
else {
ASSERT(unicodeBidi == Isolate || unicodeBidi == OverrideIsolate);
direction = isolatedInline->style()->direction();
}
isolatedResolver.setStatus(statusWithDirection(direction, isOverride(unicodeBidi)));
if (!bidiFirstSkippingEmptyInlines(isolatedInline, &isolatedResolver))
continue;
InlineIterator iter = InlineIterator(isolatedInline, startObj, isolatedRun->m_start);
isolatedResolver.setPositionIgnoringNestedIsolates(iter);
isolatedResolver.createBidiRunsForLine(endOfLine, NoVisualOverride, previousLineBrokeCleanly);
bidiRuns.replaceRunWithRuns(isolatedRun, isolatedResolver.runs());
if (!isolatedResolver.isolatedRuns().isEmpty()) {
topResolver.isolatedRuns().append(isolatedResolver.isolatedRuns());
isolatedResolver.isolatedRuns().clear();
}
}
}
RootInlineBox* RenderBlock::createLineBoxesFromBidiRuns(BidiRunList<BidiRun>& bidiRuns, const InlineIterator& end, LineInfo& lineInfo, VerticalPositionCache& verticalPositionCache, BidiRun* trailingSpaceRun)
{
if (!bidiRuns.runCount())
return 0;
lineInfo.setLastLine(!end.m_obj);
RootInlineBox* lineBox = constructLine(bidiRuns, lineInfo);
if (!lineBox)
return 0;
lineBox->setEndsWithBreak(lineInfo.previousLineBrokeCleanly());
#if ENABLE(SVG)
bool isSVGRootInlineBox = lineBox->isSVGRootInlineBox();
#else
bool isSVGRootInlineBox = false;
#endif
GlyphOverflowAndFallbackFontsMap textBoxDataMap;
if (!isSVGRootInlineBox)
computeInlineDirectionPositionsForLine(lineBox, lineInfo, bidiRuns.firstRun(), trailingSpaceRun, end.atEnd(), textBoxDataMap, verticalPositionCache);
computeBlockDirectionPositionsForLine(lineBox, bidiRuns.firstRun(), textBoxDataMap, verticalPositionCache);
#if ENABLE(SVG)
if (isSVGRootInlineBox) {
ASSERT(isSVGText());
static_cast<SVGRootInlineBox*>(lineBox)->computePerCharacterLayoutInformation();
}
#endif
lineBox->computeOverflow(lineBox->lineTop(), lineBox->lineBottom(), textBoxDataMap);
#if PLATFORM(MAC)
if (style()->highlight() != nullAtom)
lineBox->addHighlightOverflow();
#endif
return lineBox;
}
class LineLayoutState {
public:
LineLayoutState(bool fullLayout, LayoutUnit& repaintLogicalTop, LayoutUnit& repaintLogicalBottom)
: m_lastFloat(0)
, m_endLine(0)
, m_floatIndex(0)
, m_endLineLogicalTop(0)
, m_endLineMatched(false)
, m_checkForFloatsFromLastLine(false)
, m_isFullLayout(fullLayout)
, m_repaintLogicalTop(repaintLogicalTop)
, m_repaintLogicalBottom(repaintLogicalBottom)
, m_usesRepaintBounds(false)
{ }
void markForFullLayout() { m_isFullLayout = true; }
bool isFullLayout() const { return m_isFullLayout; }
bool usesRepaintBounds() const { return m_usesRepaintBounds; }
void setRepaintRange(LayoutUnit logicalHeight)
{
m_usesRepaintBounds = true;
m_repaintLogicalTop = m_repaintLogicalBottom = logicalHeight;
}
void updateRepaintRangeFromBox(RootInlineBox* box, LayoutUnit paginationDelta = 0)
{
m_usesRepaintBounds = true;
m_repaintLogicalTop = min(m_repaintLogicalTop, box->logicalTopVisualOverflow() + min(paginationDelta, ZERO_LAYOUT_UNIT));
m_repaintLogicalBottom = max(m_repaintLogicalBottom, box->logicalBottomVisualOverflow() + max(paginationDelta, ZERO_LAYOUT_UNIT));
}
bool endLineMatched() const { return m_endLineMatched; }
void setEndLineMatched(bool endLineMatched) { m_endLineMatched = endLineMatched; }
bool checkForFloatsFromLastLine() const { return m_checkForFloatsFromLastLine; }
void setCheckForFloatsFromLastLine(bool check) { m_checkForFloatsFromLastLine = check; }
LineInfo& lineInfo() { return m_lineInfo; }
const LineInfo& lineInfo() const { return m_lineInfo; }
LayoutUnit endLineLogicalTop() const { return m_endLineLogicalTop; }
void setEndLineLogicalTop(LayoutUnit logicalTop) { m_endLineLogicalTop = logicalTop; }
RootInlineBox* endLine() const { return m_endLine; }
void setEndLine(RootInlineBox* line) { m_endLine = line; }
RenderBlock::FloatingObject* lastFloat() const { return m_lastFloat; }
void setLastFloat(RenderBlock::FloatingObject* lastFloat) { m_lastFloat = lastFloat; }
Vector<RenderBlock::FloatWithRect>& floats() { return m_floats; }
unsigned floatIndex() const { return m_floatIndex; }
void setFloatIndex(unsigned floatIndex) { m_floatIndex = floatIndex; }
private:
Vector<RenderBlock::FloatWithRect> m_floats;
RenderBlock::FloatingObject* m_lastFloat;
RootInlineBox* m_endLine;
LineInfo m_lineInfo;
unsigned m_floatIndex;
LayoutUnit m_endLineLogicalTop;
bool m_endLineMatched;
bool m_checkForFloatsFromLastLine;
bool m_isFullLayout;
LayoutUnit& m_repaintLogicalTop;
LayoutUnit& m_repaintLogicalBottom;
bool m_usesRepaintBounds;
};
static void deleteLineRange(LineLayoutState& layoutState, RenderArena* arena, RootInlineBox* startLine, RootInlineBox* stopLine = 0)
{
RootInlineBox* boxToDelete = startLine;
while (boxToDelete && boxToDelete != stopLine) {
layoutState.updateRepaintRangeFromBox(boxToDelete);
RootInlineBox* next = boxToDelete->nextRootBox();
boxToDelete->deleteLine(arena);
boxToDelete = next;
}
}
void RenderBlock::layoutRunsAndFloats(LineLayoutState& layoutState, bool hasInlineChild)
{
InlineBidiResolver resolver;
RootInlineBox* startLine = determineStartPosition(layoutState, resolver);
unsigned consecutiveHyphenatedLines = 0;
if (startLine) {
for (RootInlineBox* line = startLine->prevRootBox(); line && line->isHyphenated(); line = line->prevRootBox())
consecutiveHyphenatedLines++;
}
if (layoutState.isFullLayout() && hasInlineChild && !selfNeedsLayout()) {
setNeedsLayout(true, MarkOnlyThis); RenderView* v = view();
if (v && !v->doingFullRepaint() && hasLayer()) {
repaintUsingContainer(containerForRepaint(), layer()->repaintRect());
}
}
if (m_floatingObjects && !m_floatingObjects->set().isEmpty())
layoutState.setLastFloat(m_floatingObjects->set().last());
InlineIterator cleanLineStart;
BidiStatus cleanLineBidiStatus;
if (!layoutState.isFullLayout() && startLine)
determineEndPosition(layoutState, startLine, cleanLineStart, cleanLineBidiStatus);
if (startLine) {
if (!layoutState.usesRepaintBounds())
layoutState.setRepaintRange(logicalHeight());
deleteLineRange(layoutState, renderArena(), startLine);
}
if (!layoutState.isFullLayout() && lastRootBox() && lastRootBox()->endsWithBreak()) {
if (InlineBox* lastLeafChild = lastRootBox()->lastLeafChild()) {
RenderObject* lastObject = lastLeafChild->renderer();
if (!lastObject->isBR())
lastObject = lastRootBox()->firstLeafChild()->renderer();
if (lastObject->isBR()) {
EClear clear = lastObject->style()->clear();
if (clear != CNONE)
newLine(clear);
}
}
}
layoutRunsAndFloatsInRange(layoutState, resolver, cleanLineStart, cleanLineBidiStatus, consecutiveHyphenatedLines);
linkToEndLineIfNeeded(layoutState);
repaintDirtyFloats(layoutState.floats());
}
void RenderBlock::layoutRunsAndFloatsInRange(LineLayoutState& layoutState, InlineBidiResolver& resolver, const InlineIterator& cleanLineStart, const BidiStatus& cleanLineBidiStatus, unsigned consecutiveHyphenatedLines)
{
RenderStyle* styleToUse = style();
bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
LineMidpointState& lineMidpointState = resolver.midpointState();
InlineIterator end = resolver.position();
bool checkForEndLineMatch = layoutState.endLine();
LineBreakIteratorInfo lineBreakIteratorInfo;
VerticalPositionCache verticalPositionCache;
LineBreaker lineBreaker(this);
while (!end.atEnd()) {
if (checkForEndLineMatch) {
layoutState.setEndLineMatched(matchedEndLine(layoutState, resolver, cleanLineStart, cleanLineBidiStatus));
if (layoutState.endLineMatched()) {
resolver.setPosition(InlineIterator(resolver.position().root(), 0, 0), 0);
break;
}
}
lineMidpointState.reset();
layoutState.lineInfo().setEmpty(true);
const InlineIterator oldEnd = end;
bool isNewUBAParagraph = layoutState.lineInfo().previousLineBrokeCleanly();
FloatingObject* lastFloatFromPreviousLine = (m_floatingObjects && !m_floatingObjects->set().isEmpty()) ? m_floatingObjects->set().last() : 0;
end = lineBreaker.nextLineBreak(resolver, layoutState.lineInfo(), lineBreakIteratorInfo, lastFloatFromPreviousLine, consecutiveHyphenatedLines);
if (resolver.position().atEnd()) {
resolver.runs().deleteRuns();
resolver.markCurrentRunEmpty(); layoutState.setCheckForFloatsFromLastLine(true);
resolver.setPosition(InlineIterator(resolver.position().root(), 0, 0), 0);
break;
}
ASSERT(end != resolver.position());
if (layoutState.lineInfo().isEmpty()) {
if (lastRootBox())
lastRootBox()->setLineBreakInfo(end.m_obj, end.m_pos, resolver.status());
} else {
VisualDirectionOverride override = (styleToUse->rtlOrdering() == VisualOrder ? (styleToUse->direction() == LTR ? VisualLeftToRightOverride : VisualRightToLeftOverride) : NoVisualOverride);
if (isNewUBAParagraph && styleToUse->unicodeBidi() == Plaintext && !resolver.context()->parent()) {
TextDirection direction = styleToUse->direction();
determineDirectionality(direction, resolver.position());
resolver.setStatus(BidiStatus(direction, isOverride(styleToUse->unicodeBidi())));
}
BidiRunList<BidiRun>& bidiRuns = resolver.runs();
constructBidiRuns(resolver, bidiRuns, end, override, layoutState.lineInfo().previousLineBrokeCleanly());
ASSERT(resolver.position() == end);
BidiRun* trailingSpaceRun = !layoutState.lineInfo().previousLineBrokeCleanly() ? handleTrailingSpaces(bidiRuns, resolver.context()) : 0;
if (bidiRuns.runCount() && lineBreaker.lineWasHyphenated()) {
bidiRuns.logicallyLastRun()->m_hasHyphen = true;
consecutiveHyphenatedLines++;
} else
consecutiveHyphenatedLines = 0;
LayoutUnit oldLogicalHeight = logicalHeight();
RootInlineBox* lineBox = createLineBoxesFromBidiRuns(bidiRuns, end, layoutState.lineInfo(), verticalPositionCache, trailingSpaceRun);
bidiRuns.deleteRuns();
resolver.markCurrentRunEmpty();
if (lineBox) {
lineBox->setLineBreakInfo(end.m_obj, end.m_pos, resolver.status());
if (layoutState.usesRepaintBounds())
layoutState.updateRepaintRangeFromBox(lineBox);
if (paginated) {
LayoutUnit adjustment = 0;
adjustLinePositionForPagination(lineBox, adjustment);
if (adjustment) {
LayoutUnit oldLineWidth = availableLogicalWidthForLine(oldLogicalHeight, layoutState.lineInfo().isFirstLine());
lineBox->adjustBlockDirectionPosition(adjustment);
if (layoutState.usesRepaintBounds())
layoutState.updateRepaintRangeFromBox(lineBox);
if (availableLogicalWidthForLine(oldLogicalHeight + adjustment, layoutState.lineInfo().isFirstLine()) != oldLineWidth) {
lineBox->deleteLine(renderArena());
removeFloatingObjectsBelow(lastFloatFromPreviousLine, oldLogicalHeight);
setLogicalHeight(oldLogicalHeight + adjustment);
resolver.setPositionIgnoringNestedIsolates(oldEnd);
end = oldEnd;
continue;
}
setLogicalHeight(lineBox->lineBottomWithLeading());
}
}
}
for (size_t i = 0; i < lineBreaker.positionedObjects().size(); ++i)
setStaticPositions(this, lineBreaker.positionedObjects()[i]);
layoutState.lineInfo().setFirstLine(false);
newLine(lineBreaker.clear());
}
if (m_floatingObjects && lastRootBox()) {
const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
FloatingObjectSetIterator it = floatingObjectSet.begin();
FloatingObjectSetIterator end = floatingObjectSet.end();
if (layoutState.lastFloat()) {
FloatingObjectSetIterator lastFloatIterator = floatingObjectSet.find(layoutState.lastFloat());
ASSERT(lastFloatIterator != end);
++lastFloatIterator;
it = lastFloatIterator;
}
for (; it != end; ++it) {
FloatingObject* f = *it;
appendFloatingObjectToLastLine(f);
ASSERT(f->m_renderer == layoutState.floats()[layoutState.floatIndex()].object);
if (layoutState.floats()[layoutState.floatIndex()].rect != f->frameRect())
checkForEndLineMatch = false;
layoutState.setFloatIndex(layoutState.floatIndex() + 1);
}
layoutState.setLastFloat(!floatingObjectSet.isEmpty() ? floatingObjectSet.last() : 0);
}
lineMidpointState.reset();
resolver.setPosition(end, numberOfIsolateAncestors(end));
}
}
void RenderBlock::linkToEndLineIfNeeded(LineLayoutState& layoutState)
{
if (layoutState.endLine()) {
if (layoutState.endLineMatched()) {
bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
LayoutUnit delta = logicalHeight() - layoutState.endLineLogicalTop();
for (RootInlineBox* line = layoutState.endLine(); line; line = line->nextRootBox()) {
line->attachLine();
if (paginated) {
delta -= line->paginationStrut();
adjustLinePositionForPagination(line, delta);
}
if (delta) {
layoutState.updateRepaintRangeFromBox(line, delta);
line->adjustBlockDirectionPosition(delta);
}
if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
Vector<RenderBox*>::iterator end = cleanLineFloats->end();
for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
FloatingObject* floatingObject = insertFloatingObject(*f);
ASSERT(!floatingObject->m_originatingLine);
floatingObject->m_originatingLine = line;
setLogicalHeight(logicalTopForChild(*f) - marginBeforeForChild(*f) + delta);
positionNewFloats();
}
}
}
setLogicalHeight(lastRootBox()->lineBottomWithLeading());
} else {
deleteLineRange(layoutState, renderArena(), layoutState.endLine());
}
}
if (m_floatingObjects && (layoutState.checkForFloatsFromLastLine() || positionNewFloats()) && lastRootBox()) {
if (layoutState.checkForFloatsFromLastLine()) {
LayoutUnit bottomVisualOverflow = lastRootBox()->logicalBottomVisualOverflow();
LayoutUnit bottomLayoutOverflow = lastRootBox()->logicalBottomLayoutOverflow();
TrailingFloatsRootInlineBox* trailingFloatsLineBox = new (renderArena()) TrailingFloatsRootInlineBox(this);
m_lineBoxes.appendLineBox(trailingFloatsLineBox);
trailingFloatsLineBox->setConstructed();
GlyphOverflowAndFallbackFontsMap textBoxDataMap;
VerticalPositionCache verticalPositionCache;
LayoutUnit blockLogicalHeight = logicalHeight();
trailingFloatsLineBox->alignBoxesInBlockDirection(blockLogicalHeight, textBoxDataMap, verticalPositionCache);
trailingFloatsLineBox->setLineTopBottomPositions(blockLogicalHeight, blockLogicalHeight, blockLogicalHeight, blockLogicalHeight);
trailingFloatsLineBox->setPaginatedLineWidth(availableLogicalWidthForContent(blockLogicalHeight));
LayoutRect logicalLayoutOverflow(0, blockLogicalHeight, 1, bottomLayoutOverflow - blockLogicalHeight);
LayoutRect logicalVisualOverflow(0, blockLogicalHeight, 1, bottomVisualOverflow - blockLogicalHeight);
trailingFloatsLineBox->setOverflowFromLogicalRects(logicalLayoutOverflow, logicalVisualOverflow, trailingFloatsLineBox->lineTop(), trailingFloatsLineBox->lineBottom());
}
const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
FloatingObjectSetIterator it = floatingObjectSet.begin();
FloatingObjectSetIterator end = floatingObjectSet.end();
if (layoutState.lastFloat()) {
FloatingObjectSetIterator lastFloatIterator = floatingObjectSet.find(layoutState.lastFloat());
ASSERT(lastFloatIterator != end);
++lastFloatIterator;
it = lastFloatIterator;
}
for (; it != end; ++it)
appendFloatingObjectToLastLine(*it);
layoutState.setLastFloat(!floatingObjectSet.isEmpty() ? floatingObjectSet.last() : 0);
}
}
void RenderBlock::repaintDirtyFloats(Vector<FloatWithRect>& floats)
{
size_t floatCount = floats.size();
for (size_t i = 0; i < floatCount; ++i) {
if (!floats[i].everHadLayout) {
RenderBox* f = floats[i].object;
if (!f->x() && !f->y() && f->checkForRepaintDuringLayout())
f->repaint();
}
}
}
void RenderBlock::layoutInlineChildren(bool relayoutChildren, LayoutUnit& repaintLogicalTop, LayoutUnit& repaintLogicalBottom)
{
m_overflow.clear();
setLogicalHeight(borderBefore() + paddingBefore());
if (view()->layoutState() && view()->layoutState()->lineGrid() == this)
layoutLineGridBox();
bool isFullLayout = !firstLineBox() || selfNeedsLayout() || relayoutChildren;
LineLayoutState layoutState(isFullLayout, repaintLogicalTop, repaintLogicalBottom);
if (isFullLayout)
lineBoxes()->deleteLineBoxes(renderArena());
bool hasTextOverflow = style()->textOverflow() && hasOverflowClip();
if (hasTextOverflow)
deleteEllipsisLineBoxes();
if (firstChild()) {
bool hasInlineChild = false;
for (InlineWalker walker(this); !walker.atEnd(); walker.advance()) {
RenderObject* o = walker.current();
if (!hasInlineChild && o->isInline())
hasInlineChild = true;
if (o->isReplaced() || o->isFloating() || o->isPositioned()) {
RenderBox* box = toRenderBox(o);
if (relayoutChildren || box->hasRelativeDimensions())
o->setChildNeedsLayout(true, MarkOnlyThis);
if (relayoutChildren && box->needsPreferredWidthsRecalculation())
o->setPreferredLogicalWidthsDirty(true, MarkOnlyThis);
if (o->isPositioned())
o->containingBlock()->insertPositionedObject(box);
else if (o->isFloating())
layoutState.floats().append(FloatWithRect(box));
else if (layoutState.isFullLayout() || o->needsLayout()) {
toRenderBox(o)->dirtyLineBoxes(layoutState.isFullLayout());
}
} else if (o->isText() || (o->isRenderInline() && !walker.atEndOfInline())) {
if (!o->isText())
toRenderInline(o)->updateAlwaysCreateLineBoxes(layoutState.isFullLayout());
if (layoutState.isFullLayout() || o->selfNeedsLayout())
dirtyLineBoxesForRenderer(o, layoutState.isFullLayout());
o->setNeedsLayout(false);
}
}
layoutRunsAndFloats(layoutState, hasInlineChild);
}
int lastLineAnnotationsAdjustment = 0;
if (lastRootBox()) {
LayoutUnit lowestAllowedPosition = max(lastRootBox()->lineBottom(), logicalHeight() + paddingAfter());
if (!style()->isFlippedLinesWritingMode())
lastLineAnnotationsAdjustment = lastRootBox()->computeUnderAnnotationAdjustment(lowestAllowedPosition);
else
lastLineAnnotationsAdjustment = lastRootBox()->computeOverAnnotationAdjustment(lowestAllowedPosition);
}
setLogicalHeight(logicalHeight() + lastLineAnnotationsAdjustment + borderAfter() + paddingAfter() + scrollbarLogicalHeight());
if (!firstLineBox() && hasLineIfEmpty())
setLogicalHeight(logicalHeight() + lineHeight(true, isHorizontalWritingMode() ? HorizontalLine : VerticalLine, PositionOfInteriorLineBoxes));
if (hasTextOverflow)
checkLinesForTextOverflow();
}
void RenderBlock::checkFloatsInCleanLine(RootInlineBox* line, Vector<FloatWithRect>& floats, size_t& floatIndex, bool& encounteredNewFloat, bool& dirtiedByFloat)
{
Vector<RenderBox*>* cleanLineFloats = line->floatsPtr();
if (!cleanLineFloats)
return;
Vector<RenderBox*>::iterator end = cleanLineFloats->end();
for (Vector<RenderBox*>::iterator it = cleanLineFloats->begin(); it != end; ++it) {
RenderBox* floatingBox = *it;
floatingBox->layoutIfNeeded();
LayoutSize newSize(floatingBox->width() + floatingBox->marginWidth(), floatingBox->height() + floatingBox->marginHeight());
ASSERT(floatIndex < floats.size());
if (floats[floatIndex].object != floatingBox) {
encounteredNewFloat = true;
return;
}
if (floats[floatIndex].rect.size() != newSize) {
LayoutUnit floatTop = isHorizontalWritingMode() ? floats[floatIndex].rect.y() : floats[floatIndex].rect.x();
LayoutUnit floatHeight = isHorizontalWritingMode() ? max(floats[floatIndex].rect.height(), newSize.height())
: max(floats[floatIndex].rect.width(), newSize.width());
floatHeight = min(floatHeight, MAX_LAYOUT_UNIT - floatTop);
line->markDirty();
markLinesDirtyInBlockRange(line->lineBottomWithLeading(), floatTop + floatHeight, line);
floats[floatIndex].rect.setSize(newSize);
dirtiedByFloat = true;
}
floatIndex++;
}
}
RootInlineBox* RenderBlock::determineStartPosition(LineLayoutState& layoutState, InlineBidiResolver& resolver)
{
RootInlineBox* curr = 0;
RootInlineBox* last = 0;
bool dirtiedByFloat = false;
if (!layoutState.isFullLayout()) {
bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
LayoutUnit paginationDelta = 0;
size_t floatIndex = 0;
for (curr = firstRootBox(); curr && !curr->isDirty(); curr = curr->nextRootBox()) {
if (paginated) {
if (lineWidthForPaginatedLineChanged(curr)) {
curr->markDirty();
break;
}
paginationDelta -= curr->paginationStrut();
adjustLinePositionForPagination(curr, paginationDelta);
if (paginationDelta) {
if (containsFloats() || !layoutState.floats().isEmpty()) {
layoutState.markForFullLayout();
break;
}
layoutState.updateRepaintRangeFromBox(curr, paginationDelta);
curr->adjustBlockDirectionPosition(paginationDelta);
}
}
bool encounteredNewFloat = false;
checkFloatsInCleanLine(curr, layoutState.floats(), floatIndex, encounteredNewFloat, dirtiedByFloat);
if (encounteredNewFloat)
layoutState.markForFullLayout();
if (dirtiedByFloat || layoutState.isFullLayout())
break;
}
if (!curr && floatIndex < layoutState.floats().size())
layoutState.markForFullLayout();
}
if (layoutState.isFullLayout()) {
RenderArena* arena = renderArena();
curr = firstRootBox();
while (curr) {
RootInlineBox* next = curr->nextRootBox();
curr->deleteLine(arena);
curr = next;
}
ASSERT(!firstLineBox() && !lastLineBox());
} else {
if (curr) {
if (RootInlineBox* prevRootBox = curr->prevRootBox()) {
if (!dirtiedByFloat && (!prevRootBox->endsWithBreak() || (prevRootBox->lineBreakObj()->isText() && prevRootBox->lineBreakPos() >= toRenderText(prevRootBox->lineBreakObj())->textLength())))
curr = prevRootBox;
}
} else {
if (lastRootBox() && !lastRootBox()->endsWithBreak())
curr = lastRootBox();
}
last = curr ? curr->prevRootBox() : lastRootBox();
}
unsigned numCleanFloats = 0;
if (!layoutState.floats().isEmpty()) {
LayoutUnit savedLogicalHeight = logicalHeight();
RootInlineBox* line = firstRootBox();
while (line != curr) {
if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
Vector<RenderBox*>::iterator end = cleanLineFloats->end();
for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
FloatingObject* floatingObject = insertFloatingObject(*f);
ASSERT(!floatingObject->m_originatingLine);
floatingObject->m_originatingLine = line;
setLogicalHeight(logicalTopForChild(*f) - marginBeforeForChild(*f));
positionNewFloats();
ASSERT(layoutState.floats()[numCleanFloats].object == *f);
numCleanFloats++;
}
}
line = line->nextRootBox();
}
setLogicalHeight(savedLogicalHeight);
}
layoutState.setFloatIndex(numCleanFloats);
layoutState.lineInfo().setFirstLine(!last);
layoutState.lineInfo().setPreviousLineBrokeCleanly(!last || last->endsWithBreak());
if (last) {
setLogicalHeight(last->lineBottomWithLeading());
InlineIterator iter = InlineIterator(this, last->lineBreakObj(), last->lineBreakPos());
resolver.setPosition(iter, numberOfIsolateAncestors(iter));
resolver.setStatus(last->lineBreakBidiStatus());
} else {
TextDirection direction = style()->direction();
if (style()->unicodeBidi() == Plaintext)
determineDirectionality(direction, InlineIterator(this, bidiFirstSkippingEmptyInlines(this), 0));
resolver.setStatus(BidiStatus(direction, isOverride(style()->unicodeBidi())));
InlineIterator iter = InlineIterator(this, bidiFirstSkippingEmptyInlines(this, &resolver), 0);
resolver.setPosition(iter, numberOfIsolateAncestors(iter));
}
return curr;
}
void RenderBlock::determineEndPosition(LineLayoutState& layoutState, RootInlineBox* startLine, InlineIterator& cleanLineStart, BidiStatus& cleanLineBidiStatus)
{
ASSERT(!layoutState.endLine());
size_t floatIndex = layoutState.floatIndex();
RootInlineBox* last = 0;
for (RootInlineBox* curr = startLine->nextRootBox(); curr; curr = curr->nextRootBox()) {
if (!curr->isDirty()) {
bool encounteredNewFloat = false;
bool dirtiedByFloat = false;
checkFloatsInCleanLine(curr, layoutState.floats(), floatIndex, encounteredNewFloat, dirtiedByFloat);
if (encounteredNewFloat)
return;
}
if (curr->isDirty())
last = 0;
else if (!last)
last = curr;
}
if (!last)
return;
RootInlineBox* prev = last->prevRootBox();
cleanLineStart = InlineIterator(this, prev->lineBreakObj(), prev->lineBreakPos());
cleanLineBidiStatus = prev->lineBreakBidiStatus();
layoutState.setEndLineLogicalTop(prev->lineBottomWithLeading());
for (RootInlineBox* line = last; line; line = line->nextRootBox())
line->extractLine();
layoutState.setEndLine(last);
}
bool RenderBlock::checkPaginationAndFloatsAtEndLine(LineLayoutState& layoutState)
{
LayoutUnit lineDelta = logicalHeight() - layoutState.endLineLogicalTop();
bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
if (paginated && inRenderFlowThread()) {
for (RootInlineBox* lineBox = layoutState.endLine(); lineBox; lineBox = lineBox->nextRootBox()) {
if (paginated) {
LayoutUnit oldPaginationStrut = lineBox->paginationStrut();
lineDelta -= oldPaginationStrut;
adjustLinePositionForPagination(lineBox, lineDelta);
lineBox->setPaginationStrut(oldPaginationStrut);
}
if (lineWidthForPaginatedLineChanged(lineBox, lineDelta))
return false;
}
}
if (!lineDelta || !m_floatingObjects)
return true;
LayoutUnit logicalTop = min(logicalHeight(), layoutState.endLineLogicalTop());
RootInlineBox* lastLine = layoutState.endLine();
while (RootInlineBox* nextLine = lastLine->nextRootBox())
lastLine = nextLine;
LayoutUnit logicalBottom = lastLine->lineBottomWithLeading() + absoluteValue(lineDelta);
const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
FloatingObjectSetIterator end = floatingObjectSet.end();
for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
FloatingObject* f = *it;
if (logicalBottomForFloat(f) >= logicalTop && logicalBottomForFloat(f) < logicalBottom)
return false;
}
return true;
}
bool RenderBlock::matchedEndLine(LineLayoutState& layoutState, const InlineBidiResolver& resolver, const InlineIterator& endLineStart, const BidiStatus& endLineStatus)
{
if (resolver.position() == endLineStart) {
if (resolver.status() != endLineStatus)
return false;
return checkPaginationAndFloatsAtEndLine(layoutState);
}
static int numLines = 8; RootInlineBox* originalEndLine = layoutState.endLine();
RootInlineBox* line = originalEndLine;
for (int i = 0; i < numLines && line; i++, line = line->nextRootBox()) {
if (line->lineBreakObj() == resolver.position().m_obj && line->lineBreakPos() == resolver.position().m_pos) {
if (line->lineBreakBidiStatus() != resolver.status())
return false;
bool matched = false;
RootInlineBox* result = line->nextRootBox();
layoutState.setEndLine(result);
if (result) {
layoutState.setEndLineLogicalTop(line->lineBottomWithLeading());
matched = checkPaginationAndFloatsAtEndLine(layoutState);
}
deleteLineRange(layoutState, renderArena(), originalEndLine, result);
return matched;
}
}
return false;
}
static inline bool skipNonBreakingSpace(const InlineIterator& it, const LineInfo& lineInfo)
{
if (it.m_obj->style()->nbspMode() != SPACE || it.current() != noBreakSpace)
return false;
if (lineInfo.isEmpty() && lineInfo.previousLineBrokeCleanly())
return false;
return true;
}
enum WhitespacePosition { LeadingWhitespace, TrailingWhitespace };
static inline bool shouldCollapseWhiteSpace(const RenderStyle* style, const LineInfo& lineInfo, WhitespacePosition whitespacePosition)
{
return style->collapseWhiteSpace()
|| (whitespacePosition == TrailingWhitespace && style->whiteSpace() == PRE_WRAP && (!lineInfo.isEmpty() || !lineInfo.previousLineBrokeCleanly()));
}
static bool requiresLineBoxForContent(RenderInline* flow, const LineInfo& lineInfo)
{
RenderObject* parent = flow->parent();
if (flow->document()->inNoQuirksMode()
&& (flow->style(lineInfo.isFirstLine())->lineHeight() != parent->style(lineInfo.isFirstLine())->lineHeight()
|| flow->style()->verticalAlign() != parent->style()->verticalAlign()
|| !parent->style()->font().fontMetrics().hasIdenticalAscentDescentAndLineGap(flow->style()->font().fontMetrics())))
return true;
return false;
}
static bool alwaysRequiresLineBox(RenderInline* flow)
{
return !flow->firstChild() && flow->hasInlineDirectionBordersPaddingOrMargin();
}
static bool requiresLineBox(const InlineIterator& it, const LineInfo& lineInfo = LineInfo(), WhitespacePosition whitespacePosition = LeadingWhitespace)
{
if (it.m_obj->isFloatingOrPositioned())
return false;
if (it.m_obj->isRenderInline() && !alwaysRequiresLineBox(toRenderInline(it.m_obj)) && !requiresLineBoxForContent(toRenderInline(it.m_obj), lineInfo))
return false;
if (!shouldCollapseWhiteSpace(it.m_obj->style(), lineInfo, whitespacePosition) || it.m_obj->isBR())
return true;
UChar current = it.current();
return current != ' ' && current != '\t' && current != softHyphen && (current != '\n' || it.m_obj->preservesNewline())
&& !skipNonBreakingSpace(it, lineInfo);
}
bool RenderBlock::generatesLineBoxesForInlineChild(RenderObject* inlineObj)
{
ASSERT(inlineObj->parent() == this);
InlineIterator it(this, inlineObj, 0);
while (!it.atEnd() && !requiresLineBox(it))
it.increment();
return !it.atEnd();
}
void RenderBlock::LineBreaker::skipTrailingWhitespace(InlineIterator& iterator, const LineInfo& lineInfo)
{
while (!iterator.atEnd() && !requiresLineBox(iterator, lineInfo, TrailingWhitespace)) {
RenderObject* object = iterator.m_obj;
if (object->isPositioned())
setStaticPositions(m_block, toRenderBox(object));
else if (object->isFloating())
m_block->insertFloatingObject(toRenderBox(object));
iterator.increment();
}
}
void RenderBlock::LineBreaker::skipLeadingWhitespace(InlineBidiResolver& resolver, LineInfo& lineInfo,
FloatingObject* lastFloatFromPreviousLine, LineWidth& width)
{
while (!resolver.position().atEnd() && !requiresLineBox(resolver.position(), lineInfo, LeadingWhitespace)) {
RenderObject* object = resolver.position().m_obj;
if (object->isPositioned())
setStaticPositions(m_block, toRenderBox(object));
else if (object->isFloating())
m_block->positionNewFloatOnLine(m_block->insertFloatingObject(toRenderBox(object)), lastFloatFromPreviousLine, lineInfo, width);
else if (object->isText() && object->style()->hasTextCombine() && object->isCombineText() && !toRenderCombineText(object)->isCombined()) {
toRenderCombineText(object)->combineText();
if (toRenderCombineText(object)->isCombined())
continue;
}
resolver.increment();
}
resolver.commitExplicitEmbedding();
}
static bool shouldSkipWhitespaceAfterStartObject(RenderBlock* block, RenderObject* o, LineMidpointState& lineMidpointState)
{
RenderObject* next = bidiNextSkippingEmptyInlines(block, o);
if (next && !next->isBR() && next->isText() && toRenderText(next)->textLength() > 0) {
RenderText* nextText = toRenderText(next);
UChar nextChar = nextText->characters()[0];
if (nextText->style()->isCollapsibleWhiteSpace(nextChar)) {
addMidpoint(lineMidpointState, InlineIterator(0, o, 0));
return true;
}
}
return false;
}
static inline float textWidth(RenderText* text, unsigned from, unsigned len, const Font& font, float xPos, bool isFixedPitch, bool collapseWhiteSpace)
{
if (isFixedPitch || (!from && len == text->textLength()) || text->style()->hasTextCombine())
return text->width(from, len, font, xPos);
TextRun run = RenderBlock::constructTextRun(text, font, text->characters() + from, len, text->style());
run.setCharactersLength(text->textLength() - from);
ASSERT(run.charactersLength() >= run.length());
run.setCharacterScanForCodePath(!text->canUseSimpleFontCodePath());
run.setAllowTabs(!collapseWhiteSpace);
run.setXPos(xPos);
return font.width(run);
}
static void tryHyphenating(RenderText* text, const Font& font, const AtomicString& localeIdentifier, unsigned consecutiveHyphenatedLines, int consecutiveHyphenatedLinesLimit, int minimumPrefixLength, int minimumSuffixLength, int lastSpace, int pos, float xPos, int availableWidth, bool isFixedPitch, bool collapseWhiteSpace, int lastSpaceWordSpacing, InlineIterator& lineBreak, int nextBreakable, bool& hyphenated)
{
if (minimumPrefixLength < 0)
minimumPrefixLength = 2;
if (minimumSuffixLength < 0)
minimumSuffixLength = 2;
if (pos - lastSpace <= minimumSuffixLength)
return;
if (consecutiveHyphenatedLinesLimit >= 0 && consecutiveHyphenatedLines >= static_cast<unsigned>(consecutiveHyphenatedLinesLimit))
return;
int hyphenWidth = measureHyphenWidth(text, font);
float maxPrefixWidth = availableWidth - xPos - hyphenWidth - lastSpaceWordSpacing;
if (maxPrefixWidth <= font.pixelSize() * 5 / 4)
return;
TextRun run = RenderBlock::constructTextRun(text, font, text->characters() + lastSpace, pos - lastSpace, text->style());
run.setCharactersLength(text->textLength() - lastSpace);
ASSERT(run.charactersLength() >= run.length());
run.setAllowTabs(!collapseWhiteSpace);
run.setXPos(xPos + lastSpaceWordSpacing);
unsigned prefixLength = font.offsetForPosition(run, maxPrefixWidth, false);
if (prefixLength < static_cast<unsigned>(minimumPrefixLength))
return;
prefixLength = lastHyphenLocation(text->characters() + lastSpace, pos - lastSpace, min(prefixLength, static_cast<unsigned>(pos - lastSpace - minimumSuffixLength)) + 1, localeIdentifier);
if (!prefixLength || prefixLength < static_cast<unsigned>(minimumPrefixLength))
return;
if (prefixLength == static_cast<unsigned>(minimumPrefixLength)) {
UChar characterAtLastSpace = text->characters()[lastSpace];
if (characterAtLastSpace == ' ' || characterAtLastSpace == '\n' || characterAtLastSpace == '\t' || characterAtLastSpace == noBreakSpace)
return;
}
ASSERT(pos - lastSpace - prefixLength >= static_cast<unsigned>(minimumSuffixLength));
#if !ASSERT_DISABLED
float prefixWidth = hyphenWidth + textWidth(text, lastSpace, prefixLength, font, xPos, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
ASSERT(xPos + prefixWidth <= availableWidth);
#else
UNUSED_PARAM(isFixedPitch);
#endif
lineBreak.moveTo(text, lastSpace + prefixLength, nextBreakable);
hyphenated = true;
}
class TrailingObjects {
public:
TrailingObjects();
void setTrailingWhitespace(RenderText*);
void clear();
void appendBoxIfNeeded(RenderBox*);
enum CollapseFirstSpaceOrNot { DoNotCollapseFirstSpace, CollapseFirstSpace };
void updateMidpointsForTrailingBoxes(LineMidpointState&, const InlineIterator& lBreak, CollapseFirstSpaceOrNot);
private:
RenderText* m_whitespace;
Vector<RenderBox*, 4> m_boxes;
};
TrailingObjects::TrailingObjects()
: m_whitespace(0)
{
}
inline void TrailingObjects::setTrailingWhitespace(RenderText* whitespace)
{
ASSERT(whitespace);
m_whitespace = whitespace;
}
inline void TrailingObjects::clear()
{
m_whitespace = 0;
m_boxes.clear();
}
inline void TrailingObjects::appendBoxIfNeeded(RenderBox* box)
{
if (m_whitespace)
m_boxes.append(box);
}
void TrailingObjects::updateMidpointsForTrailingBoxes(LineMidpointState& lineMidpointState, const InlineIterator& lBreak, CollapseFirstSpaceOrNot collapseFirstSpace)
{
if (!m_whitespace)
return;
if (lineMidpointState.numMidpoints % 2) {
int trailingSpaceMidpoint = lineMidpointState.numMidpoints - 1;
for ( ; trailingSpaceMidpoint > 0 && lineMidpointState.midpoints[trailingSpaceMidpoint].m_obj != m_whitespace; --trailingSpaceMidpoint) { }
ASSERT(trailingSpaceMidpoint >= 0);
if (collapseFirstSpace == CollapseFirstSpace)
lineMidpointState.midpoints[trailingSpaceMidpoint].m_pos--;
size_t currentMidpoint = trailingSpaceMidpoint + 1;
for (size_t i = 0; i < m_boxes.size(); ++i) {
if (currentMidpoint >= lineMidpointState.numMidpoints) {
InlineIterator ignoreStart(0, m_boxes[i], 0);
addMidpoint(lineMidpointState, ignoreStart); addMidpoint(lineMidpointState, ignoreStart); } else {
ASSERT(lineMidpointState.midpoints[currentMidpoint].m_obj == m_boxes[i]);
ASSERT(lineMidpointState.midpoints[currentMidpoint + 1].m_obj == m_boxes[i]);
}
currentMidpoint += 2;
}
} else if (!lBreak.m_obj) {
ASSERT(m_whitespace->isText());
ASSERT(collapseFirstSpace == CollapseFirstSpace);
unsigned length = m_whitespace->textLength();
unsigned pos = length >= 2 ? length - 2 : UINT_MAX;
InlineIterator endMid(0, m_whitespace, pos);
addMidpoint(lineMidpointState, endMid);
for (size_t i = 0; i < m_boxes.size(); ++i) {
InlineIterator ignoreStart(0, m_boxes[i], 0);
addMidpoint(lineMidpointState, ignoreStart); addMidpoint(lineMidpointState, ignoreStart); }
}
}
void RenderBlock::LineBreaker::reset()
{
m_positionedObjects.clear();
m_hyphenated = false;
m_clear = CNONE;
}
InlineIterator RenderBlock::LineBreaker::nextLineBreak(InlineBidiResolver& resolver, LineInfo& lineInfo,
LineBreakIteratorInfo& lineBreakIteratorInfo, FloatingObject* lastFloatFromPreviousLine, unsigned consecutiveHyphenatedLines)
{
reset();
ASSERT(resolver.position().root() == m_block);
bool appliedStartWidth = resolver.position().m_pos > 0;
bool includeEndWidth = true;
LineMidpointState& lineMidpointState = resolver.midpointState();
LineWidth width(m_block, lineInfo.isFirstLine());
skipLeadingWhitespace(resolver, lineInfo, lastFloatFromPreviousLine, width);
if (resolver.position().atEnd())
return resolver.position();
bool ignoringSpaces = false;
InlineIterator ignoreStart;
bool currentCharacterIsSpace = false;
bool currentCharacterIsWS = false;
TrailingObjects trailingObjects;
InlineIterator lBreak = resolver.position();
InlineIterator current = resolver.position();
RenderObject* last = current.m_obj;
bool atStart = true;
bool startingNewParagraph = lineInfo.previousLineBrokeCleanly();
lineInfo.setPreviousLineBrokeCleanly(false);
bool autoWrapWasEverTrueOnLine = false;
bool floatsFitOnLine = true;
RenderStyle* blockStyle = m_block->style();
bool allowImagesToBreak = !m_block->document()->inQuirksMode() || !m_block->isTableCell() || !blockStyle->logicalWidth().isIntrinsicOrAuto();
EWhiteSpace currWS = blockStyle->whiteSpace();
EWhiteSpace lastWS = currWS;
while (current.m_obj) {
RenderStyle* currentStyle = current.m_obj->style();
RenderObject* next = bidiNextSkippingEmptyInlines(m_block, current.m_obj);
if (next && next->parent() && !next->parent()->isDescendantOf(current.m_obj->parent()))
includeEndWidth = true;
currWS = current.m_obj->isReplaced() ? current.m_obj->parent()->style()->whiteSpace() : currentStyle->whiteSpace();
lastWS = last->isReplaced() ? last->parent()->style()->whiteSpace() : last->style()->whiteSpace();
bool autoWrap = RenderStyle::autoWrap(currWS);
autoWrapWasEverTrueOnLine = autoWrapWasEverTrueOnLine || autoWrap;
#if ENABLE(SVG)
bool preserveNewline = current.m_obj->isSVGInlineText() ? false : RenderStyle::preserveNewline(currWS);
#else
bool preserveNewline = RenderStyle::preserveNewline(currWS);
#endif
bool collapseWhiteSpace = RenderStyle::collapseWhiteSpace(currWS);
if (current.m_obj->isBR()) {
if (width.fitsOnLine()) {
lBreak.moveToStartOf(current.m_obj);
lBreak.increment();
if (startingNewParagraph)
lineInfo.setEmpty(false, m_block, &width);
trailingObjects.clear();
lineInfo.setPreviousLineBrokeCleanly(true);
if (!lineInfo.isEmpty())
m_clear = currentStyle->clear();
}
goto end;
}
if (current.m_obj->isPositioned()) {
RenderBox* box = toRenderBox(current.m_obj);
bool isInlineType = box->style()->isOriginalDisplayInlineType();
if (!isInlineType)
m_block->setStaticInlinePositionForChild(box, m_block->logicalHeight(), m_block->startOffsetForContent(m_block->logicalHeight()));
else {
box->layer()->setStaticBlockPosition(m_block->logicalHeight());
}
if (isInlineType || current.m_obj->container()->isRenderInline()) {
if (ignoringSpaces) {
ignoreStart.m_obj = current.m_obj;
ignoreStart.m_pos = 0;
addMidpoint(lineMidpointState, ignoreStart); addMidpoint(lineMidpointState, ignoreStart); }
trailingObjects.appendBoxIfNeeded(box);
} else
m_positionedObjects.append(box);
} else if (current.m_obj->isFloating()) {
RenderBox* floatBox = toRenderBox(current.m_obj);
FloatingObject* f = m_block->insertFloatingObject(floatBox);
if (floatsFitOnLine && width.fitsOnLine(m_block->logicalWidthForFloat(f))) {
m_block->positionNewFloatOnLine(f, lastFloatFromPreviousLine, lineInfo, width);
if (lBreak.m_obj == current.m_obj) {
ASSERT(!lBreak.m_pos);
lBreak.increment();
}
} else
floatsFitOnLine = false;
} else if (current.m_obj->isRenderInline()) {
ASSERT(!current.m_obj->firstChild());
RenderInline* flowBox = toRenderInline(current.m_obj);
bool requiresLineBox = alwaysRequiresLineBox(flowBox);
if (requiresLineBox || requiresLineBoxForContent(flowBox, lineInfo)) {
if (requiresLineBox)
lineInfo.setEmpty(false, m_block, &width);
if (ignoringSpaces) {
trailingObjects.clear();
addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, 0)); addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, 0)); } else if (blockStyle->collapseWhiteSpace() && resolver.position().m_obj == current.m_obj
&& shouldSkipWhitespaceAfterStartObject(m_block, current.m_obj, lineMidpointState)) {
currentCharacterIsSpace = true;
currentCharacterIsWS = true;
ignoringSpaces = true;
}
}
width.addUncommittedWidth(borderPaddingMarginStart(flowBox) + borderPaddingMarginEnd(flowBox));
} else if (current.m_obj->isReplaced()) {
RenderBox* replacedBox = toRenderBox(current.m_obj);
replacedBox->layoutIfNeeded();
if ((autoWrap || RenderStyle::autoWrap(lastWS)) && (!current.m_obj->isImage() || allowImagesToBreak)) {
width.commit();
lBreak.moveToStartOf(current.m_obj);
}
if (ignoringSpaces)
addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, 0));
lineInfo.setEmpty(false, m_block, &width);
ignoringSpaces = false;
currentCharacterIsSpace = false;
currentCharacterIsWS = false;
trailingObjects.clear();
LayoutUnit replacedLogicalWidth = m_block->logicalWidthForChild(replacedBox) + m_block->marginStartForChild(replacedBox) + m_block->marginEndForChild(replacedBox) + inlineLogicalWidth(current.m_obj);
if (current.m_obj->isListMarker()) {
if (blockStyle->collapseWhiteSpace() && shouldSkipWhitespaceAfterStartObject(m_block, current.m_obj, lineMidpointState)) {
currentCharacterIsSpace = true;
currentCharacterIsWS = true;
ignoringSpaces = true;
}
if (toRenderListMarker(current.m_obj)->isInside())
width.addUncommittedWidth(replacedLogicalWidth);
} else
width.addUncommittedWidth(replacedLogicalWidth);
if (current.m_obj->isRubyRun())
width.applyOverhang(toRenderRubyRun(current.m_obj), last, next);
} else if (current.m_obj->isText()) {
if (!current.m_pos)
appliedStartWidth = false;
RenderText* t = toRenderText(current.m_obj);
#if ENABLE(SVG)
bool isSVGText = t->isSVGInlineText();
#endif
if (t->style()->hasTextCombine() && current.m_obj->isCombineText() && !toRenderCombineText(current.m_obj)->isCombined())
toRenderCombineText(current.m_obj)->combineText();
RenderStyle* style = t->style(lineInfo.isFirstLine());
const Font& f = style->font();
bool isFixedPitch = f.isFixedPitch();
bool canHyphenate = style->hyphens() == HyphensAuto && WebCore::canHyphenate(style->locale());
int lastSpace = current.m_pos;
float wordSpacing = currentStyle->wordSpacing();
float lastSpaceWordSpacing = 0;
float wordTrailingSpaceWidth = f.typesettingFeatures() & Kerning ? f.width(constructTextRun(t, f, &space, 1, style)) + wordSpacing : 0;
float wrapW = width.uncommittedWidth() + inlineLogicalWidth(current.m_obj, !appliedStartWidth, true);
float charWidth = 0;
bool breakNBSP = autoWrap && currentStyle->nbspMode() == SPACE;
bool breakWords = currentStyle->breakWords() && ((autoWrap && !width.committedWidth()) || currWS == PRE);
bool midWordBreak = false;
bool breakAll = currentStyle->wordBreak() == BreakAllWordBreak && autoWrap;
float hyphenWidth = 0;
if (t->isWordBreak()) {
width.commit();
lBreak.moveToStartOf(current.m_obj);
ASSERT(current.m_pos == t->textLength());
}
for (; current.m_pos < t->textLength(); current.fastIncrementInTextNode()) {
bool previousCharacterIsSpace = currentCharacterIsSpace;
bool previousCharacterIsWS = currentCharacterIsWS;
UChar c = current.current();
currentCharacterIsSpace = c == ' ' || c == '\t' || (!preserveNewline && (c == '\n'));
if (!collapseWhiteSpace || !currentCharacterIsSpace)
lineInfo.setEmpty(false, m_block, &width);
if (c == softHyphen && autoWrap && !hyphenWidth && style->hyphens() != HyphensNone) {
hyphenWidth = measureHyphenWidth(t, f);
width.addUncommittedWidth(hyphenWidth);
}
bool applyWordSpacing = false;
currentCharacterIsWS = currentCharacterIsSpace || (breakNBSP && c == noBreakSpace);
if ((breakAll || breakWords) && !midWordBreak) {
wrapW += charWidth;
bool midWordBreakIsBeforeSurrogatePair = U16_IS_LEAD(c) && current.m_pos + 1 < t->textLength() && U16_IS_TRAIL(t->characters()[current.m_pos + 1]);
charWidth = textWidth(t, current.m_pos, midWordBreakIsBeforeSurrogatePair ? 2 : 1, f, width.committedWidth() + wrapW, isFixedPitch, collapseWhiteSpace);
midWordBreak = width.committedWidth() + wrapW + charWidth > width.availableWidth();
}
if ((lineBreakIteratorInfo.first != t) || (lineBreakIteratorInfo.second.string() != t->characters())) {
lineBreakIteratorInfo.first = t;
lineBreakIteratorInfo.second.reset(t->characters(), t->textLength(), style->locale());
}
bool betweenWords = c == '\n' || (currWS != PRE && !atStart && isBreakable(lineBreakIteratorInfo.second, current.m_pos, current.m_nextBreakablePosition, breakNBSP)
&& (style->hyphens() != HyphensNone || (current.previousInSameNode() != softHyphen)));
if (betweenWords || midWordBreak) {
bool stoppedIgnoringSpaces = false;
if (ignoringSpaces) {
if (!currentCharacterIsSpace) {
ignoringSpaces = false;
lastSpaceWordSpacing = 0;
lastSpace = current.m_pos; addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos));
stoppedIgnoringSpaces = true;
} else {
continue;
}
}
float additionalTmpW;
if (wordTrailingSpaceWidth && currentCharacterIsSpace)
additionalTmpW = textWidth(t, lastSpace, current.m_pos + 1 - lastSpace, f, width.currentWidth(), isFixedPitch, collapseWhiteSpace) - wordTrailingSpaceWidth + lastSpaceWordSpacing;
else
additionalTmpW = textWidth(t, lastSpace, current.m_pos - lastSpace, f, width.currentWidth(), isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
width.addUncommittedWidth(additionalTmpW);
if (!appliedStartWidth) {
width.addUncommittedWidth(inlineLogicalWidth(current.m_obj, true, false));
appliedStartWidth = true;
}
applyWordSpacing = wordSpacing && currentCharacterIsSpace && !previousCharacterIsSpace;
if (!width.committedWidth() && autoWrap && !width.fitsOnLine())
width.fitBelowFloats();
if (autoWrap || breakWords) {
bool lineWasTooWide = false;
if (width.fitsOnLine() && currentCharacterIsWS && currentStyle->breakOnlyAfterWhiteSpace() && !midWordBreak) {
float charWidth = textWidth(t, current.m_pos, 1, f, width.currentWidth(), isFixedPitch, collapseWhiteSpace) + (applyWordSpacing ? wordSpacing : 0);
if (!width.fitsOnLine(charWidth)) {
lineWasTooWide = true;
lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
skipTrailingWhitespace(lBreak, lineInfo);
}
}
if (lineWasTooWide || !width.fitsOnLine()) {
if (canHyphenate && !width.fitsOnLine()) {
tryHyphenating(t, f, style->locale(), consecutiveHyphenatedLines, blockStyle->hyphenationLimitLines(), style->hyphenationLimitBefore(), style->hyphenationLimitAfter(), lastSpace, current.m_pos, width.currentWidth() - additionalTmpW, width.availableWidth(), isFixedPitch, collapseWhiteSpace, lastSpaceWordSpacing, lBreak, current.m_nextBreakablePosition, m_hyphenated);
if (m_hyphenated)
goto end;
}
if (lBreak.atTextParagraphSeparator()) {
if (!stoppedIgnoringSpaces && current.m_pos > 0) {
addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos - 1)); addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos)); }
lBreak.increment();
lineInfo.setPreviousLineBrokeCleanly(true);
}
if (lBreak.m_obj && lBreak.m_pos && lBreak.m_obj->isText() && toRenderText(lBreak.m_obj)->textLength() && toRenderText(lBreak.m_obj)->characters()[lBreak.m_pos - 1] == softHyphen && style->hyphens() != HyphensNone)
m_hyphenated = true;
if (ignoringSpaces || !currentStyle->collapseWhiteSpace() || !currentCharacterIsSpace || !previousCharacterIsSpace)
goto end;
} else {
if (!betweenWords || (midWordBreak && !autoWrap))
width.addUncommittedWidth(-additionalTmpW);
if (hyphenWidth) {
width.addUncommittedWidth(-hyphenWidth);
hyphenWidth = 0;
}
}
}
if (c == '\n' && preserveNewline) {
if (!stoppedIgnoringSpaces && current.m_pos > 0) {
addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos - 1)); addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos)); }
lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
lBreak.increment();
lineInfo.setPreviousLineBrokeCleanly(true);
return lBreak;
}
if (autoWrap && betweenWords) {
width.commit();
wrapW = 0;
lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
breakWords = false;
}
if (midWordBreak && !U16_IS_TRAIL(c) && !(category(c) & (Mark_NonSpacing | Mark_Enclosing | Mark_SpacingCombining))) {
lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
midWordBreak &= (breakWords || breakAll);
}
if (betweenWords) {
lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
lastSpace = current.m_pos;
}
if (!ignoringSpaces && currentStyle->collapseWhiteSpace()) {
if (currentCharacterIsSpace && previousCharacterIsSpace) {
ignoringSpaces = true;
addMidpoint(lineMidpointState, ignoreStart);
trailingObjects.updateMidpointsForTrailingBoxes(lineMidpointState, InlineIterator(), TrailingObjects::DoNotCollapseFirstSpace);
}
}
} else if (ignoringSpaces) {
ignoringSpaces = false;
lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
lastSpace = current.m_pos; addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos));
}
#if ENABLE(SVG)
if (isSVGText && current.m_pos > 0) {
if (toRenderSVGInlineText(t)->characterStartsNewTextChunk(current.m_pos)) {
addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos - 1));
addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos));
}
}
#endif
if (currentCharacterIsSpace && !previousCharacterIsSpace) {
ignoreStart.m_obj = current.m_obj;
ignoreStart.m_pos = current.m_pos;
}
if (!currentCharacterIsWS && previousCharacterIsWS) {
if (autoWrap && currentStyle->breakOnlyAfterWhiteSpace())
lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
}
if (collapseWhiteSpace && currentCharacterIsSpace && !ignoringSpaces)
trailingObjects.setTrailingWhitespace(toRenderText(current.m_obj));
else if (!currentStyle->collapseWhiteSpace() || !currentCharacterIsSpace)
trailingObjects.clear();
atStart = false;
}
float additionalTmpW = ignoringSpaces ? 0 : textWidth(t, lastSpace, current.m_pos - lastSpace, f, width.currentWidth(), isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
width.addUncommittedWidth(additionalTmpW + inlineLogicalWidth(current.m_obj, !appliedStartWidth, includeEndWidth));
includeEndWidth = false;
if (!width.fitsOnLine()) {
if (canHyphenate)
tryHyphenating(t, f, style->locale(), consecutiveHyphenatedLines, blockStyle->hyphenationLimitLines(), style->hyphenationLimitBefore(), style->hyphenationLimitAfter(), lastSpace, current.m_pos, width.currentWidth() - additionalTmpW, width.availableWidth(), isFixedPitch, collapseWhiteSpace, lastSpaceWordSpacing, lBreak, current.m_nextBreakablePosition, m_hyphenated);
if (!m_hyphenated && lBreak.previousInSameNode() == softHyphen && style->hyphens() != HyphensNone)
m_hyphenated = true;
if (m_hyphenated)
goto end;
}
} else
ASSERT_NOT_REACHED();
bool checkForBreak = autoWrap;
if (width.committedWidth() && !width.fitsOnLine() && lBreak.m_obj && currWS == NOWRAP)
checkForBreak = true;
else if (next && current.m_obj->isText() && next->isText() && !next->isBR() && (autoWrap || next->style()->autoWrap())) {
if (autoWrap && currentCharacterIsSpace)
checkForBreak = true;
else {
RenderText* nextText = toRenderText(next);
if (nextText->textLength()) {
UChar c = nextText->characters()[0];
checkForBreak = !currentCharacterIsSpace && (c == ' ' || c == '\t' || (c == '\n' && !next->preservesNewline()));
} else if (nextText->isWordBreak())
checkForBreak = true;
if (!width.fitsOnLine() && !width.committedWidth())
width.fitBelowFloats();
bool canPlaceOnLine = width.fitsOnLine() || !autoWrapWasEverTrueOnLine;
if (canPlaceOnLine && checkForBreak) {
width.commit();
lBreak.moveToStartOf(next);
}
}
}
if (checkForBreak && !width.fitsOnLine()) {
if (currentCharacterIsSpace && !ignoringSpaces && currentStyle->collapseWhiteSpace())
trailingObjects.clear();
if (width.committedWidth())
goto end;
width.fitBelowFloats();
if (!width.fitsOnLine())
goto end;
}
if (!current.m_obj->isFloatingOrPositioned()) {
last = current.m_obj;
if (last->isReplaced() && autoWrap && (!last->isImage() || allowImagesToBreak) && (!last->isListMarker() || toRenderListMarker(last)->isInside())) {
width.commit();
lBreak.moveToStartOf(next);
}
}
if (!collapseWhiteSpace)
currentCharacterIsSpace = false;
current.moveToStartOf(next);
atStart = false;
}
if (width.fitsOnLine() || lastWS == NOWRAP)
lBreak.clear();
end:
if (lBreak == resolver.position() && (!lBreak.m_obj || !lBreak.m_obj->isBR())) {
if (blockStyle->whiteSpace() == PRE && !current.m_pos) {
lBreak.moveTo(last, last->isText() ? last->length() : 0);
} else if (lBreak.m_obj) {
lBreak.moveTo(current.m_obj, current.m_pos);
}
}
if (lBreak == resolver.position())
lBreak.increment();
checkMidpoints(lineMidpointState, lBreak);
trailingObjects.updateMidpointsForTrailingBoxes(lineMidpointState, lBreak, TrailingObjects::CollapseFirstSpace);
if (lBreak.m_pos > 0) {
lBreak.m_pos--;
lBreak.increment();
}
return lBreak;
}
void RenderBlock::addOverflowFromInlineChildren()
{
LayoutUnit endPadding = hasOverflowClip() ? paddingEnd() : ZERO_LAYOUT_UNIT;
if (hasOverflowClip() && !endPadding && node() && node()->rendererIsEditable() && node() == node()->rootEditableElement() && style()->isLeftToRightDirection())
endPadding = 1;
for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
addLayoutOverflow(curr->paddedLayoutOverflowRect(endPadding));
if (!hasOverflowClip())
addVisualOverflow(curr->visualOverflowRect(curr->lineTop(), curr->lineBottom()));
}
}
void RenderBlock::deleteEllipsisLineBoxes()
{
for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox())
curr->clearTruncation();
}
void RenderBlock::checkLinesForTextOverflow()
{
const Font& font = style()->font();
DEFINE_STATIC_LOCAL(AtomicString, ellipsisStr, (&horizontalEllipsis, 1));
const Font& firstLineFont = firstLineStyle()->font();
int firstLineEllipsisWidth = firstLineFont.width(constructTextRun(this, firstLineFont, &horizontalEllipsis, 1, firstLineStyle()));
int ellipsisWidth = (font == firstLineFont) ? firstLineEllipsisWidth : font.width(constructTextRun(this, font, &horizontalEllipsis, 1, style()));
bool ltr = style()->isLeftToRightDirection();
for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
LayoutUnit blockRightEdge = logicalRightOffsetForLine(curr->y(), curr == firstRootBox());
LayoutUnit blockLeftEdge = logicalLeftOffsetForLine(curr->y(), curr == firstRootBox());
LayoutUnit lineBoxEdge = ltr ? curr->x() + curr->logicalWidth() : curr->x();
if ((ltr && lineBoxEdge > blockRightEdge) || (!ltr && lineBoxEdge < blockLeftEdge)) {
LayoutUnit width = curr == firstRootBox() ? firstLineEllipsisWidth : ellipsisWidth;
LayoutUnit blockEdge = ltr ? blockRightEdge : blockLeftEdge;
if (curr->lineCanAccommodateEllipsis(ltr, blockEdge, lineBoxEdge, width))
curr->placeEllipsis(ellipsisStr, ltr, blockLeftEdge, blockRightEdge, width);
}
}
}
bool RenderBlock::positionNewFloatOnLine(FloatingObject* newFloat, FloatingObject* lastFloatFromPreviousLine, LineInfo& lineInfo, LineWidth& width)
{
if (!positionNewFloats())
return false;
width.shrinkAvailableWidthForNewFloatIfNeeded(newFloat);
if (!newFloat->m_paginationStrut || !lineInfo.previousLineBrokeCleanly() || !lineInfo.isEmpty())
return true;
const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
ASSERT(floatingObjectSet.last() == newFloat);
LayoutUnit floatLogicalTop = logicalTopForFloat(newFloat);
int paginationStrut = newFloat->m_paginationStrut;
if (floatLogicalTop - paginationStrut != logicalHeight() + lineInfo.floatPaginationStrut())
return true;
FloatingObjectSetIterator it = floatingObjectSet.end();
--it; FloatingObjectSetIterator begin = floatingObjectSet.begin();
while (it != begin) {
--it;
FloatingObject* f = *it;
if (f == lastFloatFromPreviousLine)
break;
if (logicalTopForFloat(f) == logicalHeight() + lineInfo.floatPaginationStrut()) {
f->m_paginationStrut += paginationStrut;
RenderBox* o = f->m_renderer;
setLogicalTopForChild(o, logicalTopForChild(o) + marginBeforeForChild(o) + paginationStrut);
if (o->isRenderBlock())
toRenderBlock(o)->setChildNeedsLayout(true, MarkOnlyThis);
o->layoutIfNeeded();
LayoutUnit oldLogicalTop = logicalTopForFloat(f);
m_floatingObjects->removePlacedObject(f);
setLogicalTopForFloat(f, oldLogicalTop + paginationStrut);
m_floatingObjects->addPlacedObject(f);
}
}
lineInfo.setFloatPaginationStrut(lineInfo.floatPaginationStrut() + paginationStrut);
return true;
}
void RenderBlock::layoutLineGridBox()
{
if (style()->lineGrid() == RenderStyle::initialLineGrid()) {
setLineGridBox(0);
return;
}
setLineGridBox(0);
RootInlineBox* lineGridBox = new (renderArena()) RootInlineBox(this);
lineGridBox->setHasTextChildren(); lineGridBox->setConstructed();
GlyphOverflowAndFallbackFontsMap textBoxDataMap;
VerticalPositionCache verticalPositionCache;
lineGridBox->alignBoxesInBlockDirection(logicalHeight(), textBoxDataMap, verticalPositionCache);
setLineGridBox(lineGridBox);
}
}