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