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