Merge mozilla-central to autoland on a CLOSED TREE
[gecko.git] / dom / svg / SVGMatrix.h
blob2a5018c988ec4168536a20013e0f1fdab01c05a1
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /**
8 * Notes on transforms in Mozilla and the SVG code.
10 * It's important to note that the matrix convention used in the SVG standard
11 * is the opposite convention to the one used in the Mozilla code or, more
12 * specifically, the convention used in Thebes code (code using gfxMatrix).
13 * Whereas the SVG standard uses the column vector convention, Thebes code uses
14 * the row vector convention. Thus, whereas in the SVG standard you have
15 * [M1][M2][M3]|p|, in Thebes you have |p|'[M3]'[M2]'[M1]'. In other words, the
16 * following are equivalent:
18 * / a1 c1 tx1 \ / a2 c2 tx2 \ / a3 c3 tx3 \ / x \
19 * SVG: | b1 d1 ty1 | | b2 d2 ty2 | | b3 d3 ty3 | | y |
20 * \ 0 0 1 / \ 0 0 1 / \ 0 0 1 / \ 1 /
22 * / a3 b3 0 \ / a2 b2 0 \ / a1 b1 0 \
23 * Thebes: [ x y 1 ] | c3 d3 0 | | c2 d2 0 | | c1 d1 0 |
24 * \ tx3 ty3 1 / \ tx2 ty2 1 / \ tx1 ty1 1 /
26 * Because the Thebes representation of a transform is the transpose of the SVG
27 * representation, our transform order must be reversed when representing SVG
28 * transforms using gfxMatrix in the SVG code. Since the SVG implementation
29 * stores and obtains matrices in SVG order, to do this we must pre-multiply
30 * gfxMatrix objects that represent SVG transforms instead of post-multiplying
31 * them as we would for matrices using SVG's column vector convention.
32 * Pre-multiplying may look wrong if you're only familiar with the SVG
33 * convention, but in that case hopefully the above explanation clears things
34 * up.
37 #ifndef DOM_SVG_SVGMATRIX_H_
38 #define DOM_SVG_SVGMATRIX_H_
40 #include "DOMSVGTransform.h"
41 #include "gfxMatrix.h"
42 #include "nsCycleCollectionParticipant.h"
43 #include "nsWrapperCache.h"
44 #include "mozilla/Attributes.h"
46 namespace mozilla::dom {
48 /**
49 * DOM wrapper for an SVG matrix.
51 class SVGMatrix final : public nsWrapperCache {
52 public:
53 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(SVGMatrix)
54 NS_DECL_CYCLE_COLLECTION_NATIVE_WRAPPERCACHE_CLASS(SVGMatrix)
56 /**
57 * Ctor for SVGMatrix objects that belong to a DOMSVGTransform.
59 explicit SVGMatrix(DOMSVGTransform& aTransform) : mTransform(&aTransform) {}
61 /**
62 * Ctors for SVGMatrix objects created independently of a DOMSVGTransform.
64 // Default ctor for gfxMatrix will produce identity mx
65 SVGMatrix() = default;
67 explicit SVGMatrix(const gfxMatrix& aMatrix) : mMatrix(aMatrix) {}
69 // WebIDL
70 DOMSVGTransform* GetParentObject() const;
71 JSObject* WrapObject(JSContext* aCx,
72 JS::Handle<JSObject*> aGivenProto) override;
74 float A() const { return static_cast<float>(GetMatrix()._11); }
75 void SetA(float aA, ErrorResult& rv);
76 float B() const { return static_cast<float>(GetMatrix()._12); }
77 void SetB(float aB, ErrorResult& rv);
78 float C() const { return static_cast<float>(GetMatrix()._21); }
79 void SetC(float aC, ErrorResult& rv);
80 float D() const { return static_cast<float>(GetMatrix()._22); }
81 void SetD(float aD, ErrorResult& rv);
82 float E() const { return static_cast<float>(GetMatrix()._31); }
83 void SetE(float aE, ErrorResult& rv);
84 float F() const { return static_cast<float>(GetMatrix()._32); }
85 void SetF(float aF, ErrorResult& rv);
86 already_AddRefed<SVGMatrix> Multiply(SVGMatrix& aMatrix);
87 already_AddRefed<SVGMatrix> Inverse(ErrorResult& aRv);
88 already_AddRefed<SVGMatrix> Translate(float x, float y);
89 already_AddRefed<SVGMatrix> Scale(float scaleFactor);
90 already_AddRefed<SVGMatrix> ScaleNonUniform(float scaleFactorX,
91 float scaleFactorY);
92 already_AddRefed<SVGMatrix> Rotate(float angle);
93 already_AddRefed<SVGMatrix> RotateFromVector(float x, float y,
94 ErrorResult& aRv);
95 already_AddRefed<SVGMatrix> FlipX();
96 already_AddRefed<SVGMatrix> FlipY();
97 already_AddRefed<SVGMatrix> SkewX(float angle, ErrorResult& rv);
98 already_AddRefed<SVGMatrix> SkewY(float angle, ErrorResult& rv);
100 private:
101 ~SVGMatrix() = default;
103 const gfxMatrix& GetMatrix() const {
104 return mTransform ? mTransform->Matrixgfx() : mMatrix;
107 void SetMatrix(const gfxMatrix& aMatrix) {
108 if (mTransform) {
109 mTransform->SetMatrix(aMatrix);
110 } else {
111 mMatrix = aMatrix;
115 bool IsAnimVal() const {
116 return mTransform ? mTransform->IsAnimVal() : false;
119 RefPtr<DOMSVGTransform> mTransform;
121 // Typically we operate on the matrix data accessed via mTransform but for
122 // matrices that exist independently of an DOMSVGTransform we use mMatrix
123 // below.
124 gfxMatrix mMatrix;
127 } // namespace mozilla::dom
129 #endif // DOM_SVG_SVGMATRIX_H_