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