<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[285862] trunk/Source/WebCore</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/285862">285862</a></dd>
<dt>Author</dt> <dd>wenson_hsieh@apple.com</dd>
<dt>Date</dt> <dd>2021-11-16 08:37:05 -0800 (Tue, 16 Nov 2021)</dd>
</dl>

<h3>Log Message</h3>
<pre>Add support for injecting and rendering text recognition blocks
https://bugs.webkit.org/show_bug.cgi?id=233044

Reviewed by Aditya Keerthi.

Adds support for rendering text recognition blocks, which appear as opaque div elements over images. See below
for more details; no change in behavior, since nothing currently generates TextRecognitionBlockData yet.

* dom/ImageOverlay.cpp:
(WebCore::ImageOverlay::imageOverlayDataDetectorClass):
(WebCore::ImageOverlay::imageOverlayBlockClass):
(WebCore::ImageOverlay::isDataDetectorResult):
(WebCore::ImageOverlay::updateSubtree):

Add support for creating text recognition block containers in the UA shadow root, if needed.

(WebCore::ImageOverlay::fitElementToQuad):

Factor out logic to adjust the width, height and transforms on a given element to fit the given quad, and return
rotated bounding rect info; we use this in three places below.

(WebCore::ImageOverlay::updateWithTextRecognitionResult):

Add support for adjusting the size and transforms on each of the block containers created above.

(WebCore::ImageOverlay::imageOverlayDataDetectorClassName): Deleted.

Rename this to just `imageOverlayDataDetectorClass()` to match the other static helper functions.

* html/shadow/imageOverlay.css:
(div#image-overlay):
(div.image-overlay-line):
(div.image-overlay-line, div.image-overlay-block):
(div.image-overlay-block):
(div.image-overlay-line, .image-overlay-text):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoredomImageOverlaycpp">trunk/Source/WebCore/dom/ImageOverlay.cpp</a></li>
<li><a href="#trunkSourceWebCorehtmlshadowimageOverlaycss">trunk/Source/WebCore/html/shadow/imageOverlay.css</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (285861 => 285862)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog   2021-11-16 15:24:18 UTC (rev 285861)
+++ trunk/Source/WebCore/ChangeLog      2021-11-16 16:37:05 UTC (rev 285862)
</span><span class="lines">@@ -1,3 +1,41 @@
</span><ins>+2021-11-16  Wenson Hsieh  <wenson_hsieh@apple.com>
+
+        Add support for injecting and rendering text recognition blocks
+        https://bugs.webkit.org/show_bug.cgi?id=233044
+
+        Reviewed by Aditya Keerthi.
+
+        Adds support for rendering text recognition blocks, which appear as opaque div elements over images. See below
+        for more details; no change in behavior, since nothing currently generates TextRecognitionBlockData yet.
+
+        * dom/ImageOverlay.cpp:
+        (WebCore::ImageOverlay::imageOverlayDataDetectorClass):
+        (WebCore::ImageOverlay::imageOverlayBlockClass):
+        (WebCore::ImageOverlay::isDataDetectorResult):
+        (WebCore::ImageOverlay::updateSubtree):
+
+        Add support for creating text recognition block containers in the UA shadow root, if needed.
+
+        (WebCore::ImageOverlay::fitElementToQuad):
+
+        Factor out logic to adjust the width, height and transforms on a given element to fit the given quad, and return
+        rotated bounding rect info; we use this in three places below.
+
+        (WebCore::ImageOverlay::updateWithTextRecognitionResult):
+
+        Add support for adjusting the size and transforms on each of the block containers created above.
+
+        (WebCore::ImageOverlay::imageOverlayDataDetectorClassName): Deleted.
+
+        Rename this to just `imageOverlayDataDetectorClass()` to match the other static helper functions.
+
+        * html/shadow/imageOverlay.css:
+        (div#image-overlay):
+        (div.image-overlay-line):
+        (div.image-overlay-line, div.image-overlay-block):
+        (div.image-overlay-block):
+        (div.image-overlay-line, .image-overlay-text):
+
</ins><span class="cx"> 2021-11-16  Andreu Botella  <andreu@andreubotella.com>
</span><span class="cx"> 
</span><span class="cx">         Empty <input type=file> is represented incorrectly in FormData
</span></span></pre></div>
<a id="trunkSourceWebCoredomImageOverlaycpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/ImageOverlay.cpp (285861 => 285862)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/ImageOverlay.cpp        2021-11-16 15:24:18 UTC (rev 285861)
+++ trunk/Source/WebCore/dom/ImageOverlay.cpp   2021-11-16 16:37:05 UTC (rev 285862)
</span><span class="lines">@@ -63,7 +63,7 @@
</span><span class="cx">     return identifier;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static const AtomString& imageOverlayDataDetectorClassName()
</del><ins>+static const AtomString& imageOverlayDataDetectorClass()
</ins><span class="cx"> {
</span><span class="cx">     static MainThreadNeverDestroyed<const AtomString> className("image-overlay-data-detector-result", AtomString::ConstructFromLiteral);
</span><span class="cx">     return className;
</span><span class="lines">@@ -83,6 +83,12 @@
</span><span class="cx">     return className;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static const AtomString& imageOverlayBlockClass()
+{
+    static MainThreadNeverDestroyed<const AtomString> className("image-overlay-block", AtomString::ConstructFromLiteral);
+    return className;
+}
+
</ins><span class="cx"> #endif // ENABLE(IMAGE_ANALYSIS)
</span><span class="cx"> 
</span><span class="cx"> bool hasOverlay(const HTMLElement& element)
</span><span class="lines">@@ -106,7 +112,7 @@
</span><span class="cx"> 
</span><span class="cx"> bool isDataDetectorResult(const HTMLElement& element)
</span><span class="cx"> {
</span><del>-    return imageOverlayHost(element) && element.hasClass() && element.classNames().contains(imageOverlayDataDetectorClassName());
</del><ins>+    return imageOverlayHost(element) && element.hasClass() && element.classNames().contains(imageOverlayDataDetectorClass());
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> bool isInsideOverlay(const SimpleRange& range)
</span><span class="lines">@@ -191,6 +197,7 @@
</span><span class="cx">     RefPtr<HTMLDivElement> root;
</span><span class="cx">     Vector<LineElements> lines;
</span><span class="cx">     Vector<Ref<HTMLDivElement>> dataDetectors;
</span><ins>+    Vector<Ref<HTMLDivElement>> blocks;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> static Elements updateSubtree(HTMLElement& element, const TextRecognitionResult& result)
</span><span class="lines">@@ -229,17 +236,26 @@
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     if (elements.root) {
</span><del>-        for (auto& lineOrDataDetector : childrenOfType<HTMLDivElement>(*elements.root)) {
-            if (!lineOrDataDetector.hasClass())
</del><ins>+        for (auto& childElement : childrenOfType<HTMLDivElement>(*elements.root)) {
+            if (!childElement.hasClass())
</ins><span class="cx">                 continue;
</span><span class="cx"> 
</span><del>-            if (lineOrDataDetector.classList().contains(imageOverlayLineClass())) {
-                LineElements lineElements { lineOrDataDetector, { } };
-                for (auto& text : childrenOfType<HTMLDivElement>(lineOrDataDetector))
-                    lineElements.children.append(text);
-                elements.lines.append(WTFMove(lineElements));
-            } else if (lineOrDataDetector.classList().contains(imageOverlayDataDetectorClassName()))
-                elements.dataDetectors.append(lineOrDataDetector);
</del><ins>+            auto& classes = childElement.classList();
+            if (classes.contains(imageOverlayDataDetectorClass())) {
+                elements.dataDetectors.append(childElement);
+                continue;
+            }
+
+            if (classes.contains(imageOverlayBlockClass())) {
+                elements.blocks.append(childElement);
+                continue;
+            }
+
+            ASSERT(classes.contains(imageOverlayLineClass()));
+            LineElements lineElements { childElement, { } };
+            for (auto& text : childrenOfType<HTMLDivElement>(childElement))
+                lineElements.children.append(text);
+            elements.lines.append(WTFMove(lineElements));
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         bool canUseExistingElements = ([&] {
</span><span class="lines">@@ -249,6 +265,9 @@
</span><span class="cx">             if (result.lines.size() != elements.lines.size())
</span><span class="cx">                 return false;
</span><span class="cx"> 
</span><ins>+            if (result.blocks.size() != elements.blocks.size())
+                return false;
+
</ins><span class="cx">             for (size_t lineIndex = 0; lineIndex < result.lines.size(); ++lineIndex) {
</span><span class="cx">                 auto& childResults = result.lines[lineIndex].children;
</span><span class="cx">                 auto& childTextElements = elements.lines[lineIndex].children;
</span><span class="lines">@@ -261,6 +280,11 @@
</span><span class="cx">                 }
</span><span class="cx">             }
</span><span class="cx"> 
</span><ins>+            for (size_t index = 0; index < result.blocks.size(); ++index) {
+                if (result.blocks[index].text != elements.blocks[index]->textContent())
+                    return false;
+            }
+
</ins><span class="cx">             return true;
</span><span class="cx">         })();
</span><span class="cx"> 
</span><span class="lines">@@ -310,12 +334,21 @@
</span><span class="cx">         elements.dataDetectors.reserveInitialCapacity(result.dataDetectors.size());
</span><span class="cx">         for (auto& dataDetector : result.dataDetectors) {
</span><span class="cx">             auto dataDetectorContainer = DataDetection::createElementForImageOverlay(document.get(), dataDetector);
</span><del>-            dataDetectorContainer->classList().add(imageOverlayDataDetectorClassName());
</del><ins>+            dataDetectorContainer->classList().add(imageOverlayDataDetectorClass());
</ins><span class="cx">             rootContainer->appendChild(dataDetectorContainer);
</span><span class="cx">             elements.dataDetectors.uncheckedAppend(WTFMove(dataDetectorContainer));
</span><span class="cx">         }
</span><span class="cx"> #endif // ENABLE(DATA_DETECTION)
</span><span class="cx"> 
</span><ins>+        elements.blocks.reserveInitialCapacity(result.blocks.size());
+        for (auto& block : result.blocks) {
+            auto blockContainer = HTMLDivElement::create(document.get());
+            blockContainer->classList().add(imageOverlayBlockClass());
+            rootContainer->appendChild(blockContainer);
+            blockContainer->appendChild(Text::create(document.get(), makeString('\n', block.text)));
+            elements.blocks.uncheckedAppend(WTFMove(blockContainer));
+        }
+
</ins><span class="cx">         if (document->quirks().needsToForceUserSelectWhenInstallingImageOverlay())
</span><span class="cx">             element.setInlineStyleProperty(CSSPropertyWebkitUserSelect, CSSValueText);
</span><span class="cx">     }
</span><span class="lines">@@ -330,6 +363,20 @@
</span><span class="cx">     return elements;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static RotatedRect fitElementToQuad(HTMLElement& container, const FloatQuad& quad)
+{
+    auto bounds = rotatedBoundingRectWithMinimumAngleOfRotation(quad, 0.01);
+    container.setInlineStyleProperty(CSSPropertyWidth, bounds.size.width(), CSSUnitType::CSS_PX);
+    container.setInlineStyleProperty(CSSPropertyHeight, bounds.size.height(), CSSUnitType::CSS_PX);
+    container.setInlineStyleProperty(CSSPropertyTransform, makeString(
+        "translate("_s,
+        std::round(bounds.center.x() - (bounds.size.width() / 2)), "px, "_s,
+        std::round(bounds.center.y() - (bounds.size.height() / 2)), "px) "_s,
+        bounds.angleInRadians ? makeString("rotate("_s, bounds.angleInRadians, "rad) "_s) : emptyString()
+    ));
+    return bounds;
+}
+
</ins><span class="cx"> void updateWithTextRecognitionResult(HTMLElement& element, const TextRecognitionResult& result, CacheTextRecognitionResults cacheTextRecognitionResults)
</span><span class="cx"> {
</span><span class="cx">     auto elements = updateSubtree(element, result);
</span><span class="lines">@@ -362,16 +409,7 @@
</span><span class="cx">         if (lineQuad.isEmpty())
</span><span class="cx">             continue;
</span><span class="cx"> 
</span><del>-        auto lineBounds = rotatedBoundingRectWithMinimumAngleOfRotation(lineQuad, 0.01);
-        lineContainer->setInlineStyleProperty(CSSPropertyWidth, lineBounds.size.width(), CSSUnitType::CSS_PX);
-        lineContainer->setInlineStyleProperty(CSSPropertyHeight, lineBounds.size.height(), CSSUnitType::CSS_PX);
-        lineContainer->setInlineStyleProperty(CSSPropertyTransform, makeString(
-            "translate("_s,
-            std::round(lineBounds.center.x() - (lineBounds.size.width() / 2)), "px, "_s,
-            std::round(lineBounds.center.y() - (lineBounds.size.height() / 2)), "px) "_s,
-            lineBounds.angleInRadians ? makeString("rotate("_s, lineBounds.angleInRadians, "rad) "_s) : emptyString()
-        ));
-
</del><ins>+        auto lineBounds = fitElementToQuad(lineContainer.get(), lineQuad);
</ins><span class="cx">         auto offsetAlongHorizontalAxis = [&](const FloatPoint& quadPoint1, const FloatPoint& quadPoint2) {
</span><span class="cx">             auto intervalLength = lineBounds.size.width();
</span><span class="cx">             auto mid = midPoint(quadPoint1, quadPoint2);
</span><span class="lines">@@ -451,20 +489,28 @@
</span><span class="cx">         if (dataDetector.normalizedQuads.isEmpty())
</span><span class="cx">             continue;
</span><span class="cx"> 
</span><ins>+        auto firstQuad = dataDetector.normalizedQuads.first();
+        if (firstQuad.isEmpty())
+            continue;
+
</ins><span class="cx">         // FIXME: We should come up with a way to coalesce the bounding quads into one or more rotated rects with the same angle of rotation.
</span><del>-        auto targetQuad = convertToContainerCoordinates(dataDetector.normalizedQuads.first());
-        auto targetBounds = rotatedBoundingRectWithMinimumAngleOfRotation(targetQuad, 0.01);
-        dataDetectorContainer->setInlineStyleProperty(CSSPropertyWidth, targetBounds.size.width(), CSSUnitType::CSS_PX);
-        dataDetectorContainer->setInlineStyleProperty(CSSPropertyHeight, targetBounds.size.height(), CSSUnitType::CSS_PX);
-        dataDetectorContainer->setInlineStyleProperty(CSSPropertyTransform, makeString(
-            "translate("_s,
-            std::round(targetBounds.center.x() - (targetBounds.size.width() / 2)), "px, "_s,
-            std::round(targetBounds.center.y() - (targetBounds.size.height() / 2)), "px) "_s,
-            targetBounds.angleInRadians ? makeString("rotate("_s, targetBounds.angleInRadians, "rad) "_s) : emptyString()
-        ));
</del><ins>+        fitElementToQuad(dataDetectorContainer.get(), convertToContainerCoordinates(firstQuad));
</ins><span class="cx">     }
</span><span class="cx"> #endif // ENABLE(DATA_DETECTION)
</span><span class="cx"> 
</span><ins>+    ASSERT(result.blocks.size() == elements.blocks.size());
+    for (size_t index = 0; index < result.blocks.size(); ++index) {
+        auto& block = result.blocks[index];
+        if (block.normalizedQuad.isEmpty())
+            continue;
+
+        auto blockContainer = elements.blocks[index];
+        auto bounds = fitElementToQuad(blockContainer.get(), convertToContainerCoordinates(block.normalizedQuad));
+        // FIXME: We'll need a smarter algorithm here that chooses the largest font size for the container without
+        // vertically overflowing the container.
+        blockContainer->setInlineStyleProperty(CSSPropertyFontSize, std::round(0.8 * bounds.size.height()), CSSUnitType::CSS_PX);
+    }
+
</ins><span class="cx">     if (RefPtr frame = document->frame())
</span><span class="cx">         frame->eventHandler().scheduleCursorUpdate();
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCorehtmlshadowimageOverlaycss"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/html/shadow/imageOverlay.css (285861 => 285862)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/html/shadow/imageOverlay.css        2021-11-16 15:24:18 UTC (rev 285861)
+++ trunk/Source/WebCore/html/shadow/imageOverlay.css   2021-11-16 16:37:05 UTC (rev 285862)
</span><span class="lines">@@ -30,21 +30,37 @@
</span><span class="cx">     color: transparent;
</span><span class="cx">     text-shadow: none;
</span><span class="cx">     text-align: center;
</span><ins>+    font-family: system-ui;
+}
+
+div.image-overlay-line {
</ins><span class="cx">     white-space: nowrap;
</span><span class="cx">     line-height: 100%;
</span><del>-    font-family: system-ui;
</del><span class="cx">     font-size: 1024px; /* This large font size is chosen to minimize gaps when painting selection quads. */
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-div.image-overlay-line, .image-overlay-text {
</del><ins>+div.image-overlay-line, div.image-overlay-block {
+    pointer-events: auto;
+}
+
+div.image-overlay-block {
+    background-color: rgba(255, 255, 255, 0.75);
+    border-radius: calc(clamp(2px, 0.1em, 12px));
+    box-shadow: rgba(100, 100, 100, 0.2) 3px 4px 8px 4px;
+    color: rgb(90, 90, 90);
+    font-weight: bold;
+    display: flex;
+    justify-content: center;
+    align-content: center;
+    flex-direction: column;
+    -webkit-backdrop-filter: blur(8px);
+}
+
+div.image-overlay-line, .image-overlay-text, div.image-overlay-block {
</ins><span class="cx">     position: absolute;
</span><span class="cx">     overflow: hidden;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-div.image-overlay-line {
-    pointer-events: auto;
-}
-
</del><span class="cx"> .image-overlay-text::selection {
</span><span class="cx">     color: transparent;
</span><span class="cx">     background-color: highlight;
</span></span></pre>
</div>
</div>

</body>
</html>