Diff
Modified: trunk/Source/WebCore/ChangeLog (273710 => 273711)
--- trunk/Source/WebCore/ChangeLog 2021-03-02 01:22:52 UTC (rev 273710)
+++ trunk/Source/WebCore/ChangeLog 2021-03-02 01:32:58 UTC (rev 273711)
@@ -1,3 +1,36 @@
+2021-03-01 Wenson Hsieh <[email protected]>
+
+ Support rotated image overlay text for non-rectilinear text quads
+ https://bugs.webkit.org/show_bug.cgi?id=222535
+ <rdar://problem/74886200>
+
+ Reviewed by Tim Horton.
+
+ Add support for rendering image overlay elements at rotated angles. To achieve this, we add a new helper
+ function to `GeometryUtilities.h`, `rotatedBoundingRect`, that takes a `FloatQuad` and returns a struct that
+ describes a (potentially) rotated bounding rect that covers all the points in the quad.
+
+ Tests: FloatQuad.RotatedBoundingRectForPoint
+ FloatQuad.RotatedBoundingRectForLineSegment
+ FloatQuad.RotatedBoundingRectForTriangle
+ FloatQuad.RotatedBoundingRectForTrapezoid
+ FloatQuad.RotatedBoundingRectWithMinimumAngle
+
+ * html/HTMLElement.cpp:
+ (WebCore::HTMLElement::updateWithImageExtractionResult):
+
+ Add a slight (0.01 rad) tolerance here, such we won't attempt to apply a rotation transformation if the text
+ quad is aligned (or nearly aligned) with the unit vector (1, 0).
+
+ * platform/graphics/GeometryUtilities.cpp:
+ (WebCore::midPoint):
+ (WebCore::dotProduct):
+ (WebCore::angleBetweenVectors):
+ (WebCore::rotatedBoundingRect):
+ * platform/graphics/GeometryUtilities.h:
+ * platform/graphics/PathTraversalState.cpp:
+ (WebCore::midPoint): Deleted.
+
2021-03-01 Truitt Savell <[email protected]>
Unreviewed, reverting r273655.
Modified: trunk/Source/WebCore/html/HTMLElement.cpp (273710 => 273711)
--- trunk/Source/WebCore/html/HTMLElement.cpp 2021-03-02 01:22:52 UTC (rev 273710)
+++ trunk/Source/WebCore/html/HTMLElement.cpp 2021-03-02 01:32:58 UTC (rev 273711)
@@ -42,6 +42,7 @@
#include "Frame.h"
#include "FrameLoader.h"
#include "FrameView.h"
+#include "GeometryUtilities.h"
#include "HTMLBDIElement.h"
#include "HTMLBRElement.h"
#include "HTMLButtonElement.h"
@@ -1273,17 +1274,20 @@
if (originalSize.isEmpty())
continue;
- auto targetRect = data.normalizedQuad.boundingBox();
- targetRect.scale(containerSize);
+ auto targetQuad = data.normalizedQuad;
+ targetQuad.scale(containerSize.width(), containerSize.height());
- IntPoint translationOffset {
- static_cast<int>(targetRect.x() + (targetRect.width() - originalSize.width()) / 2),
- static_cast<int>(targetRect.y() + (targetRect.height() - originalSize.height()) / 2)
- };
+ auto rotatedRect = rotatedBoundingRect(targetQuad, 0.01);
+ auto scale = rotatedRect.size / originalSize;
+ String rotationTransformationAsText;
+ if (rotatedRect.angleInRadians)
+ rotationTransformationAsText = makeString("rotate(", rotatedRect.angleInRadians, "rad) "_s);
+ auto offset = roundedIntPoint(rotatedRect.center - (originalSize / 2));
child->setInlineStyleProperty(CSSPropertyTransform, makeString(
- "translate("_s, translationOffset.x(), "px, "_s, translationOffset.y(), "px) "_s
- "scale("_s, targetRect.width() / originalSize.width(), ", "_s, targetRect.height() / originalSize.height(), ")"_s
+ "translate("_s, offset.x(), "px, "_s, offset.y(), "px) "_s,
+ rotationTransformationAsText,
+ "scale("_s, scale.width(), ", "_s, scale.height(), ") "_s
));
}
Modified: trunk/Source/WebCore/platform/graphics/GeometryUtilities.cpp (273710 => 273711)
--- trunk/Source/WebCore/platform/graphics/GeometryUtilities.cpp 2021-03-02 01:22:52 UTC (rev 273710)
+++ trunk/Source/WebCore/platform/graphics/GeometryUtilities.cpp 2021-03-02 01:32:58 UTC (rev 273711)
@@ -25,6 +25,9 @@
#include "config.h"
#include "GeometryUtilities.h"
+
+#include "FloatQuad.h"
+#include <wtf/MathExtras.h>
#include <wtf/Vector.h>
namespace WebCore {
@@ -199,4 +202,58 @@
return std::abs(transformedPoint.x()) + std::abs(transformedPoint.y()) <= transformedRadius || transformedPoint.lengthSquared() <= transformedRadius * transformedRadius;
}
+FloatPoint midPoint(const FloatPoint& first, const FloatPoint& second)
+{
+ return { (first.x() + second.x()) / 2, (first.y() + second.y()) / 2 };
}
+
+static float dotProduct(const FloatSize& u, const FloatSize& v)
+{
+ return u.width() * v.width() + u.height() * v.height();
+}
+
+static float angleBetweenVectors(const FloatSize& u, const FloatSize& v)
+{
+ auto magnitudes = u.diagonalLength() * v.diagonalLength();
+ return magnitudes ? acos(clampTo<float>(dotProduct(u, v) / magnitudes, -1, 1)) : 0;
+}
+
+RotatedRect rotatedBoundingRect(const FloatQuad& quad, Optional<float> minRotationInRadians)
+{
+ constexpr auto twoPiFloat = 2 * piFloat;
+
+ auto minRotationAmount = minRotationInRadians.valueOr(std::numeric_limits<float>::epsilon());
+
+ auto leftMidPoint = midPoint(quad.p1(), quad.p4());
+ auto rightMidPoint = midPoint(quad.p2(), quad.p3());
+ auto widthVector = rightMidPoint - leftMidPoint;
+
+ auto midPointToMidPointDistance = widthVector.diagonalLength();
+ int signOfWidthVectorHeight = widthVector.height() < 0 ? -1 : 1;
+ auto angle = midPointToMidPointDistance ? signOfWidthVectorHeight * acos(widthVector.width() / midPointToMidPointDistance) : 0;
+ if (angle < 0)
+ angle += twoPiFloat;
+
+ if (std::abs(angle) < minRotationAmount || std::abs(twoPiFloat - angle) < minRotationAmount) {
+ auto boundingBox = quad.boundingBox();
+ return { boundingBox.center(), boundingBox.size(), 0 };
+ }
+
+ auto heightVector = FloatSize { widthVector.height(), -widthVector.width() };
+ auto leftPerpendicularAngle = angleBetweenVectors(heightVector, quad.p1() - leftMidPoint);
+ auto rightPerpendicularAngle = angleBetweenVectors(heightVector, quad.p2() - rightMidPoint);
+
+ auto leftHypotenuseLength = (leftMidPoint - quad.p1()).diagonalLength();
+ auto rightHypotenuseLength = (rightMidPoint - quad.p2()).diagonalLength();
+
+ auto leftMargin = leftHypotenuseLength * sin(leftPerpendicularAngle);
+ auto rightMargin = rightHypotenuseLength * sin(rightPerpendicularAngle);
+ auto width = midPointToMidPointDistance + leftMargin + rightMargin;
+ auto height = 2 * std::max(leftHypotenuseLength * cos(leftPerpendicularAngle), rightHypotenuseLength * cos(rightPerpendicularAngle));
+
+ auto leftMidToCenterDistance = (midPointToMidPointDistance + rightMargin - leftMargin) / 2;
+ auto center = leftMidPoint + (widthVector * leftMidToCenterDistance / midPointToMidPointDistance);
+ return { center, { width, height }, angle };
+}
+
+}
Modified: trunk/Source/WebCore/platform/graphics/GeometryUtilities.h (273710 => 273711)
--- trunk/Source/WebCore/platform/graphics/GeometryUtilities.h 2021-03-02 01:22:52 UTC (rev 273710)
+++ trunk/Source/WebCore/platform/graphics/GeometryUtilities.h 2021-03-02 01:32:58 UTC (rev 273711)
@@ -31,6 +31,8 @@
namespace WebCore {
+class FloatQuad;
+
float euclidianDistance(const FloatPoint&, const FloatPoint&);
float findSlope(const FloatPoint& p1, const FloatPoint& p2, float& c);
@@ -57,4 +59,15 @@
FloatRect boundsOfRotatingRect(const FloatRect&);
bool ellipseContainsPoint(const FloatPoint& center, const FloatSize& radii, const FloatPoint&);
+
+FloatPoint midPoint(const FloatPoint&, const FloatPoint&);
+
+struct RotatedRect {
+ FloatPoint center;
+ FloatSize size;
+ float angleInRadians;
+};
+
+WEBCORE_EXPORT RotatedRect rotatedBoundingRect(const FloatQuad&, Optional<float> minRotationInRadians = WTF::nullopt);
+
}
Modified: trunk/Source/WebCore/platform/graphics/PathTraversalState.cpp (273710 => 273711)
--- trunk/Source/WebCore/platform/graphics/PathTraversalState.cpp 2021-03-02 01:22:52 UTC (rev 273710)
+++ trunk/Source/WebCore/platform/graphics/PathTraversalState.cpp 2021-03-02 01:32:58 UTC (rev 273711)
@@ -21,6 +21,7 @@
#include "config.h"
#include "PathTraversalState.h"
+#include "GeometryUtilities.h"
#include <wtf/MathExtras.h>
#include <wtf/Vector.h>
@@ -28,11 +29,6 @@
static const float kPathSegmentLengthTolerance = 0.00001f;
-static inline FloatPoint midPoint(const FloatPoint& first, const FloatPoint& second)
-{
- return FloatPoint((first.x() + second.x()) / 2.0f, (first.y() + second.y()) / 2.0f);
-}
-
static inline float distanceLine(const FloatPoint& start, const FloatPoint& end)
{
return std::hypot(end.x() - start.x(), end.y() - start.y());
Modified: trunk/Tools/ChangeLog (273710 => 273711)
--- trunk/Tools/ChangeLog 2021-03-02 01:22:52 UTC (rev 273710)
+++ trunk/Tools/ChangeLog 2021-03-02 01:32:58 UTC (rev 273711)
@@ -1,3 +1,20 @@
+2021-03-01 Wenson Hsieh <[email protected]>
+
+ Support rotated image overlay text for non-rectilinear text quads
+ https://bugs.webkit.org/show_bug.cgi?id=222535
+ <rdar://problem/74886200>
+
+ Reviewed by Tim Horton.
+
+ Add a new API test file to exercise the new `rotatedBoundingRect` function, with tests to cover a few degenerate
+ cases such as a single point, line segments, and triangles.
+
+ * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+ * TestWebKitAPI/Tests/WebCore/FloatQuadTests.cpp: Added.
+ (TestWebKitAPI::areApproximatelyEqual):
+ (TestWebKitAPI::expectRotatedRect):
+ (TestWebKitAPI::TEST):
+
2021-03-01 Chris Dumez <[email protected]>
Crash under WebProcessPool::pageBeginUsingWebsiteDataStore()
Modified: trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj (273710 => 273711)
--- trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj 2021-03-02 01:22:52 UTC (rev 273710)
+++ trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj 2021-03-02 01:32:58 UTC (rev 273711)
@@ -1194,6 +1194,7 @@
F4A32ECB1F0643370047C544 /* contenteditable-in-iframe.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = F4A32ECA1F0642F40047C544 /* contenteditable-in-iframe.html */; };
F4A9202F1FEE34E900F59590 /* apple-data-url.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = F4A9202E1FEE34C800F59590 /* apple-data-url.html */; };
F4AB578A1F65165400DB0DA1 /* custom-draggable-div.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = F4AB57891F65164B00DB0DA1 /* custom-draggable-div.html */; };
+ F4AD183825ED791500B1A19F /* FloatQuadTests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4AD183725ED791500B1A19F /* FloatQuadTests.cpp */; };
F4B0168325AE060F00E445C4 /* DisableSpellcheck.mm in Sources */ = {isa = PBXBuildFile; fileRef = F4B0168225AE060F00E445C4 /* DisableSpellcheck.mm */; };
F4B0168425AE08F800E445C4 /* DisableSpellcheckPlugIn.mm in Sources */ = {isa = PBXBuildFile; fileRef = F4B0167F25AE02D600E445C4 /* DisableSpellcheckPlugIn.mm */; };
F4B825D81EF4DBFB006E417F /* compressed-files.zip in Copy Resources */ = {isa = PBXBuildFile; fileRef = F4B825D61EF4DBD4006E417F /* compressed-files.zip */; };
@@ -2980,6 +2981,8 @@
F4A32ECA1F0642F40047C544 /* contenteditable-in-iframe.html */ = {isa = PBXFileReference; lastKnownFileType = text.html; path = "contenteditable-in-iframe.html"; sourceTree = "<group>"; };
F4A9202E1FEE34C800F59590 /* apple-data-url.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "apple-data-url.html"; sourceTree = "<group>"; };
F4AB57891F65164B00DB0DA1 /* custom-draggable-div.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "custom-draggable-div.html"; sourceTree = "<group>"; };
+ F4AD183625ED791500B1A19F /* FloatQuadTests.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FloatQuadTests.h; sourceTree = "<group>"; };
+ F4AD183725ED791500B1A19F /* FloatQuadTests.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = FloatQuadTests.cpp; sourceTree = "<group>"; };
F4B0167F25AE02D600E445C4 /* DisableSpellcheckPlugIn.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = DisableSpellcheckPlugIn.mm; sourceTree = "<group>"; };
F4B0168225AE060F00E445C4 /* DisableSpellcheck.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = DisableSpellcheck.mm; sourceTree = "<group>"; };
F4B825D61EF4DBD4006E417F /* compressed-files.zip */ = {isa = PBXFileReference; lastKnownFileType = archive.zip; path = "compressed-files.zip"; sourceTree = "<group>"; };
@@ -3587,6 +3590,8 @@
572B40352176A029000AD43E /* FidoTestData.h */,
7A32D7491F02151500162C44 /* FileMonitor.cpp */,
7A909A701D877475007E10F8 /* FloatPointTests.cpp */,
+ F4AD183725ED791500B1A19F /* FloatQuadTests.cpp */,
+ F4AD183625ED791500B1A19F /* FloatQuadTests.h */,
7A909A711D877475007E10F8 /* FloatRectTests.cpp */,
7A909A721D877475007E10F8 /* FloatSizeTests.cpp */,
8E4A85361E1D1AA100F53B0F /* GridPosition.cpp */,
@@ -5300,6 +5305,7 @@
118153482208BADF00B2CCD2 /* FirstVisuallyNonEmptyMilestone.mm in Sources */,
7C83E0BC1D0A650700FEBCF3 /* FixedLayoutSize.mm in Sources */,
7A909A7E1D877480007E10F8 /* FloatPointTests.cpp in Sources */,
+ F4AD183825ED791500B1A19F /* FloatQuadTests.cpp in Sources */,
7A909A7F1D877480007E10F8 /* FloatRectTests.cpp in Sources */,
7A909A801D877480007E10F8 /* FloatSizeTests.cpp in Sources */,
F4BC0B142146C849002A0478 /* FocusPreservationTests.mm in Sources */,
Added: trunk/Tools/TestWebKitAPI/Tests/WebCore/FloatQuadTests.cpp (0 => 273711)
--- trunk/Tools/TestWebKitAPI/Tests/WebCore/FloatQuadTests.cpp (rev 0)
+++ trunk/Tools/TestWebKitAPI/Tests/WebCore/FloatQuadTests.cpp 2021-03-02 01:32:58 UTC (rev 273711)
@@ -0,0 +1,112 @@
+/*
+ * Copyright (C) 2021 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+
+#include <WebCore/FloatQuad.h>
+#include <WebCore/GeometryUtilities.h>
+
+namespace TestWebKitAPI {
+using namespace WebCore;
+
+static bool areApproximatelyEqual(float a, float b)
+{
+ return std::abs(a - b) < 0.001;
+}
+
+static void expectRotatedRect(const FloatPoint& center, const FloatSize& size, float angle, const RotatedRect& info)
+{
+ areApproximatelyEqual(info.angleInRadians, angle);
+ areApproximatelyEqual(info.center.x(), center.x());
+ areApproximatelyEqual(info.center.y(), center.y());
+ areApproximatelyEqual(info.size.width(), size.width());
+ areApproximatelyEqual(info.size.height(), size.height());
+}
+
+TEST(FloatQuad, RotatedBoundingRectForPoint)
+{
+ expectRotatedRect({ 1, -5 }, { 0, 0 }, 0, rotatedBoundingRect({
+ FloatPoint(1, -5),
+ FloatPoint(1, -5),
+ FloatPoint(1, -5),
+ FloatPoint(1, -5)
+ }));
+}
+
+TEST(FloatQuad, RotatedBoundingRectForLineSegment)
+{
+ expectRotatedRect({ 0, -5 }, { 20, 0 }, 0, rotatedBoundingRect({
+ FloatPoint(-10, -5),
+ FloatPoint(10, -5),
+ FloatPoint(10, -5),
+ FloatPoint(-10, -5)
+ }));
+}
+
+TEST(FloatQuad, RotatedBoundingRectForTriangle)
+{
+ expectRotatedRect({ 10, 0 }, { 40, 20 }, 0, rotatedBoundingRect({
+ FloatPoint(-10, -10),
+ FloatPoint(30, 0),
+ FloatPoint(30, 0),
+ FloatPoint(-10, 10)
+ }));
+}
+
+TEST(FloatQuad, RotatedBoundingRectForTrapezoid)
+{
+ expectRotatedRect({ 25, 15 }, { 28.284, 14.142 }, 5.498, rotatedBoundingRect({
+ FloatPoint(10, 20),
+ FloatPoint(20, 10),
+ FloatPoint(40, 10),
+ FloatPoint(20, 30)
+ }));
+
+ expectRotatedRect({ 20, 25 }, { 26.83, 13.42 }, 1.107, rotatedBoundingRect({
+ FloatPoint(20, 10),
+ FloatPoint(30, 30),
+ FloatPoint(20, 40),
+ FloatPoint(10, 20)
+ }));
+}
+
+TEST(FloatQuad, RotatedBoundingRectWithMinimumAngle)
+{
+ expectRotatedRect({ 20, -14.95 }, { 20, 10.1 }, 0, rotatedBoundingRect({
+ FloatPoint(10, -20),
+ FloatPoint(30, -19.9),
+ FloatPoint(30, -9.9),
+ FloatPoint(10, -10)
+ }, 0.01));
+
+ expectRotatedRect({ 20, -15.05 }, { 20, 10.1 }, 0, rotatedBoundingRect({
+ FloatPoint(10, -20),
+ FloatPoint(30, -20.1),
+ FloatPoint(30, -10.1),
+ FloatPoint(10, -10)
+ }, 0.01));
+}
+
+} // namespace TestWebKitAPI