SMILFloatType.cpp (2892B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "SMILFloatType.h" 8 9 #include <math.h> 10 11 #include "mozilla/SMILValue.h" 12 #include "nsDebug.h" 13 14 namespace mozilla { 15 16 void SMILFloatType::InitValue(SMILValue& aValue) const { 17 MOZ_ASSERT(aValue.IsNull(), "Unexpected value type"); 18 aValue.mU.mDouble = 0.0; 19 aValue.mType = this; 20 } 21 22 void SMILFloatType::DestroyValue(SMILValue& aValue) const { 23 MOZ_ASSERT(aValue.mType == this, "Unexpected SMIL value"); 24 aValue.mU.mDouble = 0.0; 25 aValue.mType = SMILNullType::Singleton(); 26 } 27 28 nsresult SMILFloatType::Assign(SMILValue& aDest, const SMILValue& aSrc) const { 29 MOZ_ASSERT(aDest.mType == aSrc.mType, "Incompatible SMIL types"); 30 MOZ_ASSERT(aDest.mType == this, "Unexpected SMIL value"); 31 aDest.mU.mDouble = aSrc.mU.mDouble; 32 return NS_OK; 33 } 34 35 bool SMILFloatType::IsEqual(const SMILValue& aLeft, 36 const SMILValue& aRight) const { 37 MOZ_ASSERT(aLeft.mType == aRight.mType, "Incompatible SMIL types"); 38 MOZ_ASSERT(aLeft.mType == this, "Unexpected type for SMIL value"); 39 40 return aLeft.mU.mDouble == aRight.mU.mDouble; 41 } 42 43 nsresult SMILFloatType::Add(SMILValue& aDest, const SMILValue& aValueToAdd, 44 uint32_t aCount) const { 45 MOZ_ASSERT(aValueToAdd.mType == aDest.mType, "Trying to add invalid types"); 46 MOZ_ASSERT(aValueToAdd.mType == this, "Unexpected source type"); 47 aDest.mU.mDouble += aValueToAdd.mU.mDouble * aCount; 48 return NS_OK; 49 } 50 51 nsresult SMILFloatType::ComputeDistance(const SMILValue& aFrom, 52 const SMILValue& aTo, 53 double& aDistance) const { 54 MOZ_ASSERT(aFrom.mType == aTo.mType, "Trying to compare different types"); 55 MOZ_ASSERT(aFrom.mType == this, "Unexpected source type"); 56 57 const double& from = aFrom.mU.mDouble; 58 const double& to = aTo.mU.mDouble; 59 60 aDistance = fabs(to - from); 61 62 return NS_OK; 63 } 64 65 nsresult SMILFloatType::Interpolate(const SMILValue& aStartVal, 66 const SMILValue& aEndVal, 67 double aUnitDistance, 68 SMILValue& aResult) const { 69 MOZ_ASSERT(aStartVal.mType == aEndVal.mType, 70 "Trying to interpolate different types"); 71 MOZ_ASSERT(aStartVal.mType == this, "Unexpected types for interpolation"); 72 MOZ_ASSERT(aResult.mType == this, "Unexpected result type"); 73 74 const double& startVal = aStartVal.mU.mDouble; 75 const double& endVal = aEndVal.mU.mDouble; 76 77 aResult.mU.mDouble = (startVal + (endVal - startVal) * aUnitDistance); 78 79 return NS_OK; 80 } 81 82 } // namespace mozilla