Bug 1270832 - Activate standard c++ library hardening r=glandium
[gecko.git] / dom / svg / SVGLengthListSMILType.cpp
blob0f96d51eb05b61cec35ceff5b87eac02b0d82111
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/. */
7 #include "SVGLengthListSMILType.h"
9 #include "mozilla/FloatingPoint.h"
10 #include "mozilla/SMILValue.h"
11 #include "nsMathUtils.h"
12 #include "SVGLengthList.h"
13 #include <math.h>
14 #include <algorithm>
16 namespace mozilla {
18 /*static*/
19 SVGLengthListSMILType SVGLengthListSMILType::sSingleton;
21 //----------------------------------------------------------------------
22 // nsISMILType implementation
24 void SVGLengthListSMILType::Init(SMILValue& aValue) const {
25 MOZ_ASSERT(aValue.IsNull(), "Unexpected value type");
27 aValue.mU.mPtr = new SVGLengthListAndInfo();
28 aValue.mType = this;
31 void SVGLengthListSMILType::Destroy(SMILValue& aValue) const {
32 MOZ_ASSERT(aValue.mType == this, "Unexpected SMIL value type");
33 delete static_cast<SVGLengthListAndInfo*>(aValue.mU.mPtr);
34 aValue.mU.mPtr = nullptr;
35 aValue.mType = SMILNullType::Singleton();
38 nsresult SVGLengthListSMILType::Assign(SMILValue& aDest,
39 const SMILValue& aSrc) const {
40 MOZ_ASSERT(aDest.mType == aSrc.mType, "Incompatible SMIL types");
41 MOZ_ASSERT(aDest.mType == this, "Unexpected SMIL value");
43 const SVGLengthListAndInfo* src =
44 static_cast<const SVGLengthListAndInfo*>(aSrc.mU.mPtr);
45 SVGLengthListAndInfo* dest =
46 static_cast<SVGLengthListAndInfo*>(aDest.mU.mPtr);
48 return dest->CopyFrom(*src);
51 bool SVGLengthListSMILType::IsEqual(const SMILValue& aLeft,
52 const SMILValue& aRight) const {
53 MOZ_ASSERT(aLeft.mType == aRight.mType, "Incompatible SMIL types");
54 MOZ_ASSERT(aLeft.mType == this, "Unexpected type for SMIL value");
56 return *static_cast<const SVGLengthListAndInfo*>(aLeft.mU.mPtr) ==
57 *static_cast<const SVGLengthListAndInfo*>(aRight.mU.mPtr);
60 nsresult SVGLengthListSMILType::Add(SMILValue& aDest,
61 const SMILValue& aValueToAdd,
62 uint32_t aCount) const {
63 MOZ_ASSERT(aDest.mType == this, "Unexpected SMIL type");
64 MOZ_ASSERT(aValueToAdd.mType == this, "Incompatible SMIL type");
66 SVGLengthListAndInfo& dest =
67 *static_cast<SVGLengthListAndInfo*>(aDest.mU.mPtr);
68 const SVGLengthListAndInfo& valueToAdd =
69 *static_cast<const SVGLengthListAndInfo*>(aValueToAdd.mU.mPtr);
71 // To understand this code, see the comments documenting our Init() method,
72 // and documenting SVGLengthListAndInfo::CanZeroPadList().
74 // Note that *this* method actually may safely zero pad a shorter list
75 // regardless of the value returned by CanZeroPadList() for that list,
76 // just so long as the shorter list is being added *to* the longer list
77 // and *not* vice versa! It's okay in the case of adding a shorter list to a
78 // longer list because during the add operation we'll end up adding the
79 // zeros to actual specified values. It's *not* okay in the case of adding a
80 // longer list to a shorter list because then we end up adding to implicit
81 // zeros when we'd actually need to add to whatever the underlying values
82 // should be, not zeros, and those values are not explicit or otherwise
83 // available.
85 if (valueToAdd.IsIdentity()) { // Adding identity value - no-op
86 return NS_OK;
89 if (dest.IsIdentity()) { // Adding *to* an identity value
90 if (!dest.SetLength(valueToAdd.Length())) {
91 return NS_ERROR_OUT_OF_MEMORY;
93 for (uint32_t i = 0; i < dest.Length(); ++i) {
94 dest[i].SetValueAndUnit(valueToAdd[i].GetValueInCurrentUnits() * aCount,
95 valueToAdd[i].GetUnit());
97 dest.SetInfo(
98 valueToAdd.Element(), valueToAdd.Axis(),
99 valueToAdd.CanZeroPadList()); // propagate target element info!
100 return NS_OK;
102 MOZ_ASSERT(dest.Element() == valueToAdd.Element(),
103 "adding values from different elements...?");
105 // Zero-pad our |dest| list, if necessary.
106 if (dest.Length() < valueToAdd.Length()) {
107 if (!dest.CanZeroPadList()) {
108 // SVGContentUtils::ReportToConsole
109 return NS_ERROR_FAILURE;
112 MOZ_ASSERT(valueToAdd.CanZeroPadList(),
113 "values disagree about attribute's zero-paddibility");
115 uint32_t i = dest.Length();
116 if (!dest.SetLength(valueToAdd.Length())) {
117 return NS_ERROR_OUT_OF_MEMORY;
119 for (; i < valueToAdd.Length(); ++i) {
120 dest[i].SetValueAndUnit(0.0f, valueToAdd[i].GetUnit());
124 for (uint32_t i = 0; i < valueToAdd.Length(); ++i) {
125 float valToAdd;
126 if (dest[i].GetUnit() == valueToAdd[i].GetUnit()) {
127 valToAdd = valueToAdd[i].GetValueInCurrentUnits();
128 } else {
129 // If units differ, we use the unit of the item in 'dest'.
130 // We leave it to the frame code to check that values are finite.
131 valToAdd = valueToAdd[i].GetValueInSpecifiedUnit(
132 dest[i].GetUnit(), dest.Element(), dest.Axis());
134 dest[i].SetValueAndUnit(
135 dest[i].GetValueInCurrentUnits() + valToAdd * aCount,
136 dest[i].GetUnit());
139 // propagate target element info!
140 dest.SetInfo(valueToAdd.Element(), valueToAdd.Axis(),
141 dest.CanZeroPadList() && valueToAdd.CanZeroPadList());
143 return NS_OK;
146 nsresult SVGLengthListSMILType::ComputeDistance(const SMILValue& aFrom,
147 const SMILValue& aTo,
148 double& aDistance) const {
149 MOZ_ASSERT(aFrom.mType == this, "Unexpected SMIL type");
150 MOZ_ASSERT(aTo.mType == this, "Incompatible SMIL type");
152 const SVGLengthListAndInfo& from =
153 *static_cast<const SVGLengthListAndInfo*>(aFrom.mU.mPtr);
154 const SVGLengthListAndInfo& to =
155 *static_cast<const SVGLengthListAndInfo*>(aTo.mU.mPtr);
157 // To understand this code, see the comments documenting our Init() method,
158 // and documenting SVGLengthListAndInfo::CanZeroPadList().
160 NS_ASSERTION((from.CanZeroPadList() == to.CanZeroPadList()) ||
161 (from.CanZeroPadList() && from.IsEmpty()) ||
162 (to.CanZeroPadList() && to.IsEmpty()),
163 "Only \"zero\" SMILValues from the SMIL engine should "
164 "return true for CanZeroPadList() when the attribute "
165 "being animated can't be zero padded");
167 if ((from.Length() < to.Length() && !from.CanZeroPadList()) ||
168 (to.Length() < from.Length() && !to.CanZeroPadList())) {
169 // SVGContentUtils::ReportToConsole
170 return NS_ERROR_FAILURE;
173 // We return the root of the sum of the squares of the deltas between the
174 // user unit values of the lengths at each correspanding index. In the
175 // general case, paced animation is probably not useful, but this strategy at
176 // least does the right thing for paced animation in the face of simple
177 // 'values' lists such as:
179 // values="100 200 300; 101 201 301; 110 210 310"
181 // I.e. half way through the simple duration we'll get "105 205 305".
183 double total = 0.0;
185 uint32_t i = 0;
186 for (; i < from.Length() && i < to.Length(); ++i) {
187 double f = from[i].GetValueInPixels(from.Element(), from.Axis());
188 double t = to[i].GetValueInPixels(to.Element(), to.Axis());
189 double delta = t - f;
190 total += delta * delta;
193 // In the case that from.Length() != to.Length(), one of the following loops
194 // will run. (OK since CanZeroPadList()==true for the other list.)
196 for (; i < from.Length(); ++i) {
197 double f = from[i].GetValueInPixels(from.Element(), from.Axis());
198 total += f * f;
200 for (; i < to.Length(); ++i) {
201 double t = to[i].GetValueInPixels(to.Element(), to.Axis());
202 total += t * t;
205 float distance = sqrt(total);
206 if (!std::isfinite(distance)) {
207 return NS_ERROR_FAILURE;
209 aDistance = distance;
210 return NS_OK;
213 nsresult SVGLengthListSMILType::Interpolate(const SMILValue& aStartVal,
214 const SMILValue& aEndVal,
215 double aUnitDistance,
216 SMILValue& aResult) const {
217 MOZ_ASSERT(aStartVal.mType == aEndVal.mType,
218 "Trying to interpolate different types");
219 MOZ_ASSERT(aStartVal.mType == this, "Unexpected types for interpolation");
220 MOZ_ASSERT(aResult.mType == this, "Unexpected result type");
222 const SVGLengthListAndInfo& start =
223 *static_cast<const SVGLengthListAndInfo*>(aStartVal.mU.mPtr);
224 const SVGLengthListAndInfo& end =
225 *static_cast<const SVGLengthListAndInfo*>(aEndVal.mU.mPtr);
226 SVGLengthListAndInfo& result =
227 *static_cast<SVGLengthListAndInfo*>(aResult.mU.mPtr);
229 // To understand this code, see the comments documenting our Init() method,
230 // and documenting SVGLengthListAndInfo::CanZeroPadList().
232 NS_ASSERTION((start.CanZeroPadList() == end.CanZeroPadList()) ||
233 (start.CanZeroPadList() && start.IsEmpty()) ||
234 (end.CanZeroPadList() && end.IsEmpty()),
235 "Only \"zero\" SMILValues from the SMIL engine should "
236 "return true for CanZeroPadList() when the attribute "
237 "being animated can't be zero padded");
239 if ((start.Length() < end.Length() && !start.CanZeroPadList()) ||
240 (end.Length() < start.Length() && !end.CanZeroPadList())) {
241 // SVGContentUtils::ReportToConsole
242 return NS_ERROR_FAILURE;
245 if (!result.SetLength(std::max(start.Length(), end.Length()))) {
246 return NS_ERROR_OUT_OF_MEMORY;
249 // If units differ, we use the unit of the nearest item.
250 // We leave it to the frame code to check that values are finite.
251 bool useEndUnits = (aUnitDistance > 0.5);
253 uint32_t i = 0;
254 for (; i < start.Length() && i < end.Length(); ++i) {
255 float s, e;
256 uint8_t unit;
257 if (start[i].GetUnit() == end[i].GetUnit()) {
258 unit = start[i].GetUnit();
259 s = start[i].GetValueInCurrentUnits();
260 e = end[i].GetValueInCurrentUnits();
261 } else if (useEndUnits) {
262 unit = end[i].GetUnit();
263 s = start[i].GetValueInSpecifiedUnit(unit, end.Element(), end.Axis());
264 e = end[i].GetValueInCurrentUnits();
265 } else {
266 unit = start[i].GetUnit();
267 s = start[i].GetValueInCurrentUnits();
268 e = end[i].GetValueInSpecifiedUnit(unit, start.Element(), start.Axis());
270 result[i].SetValueAndUnit(s + (e - s) * aUnitDistance, unit);
273 // In the case that start.Length() != end.Length(), one of the following
274 // loops will run. (Okay, since CanZeroPadList()==true for the other list.)
276 for (; i < start.Length(); ++i) {
277 result[i].SetValueAndUnit(
278 start[i].GetValueInCurrentUnits() -
279 start[i].GetValueInCurrentUnits() * aUnitDistance,
280 start[i].GetUnit());
282 for (; i < end.Length(); ++i) {
283 result[i].SetValueAndUnit(end[i].GetValueInCurrentUnits() * aUnitDistance,
284 end[i].GetUnit());
287 // propagate target element info!
288 result.SetInfo(end.Element(), end.Axis(),
289 start.CanZeroPadList() && end.CanZeroPadList());
291 return NS_OK;
294 } // namespace mozilla