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