Changeset 279751 in webkit
- Timestamp:
- Jul 8, 2021 1:59:32 PM (13 months ago)
- Location:
- trunk
- Files:
-
- 2 added
- 10 edited
-
LayoutTests/ChangeLog (modified) (1 diff)
-
LayoutTests/fast/images/text-recognition/image-overlay-in-transparent-image-expected.txt (added)
-
LayoutTests/fast/images/text-recognition/image-overlay-in-transparent-image.html (added)
-
Source/WebCore/ChangeLog (modified) (1 diff)
-
Source/WebCore/html/HTMLElement.cpp (modified) (2 diffs)
-
Source/WebCore/html/HTMLElement.h (modified) (1 diff)
-
Source/WebCore/page/Page.cpp (modified) (1 diff)
-
Source/WebKit/ChangeLog (modified) (1 diff)
-
Source/WebKit/WebProcess/WebCoreSupport/ShareableBitmapUtilities.cpp (modified) (2 diffs)
-
Source/WebKit/WebProcess/WebCoreSupport/ShareableBitmapUtilities.h (modified) (1 diff)
-
Source/WebKit/WebProcess/WebPage/WebPage.cpp (modified) (1 diff)
-
Source/WebKit/WebProcess/WebPage/ios/WebPageIOS.mm (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/LayoutTests/ChangeLog
r279750 r279751 1 2021-07-08 Wenson Hsieh <wenson_hsieh@apple.com> 2 3 [Live Text] Selection is misaligned on some images on twitter.com 4 https://bugs.webkit.org/show_bug.cgi?id=227775 5 rdar://77142364 6 7 Reviewed by Tim Horton. 8 9 * fast/images/text-recognition/image-overlay-in-transparent-image-expected.txt: Added. 10 * fast/images/text-recognition/image-overlay-in-transparent-image.html: Added. 11 1 12 2021-07-08 Kate Cheney <katherine_cheney@apple.com> 2 13 -
trunk/Source/WebCore/ChangeLog
r279750 r279751 1 2021-07-08 Wenson Hsieh <wenson_hsieh@apple.com> 2 3 [Live Text] Selection is misaligned on some images on twitter.com 4 https://bugs.webkit.org/show_bug.cgi?id=227775 5 rdar://77142364 6 7 Reviewed by Tim Horton. 8 9 On Twitter, image thumbnails consist of fully transparent image elements (`opacity: 0;`) that are overlaid over 10 `div` elements with a background image. The images for some of these transparent image elements sometimes don't 11 match the background image behind it (namely, the image is stretched to exactly fit the bounds of the 12 transparent image element in a way that does not preserve aspect ratio, whereas the background image is cropped 13 to cover the `div`). As such, Live Text in these image elements ends up misaligned against the background image 14 (which is actually visible to the user). 15 16 To address this, special case fully transparent images such that we take a snapshot of the page using the 17 absolute bounds of the transparent image and run OCR over this snapshot, instead of using the image data as-is. 18 19 Test: fast/images/text-recognition/image-overlay-in-transparent-image.html 20 21 * html/HTMLElement.cpp: 22 (WebCore::HTMLElement::containerRectForTextRecognition): 23 (WebCore::HTMLElement::updateWithTextRecognitionResult): 24 * html/HTMLElement.h: 25 * page/Page.cpp: 26 (WebCore::Page::updateElementsWithTextRecognitionResults): 27 1 28 2021-07-08 Kate Cheney <katherine_cheney@apple.com> 2 29 -
trunk/Source/WebCore/html/HTMLElement.cpp
r279609 r279751 1341 1341 #if ENABLE(IMAGE_ANALYSIS) 1342 1342 1343 IntRect HTMLElement::containerRectForTextRecognition() 1344 { 1345 auto* renderer = this->renderer(); 1346 if (!is<RenderImage>(renderer)) 1347 return { }; 1348 1349 if (!renderer->opacity()) 1350 return { 0, 0, offsetWidth(), offsetHeight() }; 1351 1352 return enclosingIntRect(downcast<RenderImage>(*renderer).replacedContentRect()); 1353 } 1354 1343 1355 void HTMLElement::updateWithTextRecognitionResult(const TextRecognitionResult& result, CacheTextRecognitionResults cacheTextRecognitionResults) 1344 1356 { … … 1473 1485 downcast<RenderImage>(*renderer).setHasImageOverlay(); 1474 1486 1475 auto containerRect = enclosingIntRect(downcast<RenderImage>(*renderer).replacedContentRect());1487 auto containerRect = containerRectForTextRecognition(); 1476 1488 auto convertToContainerCoordinates = [&](const FloatQuad& normalizedQuad) { 1477 1489 auto quad = normalizedQuad; -
trunk/Source/WebCore/html/HTMLElement.h
r279108 r279751 140 140 141 141 #if ENABLE(IMAGE_ANALYSIS) 142 IntRect containerRectForTextRecognition(); 142 143 enum class CacheTextRecognitionResults : bool { No, Yes }; 143 144 WEBCORE_EXPORT void updateWithTextRecognitionResult(const TextRecognitionResult&, CacheTextRecognitionResults = CacheTextRecognitionResults::Yes); -
trunk/Source/WebCore/page/Page.cpp
r279349 r279751 3631 3631 continue; 3632 3632 3633 auto newContainerRect = enclosingIntRect(downcast<RenderImage>(*renderer).replacedContentRect());3633 auto newContainerRect = protectedElement->containerRectForTextRecognition(); 3634 3634 if (containerRect == newContainerRect) 3635 3635 continue; -
trunk/Source/WebKit/ChangeLog
r279750 r279751 1 2021-07-08 Wenson Hsieh <wenson_hsieh@apple.com> 2 3 [Live Text] Selection is misaligned on some images on twitter.com 4 https://bugs.webkit.org/show_bug.cgi?id=227775 5 rdar://77142364 6 7 Reviewed by Tim Horton. 8 9 See WebCore ChangeLog for more details. 10 11 * WebProcess/WebCoreSupport/ShareableBitmapUtilities.cpp: 12 (WebKit::createShareableBitmap): 13 * WebProcess/WebCoreSupport/ShareableBitmapUtilities.h: 14 (WebKit::createShareableBitmap): 15 16 Add an option to handle the scenario where the image renderer is fully transparent by falling back to a frame- 17 level snapshot, using the bounds of the image renderer. 18 19 * WebProcess/WebPage/WebPage.cpp: 20 (WebKit::WebPage::requestTextRecognition): 21 * WebProcess/WebPage/ios/WebPageIOS.mm: 22 (WebKit::imagePositionInformation): 23 (WebKit::elementPositionInformation): 24 1 25 2021-07-08 Kate Cheney <katherine_cheney@apple.com> 2 26 -
trunk/Source/WebKit/WebProcess/WebCoreSupport/ShareableBitmapUtilities.cpp
r278253 r279751 29 29 #include "ShareableBitmap.h" 30 30 #include <WebCore/CachedImage.h> 31 #include <WebCore/Frame.h> 32 #include <WebCore/FrameSnapshotting.h> 31 33 #include <WebCore/GeometryUtilities.h> 32 34 #include <WebCore/GraphicsContext.h> 35 #include <WebCore/ImageBuffer.h> 33 36 #include <WebCore/IntSize.h> 34 37 #include <WebCore/PlatformScreen.h> … … 38 41 using namespace WebCore; 39 42 40 RefPtr<ShareableBitmap> createShareableBitmap(RenderImage& renderImage, std::optional<FloatSize> screenSizeInPixels, AllowAnimatedImages allowAnimatedImages)43 RefPtr<ShareableBitmap> createShareableBitmap(RenderImage& renderImage, CreateShareableBitmapFromImageOptions&& options) 41 44 { 45 Ref frame = renderImage.frame(); 46 auto colorSpaceForBitmap = screenColorSpace(frame->mainFrame().view()); 47 if (!renderImage.opacity() && options.useSnapshotForTransparentImages == UseSnapshotForTransparentImages::Yes) { 48 auto snapshotRect = renderImage.absoluteBoundingBoxRect(); 49 if (snapshotRect.isEmpty()) 50 return { }; 51 52 OptionSet<SnapshotFlags> snapshotFlags { SnapshotFlags::ExcludeSelectionHighlighting, SnapshotFlags::PaintEverythingExcludingSelection }; 53 auto imageBuffer = snapshotFrameRect(frame.get(), snapshotRect, { snapshotFlags, PixelFormat::BGRA8, DestinationColorSpace::SRGB() }); 54 if (!imageBuffer) 55 return { }; 56 57 auto snapshotImage = ImageBuffer::sinkIntoImage(WTFMove(imageBuffer), PreserveResolution::Yes); 58 if (!snapshotImage) 59 return { }; 60 61 auto bitmap = ShareableBitmap::createShareable(snapshotRect.size(), { WTFMove(colorSpaceForBitmap) }); 62 if (!bitmap) 63 return { }; 64 65 auto context = bitmap->createGraphicsContext(); 66 if (!context) 67 return { }; 68 69 context->drawImage(*snapshotImage, { FloatPoint::zero(), snapshotRect.size() }); 70 return bitmap; 71 } 72 42 73 auto* cachedImage = renderImage.cachedImage(); 43 74 if (!cachedImage || cachedImage->errorOccurred()) 44 return nullptr;75 return { }; 45 76 46 77 auto* image = cachedImage->imageForRenderer(&renderImage); 47 78 if (!image || image->width() <= 1 || image->height() <= 1) 48 return nullptr;79 return { }; 49 80 50 if ( allowAnimatedImages == AllowAnimatedImages::No && image->isAnimated())51 return nullptr;81 if (options.allowAnimatedImages == AllowAnimatedImages::No && image->isAnimated()) 82 return { }; 52 83 53 84 auto bitmapSize = cachedImage->imageSizeForRenderer(&renderImage); 54 if ( screenSizeInPixels) {55 auto scaledSize = largestRectWithAspectRatioInsideRect(bitmapSize.width() / bitmapSize.height(), { FloatPoint(), * screenSizeInPixels }).size();85 if (options.screenSizeInPixels) { 86 auto scaledSize = largestRectWithAspectRatioInsideRect(bitmapSize.width() / bitmapSize.height(), { FloatPoint(), *options.screenSizeInPixels }).size(); 56 87 bitmapSize = scaledSize.width() < bitmapSize.width() ? scaledSize : bitmapSize; 57 88 } 58 89 59 90 // FIXME: Only select ExtendedColor on images known to need wide gamut. 60 auto sharedBitmap = ShareableBitmap::createShareable(IntSize(bitmapSize), { screenColorSpace(renderImage.frame().mainFrame().view()) });91 auto sharedBitmap = ShareableBitmap::createShareable(IntSize(bitmapSize), { WTFMove(colorSpaceForBitmap) }); 61 92 if (!sharedBitmap) 62 return nullptr;93 return { }; 63 94 64 95 auto graphicsContext = sharedBitmap->createGraphicsContext(); 65 96 if (!graphicsContext) 66 return nullptr;97 return { }; 67 98 68 99 graphicsContext->drawImage(*image, FloatRect(0, 0, bitmapSize.width(), bitmapSize.height()), { renderImage.imageOrientation() }); -
trunk/Source/WebKit/WebProcess/WebCoreSupport/ShareableBitmapUtilities.h
r278340 r279751 37 37 class ShareableBitmap; 38 38 39 enum class UseSnapshotForTransparentImages : bool { No, Yes }; 39 40 enum class AllowAnimatedImages : bool { No, Yes }; 40 RefPtr<ShareableBitmap> createShareableBitmap(WebCore::RenderImage&, std::optional<WebCore::FloatSize> screenSizeInPixels = std::nullopt, AllowAnimatedImages = AllowAnimatedImages::Yes); 41 42 struct CreateShareableBitmapFromImageOptions { 43 std::optional<WebCore::FloatSize> screenSizeInPixels; 44 AllowAnimatedImages allowAnimatedImages { AllowAnimatedImages::Yes }; 45 UseSnapshotForTransparentImages useSnapshotForTransparentImages { UseSnapshotForTransparentImages::No }; 46 }; 47 48 RefPtr<ShareableBitmap> createShareableBitmap(WebCore::RenderImage&, CreateShareableBitmapFromImageOptions&& = { }); 41 49 42 50 }; -
trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp
r279750 r279751 7407 7407 7408 7408 auto& renderImage = downcast<RenderImage>(*renderer); 7409 auto bitmap = createShareableBitmap(renderImage, std::nullopt, AllowAnimatedImages::No);7409 auto bitmap = createShareableBitmap(renderImage, { std::nullopt, AllowAnimatedImages::No, UseSnapshotForTransparentImages::Yes }); 7410 7410 if (!bitmap) { 7411 7411 if (completion) -
trunk/Source/WebKit/WebProcess/WebPage/ios/WebPageIOS.mm
r279562 r279751 2824 2824 2825 2825 if (request.includeSnapshot || request.includeImageData) 2826 info.image = createShareableBitmap(renderImage, screenSize() * page.corePage()->deviceScaleFactor());2826 info.image = createShareableBitmap(renderImage, { screenSize() * page.corePage()->deviceScaleFactor(), AllowAnimatedImages::Yes, UseSnapshotForTransparentImages::Yes }); 2827 2827 2828 2828 info.imageElementContext = page.contextForElement(element); … … 2886 2886 auto& [renderImage, image] = *rendererAndImage; 2887 2887 info.imageURL = element.document().completeURL(renderImage.cachedImage()->url().string()); 2888 info.image = createShareableBitmap(renderImage, screenSize() * page.corePage()->deviceScaleFactor());2888 info.image = createShareableBitmap(renderImage, { screenSize() * page.corePage()->deviceScaleFactor(), AllowAnimatedImages::Yes, UseSnapshotForTransparentImages::Yes }); 2889 2889 } 2890 2890 }
Note: See TracChangeset
for help on using the changeset viewer.