#pragma once
#include "BreakLines.h"
#include "Hyphenation.h"
#include "LineBreaker.h"
#include "LineInfo.h"
#include "LineLayoutState.h"
#include "LineWidth.h"
#include "RenderCombineText.h"
#include "RenderCounter.h"
#include "RenderInline.h"
#include "RenderLayer.h"
#include "RenderLineBreak.h"
#include "RenderListMarker.h"
#include "RenderRubyRun.h"
#include "RenderSVGInlineText.h"
#include "TrailingObjects.h"
#include <wtf/Function.h>
#include <wtf/Optional.h>
#include <wtf/text/StringView.h>
#include <wtf/unicode/CharacterNames.h>
namespace WebCore {
const unsigned cMaxLineDepth = 200;
struct WordMeasurement {
WordMeasurement()
: renderer(0)
, width(0)
, startOffset(0)
, endOffset(0)
{
}
RenderText* renderer;
float width;
unsigned startOffset;
unsigned endOffset;
HashSet<const Font*> fallbackFonts;
};
struct WordTrailingSpace {
WordTrailingSpace(const RenderStyle& style, bool measuringWithTrailingWhitespaceEnabled = true)
: m_style(style)
{
if (!measuringWithTrailingWhitespaceEnabled || !m_style.fontCascade().enableKerning())
m_state = WordTrailingSpaceState::Initialized;
}
Optional<float> width(HashSet<const Font*>& fallbackFonts)
{
if (m_state == WordTrailingSpaceState::Initialized)
return m_width;
auto& font = m_style.fontCascade();
m_width = font.width(RenderBlock::constructTextRun(&space, 1, m_style), &fallbackFonts) + font.wordSpacing();
m_state = WordTrailingSpaceState::Initialized;
return m_width;
}
private:
enum class WordTrailingSpaceState { Uninitialized, Initialized };
const RenderStyle& m_style;
WordTrailingSpaceState m_state { WordTrailingSpaceState::Uninitialized };
Optional<float> m_width;
};
class BreakingContext {
public:
BreakingContext(LineBreaker& lineBreaker, InlineBidiResolver& resolver, LineInfo& inLineInfo, LineWidth& lineWidth, RenderTextInfo& inRenderTextInfo, FloatingObject* inLastFloatFromPreviousLine, bool appliedStartWidth, RenderBlockFlow& block)
: m_lineBreaker(lineBreaker)
, m_resolver(resolver)
, m_current(resolver.position())
, m_lineBreak(resolver.position())
, m_block(block)
, m_lastObject(m_current.renderer())
, m_nextObject(nullptr)
, m_currentStyle(nullptr)
, m_blockStyle(block.style())
, m_lineInfo(inLineInfo)
, m_renderTextInfo(inRenderTextInfo)
, m_lastFloatFromPreviousLine(inLastFloatFromPreviousLine)
, m_width(lineWidth)
, m_currWS(WhiteSpace::Normal)
, m_lastWS(WhiteSpace::Normal)
, m_preservesNewline(false)
, m_atStart(true)
, m_ignoringSpaces(false)
, m_currentCharacterIsSpace(false)
, m_currentCharacterIsWS(false)
, m_hasFormerOpportunity(false)
, m_appliedStartWidth(appliedStartWidth)
, m_includeEndWidth(true)
, m_autoWrap(false)
, m_autoWrapWasEverTrueOnLine(false)
, m_floatsFitOnLine(true)
, m_collapseWhiteSpace(false)
, m_startingNewParagraph(m_lineInfo.previousLineBrokeCleanly())
, m_allowImagesToBreak(!block.document().inQuirksMode() || !block.isTableCell() || !m_blockStyle.logicalWidth().isIntrinsicOrAuto())
, m_atEnd(false)
, m_hadUncommittedWidthBeforeCurrent(false)
, m_lineWhitespaceCollapsingState(resolver.whitespaceCollapsingState())
{
m_lineInfo.setPreviousLineBrokeCleanly(false);
}
RenderObject* currentObject() { return m_current.renderer(); }
InlineIterator lineBreak() { return m_lineBreak; }
LineWidth& lineWidth() { return m_width; }
bool atEnd() { return m_atEnd; }
bool fitsOnLineOrHangsAtEnd() const { return m_width.fitsOnLine() || m_hangsAtEnd; }
void initializeForCurrentObject();
void increment();
void handleBR(Clear&);
void handleOutOfFlowPositioned(Vector<RenderBox*>& positionedObjects);
void handleFloat();
void handleEmptyInline();
void handleReplaced();
bool handleText(WordMeasurements&, bool& hyphenated, unsigned& consecutiveHyphenatedLines);
void trailingSpacesHang(InlineIterator&, RenderObject&, bool canBreakMidWord, bool previousCharacterIsSpace);
bool canBreakAtThisPosition();
void commitAndUpdateLineBreakIfNeeded();
InlineIterator handleEndOfLine();
float computeAdditionalBetweenWordsWidth(RenderText&, TextLayout*, UChar, WordTrailingSpace&, HashSet<const Font*>& fallbackFonts, WordMeasurements&, const FontCascade&, bool isFixedPitch, unsigned lastSpace, float lastSpaceWordSpacing, float wordSpacingForWordMeasurement, unsigned offset);
void clearLineBreakIfFitsOnLine(bool ignoringTrailingSpace = false)
{
if (m_width.fitsOnLine(ignoringTrailingSpace) || m_lastWS == WhiteSpace::NoWrap || m_hangsAtEnd)
m_lineBreak.clear();
m_hangsAtEnd = false;
}
void commitLineBreakClear()
{
m_width.commit();
m_lineBreak.clear();
m_hangsAtEnd = false;
}
void commitLineBreakAtCurrentWidth(RenderObject& object, unsigned offset = 0, Optional<unsigned> nextBreak = Optional<unsigned>())
{
m_width.commit();
m_lineBreak.moveTo(object, offset, nextBreak);
m_hangsAtEnd = false;
}
private:
LineBreaker& m_lineBreaker;
InlineBidiResolver& m_resolver;
InlineIterator m_current;
InlineIterator m_lineBreak;
InlineIterator m_startOfIgnoredSpaces;
RenderBlockFlow& m_block;
RenderObject* m_lastObject;
RenderObject* m_nextObject;
const RenderStyle* m_currentStyle;
const RenderStyle& m_blockStyle;
LineInfo& m_lineInfo;
RenderTextInfo& m_renderTextInfo;
FloatingObject* m_lastFloatFromPreviousLine;
LineWidth m_width;
WhiteSpace m_currWS;
WhiteSpace m_lastWS;
bool m_preservesNewline;
bool m_atStart;
bool m_ignoringSpaces;
bool m_currentCharacterIsSpace;
bool m_currentCharacterIsWS;
bool m_hasFormerOpportunity;
bool m_appliedStartWidth;
bool m_includeEndWidth;
bool m_autoWrap;
bool m_autoWrapWasEverTrueOnLine;
bool m_floatsFitOnLine;
bool m_collapseWhiteSpace;
bool m_startingNewParagraph;
bool m_allowImagesToBreak;
bool m_atEnd;
bool m_hadUncommittedWidthBeforeCurrent;
bool m_hangsAtEnd { false };
LineWhitespaceCollapsingState& m_lineWhitespaceCollapsingState;
TrailingObjects m_trailingObjects;
};
inline void BreakingContext::initializeForCurrentObject()
{
m_hadUncommittedWidthBeforeCurrent = !!m_width.uncommittedWidth();
m_currentStyle = &m_current.renderer()->style();
ASSERT(m_currentStyle);
m_nextObject = bidiNextSkippingEmptyInlines(m_block, m_current.renderer());
if (m_nextObject && m_nextObject->parent() && !m_nextObject->parent()->isDescendantOf(m_current.renderer()->parent()))
m_includeEndWidth = true;
m_currWS = m_current.renderer()->isReplaced() ? m_current.renderer()->parent()->style().whiteSpace() : m_currentStyle->whiteSpace();
m_lastWS = m_lastObject->isReplaced() ? m_lastObject->parent()->style().whiteSpace() : m_lastObject->style().whiteSpace();
m_autoWrap = RenderStyle::autoWrap(m_currWS);
m_autoWrapWasEverTrueOnLine = m_autoWrapWasEverTrueOnLine || m_autoWrap;
m_preservesNewline = m_current.renderer()->isSVGInlineText() ? false : RenderStyle::preserveNewline(m_currWS);
m_collapseWhiteSpace = RenderStyle::collapseWhiteSpace(m_currWS);
}
inline void BreakingContext::increment()
{
if (!m_collapseWhiteSpace)
m_currentCharacterIsSpace = false;
if (m_nextObject)
m_current.moveToStartOf(*m_nextObject);
else
m_current.clear();
m_atStart = false;
}
inline void BreakingContext::handleBR(Clear& clear)
{
if (fitsOnLineOrHangsAtEnd()) {
RenderObject& br = *m_current.renderer();
m_lineBreak.moveToStartOf(br);
m_lineBreak.increment();
if (m_startingNewParagraph)
m_lineInfo.setEmpty(false, &m_block, &m_width);
m_trailingObjects.clear();
m_lineInfo.setPreviousLineBrokeCleanly(true);
if (m_ignoringSpaces && m_currentStyle->clear() != Clear::None)
m_lineWhitespaceCollapsingState.ensureLineBoxInsideIgnoredSpaces(br);
else if (m_ignoringSpaces && (m_blockStyle.textAlign() == TextAlignMode::Right || m_blockStyle.textAlign() == TextAlignMode::WebKitRight))
m_lineWhitespaceCollapsingState.stopIgnoringSpaces(InlineIterator(0, m_current.renderer(), m_current.offset()));
if (!m_lineInfo.isEmpty())
clear = m_currentStyle->clear();
}
m_atEnd = true;
}
inline LayoutUnit borderPaddingMarginStart(const RenderInline& child)
{
return child.marginStart() + child.paddingStart() + child.borderStart();
}
inline LayoutUnit borderPaddingMarginEnd(const RenderInline& child)
{
return child.marginEnd() + child.paddingEnd() + child.borderEnd();
}
inline bool shouldAddBorderPaddingMargin(RenderObject* child)
{
if (!child)
return true;
if (is<RenderText>(*child) && !downcast<RenderText>(*child).text().length())
return true;
#if ENABLE(CSS_BOX_DECORATION_BREAK)
if (is<RenderLineBreak>(*child) && child->parent()->style().boxDecorationBreak() == BoxDecorationBreak::Clone)
return true;
#endif
return false;
}
inline RenderObject* previousInFlowSibling(RenderObject* child)
{
do {
child = child->previousSibling();
} while (child && child->isOutOfFlowPositioned());
return child;
}
inline LayoutUnit inlineLogicalWidth(RenderObject* child, bool checkStartEdge = true, bool checkEndEdge = true)
{
unsigned lineDepth = 1;
LayoutUnit extraWidth;
RenderElement* parent = child->parent();
while (is<RenderInline>(*parent) && lineDepth++ < cMaxLineDepth) {
const auto& parentAsRenderInline = downcast<RenderInline>(*parent);
if (!isEmptyInline(parentAsRenderInline)) {
checkStartEdge = checkStartEdge && shouldAddBorderPaddingMargin(previousInFlowSibling(child));
if (checkStartEdge)
extraWidth += borderPaddingMarginStart(parentAsRenderInline);
checkEndEdge = checkEndEdge && shouldAddBorderPaddingMargin(child->nextSibling());
if (checkEndEdge)
extraWidth += borderPaddingMarginEnd(parentAsRenderInline);
if (!checkStartEdge && !checkEndEdge)
return extraWidth;
}
child = parent;
parent = child->parent();
}
return extraWidth;
}
inline void BreakingContext::handleOutOfFlowPositioned(Vector<RenderBox*>& positionedObjects)
{
auto& box = downcast<RenderBox>(*m_current.renderer());
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 || box.container()->isRenderInline()) {
if (m_ignoringSpaces)
m_lineWhitespaceCollapsingState.ensureLineBoxInsideIgnoredSpaces(box);
m_trailingObjects.appendBoxIfNeeded(box);
} else
positionedObjects.append(&box);
m_width.addUncommittedWidth(inlineLogicalWidth(&box));
m_renderTextInfo.lineBreakIterator.resetPriorContext();
}
inline void BreakingContext::handleFloat()
{
auto& floatBox = downcast<RenderBox>(*m_current.renderer());
const auto& floatingObject = *m_lineBreaker.insertFloatingObject(floatBox);
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();
}
} else
m_floatsFitOnLine = false;
m_renderTextInfo.lineBreakIterator.updatePriorContext(replacementCharacter);
}
inline bool shouldSkipWhitespaceAfterStartObject(RenderBlockFlow& block, RenderObject* o, LineWhitespaceCollapsingState& lineWhitespaceCollapsingState)
{
RenderObject* next = bidiNextSkippingEmptyInlines(block, o);
while (next && next->isFloatingOrOutOfFlowPositioned())
next = bidiNextSkippingEmptyInlines(block, next);
if (is<RenderText>(next) && downcast<RenderText>(*next).text().length() > 0) {
RenderText& nextText = downcast<RenderText>(*next);
UChar nextChar = nextText.characterAt(0);
if (nextText.style().isCollapsibleWhiteSpace(nextChar)) {
lineWhitespaceCollapsingState.startIgnoringSpaces(InlineIterator(nullptr, o, 0));
return true;
}
}
return false;
}
inline void BreakingContext::handleEmptyInline()
{
RenderInline& flowBox = downcast<RenderInline>(*m_current.renderer());
ASSERT(isEmptyInline(flowBox));
bool requiresLineBox = alwaysRequiresLineBox(flowBox);
if (requiresLineBox || requiresLineBoxForContent(flowBox, m_lineInfo)) {
if (requiresLineBox)
m_lineInfo.setEmpty(false, &m_block, &m_width);
if (m_ignoringSpaces) {
m_trailingObjects.clear();
m_lineWhitespaceCollapsingState.ensureLineBoxInsideIgnoredSpaces(*m_current.renderer());
} else if (m_blockStyle.collapseWhiteSpace() && m_resolver.position().renderer() == m_current.renderer()
&& shouldSkipWhitespaceAfterStartObject(m_block, m_current.renderer(), m_lineWhitespaceCollapsingState)) {
m_currentCharacterIsSpace = true;
m_currentCharacterIsWS = true;
m_ignoringSpaces = true;
} else
m_trailingObjects.appendBoxIfNeeded(flowBox);
}
float inlineWidth = inlineLogicalWidth(m_current.renderer()) + borderPaddingMarginStart(flowBox) + borderPaddingMarginEnd(flowBox);
m_width.addUncommittedWidth(inlineWidth);
if (m_hangsAtEnd && inlineWidth)
m_hangsAtEnd = false;
}
inline void BreakingContext::handleReplaced()
{
auto& replacedBox = downcast<RenderBox>(*m_current.renderer());
if (m_atStart)
m_width.updateAvailableWidth(replacedBox.logicalHeight());
if ((m_autoWrap || RenderStyle::autoWrap(m_lastWS)) && (!replacedBox.isImage() || m_allowImagesToBreak)
&& (!is<RenderRubyRun>(replacedBox) || downcast<RenderRubyRun>(replacedBox).canBreakBefore(m_renderTextInfo.lineBreakIterator))) {
if (auto* renderer = m_current.renderer())
commitLineBreakAtCurrentWidth(*renderer);
else
commitLineBreakClear();
} else
m_hangsAtEnd = false;
if (m_ignoringSpaces)
m_lineWhitespaceCollapsingState.stopIgnoringSpaces(InlineIterator(0, &replacedBox, 0));
m_lineInfo.setEmpty(false, &m_block, &m_width);
m_ignoringSpaces = false;
m_currentCharacterIsSpace = false;
m_currentCharacterIsWS = false;
m_trailingObjects.clear();
LayoutUnit replacedLogicalWidth = m_block.logicalWidthForChild(replacedBox) + m_block.marginStartForChild(replacedBox) + m_block.marginEndForChild(replacedBox) + inlineLogicalWidth(&replacedBox);
if (is<RenderListMarker>(replacedBox)) {
if (m_blockStyle.collapseWhiteSpace() && shouldSkipWhitespaceAfterStartObject(m_block, &replacedBox, m_lineWhitespaceCollapsingState)) {
m_currentCharacterIsSpace = true;
m_currentCharacterIsWS = false;
m_ignoringSpaces = true;
}
if (downcast<RenderListMarker>(replacedBox).isInside())
m_width.addUncommittedReplacedWidth(replacedLogicalWidth);
} else
m_width.addUncommittedReplacedWidth(replacedLogicalWidth);
if (is<RenderRubyRun>(replacedBox)) {
m_width.applyOverhang(downcast<RenderRubyRun>(replacedBox), m_lastObject, m_nextObject);
downcast<RenderRubyRun>(replacedBox).updatePriorContextFromCachedBreakIterator(m_renderTextInfo.lineBreakIterator);
} else {
m_renderTextInfo.lineBreakIterator.updatePriorContext(replacementCharacter);
}
}
inline float firstPositiveWidth(const WordMeasurements& wordMeasurements)
{
for (size_t i = 0; i < wordMeasurements.size(); ++i) {
if (wordMeasurements[i].width > 0)
return wordMeasurements[i].width;
}
return 0;
}
inline bool iteratorIsBeyondEndOfRenderCombineText(const InlineIterator& iter, RenderCombineText& renderer)
{
return iter.renderer() == &renderer && iter.offset() >= renderer.text().length();
}
inline void nextCharacter(UChar& currentCharacter, UChar& lastCharacter, UChar& secondToLastCharacter)
{
secondToLastCharacter = lastCharacter;
lastCharacter = currentCharacter;
}
inline void updateCounterIfNeeded(RenderText& renderText)
{
if (!renderText.preferredLogicalWidthsDirty() || !is<RenderCounter>(renderText))
return;
downcast<RenderCounter>(renderText).updateCounter();
}
inline float measureHyphenWidth(RenderText& renderer, const FontCascade& font, HashSet<const Font*>* fallbackFonts = 0)
{
const RenderStyle& style = renderer.style();
return font.width(RenderBlock::constructTextRun(style.hyphenString().string(), style), fallbackFonts);
}
ALWAYS_INLINE float textWidth(RenderText& text, unsigned from, unsigned len, const FontCascade& font, float xPos, bool isFixedPitch, bool collapseWhiteSpace, HashSet<const Font*>& fallbackFonts, TextLayout* layout = nullptr)
{
const RenderStyle& style = text.style();
GlyphOverflow glyphOverflow;
if (isFixedPitch || (!from && len == text.text().length()) || style.hasTextCombine())
return text.width(from, len, font, xPos, &fallbackFonts, &glyphOverflow);
if (layout)
return FontCascade::width(*layout, from, len, &fallbackFonts);
TextRun run = RenderBlock::constructTextRun(text, from, len, style);
run.setCharacterScanForCodePath(!text.canUseSimpleFontCodePath());
run.setTabSize(!collapseWhiteSpace, style.tabSize());
run.setXPos(xPos);
return font.width(run, &fallbackFonts, &glyphOverflow);
}
inline void ensureCharacterGetsLineBox(LineWhitespaceCollapsingState& lineWhitespaceCollapsingState, InlineIterator& textParagraphSeparator)
{
InlineIterator transition(0, textParagraphSeparator.renderer(), textParagraphSeparator.offset());
lineWhitespaceCollapsingState.startIgnoringSpaces(InlineIterator(0, textParagraphSeparator.renderer(), textParagraphSeparator.offset() - 1));
lineWhitespaceCollapsingState.stopIgnoringSpaces(InlineIterator(0, textParagraphSeparator.renderer(), textParagraphSeparator.offset()));
}
inline void tryHyphenating(RenderText& text, const FontCascade& font, const AtomString& localeIdentifier, unsigned consecutiveHyphenatedLines, int consecutiveHyphenatedLinesLimit, int minimumPrefixLimit, int minimumSuffixLimit, unsigned lastSpace, unsigned pos, float xPos, float availableWidth, bool isFixedPitch, bool collapseWhiteSpace, int lastSpaceWordSpacing, InlineIterator& lineBreak, Optional<unsigned> nextBreakable, bool& hyphenated)
{
unsigned minimumPrefixLength;
unsigned minimumSuffixLength;
if (minimumPrefixLimit < 0)
minimumPrefixLength = 2;
else
minimumPrefixLength = static_cast<unsigned>(minimumPrefixLimit);
if (minimumSuffixLimit < 0)
minimumSuffixLength = 2;
else
minimumSuffixLength = static_cast<unsigned>(minimumSuffixLimit);
if (pos - lastSpace <= minimumSuffixLength)
return;
if (consecutiveHyphenatedLinesLimit >= 0 && consecutiveHyphenatedLines >= static_cast<unsigned>(consecutiveHyphenatedLinesLimit))
return;
float hyphenWidth = measureHyphenWidth(text, font);
float maxPrefixWidth = availableWidth - xPos - hyphenWidth - lastSpaceWordSpacing;
if (!enoughWidthForHyphenation(maxPrefixWidth, font.pixelSize()))
return;
const RenderStyle& style = text.style();
TextRun run = RenderBlock::constructTextRun(text, lastSpace, pos - lastSpace, style);
run.setTabSize(!collapseWhiteSpace, style.tabSize());
run.setXPos(xPos + lastSpaceWordSpacing);
unsigned prefixLength = font.offsetForPosition(run, maxPrefixWidth, false);
if (prefixLength < minimumPrefixLength)
return;
prefixLength = lastHyphenLocation(StringView(text.text()).substring(lastSpace, pos - lastSpace), std::min(prefixLength, pos - lastSpace - minimumSuffixLength) + 1, localeIdentifier);
if (!prefixLength || prefixLength < minimumPrefixLength)
return;
if (prefixLength == minimumPrefixLength) {
UChar characterAtLastSpace = text.characterAt(lastSpace);
if (characterAtLastSpace == ' ' || characterAtLastSpace == '\n' || characterAtLastSpace == '\t' || characterAtLastSpace == noBreakSpace)
return;
}
ASSERT(pos - lastSpace - prefixLength >= minimumSuffixLength);
#if !ASSERT_DISABLED
HashSet<const Font*> fallbackFonts;
float prefixWidth = hyphenWidth + textWidth(text, lastSpace, prefixLength, font, xPos, isFixedPitch, collapseWhiteSpace, fallbackFonts) + lastSpaceWordSpacing;
ASSERT(xPos + prefixWidth <= availableWidth);
#else
UNUSED_PARAM(isFixedPitch);
#endif
lineBreak.moveTo(text, lastSpace + prefixLength, nextBreakable);
hyphenated = true;
}
inline float BreakingContext::computeAdditionalBetweenWordsWidth(RenderText& renderText, TextLayout* textLayout, UChar currentCharacter, WordTrailingSpace& wordTrailingSpace, HashSet<const Font*>& fallbackFonts, WordMeasurements& wordMeasurements, const FontCascade& font, bool isFixedPitch, unsigned lastSpace, float lastSpaceWordSpacing, float wordSpacingForWordMeasurement, unsigned offset)
{
wordMeasurements.grow(wordMeasurements.size() + 1);
WordMeasurement& wordMeasurement = wordMeasurements.last();
wordMeasurement.renderer = &renderText;
wordMeasurement.endOffset = offset;
wordMeasurement.startOffset = lastSpace;
float additionalTempWidth = 0;
Optional<float> wordTrailingSpaceWidth;
if (currentCharacter == ' ')
wordTrailingSpaceWidth = wordTrailingSpace.width(fallbackFonts);
if (wordTrailingSpaceWidth)
additionalTempWidth = textWidth(renderText, lastSpace, offset + 1 - lastSpace, font, m_width.currentWidth(), isFixedPitch, m_collapseWhiteSpace, wordMeasurement.fallbackFonts, textLayout) - wordTrailingSpaceWidth.value();
else
additionalTempWidth = textWidth(renderText, lastSpace, offset - lastSpace, font, m_width.currentWidth(), isFixedPitch, m_collapseWhiteSpace, wordMeasurement.fallbackFonts, textLayout);
if (wordMeasurement.fallbackFonts.isEmpty() && !fallbackFonts.isEmpty())
wordMeasurement.fallbackFonts.swap(fallbackFonts);
fallbackFonts.clear();
wordMeasurement.width = additionalTempWidth + wordSpacingForWordMeasurement;
additionalTempWidth += lastSpaceWordSpacing;
return additionalTempWidth;
}
inline bool BreakingContext::handleText(WordMeasurements& wordMeasurements, bool& hyphenated, unsigned& consecutiveHyphenatedLines)
{
if (!m_current.offset())
m_appliedStartWidth = false;
RenderObject& renderObject = *m_current.renderer();
RenderText& renderText = downcast<RenderText>(renderObject);
bool isSVGText = renderText.isSVGInlineText();
if (m_autoWrap && !RenderStyle::autoWrap(m_lastWS) && m_ignoringSpaces)
commitLineBreakAtCurrentWidth(renderText);
if (renderText.style().hasTextCombine() && is<RenderCombineText>(*m_current.renderer())) {
auto& combineRenderer = downcast<RenderCombineText>(*m_current.renderer());
combineRenderer.combineTextIfNeeded();
if (iteratorIsBeyondEndOfRenderCombineText(m_lineBreak, combineRenderer)) {
ASSERT(iteratorIsBeyondEndOfRenderCombineText(m_resolver.position(), combineRenderer));
m_lineBreak.increment();
m_resolver.increment();
}
}
const RenderStyle& style = lineStyle(renderText, m_lineInfo);
const FontCascade& font = style.fontCascade();
bool isFixedPitch = font.isFixedPitch();
bool canHyphenate = style.hyphens() == Hyphens::Auto && WebCore::canHyphenate(style.locale());
bool canHangPunctuationAtStart = style.hangingPunctuation().contains(HangingPunctuation::First);
bool canHangPunctuationAtEnd = style.hangingPunctuation().contains(HangingPunctuation::Last);
bool canHangStopOrCommaAtLineEnd = style.hangingPunctuation().contains(HangingPunctuation::AllowEnd);
int endPunctuationIndex = canHangPunctuationAtEnd && m_collapseWhiteSpace ? renderText.lastCharacterIndexStrippingSpaces() : renderText.text().length() - 1;
unsigned lastSpace = m_current.offset();
float wordSpacing = m_currentStyle->fontCascade().wordSpacing();
float lastSpaceWordSpacing = 0;
float wordSpacingForWordMeasurement = 0;
float wrapWidthOffset = m_width.uncommittedWidth() + inlineLogicalWidth(m_current.renderer(), !m_appliedStartWidth, true);
float wrapW = wrapWidthOffset;
float charWidth = 0;
bool breakNBSP = m_autoWrap && m_currentStyle->nbspMode() == NBSPMode::Space;
bool breakWords = m_currentStyle->breakWords() && ((m_autoWrap && (!m_width.committedWidth() && !m_width.hasCommittedReplaced())) || m_currWS == WhiteSpace::Pre);
bool midWordBreak = false;
bool breakAnywhere = m_currentStyle->lineBreak() == LineBreak::Anywhere && m_autoWrap;
bool breakAll = (m_currentStyle->wordBreak() == WordBreak::BreakAll || breakAnywhere) && m_autoWrap;
bool keepAllWords = m_currentStyle->wordBreak() == WordBreak::KeepAll;
float hyphenWidth = 0;
auto iteratorMode = mapLineBreakToIteratorMode(m_blockStyle.lineBreak());
bool canUseLineBreakShortcut = iteratorMode == LineBreakIteratorMode::Default;
bool isLineEmpty = m_lineInfo.isEmpty();
if (isSVGText) {
breakWords = false;
breakAll = false;
}
if (m_renderTextInfo.text != &renderText) {
updateCounterIfNeeded(renderText);
m_renderTextInfo.text = &renderText;
m_renderTextInfo.font = &font;
m_renderTextInfo.layout = font.createLayout(renderText, m_width.currentWidth(), m_collapseWhiteSpace);
m_renderTextInfo.lineBreakIterator.resetStringAndReleaseIterator(renderText.text(), style.locale(), iteratorMode);
} else if (m_renderTextInfo.layout && m_renderTextInfo.font != &font) {
m_renderTextInfo.font = &font;
m_renderTextInfo.layout = font.createLayout(renderText, m_width.currentWidth(), m_collapseWhiteSpace);
}
HashSet<const Font*> fallbackFonts;
m_hasFormerOpportunity = false;
bool canBreakMidWord = breakWords || breakAll;
UChar lastCharacterFromPreviousRenderText = m_renderTextInfo.lineBreakIterator.lastCharacter();
UChar lastCharacter = m_renderTextInfo.lineBreakIterator.lastCharacter();
UChar secondToLastCharacter = m_renderTextInfo.lineBreakIterator.secondToLastCharacter();
TextLayout* textLayout = m_renderTextInfo.layout.get();
WordTrailingSpace wordTrailingSpace(style, !textLayout);
for (; m_current.offset() < renderText.text().length(); m_current.fastIncrementInTextNode()) {
bool previousCharacterIsSpace = m_currentCharacterIsSpace;
bool previousCharacterIsWS = m_currentCharacterIsWS;
UChar c = m_current.current();
m_currentCharacterIsSpace = c == ' ' || c == '\t' || (!m_preservesNewline && (c == '\n'));
if (m_atStart && m_currentCharacterIsSpace && !previousCharacterIsSpace) {
m_hasFormerOpportunity = !breakAnywhere;
breakWords = false;
canBreakMidWord = breakAll;
}
if (canHangPunctuationAtStart && m_width.isFirstLine() && !m_width.committedWidth() && !wrapW && !inlineLogicalWidth(m_current.renderer(), true, false)) {
m_width.addUncommittedWidth(-renderText.hangablePunctuationStartWidth(m_current.offset()));
canHangPunctuationAtStart = false;
}
if (canHangPunctuationAtEnd && !m_nextObject && (int)m_current.offset() == endPunctuationIndex && !inlineLogicalWidth(m_current.renderer(), false, true)) {
m_width.addUncommittedWidth(-renderText.hangablePunctuationEndWidth(endPunctuationIndex));
canHangPunctuationAtEnd = false;
}
if (!m_collapseWhiteSpace || !m_currentCharacterIsSpace)
m_lineInfo.setEmpty(false, &m_block, &m_width);
if (c == softHyphen && m_autoWrap && !hyphenWidth && style.hyphens() != Hyphens::None) {
hyphenWidth = measureHyphenWidth(renderText, font, &fallbackFonts);
m_width.addUncommittedWidth(hyphenWidth);
}
bool applyWordSpacing = false;
m_currentCharacterIsWS = m_currentCharacterIsSpace || (breakNBSP && c == noBreakSpace);
if (canBreakMidWord && !midWordBreak && (!m_currentCharacterIsSpace || m_atStart || style.whiteSpace() != WhiteSpace::PreWrap)) {
wrapW += charWidth;
bool midWordBreakIsBeforeSurrogatePair = U16_IS_LEAD(c) && U16_IS_TRAIL(renderText.characterAt(m_current.offset() + 1));
charWidth = textWidth(renderText, m_current.offset(), midWordBreakIsBeforeSurrogatePair ? 2 : 1, font, m_width.committedWidth() + wrapW, isFixedPitch, m_collapseWhiteSpace, fallbackFonts, textLayout);
midWordBreak = m_width.committedWidth() + wrapW + charWidth > m_width.availableWidth();
}
Optional<unsigned> nextBreakablePosition = m_current.nextBreakablePosition();
bool betweenWords = c == '\n' || (m_currWS != WhiteSpace::Pre && !m_atStart && isBreakable(m_renderTextInfo.lineBreakIterator, m_current.offset(), nextBreakablePosition, breakNBSP, canUseLineBreakShortcut, keepAllWords, breakAnywhere)
&& (style.hyphens() != Hyphens::None || (m_current.previousInSameNode() != softHyphen)));
m_current.setNextBreakablePosition(nextBreakablePosition);
if (canHangStopOrCommaAtLineEnd && renderText.isHangableStopOrComma(c) && m_width.fitsOnLine()) {
m_hangsAtEnd = false;
float inlineStartWidth = !m_appliedStartWidth ? inlineLogicalWidth(m_current.renderer(), true, false) : 0_lu;
float widthIncludingComma = computeAdditionalBetweenWordsWidth(renderText, textLayout, c, wordTrailingSpace, fallbackFonts, wordMeasurements, font, isFixedPitch, lastSpace, lastSpaceWordSpacing, wordSpacingForWordMeasurement, m_current.offset() + 1) + inlineStartWidth;
m_width.addUncommittedWidth(widthIncludingComma);
if (!m_width.fitsOnLine()) {
float widthWithoutStopOrComma = computeAdditionalBetweenWordsWidth(renderText, textLayout, lastCharacter, wordTrailingSpace, fallbackFonts, wordMeasurements, font, isFixedPitch, lastSpace, lastSpaceWordSpacing, wordSpacingForWordMeasurement, m_current.offset()) + inlineStartWidth;
m_width.addUncommittedWidth(widthWithoutStopOrComma - widthIncludingComma);
if (m_width.fitsOnLine())
m_hangsAtEnd = true;
} else
m_width.addUncommittedWidth(-widthIncludingComma);
}
if (betweenWords || midWordBreak) {
bool stoppedIgnoringSpaces = false;
if (m_ignoringSpaces) {
lastSpaceWordSpacing = 0;
if (!m_currentCharacterIsSpace) {
m_ignoringSpaces = false;
wordSpacingForWordMeasurement = 0;
lastSpace = m_current.offset(); m_lineWhitespaceCollapsingState.stopIgnoringSpaces(InlineIterator(0, m_current.renderer(), m_current.offset()));
stoppedIgnoringSpaces = true;
} else {
nextCharacter(c, lastCharacter, secondToLastCharacter);
continue;
}
}
float additionalTempWidth = computeAdditionalBetweenWordsWidth(renderText, textLayout, c, wordTrailingSpace, fallbackFonts, wordMeasurements, font, isFixedPitch, lastSpace, lastSpaceWordSpacing, wordSpacingForWordMeasurement, m_current.offset());
m_width.addUncommittedWidth(additionalTempWidth);
WordMeasurement& wordMeasurement = wordMeasurements.last();
if (m_collapseWhiteSpace && previousCharacterIsSpace && m_currentCharacterIsSpace && additionalTempWidth)
m_width.setTrailingWhitespaceWidth(additionalTempWidth);
if (!m_appliedStartWidth) {
float inlineStartWidth = inlineLogicalWidth(m_current.renderer(), true, false);
m_width.addUncommittedWidth(inlineStartWidth);
m_appliedStartWidth = true;
if (m_hangsAtEnd && inlineStartWidth)
m_hangsAtEnd = false;
}
applyWordSpacing = wordSpacing && m_currentCharacterIsSpace;
if (!m_width.hasCommitted() && m_autoWrap && !fitsOnLineOrHangsAtEnd())
m_width.fitBelowFloats(m_lineInfo.isFirstLine());
if (m_autoWrap || breakWords) {
bool lineWasTooWide = false;
if (fitsOnLineOrHangsAtEnd() && m_currentCharacterIsWS && m_currentStyle->breakOnlyAfterWhiteSpace() && (!midWordBreak || m_currWS == WhiteSpace::BreakSpaces)) {
float charWidth = textWidth(renderText, m_current.offset(), 1, font, m_width.currentWidth(), isFixedPitch, m_collapseWhiteSpace, wordMeasurement.fallbackFonts, textLayout) + (applyWordSpacing ? wordSpacing : 0);
if (!m_width.fitsOnLineIncludingExtraWidth(charWidth)) {
lineWasTooWide = true;
if (m_currWS == WhiteSpace::BreakSpaces)
trailingSpacesHang(m_lineBreak, renderObject, canBreakMidWord, previousCharacterIsSpace);
else {
m_lineBreak.moveTo(renderObject, m_current.offset(), m_current.nextBreakablePosition());
m_lineBreaker.skipTrailingWhitespace(m_lineBreak, m_lineInfo);
}
}
}
if ((lineWasTooWide || !m_width.fitsOnLine()) && !m_hangsAtEnd) {
if (canHyphenate && !m_width.fitsOnLine() && (m_nextObject || !renderText.containsOnlyHTMLWhitespace(m_current.offset(), renderText.text().length() - m_current.offset()) || isLineEmpty)) {
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 (m_lineBreaker.m_hyphenated) {
m_atEnd = true;
return false;
}
}
if (m_lineBreak.atTextParagraphSeparator()) {
if (!stoppedIgnoringSpaces && m_current.offset() > 0)
ensureCharacterGetsLineBox(m_lineWhitespaceCollapsingState, m_current);
m_lineBreak.increment();
m_lineInfo.setPreviousLineBrokeCleanly(true);
wordMeasurement.endOffset = m_lineBreak.offset();
}
if (!hyphenated && style.hyphens() != Hyphens::None) {
Optional<unsigned> lastBreakingPositon;
const RenderObject* rendererAtBreakingPosition = nullptr;
if (m_lineBreak.offset() || m_lineBreak.nextBreakablePosition()) {
lastBreakingPositon = m_lineBreak.offset();
rendererAtBreakingPosition = m_lineBreak.renderer();
} else if (m_current.nextBreakablePosition() && m_current.nextBreakablePosition().value() <= m_current.offset()) {
lastBreakingPositon = m_current.nextBreakablePosition().value();
rendererAtBreakingPosition = m_current.renderer();
}
if (lastBreakingPositon) {
Optional<UChar> characterBeforeBreakingPosition;
if (!lastBreakingPositon.value())
characterBeforeBreakingPosition = lastCharacterFromPreviousRenderText;
else if (is<RenderText>(rendererAtBreakingPosition)) {
const auto& textRenderer = downcast<RenderText>(*rendererAtBreakingPosition);
ASSERT(lastBreakingPositon.value() >= 1 && textRenderer.text().length() > (lastBreakingPositon.value() - 1));
characterBeforeBreakingPosition = textRenderer.characterAt(lastBreakingPositon.value() - 1);
}
if (characterBeforeBreakingPosition)
hyphenated = characterBeforeBreakingPosition.value() == softHyphen;
}
}
if (m_lineBreak.offset() && m_lineBreak.offset() != (unsigned)wordMeasurement.endOffset && !wordMeasurement.width) {
if (charWidth) {
wordMeasurement.endOffset = m_lineBreak.offset();
wordMeasurement.width = charWidth;
}
}
if (m_ignoringSpaces || !m_collapseWhiteSpace || !m_currentCharacterIsSpace || !previousCharacterIsSpace) {
m_atEnd = true;
return false;
}
} else {
if (!betweenWords || (midWordBreak && !m_autoWrap))
m_width.addUncommittedWidth(-additionalTempWidth);
if (hyphenWidth) {
m_width.addUncommittedWidth(-hyphenWidth);
hyphenWidth = 0;
}
}
}
if (c == '\n' && m_preservesNewline) {
if (!stoppedIgnoringSpaces && m_current.offset())
ensureCharacterGetsLineBox(m_lineWhitespaceCollapsingState, m_current);
commitLineBreakAtCurrentWidth(renderObject, m_current.offset(), m_current.nextBreakablePosition());
m_lineBreak.increment();
m_lineInfo.setPreviousLineBrokeCleanly(true);
return true;
}
if (m_autoWrap && betweenWords) {
commitLineBreakAtCurrentWidth(renderObject, m_current.offset(), m_current.nextBreakablePosition());
wrapWidthOffset = 0;
wrapW = wrapWidthOffset;
m_hasFormerOpportunity = !breakAnywhere;
breakWords = false;
canBreakMidWord = breakAll;
}
if (midWordBreak && !U16_IS_TRAIL(c) && !(U_GET_GC_MASK(c) & U_GC_M_MASK)) {
m_lineBreak.moveTo(renderObject, m_current.offset(), m_current.nextBreakablePosition());
midWordBreak &= canBreakMidWord;
}
if (betweenWords) {
lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
wordSpacingForWordMeasurement = (applyWordSpacing && wordMeasurement.width) ? wordSpacing : 0;
lastSpace = m_current.offset();
}
if (!m_ignoringSpaces && m_currentStyle->collapseWhiteSpace()) {
if (m_currentCharacterIsSpace && previousCharacterIsSpace) {
m_ignoringSpaces = true;
m_lineWhitespaceCollapsingState.startIgnoringSpaces(m_startOfIgnoredSpaces);
m_trailingObjects.updateWhitespaceCollapsingTransitionsForTrailingBoxes(m_lineWhitespaceCollapsingState, InlineIterator(), TrailingObjects::DoNotCollapseFirstSpace);
}
}
if (!renderText.canUseSimpleFontCodePath() && midWordBreak && m_width.fitsOnLine()) {
midWordBreak = false;
wrapW = wrapWidthOffset + additionalTempWidth;
}
isLineEmpty = m_lineInfo.isEmpty();
} else {
if (m_ignoringSpaces) {
m_ignoringSpaces = false;
lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
wordSpacingForWordMeasurement = (applyWordSpacing && wordMeasurements.last().width) ? wordSpacing : 0;
lastSpace = m_current.offset(); m_lineWhitespaceCollapsingState.stopIgnoringSpaces(InlineIterator(nullptr, m_current.renderer(), m_current.offset()));
}
if (m_hangsAtEnd && !renderText.isHangableStopOrComma(c))
m_hangsAtEnd = false;
}
if (isSVGText && m_current.offset()) {
if (downcast<RenderSVGInlineText>(renderText).characterStartsNewTextChunk(m_current.offset()))
ensureCharacterGetsLineBox(m_lineWhitespaceCollapsingState, m_current);
}
if (m_currentCharacterIsSpace && !previousCharacterIsSpace) {
m_startOfIgnoredSpaces.setRenderer(m_current.renderer());
m_startOfIgnoredSpaces.setOffset(m_current.offset());
if (m_nextObject && m_startOfIgnoredSpaces.offset() && m_nextObject->isBR() && (m_blockStyle.textAlign() == TextAlignMode::Right || m_blockStyle.textAlign() == TextAlignMode::WebKitRight)) {
m_startOfIgnoredSpaces.setOffset(m_startOfIgnoredSpaces.offset() - 1);
if (m_current.offset() == renderText.text().length() - 1)
m_lineWhitespaceCollapsingState.startIgnoringSpaces(m_startOfIgnoredSpaces);
}
}
if (!m_currentCharacterIsWS && previousCharacterIsWS) {
if (m_autoWrap && m_currentStyle->breakOnlyAfterWhiteSpace())
m_lineBreak.moveTo(renderObject, m_current.offset(), m_current.nextBreakablePosition());
}
if (m_collapseWhiteSpace && m_currentCharacterIsSpace && !m_ignoringSpaces)
m_trailingObjects.setTrailingWhitespace(downcast<RenderText>(m_current.renderer()));
else if (!m_currentStyle->collapseWhiteSpace() || !m_currentCharacterIsSpace)
m_trailingObjects.clear();
m_atStart = false;
nextCharacter(c, lastCharacter, secondToLastCharacter);
}
m_renderTextInfo.lineBreakIterator.setPriorContext(lastCharacter, secondToLastCharacter);
wordMeasurements.grow(wordMeasurements.size() + 1);
WordMeasurement& wordMeasurement = wordMeasurements.last();
wordMeasurement.renderer = &renderText;
float additionalTempWidth = m_ignoringSpaces ? 0 : textWidth(renderText, lastSpace, m_current.offset() - lastSpace, font, m_width.currentWidth(), isFixedPitch, m_collapseWhiteSpace, wordMeasurement.fallbackFonts, textLayout);
wordMeasurement.startOffset = lastSpace;
wordMeasurement.endOffset = m_current.offset();
wordMeasurement.width = m_ignoringSpaces ? 0 : additionalTempWidth + wordSpacingForWordMeasurement;
additionalTempWidth += lastSpaceWordSpacing;
float inlineLogicalTempWidth = inlineLogicalWidth(m_current.renderer(), !m_appliedStartWidth, m_includeEndWidth);
m_width.addUncommittedWidth(additionalTempWidth + inlineLogicalTempWidth);
if (m_hangsAtEnd && inlineLogicalTempWidth)
m_hangsAtEnd = false;
if (wordMeasurement.fallbackFonts.isEmpty() && !fallbackFonts.isEmpty())
wordMeasurement.fallbackFonts.swap(fallbackFonts);
fallbackFonts.clear();
if (m_collapseWhiteSpace && m_currentCharacterIsSpace && additionalTempWidth)
m_width.setTrailingWhitespaceWidth(additionalTempWidth, inlineLogicalTempWidth);
m_includeEndWidth = false;
if (!fitsOnLineOrHangsAtEnd()) {
if (canHyphenate && (m_nextObject || isLineEmpty))
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 (!hyphenated && m_lineBreak.previousInSameNode() == softHyphen && style.hyphens() != Hyphens::None) {
hyphenated = true;
m_atEnd = true;
}
}
return false;
}
inline bool textBeginsWithBreakablePosition(RenderText& nextText)
{
UChar c = nextText.characterAt(0);
return c == ' ' || c == '\t' || (c == '\n' && !nextText.preservesNewline());
}
inline void BreakingContext::trailingSpacesHang(InlineIterator& lineBreak, RenderObject& renderObject, bool canBreakMidWord, bool previousCharacterIsSpace)
{
ASSERT(m_currWS == WhiteSpace::BreakSpaces);
if (m_hasFormerOpportunity && !previousCharacterIsSpace)
return;
lineBreak.moveTo(renderObject, m_current.offset(), m_current.nextBreakablePosition());
if (!previousCharacterIsSpace && !canBreakMidWord)
lineBreak.increment();
}
inline bool BreakingContext::canBreakAtThisPosition()
{
if (m_width.committedWidth() && !m_width.fitsOnLine(m_currentCharacterIsSpace) && m_currWS == WhiteSpace::NoWrap)
return true;
if (is<RenderInline>(*m_current.renderer()) && isEmptyInline(downcast<RenderInline>(*m_current.renderer())))
return false;
if (!m_current.renderer()->isReplaced() && is<RenderInline>(m_nextObject) && isEmptyInline(downcast<RenderInline>(*m_nextObject)))
return false;
if (m_autoWrap && m_currentCharacterIsSpace)
return true;
if (m_nextObject && m_nextObject->isLineBreakOpportunity())
return m_autoWrap;
bool nextIsAutoWrappingText = is<RenderText>(m_nextObject) && (m_autoWrap || m_nextObject->style().autoWrap());
if (!nextIsAutoWrappingText)
return m_autoWrap;
RenderText& nextRenderText = downcast<RenderText>(*m_nextObject);
bool currentIsTextOrEmptyInline = is<RenderText>(*m_current.renderer()) || (is<RenderInline>(*m_current.renderer()) && isEmptyInline(downcast<RenderInline>(*m_current.renderer())));
if (!currentIsTextOrEmptyInline)
return m_autoWrap && !m_current.renderer()->isRubyRun();
bool canBreakHere = !m_currentCharacterIsSpace && textBeginsWithBreakablePosition(nextRenderText);
if (!m_width.fitsOnLine() && !m_width.hasCommitted())
m_width.fitBelowFloats(m_lineInfo.isFirstLine());
bool canPlaceOnLine = m_width.fitsOnLine() || !m_autoWrapWasEverTrueOnLine;
if (canPlaceOnLine && canBreakHere)
commitLineBreakAtCurrentWidth(nextRenderText);
return canBreakHere;
}
inline void BreakingContext::commitAndUpdateLineBreakIfNeeded()
{
bool checkForBreak = canBreakAtThisPosition();
if (checkForBreak && !m_width.fitsOnLine(m_ignoringSpaces) && !m_hangsAtEnd) {
if (m_currentCharacterIsSpace && !m_ignoringSpaces && m_currentStyle->collapseWhiteSpace())
m_trailingObjects.clear();
if (m_width.committedWidth()) {
m_atEnd = true;
return;
}
if (!m_hangsAtEnd)
m_width.fitBelowFloats(m_lineInfo.isFirstLine());
if (!m_width.fitsOnLine(m_ignoringSpaces)) {
m_atEnd = true;
return;
}
} else if (m_blockStyle.autoWrap() && !m_width.fitsOnLine() && !m_width.hasCommitted() && !m_hangsAtEnd) {
m_width.fitBelowFloats(m_lineInfo.isFirstLine());
}
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_nextObject)
commitLineBreakAtCurrentWidth(*m_nextObject);
else
commitLineBreakClear();
}
}
}
inline TrailingObjects::CollapseFirstSpaceOrNot checkWhitespaceCollapsingTransitions(LineWhitespaceCollapsingState& lineWhitespaceCollapsingState, const InlineIterator& lBreak)
{
if (lBreak.renderer() && lineWhitespaceCollapsingState.numTransitions() && !(lineWhitespaceCollapsingState.numTransitions() % 2)) {
const InlineIterator* transitions = lineWhitespaceCollapsingState.transitions().data();
const InlineIterator& endpoint = transitions[lineWhitespaceCollapsingState.numTransitions() - 2];
const InlineIterator& startpoint = transitions[lineWhitespaceCollapsingState.numTransitions() - 1];
InlineIterator currpoint = endpoint;
while (!currpoint.atEnd() && currpoint != startpoint && currpoint != lBreak)
currpoint.increment();
if (currpoint == lBreak) {
lineWhitespaceCollapsingState.decrementNumTransitions();
if (endpoint.renderer()->style().collapseWhiteSpace() && endpoint.renderer()->isText()) {
lineWhitespaceCollapsingState.decrementTransitionAt(lineWhitespaceCollapsingState.numTransitions() - 1);
return TrailingObjects::DoNotCollapseFirstSpace;
}
}
}
return TrailingObjects::CollapseFirstSpace;
}
inline InlineIterator BreakingContext::handleEndOfLine()
{
if (m_lineBreak == m_resolver.position()) {
if (!m_lineBreak.renderer() || !m_lineBreak.renderer()->isBR()) {
if (m_blockStyle.whiteSpace() == WhiteSpace::Pre && !m_current.offset()) {
if (m_lastObject)
commitLineBreakAtCurrentWidth(*m_lastObject, m_lastObject->isText() ? m_lastObject->length() : 0);
else
commitLineBreakClear();
} else if (m_lineBreak.renderer()) {
commitLineBreakAtCurrentWidth(*m_current.renderer(), m_current.offset());
}
}
if (m_lineBreak == m_resolver.position())
m_lineBreak.increment();
} else if (!m_current.offset() && !m_width.committedWidth() && m_width.uncommittedWidth() && !m_hadUncommittedWidthBeforeCurrent) {
m_lineBreak.increment();
}
TrailingObjects::CollapseFirstSpaceOrNot collapsed = checkWhitespaceCollapsingTransitions(m_lineWhitespaceCollapsingState, m_lineBreak);
m_trailingObjects.updateWhitespaceCollapsingTransitionsForTrailingBoxes(m_lineWhitespaceCollapsingState, m_lineBreak, collapsed);
if (m_lineBreak.offset()) {
m_lineBreak.setOffset(m_lineBreak.offset() - 1);
m_lineBreak.increment();
}
return m_lineBreak;
}
}