| 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/Float32Array.h> |
| 33 | #include <JavaScriptCore/Float64Array.h> |
| 34 | #include <wtf/RefCounted.h> |
| 35 | #include <wtf/Variant.h> |
| 36 | #include <wtf/Vector.h> |
| 37 | #include <wtf/text/WTFString.h> |
| 38 | |
| 39 | namespace WebCore { |
| 40 | |
| 41 | class DOMMatrix; |
| 42 | class DOMPoint; |
| 43 | class ScriptExecutionContext; |
| 44 | struct DOMPointInit; |
| 45 | |
| 46 | class DOMMatrixReadOnly : public ScriptWrappable, public RefCounted<DOMMatrixReadOnly> { |
| 47 | WTF_MAKE_ISO_ALLOCATED(DOMMatrixReadOnly); |
| 48 | public: |
| 49 | static ExceptionOr<Ref<DOMMatrixReadOnly>> create(ScriptExecutionContext&, Optional<Variant<String, Vector<double>>>&&); |
| 50 | |
| 51 | enum class Is2D { No, Yes }; |
| 52 | static Ref<DOMMatrixReadOnly> create(const TransformationMatrix& matrix, Is2D is2D) |
| 53 | { |
| 54 | return adoptRef(*new DOMMatrixReadOnly(matrix, is2D)); |
| 55 | } |
| 56 | |
| 57 | static Ref<DOMMatrixReadOnly> create(TransformationMatrix&& matrix, Is2D is2D) |
| 58 | { |
| 59 | return adoptRef(*new DOMMatrixReadOnly(WTFMove(matrix), is2D)); |
| 60 | } |
| 61 | |
| 62 | static ExceptionOr<Ref<DOMMatrixReadOnly>> fromMatrix(DOMMatrixInit&&); |
| 63 | |
| 64 | static ExceptionOr<Ref<DOMMatrixReadOnly>> fromFloat32Array(Ref<Float32Array>&&); |
| 65 | static ExceptionOr<Ref<DOMMatrixReadOnly>> fromFloat64Array(Ref<Float64Array>&&); |
| 66 | |
| 67 | static ExceptionOr<void> validateAndFixup(DOMMatrix2DInit&); |
| 68 | static ExceptionOr<void> validateAndFixup(DOMMatrixInit&); |
| 69 | |
| 70 | double a() const { return m_matrix.a(); } |
| 71 | double b() const { return m_matrix.b(); } |
| 72 | double c() const { return m_matrix.c(); } |
| 73 | double d() const { return m_matrix.d(); } |
| 74 | double e() const { return m_matrix.e(); } |
| 75 | double f() const { return m_matrix.f(); } |
| 76 | |
| 77 | double m11() const { return m_matrix.m11(); } |
| 78 | double m12() const { return m_matrix.m12(); } |
| 79 | double m13() const { return m_matrix.m13(); } |
| 80 | double m14() const { return m_matrix.m14(); } |
| 81 | double m21() const { return m_matrix.m21(); } |
| 82 | double m22() const { return m_matrix.m22(); } |
| 83 | double m23() const { return m_matrix.m23(); } |
| 84 | double m24() const { return m_matrix.m24(); } |
| 85 | double m31() const { return m_matrix.m31(); } |
| 86 | double m32() const { return m_matrix.m32(); } |
| 87 | double m33() const { return m_matrix.m33(); } |
| 88 | double m34() const { return m_matrix.m34(); } |
| 89 | double m41() const { return m_matrix.m41(); } |
| 90 | double m42() const { return m_matrix.m42(); } |
| 91 | double m43() const { return m_matrix.m43(); } |
| 92 | double m44() const { return m_matrix.m44(); } |
| 93 | |
| 94 | bool is2D() const { return m_is2D; } |
| 95 | bool isIdentity() const; |
| 96 | |
| 97 | ExceptionOr<void> setMatrixValue(const String&); |
| 98 | ExceptionOr<void> setMatrixValue(const Vector<double>&); |
| 99 | |
| 100 | Ref<DOMMatrix> translate(double tx = 0, double ty = 0, double tz = 0); |
| 101 | ExceptionOr<Ref<DOMMatrix>> multiply(DOMMatrixInit&& other) const; |
| 102 | Ref<DOMMatrix> flipX(); |
| 103 | Ref<DOMMatrix> flipY(); |
| 104 | Ref<DOMMatrix> scale(double scaleX = 1, Optional<double> scaleY = WTF::nullopt, double scaleZ = 1, double originX = 0, double originY = 0, double originZ = 0); |
| 105 | Ref<DOMMatrix> scale3d(double scale = 1, double originX = 0, double originY = 0, double originZ = 0); |
| 106 | Ref<DOMMatrix> rotate(double rotX = 0, Optional<double> rotY = WTF::nullopt, Optional<double> rotZ = WTF::nullopt); // Angles are in degrees. |
| 107 | Ref<DOMMatrix> rotateFromVector(double x = 0, double y = 0); |
| 108 | Ref<DOMMatrix> rotateAxisAngle(double x = 0, double y = 0, double z = 0, double angle = 0); // Angle is in degrees. |
| 109 | Ref<DOMMatrix> skewX(double sx = 0); // Angle is in degrees. |
| 110 | Ref<DOMMatrix> skewY(double sy = 0); // Angle is in degrees. |
| 111 | Ref<DOMMatrix> inverse() const; |
| 112 | |
| 113 | Ref<DOMPoint> transformPoint(DOMPointInit&&); |
| 114 | |
| 115 | ExceptionOr<Ref<Float32Array>> toFloat32Array() const; |
| 116 | ExceptionOr<Ref<Float64Array>> toFloat64Array() const; |
| 117 | |
| 118 | ExceptionOr<String> toString() const; |
| 119 | |
| 120 | const TransformationMatrix& transformationMatrix() const { return m_matrix; } |
| 121 | |
| 122 | protected: |
| 123 | DOMMatrixReadOnly() = default; |
| 124 | DOMMatrixReadOnly(const TransformationMatrix&, Is2D); |
| 125 | DOMMatrixReadOnly(TransformationMatrix&&, Is2D); |
| 126 | |
| 127 | struct AbstractMatrix { |
| 128 | TransformationMatrix matrix; |
| 129 | bool is2D { true }; |
| 130 | }; |
| 131 | |
| 132 | static ExceptionOr<AbstractMatrix> parseStringIntoAbstractMatrix(const String&); |
| 133 | |
| 134 | Ref<DOMMatrix> cloneAsDOMMatrix() const; |
| 135 | |
| 136 | template <typename T> |
| 137 | static ExceptionOr<Ref<T>> fromMatrixHelper(DOMMatrixInit&&); |
| 138 | |
| 139 | TransformationMatrix m_matrix; |
| 140 | bool m_is2D { true }; |
| 141 | }; |
| 142 | |
| 143 | // https://drafts.fxtf.org/geometry/#create-a-dommatrix-from-the-dictionary |
| 144 | template<typename T> |
| 145 | inline ExceptionOr<Ref<T>> DOMMatrixReadOnly::fromMatrixHelper(DOMMatrixInit&& init) |
| 146 | { |
| 147 | auto result = validateAndFixup(init); |
| 148 | if (result.hasException()) |
| 149 | return result.releaseException(); |
| 150 | if (init.is2D.value()) |
| 151 | return T::create(TransformationMatrix { init.m11.value(), init.m12.value(), init.m21.value(), init.m22.value(), init.m41.value(), init.m42.value() }, Is2D::Yes); |
| 152 | return T::create(TransformationMatrix { |
| 153 | init.m11.value(), init.m12.value(), init.m13, init.m14, |
| 154 | init.m21.value(), init.m22.value(), init.m23, init.m24, |
| 155 | init.m31, init.m32, init.m33, init.m34, |
| 156 | init.m41.value(), init.m42.value(), init.m43, init.m44 |
| 157 | }, Is2D::No); |
| 158 | } |
| 159 | |
| 160 | } // namespace WebCore |
| 161 | |