1 | /*
|
---|
2 | * Copyright (C) 2017 Apple Inc. All rights reserved.
|
---|
3 | *
|
---|
4 | * Redistribution and use in source and binary forms, with or without
|
---|
5 | * modification, are permitted provided that the following conditions
|
---|
6 | * are met:
|
---|
7 | * 1. Redistributions of source code must retain the above copyright
|
---|
8 | * notice, this list of conditions and the following disclaimer.
|
---|
9 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer in the
|
---|
11 | * documentation and/or other materials provided with the distribution.
|
---|
12 | *
|
---|
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
|
---|
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
---|
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
|
---|
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
---|
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
---|
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
---|
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
---|
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
---|
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
---|
23 | * THE POSSIBILITY OF SUCH DAMAGE.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #pragma once
|
---|
27 |
|
---|
28 | #include "DOMMatrixInit.h"
|
---|
29 | #include "ExceptionOr.h"
|
---|
30 | #include "ScriptWrappable.h"
|
---|
31 | #include "TransformationMatrix.h"
|
---|
32 | #include <JavaScriptCore/Forward.h>
|
---|
33 | #include <variant>
|
---|
34 | #include <wtf/RefCounted.h>
|
---|
35 | #include <wtf/Vector.h>
|
---|
36 | #include <wtf/text/WTFString.h>
|
---|
37 |
|
---|
38 | namespace WebCore {
|
---|
39 |
|
---|
40 | class DOMMatrix;
|
---|
41 | class DOMPoint;
|
---|
42 | class ScriptExecutionContext;
|
---|
43 | struct DOMPointInit;
|
---|
44 |
|
---|
45 | class DOMMatrixReadOnly : public ScriptWrappable, public RefCounted<DOMMatrixReadOnly> {
|
---|
46 | WTF_MAKE_ISO_ALLOCATED(DOMMatrixReadOnly);
|
---|
47 | public:
|
---|
48 | static ExceptionOr<Ref<DOMMatrixReadOnly>> create(ScriptExecutionContext&, std::optional<std::variant<String, Vector<double>>>&&);
|
---|
49 |
|
---|
50 | enum class Is2D { No, Yes };
|
---|
51 | static Ref<DOMMatrixReadOnly> create(const TransformationMatrix& matrix, Is2D is2D)
|
---|
52 | {
|
---|
53 | return adoptRef(*new DOMMatrixReadOnly(matrix, is2D));
|
---|
54 | }
|
---|
55 |
|
---|
56 | static Ref<DOMMatrixReadOnly> create(TransformationMatrix&& matrix, Is2D is2D)
|
---|
57 | {
|
---|
58 | return adoptRef(*new DOMMatrixReadOnly(WTFMove(matrix), is2D));
|
---|
59 | }
|
---|
60 |
|
---|
61 | static ExceptionOr<Ref<DOMMatrixReadOnly>> fromMatrix(DOMMatrixInit&&);
|
---|
62 |
|
---|
63 | static ExceptionOr<Ref<DOMMatrixReadOnly>> fromFloat32Array(Ref<Float32Array>&&);
|
---|
64 | static ExceptionOr<Ref<DOMMatrixReadOnly>> fromFloat64Array(Ref<Float64Array>&&);
|
---|
65 |
|
---|
66 | static ExceptionOr<void> validateAndFixup(DOMMatrix2DInit&);
|
---|
67 | static ExceptionOr<void> validateAndFixup(DOMMatrixInit&);
|
---|
68 |
|
---|
69 | double a() const { return m_matrix.a(); }
|
---|
70 | double b() const { return m_matrix.b(); }
|
---|
71 | double c() const { return m_matrix.c(); }
|
---|
72 | double d() const { return m_matrix.d(); }
|
---|
73 | double e() const { return m_matrix.e(); }
|
---|
74 | double f() const { return m_matrix.f(); }
|
---|
75 |
|
---|
76 | double m11() const { return m_matrix.m11(); }
|
---|
77 | double m12() const { return m_matrix.m12(); }
|
---|
78 | double m13() const { return m_matrix.m13(); }
|
---|
79 | double m14() const { return m_matrix.m14(); }
|
---|
80 | double m21() const { return m_matrix.m21(); }
|
---|
81 | double m22() const { return m_matrix.m22(); }
|
---|
82 | double m23() const { return m_matrix.m23(); }
|
---|
83 | double m24() const { return m_matrix.m24(); }
|
---|
84 | double m31() const { return m_matrix.m31(); }
|
---|
85 | double m32() const { return m_matrix.m32(); }
|
---|
86 | double m33() const { return m_matrix.m33(); }
|
---|
87 | double m34() const { return m_matrix.m34(); }
|
---|
88 | double m41() const { return m_matrix.m41(); }
|
---|
89 | double m42() const { return m_matrix.m42(); }
|
---|
90 | double m43() const { return m_matrix.m43(); }
|
---|
91 | double m44() const { return m_matrix.m44(); }
|
---|
92 |
|
---|
93 | bool is2D() const { return m_is2D; }
|
---|
94 | bool isIdentity() const;
|
---|
95 |
|
---|
96 | ExceptionOr<void> setMatrixValue(const String&);
|
---|
97 | ExceptionOr<void> setMatrixValue(const Vector<double>&);
|
---|
98 |
|
---|
99 | Ref<DOMMatrix> translate(double tx = 0, double ty = 0, double tz = 0);
|
---|
100 | ExceptionOr<Ref<DOMMatrix>> multiply(DOMMatrixInit&& other) const;
|
---|
101 | Ref<DOMMatrix> flipX();
|
---|
102 | Ref<DOMMatrix> flipY();
|
---|
103 | Ref<DOMMatrix> scale(double scaleX = 1, std::optional<double> scaleY = std::nullopt, double scaleZ = 1, double originX = 0, double originY = 0, double originZ = 0);
|
---|
104 | Ref<DOMMatrix> scale3d(double scale = 1, double originX = 0, double originY = 0, double originZ = 0);
|
---|
105 | Ref<DOMMatrix> rotate(double rotX = 0, std::optional<double> rotY = std::nullopt, std::optional<double> rotZ = std::nullopt); // Angles are in degrees.
|
---|
106 | Ref<DOMMatrix> rotateFromVector(double x = 0, double y = 0);
|
---|
107 | Ref<DOMMatrix> rotateAxisAngle(double x = 0, double y = 0, double z = 0, double angle = 0); // Angle is in degrees.
|
---|
108 | Ref<DOMMatrix> skewX(double sx = 0); // Angle is in degrees.
|
---|
109 | Ref<DOMMatrix> skewY(double sy = 0); // Angle is in degrees.
|
---|
110 | Ref<DOMMatrix> inverse() const;
|
---|
111 |
|
---|
112 | Ref<DOMPoint> transformPoint(DOMPointInit&&);
|
---|
113 |
|
---|
114 | ExceptionOr<Ref<Float32Array>> toFloat32Array() const;
|
---|
115 | ExceptionOr<Ref<Float64Array>> toFloat64Array() const;
|
---|
116 |
|
---|
117 | ExceptionOr<String> toString() const;
|
---|
118 |
|
---|
119 | const TransformationMatrix& transformationMatrix() const { return m_matrix; }
|
---|
120 |
|
---|
121 | Ref<DOMMatrix> cloneAsDOMMatrix() const;
|
---|
122 |
|
---|
123 | protected:
|
---|
124 | DOMMatrixReadOnly() = default;
|
---|
125 | DOMMatrixReadOnly(const TransformationMatrix&, Is2D);
|
---|
126 | DOMMatrixReadOnly(TransformationMatrix&&, Is2D);
|
---|
127 |
|
---|
128 | struct AbstractMatrix {
|
---|
129 | TransformationMatrix matrix;
|
---|
130 | bool is2D { true };
|
---|
131 | };
|
---|
132 |
|
---|
133 | static ExceptionOr<AbstractMatrix> parseStringIntoAbstractMatrix(const String&);
|
---|
134 |
|
---|
135 | template <typename T>
|
---|
136 | static ExceptionOr<Ref<T>> fromMatrixHelper(DOMMatrixInit&&);
|
---|
137 |
|
---|
138 | TransformationMatrix m_matrix;
|
---|
139 | bool m_is2D { true };
|
---|
140 | };
|
---|
141 |
|
---|
142 | // https://drafts.fxtf.org/geometry/#create-a-dommatrix-from-the-dictionary
|
---|
143 | template<typename T>
|
---|
144 | inline ExceptionOr<Ref<T>> DOMMatrixReadOnly::fromMatrixHelper(DOMMatrixInit&& init)
|
---|
145 | {
|
---|
146 | auto result = validateAndFixup(init);
|
---|
147 | if (result.hasException())
|
---|
148 | return result.releaseException();
|
---|
149 | if (init.is2D.value())
|
---|
150 | return T::create(TransformationMatrix { init.m11.value(), init.m12.value(), init.m21.value(), init.m22.value(), init.m41.value(), init.m42.value() }, Is2D::Yes);
|
---|
151 | return T::create(TransformationMatrix {
|
---|
152 | init.m11.value(), init.m12.value(), init.m13, init.m14,
|
---|
153 | init.m21.value(), init.m22.value(), init.m23, init.m24,
|
---|
154 | init.m31, init.m32, init.m33, init.m34,
|
---|
155 | init.m41.value(), init.m42.value(), init.m43, init.m44
|
---|
156 | }, Is2D::No);
|
---|
157 | }
|
---|
158 |
|
---|
159 | } // namespace WebCore
|
---|