#include "config.h"
#include "bidi.h"
#include "CharacterNames.h"
#include "Document.h"
#include "Element.h"
#include "FrameView.h"
#include "InlineTextBox.h"
#include "Logging.h"
#include "RenderArena.h"
#include "RenderLayer.h"
#include "RenderListMarker.h"
#include "RenderView.h"
#include "break_lines.h"
#include <wtf/AlwaysInline.h>
#include <wtf/RefCountedLeakCounter.h>
#include <wtf/StdLibExtras.h>
#include <wtf/Vector.h>
using namespace std;
using namespace WTF;
using namespace Unicode;
namespace WebCore {
const unsigned cMaxLineDepth = 200;
class InlineIterator {
public:
InlineIterator()
: block(0)
, obj(0)
, pos(0)
, nextBreakablePosition(-1)
{
}
InlineIterator(RenderBlock* b, RenderObject* o, unsigned p)
: block(b)
, obj(o)
, pos(p)
, nextBreakablePosition(-1)
{
}
void increment(InlineBidiResolver* resolver = 0);
bool atEnd() const;
UChar current() const;
WTF::Unicode::Direction direction() const;
RenderBlock* block;
RenderObject* obj;
unsigned pos;
int nextBreakablePosition;
};
static Vector<InlineIterator>* smidpoints;
static unsigned sNumMidpoints;
static unsigned sCurrMidpoint;
static bool betweenMidpoints;
static bool isLineEmpty = true;
static bool previousLineBrokeCleanly = true;
static int getBorderPaddingMargin(RenderBox* child, bool endOfInline)
{
bool leftSide = (child->style()->direction() == LTR) ? !endOfInline : endOfInline;
if (leftSide)
return child->marginLeft() + child->paddingLeft() + child->borderLeft();
return child->marginRight() + child->paddingRight() + child->borderRight();
}
static int inlineWidth(RenderObject* child, bool start = true, bool end = true)
{
unsigned lineDepth = 1;
int extraWidth = 0;
RenderObject* parent = child->parent();
while (parent->isBox() && parent->isInline() && !parent->isInlineBlockOrInlineTable() && lineDepth++ < cMaxLineDepth) {
if (start && parent->firstChild() == child)
extraWidth += getBorderPaddingMargin(toRenderBox(parent), false);
if (end && parent->lastChild() == child)
extraWidth += getBorderPaddingMargin(toRenderBox(parent), true);
child = parent;
parent = child->parent();
}
return extraWidth;
}
#ifndef NDEBUG
static WTF::RefCountedLeakCounter bidiRunCounter("BidiRun");
static bool inBidiRunDestroy;
#endif
void BidiRun::destroy()
{
#ifndef NDEBUG
inBidiRunDestroy = true;
#endif
RenderArena* renderArena = m_object->renderArena();
delete this;
#ifndef NDEBUG
inBidiRunDestroy = false;
#endif
renderArena->free(*reinterpret_cast<size_t*>(this), this);
}
void* BidiRun::operator new(size_t sz, RenderArena* renderArena) throw()
{
#ifndef NDEBUG
bidiRunCounter.increment();
#endif
return renderArena->allocate(sz);
}
void BidiRun::operator delete(void* ptr, size_t sz)
{
#ifndef NDEBUG
bidiRunCounter.decrement();
#endif
ASSERT(inBidiRunDestroy);
*(size_t*)ptr = sz;
}
inline bool operator==(const InlineIterator& it1, const InlineIterator& it2)
{
return it1.pos == it2.pos && it1.obj == it2.obj;
}
inline bool operator!=(const InlineIterator& it1, const InlineIterator& it2)
{
return it1.pos != it2.pos || it1.obj != it2.obj;
}
static inline RenderObject* bidiNext(RenderBlock* block, RenderObject* current, InlineBidiResolver* resolver = 0, bool skipInlines = true, bool* endOfInlinePtr = 0)
{
RenderObject* next = 0;
bool oldEndOfInline = endOfInlinePtr ? *endOfInlinePtr : false;
bool endOfInline = false;
while (current) {
next = 0;
if (!oldEndOfInline && !current->isFloating() && !current->isReplaced() && !current->isPositioned()) {
next = current->firstChild();
if (next && resolver && next->isRenderInline()) {
EUnicodeBidi ub = next->style()->unicodeBidi();
if (ub != UBNormal) {
TextDirection dir = next->style()->direction();
Direction d = (ub == Embed
? (dir == RTL ? RightToLeftEmbedding : LeftToRightEmbedding)
: (dir == RTL ? RightToLeftOverride : LeftToRightOverride));
resolver->embed(d);
}
}
}
if (!next) {
if (!skipInlines && !oldEndOfInline && current->isRenderInline()) {
next = current;
endOfInline = true;
break;
}
while (current && current != block) {
if (resolver && current->isRenderInline() && current->style()->unicodeBidi() != UBNormal)
resolver->embed(PopDirectionalFormat);
next = current->nextSibling();
if (next) {
if (resolver && next->isRenderInline()) {
EUnicodeBidi ub = next->style()->unicodeBidi();
if (ub != UBNormal) {
TextDirection dir = next->style()->direction();
Direction d = (ub == Embed
? (dir == RTL ? RightToLeftEmbedding: LeftToRightEmbedding)
: (dir == RTL ? RightToLeftOverride : LeftToRightOverride));
resolver->embed(d);
}
}
break;
}
current = current->parent();
if (!skipInlines && current && current != block && current->isRenderInline()) {
next = current;
endOfInline = true;
break;
}
}
}
if (!next)
break;
if (next->isText() || next->isFloating() || next->isReplaced() || next->isPositioned()
|| ((!skipInlines || !next->firstChild()) && next->isRenderInline()))
break;
current = next;
}
if (endOfInlinePtr)
*endOfInlinePtr = endOfInline;
return next;
}
static RenderObject* bidiFirst(RenderBlock* block, InlineBidiResolver* resolver, bool skipInlines = true)
{
if (!block->firstChild())
return 0;
RenderObject* o = block->firstChild();
if (o->isRenderInline()) {
if (resolver) {
EUnicodeBidi ub = o->style()->unicodeBidi();
if (ub != UBNormal) {
TextDirection dir = o->style()->direction();
Direction d = (ub == Embed
? (dir == RTL ? RightToLeftEmbedding : LeftToRightEmbedding)
: (dir == RTL ? RightToLeftOverride : LeftToRightOverride));
resolver->embed(d);
}
}
if (skipInlines && o->firstChild())
o = bidiNext(block, o, resolver, skipInlines);
else {
if (resolver)
resolver->commitExplicitEmbedding();
return o;
}
}
if (o && !o->isText() && !o->isReplaced() && !o->isFloating() && !o->isPositioned())
o = bidiNext(block, o, resolver, skipInlines);
if (resolver)
resolver->commitExplicitEmbedding();
return o;
}
inline void InlineIterator::increment(InlineBidiResolver* resolver)
{
if (!obj)
return;
if (obj->isText()) {
pos++;
if (pos >= toRenderText(obj)->textLength()) {
obj = bidiNext(block, obj, resolver);
pos = 0;
nextBreakablePosition = -1;
}
} else {
obj = bidiNext(block, obj, resolver);
pos = 0;
nextBreakablePosition = -1;
}
}
template<>
inline void InlineBidiResolver::increment()
{
current.increment(this);
}
inline bool InlineIterator::atEnd() const
{
return !obj;
}
inline UChar InlineIterator::current() const
{
if (!obj || !obj->isText())
return 0;
RenderText* text = toRenderText(obj);
if (pos >= text->textLength())
return 0;
return text->characters()[pos];
}
ALWAYS_INLINE Direction InlineIterator::direction() const
{
if (UChar c = current())
return Unicode::direction(c);
if (obj && obj->isListMarker())
return obj->style()->direction() == LTR ? LeftToRight : RightToLeft;
return OtherNeutral;
}
static void chopMidpointsAt(RenderObject* obj, unsigned pos)
{
if (!sNumMidpoints)
return;
InlineIterator* midpoints = smidpoints->data();
for (int i = sNumMidpoints - 1; i >= 0; i--) {
const InlineIterator& point = midpoints[i];
if (point.obj == obj && point.pos == pos) {
sNumMidpoints = i;
break;
}
}
}
static void checkMidpoints(InlineIterator& lBreak)
{
if (lBreak.obj && sNumMidpoints && sNumMidpoints % 2 == 0) {
InlineIterator* midpoints = smidpoints->data();
InlineIterator& endpoint = midpoints[sNumMidpoints-2];
const InlineIterator& startpoint = midpoints[sNumMidpoints-1];
InlineIterator currpoint = endpoint;
while (!currpoint.atEnd() && currpoint != startpoint && currpoint != lBreak)
currpoint.increment();
if (currpoint == lBreak) {
sNumMidpoints--;
if (endpoint.obj->style()->collapseWhiteSpace()) {
if (endpoint.obj->isText()) {
RenderText* textObj = toRenderText(endpoint.obj);
if (endpoint.pos + 1 < textObj->textLength()) {
if (textObj->characters()[endpoint.pos+1] == softHyphen)
return;
} else if (startpoint.obj->isText()) {
RenderText *startText = toRenderText(startpoint.obj);
if (startText->textLength() && startText->characters()[0] == softHyphen)
return;
}
}
endpoint.pos--;
}
}
}
}
static void addMidpoint(const InlineIterator& midpoint)
{
if (smidpoints->size() <= sNumMidpoints)
smidpoints->grow(sNumMidpoints + 10);
InlineIterator* midpoints = smidpoints->data();
midpoints[sNumMidpoints++] = midpoint;
}
static void appendRunsForObject(int start, int end, RenderObject* obj, InlineBidiResolver& resolver)
{
if (start > end || obj->isFloating() ||
(obj->isPositioned() && !obj->hasStaticX() && !obj->hasStaticY() && !obj->container()->isRenderInline()))
return;
bool haveNextMidpoint = (sCurrMidpoint < sNumMidpoints);
InlineIterator nextMidpoint;
if (haveNextMidpoint)
nextMidpoint = smidpoints->at(sCurrMidpoint);
if (betweenMidpoints) {
if (!(haveNextMidpoint && nextMidpoint.obj == obj))
return;
betweenMidpoints = false;
start = nextMidpoint.pos;
sCurrMidpoint++;
if (start < end)
return appendRunsForObject(start, end, obj, resolver);
} else {
if (!haveNextMidpoint || (obj != nextMidpoint.obj)) {
resolver.addRun(new (obj->renderArena()) BidiRun(start, end, obj, resolver.context(), resolver.dir()));
return;
}
if (static_cast<int>(nextMidpoint.pos + 1) <= end) {
betweenMidpoints = true;
sCurrMidpoint++;
if (nextMidpoint.pos != UINT_MAX) { if (static_cast<int>(nextMidpoint.pos + 1) > start)
resolver.addRun(new (obj->renderArena())
BidiRun(start, nextMidpoint.pos + 1, obj, resolver.context(), resolver.dir()));
return appendRunsForObject(nextMidpoint.pos + 1, end, obj, resolver);
}
} else
resolver.addRun(new (obj->renderArena()) BidiRun(start, end, obj, resolver.context(), resolver.dir()));
}
}
template <>
void InlineBidiResolver::appendRun()
{
if (!emptyRun && !eor.atEnd()) {
int start = sor.pos;
RenderObject *obj = sor.obj;
while (obj && obj != eor.obj && obj != endOfLine.obj) {
appendRunsForObject(start, obj->length(), obj, *this);
start = 0;
obj = bidiNext(sor.block, obj);
}
if (obj) {
unsigned pos = obj == eor.obj ? eor.pos : UINT_MAX;
if (obj == endOfLine.obj && endOfLine.pos <= pos) {
reachedEndOfLine = true;
pos = endOfLine.pos;
}
int end = obj->length() ? pos+1 : 0;
appendRunsForObject(start, end, obj, *this);
}
eor.increment();
sor = eor;
}
m_direction = OtherNeutral;
m_status.eor = OtherNeutral;
}
InlineFlowBox* RenderBlock::createLineBoxes(RenderObject* obj)
{
unsigned lineDepth = 1;
InlineFlowBox* childBox = 0;
InlineFlowBox* parentBox = 0;
InlineFlowBox* result = 0;
do {
ASSERT(obj->isRenderInline() || obj == this);
RenderFlow* flow = static_cast<RenderFlow*>(obj);
parentBox = flow->lastLineBox();
bool constructedNewBox = false;
if (!parentBox || parentBox->isConstructed() || parentBox->nextOnLine()) {
InlineBox* newBox = obj->createInlineBox(false, obj == this);
ASSERT(newBox->isInlineFlowBox());
parentBox = static_cast<InlineFlowBox*>(newBox);
parentBox->setFirstLineStyleBit(m_firstLine);
constructedNewBox = true;
}
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;
}
RootInlineBox* RenderBlock::constructLine(unsigned runCount, BidiRun* firstRun, BidiRun* lastRun, bool lastLine, RenderObject* endObject)
{
ASSERT(firstRun);
InlineFlowBox* parentBox = 0;
for (BidiRun* r = firstRun; r; r = r->next()) {
bool isOnlyRun = (runCount == 1);
if (runCount == 2 && !r->m_object->isListMarker())
isOnlyRun = ((style()->direction() == RTL) ? lastRun : firstRun)->m_object->isListMarker();
InlineBox* box = r->m_object->createInlineBox(r->m_object->isPositioned(), false, isOnlyRun);
r->m_box = box;
if (box) {
if (!parentBox || parentBox->object() != r->m_object->parent())
parentBox = createLineBoxes(r->m_object->parent());
parentBox->addToLine(box);
bool visuallyOrdered = r->m_object->style()->visuallyOrdered();
box->setBidiLevel(visuallyOrdered ? 0 : r->level());
if (box->isInlineTextBox()) {
InlineTextBox* text = static_cast<InlineTextBox*>(box);
text->setStart(r->m_start);
text->setLen(r->m_stop - r->m_start);
text->m_dirOverride = r->dirOverride(visuallyOrdered);
}
}
}
ASSERT(lastLineBox() && !lastLineBox()->isConstructed());
lastLineBox()->determineSpacingForFlowBoxes(lastLine, endObject);
lastLineBox()->setConstructed();
return lastRootBox();
}
void RenderBlock::computeHorizontalPositionsForLine(RootInlineBox* lineBox, BidiRun* firstRun, BidiRun* trailingSpaceRun, bool reachedEnd)
{
int availableWidth = lineWidth(height());
int totWidth = lineBox->getFlowSpacingWidth();
bool needsWordSpacing = false;
unsigned numSpaces = 0;
ETextAlign textAlign = style()->textAlign();
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) {
const UChar* characters = rt->characters();
for (int i = r->m_start; i < r->m_stop; i++) {
UChar c = characters[i];
if (c == ' ' || c == '\n' || c == '\t')
numSpaces++;
}
}
if (int length = rt->textLength()) {
if (!r->m_start && needsWordSpacing && isSpaceOrNewline(rt->characters()[r->m_start]))
totWidth += rt->style(m_firstLine)->font().wordSpacing();
needsWordSpacing = !isSpaceOrNewline(rt->characters()[r->m_stop - 1]) && r->m_stop == length;
}
r->m_box->setWidth(rt->width(r->m_start, r->m_stop - r->m_start, totWidth, m_firstLine));
} else if (!r->m_object->isRenderInline()) {
RenderBox* renderBox = toRenderBox(r->m_object);
renderBox->calcWidth();
r->m_box->setWidth(renderBox->width());
totWidth += renderBox->marginLeft() + renderBox->marginRight();
}
totWidth += r->m_box->width();
}
int x = leftOffset(height());
switch(textAlign) {
case LEFT:
case WEBKIT_LEFT:
if (style()->direction() == LTR) {
if (totWidth > availableWidth && trailingSpaceRun)
trailingSpaceRun->m_box->setWidth(max(0, trailingSpaceRun->m_box->width() - totWidth + availableWidth));
} else {
if (trailingSpaceRun)
trailingSpaceRun->m_box->setWidth(0);
else if (totWidth > availableWidth)
x -= (totWidth - availableWidth);
}
break;
case JUSTIFY:
if (numSpaces && !reachedEnd && !lineBox->endsWithBreak()) {
if (trailingSpaceRun) {
totWidth -= trailingSpaceRun->m_box->width();
trailingSpaceRun->m_box->setWidth(0);
}
break;
}
case TAAUTO:
numSpaces = 0;
if (style()->direction() == LTR) {
if (totWidth > availableWidth && trailingSpaceRun)
trailingSpaceRun->m_box->setWidth(max(0, trailingSpaceRun->m_box->width() - totWidth + availableWidth));
break;
}
case RIGHT:
case WEBKIT_RIGHT:
if (style()->direction() == LTR) {
if (trailingSpaceRun) {
totWidth -= trailingSpaceRun->m_box->width();
trailingSpaceRun->m_box->setWidth(0);
}
if (totWidth < availableWidth)
x += availableWidth - totWidth;
} else {
if (totWidth > availableWidth && trailingSpaceRun) {
trailingSpaceRun->m_box->setWidth(max(0, trailingSpaceRun->m_box->width() - totWidth + availableWidth));
totWidth -= trailingSpaceRun->m_box->width();
} else
x += availableWidth - totWidth;
}
break;
case CENTER:
case WEBKIT_CENTER:
int trailingSpaceWidth = 0;
if (trailingSpaceRun) {
totWidth -= trailingSpaceRun->m_box->width();
trailingSpaceWidth = min(trailingSpaceRun->m_box->width(), (availableWidth - totWidth + 1) / 2);
trailingSpaceRun->m_box->setWidth(max(0, trailingSpaceWidth));
}
if (style()->direction() == LTR)
x += max((availableWidth - totWidth) / 2, 0);
else
x += totWidth > availableWidth ? (availableWidth - totWidth) : (availableWidth - totWidth) / 2 - trailingSpaceWidth;
break;
}
if (numSpaces) {
for (BidiRun* r = firstRun; r; r = r->next()) {
if (!r->m_box || r == trailingSpaceRun)
continue;
int spaceAdd = 0;
if (r->m_object->isText()) {
unsigned spaces = 0;
const UChar* characters = toRenderText(r->m_object)->characters();
for (int i = r->m_start; i < r->m_stop; i++) {
UChar c = characters[i];
if (c == ' ' || c == '\n' || c == '\t')
spaces++;
}
ASSERT(spaces <= numSpaces);
if (r->m_object->style()->collapseWhiteSpace()) {
spaceAdd = (availableWidth - totWidth) * spaces / numSpaces;
static_cast<InlineTextBox*>(r->m_box)->setSpaceAdd(spaceAdd);
totWidth += spaceAdd;
}
numSpaces -= spaces;
if (!numSpaces)
break;
}
}
}
int leftPosition = x;
int rightPosition = x;
needsWordSpacing = false;
lineBox->placeBoxesHorizontally(x, leftPosition, rightPosition, needsWordSpacing);
lineBox->setHorizontalOverflowPositions(leftPosition, rightPosition);
}
void RenderBlock::computeVerticalPositionsForLine(RootInlineBox* lineBox, BidiRun* firstRun)
{
setHeight(lineBox->verticallyAlignBoxes(height()));
lineBox->setBlockHeight(height());
int bottomOfLine = lineBox->bottomOverflow();
if (bottomOfLine > height() && bottomOfLine > m_overflowHeight)
m_overflowHeight = bottomOfLine;
for (BidiRun* r = firstRun; r; r = r->next()) {
if (!r->m_box)
continue;
if (r->m_object->isPositioned())
r->m_box->setYPos(height());
r->m_object->position(r->m_box);
}
lineBox->markDirty(false);
}
void RenderBlock::bidiReorderLine(InlineBidiResolver& resolver, const InlineIterator& end)
{
resolver.createBidiRunsForLine(end, style()->visuallyOrdered(), previousLineBrokeCleanly);
}
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;
}
void RenderBlock::layoutInlineChildren(bool relayoutChildren, int& repaintTop, int& repaintBottom)
{
bool useRepaintBounds = false;
invalidateVerticalPosition();
m_overflowHeight = 0;
setHeight(borderTop() + paddingTop());
int toAdd = borderBottom() + paddingBottom() + horizontalScrollbarHeight();
bool fullLayout = !firstLineBox() || !firstChild() || selfNeedsLayout() || relayoutChildren;
if (fullLayout)
deleteLineBoxes();
bool hasTextOverflow = style()->textOverflow() && hasOverflowClip();
if (hasTextOverflow)
deleteEllipsisLineBoxes();
if (firstChild()) {
bool endOfInline = false;
RenderObject* o = bidiFirst(this, 0, false);
Vector<FloatWithRect> floats;
int containerWidth = max(0, containingBlockWidth());
while (o) {
o->invalidateVerticalPosition();
if (o->isReplaced() || o->isFloating() || o->isPositioned()) {
RenderBox* box = toRenderBox(o);
if (relayoutChildren || o->style()->width().isPercent() || o->style()->height().isPercent())
o->setChildNeedsLayout(true, false);
if (relayoutChildren && (o->style()->paddingLeft().isPercent() || o->style()->paddingRight().isPercent()))
o->setPrefWidthsDirty(true, false);
if (o->isPositioned())
o->containingBlock()->insertPositionedObject(box);
else {
if (o->isFloating())
floats.append(FloatWithRect(box));
else if (fullLayout || o->needsLayout()) o->dirtyLineBoxes(fullLayout);
o->layoutIfNeeded();
}
} else if (o->isText() || (o->isRenderInline() && !endOfInline)) {
if (fullLayout || o->selfNeedsLayout())
o->dirtyLineBoxes(fullLayout);
if (o->isRenderInline())
static_cast<RenderFlow*>(o)->calcMargins(containerWidth);
o->setNeedsLayout(false);
}
o = bidiNext(this, o, 0, false, &endOfInline);
}
InlineBidiResolver resolver;
unsigned floatIndex;
RootInlineBox* startLine = determineStartPosition(fullLayout, resolver, floats, floatIndex);
if (fullLayout && !selfNeedsLayout()) {
setNeedsLayout(true, false); RenderView* v = view();
if (v && !v->doingFullRepaint() && m_layer) {
repaintUsingContainer(containerForRepaint(), m_layer->repaintRect());
}
}
FloatingObject* lastFloat = m_floatingObjects ? m_floatingObjects->last() : 0;
if (!smidpoints)
smidpoints = new Vector<InlineIterator>();
sNumMidpoints = 0;
sCurrMidpoint = 0;
InlineIterator cleanLineStart;
BidiStatus cleanLineBidiStatus;
int endLineYPos = 0;
RootInlineBox* endLine = (fullLayout || !startLine) ?
0 : determineEndPosition(startLine, cleanLineStart, cleanLineBidiStatus, endLineYPos);
if (startLine) {
useRepaintBounds = true;
repaintTop = height();
repaintBottom = height();
RenderArena* arena = renderArena();
RootInlineBox* box = startLine;
while (box) {
repaintTop = min(repaintTop, box->topOverflow());
repaintBottom = max(repaintBottom, box->bottomOverflow());
RootInlineBox* next = box->nextRootBox();
box->deleteLine(arena);
box = next;
}
}
InlineIterator end = resolver.position();
if (!fullLayout && lastRootBox() && lastRootBox()->endsWithBreak()) {
if (InlineBox* lastLeafChild = lastRootBox()->lastLeafChild()) {
RenderObject* lastObject = lastLeafChild->object();
if (!lastObject->isBR())
lastObject = lastRootBox()->firstLeafChild()->object();
if (lastObject->isBR()) {
EClear clear = lastObject->style()->clear();
if (clear != CNONE)
newLine(clear);
}
}
}
bool endLineMatched = false;
bool checkForEndLineMatch = endLine;
bool checkForFloatsFromLastLine = false;
int lastHeight = height();
while (!end.atEnd()) {
if (checkForEndLineMatch && (endLineMatched = matchedEndLine(resolver, cleanLineStart, cleanLineBidiStatus, endLine, endLineYPos, repaintBottom, repaintTop)))
break;
betweenMidpoints = false;
isLineEmpty = true;
EClear clear = CNONE;
end = findNextLineBreak(resolver, &clear);
if (resolver.position().atEnd()) {
resolver.deleteRuns();
checkForFloatsFromLastLine = true;
break;
}
ASSERT(end != resolver.position());
if (!isLineEmpty) {
bidiReorderLine(resolver, end);
ASSERT(resolver.position() == end);
BidiRun* trailingSpaceRun = 0;
if (!previousLineBrokeCleanly && resolver.runCount() && resolver.logicallyLastRun()->m_object->style()->breakOnlyAfterWhiteSpace()
&& resolver.logicallyLastRun()->m_object->style()->autoWrap()) {
trailingSpaceRun = resolver.logicallyLastRun();
RenderObject* lastObject = trailingSpaceRun->m_object;
if (lastObject->isText()) {
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())
trailingSpaceRun = 0;
else {
TextDirection direction = style()->direction();
bool shouldReorder = trailingSpaceRun != (direction == LTR ? resolver.lastRun() : resolver.firstRun());
if (firstSpace != trailingSpaceRun->start()) {
ETextAlign textAlign = style()->textAlign();
bool shouldSeparateSpaces = textAlign != LEFT && textAlign != WEBKIT_LEFT && textAlign != TAAUTO || trailingSpaceRun->m_level % 2 || direction == RTL || shouldReorder;
if (shouldSeparateSpaces) {
BidiContext* baseContext = resolver.context();
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)
resolver.addRun(newTrailingRun);
else
resolver.prependRun(newTrailingRun);
trailingSpaceRun = newTrailingRun;
shouldReorder = false;
}
}
if (shouldReorder) {
if (direction == LTR) {
resolver.moveRunToEnd(trailingSpaceRun);
trailingSpaceRun->m_level = 0;
} else {
resolver.moveRunToBeginning(trailingSpaceRun);
trailingSpaceRun->m_level = 1;
}
}
}
} else
trailingSpaceRun = 0;
}
RootInlineBox* lineBox = 0;
if (resolver.runCount()) {
lineBox = constructLine(resolver.runCount(), resolver.firstRun(), resolver.lastRun(), !end.obj, end.obj && !end.pos ? end.obj : 0);
if (lineBox) {
lineBox->setEndsWithBreak(previousLineBrokeCleanly);
computeHorizontalPositionsForLine(lineBox, resolver.firstRun(), trailingSpaceRun, end.atEnd());
computeVerticalPositionsForLine(lineBox, resolver.firstRun());
#if ENABLE(SVG)
lineBox->computePerCharacterLayoutInformation();
#endif
#if PLATFORM(MAC)
if (style()->highlight() != nullAtom)
lineBox->addHighlightOverflow();
#endif
}
}
resolver.deleteRuns();
if (lineBox) {
lineBox->setLineBreakInfo(end.obj, end.pos, resolver.status());
if (useRepaintBounds) {
repaintTop = min(repaintTop, lineBox->topOverflow());
repaintBottom = max(repaintBottom, lineBox->bottomOverflow());
}
}
m_firstLine = false;
newLine(clear);
}
if (m_floatingObjects && lastRootBox()) {
if (lastFloat) {
for (FloatingObject* f = m_floatingObjects->last(); f != lastFloat; f = m_floatingObjects->prev()) {
}
m_floatingObjects->next();
} else
m_floatingObjects->first();
for (FloatingObject* f = m_floatingObjects->current(); f; f = m_floatingObjects->next()) {
if (f->m_bottom > lastHeight)
lastRootBox()->floats().append(f->m_renderer);
ASSERT(f->m_renderer == floats[floatIndex].object);
if (floats[floatIndex].rect != IntRect(f->m_left, f->m_top, f->m_width, f->m_bottom - f->m_top))
checkForEndLineMatch = false;
floatIndex++;
}
lastFloat = m_floatingObjects->last();
}
lastHeight = height();
sNumMidpoints = 0;
sCurrMidpoint = 0;
resolver.setPosition(end);
}
if (endLine) {
if (endLineMatched) {
int delta = height() - endLineYPos;
for (RootInlineBox* line = endLine; line; line = line->nextRootBox()) {
line->attachLine();
if (delta) {
repaintTop = min(repaintTop, line->topOverflow() + min(delta, 0));
repaintBottom = max(repaintBottom, line->bottomOverflow() + max(delta, 0));
line->adjustPosition(0, delta);
}
if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
Vector<RenderBox*>::iterator end = cleanLineFloats->end();
for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
int floatTop = (*f)->y() - (*f)->marginTop();
insertFloatingObject(*f);
setHeight(floatTop + delta);
positionNewFloats();
}
}
}
setHeight(lastRootBox()->blockHeight());
} else {
InlineRunBox* line = endLine;
RenderArena* arena = renderArena();
while (line) {
repaintTop = min(repaintTop, line->topOverflow());
repaintBottom = max(repaintBottom, line->bottomOverflow());
InlineRunBox* next = line->nextLineBox();
line->deleteLine(arena);
line = next;
}
}
}
if (m_floatingObjects && (checkForFloatsFromLastLine || positionNewFloats()) && lastRootBox()) {
if (lastFloat) {
for (FloatingObject* f = m_floatingObjects->last(); f != lastFloat; f = m_floatingObjects->prev()) {
}
m_floatingObjects->next();
} else
m_floatingObjects->first();
for (FloatingObject* f = m_floatingObjects->current(); f; f = m_floatingObjects->next()) {
if (f->m_bottom > lastHeight)
lastRootBox()->floats().append(f->m_renderer);
}
lastFloat = m_floatingObjects->last();
}
}
sNumMidpoints = 0;
sCurrMidpoint = 0;
setHeight(height() + toAdd);
m_overflowHeight = max(height(), m_overflowHeight);
checkLinesForOverflow();
if (!firstLineBox() && hasLineIfEmpty())
setHeight(height() + lineHeight(true, true));
if (hasTextOverflow)
checkLinesForTextOverflow();
}
RootInlineBox* RenderBlock::determineStartPosition(bool& fullLayout, InlineBidiResolver& resolver, Vector<FloatWithRect>& floats, unsigned& numCleanFloats)
{
RootInlineBox* curr = 0;
RootInlineBox* last = 0;
bool dirtiedByFloat = false;
if (!fullLayout) {
size_t floatIndex = 0;
for (curr = firstRootBox(); curr && !curr->isDirty(); curr = curr->nextRootBox()) {
if (Vector<RenderBox*>* cleanLineFloats = curr->floatsPtr()) {
Vector<RenderBox*>::iterator end = cleanLineFloats->end();
for (Vector<RenderBox*>::iterator o = cleanLineFloats->begin(); o != end; ++o) {
RenderBox* f = *o;
IntSize newSize(f->width() + f->marginLeft() +f->marginRight(), f->height() + f->marginTop() + f->marginBottom());
ASSERT(floatIndex < floats.size());
if (floats[floatIndex].object != f) {
fullLayout = true;
break;
}
if (floats[floatIndex].rect.size() != newSize) {
int floatTop = floats[floatIndex].rect.y();
curr->markDirty();
markLinesDirtyInVerticalRange(curr->blockHeight(), floatTop + max(floats[floatIndex].rect.height(), newSize.height()));
floats[floatIndex].rect.setSize(newSize);
dirtiedByFloat = true;
}
floatIndex++;
}
}
if (dirtiedByFloat || fullLayout)
break;
}
if (!curr && floatIndex < floats.size())
fullLayout = true;
}
if (fullLayout) {
if (firstRootBox()) {
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();
}
numCleanFloats = 0;
if (!floats.isEmpty()) {
int savedHeight = height();
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) {
insertFloatingObject(*f);
setHeight((*f)->y() - (*f)->marginTop());
positionNewFloats();
ASSERT(floats[numCleanFloats].object == *f);
numCleanFloats++;
}
}
line = line->nextRootBox();
}
setHeight(savedHeight);
}
m_firstLine = !last;
previousLineBrokeCleanly = !last || last->endsWithBreak();
RenderObject* startObj;
int pos = 0;
if (last) {
setHeight(last->blockHeight());
startObj = last->lineBreakObj();
pos = last->lineBreakPos();
resolver.setStatus(last->lineBreakBidiStatus());
} else {
bool ltr = style()->direction() == LTR
#if ENABLE(SVG)
|| (style()->unicodeBidi() == UBNormal && isSVGText())
#endif
;
BidiContext* context = new BidiContext(ltr ? 0 : 1, ltr ? LeftToRight : RightToLeft, style()->unicodeBidi() == Override);
resolver.setLastStrongDir(context->dir());
resolver.setLastDir(context->dir());
resolver.setEorDir(context->dir());
resolver.setContext(context);
startObj = bidiFirst(this, &resolver);
}
resolver.setPosition(InlineIterator(this, startObj, pos));
return curr;
}
RootInlineBox* RenderBlock::determineEndPosition(RootInlineBox* startLine, InlineIterator& cleanLineStart, BidiStatus& cleanLineBidiStatus, int& yPos)
{
RootInlineBox* last = 0;
if (!startLine)
last = 0;
else {
for (RootInlineBox* curr = startLine->nextRootBox(); curr; curr = curr->nextRootBox()) {
if (curr->isDirty())
last = 0;
else if (!last)
last = curr;
}
}
if (!last)
return 0;
RootInlineBox* prev = last->prevRootBox();
cleanLineStart = InlineIterator(this, prev->lineBreakObj(), prev->lineBreakPos());
cleanLineBidiStatus = prev->lineBreakBidiStatus();
yPos = prev->blockHeight();
for (RootInlineBox* line = last; line; line = line->nextRootBox())
line->extractLine();
return last;
}
bool RenderBlock::matchedEndLine(const InlineBidiResolver& resolver, const InlineIterator& endLineStart, const BidiStatus& endLineStatus, RootInlineBox*& endLine, int& endYPos, int& repaintBottom, int& repaintTop)
{
if (resolver.position() == endLineStart) {
if (resolver.status() != endLineStatus)
return false;
int delta = height() - endYPos;
if (!delta || !m_floatingObjects)
return true;
int top = min(height(), endYPos);
RootInlineBox* lastLine = endLine;
while (RootInlineBox* nextLine = lastLine->nextRootBox())
lastLine = nextLine;
int bottom = lastLine->blockHeight() + abs(delta);
for (FloatingObject* f = m_floatingObjects->first(); f; f = m_floatingObjects->next()) {
if (f->m_bottom >= top && f->m_bottom < bottom)
return false;
}
return true;
}
static int numLines = 8; RootInlineBox* line = endLine;
for (int i = 0; i < numLines && line; i++, line = line->nextRootBox()) {
if (line->lineBreakObj() == resolver.position().obj && line->lineBreakPos() == resolver.position().pos) {
if (line->lineBreakBidiStatus() != resolver.status())
return false; RootInlineBox* result = line->nextRootBox();
if (result)
endYPos = line->blockHeight();
int delta = height() - endYPos;
if (delta && m_floatingObjects) {
int top = min(height(), endYPos);
RootInlineBox* lastLine = endLine;
while (RootInlineBox* nextLine = lastLine->nextRootBox())
lastLine = nextLine;
int bottom = lastLine->blockHeight() + abs(delta);
for (FloatingObject* f = m_floatingObjects->first(); f; f = m_floatingObjects->next()) {
if (f->m_bottom >= top && f->m_bottom < bottom)
return false;
}
}
RootInlineBox* boxToDelete = endLine;
RenderArena* arena = renderArena();
while (boxToDelete && boxToDelete != result) {
repaintTop = min(repaintTop, boxToDelete->topOverflow());
repaintBottom = max(repaintBottom, boxToDelete->bottomOverflow());
RootInlineBox* next = boxToDelete->nextRootBox();
boxToDelete->deleteLine(arena);
boxToDelete = next;
}
endLine = result;
return result;
}
}
return false;
}
static inline bool skipNonBreakingSpace(const InlineIterator& it)
{
if (it.obj->style()->nbspMode() != SPACE || it.current() != noBreakSpace)
return false;
if (isLineEmpty && previousLineBrokeCleanly)
return false;
return true;
}
static inline bool shouldCollapseWhiteSpace(const RenderStyle* style)
{
return style->collapseWhiteSpace() || (style->whiteSpace() == PRE_WRAP && (!isLineEmpty || !previousLineBrokeCleanly));
}
static inline bool shouldPreserveNewline(RenderObject* object)
{
#if ENABLE(SVG)
if (object->isSVGText())
return false;
#endif
return object->style()->preserveNewline();
}
static bool inlineFlowRequiresLineBox(RenderBox* flow)
{
return flow->isRenderInline() && !flow->firstChild() && flow->hasHorizontalBordersPaddingOrMargin();
}
static inline bool requiresLineBox(const InlineIterator& it)
{
if (it.obj->isFloatingOrPositioned())
return false;
if (it.obj->isRenderInline() && !inlineFlowRequiresLineBox(toRenderBox(it.obj)))
return false;
if (!shouldCollapseWhiteSpace(it.obj->style()) || it.obj->isBR())
return true;
UChar current = it.current();
return current != ' ' && current != '\t' && current != softHyphen && (current != '\n' || shouldPreserveNewline(it.obj)) && !skipNonBreakingSpace(it);
}
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::skipTrailingWhitespace(InlineIterator& iterator)
{
while (!iterator.atEnd() && !requiresLineBox(iterator)) {
RenderObject* object = iterator.obj;
if (object->isFloating()) {
insertFloatingObject(toRenderBox(object));
} else if (object->isPositioned()) {
RenderObject* c = object->container();
if (c->isRenderInline()) {
c->setStaticX(style()->direction() == LTR ? leftOffset(height()) : rightOffset(height()));
c->setStaticY(height());
}
if (object->hasStaticX()) {
if (object->style()->isOriginalDisplayInlineType())
object->setStaticX(style()->direction() == LTR ? leftOffset(height()) : width() - rightOffset(height()));
else
object->setStaticX(style()->direction() == LTR ? borderLeft() + paddingLeft() : borderRight() + paddingRight());
}
if (object->hasStaticY())
object->setStaticY(height());
}
iterator.increment();
}
}
int RenderBlock::skipLeadingWhitespace(InlineBidiResolver& resolver)
{
int availableWidth = lineWidth(height());
while (!resolver.position().atEnd() && !requiresLineBox(resolver.position())) {
RenderObject* object = resolver.position().obj;
if (object->isFloating()) {
insertFloatingObject(toRenderBox(object));
positionNewFloats();
availableWidth = lineWidth(height());
} else if (object->isPositioned()) {
RenderObject* c = object->container();
if (c->isRenderInline()) {
c->setStaticX(style()->direction() == LTR ? leftOffset(height()) : rightOffset(height()));
c->setStaticY(height());
}
if (object->hasStaticX()) {
if (object->style()->isOriginalDisplayInlineType())
object->setStaticX(style()->direction() == LTR ? leftOffset(height()) : width() - rightOffset(height()));
else
object->setStaticX(style()->direction() == LTR ? borderLeft() + paddingLeft() : borderRight() + paddingRight());
}
if (object->hasStaticY())
object->setStaticY(height());
}
resolver.increment();
}
resolver.commitExplicitEmbedding();
return availableWidth;
}
static bool shouldSkipWhitespaceAfterStartObject(RenderBlock* block, RenderObject* o)
{
RenderObject* next = bidiNext(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(InlineIterator(0, o, 0));
return true;
}
}
return false;
}
void RenderBlock::fitBelowFloats(int widthToFit, int& availableWidth)
{
ASSERT(widthToFit > availableWidth);
int floatBottom;
int lastFloatBottom = height();
int newLineWidth = availableWidth;
while (true) {
floatBottom = nextFloatBottomBelow(lastFloatBottom);
if (!floatBottom)
break;
newLineWidth = lineWidth(floatBottom);
lastFloatBottom = floatBottom;
if (newLineWidth >= widthToFit)
break;
}
if (newLineWidth > availableWidth) {
setHeight(lastFloatBottom);
availableWidth = newLineWidth;
}
}
InlineIterator RenderBlock::findNextLineBreak(InlineBidiResolver& resolver, EClear* clear)
{
ASSERT(resolver.position().block == this);
bool appliedStartWidth = resolver.position().pos > 0;
int width = skipLeadingWhitespace(resolver);
int w = 0;
int tmpW = 0;
if (resolver.position().atEnd())
return resolver.position();
bool ignoringSpaces = false;
InlineIterator ignoreStart;
bool currentCharacterIsSpace = false;
bool currentCharacterIsWS = false;
RenderObject* trailingSpaceObject = 0;
InlineIterator lBreak = resolver.position();
RenderObject *o = resolver.position().obj;
RenderObject *last = o;
unsigned pos = resolver.position().pos;
int nextBreakable = resolver.position().nextBreakablePosition;
bool atStart = true;
bool prevLineBrokeCleanly = previousLineBrokeCleanly;
previousLineBrokeCleanly = false;
bool autoWrapWasEverTrueOnLine = false;
bool floatsFitOnLine = true;
bool allowImagesToBreak = !style()->htmlHacks() || !isTableCell() || !style()->width().isIntrinsicOrAuto();
EWhiteSpace currWS = style()->whiteSpace();
EWhiteSpace lastWS = currWS;
while (o) {
currWS = o->isReplaced() ? o->parent()->style()->whiteSpace() : o->style()->whiteSpace();
lastWS = last->isReplaced() ? last->parent()->style()->whiteSpace() : last->style()->whiteSpace();
bool autoWrap = RenderStyle::autoWrap(currWS);
autoWrapWasEverTrueOnLine = autoWrapWasEverTrueOnLine || autoWrap;
#if ENABLE(SVG)
bool preserveNewline = o->isSVGText() ? false : RenderStyle::preserveNewline(currWS);
#else
bool preserveNewline = RenderStyle::preserveNewline(currWS);
#endif
bool collapseWhiteSpace = RenderStyle::collapseWhiteSpace(currWS);
if (o->isBR()) {
if (w + tmpW <= width) {
lBreak.obj = o;
lBreak.pos = 0;
lBreak.nextBreakablePosition = -1;
lBreak.increment();
if (prevLineBrokeCleanly)
isLineEmpty = false;
trailingSpaceObject = 0;
previousLineBrokeCleanly = true;
if (!isLineEmpty && clear)
*clear = o->style()->clear();
}
goto end;
}
if (o->isFloatingOrPositioned()) {
if (o->isFloating()) {
RenderBox* floatBox = toRenderBox(o);
insertFloatingObject(floatBox);
if (floatsFitOnLine && floatBox->width() + floatBox->marginLeft() + floatBox->marginRight() + w + tmpW <= width) {
positionNewFloats();
width = lineWidth(height());
} else
floatsFitOnLine = false;
} else if (o->isPositioned()) {
bool isInlineType = o->style()->isOriginalDisplayInlineType();
bool needToSetStaticX = o->hasStaticX();
if (o->hasStaticX() && !isInlineType) {
o->setStaticX(o->parent()->style()->direction() == LTR ?
borderLeft() + paddingLeft() :
borderRight() + paddingRight());
needToSetStaticX = false;
}
bool needToSetStaticY = o->hasStaticY();
if (o->hasStaticY() && isInlineType) {
o->setStaticY(height());
needToSetStaticY = false;
}
bool needToCreateLineBox = needToSetStaticX || needToSetStaticY;
RenderObject* c = o->container();
if (c->isRenderInline() && (!needToSetStaticX || !needToSetStaticY))
needToCreateLineBox = true;
if (needToCreateLineBox) {
trailingSpaceObject = 0;
ignoreStart.obj = o;
ignoreStart.pos = 0;
if (ignoringSpaces) {
addMidpoint(ignoreStart); addMidpoint(ignoreStart); }
}
}
} else if (o->isRenderInline()) {
ASSERT(!o->firstChild());
RenderBox* flowBox = toRenderBox(o);
if (inlineFlowRequiresLineBox(flowBox)) {
isLineEmpty = false;
if (ignoringSpaces) {
trailingSpaceObject = 0;
addMidpoint(InlineIterator(0, o, 0)); addMidpoint(InlineIterator(0, o, 0)); } else if (style()->collapseWhiteSpace() && resolver.position().obj == o
&& shouldSkipWhitespaceAfterStartObject(this, o)) {
currentCharacterIsSpace = true;
currentCharacterIsWS = true;
ignoringSpaces = true;
}
}
tmpW += flowBox->marginLeft() + flowBox->borderLeft() + flowBox->paddingLeft() +
flowBox->marginRight() + flowBox->borderRight() + flowBox->paddingRight();
} else if (o->isReplaced()) {
RenderBox* replacedBox = toRenderBox(o);
if ((autoWrap || RenderStyle::autoWrap(lastWS)) && (!o->isImage() || allowImagesToBreak)) {
w += tmpW;
tmpW = 0;
lBreak.obj = o;
lBreak.pos = 0;
lBreak.nextBreakablePosition = -1;
}
if (ignoringSpaces)
addMidpoint(InlineIterator(0, o, 0));
isLineEmpty = false;
ignoringSpaces = false;
currentCharacterIsSpace = false;
currentCharacterIsWS = false;
trailingSpaceObject = 0;
if (o->isListMarker() && !static_cast<RenderListMarker*>(o)->isInside()) {
if (style()->collapseWhiteSpace() && shouldSkipWhitespaceAfterStartObject(this, o)) {
currentCharacterIsSpace = true;
currentCharacterIsWS = true;
ignoringSpaces = true;
}
} else
tmpW += replacedBox->width() + replacedBox->marginLeft() + replacedBox->marginRight() + inlineWidth(o);
} else if (o->isText()) {
if (!pos)
appliedStartWidth = false;
RenderText* t = toRenderText(o);
int strlen = t->textLength();
int len = strlen - pos;
const UChar* str = t->characters();
const Font& f = t->style(m_firstLine)->font();
int lastSpace = pos;
int wordSpacing = o->style()->wordSpacing();
int lastSpaceWordSpacing = 0;
int wrapW = tmpW + inlineWidth(o, !appliedStartWidth, true);
int charWidth = 0;
bool breakNBSP = autoWrap && o->style()->nbspMode() == SPACE;
bool breakWords = o->style()->breakWords() && ((autoWrap && !w) || currWS == PRE);
bool midWordBreak = false;
bool breakAll = o->style()->wordBreak() == BreakAllWordBreak && autoWrap;
if (t->isWordBreak()) {
w += tmpW;
tmpW = 0;
lBreak.obj = o;
lBreak.pos = 0;
lBreak.nextBreakablePosition = -1;
ASSERT(!len);
}
while (len) {
bool previousCharacterIsSpace = currentCharacterIsSpace;
bool previousCharacterIsWS = currentCharacterIsWS;
UChar c = str[pos];
currentCharacterIsSpace = c == ' ' || c == '\t' || (!preserveNewline && (c == '\n'));
if (!collapseWhiteSpace || !currentCharacterIsSpace)
isLineEmpty = false;
if (c == softHyphen) {
if (!ignoringSpaces) {
InlineIterator beforeSoftHyphen;
if (pos)
beforeSoftHyphen = InlineIterator(0, o, pos - 1);
else
beforeSoftHyphen = InlineIterator(0, last, last->isText() ? toRenderText(last)->textLength() - 1 : 0);
if (sNumMidpoints && smidpoints->at(sNumMidpoints - 1).obj == o && smidpoints->at(sNumMidpoints - 1).pos == pos)
sNumMidpoints--;
else
addMidpoint(beforeSoftHyphen);
tmpW += t->width(lastSpace, pos - lastSpace, f, w + tmpW) + lastSpaceWordSpacing;
if (autoWrap)
tmpW += t->width(pos, 1, f, w + tmpW);
InlineIterator afterSoftHyphen(0, o, pos);
afterSoftHyphen.increment();
addMidpoint(afterSoftHyphen);
}
pos++;
len--;
lastSpaceWordSpacing = 0;
lastSpace = pos; continue;
}
bool applyWordSpacing = false;
currentCharacterIsWS = currentCharacterIsSpace || (breakNBSP && c == noBreakSpace);
if ((breakAll || breakWords) && !midWordBreak) {
wrapW += charWidth;
charWidth = t->width(pos, 1, f, w + wrapW);
midWordBreak = w + wrapW + charWidth > width;
}
bool betweenWords = c == '\n' || (currWS != PRE && !atStart && isBreakable(str, pos, strlen, nextBreakable, breakNBSP));
if (betweenWords || midWordBreak) {
if (strlen > 0 && pos > 0 && (str[pos - 1] == '-' || str[pos - 1] == '?'))
midWordBreak = true;
bool stoppedIgnoringSpaces = false;
if (ignoringSpaces) {
if (!currentCharacterIsSpace) {
ignoringSpaces = false;
lastSpaceWordSpacing = 0;
lastSpace = pos; addMidpoint(InlineIterator(0, o, pos));
stoppedIgnoringSpaces = true;
} else {
pos++;
len--;
continue;
}
}
int additionalTmpW = t->width(lastSpace, pos - lastSpace, f, w+tmpW) + lastSpaceWordSpacing;
tmpW += additionalTmpW;
if (!appliedStartWidth) {
tmpW += inlineWidth(o, true, false);
appliedStartWidth = true;
}
applyWordSpacing = wordSpacing && currentCharacterIsSpace && !previousCharacterIsSpace;
if (!w && autoWrap && tmpW > width)
fitBelowFloats(tmpW, width);
if (autoWrap || breakWords) {
bool lineWasTooWide = false;
if (w + tmpW <= width && currentCharacterIsWS && o->style()->breakOnlyAfterWhiteSpace() && !midWordBreak) {
int charWidth = t->width(pos, 1, f, w + tmpW) + (applyWordSpacing ? wordSpacing : 0);
if (w + tmpW + charWidth > width) {
lineWasTooWide = true;
lBreak.obj = o;
lBreak.pos = pos;
lBreak.nextBreakablePosition = nextBreakable;
skipTrailingWhitespace(lBreak);
}
}
if (lineWasTooWide || w + tmpW > width) {
if (lBreak.obj && shouldPreserveNewline(lBreak.obj) && lBreak.obj->isText() && !toRenderText(lBreak.obj)->isWordBreak() && toRenderText(lBreak.obj)->characters()[lBreak.pos] == '\n') {
if (!stoppedIgnoringSpaces && pos > 0) {
addMidpoint(InlineIterator(0, o, pos - 1)); addMidpoint(InlineIterator(0, o, pos)); }
lBreak.increment();
previousLineBrokeCleanly = true;
}
goto end; } else {
if (!betweenWords || (midWordBreak && !autoWrap))
tmpW -= additionalTmpW;
if (pos > 0 && str[pos-1] == softHyphen)
tmpW -= t->width(pos-1, 1, f, w+tmpW);
}
}
if (c == '\n' && preserveNewline) {
if (!stoppedIgnoringSpaces && pos > 0) {
addMidpoint(InlineIterator(0, o, pos - 1)); addMidpoint(InlineIterator(0, o, pos)); }
lBreak.obj = o;
lBreak.pos = pos;
lBreak.nextBreakablePosition = nextBreakable;
lBreak.increment();
previousLineBrokeCleanly = true;
return lBreak;
}
if (autoWrap && betweenWords) {
w += tmpW;
wrapW = 0;
tmpW = 0;
lBreak.obj = o;
lBreak.pos = pos;
lBreak.nextBreakablePosition = nextBreakable;
breakWords = false;
}
if (midWordBreak) {
lBreak.obj = o;
lBreak.pos = pos;
lBreak.nextBreakablePosition = nextBreakable;
midWordBreak &= (breakWords || breakAll);
}
if (betweenWords) {
lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
lastSpace = pos;
}
if (!ignoringSpaces && o->style()->collapseWhiteSpace()) {
if (currentCharacterIsSpace && previousCharacterIsSpace) {
ignoringSpaces = true;
addMidpoint(ignoreStart);
}
}
} else if (ignoringSpaces) {
ignoringSpaces = false;
lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
lastSpace = pos; addMidpoint(InlineIterator(0, o, pos));
}
if (currentCharacterIsSpace && !previousCharacterIsSpace) {
ignoreStart.obj = o;
ignoreStart.pos = pos;
}
if (!currentCharacterIsWS && previousCharacterIsWS) {
if (autoWrap && o->style()->breakOnlyAfterWhiteSpace()) {
lBreak.obj = o;
lBreak.pos = pos;
lBreak.nextBreakablePosition = nextBreakable;
}
}
if (collapseWhiteSpace && currentCharacterIsSpace && !ignoringSpaces)
trailingSpaceObject = o;
else if (!o->style()->collapseWhiteSpace() || !currentCharacterIsSpace)
trailingSpaceObject = 0;
pos++;
len--;
atStart = false;
}
if (!ignoringSpaces)
tmpW += t->width(lastSpace, pos - lastSpace, f, w+tmpW) + lastSpaceWordSpacing;
tmpW += inlineWidth(o, !appliedStartWidth, true);
} else
ASSERT_NOT_REACHED();
RenderObject* next = bidiNext(this, o);
bool checkForBreak = autoWrap;
if (w && w + tmpW > width && lBreak.obj && currWS == NOWRAP)
checkForBreak = true;
else if (next && o->isText() && next->isText() && !next->isBR()) {
if (autoWrap || (next->style()->autoWrap())) {
if (currentCharacterIsSpace)
checkForBreak = true;
else {
checkForBreak = false;
RenderText* nextText = toRenderText(next);
if (nextText->textLength()) {
UChar c = nextText->characters()[0];
if (c == ' ' || c == '\t' || (c == '\n' && !shouldPreserveNewline(next)))
checkForBreak = true;
} else if (nextText->isWordBreak())
checkForBreak = true;
bool willFitOnLine = w + tmpW <= width;
if (!willFitOnLine && !w) {
fitBelowFloats(tmpW, width);
willFitOnLine = tmpW <= width;
}
bool canPlaceOnLine = willFitOnLine || !autoWrapWasEverTrueOnLine;
if (canPlaceOnLine && checkForBreak) {
w += tmpW;
tmpW = 0;
lBreak.obj = next;
lBreak.pos = 0;
lBreak.nextBreakablePosition = -1;
}
}
}
}
if (checkForBreak && (w + tmpW > width)) {
if (currentCharacterIsSpace && !ignoringSpaces && o->style()->collapseWhiteSpace())
trailingSpaceObject = 0;
if (w)
goto end;
fitBelowFloats(tmpW, width);
if (w + tmpW > width)
goto end;
}
if (!o->isFloatingOrPositioned()) {
last = o;
if (last->isReplaced() && autoWrap && (!last->isImage() || allowImagesToBreak) && (!last->isListMarker() || static_cast<RenderListMarker*>(last)->isInside())) {
w += tmpW;
tmpW = 0;
lBreak.obj = next;
lBreak.pos = 0;
lBreak.nextBreakablePosition = -1;
}
}
o = next;
nextBreakable = -1;
if (!collapseWhiteSpace)
currentCharacterIsSpace = false;
pos = 0;
atStart = false;
}
if (w + tmpW <= width || lastWS == NOWRAP) {
lBreak.obj = 0;
lBreak.pos = 0;
lBreak.nextBreakablePosition = -1;
}
end:
if (lBreak == resolver.position() && !lBreak.obj->isBR()) {
if (style()->whiteSpace() == PRE) {
if (pos != 0) {
lBreak.obj = o;
lBreak.pos = pos - 1;
} else {
lBreak.obj = last;
lBreak.pos = last->isText() ? last->length() : 0;
lBreak.nextBreakablePosition = -1;
}
} else if (lBreak.obj) {
if (last != o && !last->isListMarker()) {
lBreak.obj = o;
lBreak.pos = 0;
lBreak.nextBreakablePosition = -1;
} else {
lBreak.obj = o;
lBreak.pos = pos;
lBreak.nextBreakablePosition = -1;
}
}
}
if (lBreak == resolver.position())
lBreak.increment();
checkMidpoints(lBreak);
if (trailingSpaceObject) {
if (sNumMidpoints%2==1) {
InlineIterator* midpoints = smidpoints->data();
midpoints[sNumMidpoints-1].pos--;
}
else if (lBreak.obj == 0 && trailingSpaceObject->isText()) {
RenderText* text = toRenderText(trailingSpaceObject);
unsigned length = text->textLength();
unsigned pos = length >= 2 ? length - 2 : UINT_MAX;
InlineIterator endMid(0, trailingSpaceObject, pos);
addMidpoint(endMid);
}
}
if (lBreak.pos > 0) {
lBreak.pos--;
lBreak.increment();
}
if (lBreak.obj && lBreak.pos >= 2 && lBreak.obj->isText()) {
UChar c = toRenderText(lBreak.obj)->characters()[lBreak.pos-1];
if (c == softHyphen)
chopMidpointsAt(lBreak.obj, lBreak.pos-2);
}
return lBreak;
}
void RenderBlock::checkLinesForOverflow()
{
m_overflowWidth = width();
for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
m_overflowLeft = min(curr->leftOverflow(), m_overflowLeft);
m_overflowTop = min(curr->topOverflow(), m_overflowTop);
m_overflowWidth = max(curr->rightOverflow(), m_overflowWidth);
m_overflowHeight = max(curr->bottomOverflow(), m_overflowHeight);
}
}
void RenderBlock::deleteEllipsisLineBoxes()
{
for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox())
curr->clearTruncation();
}
void RenderBlock::checkLinesForTextOverflow()
{
TextRun ellipsisRun(&horizontalEllipsis, 1);
DEFINE_STATIC_LOCAL(AtomicString, ellipsisStr, (&horizontalEllipsis, 1));
const Font& firstLineFont = firstLineStyle()->font();
const Font& font = style()->font();
int firstLineEllipsisWidth = firstLineFont.width(ellipsisRun);
int ellipsisWidth = (font == firstLineFont) ? firstLineEllipsisWidth : font.width(ellipsisRun);
bool ltr = style()->direction() == LTR;
for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
int blockEdge = ltr ? rightOffset(curr->yPos()) : leftOffset(curr->yPos());
int lineBoxEdge = ltr ? curr->xPos() + curr->width() : curr->xPos();
if ((ltr && lineBoxEdge > blockEdge) || (!ltr && lineBoxEdge < blockEdge)) {
int width = curr == firstRootBox() ? firstLineEllipsisWidth : ellipsisWidth;
if (curr->canAccommodateEllipsis(ltr, blockEdge, lineBoxEdge, width))
curr->placeEllipsis(ellipsisStr, ltr, blockEdge, width);
}
}
}
}