: m_lineBreaker(lineBreaker)
, m_resolver(resolver)
, m_current(resolver.position())
- , m_lineBreak(resolver.position())
+#if ENABLE(CSS_TRAILING_WORD)
+ , m_lineBreakHistory(InlineIterator(resolver.position()), block.style().trailingWord() == TrailingWord::PartiallyBalanced ? 5 : 1)
+#else
+ , m_lineBreakHistory(InlineIterator(resolver.position()), 1)
+#endif
, m_block(block)
, m_lastObject(m_current.renderer())
, m_nextObject(nullptr)
}
RenderObject* currentObject() { return m_current.renderer(); }
- InlineIterator lineBreak() { return m_lineBreak; }
- InlineIterator& lineBreakRef() {return m_lineBreak; }
+ InlineIterator lineBreak() { return m_lineBreakHistory.current(); }
LineWidth& lineWidth() { return m_width; }
bool atEnd() { return m_atEnd; }
bool canBreakAtThisPosition();
void commitAndUpdateLineBreakIfNeeded();
InlineIterator handleEndOfLine();
+#if ENABLE(CSS_TRAILING_WORD)
+ InlineIterator optimalLineBreakLocationForTrailingWord();
+#endif
void clearLineBreakIfFitsOnLine(bool ignoringTrailingSpace = false)
{
if (m_width.fitsOnLine(ignoringTrailingSpace) || m_lastWS == NOWRAP)
- m_lineBreak.clear();
+ m_lineBreakHistory.clear();
}
void commitLineBreakAtCurrentWidth(RenderObject& object, unsigned offset = 0, int nextBreak = -1)
{
m_width.commit();
- m_lineBreak.moveTo(&object, offset, nextBreak);
+ m_lineBreakHistory.moveTo(&object, offset, nextBreak);
}
private:
+ // This class keeps a sliding window of the past n locations for an InlineIterator.
+ class InlineIteratorHistory : private Vector<InlineIterator, 1> {
+ public:
+ InlineIteratorHistory() = delete;
+ InlineIteratorHistory(const InlineIterator& initial, size_t capacity)
+ : m_capacity(capacity)
+ {
+ ASSERT(capacity > 0);
+ this->append(initial);
+ }
+
+ void push(std::function<void(InlineIterator& modifyMe)> updater)
+ {
+ ASSERT(!this->isEmpty());
+ if (m_capacity != 1)
+ this->insert(0, InlineIterator(this->at(0)));
+ updater(this->at(0));
+ if (m_capacity != 1)
+ this->resize(m_capacity);
+ }
+
+ void update(std::function<void(InlineIterator& modifyMe)> updater)
+ {
+ ASSERT(!this->isEmpty());
+ updater(this->at(0));
+ }
+
+ RenderObject* renderer() const { return this->at(0).renderer(); }
+ unsigned offset() const { return this->at(0).offset(); }
+ bool atTextParagraphSeparator() const { return this->at(0).atTextParagraphSeparator(); }
+ UChar previousInSameNode() const { return this->at(0).previousInSameNode(); }
+ const InlineIterator& get(size_t i) const { return this->at(i); };
+ const InlineIterator& current() const { return get(0); }
+ size_t historyLength() const { return this->size(); }
+
+ void moveTo(RenderObject* object, unsigned offset, int nextBreak = -1)
+ {
+ push([&](InlineIterator& modifyMe) {
+ modifyMe.moveTo(object, offset, nextBreak);
+ });
+ }
+
+ void increment()
+ {
+ update([](InlineIterator& modifyMe) {
+ modifyMe.increment();
+ });
+ }
+
+ void clear()
+ {
+ push([](InlineIterator& modifyMe) {
+ modifyMe.clear();
+ });
+ }
+
+ private:
+ const size_t m_capacity;
+ };
+
LineBreaker& m_lineBreaker;
InlineBidiResolver& m_resolver;
InlineIterator m_current;
- InlineIterator m_lineBreak;
+ InlineIteratorHistory m_lineBreakHistory;
InlineIterator m_startOfIgnoredSpaces;
RenderBlockFlow& m_block;
{
if (m_width.fitsOnLine()) {
RenderObject* br = m_current.renderer();
- m_lineBreak.moveToStartOf(br);
- m_lineBreak.increment();
+ m_lineBreakHistory.push([&](InlineIterator& modifyMe) {
+ modifyMe.moveToStartOf(br);
+ modifyMe.increment();
+ });
// A <br> always breaks a line, so don't let the line be collapsed
// away. Also, the space at the end of a line with a <br> does not
// it after moving to next line (in clearFloats() func)
if (m_floatsFitOnLine && m_width.fitsOnLineExcludingTrailingWhitespace(m_block.logicalWidthForFloat(floatingObject))) {
m_lineBreaker.positionNewFloatOnLine(floatingObject, m_lastFloatFromPreviousLine, m_lineInfo, m_width);
- if (m_lineBreak.renderer() == m_current.renderer()) {
- ASSERT(!m_lineBreak.offset());
- m_lineBreak.increment();
+ if (m_lineBreakHistory.renderer() == m_current.renderer()) {
+ ASSERT(!m_lineBreakHistory.offset());
+ m_lineBreakHistory.increment();
}
} else
m_floatsFitOnLine = false;
auto& combineRenderer = downcast<RenderCombineText>(*m_current.renderer());
combineRenderer.combineText();
// The length of the renderer's text may have changed. Increment stale iterator positions
- if (iteratorIsBeyondEndOfRenderCombineText(m_lineBreak, combineRenderer)) {
+ if (iteratorIsBeyondEndOfRenderCombineText(m_lineBreakHistory.current(), combineRenderer)) {
ASSERT(iteratorIsBeyondEndOfRenderCombineText(m_resolver.position(), combineRenderer));
- m_lineBreak.increment();
+ m_lineBreakHistory.increment();
m_resolver.increment();
}
}
// additional whitespace.
if (!m_width.fitsOnLineIncludingExtraWidth(charWidth)) {
lineWasTooWide = true;
- m_lineBreak.moveTo(m_current.renderer(), m_current.offset(), m_current.nextBreakablePosition());
- m_lineBreaker.skipTrailingWhitespace(m_lineBreak, m_lineInfo);
+ m_lineBreakHistory.push([&](InlineIterator& modifyMe) {
+ modifyMe.moveTo(m_current.renderer(), m_current.offset(), m_current.nextBreakablePosition());
+ m_lineBreaker.skipTrailingWhitespace(modifyMe, m_lineInfo);
+ });
}
}
if (lineWasTooWide || !m_width.fitsOnLine()) {
if (canHyphenate && !m_width.fitsOnLine()) {
- tryHyphenating(&renderText, font, style.locale(), consecutiveHyphenatedLines, m_blockStyle.hyphenationLimitLines(), style.hyphenationLimitBefore(), style.hyphenationLimitAfter(), lastSpace, m_current.offset(), m_width.currentWidth() - additionalTempWidth, m_width.availableWidth(), isFixedPitch, m_collapseWhiteSpace, lastSpaceWordSpacing, m_lineBreak, m_current.nextBreakablePosition(), m_lineBreaker.m_hyphenated);
+ m_lineBreakHistory.push([&](InlineIterator& modifyMe) {
+ tryHyphenating(&renderText, font, style.locale(), consecutiveHyphenatedLines, m_blockStyle.hyphenationLimitLines(), style.hyphenationLimitBefore(), style.hyphenationLimitAfter(), lastSpace, m_current.offset(), m_width.currentWidth() - additionalTempWidth, m_width.availableWidth(), isFixedPitch, m_collapseWhiteSpace, lastSpaceWordSpacing, modifyMe, m_current.nextBreakablePosition(), m_lineBreaker.m_hyphenated);
+ });
if (m_lineBreaker.m_hyphenated) {
m_atEnd = true;
return false;
}
}
- if (m_lineBreak.atTextParagraphSeparator()) {
+ if (m_lineBreakHistory.atTextParagraphSeparator()) {
if (!stoppedIgnoringSpaces && m_current.offset() > 0)
ensureCharacterGetsLineBox(m_lineMidpointState, m_current);
- m_lineBreak.increment();
+ m_lineBreakHistory.increment();
m_lineInfo.setPreviousLineBrokeCleanly(true);
- wordMeasurement.endOffset = m_lineBreak.offset();
+ wordMeasurement.endOffset = m_lineBreakHistory.offset();
}
- if (m_lineBreak.offset() && downcast<RenderText>(m_lineBreak.renderer()) && downcast<RenderText>(*m_lineBreak.renderer()).textLength() && downcast<RenderText>(*m_lineBreak.renderer()).characterAt(m_lineBreak.offset() - 1) == softHyphen && style.hyphens() != HyphensNone)
+ if (m_lineBreakHistory.offset() && downcast<RenderText>(m_lineBreakHistory.renderer()) && downcast<RenderText>(*m_lineBreakHistory.renderer()).textLength() && downcast<RenderText>(*m_lineBreakHistory.renderer()).characterAt(m_lineBreakHistory.offset() - 1) == softHyphen && style.hyphens() != HyphensNone)
hyphenated = true;
- if (m_lineBreak.offset() && m_lineBreak.offset() != (unsigned)wordMeasurement.endOffset && !wordMeasurement.width) {
+ if (m_lineBreakHistory.offset() && m_lineBreakHistory.offset() != (unsigned)wordMeasurement.endOffset && !wordMeasurement.width) {
if (charWidth) {
- wordMeasurement.endOffset = m_lineBreak.offset();
+ wordMeasurement.endOffset = m_lineBreakHistory.offset();
wordMeasurement.width = charWidth;
}
}
if (!stoppedIgnoringSpaces && m_current.offset())
ensureCharacterGetsLineBox(m_lineMidpointState, m_current);
commitLineBreakAtCurrentWidth(*m_current.renderer(), m_current.offset(), m_current.nextBreakablePosition());
- m_lineBreak.increment();
+ m_lineBreakHistory.increment();
m_lineInfo.setPreviousLineBrokeCleanly(true);
return true;
}
if (midWordBreak && !U16_IS_TRAIL(c) && !(U_GET_GC_MASK(c) & U_GC_M_MASK)) {
// Remember this as a breakable position in case
// adding the end width forces a break.
- m_lineBreak.moveTo(m_current.renderer(), m_current.offset(), m_current.nextBreakablePosition());
+ m_lineBreakHistory.moveTo(m_current.renderer(), m_current.offset(), m_current.nextBreakablePosition());
midWordBreak &= (breakWords || breakAll);
}
if (!m_currentCharacterIsWS && previousCharacterIsWS) {
if (m_autoWrap && m_currentStyle->breakOnlyAfterWhiteSpace())
- m_lineBreak.moveTo(m_current.renderer(), m_current.offset(), m_current.nextBreakablePosition());
+ m_lineBreakHistory.moveTo(m_current.renderer(), m_current.offset(), m_current.nextBreakablePosition());
}
if (m_collapseWhiteSpace && m_currentCharacterIsSpace && !m_ignoringSpaces)
m_includeEndWidth = false;
if (!m_width.fitsOnLine()) {
- if (canHyphenate)
- tryHyphenating(&renderText, font, style.locale(), consecutiveHyphenatedLines, m_blockStyle.hyphenationLimitLines(), style.hyphenationLimitBefore(), style.hyphenationLimitAfter(), lastSpace, m_current.offset(), m_width.currentWidth() - additionalTempWidth, m_width.availableWidth(), isFixedPitch, m_collapseWhiteSpace, lastSpaceWordSpacing, m_lineBreak, m_current.nextBreakablePosition(), m_lineBreaker.m_hyphenated);
+ if (canHyphenate) {
+ m_lineBreakHistory.push([&](InlineIterator& modifyMe) {
+ tryHyphenating(&renderText, font, style.locale(), consecutiveHyphenatedLines, m_blockStyle.hyphenationLimitLines(), style.hyphenationLimitBefore(), style.hyphenationLimitAfter(), lastSpace, m_current.offset(), m_width.currentWidth() - additionalTempWidth, m_width.availableWidth(), isFixedPitch, m_collapseWhiteSpace, lastSpaceWordSpacing, modifyMe, m_current.nextBreakablePosition(), m_lineBreaker.m_hyphenated);
+ });
+ }
- if (!hyphenated && m_lineBreak.previousInSameNode() == softHyphen && style.hyphens() != HyphensNone) {
+ if (!hyphenated && m_lineBreakHistory.previousInSameNode() == softHyphen && style.hyphens() != HyphensNone) {
hyphenated = true;
m_atEnd = true;
}
if (!m_current.renderer()->isFloatingOrOutOfFlowPositioned()) {
m_lastObject = m_current.renderer();
- if (m_lastObject->isReplaced() && m_autoWrap && !m_lastObject->isRubyRun() && (!m_lastObject->isImage() || m_allowImagesToBreak) && (!is<RenderListMarker>(*m_lastObject) || downcast<RenderListMarker>(*m_lastObject).isInside())) {
+ if (m_lastObject->isReplaced() && m_autoWrap && !m_lastObject->isRubyRun() && (!m_lastObject->isImage() || m_allowImagesToBreak) && (!is<RenderListMarker>(*m_lastObject) || downcast<RenderListMarker>(*m_lastObject).isInside()))
commitLineBreakAtCurrentWidth(*m_nextObject);
- }
}
}
-inline TrailingObjects::CollapseFirstSpaceOrNot checkMidpoints(LineMidpointState& lineMidpointState, InlineIterator& lBreak)
+inline TrailingObjects::CollapseFirstSpaceOrNot checkMidpoints(LineMidpointState& lineMidpointState, const InlineIterator& lBreak)
{
// Check to see if our last midpoint is a start point beyond the line break. If so,
// shave it off the list, and shave off a trailing space if the previous end point doesn't
inline InlineIterator BreakingContext::handleEndOfLine()
{
- if (m_lineBreak == m_resolver.position()) {
- if (!m_lineBreak.renderer() || !m_lineBreak.renderer()->isBR()) {
+ if (m_lineBreakHistory.current() == m_resolver.position()) {
+ if (!m_lineBreakHistory.renderer() || !m_lineBreakHistory.renderer()->isBR()) {
// we just add as much as possible
if (m_blockStyle.whiteSpace() == PRE && !m_current.offset())
commitLineBreakAtCurrentWidth(*m_lastObject, m_lastObject->isText() ? m_lastObject->length() : 0);
- else if (m_lineBreak.renderer()) {
+ else if (m_lineBreakHistory.renderer()) {
// Don't ever break in the middle of a word if we can help it.
// There's no room at all. We just have to be on this line,
// even though we'll spill out.
}
}
// make sure we consume at least one char/object.
- if (m_lineBreak == m_resolver.position())
- m_lineBreak.increment();
+ if (m_lineBreakHistory.current() == m_resolver.position())
+ m_lineBreakHistory.increment();
} else if (!m_current.offset() && !m_width.committedWidth() && m_width.uncommittedWidth() && !m_hadUncommittedWidthBeforeCurrent) {
// Do not push the current object to the next line, when this line has some content, but it is still considered empty.
// Empty inline elements like <span></span> can produce such lines and now we just ignore these break opportunities
// at the start of a line, if no width has been committed yet.
// Behave as if it was actually empty and consume at least one object.
- m_lineBreak.increment();
+ m_lineBreakHistory.increment();
}
// Sanity check our midpoints.
- TrailingObjects::CollapseFirstSpaceOrNot collapsed = checkMidpoints(m_lineMidpointState, m_lineBreak);
+ TrailingObjects::CollapseFirstSpaceOrNot collapsed = checkMidpoints(m_lineMidpointState, m_lineBreakHistory.current());
- m_trailingObjects.updateMidpointsForTrailingBoxes(m_lineMidpointState, m_lineBreak, collapsed);
+ m_trailingObjects.updateMidpointsForTrailingBoxes(m_lineMidpointState, m_lineBreakHistory.current(), collapsed);
// We might have made lineBreak an iterator that points past the end
// of the object. Do this adjustment to make it point to the start
// of the next object instead to avoid confusing the rest of the
// code.
- if (m_lineBreak.offset()) {
- m_lineBreak.setOffset(m_lineBreak.offset() - 1);
- m_lineBreak.increment();
+ if (m_lineBreakHistory.offset()) {
+ m_lineBreakHistory.update([](InlineIterator& modifyMe) {
+ modifyMe.setOffset(modifyMe.offset() - 1);
+ modifyMe.increment();
+ });
}
- return m_lineBreak;
+#if ENABLE(CSS_TRAILING_WORD)
+ if (m_blockStyle.trailingWord() == TrailingWord::PartiallyBalanced)
+ return optimalLineBreakLocationForTrailingWord();
+#endif
+ return m_lineBreakHistory.current();
}
+#if ENABLE(CSS_TRAILING_WORD)
+inline InlineIterator BreakingContext::optimalLineBreakLocationForTrailingWord()
+{
+ const unsigned longTrailingWordLength = 20;
+ const float optimalTrailingLineRatio = 0.1;
+ InlineIterator lineBreak = m_lineBreakHistory.current();
+ if (!lineBreak.renderer() || !m_lineInfo.isFirstLine() || bidiNextSkippingEmptyInlines(*lineBreak.root(), lineBreak.renderer()) || !is<RenderText>(lineBreak.renderer()))
+ return lineBreak;
+ RenderText& renderText = downcast<RenderText>(*lineBreak.renderer());
+ // Don't even bother measuring if our remaining line has many characters
+ if (renderText.textLength() == lineBreak.offset() || renderText.textLength() - lineBreak.offset() > longTrailingWordLength)
+ return lineBreak;
+ bool isLooseCJKMode = m_renderTextInfo.m_text != &renderText && m_renderTextInfo.m_lineBreakIterator.isLooseCJKMode();
+ bool breakNBSP = m_autoWrap && m_currentStyle->nbspMode() == SPACE;
+ int nextBreakablePosition = lineBreak.nextBreakablePosition();
+ isBreakable(m_renderTextInfo.m_lineBreakIterator, lineBreak.offset() + 1, nextBreakablePosition, breakNBSP, isLooseCJKMode);
+ if (nextBreakablePosition < 0 || static_cast<unsigned>(nextBreakablePosition) != renderText.textLength())
+ return lineBreak;
+ const RenderStyle& style = lineStyle(renderText, m_lineInfo);
+ const FontCascade& font = style.fontCascade();
+ HashSet<const Font*> dummyFonts;
+ InlineIterator best = lineBreak;
+ for (size_t i = 1; i < m_lineBreakHistory.historyLength(); ++i) {
+ const InlineIterator& candidate = m_lineBreakHistory.get(i);
+ if (candidate.renderer() != lineBreak.renderer())
+ return best;
+ float width = textWidth(&renderText, candidate.offset(), renderText.textLength() - candidate.offset(), font, 0, font.isFixedPitch(), m_collapseWhiteSpace, dummyFonts);
+ if (width > m_width.availableWidth())
+ return best;
+ if (width / m_width.availableWidth() > optimalTrailingLineRatio) // Subsequent line is long enough
+ return candidate;
+ best = candidate;
+ }
+ return best;
+}
+#endif
+
}
#endif // BreakingContextInlineHeaders_h