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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 "nsSVGViewBox.h"
#include "mozilla/Move.h"
#include "nsCharSeparatedTokenizer.h"
#include "nsSMILValue.h"
#include "nsTextFormatter.h"
#include "SVGContentUtils.h"
#include "SVGViewBoxSMILType.h"
#define NUM_VIEWBOX_COMPONENTS 4
using namespace mozilla;
/* Implementation of nsSVGViewBoxRect methods */
bool
nsSVGViewBoxRect::operator==(const nsSVGViewBoxRect& aOther) const
{
if (&aOther == this)
return true;
return (none && aOther.none) ||
(!none && !aOther.none &&
x == aOther.x &&
y == aOther.y &&
width == aOther.width &&
height == aOther.height);
}
/* Cycle collection macros for nsSVGViewBox */
NS_SVG_VAL_IMPL_CYCLE_COLLECTION_WRAPPERCACHED(nsSVGViewBox::DOMBaseVal, mSVGElement)
NS_SVG_VAL_IMPL_CYCLE_COLLECTION_WRAPPERCACHED(nsSVGViewBox::DOMAnimVal, mSVGElement)
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsSVGViewBox::DOMBaseVal)
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsSVGViewBox::DOMBaseVal)
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsSVGViewBox::DOMAnimVal)
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsSVGViewBox::DOMAnimVal)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsSVGViewBox::DOMBaseVal)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsSVGViewBox::DOMAnimVal)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
static nsSVGAttrTearoffTable<nsSVGViewBox, nsSVGViewBox::DOMBaseVal>
sBaseSVGViewBoxTearoffTable;
static nsSVGAttrTearoffTable<nsSVGViewBox, nsSVGViewBox::DOMAnimVal>
sAnimSVGViewBoxTearoffTable;
nsSVGAttrTearoffTable<nsSVGViewBox, dom::SVGAnimatedRect>
nsSVGViewBox::sSVGAnimatedRectTearoffTable;
/* Implementation of nsSVGViewBox methods */
void
nsSVGViewBox::Init()
{
mHasBaseVal = false;
// We shouldn't use mBaseVal for rendering (its usages should be guarded with
// "mHasBaseVal" checks), but just in case we do by accident, this will
// ensure that we treat it as "none" and ignore its numeric values:
mBaseVal.none = true;
mAnimVal = nullptr;
}
bool
nsSVGViewBox::HasRect() const
{
// Check mAnimVal if we have one; otherwise, check mBaseVal if we have one;
// otherwise, just return false (we clearly do not have a rect).
const nsSVGViewBoxRect* rect = mAnimVal;
if (!rect) {
if (!mHasBaseVal) {
// no anim val, no base val --> no viewbox rect
return false;
}
rect = &mBaseVal;
}
return !rect->none && rect->width >= 0 && rect->height >= 0;
}
void
nsSVGViewBox::SetAnimValue(const nsSVGViewBoxRect& aRect,
nsSVGElement *aSVGElement)
{
if (!mAnimVal) {
// it's okay if allocation fails - and no point in reporting that
mAnimVal = new nsSVGViewBoxRect(aRect);
} else {
if (aRect == *mAnimVal) {
return;
}
*mAnimVal = aRect;
}
aSVGElement->DidAnimateViewBox();
}
void
nsSVGViewBox::SetBaseValue(const nsSVGViewBoxRect& aRect,
nsSVGElement *aSVGElement)
{
if (!mHasBaseVal || mBaseVal == aRect) {
// This method is used to set a single x, y, width
// or height value. It can't create a base value
// as the other components may be undefined. We record
// the new value though, so as not to lose data.
mBaseVal = aRect;
return;
}
nsAttrValue emptyOrOldValue = aSVGElement->WillChangeViewBox();
mBaseVal = aRect;
mHasBaseVal = true;
aSVGElement->DidChangeViewBox(emptyOrOldValue);
if (mAnimVal) {
aSVGElement->AnimationNeedsResample();
}
}
static nsresult
ToSVGViewBoxRect(const nsAString& aStr, nsSVGViewBoxRect *aViewBox)
{
if (aStr.EqualsLiteral("none")) {
aViewBox->none = true;
return NS_OK;
}
nsCharSeparatedTokenizerTemplate<IsSVGWhitespace>
tokenizer(aStr, ',',
nsCharSeparatedTokenizer::SEPARATOR_OPTIONAL);
float vals[NUM_VIEWBOX_COMPONENTS];
uint32_t i;
for (i = 0; i < NUM_VIEWBOX_COMPONENTS && tokenizer.hasMoreTokens(); ++i) {
if (!SVGContentUtils::ParseNumber(tokenizer.nextToken(), vals[i])) {
return NS_ERROR_DOM_SYNTAX_ERR;
}
}
if (i != NUM_VIEWBOX_COMPONENTS || // Too few values.
tokenizer.hasMoreTokens() || // Too many values.
tokenizer.separatorAfterCurrentToken()) { // Trailing comma.
return NS_ERROR_DOM_SYNTAX_ERR;
}
aViewBox->x = vals[0];
aViewBox->y = vals[1];
aViewBox->width = vals[2];
aViewBox->height = vals[3];
aViewBox->none = false;
return NS_OK;
}
nsresult
nsSVGViewBox::SetBaseValueString(const nsAString& aValue,
nsSVGElement *aSVGElement,
bool aDoSetAttr)
{
nsSVGViewBoxRect viewBox;
nsresult rv = ToSVGViewBoxRect(aValue, &viewBox);
if (NS_FAILED(rv)) {
return rv;
}
// Comparison against mBaseVal is only valid if we currently have a base val.
if (mHasBaseVal && viewBox == mBaseVal) {
return NS_OK;
}
nsAttrValue emptyOrOldValue;
if (aDoSetAttr) {
emptyOrOldValue = aSVGElement->WillChangeViewBox();
}
mHasBaseVal = true;
mBaseVal = viewBox;
if (aDoSetAttr) {
aSVGElement->DidChangeViewBox(emptyOrOldValue);
}
if (mAnimVal) {
aSVGElement->AnimationNeedsResample();
}
return NS_OK;
}
void
nsSVGViewBox::GetBaseValueString(nsAString& aValue) const
{
if (mBaseVal.none) {
aValue.AssignLiteral("none");
return;
}
char16_t buf[200];
nsTextFormatter::snprintf(buf, sizeof(buf)/sizeof(char16_t),
u"%g %g %g %g",
(double)mBaseVal.x, (double)mBaseVal.y,
(double)mBaseVal.width, (double)mBaseVal.height);
aValue.Assign(buf);
}
already_AddRefed<dom::SVGAnimatedRect>
nsSVGViewBox::ToSVGAnimatedRect(nsSVGElement* aSVGElement)
{
RefPtr<dom::SVGAnimatedRect> domAnimatedRect =
sSVGAnimatedRectTearoffTable.GetTearoff(this);
if (!domAnimatedRect) {
domAnimatedRect = new dom::SVGAnimatedRect(this, aSVGElement);
sSVGAnimatedRectTearoffTable.AddTearoff(this, domAnimatedRect);
}
return domAnimatedRect.forget();
}
already_AddRefed<dom::SVGIRect>
nsSVGViewBox::ToDOMBaseVal(nsSVGElement *aSVGElement)
{
if (!mHasBaseVal || mBaseVal.none) {
return nullptr;
}
RefPtr<DOMBaseVal> domBaseVal =
sBaseSVGViewBoxTearoffTable.GetTearoff(this);
if (!domBaseVal) {
domBaseVal = new DOMBaseVal(this, aSVGElement);
sBaseSVGViewBoxTearoffTable.AddTearoff(this, domBaseVal);
}
return domBaseVal.forget();
}
nsSVGViewBox::DOMBaseVal::~DOMBaseVal()
{
sBaseSVGViewBoxTearoffTable.RemoveTearoff(mVal);
}
already_AddRefed<dom::SVGIRect>
nsSVGViewBox::ToDOMAnimVal(nsSVGElement *aSVGElement)
{
if ((mAnimVal && mAnimVal->none) ||
(!mAnimVal && (!mHasBaseVal || mBaseVal.none))) {
return nullptr;
}
RefPtr<DOMAnimVal> domAnimVal =
sAnimSVGViewBoxTearoffTable.GetTearoff(this);
if (!domAnimVal) {
domAnimVal = new DOMAnimVal(this, aSVGElement);
sAnimSVGViewBoxTearoffTable.AddTearoff(this, domAnimVal);
}
return domAnimVal.forget();
}
nsSVGViewBox::DOMAnimVal::~DOMAnimVal()
{
sAnimSVGViewBoxTearoffTable.RemoveTearoff(mVal);
}
void
nsSVGViewBox::DOMBaseVal::SetX(float aX, ErrorResult& aRv)
{
nsSVGViewBoxRect rect = mVal->GetBaseValue();
rect.x = aX;
mVal->SetBaseValue(rect, mSVGElement);
}
void
nsSVGViewBox::DOMBaseVal::SetY(float aY, ErrorResult& aRv)
{
nsSVGViewBoxRect rect = mVal->GetBaseValue();
rect.y = aY;
mVal->SetBaseValue(rect, mSVGElement);
}
void
nsSVGViewBox::DOMBaseVal::SetWidth(float aWidth, ErrorResult& aRv)
{
nsSVGViewBoxRect rect = mVal->GetBaseValue();
rect.width = aWidth;
mVal->SetBaseValue(rect, mSVGElement);
}
void
nsSVGViewBox::DOMBaseVal::SetHeight(float aHeight, ErrorResult& aRv)
{
nsSVGViewBoxRect rect = mVal->GetBaseValue();
rect.height = aHeight;
mVal->SetBaseValue(rect, mSVGElement);
}
nsISMILAttr*
nsSVGViewBox::ToSMILAttr(nsSVGElement *aSVGElement)
{
return new SMILViewBox(this, aSVGElement);
}
nsresult
nsSVGViewBox::SMILViewBox
::ValueFromString(const nsAString& aStr,
const dom::SVGAnimationElement* /*aSrcElement*/,
nsSMILValue& aValue,
bool& aPreventCachingOfSandwich) const
{
nsSVGViewBoxRect viewBox;
nsresult res = ToSVGViewBoxRect(aStr, &viewBox);
if (NS_FAILED(res)) {
return res;
}
nsSMILValue val(&SVGViewBoxSMILType::sSingleton);
*static_cast<nsSVGViewBoxRect*>(val.mU.mPtr) = viewBox;
aValue = Move(val);
aPreventCachingOfSandwich = false;
return NS_OK;
}
nsSMILValue
nsSVGViewBox::SMILViewBox::GetBaseValue() const
{
nsSMILValue val(&SVGViewBoxSMILType::sSingleton);
*static_cast<nsSVGViewBoxRect*>(val.mU.mPtr) = mVal->mBaseVal;
return val;
}
void
nsSVGViewBox::SMILViewBox::ClearAnimValue()
{
if (mVal->mAnimVal) {
mVal->mAnimVal = nullptr;
mSVGElement->DidAnimateViewBox();
}
}
nsresult
nsSVGViewBox::SMILViewBox::SetAnimValue(const nsSMILValue& aValue)
{
NS_ASSERTION(aValue.mType == &SVGViewBoxSMILType::sSingleton,
"Unexpected type to assign animated value");
if (aValue.mType == &SVGViewBoxSMILType::sSingleton) {
nsSVGViewBoxRect &vb = *static_cast<nsSVGViewBoxRect*>(aValue.mU.mPtr);
mVal->SetAnimValue(vb, mSVGElement);
}
return NS_OK;
}
|