summaryrefslogtreecommitdiffstats
path: root/dom/svg/SVGAnimatedInteger.cpp
blob: b14a32ce1c8c8eddbef805dbadc65492f79b04b1 (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
/* -*- 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 "SVGAnimatedInteger.h"

#include "nsError.h"
#include "SMILIntegerType.h"
#include "SVGAttrTearoffTable.h"
#include "mozilla/SMILValue.h"
#include "mozilla/SVGContentUtils.h"

using namespace mozilla::dom;

namespace mozilla {

/* Implementation */

//----------------------------------------------------------------------
// Helper class: AutoChangeIntegerNotifier
// Stack-based helper class ensure DidChangeInteger is called.
class MOZ_RAII AutoChangeIntegerNotifier {
 public:
  AutoChangeIntegerNotifier(SVGAnimatedInteger* aInteger,
                            SVGElement* aSVGElement, bool aDoSetAttr = true)
      : mInteger(aInteger), mSVGElement(aSVGElement), mDoSetAttr(aDoSetAttr) {
    MOZ_ASSERT(mInteger, "Expecting non-null integer");
    MOZ_ASSERT(mSVGElement, "Expecting non-null element");
  }

  ~AutoChangeIntegerNotifier() {
    if (mDoSetAttr) {
      mSVGElement->DidChangeInteger(mInteger->mAttrEnum);
    }
    if (mInteger->mIsAnimated) {
      mSVGElement->AnimationNeedsResample();
    }
  }

 private:
  SVGAnimatedInteger* const mInteger;
  SVGElement* const mSVGElement;
  bool mDoSetAttr;
};

static SVGAttrTearoffTable<SVGAnimatedInteger,
                           SVGAnimatedInteger::DOMAnimatedInteger>
    sSVGAnimatedIntegerTearoffTable;

nsresult SVGAnimatedInteger::SetBaseValueString(const nsAString& aValueAsString,
                                                SVGElement* aSVGElement) {
  bool success;
  auto token = SVGContentUtils::GetAndEnsureOneToken(aValueAsString, success);

  if (!success) {
    return NS_ERROR_DOM_SYNTAX_ERR;
  }

  int32_t value;

  if (!SVGContentUtils::ParseInteger(token, value)) {
    return NS_ERROR_DOM_SYNTAX_ERR;
  }

  AutoChangeIntegerNotifier notifier(this, aSVGElement, false);

  mIsBaseSet = true;
  mBaseVal = value;
  if (!mIsAnimated) {
    mAnimVal = mBaseVal;
  }
  return NS_OK;
}

void SVGAnimatedInteger::GetBaseValueString(nsAString& aValueAsString) {
  aValueAsString.Truncate();
  aValueAsString.AppendInt(mBaseVal);
}

void SVGAnimatedInteger::SetBaseValue(int aValue, SVGElement* aSVGElement) {
  // We can't just rely on SetParsedAttrValue (as called by DidChangeInteger)
  // detecting redundant changes since it will compare false if the existing
  // attribute value has an associated serialized version (a string value) even
  // if the integers match due to the way integers are stored in nsAttrValue.
  if (aValue == mBaseVal && mIsBaseSet) {
    return;
  }

  AutoChangeIntegerNotifier notifier(this, aSVGElement);

  mBaseVal = aValue;
  mIsBaseSet = true;
  if (!mIsAnimated) {
    mAnimVal = mBaseVal;
  }
}

void SVGAnimatedInteger::SetAnimValue(int aValue, SVGElement* aSVGElement) {
  if (mIsAnimated && aValue == mAnimVal) {
    return;
  }
  mAnimVal = aValue;
  mIsAnimated = true;
  aSVGElement->DidAnimateInteger(mAttrEnum);
}

already_AddRefed<DOMSVGAnimatedInteger>
SVGAnimatedInteger::ToDOMAnimatedInteger(SVGElement* aSVGElement) {
  RefPtr<DOMAnimatedInteger> domAnimatedInteger =
      sSVGAnimatedIntegerTearoffTable.GetTearoff(this);
  if (!domAnimatedInteger) {
    domAnimatedInteger = new DOMAnimatedInteger(this, aSVGElement);
    sSVGAnimatedIntegerTearoffTable.AddTearoff(this, domAnimatedInteger);
  }

  return domAnimatedInteger.forget();
}

SVGAnimatedInteger::DOMAnimatedInteger::~DOMAnimatedInteger() {
  sSVGAnimatedIntegerTearoffTable.RemoveTearoff(mVal);
}

UniquePtr<SMILAttr> SVGAnimatedInteger::ToSMILAttr(SVGElement* aSVGElement) {
  return MakeUnique<SMILInteger>(this, aSVGElement);
}

nsresult SVGAnimatedInteger::SMILInteger::ValueFromString(
    const nsAString& aStr, const dom::SVGAnimationElement* /*aSrcElement*/,
    SMILValue& aValue, bool& aPreventCachingOfSandwich) const {
  int32_t val;

  if (!SVGContentUtils::ParseInteger(aStr, val)) {
    return NS_ERROR_DOM_SYNTAX_ERR;
  }

  SMILValue smilVal(SMILIntegerType::Singleton());
  smilVal.mU.mInt = val;
  aValue = smilVal;
  return NS_OK;
}

SMILValue SVGAnimatedInteger::SMILInteger::GetBaseValue() const {
  SMILValue val(SMILIntegerType::Singleton());
  val.mU.mInt = mVal->mBaseVal;
  return val;
}

void SVGAnimatedInteger::SMILInteger::ClearAnimValue() {
  if (mVal->mIsAnimated) {
    mVal->mIsAnimated = false;
    mVal->mAnimVal = mVal->mBaseVal;
    mSVGElement->DidAnimateInteger(mVal->mAttrEnum);
  }
}

nsresult SVGAnimatedInteger::SMILInteger::SetAnimValue(
    const SMILValue& aValue) {
  NS_ASSERTION(aValue.mType == SMILIntegerType::Singleton(),
               "Unexpected type to assign animated value");
  if (aValue.mType == SMILIntegerType::Singleton()) {
    mVal->SetAnimValue(int(aValue.mU.mInt), mSVGElement);
  }
  return NS_OK;
}

}  // namespace mozilla