Title: [253815] trunk/Source/WebCore
Revision
253815
Author
[email protected]
Date
2019-12-20 05:44:01 -0800 (Fri, 20 Dec 2019)

Log Message

[LFC][IFC] Refactor LineLayoutContext class
https://bugs.webkit.org/show_bug.cgi?id=205494
<rdar://problem/58109493>

Reviewed by Antti Koivisto.

This patch is in preparation for being able to pre-scan the inline content for soft wrap opportunities.

Currently processing the inline content means pushing the inline items to an uncommitted queue until after
we find a soft wrap opportunity and then we ask the LineBreaker whether this uncommitted, "continuous content" can be placed
on the current line.
while (has unprocessed inline item) {
    get next inline item
    if (inline item is at a soft wrap opportunity)
        sumbit uncommitted queue to line breaking
    else
        add to uncommitted queue
}
This patch omits the uncommitted queue by collecting the inline items first. This removes some code complexity and it also
helps to be able to pre-scan the content for soft wrap opportunities.
while (has unprocessed inline item) {
    get next continuous content
    submit content to line breaking
}

* layout/inlineformatting/InlineLineBreaker.cpp:
(WebCore::Layout::endsWithSoftWrapOpportunity):
(WebCore::Layout::LineBreaker::ContinousContent::ContinousContent):
* layout/inlineformatting/InlineLineBreaker.h:
(WebCore::Layout::LineBreaker::Run::Run):
* layout/inlineformatting/LineLayoutContext.cpp:
(WebCore::Layout::ContinousContent::hasIntrusiveFloats const):
(WebCore::Layout::ContinousContent::runs const):
(WebCore::Layout::ContinousContent::floats const):
(WebCore::Layout::ContinousContent::endsWithLineBreak const):
(WebCore::Layout::ContinousContent::setEndsWithLineBreak):
(WebCore::Layout::ContinousContent::append):
(WebCore::Layout::LineLayoutContext::layoutLine):
(WebCore::Layout::LineLayoutContext::close):
(WebCore::Layout::LineLayoutContext::nextContinousContentForLine):
(WebCore::Layout::LineLayoutContext::addFloatItems):
(WebCore::Layout::LineLayoutContext::placeInlineContentOnCurrentLine):
(WebCore::Layout::LineLayoutContext::commitContent):
(WebCore::Layout::LineLayoutContext::commitPendingContent): Deleted.
(WebCore::Layout::LineLayoutContext::placeInlineItem): Deleted.
(WebCore::Layout::LineLayoutContext::processUncommittedContent): Deleted.
(WebCore::Layout::LineLayoutContext::UncommittedContent::append): Deleted.
(WebCore::Layout::LineLayoutContext::UncommittedContent::reset): Deleted.
(WebCore::Layout::LineLayoutContext::UncommittedContent::shrink): Deleted.
* layout/inlineformatting/LineLayoutContext.h:
(WebCore::Layout::LineLayoutContext::formattingContext const):
(WebCore::Layout::LineLayoutContext::root const):
(WebCore::Layout::LineLayoutContext::UncommittedContent::width const): Deleted.
(WebCore::Layout::LineLayoutContext::UncommittedContent::size): Deleted.
(WebCore::Layout::LineLayoutContext::UncommittedContent::isEmpty): Deleted.
(WebCore::Layout::LineLayoutContext::UncommittedContent::runs const): Deleted.

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (253814 => 253815)


--- trunk/Source/WebCore/ChangeLog	2019-12-20 13:35:19 UTC (rev 253814)
+++ trunk/Source/WebCore/ChangeLog	2019-12-20 13:44:01 UTC (rev 253815)
@@ -1,3 +1,62 @@
+2019-12-20  Zalan Bujtas  <[email protected]>
+
+        [LFC][IFC] Refactor LineLayoutContext class
+        https://bugs.webkit.org/show_bug.cgi?id=205494
+        <rdar://problem/58109493>
+
+        Reviewed by Antti Koivisto.
+
+        This patch is in preparation for being able to pre-scan the inline content for soft wrap opportunities.
+
+        Currently processing the inline content means pushing the inline items to an uncommitted queue until after
+        we find a soft wrap opportunity and then we ask the LineBreaker whether this uncommitted, "continuous content" can be placed
+        on the current line.
+        while (has unprocessed inline item) {
+            get next inline item
+            if (inline item is at a soft wrap opportunity)
+                sumbit uncommitted queue to line breaking
+            else
+                add to uncommitted queue
+        }
+        This patch omits the uncommitted queue by collecting the inline items first. This removes some code complexity and it also
+        helps to be able to pre-scan the content for soft wrap opportunities.
+        while (has unprocessed inline item) {
+            get next continuous content
+            submit content to line breaking
+        }
+
+        * layout/inlineformatting/InlineLineBreaker.cpp:
+        (WebCore::Layout::endsWithSoftWrapOpportunity):
+        (WebCore::Layout::LineBreaker::ContinousContent::ContinousContent):
+        * layout/inlineformatting/InlineLineBreaker.h:
+        (WebCore::Layout::LineBreaker::Run::Run):
+        * layout/inlineformatting/LineLayoutContext.cpp:
+        (WebCore::Layout::ContinousContent::hasIntrusiveFloats const):
+        (WebCore::Layout::ContinousContent::runs const):
+        (WebCore::Layout::ContinousContent::floats const):
+        (WebCore::Layout::ContinousContent::endsWithLineBreak const):
+        (WebCore::Layout::ContinousContent::setEndsWithLineBreak):
+        (WebCore::Layout::ContinousContent::append):
+        (WebCore::Layout::LineLayoutContext::layoutLine):
+        (WebCore::Layout::LineLayoutContext::close):
+        (WebCore::Layout::LineLayoutContext::nextContinousContentForLine):
+        (WebCore::Layout::LineLayoutContext::addFloatItems):
+        (WebCore::Layout::LineLayoutContext::placeInlineContentOnCurrentLine):
+        (WebCore::Layout::LineLayoutContext::commitContent):
+        (WebCore::Layout::LineLayoutContext::commitPendingContent): Deleted.
+        (WebCore::Layout::LineLayoutContext::placeInlineItem): Deleted.
+        (WebCore::Layout::LineLayoutContext::processUncommittedContent): Deleted.
+        (WebCore::Layout::LineLayoutContext::UncommittedContent::append): Deleted.
+        (WebCore::Layout::LineLayoutContext::UncommittedContent::reset): Deleted.
+        (WebCore::Layout::LineLayoutContext::UncommittedContent::shrink): Deleted.
+        * layout/inlineformatting/LineLayoutContext.h:
+        (WebCore::Layout::LineLayoutContext::formattingContext const):
+        (WebCore::Layout::LineLayoutContext::root const):
+        (WebCore::Layout::LineLayoutContext::UncommittedContent::width const): Deleted.
+        (WebCore::Layout::LineLayoutContext::UncommittedContent::size): Deleted.
+        (WebCore::Layout::LineLayoutContext::UncommittedContent::isEmpty): Deleted.
+        (WebCore::Layout::LineLayoutContext::UncommittedContent::runs const): Deleted.
+
 2019-12-20  Rob Buis  <[email protected]>
 
         Fetch: handle emtpy Location value

Modified: trunk/Source/WebCore/layout/inlineformatting/InlineLineBreaker.cpp (253814 => 253815)


--- trunk/Source/WebCore/layout/inlineformatting/InlineLineBreaker.cpp	2019-12-20 13:35:19 UTC (rev 253814)
+++ trunk/Source/WebCore/layout/inlineformatting/InlineLineBreaker.cpp	2019-12-20 13:44:01 UTC (rev 253815)
@@ -54,7 +54,7 @@
     return isTextContentWrappingAllowed(run.inlineItem.style());
 }
 
-static inline bool shouldKeepEndOfLineWhitespace(const LineBreaker::Content& candidateRuns)
+static inline bool shouldKeepEndOfLineWhitespace(const LineBreaker::ContinousContent& candidateRuns)
 {
     // Grab the style and check for white-space property to decided whether we should let this whitespace content overflow the current line.
     // Note that the "keep" in the context means we let the whitespace content sit on the current line.
@@ -64,7 +64,7 @@
     return whitespace == WhiteSpace::Normal || whitespace == WhiteSpace::NoWrap || whitespace == WhiteSpace::PreWrap || whitespace == WhiteSpace::PreLine;
 }
 
-LineBreaker::BreakingContext LineBreaker::breakingContextForInlineContent(const Content& candidateRuns, const LineStatus& lineStatus)
+LineBreaker::BreakingContext LineBreaker::breakingContextForInlineContent(const ContinousContent& candidateRuns, const LineStatus& lineStatus)
 {
     ASSERT(!candidateRuns.isEmpty());
     if (candidateRuns.width() <= lineStatus.availableWidth)
@@ -255,7 +255,6 @@
 
 static bool endsWithSoftWrapOpportunity(const InlineTextItem& previousTextItem, const InlineTextItem& nextInlineTextItem)
 {
-    ASSERT(!previousTextItem.isWhitespace());
     ASSERT(!nextInlineTextItem.isWhitespace());
     // When both these non-whitespace runs belong to the same layout box, it's guaranteed that
     // they are split at a soft breaking opportunity. See InlineTextItem::moveToNextBreakablePosition.
@@ -386,49 +385,46 @@
     return true;
 }
 
-void LineBreaker::Content::append(const InlineItem& inlineItem, InlineLayoutUnit logicalWidth)
+LineBreaker::ContinousContent::ContinousContent(const RunList& runs)
+    : m_runs(runs)
 {
-    ASSERT(!inlineItem.isFloat());
-    m_continousRuns.append({ inlineItem, logicalWidth });
-    m_width += logicalWidth;
     // Figure out the trailing collapsible state.
-    if (inlineItem.isBox() || inlineItem.isLineBreak())
-        m_trailingCollapsibleContent.reset();
-    else if (inlineItem.isText()) {
-        auto& inlineTextItem = downcast<InlineTextItem>(inlineItem);
-        auto isFullyCollapsible = [&] {
-            return inlineTextItem.isWhitespace() && !TextUtil::shouldPreserveTrailingWhitespace(inlineTextItem.style());
-        };
-        if (isFullyCollapsible()) {
-            m_trailingCollapsibleContent.width += logicalWidth;
-            m_trailingCollapsibleContent.isFullyCollapsible = true;
-        } else if (auto collapsibleWidth = inlineTextItem.style().letterSpacing()) {
-            m_trailingCollapsibleContent.width = collapsibleWidth;
-            m_trailingCollapsibleContent.isFullyCollapsible = false;
-        } else
-            m_trailingCollapsibleContent.reset();
+    for (auto& run : WTF::makeReversedRange(m_runs)) {
+        auto& inlineItem = run.inlineItem;
+        if (inlineItem.isBox() || inlineItem.isLineBreak()) {
+            // We did reach a non-collapsible content. We have all the trailing whitespace now.
+            break;
+        }
+        if (inlineItem.isText()) {
+            auto& inlineTextItem = downcast<InlineTextItem>(inlineItem);
+            auto isFullyCollapsible = [&] {
+                return inlineTextItem.isWhitespace() && !TextUtil::shouldPreserveTrailingWhitespace(inlineTextItem.style());
+            };
+            if (isFullyCollapsible()) {
+                m_trailingCollapsibleContent.width += run.logicalWidth;
+                m_trailingCollapsibleContent.isFullyCollapsible = true;
+                // Let's see if we've got more trailing whitespace content.
+                continue;
+            }
+            if (auto collapsibleWidth = inlineTextItem.style().letterSpacing()) {
+                m_trailingCollapsibleContent.width += collapsibleWidth;
+                m_trailingCollapsibleContent.isFullyCollapsible = false;
+            }
+            // End of whitspace content.
+            break;
+        }
     }
+    // The trailing whitespace loop above is mostly about inspecting the last entry, so while it
+    // looks like we are looping through the m_runs twice, it's really just one full loop in addition to checking the last run.
+    for (auto& run : m_runs)
+        m_width += run.logicalWidth;
 }
 
-void LineBreaker::Content::reset()
+bool LineBreaker::ContinousContent::hasTextContentOnly() const
 {
-    m_continousRuns.clear();
-    m_trailingCollapsibleContent.reset();
-    m_width = 0_lu;
-}
-
-void LineBreaker::Content::shrink(unsigned newSize)
-{
-    for (auto i = m_continousRuns.size(); i--;)
-        m_width -= m_continousRuns[i].logicalWidth;
-    m_continousRuns.shrink(newSize);
-}
-
-bool LineBreaker::Content::hasTextContentOnly() const
-{
     // <span>text</span> is considered a text run even with the [container start][container end] inline items.
     // Due to commit boundary rules, we just need to check the first non-typeless inline item (can't have both [img] and [text])
-    for (auto& run : m_continousRuns) {
+    for (auto& run : m_runs) {
         auto& inlineItem = run.inlineItem;
         if (inlineItem.isContainerStart() || inlineItem.isContainerEnd())
             continue;
@@ -437,12 +433,12 @@
     return false;
 }
 
-bool LineBreaker::Content::isVisuallyEmptyWhitespaceContentOnly() const
+bool LineBreaker::ContinousContent::isVisuallyEmptyWhitespaceContentOnly() const
 {
     // [<span></span> ] [<span> </span>] [ <span style="padding: 0px;"></span>] are all considered visually empty whitespace content.
     // [<span style="border: 1px solid red"></span> ] while this is whitespace content only, it is not considered visually empty.
     // Due to commit boundary rules, we just need to check the first non-typeless inline item (can't have both [img] and [text])
-    for (auto& run : m_continousRuns) {
+    for (auto& run : m_runs) {
         auto& inlineItem = run.inlineItem;
         // FIXME: check for padding border etc.
         if (inlineItem.isContainerStart() || inlineItem.isContainerEnd())
@@ -452,19 +448,19 @@
     return false;
 }
 
-Optional<unsigned> LineBreaker::Content::firstTextRunIndex() const
+Optional<unsigned> LineBreaker::ContinousContent::firstTextRunIndex() const
 {
-    for (size_t index = 0; index < m_continousRuns.size(); ++index) {
-        if (m_continousRuns[index].inlineItem.isText())
+    for (size_t index = 0; index < m_runs.size(); ++index) {
+        if (m_runs[index].inlineItem.isText())
             return index;
     }
     return { };
 }
 
-bool LineBreaker::Content::hasNonContentRunsOnly() const
+bool LineBreaker::ContinousContent::hasNonContentRunsOnly() const
 {
     // <span></span> <- non content runs.
-    for (auto& run : m_continousRuns) {
+    for (auto& run : m_runs) {
         auto& inlineItem = run.inlineItem;
         if (inlineItem.isContainerStart() || inlineItem.isContainerEnd())
             continue;
@@ -473,7 +469,7 @@
     return true;
 }
 
-void LineBreaker::Content::TrailingCollapsibleContent::reset()
+void LineBreaker::ContinousContent::TrailingCollapsibleContent::reset()
 {
     isFullyCollapsible = false;
     width = 0_lu;

Modified: trunk/Source/WebCore/layout/inlineformatting/InlineLineBreaker.h (253814 => 253815)


--- trunk/Source/WebCore/layout/inlineformatting/InlineLineBreaker.h	2019-12-20 13:35:19 UTC (rev 253814)
+++ trunk/Source/WebCore/layout/inlineformatting/InlineLineBreaker.h	2019-12-20 13:44:01 UTC (rev 253815)
@@ -51,12 +51,16 @@
         ContentWrappingRule contentWrappingRule;
         struct PartialTrailingContent {
             unsigned triailingRunIndex { 0 };
-            Optional<PartialRun> partialRun; // Empty partial run means the trailing run is a complete run.
+            Optional<PartialRun> partialRun; // nullopt partial run means the trailing run is a complete run.
         };
         Optional<PartialTrailingContent> partialTrailingContent;
     };
 
     struct Run {
+        Run(const InlineItem&, InlineLayoutUnit);
+        Run(const Run&);
+        Run& operator=(const Run&);
+
         const InlineItem& inlineItem;
         InlineLayoutUnit logicalWidth { 0 };
     };
@@ -72,18 +76,15 @@
     // [content]
     // [container start][span1][container end][between][container start][span2][container end]
     // see https://drafts.csswg.org/css-text-3/#line-break-details
-    struct Content {
-        void append(const InlineItem&, InlineLayoutUnit logicalWidth);
-        void reset();
-        void shrink(unsigned newSize);
+    struct ContinousContent {
+        ContinousContent(const RunList&);
 
-        RunList& runs() { return m_continousRuns; }
-        const RunList& runs() const { return m_continousRuns; }
-        bool isEmpty() const { return m_continousRuns.isEmpty(); }
+        const RunList& runs() const { return m_runs; }
+        bool isEmpty() const { return m_runs.isEmpty(); }
         bool hasTextContentOnly() const;
         bool isVisuallyEmptyWhitespaceContentOnly() const;
         bool hasNonContentRunsOnly() const;
-        unsigned size() const { return m_continousRuns.size(); }
+        size_t size() const { return m_runs.size(); }
         InlineLayoutUnit width() const { return m_width; }
         InlineLayoutUnit nonCollapsibleWidth() const { return m_width - m_trailingCollapsibleContent.width; }
 
@@ -93,7 +94,7 @@
         Optional<unsigned> firstTextRunIndex() const;
 
     private:
-        RunList m_continousRuns;
+        RunList m_runs;
         struct TrailingCollapsibleContent {
             void reset();
 
@@ -110,7 +111,7 @@
         bool lineHasFullyCollapsibleTrailingRun { false };
         bool lineIsEmpty { true };
     };
-    BreakingContext breakingContextForInlineContent(const Content& candidateRuns, const LineStatus&);
+    BreakingContext breakingContextForInlineContent(const ContinousContent& candidateRuns, const LineStatus&);
     bool shouldWrapFloatBox(InlineLayoutUnit floatLogicalWidth, InlineLayoutUnit availableWidth, bool lineIsEmpty);
 
     void setHyphenationDisabled() { n_hyphenationIsDisabled = true; }
@@ -134,6 +135,18 @@
     bool n_hyphenationIsDisabled { false };
 };
 
+inline LineBreaker::Run::Run(const InlineItem& inlineItem, InlineLayoutUnit logicalWidth)
+    : inlineItem(inlineItem)
+    , logicalWidth(logicalWidth)
+{
 }
+
+inline LineBreaker::Run::Run(const Run& other)
+    : inlineItem(other.inlineItem)
+    , logicalWidth(other.logicalWidth)
+{
 }
+
+}
+}
 #endif

Modified: trunk/Source/WebCore/layout/inlineformatting/LineLayoutContext.cpp (253814 => 253815)


--- trunk/Source/WebCore/layout/inlineformatting/LineLayoutContext.cpp	2019-12-20 13:35:19 UTC (rev 253814)
+++ trunk/Source/WebCore/layout/inlineformatting/LineLayoutContext.cpp	2019-12-20 13:44:01 UTC (rev 253815)
@@ -28,7 +28,6 @@
 
 #if ENABLE(LAYOUT_FORMATTING_CONTEXT)
 
-#include "InlineLineBreaker.h"
 #include "LayoutBox.h"
 #include "TextUtil.h"
 
@@ -35,6 +34,29 @@
 namespace WebCore {
 namespace Layout {
 
+struct ContinousContent {
+    void append(const InlineItem&, Optional<InlineLayoutUnit> logicalWidth);
+
+    bool hasIntrusiveFloats() const { return !m_floats.isEmpty(); }
+    const LineBreaker::RunList& runs() const { return m_runs; }
+    const LineLayoutContext::FloatList& floats() const { return m_floats; }
+
+    bool endsWithLineBreak() const { return m_endsWithLineBreak; }
+    void setEndsWithLineBreak() { m_endsWithLineBreak = true; }
+
+private:
+    LineBreaker::RunList m_runs;
+    LineLayoutContext::FloatList m_floats;
+    bool m_endsWithLineBreak { false };
+};
+
+void ContinousContent::append(const InlineItem& inlineItem, Optional<InlineLayoutUnit> logicalWidth)
+{
+    if (inlineItem.isFloat())
+        return m_floats.append(makeWeakPtr(inlineItem));
+    m_runs.append({ inlineItem, *logicalWidth });
+}
+
 static InlineLayoutUnit inlineItemWidth(const FormattingContext& formattingContext, const InlineItem& inlineItem, InlineLayoutUnit contentLogicalLeft)
 {
     if (inlineItem.isLineBreak())
@@ -76,62 +98,73 @@
 
 LineLayoutContext::LineContent LineLayoutContext::layoutLine(LineBuilder& line, unsigned leadingInlineItemIndex, Optional<unsigned> partialLeadingContentLength)
 {
-    auto initialize = [&] {
-        m_committedInlineItemCount = 0;
-        m_uncommittedContent.reset();
+    auto reset = [&] {
+        ASSERT(m_floats.isEmpty());
+        m_partialTrailingTextItem = { };
         m_partialLeadingTextItem = { };
-        m_partialTrailingTextItem = { };
-        m_partialContent = { };
     };
-    initialize();
-    // Iterate through the inline content and place the inline boxes on the current line.
-    // Start with the partial leading text from the previous line.
-    auto firstNonPartialInlineItemIndex = leadingInlineItemIndex;
-    if (partialLeadingContentLength) {
-        // Handle partial inline item (split text from the previous line).
-        auto& leadingTextItem = m_inlineItems[leadingInlineItemIndex];
-        RELEASE_ASSERT(leadingTextItem->isText());
-        // Construct a partial leading inline item.
-        ASSERT(!m_partialLeadingTextItem);
-        m_partialLeadingTextItem = downcast<InlineTextItem>(*leadingTextItem).right(*partialLeadingContentLength);
-        if (placeInlineItem(line, *m_partialLeadingTextItem) == IsEndOfLine::Yes)
-            return close(line, leadingInlineItemIndex);
-        ++firstNonPartialInlineItemIndex;
+    reset();
+    auto currentItemIndex = leadingInlineItemIndex;
+    unsigned committedInlineItemCount = 0;
+    while (currentItemIndex < m_inlineItems.size()) {
+        // 1. Collect the set of runs that we can commit to the line as one entity e.g. <span>text_and_span_start_span_end</span>.
+        // 2. Apply floats and shrink the available horizontal space e.g. <span>intru_<div style="float: left"></div>sive_float</span>.
+        // 3. Check if the content fits the line and commit the content accordingly (full, partial or not commit at all).
+        // 4. Return if we are at the end of the line either by not being able to fit more content or because of an explicit line break.
+        auto candidateContent = nextContinousContentForLine(currentItemIndex, partialLeadingContentLength, line.lineBox().logicalWidth());
+        if (candidateContent.hasIntrusiveFloats()) {
+            // Add floats first because they shrink the available horizontal space for the rest of the content.
+            auto floatContent = addFloatItems(line, candidateContent.floats());
+            committedInlineItemCount += floatContent.count;
+            if (floatContent.isEndOfLine == IsEndOfLine::Yes) {
+                // Floats take up all the horizontal space.
+                return close(line, leadingInlineItemIndex, committedInlineItemCount, { });
+            }
+        }
+        ASSERT(!candidateContent.runs().isEmpty() || candidateContent.endsWithLineBreak() || candidateContent.hasIntrusiveFloats());
+        if (!candidateContent.runs().isEmpty()) {
+            // Now check if we can put this content on the current line.
+            auto committedContent = placeInlineContentOnCurrentLine(line, candidateContent.runs());
+            committedInlineItemCount += committedContent.count;
+            if (committedContent.isEndOfLine == IsEndOfLine::Yes) {
+                // We can't place any more items on the current line.
+                return close(line, leadingInlineItemIndex, committedInlineItemCount, committedContent.partialContent);
+            }
+        }
+        // We could fit more content but sadly this line ends with an explicit line break.
+        if (candidateContent.endsWithLineBreak()) {
+            // Make sure the trailing line break ends up on the line too.
+            auto lineBreakIndex = leadingInlineItemIndex + committedInlineItemCount;
+            ASSERT(m_inlineItems[lineBreakIndex]->isLineBreak());
+            line.append(*m_inlineItems[lineBreakIndex], 0);
+            return close(line, leadingInlineItemIndex, ++committedInlineItemCount, { });
+        }
+        currentItemIndex = leadingInlineItemIndex + committedInlineItemCount;
+        partialLeadingContentLength = { };
     }
-
-    for (auto inlineItemIndex = firstNonPartialInlineItemIndex; inlineItemIndex < m_inlineItems.size(); ++inlineItemIndex) {
-        // FIXME: We should not need to re-measure the dropped, uncommitted content when re-using them on the next line.
-        if (placeInlineItem(line, *m_inlineItems[inlineItemIndex]) == IsEndOfLine::Yes)
-            return close(line, leadingInlineItemIndex);
-    }
-    // Check the uncommitted content whether they fit now that we know we are at a commit boundary.
-    if (!m_uncommittedContent.isEmpty())
-        processUncommittedContent(line);
-    return close(line, leadingInlineItemIndex);
+    // Looks like we've run out of runs.
+    return close(line, leadingInlineItemIndex, committedInlineItemCount, { });
 }
 
-void LineLayoutContext::commitPendingContent(LineBuilder& line)
+LineLayoutContext::LineContent LineLayoutContext::close(LineBuilder& line, unsigned leadingInlineItemIndex, unsigned committedInlineItemCount, Optional<LineContent::PartialContent> partialContent)
 {
-    if (m_uncommittedContent.isEmpty())
-        return;
-    m_committedInlineItemCount += m_uncommittedContent.size();
-    for (auto& uncommittedRun : m_uncommittedContent.runs())
-        line.append(uncommittedRun.inlineItem, uncommittedRun.logicalWidth);
-    m_uncommittedContent.reset();
-}
-
-LineLayoutContext::LineContent LineLayoutContext::close(LineBuilder& line, unsigned leadingInlineItemIndex)
-{
-    ASSERT(m_committedInlineItemCount || line.hasIntrusiveFloat());
-    m_uncommittedContent.reset();
-    if (!m_committedInlineItemCount)
+    ASSERT(committedInlineItemCount || line.hasIntrusiveFloat());
+    if (!committedInlineItemCount)
         return LineContent { { }, { }, WTFMove(m_floats), line.close(), line.lineBox() };
 
-    auto trailingInlineItemIndex = leadingInlineItemIndex + m_committedInlineItemCount - 1;
+    // Adjust hyphenated line count.
+    if (partialContent && partialContent->trailingContentNeedsHyphen)
+        ++m_successiveHyphenatedLineCount;
+    else
+        m_successiveHyphenatedLineCount = 0;
+
+    auto trailingInlineItemIndex = leadingInlineItemIndex + committedInlineItemCount - 1;
     auto isLastLineWithInlineContent = [&] {
-        if (m_partialContent)
+        if (trailingInlineItemIndex == m_inlineItems.size() - 1)
+            return LineBuilder::IsLastLineWithInlineContent::Yes;
+        if (partialContent)
             return LineBuilder::IsLastLineWithInlineContent::No;
-        // Skip floats backwards to see if this is going to be the last line with inline content.
+        // Omit floats to see if this is the last line with inline content.
         for (auto i = m_inlineItems.size(); i--;) {
             if (!m_inlineItems[i]->isFloat())
                 return i == trailingInlineItemIndex ? LineBuilder::IsLastLineWithInlineContent::Yes : LineBuilder::IsLastLineWithInlineContent::No;
@@ -139,113 +172,143 @@
         // There has to be at least one non-float item.
         ASSERT_NOT_REACHED();
         return LineBuilder::IsLastLineWithInlineContent::No;
-    };
+    }();
+    return LineContent { trailingInlineItemIndex, partialContent, WTFMove(m_floats), line.close(isLastLineWithInlineContent), line.lineBox() };
+}
 
-    return LineContent { trailingInlineItemIndex, m_partialContent, WTFMove(m_floats), line.close(isLastLineWithInlineContent()), line.lineBox() };
+ContinousContent LineLayoutContext::nextContinousContentForLine(unsigned inlineItemIndex, Optional<unsigned> partialLeadingContentLength, InlineLayoutUnit currentLogicalRight)
+{
+    auto candidateContent = ContinousContent { };
+    // 1. Simply add any overflow content from the previous line to the candidate content. It's always a text content.
+    // 2. Find the next soft wrap position or explicit line break.
+    // 3. Collect floats between the inline content.
+    if (partialLeadingContentLength) {
+        // Handle leading partial content first (split text from the previous line).
+        // Construct a partial leading inline item.
+        m_partialLeadingTextItem = downcast<InlineTextItem>(*m_inlineItems[inlineItemIndex]).right(*partialLeadingContentLength);
+        auto itemWidth = inlineItemWidth(formattingContext(), *m_partialLeadingTextItem, currentLogicalRight);
+        candidateContent.append(*m_partialLeadingTextItem, itemWidth);
+        currentLogicalRight += itemWidth;
+        ++inlineItemIndex;
+    }
+
+    while (inlineItemIndex < m_inlineItems.size()) {
+        auto& inlineItem = *m_inlineItems[inlineItemIndex];
+        if (inlineItem.isFloat()) {
+            // Floats are not part of the line context.
+            // FIXME: Check if their width should be added to currentLogicalRight.
+            candidateContent.append(inlineItem, { });
+            continue;
+        }
+        if (inlineItem.isLineBreak()) {
+            // We definitely need to stop at the line break.
+            candidateContent.setEndsWithLineBreak();
+            return candidateContent;
+        }
+        if (LineBreaker::lastSoftWrapOpportunity(inlineItem, candidateContent.runs())) {
+            // Let's wrap up this list, if we arrived to a soft wrap opportunity.
+            return candidateContent;
+        }
+
+        auto inlineItenmWidth = inlineItemWidth(formattingContext(), inlineItem, currentLogicalRight);
+        candidateContent.append(inlineItem, inlineItenmWidth);
+        currentLogicalRight += inlineItenmWidth;
+        ++inlineItemIndex;
+    }
+    return candidateContent;
 }
 
-LineLayoutContext::IsEndOfLine LineLayoutContext::placeInlineItem(LineBuilder& line, const InlineItem& inlineItem)
+LineLayoutContext::CommittedContent LineLayoutContext::addFloatItems(LineBuilder& line, const FloatList& floats)
 {
-    auto currentLogicalRight = line.lineBox().logicalWidth() + m_uncommittedContent.width();
-    auto itemLogicalWidth = inlineItemWidth(formattingContext(), inlineItem, currentLogicalRight);
+    size_t committedFloatItemCount = 0;
+    for (auto& floatItem : floats) {
+        auto logicalWidth = inlineItemWidth(formattingContext(), *floatItem, { });
 
-    // Floats are special, they are intrusive but they don't really participate in the line layout context.
-    if (inlineItem.isFloat()) {
-        // FIXME: It gets a bit more complicated when there's some uncommitted content whether they should be added to the current line
-        // e.g. text_content<div style="float: left"></div>continuous_text_content
-        // Not sure what to do when the float takes up the available space and we've got continuous content. Browser engines don't agree.
-        // Let's just commit the pending content and try placing the float for now.
-        if (!m_uncommittedContent.isEmpty()) {
-            if (processUncommittedContent(line) == IsEndOfLine::Yes)
-                return IsEndOfLine::Yes;
-        }
         auto lineIsConsideredEmpty = line.isVisuallyEmpty() && !line.hasIntrusiveFloat();
-        if (LineBreaker().shouldWrapFloatBox(itemLogicalWidth, line.availableWidth() + line.trailingCollapsibleWidth(), lineIsConsideredEmpty))
-            return IsEndOfLine::Yes;
-
+        if (LineBreaker().shouldWrapFloatBox(logicalWidth, line.availableWidth() + line.trailingCollapsibleWidth(), lineIsConsideredEmpty))
+            return { IsEndOfLine::Yes, committedFloatItemCount, { } };
         // This float can sit on the current line.
-        auto& floatBox = inlineItem.layoutBox();
+        ++committedFloatItemCount;
+        auto& floatBox = floatItem->layoutBox();
         // Shrink available space for current line and move existing inline runs.
-        floatBox.isLeftFloatingPositioned() ? line.moveLogicalLeft(itemLogicalWidth) : line.moveLogicalRight(itemLogicalWidth);
-        m_floats.append(makeWeakPtr(inlineItem));
-        ++m_committedInlineItemCount;
         line.setHasIntrusiveFloat();
-        return IsEndOfLine::No;
+        if (floatBox.isLeftFloatingPositioned())
+            line.moveLogicalLeft(logicalWidth);
+        else
+            line.moveLogicalRight(logicalWidth);
+        m_floats.append(floatItem);
     }
-    // Line breaks are also special.
-    if (inlineItem.isLineBreak()) {
-        auto isEndOfLine = IsEndOfLine::No;
-        if (!m_uncommittedContent.isEmpty())
-            isEndOfLine = processUncommittedContent(line);
-        // When the uncommitted content fits(or the line is empty), add the line break to this line as well.
-        if (isEndOfLine == IsEndOfLine::No) {
-            m_uncommittedContent.append(inlineItem, itemLogicalWidth);
-            commitPendingContent(line);
-        }
-        return IsEndOfLine::Yes;
-    }
-    auto isEndOfLine = IsEndOfLine::No;
-    // Can we commit the pending content already?
-    if (LineBreaker::lastSoftWrapOpportunity(inlineItem, m_uncommittedContent.runs()))
-        isEndOfLine = processUncommittedContent(line);
-    // The current item might fit as well.
-    if (isEndOfLine == IsEndOfLine::No)
-        m_uncommittedContent.append(inlineItem, itemLogicalWidth);
-    return isEndOfLine;
+    return { IsEndOfLine::No, committedFloatItemCount, { } };
 }
 
-LineLayoutContext::IsEndOfLine LineLayoutContext::processUncommittedContent(LineBuilder& line)
+LineLayoutContext::CommittedContent LineLayoutContext::placeInlineContentOnCurrentLine(LineBuilder& line, const LineBreaker::RunList& runs)
 {
     auto shouldDisableHyphenation = [&] {
-        auto& style = m_formattingContextRoot.style();
+        auto& style = root().style();
         unsigned limitLines = style.hyphenationLimitLines() == RenderStyle::initialHyphenationLimitLines() ? std::numeric_limits<unsigned>::max() : style.hyphenationLimitLines();
         return m_successiveHyphenatedLineCount >= limitLines;
     };
-
-    // Check if the pending content fits.
+    // Check if this new content fits.
     auto lineIsConsideredEmpty = line.isVisuallyEmpty() && !line.hasIntrusiveFloat();
+    auto lineStatus = LineBreaker::LineStatus { line.availableWidth(), line.trailingCollapsibleWidth(), line.isTrailingRunFullyCollapsible(), lineIsConsideredEmpty };
     auto lineBreaker = LineBreaker { };
+
     if (shouldDisableHyphenation())
         lineBreaker.setHyphenationDisabled();
-    auto lineStatus = LineBreaker::LineStatus { line.availableWidth(), line.trailingCollapsibleWidth(), line.isTrailingRunFullyCollapsible(), lineIsConsideredEmpty };
-    auto breakingContext = lineBreaker.breakingContextForInlineContent(m_uncommittedContent, lineStatus);
-    // The uncommitted content can fully, partially fit the current line (commit/partial commit) or not at all (reset).
-    if (breakingContext.contentWrappingRule == LineBreaker::BreakingContext::ContentWrappingRule::Keep)
-        commitPendingContent(line);
-    else if (breakingContext.contentWrappingRule == LineBreaker::BreakingContext::ContentWrappingRule::Split) {
+
+    auto breakingContext = lineBreaker.breakingContextForInlineContent(LineBreaker::ContinousContent { runs }, lineStatus);
+    if (breakingContext.contentWrappingRule == LineBreaker::BreakingContext::ContentWrappingRule::Keep) {
+        // This continuous content can be fully placed on the current line.
+        commitContent(line, runs, { });
+        return { IsEndOfLine::No, runs.size(), { } };
+    }
+
+    if (breakingContext.contentWrappingRule == LineBreaker::BreakingContext::ContentWrappingRule::Push) {
+        // This continuous content can't be placed on the current line. Nothing to commit at this time.
+        return { IsEndOfLine::Yes, 0, { } };
+    }
+
+    if (breakingContext.contentWrappingRule == LineBreaker::BreakingContext::ContentWrappingRule::Split) {
+        // Commit the combination of full and partial content on the current line.
         ASSERT(breakingContext.partialTrailingContent);
-        ASSERT(m_uncommittedContent.runs()[breakingContext.partialTrailingContent->triailingRunIndex].inlineItem.isText());
-        // Turn the uncommitted trailing run into a partial trailing run.
-        auto trailingRunIndex = breakingContext.partialTrailingContent->triailingRunIndex;
-        auto& trailingInlineTextItem = downcast<InlineTextItem>(m_uncommittedContent.runs()[trailingRunIndex].inlineItem);
+        commitContent(line, runs, breakingContext.partialTrailingContent);
+        // When splitting multiple runs <span style="word-break: break-all">text</span><span>content</span>, we might end up splitting them at run boundary.
+        // It simply means we don't really have a partial run. Partial content yes, but not partial run.
+        auto triailingRunIndex = breakingContext.partialTrailingContent->triailingRunIndex;
+        auto committedInlineItemCount = triailingRunIndex + 1;
+        if (!breakingContext.partialTrailingContent->partialRun)
+            return { IsEndOfLine::Yes, committedInlineItemCount, { } };
 
-        ASSERT(!m_partialTrailingTextItem);
-        // Construct a partial trailing inline run only if there's a need for one.
-        // When splitting multiple runs <span style="word-break: break-all">text</span><span>content</span>, we might end up splitting them at run boundary.
-        // In such case we don't need to construct a partial trailing item since the trailing run is not partial at all.
-        if (auto partialTrailingRun = breakingContext.partialTrailingContent->partialRun) {
-            m_partialTrailingTextItem = trailingInlineTextItem.left(partialTrailingRun->length);
-            m_partialContent = LineContent::PartialContent { partialTrailingRun->needsHyphen, trailingInlineTextItem.length() - partialTrailingRun->length };
-            // Keep the non-overflow part of the uncommitted runs and add the trailing partial content.
-            m_uncommittedContent.shrink(trailingRunIndex);
-            m_uncommittedContent.append(*m_partialTrailingTextItem, partialTrailingRun->logicalWidth);
-            // Adjust hyphenated line count.
-            if (partialTrailingRun->needsHyphen)
-                ++m_successiveHyphenatedLineCount;
-        } else {
-            // Trim the overflown run and keep the trailing (non-partial) run in the queue.
-            auto overflownRunIndex = trailingRunIndex + 1;
-            ASSERT(overflownRunIndex < m_uncommittedContent.size());
-            m_uncommittedContent.shrink(overflownRunIndex);
+        auto partialRun = *breakingContext.partialTrailingContent->partialRun;
+        auto& trailingInlineTextItem = downcast<InlineTextItem>(runs[triailingRunIndex].inlineItem);
+        auto overflowLength = trailingInlineTextItem.length() - partialRun.length;
+        return { IsEndOfLine::Yes, committedInlineItemCount, LineContent::PartialContent { partialRun.needsHyphen, overflowLength } };
+    }
+    ASSERT_NOT_REACHED();
+    return { IsEndOfLine::No, 0, { } };
+}
+
+void LineLayoutContext::commitContent(LineBuilder& line, const LineBreaker::RunList& runs, Optional<LineBreaker::BreakingContext::PartialTrailingContent> partialTrailingContent)
+{
+    for (size_t index = 0; index < runs.size(); ++index) {
+        auto& run = runs[index];
+        if (partialTrailingContent && partialTrailingContent->triailingRunIndex == index) {
+            ASSERT(run.inlineItem.isText());
+            // Create and commit partial trailing item.
+            if (auto partialRun = partialTrailingContent->partialRun) {
+                auto& trailingInlineTextItem = downcast<InlineTextItem>(runs[partialTrailingContent->triailingRunIndex].inlineItem);
+                // FIXME: LineBuilder should not hold on to the InlineItem.
+                ASSERT(!m_partialTrailingTextItem);
+                m_partialTrailingTextItem = trailingInlineTextItem.left(partialRun->length);
+                line.append(*m_partialTrailingTextItem, partialRun->logicalWidth);
+                return;
+            }
+            // The partial run is the last content to commit.
+            line.append(run.inlineItem, run.logicalWidth);
+            return;
         }
-        commitPendingContent(line);
-    } else if (breakingContext.contentWrappingRule == LineBreaker::BreakingContext::ContentWrappingRule::Push) {
-        m_uncommittedContent.reset();
-        // Closing the line without any hyphen.
-        m_successiveHyphenatedLineCount = 0;
-    } else
-        ASSERT_NOT_REACHED();
-    return breakingContext.contentWrappingRule == LineBreaker::BreakingContext::ContentWrappingRule::Keep ? IsEndOfLine::No : IsEndOfLine::Yes;
+        line.append(run.inlineItem, run.logicalWidth);
+    }
 }
 
 }

Modified: trunk/Source/WebCore/layout/inlineformatting/LineLayoutContext.h (253814 => 253815)


--- trunk/Source/WebCore/layout/inlineformatting/LineLayoutContext.h	2019-12-20 13:35:19 UTC (rev 253814)
+++ trunk/Source/WebCore/layout/inlineformatting/LineLayoutContext.h	2019-12-20 13:44:01 UTC (rev 253815)
@@ -33,6 +33,8 @@
 namespace WebCore {
 namespace Layout {
 
+struct ContinousContent;
+
 class LineLayoutContext {
 public:
     LineLayoutContext(const InlineFormattingContext&, const Container& formattingContextRoot, const InlineItems&);
@@ -49,25 +51,30 @@
         const Display::LineBox lineBox;
     };
     LineContent layoutLine(LineBuilder&, unsigned leadingInlineItemIndex, Optional<unsigned> partialLeadingContentLength);
+    using FloatList = Vector<WeakPtr<InlineItem>>;
 
 private:
-    const InlineFormattingContext& formattingContext() const { return m_inlineFormattingContext; }
     enum class IsEndOfLine { No, Yes };
-    IsEndOfLine placeInlineItem(LineBuilder&, const InlineItem&);
-    void commitPendingContent(LineBuilder&);
-    LineContent close(LineBuilder&, unsigned leadingInlineItemIndex);
-    bool shouldProcessUncommittedContent(const InlineItem&) const;
-    IsEndOfLine processUncommittedContent(LineBuilder&);
+    struct CommittedContent {
+        IsEndOfLine isEndOfLine;
+        size_t count { 0 };
+        Optional <LineContent::PartialContent> partialContent;
+    };
+    ContinousContent nextContinousContentForLine(unsigned inlineItemIndex, Optional<unsigned> overflowLength, InlineLayoutUnit currentLogicalRight);
+    CommittedContent addFloatItems(LineBuilder&, const FloatList&);
+    CommittedContent placeInlineContentOnCurrentLine(LineBuilder&, const LineBreaker::RunList&);
+    void commitContent(LineBuilder&, const LineBreaker::RunList&, Optional<LineBreaker::BreakingContext::PartialTrailingContent>);
+    LineContent close(LineBuilder&, unsigned leadingInlineItemIndex, unsigned committedInlineItemCount, Optional<LineContent::PartialContent>);
 
+    const InlineFormattingContext& formattingContext() const { return m_inlineFormattingContext; }
+    const Container& root() const { return m_formattingContextRoot; }
+
     const InlineFormattingContext& m_inlineFormattingContext;
     const Container& m_formattingContextRoot;
     const InlineItems& m_inlineItems;
-    LineBreaker::Content m_uncommittedContent;
-    unsigned m_committedInlineItemCount { 0 };
-    Vector<WeakPtr<InlineItem>> m_floats;
+    FloatList m_floats;
     std::unique_ptr<InlineTextItem> m_partialLeadingTextItem;
     std::unique_ptr<InlineTextItem> m_partialTrailingTextItem;
-    Optional<LineContent::PartialContent> m_partialContent;
     unsigned m_successiveHyphenatedLineCount { 0 };
 };
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to