2015-05-03 19:32:37 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 11:12:37 +00: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/. */
|
2004-10-14 23:02:53 +00:00
|
|
|
|
2006-07-28 18:10:48 +00:00
|
|
|
#include "nsSVGNumber2.h"
|
2013-09-17 12:52:39 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2011-08-11 13:29:50 +00:00
|
|
|
#include "nsContentUtils.h" // NS_ENSURE_FINITE
|
2013-09-17 12:52:39 +00:00
|
|
|
#include "nsSMILFloatType.h"
|
|
|
|
#include "nsSMILValue.h"
|
|
|
|
#include "nsSVGAttrTearoffTable.h"
|
|
|
|
#include "SVGContentUtils.h"
|
2004-10-14 23:02:53 +00:00
|
|
|
|
2013-07-01 07:03:04 +00:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2006-07-28 18:10:48 +00:00
|
|
|
/* Implementation */
|
2004-10-14 23:02:53 +00:00
|
|
|
|
2012-12-21 09:18:58 +00:00
|
|
|
static nsSVGAttrTearoffTable<nsSVGNumber2, nsSVGNumber2::DOMAnimatedNumber>
|
|
|
|
sSVGAnimatedNumberTearoffTable;
|
|
|
|
|
2013-10-01 07:50:40 +00:00
|
|
|
static bool
|
2013-10-29 17:15:39 +00:00
|
|
|
GetValueFromString(const nsAString& aString,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aPercentagesAllowed,
|
2013-10-01 07:50:40 +00:00
|
|
|
float& aValue)
|
2006-07-28 18:10:48 +00:00
|
|
|
{
|
2014-01-04 15:02:17 +00:00
|
|
|
RangedPtr<const char16_t> iter =
|
2013-10-29 17:15:39 +00:00
|
|
|
SVGContentUtils::GetStartRangedPtr(aString);
|
2014-01-04 15:02:17 +00:00
|
|
|
const RangedPtr<const char16_t> end =
|
2013-10-29 17:15:39 +00:00
|
|
|
SVGContentUtils::GetEndRangedPtr(aString);
|
2007-10-19 08:54:21 +00:00
|
|
|
|
2013-10-29 17:15:39 +00:00
|
|
|
if (!SVGContentUtils::ParseNumber(iter, end, aValue)) {
|
2013-10-01 07:50:40 +00:00
|
|
|
return false;
|
2010-12-05 20:33:31 +00:00
|
|
|
}
|
2013-10-01 07:50:40 +00:00
|
|
|
|
2013-10-29 17:15:39 +00:00
|
|
|
if (aPercentagesAllowed) {
|
|
|
|
const nsAString& units = Substring(iter.get(), end.get());
|
|
|
|
if (units.EqualsLiteral("%")) {
|
|
|
|
aValue /= 100;
|
|
|
|
return true;
|
|
|
|
}
|
2010-12-05 20:33:31 +00:00
|
|
|
}
|
2013-10-01 07:50:40 +00:00
|
|
|
|
2013-10-29 17:15:39 +00:00
|
|
|
return iter == end;
|
2010-12-05 20:33:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSVGNumber2::SetBaseValueString(const nsAString &aValueAsString,
|
2011-10-09 15:25:07 +00:00
|
|
|
nsSVGElement *aSVGElement)
|
2010-12-05 20:33:31 +00:00
|
|
|
{
|
|
|
|
float val;
|
|
|
|
|
2013-10-01 07:50:40 +00:00
|
|
|
if (!GetValueFromString(aValueAsString,
|
|
|
|
aSVGElement->NumberAttrAllowsPercentage(mAttrEnum),
|
|
|
|
val)) {
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
2010-12-05 20:33:31 +00:00
|
|
|
}
|
2007-10-19 08:54:21 +00:00
|
|
|
|
2010-02-11 19:41:48 +00:00
|
|
|
mBaseVal = val;
|
2011-10-17 14:59:28 +00:00
|
|
|
mIsBaseSet = true;
|
2010-02-11 19:41:48 +00:00
|
|
|
if (!mIsAnimated) {
|
|
|
|
mAnimVal = mBaseVal;
|
|
|
|
}
|
|
|
|
else {
|
2009-12-29 06:34:27 +00:00
|
|
|
aSVGElement->AnimationNeedsResample();
|
|
|
|
}
|
|
|
|
|
2010-02-11 19:41:48 +00:00
|
|
|
// We don't need to call DidChange* here - we're only called by
|
2012-11-14 22:10:08 +00:00
|
|
|
// nsSVGElement::ParseAttribute under Element::SetAttr,
|
2010-02-11 19:41:48 +00:00
|
|
|
// which takes care of notifying.
|
2006-07-28 18:10:48 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2004-10-14 23:02:53 +00:00
|
|
|
|
2006-07-28 18:10:48 +00:00
|
|
|
void
|
|
|
|
nsSVGNumber2::GetBaseValueString(nsAString & aValueAsString)
|
|
|
|
{
|
2010-11-30 18:18:15 +00:00
|
|
|
aValueAsString.Truncate();
|
|
|
|
aValueAsString.AppendFloat(mBaseVal);
|
2006-07-28 18:10:48 +00:00
|
|
|
}
|
2004-10-14 23:02:53 +00:00
|
|
|
|
2006-07-28 18:10:48 +00:00
|
|
|
void
|
2012-02-15 23:40:45 +00:00
|
|
|
nsSVGNumber2::SetBaseValue(float aValue, nsSVGElement *aSVGElement)
|
2006-07-28 18:10:48 +00:00
|
|
|
{
|
2012-02-15 23:40:45 +00:00
|
|
|
if (mIsBaseSet && aValue == mBaseVal) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-02-11 19:41:48 +00:00
|
|
|
mBaseVal = aValue;
|
2011-10-17 14:59:28 +00:00
|
|
|
mIsBaseSet = true;
|
2010-02-11 19:41:48 +00:00
|
|
|
if (!mIsAnimated) {
|
|
|
|
mAnimVal = mBaseVal;
|
|
|
|
}
|
|
|
|
else {
|
2009-12-29 06:34:27 +00:00
|
|
|
aSVGElement->AnimationNeedsResample();
|
|
|
|
}
|
2012-02-15 23:40:45 +00:00
|
|
|
aSVGElement->DidChangeNumber(mAttrEnum);
|
2009-12-29 06:34:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSVGNumber2::SetAnimValue(float aValue, nsSVGElement *aSVGElement)
|
|
|
|
{
|
2012-06-01 23:53:06 +00:00
|
|
|
if (mIsAnimated && aValue == mAnimVal) {
|
|
|
|
return;
|
|
|
|
}
|
2009-12-29 06:34:27 +00:00
|
|
|
mAnimVal = aValue;
|
2011-10-17 14:59:28 +00:00
|
|
|
mIsAnimated = true;
|
2009-12-29 06:34:27 +00:00
|
|
|
aSVGElement->DidAnimateNumber(mAttrEnum);
|
2006-07-28 18:10:48 +00:00
|
|
|
}
|
2004-10-14 23:02:53 +00:00
|
|
|
|
2013-07-01 07:03:04 +00:00
|
|
|
already_AddRefed<SVGAnimatedNumber>
|
2013-02-12 10:10:24 +00:00
|
|
|
nsSVGNumber2::ToDOMAnimatedNumber(nsSVGElement* aSVGElement)
|
2006-07-28 18:10:48 +00:00
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<DOMAnimatedNumber> domAnimatedNumber =
|
2012-12-21 09:18:58 +00:00
|
|
|
sSVGAnimatedNumberTearoffTable.GetTearoff(this);
|
|
|
|
if (!domAnimatedNumber) {
|
|
|
|
domAnimatedNumber = new DOMAnimatedNumber(this, aSVGElement);
|
|
|
|
sSVGAnimatedNumberTearoffTable.AddTearoff(this, domAnimatedNumber);
|
|
|
|
}
|
2006-07-28 18:10:48 +00:00
|
|
|
|
2013-02-12 10:10:24 +00:00
|
|
|
return domAnimatedNumber.forget();
|
|
|
|
}
|
|
|
|
|
2012-12-21 09:18:58 +00:00
|
|
|
nsSVGNumber2::DOMAnimatedNumber::~DOMAnimatedNumber()
|
|
|
|
{
|
|
|
|
sSVGAnimatedNumberTearoffTable.RemoveTearoff(mVal);
|
|
|
|
}
|
|
|
|
|
2009-12-29 06:34:27 +00:00
|
|
|
nsISMILAttr*
|
|
|
|
nsSVGNumber2::ToSMILAttr(nsSVGElement *aSVGElement)
|
|
|
|
{
|
|
|
|
return new SMILNumber(this, aSVGElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSVGNumber2::SMILNumber::ValueFromString(const nsAString& aStr,
|
2013-03-19 03:18:45 +00:00
|
|
|
const mozilla::dom::SVGAnimationElement* /*aSrcElement*/,
|
2010-02-20 21:13:11 +00:00
|
|
|
nsSMILValue& aValue,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool& aPreventCachingOfSandwich) const
|
2009-12-29 06:34:27 +00:00
|
|
|
{
|
|
|
|
float value;
|
|
|
|
|
2013-10-01 07:50:40 +00:00
|
|
|
if (!GetValueFromString(aStr,
|
|
|
|
mSVGElement->NumberAttrAllowsPercentage(mVal->mAttrEnum),
|
|
|
|
value)) {
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
2009-12-29 06:34:27 +00:00
|
|
|
}
|
|
|
|
|
2013-05-30 22:34:53 +00:00
|
|
|
nsSMILValue val(nsSMILFloatType::Singleton());
|
2009-12-29 06:34:27 +00:00
|
|
|
val.mU.mDouble = value;
|
|
|
|
aValue = val;
|
2011-10-17 14:59:28 +00:00
|
|
|
aPreventCachingOfSandwich = false;
|
2009-12-29 06:34:27 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSMILValue
|
|
|
|
nsSVGNumber2::SMILNumber::GetBaseValue() const
|
|
|
|
{
|
2013-05-30 22:34:53 +00:00
|
|
|
nsSMILValue val(nsSMILFloatType::Singleton());
|
2009-12-29 06:34:27 +00:00
|
|
|
val.mU.mDouble = mVal->mBaseVal;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSVGNumber2::SMILNumber::ClearAnimValue()
|
|
|
|
{
|
|
|
|
if (mVal->mIsAnimated) {
|
2011-10-17 14:59:28 +00:00
|
|
|
mVal->mIsAnimated = false;
|
2012-03-03 09:21:09 +00:00
|
|
|
mVal->mAnimVal = mVal->mBaseVal;
|
|
|
|
mSVGElement->DidAnimateNumber(mVal->mAttrEnum);
|
2009-12-29 06:34:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSVGNumber2::SMILNumber::SetAnimValue(const nsSMILValue& aValue)
|
|
|
|
{
|
2013-05-30 22:34:53 +00:00
|
|
|
NS_ASSERTION(aValue.mType == nsSMILFloatType::Singleton(),
|
2009-12-29 06:34:27 +00:00
|
|
|
"Unexpected type to assign animated value");
|
2013-05-30 22:34:53 +00:00
|
|
|
if (aValue.mType == nsSMILFloatType::Singleton()) {
|
2009-12-29 06:34:27 +00:00
|
|
|
mVal->SetAnimValue(float(aValue.mU.mDouble), mSVGElement);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|