2011-07-01 00:19:52 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2011-07-01 00:19:52 -07:00
|
|
|
|
|
|
|
#include "nsSVGNumberPair.h"
|
2012-12-21 01:18:58 -08:00
|
|
|
#include "nsSVGAttrTearoffTable.h"
|
2011-07-23 01:41:17 -07:00
|
|
|
#include "nsCharSeparatedTokenizer.h"
|
2011-07-01 00:19:52 -07:00
|
|
|
#include "nsSMILValue.h"
|
2012-09-22 12:26:05 -07:00
|
|
|
#include "SVGContentUtils.h"
|
2011-07-01 00:19:52 -07:00
|
|
|
#include "SVGNumberPairSMILType.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
2013-07-01 00:03:04 -07:00
|
|
|
using namespace mozilla::dom;
|
2011-07-01 00:19:52 -07:00
|
|
|
|
2012-12-21 01:18:58 -08:00
|
|
|
static nsSVGAttrTearoffTable<nsSVGNumberPair, nsSVGNumberPair::DOMAnimatedNumber>
|
|
|
|
sSVGFirstAnimatedNumberTearoffTable;
|
|
|
|
static nsSVGAttrTearoffTable<nsSVGNumberPair, nsSVGNumberPair::DOMAnimatedNumber>
|
|
|
|
sSVGSecondAnimatedNumberTearoffTable;
|
|
|
|
|
2011-07-01 00:19:52 -07:00
|
|
|
static nsresult
|
|
|
|
ParseNumberOptionalNumber(const nsAString& aValue,
|
|
|
|
float aValues[2])
|
|
|
|
{
|
2011-07-23 01:41:17 -07:00
|
|
|
nsCharSeparatedTokenizerTemplate<IsSVGWhitespace>
|
|
|
|
tokenizer(aValue, ',',
|
|
|
|
nsCharSeparatedTokenizer::SEPARATOR_OPTIONAL);
|
2013-09-17 05:52:39 -07:00
|
|
|
if (tokenizer.whitespaceBeforeFirstToken()) {
|
2011-07-23 01:41:17 -07:00
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
2011-07-01 00:19:52 -07:00
|
|
|
}
|
2011-07-23 01:41:17 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i;
|
2011-07-23 01:41:17 -07:00
|
|
|
for (i = 0; i < 2 && tokenizer.hasMoreTokens(); ++i) {
|
2013-10-01 00:50:40 -07:00
|
|
|
if (!SVGContentUtils::ParseNumber(tokenizer.nextToken(), aValues[i])) {
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
2011-07-01 00:19:52 -07:00
|
|
|
}
|
|
|
|
}
|
2011-07-23 01:41:17 -07:00
|
|
|
if (i == 1) {
|
|
|
|
aValues[1] = aValues[0];
|
|
|
|
}
|
2011-07-01 00:19:52 -07:00
|
|
|
|
2011-07-23 01:41:17 -07:00
|
|
|
if (i == 0 || // Too few values.
|
|
|
|
tokenizer.hasMoreTokens() || // Too many values.
|
2013-09-17 05:52:39 -07:00
|
|
|
tokenizer.whitespaceAfterCurrentToken() || // Trailing whitespace.
|
|
|
|
tokenizer.separatorAfterCurrentToken()) { // Trailing comma.
|
2011-07-23 01:41:17 -07:00
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
}
|
2011-07-01 00:19:52 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSVGNumberPair::SetBaseValueString(const nsAString &aValueAsString,
|
2011-10-09 08:25:07 -07:00
|
|
|
nsSVGElement *aSVGElement)
|
2011-07-01 00:19:52 -07:00
|
|
|
{
|
|
|
|
float val[2];
|
|
|
|
|
|
|
|
nsresult rv = ParseNumberOptionalNumber(aValueAsString, val);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
mBaseVal[0] = val[0];
|
|
|
|
mBaseVal[1] = val[1];
|
2011-10-17 07:59:28 -07:00
|
|
|
mIsBaseSet = true;
|
2011-07-01 00:19:52 -07:00
|
|
|
if (!mIsAnimated) {
|
|
|
|
mAnimVal[0] = mBaseVal[0];
|
|
|
|
mAnimVal[1] = mBaseVal[1];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
aSVGElement->AnimationNeedsResample();
|
|
|
|
}
|
|
|
|
|
2012-02-15 15:40:45 -08:00
|
|
|
// We don't need to call Will/DidChange* here - we're only called by
|
2012-11-14 14:10:08 -08:00
|
|
|
// nsSVGElement::ParseAttribute under Element::SetAttr,
|
2011-07-01 00:19:52 -07:00
|
|
|
// which takes care of notifying.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-02-15 15:40:45 -08:00
|
|
|
nsSVGNumberPair::GetBaseValueString(nsAString &aValueAsString) const
|
2011-07-01 00:19:52 -07:00
|
|
|
{
|
|
|
|
aValueAsString.Truncate();
|
|
|
|
aValueAsString.AppendFloat(mBaseVal[0]);
|
|
|
|
if (mBaseVal[0] != mBaseVal[1]) {
|
|
|
|
aValueAsString.AppendLiteral(", ");
|
|
|
|
aValueAsString.AppendFloat(mBaseVal[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSVGNumberPair::SetBaseValue(float aValue, PairIndex aPairIndex,
|
2011-10-09 08:25:07 -07:00
|
|
|
nsSVGElement *aSVGElement)
|
2011-07-01 00:19:52 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t index = (aPairIndex == eFirst ? 0 : 1);
|
2012-02-15 15:40:45 -08:00
|
|
|
if (mIsBaseSet && mBaseVal[index] == aValue) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsAttrValue emptyOrOldValue = aSVGElement->WillChangeNumberPair(mAttrEnum);
|
2011-07-01 00:19:52 -07:00
|
|
|
mBaseVal[index] = aValue;
|
2011-10-17 07:59:28 -07:00
|
|
|
mIsBaseSet = true;
|
2011-07-01 00:19:52 -07:00
|
|
|
if (!mIsAnimated) {
|
|
|
|
mAnimVal[index] = aValue;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
aSVGElement->AnimationNeedsResample();
|
|
|
|
}
|
2012-02-15 15:40:45 -08:00
|
|
|
aSVGElement->DidChangeNumberPair(mAttrEnum, emptyOrOldValue);
|
2011-07-01 00:19:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSVGNumberPair::SetBaseValues(float aValue1, float aValue2,
|
2011-10-09 08:25:07 -07:00
|
|
|
nsSVGElement *aSVGElement)
|
2011-07-01 00:19:52 -07:00
|
|
|
{
|
2012-02-15 15:40:45 -08:00
|
|
|
if (mIsBaseSet && mBaseVal[0] == aValue1 && mBaseVal[1] == aValue2) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsAttrValue emptyOrOldValue = aSVGElement->WillChangeNumberPair(mAttrEnum);
|
2011-07-01 00:19:52 -07:00
|
|
|
mBaseVal[0] = aValue1;
|
|
|
|
mBaseVal[1] = aValue2;
|
2011-10-17 07:59:28 -07:00
|
|
|
mIsBaseSet = true;
|
2011-07-01 00:19:52 -07:00
|
|
|
if (!mIsAnimated) {
|
|
|
|
mAnimVal[0] = aValue1;
|
|
|
|
mAnimVal[1] = aValue2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
aSVGElement->AnimationNeedsResample();
|
|
|
|
}
|
2012-02-15 15:40:45 -08:00
|
|
|
aSVGElement->DidChangeNumberPair(mAttrEnum, emptyOrOldValue);
|
2011-07-01 00:19:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSVGNumberPair::SetAnimValue(const float aValue[2], nsSVGElement *aSVGElement)
|
|
|
|
{
|
2012-06-01 16:53:06 -07:00
|
|
|
if (mIsAnimated && mAnimVal[0] == aValue[0] && mAnimVal[1] == aValue[1]) {
|
|
|
|
return;
|
|
|
|
}
|
2011-07-01 00:19:52 -07:00
|
|
|
mAnimVal[0] = aValue[0];
|
|
|
|
mAnimVal[1] = aValue[1];
|
2011-10-17 07:59:28 -07:00
|
|
|
mIsAnimated = true;
|
2011-07-01 00:19:52 -07:00
|
|
|
aSVGElement->DidAnimateNumberPair(mAttrEnum);
|
|
|
|
}
|
|
|
|
|
2013-07-01 00:03:04 -07:00
|
|
|
already_AddRefed<SVGAnimatedNumber>
|
2013-03-19 10:43:31 -07:00
|
|
|
nsSVGNumberPair::ToDOMAnimatedNumber(PairIndex aIndex,
|
|
|
|
nsSVGElement* aSVGElement)
|
2011-07-01 00:19:52 -07:00
|
|
|
{
|
2012-12-21 01:18:58 -08:00
|
|
|
nsRefPtr<DOMAnimatedNumber> domAnimatedNumber =
|
|
|
|
aIndex == eFirst ? sSVGFirstAnimatedNumberTearoffTable.GetTearoff(this) :
|
|
|
|
sSVGSecondAnimatedNumberTearoffTable.GetTearoff(this);
|
|
|
|
if (!domAnimatedNumber) {
|
|
|
|
domAnimatedNumber = new DOMAnimatedNumber(this, aIndex, aSVGElement);
|
|
|
|
if (aIndex == eFirst) {
|
|
|
|
sSVGFirstAnimatedNumberTearoffTable.AddTearoff(this, domAnimatedNumber);
|
|
|
|
} else {
|
|
|
|
sSVGSecondAnimatedNumberTearoffTable.AddTearoff(this, domAnimatedNumber);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-19 10:43:31 -07:00
|
|
|
return domAnimatedNumber.forget();
|
2011-07-01 00:19:52 -07:00
|
|
|
}
|
|
|
|
|
2012-12-21 01:18:58 -08:00
|
|
|
nsSVGNumberPair::DOMAnimatedNumber::~DOMAnimatedNumber()
|
|
|
|
{
|
|
|
|
if (mIndex == eFirst) {
|
|
|
|
sSVGFirstAnimatedNumberTearoffTable.RemoveTearoff(mVal);
|
|
|
|
} else {
|
|
|
|
sSVGSecondAnimatedNumberTearoffTable.RemoveTearoff(mVal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-01 00:19:52 -07:00
|
|
|
nsISMILAttr*
|
|
|
|
nsSVGNumberPair::ToSMILAttr(nsSVGElement *aSVGElement)
|
|
|
|
{
|
|
|
|
return new SMILNumberPair(this, aSVGElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSVGNumberPair::SMILNumberPair::ValueFromString(const nsAString& aStr,
|
2013-03-18 20:18:45 -07:00
|
|
|
const dom::SVGAnimationElement* /*aSrcElement*/,
|
2011-07-01 00:19:52 -07:00
|
|
|
nsSMILValue& aValue,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool& aPreventCachingOfSandwich) const
|
2011-07-01 00:19:52 -07:00
|
|
|
{
|
|
|
|
float values[2];
|
|
|
|
|
|
|
|
nsresult rv = ParseNumberOptionalNumber(aStr, values);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSMILValue val(&SVGNumberPairSMILType::sSingleton);
|
|
|
|
val.mU.mNumberPair[0] = values[0];
|
|
|
|
val.mU.mNumberPair[1] = values[1];
|
|
|
|
aValue = val;
|
2011-10-17 07:59:28 -07:00
|
|
|
aPreventCachingOfSandwich = false;
|
2011-07-01 00:19:52 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSMILValue
|
|
|
|
nsSVGNumberPair::SMILNumberPair::GetBaseValue() const
|
|
|
|
{
|
|
|
|
nsSMILValue val(&SVGNumberPairSMILType::sSingleton);
|
|
|
|
val.mU.mNumberPair[0] = mVal->mBaseVal[0];
|
|
|
|
val.mU.mNumberPair[1] = mVal->mBaseVal[1];
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSVGNumberPair::SMILNumberPair::ClearAnimValue()
|
|
|
|
{
|
|
|
|
if (mVal->mIsAnimated) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mVal->mIsAnimated = false;
|
2012-03-03 01:21:09 -08:00
|
|
|
mVal->mAnimVal[0] = mVal->mBaseVal[0];
|
|
|
|
mVal->mAnimVal[1] = mVal->mBaseVal[1];
|
|
|
|
mSVGElement->DidAnimateNumberPair(mVal->mAttrEnum);
|
2011-07-01 00:19:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSVGNumberPair::SMILNumberPair::SetAnimValue(const nsSMILValue& aValue)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aValue.mType == &SVGNumberPairSMILType::sSingleton,
|
|
|
|
"Unexpected type to assign animated value");
|
|
|
|
if (aValue.mType == &SVGNumberPairSMILType::sSingleton) {
|
|
|
|
mVal->SetAnimValue(aValue.mU.mNumberPair, mSVGElement);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|