summaryrefslogtreecommitdiffstats
path: root/dom/svg/SVGTransform.cpp
blob: c993253c4c6ce003663b42e1f62b25ab8f938628 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "SVGTransform.h"

#include "nsError.h"
#include "nsContentUtils.h"  // for NS_ENSURE_FINITE
#include "nsTextFormatter.h"

namespace {
const double kRadPerDegree = 2.0 * M_PI / 360.0;
}  // namespace

namespace mozilla {

using namespace dom::SVGTransform_Binding;

void SVGTransform::GetValueAsString(nsAString& aValue) const {
  switch (mType) {
    case SVG_TRANSFORM_TRANSLATE:
      // The spec say that if Y is not provided, it is assumed to be zero.
      if (mMatrix._32 != 0)
        nsTextFormatter::ssprintf(aValue, u"translate(%g, %g)", mMatrix._31,
                                  mMatrix._32);
      else
        nsTextFormatter::ssprintf(aValue, u"translate(%g)", mMatrix._31);
      break;
    case SVG_TRANSFORM_ROTATE:
      if (mOriginX != 0.0f || mOriginY != 0.0f)
        nsTextFormatter::ssprintf(aValue, u"rotate(%g, %g, %g)", mAngle,
                                  mOriginX, mOriginY);
      else
        nsTextFormatter::ssprintf(aValue, u"rotate(%g)", mAngle);
      break;
    case SVG_TRANSFORM_SCALE:
      if (mMatrix._11 != mMatrix._22)
        nsTextFormatter::ssprintf(aValue, u"scale(%g, %g)", mMatrix._11,
                                  mMatrix._22);
      else
        nsTextFormatter::ssprintf(aValue, u"scale(%g)", mMatrix._11);
      break;
    case SVG_TRANSFORM_SKEWX:
      nsTextFormatter::ssprintf(aValue, u"skewX(%g)", mAngle);
      break;
    case SVG_TRANSFORM_SKEWY:
      nsTextFormatter::ssprintf(aValue, u"skewY(%g)", mAngle);
      break;
    case SVG_TRANSFORM_MATRIX:
      nsTextFormatter::ssprintf(aValue, u"matrix(%g, %g, %g, %g, %g, %g)",
                                mMatrix._11, mMatrix._12, mMatrix._21,
                                mMatrix._22, mMatrix._31, mMatrix._32);
      break;
    default:
      aValue.Truncate();
      NS_ERROR("unknown transformation type");
      break;
  }
}

void SVGTransform::SetMatrix(const gfxMatrix& aMatrix) {
  mType = SVG_TRANSFORM_MATRIX;
  mMatrix = aMatrix;
  // We set the other members here too, since operator== requires it and
  // the DOM requires it for mAngle.
  mAngle = 0.f;
  mOriginX = 0.f;
  mOriginY = 0.f;
}

void SVGTransform::SetTranslate(float aTx, float aTy) {
  mType = SVG_TRANSFORM_TRANSLATE;
  mMatrix = gfxMatrix::Translation(aTx, aTy);
  mAngle = 0.f;
  mOriginX = 0.f;
  mOriginY = 0.f;
}

void SVGTransform::SetScale(float aSx, float aSy) {
  mType = SVG_TRANSFORM_SCALE;
  mMatrix = gfxMatrix::Scaling(aSx, aSy);
  mAngle = 0.f;
  mOriginX = 0.f;
  mOriginY = 0.f;
}

void SVGTransform::SetRotate(float aAngle, float aCx, float aCy) {
  mType = SVG_TRANSFORM_ROTATE;
  mMatrix = gfxMatrix::Translation(aCx, aCy)
                .PreRotate(aAngle * kRadPerDegree)
                .PreTranslate(-aCx, -aCy);
  mAngle = aAngle;
  mOriginX = aCx;
  mOriginY = aCy;
}

nsresult SVGTransform::SetSkewX(float aAngle) {
  double ta = tan(aAngle * kRadPerDegree);
  // No one actually cares about the exact error return type here.
  NS_ENSURE_FINITE(ta, NS_ERROR_INVALID_ARG);

  mType = SVG_TRANSFORM_SKEWX;
  mMatrix = gfxMatrix();
  mMatrix._21 = ta;
  mAngle = aAngle;
  mOriginX = 0.f;
  mOriginY = 0.f;
  return NS_OK;
}

nsresult SVGTransform::SetSkewY(float aAngle) {
  double ta = tan(aAngle * kRadPerDegree);
  // No one actually cares about the exact error return type here.
  NS_ENSURE_FINITE(ta, NS_ERROR_INVALID_ARG);

  mType = SVG_TRANSFORM_SKEWY;
  mMatrix = gfxMatrix();
  mMatrix._12 = ta;
  mAngle = aAngle;
  mOriginX = 0.f;
  mOriginY = 0.f;
  return NS_OK;
}

SVGTransformSMILData::SVGTransformSMILData(const SVGTransform& aTransform)
    : mTransformType(aTransform.Type()) {
  MOZ_ASSERT(mTransformType >= SVG_TRANSFORM_MATRIX &&
                 mTransformType <= SVG_TRANSFORM_SKEWY,
             "Unexpected transform type");

  for (uint32_t i = 0; i < NUM_STORED_PARAMS; ++i) {
    mParams[i] = 0.f;
  }

  switch (mTransformType) {
    case SVG_TRANSFORM_MATRIX: {
      const gfxMatrix& mx = aTransform.GetMatrix();
      mParams[0] = static_cast<float>(mx._11);
      mParams[1] = static_cast<float>(mx._12);
      mParams[2] = static_cast<float>(mx._21);
      mParams[3] = static_cast<float>(mx._22);
      mParams[4] = static_cast<float>(mx._31);
      mParams[5] = static_cast<float>(mx._32);
      break;
    }
    case SVG_TRANSFORM_TRANSLATE: {
      const gfxMatrix& mx = aTransform.GetMatrix();
      mParams[0] = static_cast<float>(mx._31);
      mParams[1] = static_cast<float>(mx._32);
      break;
    }
    case SVG_TRANSFORM_SCALE: {
      const gfxMatrix& mx = aTransform.GetMatrix();
      mParams[0] = static_cast<float>(mx._11);
      mParams[1] = static_cast<float>(mx._22);
      break;
    }
    case SVG_TRANSFORM_ROTATE:
      mParams[0] = aTransform.Angle();
      aTransform.GetRotationOrigin(mParams[1], mParams[2]);
      break;

    case SVG_TRANSFORM_SKEWX:
    case SVG_TRANSFORM_SKEWY:
      mParams[0] = aTransform.Angle();
      break;

    default:
      MOZ_ASSERT_UNREACHABLE("Unexpected transform type");
      break;
  }
}

SVGTransform SVGTransformSMILData::ToSVGTransform() const {
  SVGTransform result;

  switch (mTransformType) {
    case SVG_TRANSFORM_MATRIX:
      result.SetMatrix(gfxMatrix(mParams[0], mParams[1], mParams[2], mParams[3],
                                 mParams[4], mParams[5]));
      break;

    case SVG_TRANSFORM_TRANSLATE:
      result.SetTranslate(mParams[0], mParams[1]);
      break;

    case SVG_TRANSFORM_SCALE:
      result.SetScale(mParams[0], mParams[1]);
      break;

    case SVG_TRANSFORM_ROTATE:
      result.SetRotate(mParams[0], mParams[1], mParams[2]);
      break;

    case SVG_TRANSFORM_SKEWX:
      result.SetSkewX(mParams[0]);
      break;

    case SVG_TRANSFORM_SKEWY:
      result.SetSkewY(mParams[0]);
      break;

    default:
      MOZ_ASSERT_UNREACHABLE("Unexpected transform type");
      break;
  }
  return result;
}

}  // namespace mozilla