1 /*
2 * Copyright (C) 2014 Google 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 are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "third_party/blink/renderer/core/svg/svg_matrix_tear_off.h"
32
33 #include "third_party/blink/renderer/core/svg/svg_transform_tear_off.h"
34 #include "third_party/blink/renderer/platform/bindings/exception_state.h"
35 #include "third_party/blink/renderer/platform/heap/heap.h"
36
37 namespace blink {
38
SVGMatrixTearOff(const AffineTransform & static_value)39 SVGMatrixTearOff::SVGMatrixTearOff(const AffineTransform& static_value)
40 : static_value_(static_value) {}
41
SVGMatrixTearOff(SVGTransformTearOff * transform)42 SVGMatrixTearOff::SVGMatrixTearOff(SVGTransformTearOff* transform)
43 : context_transform_(transform) {
44 DCHECK(transform);
45 }
46
Trace(Visitor * visitor) const47 void SVGMatrixTearOff::Trace(Visitor* visitor) const {
48 visitor->Trace(context_transform_);
49 ScriptWrappable::Trace(visitor);
50 }
51
Value() const52 const AffineTransform& SVGMatrixTearOff::Value() const {
53 return context_transform_ ? context_transform_->Target()->Matrix()
54 : static_value_;
55 }
56
MutableValue()57 AffineTransform* SVGMatrixTearOff::MutableValue() {
58 return context_transform_ ? context_transform_->Target()->MutableMatrix()
59 : &static_value_;
60 }
61
CommitChange()62 void SVGMatrixTearOff::CommitChange() {
63 if (!context_transform_)
64 return;
65
66 context_transform_->Target()->OnMatrixChange();
67 context_transform_->CommitChange();
68 }
69
70 #define DEFINE_SETTER(ATTRIBUTE) \
71 void SVGMatrixTearOff::set##ATTRIBUTE(double f, \
72 ExceptionState& exceptionState) { \
73 if (context_transform_ && context_transform_->IsImmutable()) { \
74 SVGPropertyTearOffBase::ThrowReadOnly(exceptionState); \
75 return; \
76 } \
77 MutableValue()->Set##ATTRIBUTE(f); \
78 CommitChange(); \
79 }
80
81 DEFINE_SETTER(A)
DEFINE_SETTER(B)82 DEFINE_SETTER(B)
83 DEFINE_SETTER(C)
84 DEFINE_SETTER(D)
85 DEFINE_SETTER(E)
86 DEFINE_SETTER(F)
87
88 #undef DEFINE_SETTER
89
90 SVGMatrixTearOff* SVGMatrixTearOff::translate(double tx, double ty) {
91 auto* matrix = MakeGarbageCollected<SVGMatrixTearOff>(Value());
92 matrix->MutableValue()->Translate(tx, ty);
93 return matrix;
94 }
95
scale(double s)96 SVGMatrixTearOff* SVGMatrixTearOff::scale(double s) {
97 auto* matrix = MakeGarbageCollected<SVGMatrixTearOff>(Value());
98 matrix->MutableValue()->Scale(s, s);
99 return matrix;
100 }
101
scaleNonUniform(double sx,double sy)102 SVGMatrixTearOff* SVGMatrixTearOff::scaleNonUniform(double sx, double sy) {
103 auto* matrix = MakeGarbageCollected<SVGMatrixTearOff>(Value());
104 matrix->MutableValue()->Scale(sx, sy);
105 return matrix;
106 }
107
rotate(double d)108 SVGMatrixTearOff* SVGMatrixTearOff::rotate(double d) {
109 auto* matrix = MakeGarbageCollected<SVGMatrixTearOff>(Value());
110 matrix->MutableValue()->Rotate(d);
111 return matrix;
112 }
113
flipX()114 SVGMatrixTearOff* SVGMatrixTearOff::flipX() {
115 auto* matrix = MakeGarbageCollected<SVGMatrixTearOff>(Value());
116 matrix->MutableValue()->FlipX();
117 return matrix;
118 }
119
flipY()120 SVGMatrixTearOff* SVGMatrixTearOff::flipY() {
121 auto* matrix = MakeGarbageCollected<SVGMatrixTearOff>(Value());
122 matrix->MutableValue()->FlipY();
123 return matrix;
124 }
125
skewX(double angle)126 SVGMatrixTearOff* SVGMatrixTearOff::skewX(double angle) {
127 auto* matrix = MakeGarbageCollected<SVGMatrixTearOff>(Value());
128 matrix->MutableValue()->SkewX(angle);
129 return matrix;
130 }
131
skewY(double angle)132 SVGMatrixTearOff* SVGMatrixTearOff::skewY(double angle) {
133 auto* matrix = MakeGarbageCollected<SVGMatrixTearOff>(Value());
134 matrix->MutableValue()->SkewY(angle);
135 return matrix;
136 }
137
multiply(SVGMatrixTearOff * other)138 SVGMatrixTearOff* SVGMatrixTearOff::multiply(SVGMatrixTearOff* other) {
139 auto* matrix = MakeGarbageCollected<SVGMatrixTearOff>(Value());
140 *matrix->MutableValue() *= other->Value();
141 return matrix;
142 }
143
inverse(ExceptionState & exception_state)144 SVGMatrixTearOff* SVGMatrixTearOff::inverse(ExceptionState& exception_state) {
145 if (!Value().IsInvertible()) {
146 exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
147 "The matrix is not invertible.");
148 return nullptr;
149 }
150 return MakeGarbageCollected<SVGMatrixTearOff>(Value().Inverse());
151 }
152
rotateFromVector(double x,double y,ExceptionState & exception_state)153 SVGMatrixTearOff* SVGMatrixTearOff::rotateFromVector(
154 double x,
155 double y,
156 ExceptionState& exception_state) {
157 if (!x || !y) {
158 exception_state.ThrowDOMException(DOMExceptionCode::kInvalidAccessError,
159 "Arguments cannot be zero.");
160 return nullptr;
161 }
162 AffineTransform copy = Value();
163 copy.RotateFromVector(x, y);
164 return MakeGarbageCollected<SVGMatrixTearOff>(copy);
165 }
166
167 } // namespace blink
168