tor-browser

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

SVGTransform.h (5154B)


      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 #ifndef DOM_SVG_SVGTRANSFORM_H_
      8 #define DOM_SVG_SVGTRANSFORM_H_
      9 
     10 #include "gfxMatrix.h"
     11 #include "mozilla/dom/SVGTransformBinding.h"
     12 #include "mozilla/gfx/Matrix.h"
     13 #include "nsDebug.h"
     14 
     15 namespace mozilla {
     16 
     17 /*
     18 * The DOM wrapper class for this class is DOMSVGTransform.
     19 */
     20 class SVGTransform {
     21 public:
     22  // Default ctor initialises to matrix type with identity matrix
     23  SVGTransform()
     24      : mAngle(0.f),
     25        mOriginX(0.f),
     26        mOriginY(0.f),
     27        mType(dom::SVGTransform_Binding::SVG_TRANSFORM_MATRIX) {}
     28 
     29  explicit SVGTransform(const gfxMatrix& aMatrix)
     30      : mMatrix(aMatrix),
     31        mAngle(0.f),
     32        mOriginX(0.f),
     33        mOriginY(0.f),
     34        mType(dom::SVGTransform_Binding::SVG_TRANSFORM_MATRIX) {}
     35 
     36  bool operator==(const SVGTransform& rhs) const {
     37    return mType == rhs.mType && MatricesEqual(mMatrix, rhs.mMatrix) &&
     38           mAngle == rhs.mAngle && mOriginX == rhs.mOriginX &&
     39           mOriginY == rhs.mOriginY;
     40  }
     41 
     42  void GetValueAsString(nsAString& aValue) const;
     43 
     44  float Angle() const { return mAngle; }
     45  void GetRotationOrigin(float& aOriginX, float& aOriginY) const {
     46    aOriginX = mOriginX;
     47    aOriginY = mOriginY;
     48  }
     49  uint16_t Type() const { return mType; }
     50 
     51  const gfxMatrix& GetMatrix() const { return mMatrix; }
     52  void SetMatrix(const gfxMatrix& aMatrix);
     53  void SetTranslate(float aTx, float aTy);
     54  void SetScale(float aSx, float aSy);
     55  void SetRotate(float aAngle, float aCx, float aCy);
     56  nsresult SetSkewX(float aAngle);
     57  nsresult SetSkewY(float aAngle);
     58 
     59  static bool MatricesEqual(const gfxMatrix& a, const gfxMatrix& b) {
     60    return a._11 == b._11 && a._12 == b._12 && a._21 == b._21 &&
     61           a._22 == b._22 && a._31 == b._31 && a._32 == b._32;
     62  }
     63 
     64 protected:
     65  gfxMatrix mMatrix;
     66  float mAngle, mOriginX, mOriginY;
     67  uint16_t mType;
     68 };
     69 
     70 /*
     71 * A slightly more light-weight version of SVGTransform for SMIL animation.
     72 *
     73 * Storing the parameters in an array (rather than a matrix) also allows simpler
     74 * (transform type-agnostic) interpolation and addition.
     75 *
     76 * The meaning of the mParams array depends on the transform type as follows:
     77 *
     78 * Type                | mParams[0], mParams[1], mParams[2], ...
     79 * --------------------+-----------------------------------------
     80 * translate           | tx, ty
     81 * scale               | sx, sy
     82 * rotate              | rotation-angle (in degrees), cx, cy
     83 * skewX               | skew-angle (in degrees)
     84 * skewY               | skew-angle (in degrees)
     85 * matrix              | a, b, c, d, e, f
     86 *
     87 * The matrix type is never generated by animation code (it is only produced
     88 * when the user inserts one via the DOM) and often requires special handling
     89 * when we do encounter it. Therefore many users of this class are only
     90 * interested in the first three parameters and so we provide a special
     91 * constructor for setting those parameters only.
     92 */
     93 class SVGTransformSMILData {
     94 public:
     95  // Number of float-params required in constructor, if constructing one of the
     96  // 'simple' transform types (all but matrix type)
     97  static const uint32_t NUM_SIMPLE_PARAMS = 3;
     98 
     99  // Number of float-params required in constructor for matrix type.
    100  // This is also the number of params we actually store, regardless of type.
    101  static const uint32_t NUM_STORED_PARAMS = 6;
    102 
    103  explicit SVGTransformSMILData(uint16_t aType) : mTransformType(aType) {
    104    MOZ_ASSERT(aType >= dom::SVGTransform_Binding::SVG_TRANSFORM_MATRIX &&
    105                   aType <= dom::SVGTransform_Binding::SVG_TRANSFORM_SKEWY,
    106               "Unexpected transform type");
    107    for (uint32_t i = 0; i < NUM_STORED_PARAMS; ++i) {
    108      mParams[i] = 0.f;
    109    }
    110  }
    111 
    112  SVGTransformSMILData(uint16_t aType, float (&aParams)[NUM_SIMPLE_PARAMS])
    113      : mTransformType(aType) {
    114    MOZ_ASSERT(aType >= dom::SVGTransform_Binding::SVG_TRANSFORM_TRANSLATE &&
    115                   aType <= dom::SVGTransform_Binding::SVG_TRANSFORM_SKEWY,
    116               "Expected 'simple' transform type");
    117    for (uint32_t i = 0; i < NUM_SIMPLE_PARAMS; ++i) {
    118      mParams[i] = aParams[i];
    119    }
    120    for (uint32_t i = NUM_SIMPLE_PARAMS; i < NUM_STORED_PARAMS; ++i) {
    121      mParams[i] = 0.f;
    122    }
    123  }
    124 
    125  // Conversion to/from a fully-fledged SVGTransform
    126  explicit SVGTransformSMILData(const SVGTransform& aTransform);
    127  SVGTransform ToSVGTransform() const;
    128 
    129  bool operator==(const SVGTransformSMILData& aOther) const {
    130    if (mTransformType != aOther.mTransformType) return false;
    131 
    132    for (uint32_t i = 0; i < NUM_STORED_PARAMS; ++i) {
    133      if (mParams[i] != aOther.mParams[i]) {
    134        return false;
    135      }
    136    }
    137 
    138    return true;
    139  }
    140 
    141  bool operator!=(const SVGTransformSMILData& aOther) const {
    142    return !(*this == aOther);
    143  }
    144 
    145  uint16_t mTransformType;
    146  float mParams[NUM_STORED_PARAMS];
    147 };
    148 
    149 }  // namespace mozilla
    150 
    151 #endif  // DOM_SVG_SVGTRANSFORM_H_