SVGIntegerPairSMILType.cpp (3646B)
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 "SVGIntegerPairSMILType.h" 8 9 #include "mozilla/SMILValue.h" 10 #include "nsDebug.h" 11 #include "nsMathUtils.h" 12 13 namespace mozilla { 14 15 void SVGIntegerPairSMILType::InitValue(SMILValue& aValue) const { 16 MOZ_ASSERT(aValue.IsNull(), "Unexpected value type"); 17 18 aValue.mU.mIntPair[0] = 0; 19 aValue.mU.mIntPair[1] = 0; 20 aValue.mType = this; 21 } 22 23 void SVGIntegerPairSMILType::DestroyValue(SMILValue& aValue) const { 24 MOZ_ASSERT(aValue.mType == this, "Unexpected SMIL value"); 25 aValue.mU.mIntPair[0] = 0; 26 aValue.mU.mIntPair[1] = 0; 27 aValue.mType = SMILNullType::Singleton(); 28 } 29 30 nsresult SVGIntegerPairSMILType::Assign(SMILValue& aDest, 31 const SMILValue& aSrc) const { 32 MOZ_ASSERT(aDest.mType == aSrc.mType, "Incompatible SMIL types"); 33 MOZ_ASSERT(aDest.mType == this, "Unexpected SMIL value"); 34 35 aDest.mU.mIntPair[0] = aSrc.mU.mIntPair[0]; 36 aDest.mU.mIntPair[1] = aSrc.mU.mIntPair[1]; 37 return NS_OK; 38 } 39 40 bool SVGIntegerPairSMILType::IsEqual(const SMILValue& aLeft, 41 const SMILValue& aRight) const { 42 MOZ_ASSERT(aLeft.mType == aRight.mType, "Incompatible SMIL types"); 43 MOZ_ASSERT(aLeft.mType == this, "Unexpected type for SMIL value"); 44 45 return aLeft.mU.mIntPair[0] == aRight.mU.mIntPair[0] && 46 aLeft.mU.mIntPair[1] == aRight.mU.mIntPair[1]; 47 } 48 49 nsresult SVGIntegerPairSMILType::Add(SMILValue& aDest, 50 const SMILValue& aValueToAdd, 51 uint32_t aCount) const { 52 MOZ_ASSERT(aValueToAdd.mType == aDest.mType, "Trying to add invalid types"); 53 MOZ_ASSERT(aValueToAdd.mType == this, "Unexpected source type"); 54 55 aDest.mU.mIntPair[0] += aValueToAdd.mU.mIntPair[0] * aCount; 56 aDest.mU.mIntPair[1] += aValueToAdd.mU.mIntPair[1] * aCount; 57 58 return NS_OK; 59 } 60 61 nsresult SVGIntegerPairSMILType::ComputeDistance(const SMILValue& aFrom, 62 const SMILValue& aTo, 63 double& aDistance) const { 64 MOZ_ASSERT(aFrom.mType == aTo.mType, "Trying to compare different types"); 65 MOZ_ASSERT(aFrom.mType == this, "Unexpected source type"); 66 67 double delta[2]; 68 delta[0] = aTo.mU.mIntPair[0] - aFrom.mU.mIntPair[0]; 69 delta[1] = aTo.mU.mIntPair[1] - aFrom.mU.mIntPair[1]; 70 71 aDistance = NS_hypot(delta[0], delta[1]); 72 return NS_OK; 73 } 74 75 nsresult SVGIntegerPairSMILType::Interpolate(const SMILValue& aStartVal, 76 const SMILValue& aEndVal, 77 double aUnitDistance, 78 SMILValue& aResult) const { 79 MOZ_ASSERT(aStartVal.mType == aEndVal.mType, 80 "Trying to interpolate different types"); 81 MOZ_ASSERT(aStartVal.mType == this, "Unexpected types for interpolation"); 82 MOZ_ASSERT(aResult.mType == this, "Unexpected result type"); 83 84 double currentVal[2]; 85 currentVal[0] = 86 aStartVal.mU.mIntPair[0] + 87 (aEndVal.mU.mIntPair[0] - aStartVal.mU.mIntPair[0]) * aUnitDistance; 88 currentVal[1] = 89 aStartVal.mU.mIntPair[1] + 90 (aEndVal.mU.mIntPair[1] - aStartVal.mU.mIntPair[1]) * aUnitDistance; 91 92 aResult.mU.mIntPair[0] = NS_lround(currentVal[0]); 93 aResult.mU.mIntPair[1] = NS_lround(currentVal[1]); 94 return NS_OK; 95 } 96 97 } // namespace mozilla