tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

SVGMatrix.h (5130B)


      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/. */
      6 
      7 /**
      8 * Notes on transforms in Mozilla and the SVG code.
      9 *
     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:
     17 *
     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 /
     21 *
     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 /
     25 *
     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.
     35 */
     36 
     37 #ifndef DOM_SVG_SVGMATRIX_H_
     38 #define DOM_SVG_SVGMATRIX_H_
     39 
     40 #include "DOMSVGTransform.h"
     41 #include "gfxMatrix.h"
     42 #include "nsCycleCollectionParticipant.h"
     43 #include "nsWrapperCache.h"
     44 
     45 namespace mozilla::dom {
     46 
     47 struct DOMMatrix2DInit;
     48 
     49 /**
     50 * DOM wrapper for an SVG matrix.
     51 */
     52 class SVGMatrix final : public nsWrapperCache {
     53 public:
     54  NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(SVGMatrix)
     55  NS_DECL_CYCLE_COLLECTION_NATIVE_WRAPPERCACHE_CLASS(SVGMatrix)
     56 
     57  /**
     58   * Ctor for SVGMatrix objects that belong to a DOMSVGTransform.
     59   */
     60  explicit SVGMatrix(DOMSVGTransform& aTransform) : mTransform(&aTransform) {}
     61 
     62  /**
     63   * Ctors for SVGMatrix objects created independently of a DOMSVGTransform.
     64   */
     65  // Default ctor for gfxMatrix will produce identity mx
     66  SVGMatrix() = default;
     67 
     68  explicit SVGMatrix(const gfxMatrix& aMatrix) : mMatrix(aMatrix) {}
     69 
     70  // WebIDL
     71  DOMSVGTransform* GetParentObject() const;
     72  JSObject* WrapObject(JSContext* aCx,
     73                       JS::Handle<JSObject*> aGivenProto) override;
     74 
     75  float A() const { return static_cast<float>(GetMatrix()._11); }
     76  void SetA(float aA, ErrorResult& aRv);
     77  float B() const { return static_cast<float>(GetMatrix()._12); }
     78  void SetB(float aB, ErrorResult& aRv);
     79  float C() const { return static_cast<float>(GetMatrix()._21); }
     80  void SetC(float aC, ErrorResult& aRv);
     81  float D() const { return static_cast<float>(GetMatrix()._22); }
     82  void SetD(float aD, ErrorResult& aRv);
     83  float E() const { return static_cast<float>(GetMatrix()._31); }
     84  void SetE(float aE, ErrorResult& aRv);
     85  float F() const { return static_cast<float>(GetMatrix()._32); }
     86  void SetF(float aF, ErrorResult& aRv);
     87  already_AddRefed<SVGMatrix> Multiply(const DOMMatrix2DInit& aMatrix,
     88                                       ErrorResult& aRv);
     89  already_AddRefed<SVGMatrix> Inverse(ErrorResult& aRv);
     90  already_AddRefed<SVGMatrix> Translate(float x, float y);
     91  already_AddRefed<SVGMatrix> Scale(float scaleFactor);
     92  already_AddRefed<SVGMatrix> ScaleNonUniform(float scaleFactorX,
     93                                              float scaleFactorY);
     94  already_AddRefed<SVGMatrix> Rotate(float angle);
     95  already_AddRefed<SVGMatrix> RotateFromVector(float x, float y,
     96                                               ErrorResult& aRv);
     97  already_AddRefed<SVGMatrix> FlipX();
     98  already_AddRefed<SVGMatrix> FlipY();
     99  already_AddRefed<SVGMatrix> SkewX(float angle, ErrorResult& aRv);
    100  already_AddRefed<SVGMatrix> SkewY(float angle, ErrorResult& aRv);
    101 
    102 private:
    103  ~SVGMatrix() = default;
    104 
    105  const gfxMatrix& GetMatrix() const {
    106    return mTransform ? mTransform->Matrixgfx() : mMatrix;
    107  }
    108 
    109  void SetMatrix(const gfxMatrix& aMatrix) {
    110    if (mTransform) {
    111      mTransform->SetMatrix(aMatrix);
    112    } else {
    113      mMatrix = aMatrix;
    114    }
    115  }
    116 
    117  bool IsAnimVal() const {
    118    return mTransform ? mTransform->IsAnimVal() : false;
    119  }
    120 
    121  RefPtr<DOMSVGTransform> mTransform;
    122 
    123  // Typically we operate on the matrix data accessed via mTransform but for
    124  // matrices that exist independently of an DOMSVGTransform we use mMatrix
    125  // below.
    126  gfxMatrix mMatrix;
    127 };
    128 
    129 }  // namespace mozilla::dom
    130 
    131 #endif  // DOM_SVG_SVGMATRIX_H_