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 "SMILIntegerType.h"
8 : #include "nsSMILValue.h"
9 : #include "nsDebug.h"
10 : #include <math.h>
11 :
12 : namespace mozilla {
13 :
14 : void
15 0 : SMILIntegerType::Init(nsSMILValue& aValue) const
16 : {
17 0 : MOZ_ASSERT(aValue.IsNull(), "Unexpected value type");
18 0 : aValue.mU.mInt = 0;
19 0 : aValue.mType = this;
20 0 : }
21 :
22 : void
23 0 : SMILIntegerType::Destroy(nsSMILValue& aValue) const
24 : {
25 0 : NS_PRECONDITION(aValue.mType == this, "Unexpected SMIL value");
26 0 : aValue.mU.mInt = 0;
27 0 : aValue.mType = nsSMILNullType::Singleton();
28 0 : }
29 :
30 : nsresult
31 0 : SMILIntegerType::Assign(nsSMILValue& aDest, const nsSMILValue& aSrc) const
32 : {
33 0 : NS_PRECONDITION(aDest.mType == aSrc.mType, "Incompatible SMIL types");
34 0 : NS_PRECONDITION(aDest.mType == this, "Unexpected SMIL value");
35 0 : aDest.mU.mInt = aSrc.mU.mInt;
36 0 : return NS_OK;
37 : }
38 :
39 : bool
40 0 : SMILIntegerType::IsEqual(const nsSMILValue& aLeft,
41 : const nsSMILValue& aRight) const
42 : {
43 0 : NS_PRECONDITION(aLeft.mType == aRight.mType, "Incompatible SMIL types");
44 0 : NS_PRECONDITION(aLeft.mType == this, "Unexpected type for SMIL value");
45 :
46 0 : return aLeft.mU.mInt == aRight.mU.mInt;
47 : }
48 :
49 : nsresult
50 0 : SMILIntegerType::Add(nsSMILValue& aDest, const nsSMILValue& aValueToAdd,
51 : uint32_t aCount) const
52 : {
53 0 : NS_PRECONDITION(aValueToAdd.mType == aDest.mType,
54 : "Trying to add invalid types");
55 0 : NS_PRECONDITION(aValueToAdd.mType == this, "Unexpected source type");
56 0 : aDest.mU.mInt += aValueToAdd.mU.mInt * aCount;
57 0 : return NS_OK;
58 : }
59 :
60 : nsresult
61 0 : SMILIntegerType::ComputeDistance(const nsSMILValue& aFrom,
62 : const nsSMILValue& aTo,
63 : double& aDistance) const
64 : {
65 0 : NS_PRECONDITION(aFrom.mType == aTo.mType,"Trying to compare different types");
66 0 : NS_PRECONDITION(aFrom.mType == this, "Unexpected source type");
67 0 : aDistance = fabs(double(aTo.mU.mInt - aFrom.mU.mInt));
68 0 : return NS_OK;
69 : }
70 :
71 : nsresult
72 0 : SMILIntegerType::Interpolate(const nsSMILValue& aStartVal,
73 : const nsSMILValue& aEndVal,
74 : double aUnitDistance,
75 : nsSMILValue& aResult) const
76 : {
77 0 : NS_PRECONDITION(aStartVal.mType == aEndVal.mType,
78 : "Trying to interpolate different types");
79 0 : NS_PRECONDITION(aStartVal.mType == this,
80 : "Unexpected types for interpolation");
81 0 : NS_PRECONDITION(aResult.mType == this, "Unexpected result type");
82 :
83 0 : const double startVal = double(aStartVal.mU.mInt);
84 0 : const double endVal = double(aEndVal.mU.mInt);
85 0 : const double currentVal = startVal + (endVal - startVal) * aUnitDistance;
86 :
87 : // When currentVal is exactly midway between its two nearest integers, we
88 : // jump to the "next" integer to provide simple, easy to remember and
89 : // consistent behaviour (from the SMIL author's point of view).
90 :
91 0 : if (startVal < endVal) {
92 0 : aResult.mU.mInt = int64_t(floor(currentVal + 0.5)); // round mid up
93 : } else {
94 0 : aResult.mU.mInt = int64_t(ceil(currentVal - 0.5)); // round mid down
95 : }
96 :
97 0 : return NS_OK;
98 : }
99 :
100 : } // namespace mozilla
|