Title: [287863] trunk
Revision
287863
Author
[email protected]
Date
2022-01-10 15:48:40 -0800 (Mon, 10 Jan 2022)

Log Message

Add support for a -shouldWrap property when injecting Live Text
https://bugs.webkit.org/show_bug.cgi?id=235035
rdar://85139146

Reviewed by Tim Horton.

Source/WebCore:

Add support for a new property on VKWKLineInfo to indicate whether or not the line should wrap. If set to true,
we will avoid adding a `br` element at the end of the `div.image-overlay-line` that corresponds to the line of
recognized text in the image overlay; currently, we always add this trailing `br`.

Test: fast/images/text-recognition/image-overlay-line-wrapping.html

* dom/ImageOverlay.cpp:
(WebCore::ImageOverlay::updateSubtree):

Consult the flag when converting each TextRecognitionLineData into shadow DOM elements.

* platform/TextRecognitionResult.h:

Add the new boolean flag to TextRecognitionLineData, and add encoding/decoding support.

(WebCore::TextRecognitionLineData::TextRecognitionLineData):
(WebCore::TextRecognitionLineData::encode const):
(WebCore::TextRecognitionLineData::decode):
* testing/Internals.cpp:
(WebCore::makeDataForLine):

Add a way to specify a value for `shouldWrap`, when using `internals` to inject image overlay content in layout
and API tests.

* testing/Internals.h:
* testing/Internals.idl:

Source/WebCore/PAL:

Add a forward staging declaration for the new SPI property.

* pal/spi/cocoa/VisionKitCoreSPI.h:

Source/WebKit:

Consult the new SPI property (behind a runtime selector check), and pass the boolean flag through the
TextRecognitionResult.

* Platform/cocoa/TextRecognitionUtilities.mm:
(WebKit::makeTextRecognitionResult):

LayoutTests:

Add a layout test to exercise the changes. See WebCore/ChangeLog for more details.

* fast/images/text-recognition/image-overlay-line-wrapping-expected.txt: Added.
* fast/images/text-recognition/image-overlay-line-wrapping.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (287862 => 287863)


--- trunk/LayoutTests/ChangeLog	2022-01-10 23:42:34 UTC (rev 287862)
+++ trunk/LayoutTests/ChangeLog	2022-01-10 23:48:40 UTC (rev 287863)
@@ -1,3 +1,16 @@
+2022-01-10  Wenson Hsieh  <[email protected]>
+
+        Add support for a -shouldWrap property when injecting Live Text
+        https://bugs.webkit.org/show_bug.cgi?id=235035
+        rdar://85139146
+
+        Reviewed by Tim Horton.
+
+        Add a layout test to exercise the changes. See WebCore/ChangeLog for more details.
+
+        * fast/images/text-recognition/image-overlay-line-wrapping-expected.txt: Added.
+        * fast/images/text-recognition/image-overlay-line-wrapping.html: Added.
+
 2022-01-10  Chris Dumez  <[email protected]>
 
         https://brianpeiris.github.io/spatial-audio-test/?ramped stops playing after a few seconds

Added: trunk/LayoutTests/fast/images/text-recognition/image-overlay-line-wrapping-expected.txt (0 => 287863)


--- trunk/LayoutTests/fast/images/text-recognition/image-overlay-line-wrapping-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/fast/images/text-recognition/image-overlay-line-wrapping-expected.txt	2022-01-10 23:48:40 UTC (rev 287863)
@@ -0,0 +1,6 @@
+
+PASS textarea.value is "this is a test\nfoo"
+PASS successfullyParsed is true
+
+TEST COMPLETE
+

Added: trunk/LayoutTests/fast/images/text-recognition/image-overlay-line-wrapping.html (0 => 287863)


--- trunk/LayoutTests/fast/images/text-recognition/image-overlay-line-wrapping.html	                        (rev 0)
+++ trunk/LayoutTests/fast/images/text-recognition/image-overlay-line-wrapping.html	2022-01-10 23:48:40 UTC (rev 287863)
@@ -0,0 +1,96 @@
+<!DOCTYPE html>
+<html>
+<head>
+<script src=""
+<style>
+body, html {
+    margin: 0;
+}
+img {
+    width: 300px;
+    height: 300px;
+}
+</style>
+</head>
+<body>
+<textarea></textarea>
+<img src=""
+<pre id="console"></pre>
+<script>
+addEventListener("load", () => {
+    const image = document.querySelector("img");
+    internals.installImageOverlay(image, [
+        {
+            topLeft : new DOMPointReadOnly(0, 0),
+            topRight : new DOMPointReadOnly(1, 0),
+            bottomRight : new DOMPointReadOnly(1, 0.3),
+            bottomLeft : new DOMPointReadOnly(0, 0.3),
+            shouldWrap: true,
+            children: [
+                {
+                    text : "this",
+                    topLeft : new DOMPointReadOnly(0, 0),
+                    topRight : new DOMPointReadOnly(0.7, 0),
+                    bottomRight : new DOMPointReadOnly(0.7, 0.3),
+                    bottomLeft : new DOMPointReadOnly(0, 0.3),
+                },
+                {
+                    text : "is",
+                    topLeft : new DOMPointReadOnly(0.7, 0),
+                    topRight : new DOMPointReadOnly(1, 0),
+                    bottomRight : new DOMPointReadOnly(1, 0.3),
+                    bottomLeft : new DOMPointReadOnly(0.7, 0.3),
+                }
+            ],
+        },
+        {
+            topLeft : new DOMPointReadOnly(0, 0.3),
+            topRight : new DOMPointReadOnly(1, 0.3),
+            bottomRight : new DOMPointReadOnly(1, 0.7),
+            bottomLeft : new DOMPointReadOnly(0, 0.7),
+            children: [
+                {
+                    text : "a",
+                    topLeft : new DOMPointReadOnly(0, 0.3),
+                    topRight : new DOMPointReadOnly(0.3, 0.3),
+                    bottomRight : new DOMPointReadOnly(0.3, 0.7),
+                    bottomLeft : new DOMPointReadOnly(0, 0.7),
+                },
+                {
+                    text : "test",
+                    topLeft : new DOMPointReadOnly(0.3, 0.3),
+                    topRight : new DOMPointReadOnly(1, 0.3),
+                    bottomRight : new DOMPointReadOnly(1, 0.7),
+                    bottomLeft : new DOMPointReadOnly(0.3, 0.7),
+                }
+            ],
+        },
+        {
+            topLeft : new DOMPointReadOnly(0, 0.7),
+            topRight : new DOMPointReadOnly(0.7, 0.7),
+            bottomRight : new DOMPointReadOnly(0.7, 1),
+            bottomLeft : new DOMPointReadOnly(0, 1),
+            children: [
+                {
+                    text : "foo",
+                    topLeft : new DOMPointReadOnly(0, 0.7),
+                    topRight : new DOMPointReadOnly(0.7, 0.7),
+                    bottomRight : new DOMPointReadOnly(0.7, 1),
+                    bottomLeft : new DOMPointReadOnly(0, 1),
+                }
+            ],
+        }
+    ]);
+
+    getSelection().selectAllChildren(internals.shadowRoot(image).getElementById("image-overlay"));
+    document.execCommand("Copy");
+
+    textarea = document.querySelector("textarea");
+    textarea.focus();
+
+    document.execCommand("Paste");
+    shouldBeEqualToString("textarea.value", "this is a test\nfoo");
+});
+</script>
+</body>
+</html>
\ No newline at end of file

Modified: trunk/Source/WebCore/ChangeLog (287862 => 287863)


--- trunk/Source/WebCore/ChangeLog	2022-01-10 23:42:34 UTC (rev 287862)
+++ trunk/Source/WebCore/ChangeLog	2022-01-10 23:48:40 UTC (rev 287863)
@@ -1,3 +1,38 @@
+2022-01-10  Wenson Hsieh  <[email protected]>
+
+        Add support for a -shouldWrap property when injecting Live Text
+        https://bugs.webkit.org/show_bug.cgi?id=235035
+        rdar://85139146
+
+        Reviewed by Tim Horton.
+
+        Add support for a new property on VKWKLineInfo to indicate whether or not the line should wrap. If set to true,
+        we will avoid adding a `br` element at the end of the `div.image-overlay-line` that corresponds to the line of
+        recognized text in the image overlay; currently, we always add this trailing `br`.
+
+        Test: fast/images/text-recognition/image-overlay-line-wrapping.html
+
+        * dom/ImageOverlay.cpp:
+        (WebCore::ImageOverlay::updateSubtree):
+
+        Consult the flag when converting each TextRecognitionLineData into shadow DOM elements.
+
+        * platform/TextRecognitionResult.h:
+
+        Add the new boolean flag to TextRecognitionLineData, and add encoding/decoding support.
+
+        (WebCore::TextRecognitionLineData::TextRecognitionLineData):
+        (WebCore::TextRecognitionLineData::encode const):
+        (WebCore::TextRecognitionLineData::decode):
+        * testing/Internals.cpp:
+        (WebCore::makeDataForLine):
+
+        Add a way to specify a value for `shouldWrap`, when using `internals` to inject image overlay content in layout
+        and API tests.
+
+        * testing/Internals.h:
+        * testing/Internals.idl:
+
 2022-01-10  Eric Carlson  <[email protected]>
 
         hasBrokenEncryptedMediaAPISupportQuirk and needsPreloadAutoQuirk have overly permissive domain allow lists

Modified: trunk/Source/WebCore/PAL/ChangeLog (287862 => 287863)


--- trunk/Source/WebCore/PAL/ChangeLog	2022-01-10 23:42:34 UTC (rev 287862)
+++ trunk/Source/WebCore/PAL/ChangeLog	2022-01-10 23:48:40 UTC (rev 287863)
@@ -1,3 +1,15 @@
+2022-01-10  Wenson Hsieh  <[email protected]>
+
+        Add support for a -shouldWrap property when injecting Live Text
+        https://bugs.webkit.org/show_bug.cgi?id=235035
+        rdar://85139146
+
+        Reviewed by Tim Horton.
+
+        Add a forward staging declaration for the new SPI property.
+
+        * pal/spi/cocoa/VisionKitCoreSPI.h:
+
 2022-01-10  Alex Christensen  <[email protected]>
 
         Start using C++20

Modified: trunk/Source/WebCore/PAL/pal/spi/cocoa/VisionKitCoreSPI.h (287862 => 287863)


--- trunk/Source/WebCore/PAL/pal/spi/cocoa/VisionKitCoreSPI.h	2022-01-10 23:42:34 UTC (rev 287862)
+++ trunk/Source/WebCore/PAL/pal/spi/cocoa/VisionKitCoreSPI.h	2022-01-10 23:48:40 UTC (rev 287863)
@@ -146,6 +146,10 @@
 
 #endif
 
+@interface VKWKLineInfo (Staging_85139101)
+@property (nonatomic, readonly) BOOL shouldWrap;
+@end
+
 #if USE(APPLE_INTERNAL_SDK)
 #include <WebKitAdditions/VisionKitCoreSPIAdditions.h>
 #endif

Modified: trunk/Source/WebCore/dom/ImageOverlay.cpp (287862 => 287863)


--- trunk/Source/WebCore/dom/ImageOverlay.cpp	2022-01-10 23:42:34 UTC (rev 287862)
+++ trunk/Source/WebCore/dom/ImageOverlay.cpp	2022-01-10 23:48:40 UTC (rev 287863)
@@ -192,6 +192,7 @@
 struct LineElements {
     Ref<HTMLDivElement> line;
     Vector<Ref<HTMLElement>> children;
+    RefPtr<HTMLBRElement> lineBreak;
 };
 
 struct Elements {
@@ -253,10 +254,10 @@
             }
 
             ASSERT(classes.contains(imageOverlayLineClass()));
-            LineElements lineElements { childElement, { } };
+            Vector<Ref<HTMLElement>> lineChildren;
             for (auto& text : childrenOfType<HTMLDivElement>(childElement))
-                lineElements.children.append(text);
-            elements.lines.append(WTFMove(lineElements));
+                lineChildren.append(text);
+            elements.lines.append({ childElement, WTFMove(lineChildren), childrenOfType<HTMLBRElement>(childElement).first() });
         }
 
         bool canUseExistingElements = ([&] {
@@ -270,8 +271,14 @@
                 return false;
 
             for (size_t lineIndex = 0; lineIndex < result.lines.size(); ++lineIndex) {
-                auto& childResults = result.lines[lineIndex].children;
-                auto& childTextElements = elements.lines[lineIndex].children;
+                auto& lineResult = result.lines[lineIndex];
+                auto& childResults = lineResult.children;
+
+                auto& lineElements = elements.lines[lineIndex];
+                auto& childTextElements = lineElements.children;
+                if (lineResult.shouldWrap != !lineElements.lineBreak)
+                    return false;
+
                 if (childResults.size() != childTextElements.size())
                     return false;
 
@@ -317,7 +324,7 @@
             auto lineContainer = HTMLDivElement::create(document.get());
             lineContainer->classList().add(imageOverlayLineClass());
             rootContainer->appendChild(lineContainer);
-            LineElements lineElements { lineContainer, { } };
+            LineElements lineElements { lineContainer, { }, { } };
             lineElements.children.reserveInitialCapacity(line.children.size());
             for (size_t childIndex = 0; childIndex < line.children.size(); ++childIndex) {
                 auto& child = line.children[childIndex];
@@ -328,7 +335,11 @@
                 lineElements.children.uncheckedAppend(WTFMove(textContainer));
             }
 
-            lineContainer->appendChild(HTMLBRElement::create(document.get()));
+            if (!line.shouldWrap) {
+                lineElements.lineBreak = HTMLBRElement::create(document.get());
+                lineContainer->appendChild(*lineElements.lineBreak);
+            }
+
             elements.lines.uncheckedAppend(WTFMove(lineElements));
         }
 

Modified: trunk/Source/WebCore/platform/TextRecognitionResult.h (287862 => 287863)


--- trunk/Source/WebCore/platform/TextRecognitionResult.h	2022-01-10 23:42:34 UTC (rev 287862)
+++ trunk/Source/WebCore/platform/TextRecognitionResult.h	2022-01-10 23:48:40 UTC (rev 287863)
@@ -81,14 +81,16 @@
 }
 
 struct TextRecognitionLineData {
-    TextRecognitionLineData(FloatQuad&& quad, Vector<TextRecognitionWordData>&& theChildren)
+    TextRecognitionLineData(FloatQuad&& quad, Vector<TextRecognitionWordData>&& theChildren, bool wrap)
         : normalizedQuad(WTFMove(quad))
         , children(WTFMove(theChildren))
+        , shouldWrap(wrap)
     {
     }
 
     FloatQuad normalizedQuad;
     Vector<TextRecognitionWordData> children;
+    bool shouldWrap { false };
 
     template<class Encoder> void encode(Encoder&) const;
     template<class Decoder> static std::optional<TextRecognitionLineData> decode(Decoder&);
@@ -114,6 +116,7 @@
 {
     encoder << normalizedQuad;
     encoder << children;
+    encoder << shouldWrap;
 }
 
 template<class Decoder> std::optional<TextRecognitionLineData> TextRecognitionLineData::decode(Decoder& decoder)
@@ -128,7 +131,12 @@
     if (!children)
         return std::nullopt;
 
-    return {{ WTFMove(*normalizedQuad), WTFMove(*children) }};
+    std::optional<bool> shouldWrap;
+    decoder >> shouldWrap;
+    if (!shouldWrap)
+        return std::nullopt;
+
+    return { { WTFMove(*normalizedQuad), WTFMove(*children), *shouldWrap } };
 }
 
 struct TextRecognitionBlockData {

Modified: trunk/Source/WebCore/testing/Internals.cpp (287862 => 287863)


--- trunk/Source/WebCore/testing/Internals.cpp	2022-01-10 23:42:34 UTC (rev 287862)
+++ trunk/Source/WebCore/testing/Internals.cpp	2022-01-10 23:48:40 UTC (rev 287863)
@@ -5769,7 +5769,8 @@
         getQuad<Internals::ImageOverlayLine>(line),
         line.children.map([](auto& textChild) -> TextRecognitionWordData {
             return { textChild.text, getQuad<Internals::ImageOverlayText>(textChild), textChild.hasLeadingWhitespace };
-        })
+        }),
+        line.shouldWrap
     };
 }
 

Modified: trunk/Source/WebCore/testing/Internals.h (287862 => 287863)


--- trunk/Source/WebCore/testing/Internals.h	2022-01-10 23:42:34 UTC (rev 287862)
+++ trunk/Source/WebCore/testing/Internals.h	2022-01-10 23:48:40 UTC (rev 287863)
@@ -922,6 +922,7 @@
         RefPtr<DOMPointReadOnly> bottomRight;
         RefPtr<DOMPointReadOnly> bottomLeft;
         Vector<ImageOverlayText> children;
+        bool shouldWrap { false };
 
         ~ImageOverlayLine();
     };

Modified: trunk/Source/WebCore/testing/Internals.idl (287862 => 287863)


--- trunk/Source/WebCore/testing/Internals.idl	2022-01-10 23:42:34 UTC (rev 287862)
+++ trunk/Source/WebCore/testing/Internals.idl	2022-01-10 23:48:40 UTC (rev 287863)
@@ -292,6 +292,7 @@
     required DOMPointReadOnly bottomRight;
     required DOMPointReadOnly bottomLeft;
     sequence<ImageOverlayText> children;
+    boolean shouldWrap = false;
 };
 
 [

Modified: trunk/Source/WebKit/ChangeLog (287862 => 287863)


--- trunk/Source/WebKit/ChangeLog	2022-01-10 23:42:34 UTC (rev 287862)
+++ trunk/Source/WebKit/ChangeLog	2022-01-10 23:48:40 UTC (rev 287863)
@@ -1,3 +1,17 @@
+2022-01-10  Wenson Hsieh  <[email protected]>
+
+        Add support for a -shouldWrap property when injecting Live Text
+        https://bugs.webkit.org/show_bug.cgi?id=235035
+        rdar://85139146
+
+        Reviewed by Tim Horton.
+
+        Consult the new SPI property (behind a runtime selector check), and pass the boolean flag through the
+        TextRecognitionResult.
+
+        * Platform/cocoa/TextRecognitionUtilities.mm:
+        (WebKit::makeTextRecognitionResult):
+
 2022-01-10  Alex Christensen  <[email protected]>
 
         Start using C++20

Modified: trunk/Source/WebKit/Platform/cocoa/TextRecognitionUtilities.mm (287862 => 287863)


--- trunk/Source/WebKit/Platform/cocoa/TextRecognitionUtilities.mm	2022-01-10 23:42:34 UTC (rev 287862)
+++ trunk/Source/WebKit/Platform/cocoa/TextRecognitionUtilities.mm	2022-01-10 23:48:40 UTC (rev 287863)
@@ -114,7 +114,11 @@
             searchLocation = matchLocation + childText.length();
             children.uncheckedAppend({ WTFMove(childText), floatQuad(child.quad), hasLeadingWhitespace });
         }
-        result.lines.uncheckedAppend({ floatQuad(line.quad), WTFMove(children) });
+        result.lines.uncheckedAppend({
+            floatQuad(line.quad),
+            WTFMove(children),
+            [line respondsToSelector:@selector(shouldWrap)] && [line shouldWrap]
+        });
         isFirstLine = false;
     }
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to