1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/smil/SMILIntegerType.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,99 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "SMILIntegerType.h" 1.10 +#include "nsSMILValue.h" 1.11 +#include "nsDebug.h" 1.12 +#include <math.h> 1.13 + 1.14 +namespace mozilla { 1.15 + 1.16 +void 1.17 +SMILIntegerType::Init(nsSMILValue& aValue) const 1.18 +{ 1.19 + NS_ABORT_IF_FALSE(aValue.IsNull(), "Unexpected value type"); 1.20 + aValue.mU.mInt = 0; 1.21 + aValue.mType = this; 1.22 +} 1.23 + 1.24 +void 1.25 +SMILIntegerType::Destroy(nsSMILValue& aValue) const 1.26 +{ 1.27 + NS_PRECONDITION(aValue.mType == this, "Unexpected SMIL value"); 1.28 + aValue.mU.mInt = 0; 1.29 + aValue.mType = nsSMILNullType::Singleton(); 1.30 +} 1.31 + 1.32 +nsresult 1.33 +SMILIntegerType::Assign(nsSMILValue& aDest, const nsSMILValue& aSrc) const 1.34 +{ 1.35 + NS_PRECONDITION(aDest.mType == aSrc.mType, "Incompatible SMIL types"); 1.36 + NS_PRECONDITION(aDest.mType == this, "Unexpected SMIL value"); 1.37 + aDest.mU.mInt = aSrc.mU.mInt; 1.38 + return NS_OK; 1.39 +} 1.40 + 1.41 +bool 1.42 +SMILIntegerType::IsEqual(const nsSMILValue& aLeft, 1.43 + const nsSMILValue& aRight) const 1.44 +{ 1.45 + NS_PRECONDITION(aLeft.mType == aRight.mType, "Incompatible SMIL types"); 1.46 + NS_PRECONDITION(aLeft.mType == this, "Unexpected type for SMIL value"); 1.47 + 1.48 + return aLeft.mU.mInt == aRight.mU.mInt; 1.49 +} 1.50 + 1.51 +nsresult 1.52 +SMILIntegerType::Add(nsSMILValue& aDest, const nsSMILValue& aValueToAdd, 1.53 + uint32_t aCount) const 1.54 +{ 1.55 + NS_PRECONDITION(aValueToAdd.mType == aDest.mType, 1.56 + "Trying to add invalid types"); 1.57 + NS_PRECONDITION(aValueToAdd.mType == this, "Unexpected source type"); 1.58 + aDest.mU.mInt += aValueToAdd.mU.mInt * aCount; 1.59 + return NS_OK; 1.60 +} 1.61 + 1.62 +nsresult 1.63 +SMILIntegerType::ComputeDistance(const nsSMILValue& aFrom, 1.64 + const nsSMILValue& aTo, 1.65 + double& aDistance) const 1.66 +{ 1.67 + NS_PRECONDITION(aFrom.mType == aTo.mType,"Trying to compare different types"); 1.68 + NS_PRECONDITION(aFrom.mType == this, "Unexpected source type"); 1.69 + aDistance = fabs(double(aTo.mU.mInt - aFrom.mU.mInt)); 1.70 + return NS_OK; 1.71 +} 1.72 + 1.73 +nsresult 1.74 +SMILIntegerType::Interpolate(const nsSMILValue& aStartVal, 1.75 + const nsSMILValue& aEndVal, 1.76 + double aUnitDistance, 1.77 + nsSMILValue& aResult) const 1.78 +{ 1.79 + NS_PRECONDITION(aStartVal.mType == aEndVal.mType, 1.80 + "Trying to interpolate different types"); 1.81 + NS_PRECONDITION(aStartVal.mType == this, 1.82 + "Unexpected types for interpolation"); 1.83 + NS_PRECONDITION(aResult.mType == this, "Unexpected result type"); 1.84 + 1.85 + const double startVal = double(aStartVal.mU.mInt); 1.86 + const double endVal = double(aEndVal.mU.mInt); 1.87 + const double currentVal = startVal + (endVal - startVal) * aUnitDistance; 1.88 + 1.89 + // When currentVal is exactly midway between its two nearest integers, we 1.90 + // jump to the "next" integer to provide simple, easy to remember and 1.91 + // consistent behaviour (from the SMIL author's point of view). 1.92 + 1.93 + if (startVal < endVal) { 1.94 + aResult.mU.mInt = int64_t(floor(currentVal + 0.5)); // round mid up 1.95 + } else { 1.96 + aResult.mU.mInt = int64_t(ceil(currentVal - 0.5)); // round mid down 1.97 + } 1.98 + 1.99 + return NS_OK; 1.100 +} 1.101 + 1.102 +} // namespace mozilla