| 1 | /* |
| 2 | * Copyright (C) 2016-2019 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 "AffineTransform.h" |
| 29 | #include "ExceptionOr.h" |
| 30 | #include "SVGValueProperty.h" |
| 31 | |
| 32 | namespace WebCore { |
| 33 | |
| 34 | // FIXME: Remove this class once SVGMatrix becomes an alias to DOMMatrix. |
| 35 | class SVGMatrix : public SVGValueProperty<AffineTransform> { |
| 36 | using Base = SVGValueProperty<AffineTransform>; |
| 37 | using Base::Base; |
| 38 | |
| 39 | public: |
| 40 | static Ref<SVGMatrix> create(const AffineTransform& value = { }) |
| 41 | { |
| 42 | return adoptRef(*new SVGMatrix(value)); |
| 43 | } |
| 44 | |
| 45 | static Ref<SVGMatrix> create(SVGPropertyOwner* owner, SVGPropertyAccess access, const AffineTransform& value = { }) |
| 46 | { |
| 47 | return adoptRef(*new SVGMatrix(owner, access, value)); |
| 48 | } |
| 49 | |
| 50 | template<typename T> |
| 51 | static ExceptionOr<Ref<SVGMatrix>> create(ExceptionOr<T>&& value) |
| 52 | { |
| 53 | if (value.hasException()) |
| 54 | return value.releaseException(); |
| 55 | return create(value.releaseReturnValue()); |
| 56 | } |
| 57 | |
| 58 | double a() const |
| 59 | { |
| 60 | return m_value.a(); |
| 61 | } |
| 62 | |
| 63 | ExceptionOr<void> setA(double value) |
| 64 | { |
| 65 | if (isReadOnly()) |
| 66 | return Exception { NoModificationAllowedError }; |
| 67 | |
| 68 | m_value.setA(value); |
| 69 | commitChange(); |
| 70 | return { }; |
| 71 | } |
| 72 | |
| 73 | double b() const |
| 74 | { |
| 75 | return m_value.b(); |
| 76 | } |
| 77 | |
| 78 | ExceptionOr<void> setB(double value) |
| 79 | { |
| 80 | if (isReadOnly()) |
| 81 | return Exception { NoModificationAllowedError }; |
| 82 | |
| 83 | m_value.setB(value); |
| 84 | commitChange(); |
| 85 | return { }; |
| 86 | } |
| 87 | |
| 88 | double c() const |
| 89 | { |
| 90 | return m_value.c(); |
| 91 | } |
| 92 | |
| 93 | ExceptionOr<void> setC(double value) |
| 94 | { |
| 95 | if (isReadOnly()) |
| 96 | return Exception { NoModificationAllowedError }; |
| 97 | |
| 98 | m_value.setC(value); |
| 99 | commitChange(); |
| 100 | return { }; |
| 101 | } |
| 102 | |
| 103 | double d() const |
| 104 | { |
| 105 | return m_value.d(); |
| 106 | } |
| 107 | |
| 108 | ExceptionOr<void> setD(double value) |
| 109 | { |
| 110 | if (isReadOnly()) |
| 111 | return Exception { NoModificationAllowedError }; |
| 112 | |
| 113 | m_value.setD(value); |
| 114 | commitChange(); |
| 115 | return { }; |
| 116 | } |
| 117 | |
| 118 | double e() const |
| 119 | { |
| 120 | return m_value.e(); |
| 121 | } |
| 122 | |
| 123 | ExceptionOr<void> setE(double value) |
| 124 | { |
| 125 | if (isReadOnly()) |
| 126 | return Exception { NoModificationAllowedError }; |
| 127 | |
| 128 | m_value.setE(value); |
| 129 | commitChange(); |
| 130 | return { }; |
| 131 | } |
| 132 | |
| 133 | double f() const |
| 134 | { |
| 135 | return m_value.f(); |
| 136 | } |
| 137 | |
| 138 | ExceptionOr<void> setF(double value) |
| 139 | { |
| 140 | if (isReadOnly()) |
| 141 | return Exception { NoModificationAllowedError }; |
| 142 | |
| 143 | m_value.setF(value); |
| 144 | commitChange(); |
| 145 | return { }; |
| 146 | } |
| 147 | |
| 148 | Ref<SVGMatrix> multiply(SVGMatrix& secondMatrix) const |
| 149 | { |
| 150 | auto copy = m_value; |
| 151 | copy.multiply(secondMatrix.value()); |
| 152 | return SVGMatrix::create(copy); |
| 153 | } |
| 154 | |
| 155 | ExceptionOr<Ref<SVGMatrix>> inverse() const |
| 156 | { |
| 157 | auto inverse = m_value.inverse(); |
| 158 | if (!inverse) |
| 159 | return Exception { InvalidStateError, "Matrix is not invertible"_s }; |
| 160 | return SVGMatrix::create(*inverse); |
| 161 | } |
| 162 | |
| 163 | Ref<SVGMatrix> translate(float x, float y) const |
| 164 | { |
| 165 | auto copy = m_value; |
| 166 | copy.translate(x, y); |
| 167 | return SVGMatrix::create(copy); |
| 168 | } |
| 169 | |
| 170 | Ref<SVGMatrix> scale(float scaleFactor) const |
| 171 | { |
| 172 | auto copy = m_value; |
| 173 | copy.scale(scaleFactor); |
| 174 | return SVGMatrix::create(copy); |
| 175 | } |
| 176 | |
| 177 | Ref<SVGMatrix> scaleNonUniform(float scaleFactorX, float scaleFactorY) const |
| 178 | { |
| 179 | auto copy = m_value; |
| 180 | copy.scaleNonUniform(scaleFactorX, scaleFactorY); |
| 181 | return SVGMatrix::create(copy); |
| 182 | } |
| 183 | |
| 184 | Ref<SVGMatrix> rotate(float angle) const |
| 185 | { |
| 186 | auto copy = m_value; |
| 187 | copy.rotate(angle); |
| 188 | return SVGMatrix::create(copy); |
| 189 | } |
| 190 | |
| 191 | ExceptionOr<Ref<SVGMatrix>> rotateFromVector(float x, float y) const |
| 192 | { |
| 193 | if (!x || !y) |
| 194 | return Exception { TypeError }; |
| 195 | |
| 196 | auto copy = m_value; |
| 197 | copy.rotateFromVector(x, y); |
| 198 | return SVGMatrix::create(copy); |
| 199 | } |
| 200 | |
| 201 | Ref<SVGMatrix> flipX() const |
| 202 | { |
| 203 | auto copy = m_value; |
| 204 | copy.flipX(); |
| 205 | return SVGMatrix::create(copy); |
| 206 | } |
| 207 | |
| 208 | Ref<SVGMatrix> flipY() const |
| 209 | { |
| 210 | auto copy = m_value; |
| 211 | copy.flipY(); |
| 212 | return SVGMatrix::create(copy); |
| 213 | } |
| 214 | |
| 215 | Ref<SVGMatrix> skewX(float angle) const |
| 216 | { |
| 217 | auto copy = m_value; |
| 218 | copy.skewX(angle); |
| 219 | return SVGMatrix::create(copy); |
| 220 | } |
| 221 | |
| 222 | Ref<SVGMatrix> skewY(float angle) const |
| 223 | { |
| 224 | auto copy = m_value; |
| 225 | copy.skewY(angle); |
| 226 | return SVGMatrix::create(copy); |
| 227 | } |
| 228 | }; |
| 229 | |
| 230 | } // namespace WebCore |
| 231 | |