Title: [250132] trunk/Source/WebCore
Revision
250132
Author
[email protected]
Date
2019-09-20 08:41:43 -0700 (Fri, 20 Sep 2019)

Log Message

Implement Position::upstream and Position::downstream with line layout iterator
https://bugs.webkit.org/show_bug.cgi?id=202043

Reviewed by Zalan Bujtas.

These can eventually work without switching to lineboxes.

* dom/Position.cpp:
(WebCore::Position::upstream const):
(WebCore::Position::downstream const):

Factor the loops into TextBox::isLastOnLine function.

* rendering/line/LineLayoutInterfaceTextBoxes.cpp:
(WebCore::LineLayoutInterface::TextBox::isLastOnLine const):

Looking into root boxes is a much more efficient way to figure this out.

(WebCore::LineLayoutInterface::TextBox::isLast const):
(WebCore::LineLayoutInterface::TextBoxIterator::ComplexPath::nextInlineTextBoxInTextOrder const):
(WebCore::LineLayoutInterface::TextBoxIterator::traverseNextInTextOrder):
* rendering/line/LineLayoutInterfaceTextBoxes.h:

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (250131 => 250132)


--- trunk/Source/WebCore/ChangeLog	2019-09-20 15:40:09 UTC (rev 250131)
+++ trunk/Source/WebCore/ChangeLog	2019-09-20 15:41:43 UTC (rev 250132)
@@ -1,3 +1,28 @@
+2019-09-20  Antti Koivisto  <[email protected]>
+
+        Implement Position::upstream and Position::downstream with line layout iterator
+        https://bugs.webkit.org/show_bug.cgi?id=202043
+
+        Reviewed by Zalan Bujtas.
+
+        These can eventually work without switching to lineboxes.
+
+        * dom/Position.cpp:
+        (WebCore::Position::upstream const):
+        (WebCore::Position::downstream const):
+
+        Factor the loops into TextBox::isLastOnLine function.
+
+        * rendering/line/LineLayoutInterfaceTextBoxes.cpp:
+        (WebCore::LineLayoutInterface::TextBox::isLastOnLine const):
+
+        Looking into root boxes is a much more efficient way to figure this out.
+
+        (WebCore::LineLayoutInterface::TextBox::isLast const):
+        (WebCore::LineLayoutInterface::TextBoxIterator::ComplexPath::nextInlineTextBoxInTextOrder const):
+        (WebCore::LineLayoutInterface::TextBoxIterator::traverseNextInTextOrder):
+        * rendering/line/LineLayoutInterfaceTextBoxes.h:
+
 2019-09-20  Zalan Bujtas  <[email protected]>
 
         [LFC] Introduce LayoutContext class

Modified: trunk/Source/WebCore/dom/Position.cpp (250131 => 250132)


--- trunk/Source/WebCore/dom/Position.cpp	2019-09-20 15:40:09 UTC (rev 250131)
+++ trunk/Source/WebCore/dom/Position.cpp	2019-09-20 15:41:43 UTC (rev 250132)
@@ -37,6 +37,7 @@
 #include "InlineElementBox.h"
 #include "InlineIterator.h"
 #include "InlineTextBox.h"
+#include "LineLayoutInterfaceTextBoxes.h"
 #include "Logging.h"
 #include "NodeTraversal.h"
 #include "PositionIterator.h"
@@ -708,7 +709,10 @@
         RenderObject* renderer = currentNode.renderer();
         if (!renderer || renderer->style().visibility() != Visibility::Visible)
             continue;
+
+        // FIXME: The code below doesn't need line boxes.
         ensureLineBoxesIfNeeded(*renderer);
+
         if (rule == CanCrossEditingBoundary && boundaryCrossed) {
             lastVisible = currentPosition;
             break;
@@ -733,8 +737,12 @@
         // return current position if it is in rendered text
         if (is<RenderText>(*renderer)) {
             auto& textRenderer = downcast<RenderText>(*renderer);
-            if (!textRenderer.firstTextBox())
+
+            LineLayoutInterface::Provider lineLayoutProvider;
+            auto firstTextBox = lineLayoutProvider.firstTextBoxInTextOrderFor(textRenderer);
+            if (!firstTextBox)
                 continue;
+
             if (&currentNode != startNode) {
                 // This assertion fires in layout tests in the case-transform.html test because
                 // of a mix-up between offsets in the text in the DOM tree with text in the
@@ -745,40 +753,14 @@
             }
 
             unsigned textOffset = currentPosition.offsetInLeafNode();
-            auto lastTextBox = textRenderer.lastTextBox();
-            for (auto* box = textRenderer.firstTextBox(); box; box = box->nextTextBox()) {
-                if (textOffset <= box->start() + box->len()) {
-                    if (textOffset > box->start())
+            for (auto box = firstTextBox; box; box.traverseNextInTextOrder()) {
+                if (textOffset <= box->localEndOffset()) {
+                    if (textOffset > box->localStartOffset())
                         return currentPosition;
                     continue;
                 }
 
-                if (box == lastTextBox || textOffset != box->start() + box->len() + 1)
-                    continue;
-
-                // The text continues on the next line only if the last text box is not on this line and
-                // none of the boxes on this line have a larger start offset.
-
-                bool continuesOnNextLine = true;
-                InlineBox* otherBox = box;
-                while (continuesOnNextLine) {
-                    otherBox = otherBox->nextLeafChild();
-                    if (!otherBox)
-                        break;
-                    if (otherBox == lastTextBox || (&otherBox->renderer() == &textRenderer && downcast<InlineTextBox>(*otherBox).start() > textOffset))
-                        continuesOnNextLine = false;
-                }
-
-                otherBox = box;
-                while (continuesOnNextLine) {
-                    otherBox = otherBox->prevLeafChild();
-                    if (!otherBox)
-                        break;
-                    if (otherBox == lastTextBox || (&otherBox->renderer() == &textRenderer && downcast<InlineTextBox>(*otherBox).start() > textOffset))
-                        continuesOnNextLine = false;
-                }
-
-                if (continuesOnNextLine)
+                if (textOffset == box->localEndOffset() + 1 && box->isLastOnLine() && !box->isLast())
                     return currentPosition;
             }
         }
@@ -810,7 +792,7 @@
     bool boundaryCrossed = false;
     for (; !currentPosition.atEnd(); currentPosition.increment()) {
         auto& currentNode = *currentPosition.node();
-        
+
         // Don't check for an editability change if we haven't moved to a different node,
         // to avoid the expense of computing hasEditableStyle().
         if (&currentNode != lastNode) {
@@ -821,7 +803,7 @@
                     break;
                 boundaryCrossed = true;
             }
-                
+
             lastNode = &currentNode;
         }
 
@@ -843,12 +825,15 @@
         auto* renderer = currentNode.renderer();
         if (!renderer || renderer->style().visibility() != Visibility::Visible)
             continue;
+
+        // FIXME: The code below doesn't need line boxes.
         ensureLineBoxesIfNeeded(*renderer);
+
         if (rule == CanCrossEditingBoundary && boundaryCrossed) {
             lastVisible = currentPosition;
             break;
         }
-        
+
         // track last visible streamer position
         if (isStreamer(currentPosition))
             lastVisible = currentPosition;
@@ -863,56 +848,34 @@
         // return current position if it is in rendered text
         if (is<RenderText>(*renderer)) {
             auto& textRenderer = downcast<RenderText>(*renderer);
-            if (!textRenderer.firstTextBox())
+
+            LineLayoutInterface::Provider lineLayoutProvider;
+            auto firstTextBox = lineLayoutProvider.firstTextBoxInTextOrderFor(textRenderer);
+            if (!firstTextBox)
                 continue;
+
             if (&currentNode != startNode) {
                 ASSERT(currentPosition.atStartOfNode());
-                return createLegacyEditingPosition(&currentNode, renderer->caretMinOffset());
+                return createLegacyEditingPosition(&currentNode, textRenderer.caretMinOffset());
             }
 
             unsigned textOffset = currentPosition.offsetInLeafNode();
-            auto lastTextBox = textRenderer.lastTextBox();
-            for (auto* box = textRenderer.firstTextBox(); box; box = box->nextTextBox()) {
-                if (!box->len() && textOffset == box->start())
+            for (auto box = firstTextBox; box; box.traverseNextInTextOrder()) {
+                if (!box->length() && textOffset == box->localStartOffset())
                     return currentPosition;
-            
-                if (textOffset < box->end()) {
-                    if (textOffset >= box->start())
+
+                if (textOffset < box->localEndOffset()) {
+                    if (textOffset >= box->localStartOffset())
                         return currentPosition;
                     continue;
                 }
 
-                if (box == lastTextBox || textOffset != box->start() + box->len())
-                    continue;
-
-                // The text continues on the next line only if the last text box is not on this line and
-                // none of the boxes on this line have a larger start offset.
-
-                bool continuesOnNextLine = true;
-                InlineBox* otherBox = box;
-                while (continuesOnNextLine) {
-                    otherBox = otherBox->nextLeafChild();
-                    if (!otherBox)
-                        break;
-                    if (otherBox == lastTextBox || (&otherBox->renderer() == &textRenderer && downcast<InlineTextBox>(*otherBox).start() >= textOffset))
-                        continuesOnNextLine = false;
-                }
-
-                otherBox = box;
-                while (continuesOnNextLine) {
-                    otherBox = otherBox->prevLeafChild();
-                    if (!otherBox)
-                        break;
-                    if (otherBox == lastTextBox || (&otherBox->renderer() == &textRenderer && downcast<InlineTextBox>(*otherBox).start() >= textOffset))
-                        continuesOnNextLine = false;
-                }
-
-                if (continuesOnNextLine)
+                if (textOffset == box->localEndOffset() && box->isLastOnLine() && !box->isLast())
                     return currentPosition;
             }
         }
     }
-    
+
     return lastVisible;
 }
 

Modified: trunk/Source/WebCore/rendering/line/LineLayoutInterfaceTextBoxes.cpp (250131 => 250132)


--- trunk/Source/WebCore/rendering/line/LineLayoutInterfaceTextBoxes.cpp	2019-09-20 15:40:09 UTC (rev 250131)
+++ trunk/Source/WebCore/rendering/line/LineLayoutInterfaceTextBoxes.cpp	2019-09-20 15:41:43 UTC (rev 250132)
@@ -150,6 +150,38 @@
     return WTF::switchOn(iterator().m_pathVariant, simple, complex);
 }
 
+bool TextBox::isLastOnLine() const
+{
+    auto simple = [](const TextBoxIterator::SimplePath& path) {
+        auto next = path.iterator;
+        ++next;
+        return next == path.end || (*path.iterator).lineIndex() != (*next).lineIndex();
+    };
+
+    auto complex = [](const TextBoxIterator::ComplexPath& path) {
+        auto* next = path.nextInlineTextBoxInTextOrder();
+        return !next || &path.inlineTextBox->root() != &next->root();
+    };
+
+    return WTF::switchOn(iterator().m_pathVariant, simple, complex);
+}
+
+bool TextBox::isLast() const
+{
+    auto simple = [](const TextBoxIterator::SimplePath& path) {
+        auto next = path.iterator;
+        ++next;
+        return next == path.end;
+    };
+
+    auto complex = [](const TextBoxIterator::ComplexPath& path) {
+        return !path.nextInlineTextBoxInTextOrder();
+    };
+
+    return WTF::switchOn(iterator().m_pathVariant, simple, complex);
+}
+
+
 inline const TextBoxIterator& TextBox::iterator() const
 {
     return static_cast<const TextBoxIterator&>(*this);
@@ -184,6 +216,16 @@
     return *this;
 }
 
+const InlineTextBox* TextBoxIterator::ComplexPath::nextInlineTextBoxInTextOrder() const
+{
+    if (!sortedInlineTextBoxes.isEmpty()) {
+        if (sortedInlineTextBoxIndex + 1 < sortedInlineTextBoxes.size())
+            return sortedInlineTextBoxes[sortedInlineTextBoxIndex + 1];
+        return nullptr;
+    }
+    return inlineTextBox->nextTextBox();
+}
+
 TextBoxIterator& TextBoxIterator::traverseNextInTextOrder()
 {
     auto simple = [](SimplePath& path) {
@@ -191,14 +233,9 @@
     };
 
     auto complex = [](ComplexPath& path) {
-        if (!path.sortedInlineTextBoxes.isEmpty()) {
+        path.inlineTextBox = path.nextInlineTextBoxInTextOrder();
+        if (!path.sortedInlineTextBoxes.isEmpty())
             ++path.sortedInlineTextBoxIndex;
-            if (path.sortedInlineTextBoxIndex < path.sortedInlineTextBoxes.size())
-                path.inlineTextBox = path.sortedInlineTextBoxes[path.sortedInlineTextBoxIndex];
-            else
-                path.inlineTextBox = nullptr;
-        } else
-            path.inlineTextBox = path.inlineTextBox->nextTextBox();
     };
 
     WTF::switchOn(m_pathVariant, simple, complex);

Modified: trunk/Source/WebCore/rendering/line/LineLayoutInterfaceTextBoxes.h (250131 => 250132)


--- trunk/Source/WebCore/rendering/line/LineLayoutInterfaceTextBoxes.h	2019-09-20 15:40:09 UTC (rev 250131)
+++ trunk/Source/WebCore/rendering/line/LineLayoutInterfaceTextBoxes.h	2019-09-20 15:41:43 UTC (rev 250132)
@@ -61,6 +61,9 @@
     unsigned localEndOffset() const;
     unsigned length() const;
 
+    bool isLastOnLine() const;
+    bool isLast() const;
+
 protected:
     TextBox() = default;
     TextBox(const TextBox&) = default;
@@ -108,6 +111,8 @@
         const InlineTextBox* inlineTextBox;
         Vector<const InlineTextBox*> sortedInlineTextBoxes;
         size_t sortedInlineTextBoxIndex { 0 };
+
+        const InlineTextBox* nextInlineTextBoxInTextOrder() const;
     };
     Variant<SimplePath, ComplexPath> m_pathVariant;
 };
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to