michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "SMILIntegerType.h" michael@0: #include "nsSMILValue.h" michael@0: #include "nsDebug.h" michael@0: #include michael@0: michael@0: namespace mozilla { michael@0: michael@0: void michael@0: SMILIntegerType::Init(nsSMILValue& aValue) const michael@0: { michael@0: NS_ABORT_IF_FALSE(aValue.IsNull(), "Unexpected value type"); michael@0: aValue.mU.mInt = 0; michael@0: aValue.mType = this; michael@0: } michael@0: michael@0: void michael@0: SMILIntegerType::Destroy(nsSMILValue& aValue) const michael@0: { michael@0: NS_PRECONDITION(aValue.mType == this, "Unexpected SMIL value"); michael@0: aValue.mU.mInt = 0; michael@0: aValue.mType = nsSMILNullType::Singleton(); michael@0: } michael@0: michael@0: nsresult michael@0: SMILIntegerType::Assign(nsSMILValue& aDest, const nsSMILValue& aSrc) const michael@0: { michael@0: NS_PRECONDITION(aDest.mType == aSrc.mType, "Incompatible SMIL types"); michael@0: NS_PRECONDITION(aDest.mType == this, "Unexpected SMIL value"); michael@0: aDest.mU.mInt = aSrc.mU.mInt; michael@0: return NS_OK; michael@0: } michael@0: michael@0: bool michael@0: SMILIntegerType::IsEqual(const nsSMILValue& aLeft, michael@0: const nsSMILValue& aRight) const michael@0: { michael@0: NS_PRECONDITION(aLeft.mType == aRight.mType, "Incompatible SMIL types"); michael@0: NS_PRECONDITION(aLeft.mType == this, "Unexpected type for SMIL value"); michael@0: michael@0: return aLeft.mU.mInt == aRight.mU.mInt; michael@0: } michael@0: michael@0: nsresult michael@0: SMILIntegerType::Add(nsSMILValue& aDest, const nsSMILValue& aValueToAdd, michael@0: uint32_t aCount) const michael@0: { michael@0: NS_PRECONDITION(aValueToAdd.mType == aDest.mType, michael@0: "Trying to add invalid types"); michael@0: NS_PRECONDITION(aValueToAdd.mType == this, "Unexpected source type"); michael@0: aDest.mU.mInt += aValueToAdd.mU.mInt * aCount; michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: SMILIntegerType::ComputeDistance(const nsSMILValue& aFrom, michael@0: const nsSMILValue& aTo, michael@0: double& aDistance) const michael@0: { michael@0: NS_PRECONDITION(aFrom.mType == aTo.mType,"Trying to compare different types"); michael@0: NS_PRECONDITION(aFrom.mType == this, "Unexpected source type"); michael@0: aDistance = fabs(double(aTo.mU.mInt - aFrom.mU.mInt)); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: SMILIntegerType::Interpolate(const nsSMILValue& aStartVal, michael@0: const nsSMILValue& aEndVal, michael@0: double aUnitDistance, michael@0: nsSMILValue& aResult) const michael@0: { michael@0: NS_PRECONDITION(aStartVal.mType == aEndVal.mType, michael@0: "Trying to interpolate different types"); michael@0: NS_PRECONDITION(aStartVal.mType == this, michael@0: "Unexpected types for interpolation"); michael@0: NS_PRECONDITION(aResult.mType == this, "Unexpected result type"); michael@0: michael@0: const double startVal = double(aStartVal.mU.mInt); michael@0: const double endVal = double(aEndVal.mU.mInt); michael@0: const double currentVal = startVal + (endVal - startVal) * aUnitDistance; michael@0: michael@0: // When currentVal is exactly midway between its two nearest integers, we michael@0: // jump to the "next" integer to provide simple, easy to remember and michael@0: // consistent behaviour (from the SMIL author's point of view). michael@0: michael@0: if (startVal < endVal) { michael@0: aResult.mU.mInt = int64_t(floor(currentVal + 0.5)); // round mid up michael@0: } else { michael@0: aResult.mU.mInt = int64_t(ceil(currentVal - 0.5)); // round mid down michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: } // namespace mozilla