summaryrefslogtreecommitdiffstats
path: root/dom/svg/SVGAnimatedLength.cpp
blob: 081db5704ae61715259ac786657968a539acf997 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "SVGAnimatedLength.h"

#include "mozAutoDocUpdate.h"
#include "mozilla/ArrayUtils.h"
#include "mozilla/GeckoBindings.h"
#include "mozilla/Maybe.h"
#include "mozilla/PresShell.h"
#include "mozilla/SMILValue.h"
#include "mozilla/StaticPresData.h"
#include "mozilla/SVGIntegrationUtils.h"
#include "mozilla/dom/SVGViewportElement.h"
#include "DOMSVGAnimatedLength.h"
#include "DOMSVGLength.h"
#include "LayoutLogging.h"
#include "nsContentUtils.h"
#include "nsIFrame.h"
#include "nsLayoutUtils.h"
#include "nsTextFormatter.h"
#include "SVGLengthSMILType.h"
#include "SVGAttrTearoffTable.h"
#include "SVGGeometryProperty.h"

using namespace mozilla::dom;

namespace mozilla {

//----------------------------------------------------------------------
// Helper class: AutoChangeLengthNotifier
// Stack-based helper class to pair calls to WillChangeLength and
// DidChangeLength.
class MOZ_RAII AutoChangeLengthNotifier {
 public:
  AutoChangeLengthNotifier(SVGAnimatedLength* aLength, SVGElement* aSVGElement,
                           bool aDoSetAttr = true)
      : mLength(aLength), mSVGElement(aSVGElement), mDoSetAttr(aDoSetAttr) {
    MOZ_ASSERT(mLength, "Expecting non-null length");
    MOZ_ASSERT(mSVGElement, "Expecting non-null element");

    if (mDoSetAttr) {
      mUpdateBatch.emplace(aSVGElement->GetComposedDoc(), true);
      mEmptyOrOldValue =
          mSVGElement->WillChangeLength(mLength->mAttrEnum, mUpdateBatch.ref());
    }
  }

  ~AutoChangeLengthNotifier() {
    if (mDoSetAttr) {
      mSVGElement->DidChangeLength(mLength->mAttrEnum, mEmptyOrOldValue,
                                   mUpdateBatch.ref());
    }
    if (mLength->mIsAnimated) {
      mSVGElement->AnimationNeedsResample();
    }
  }

 private:
  SVGAnimatedLength* const mLength;
  SVGElement* const mSVGElement;
  Maybe<mozAutoDocUpdate> mUpdateBatch;
  nsAttrValue mEmptyOrOldValue;
  bool mDoSetAttr;
};

static SVGAttrTearoffTable<SVGAnimatedLength, DOMSVGAnimatedLength>
    sSVGAnimatedLengthTearoffTable;

/* Helper functions */

static void GetValueString(nsAString& aValueAsString, float aValue,
                           uint16_t aUnitType) {
  nsTextFormatter::ssprintf(aValueAsString, u"%g", (double)aValue);

  nsAutoString unitString;
  SVGLength::GetUnitString(unitString, aUnitType);
  aValueAsString.Append(unitString);
}

static bool GetValueFromString(const nsAString& aString, float& aValue,
                               uint16_t* aUnitType) {
  bool success;
  auto token = SVGContentUtils::GetAndEnsureOneToken(aString, success);

  if (!success) {
    return false;
  }

  RangedPtr<const char16_t> iter = SVGContentUtils::GetStartRangedPtr(token);
  const RangedPtr<const char16_t> end = SVGContentUtils::GetEndRangedPtr(token);

  if (!SVGContentUtils::ParseNumber(iter, end, aValue)) {
    return false;
  }
  const nsAString& units = Substring(iter.get(), end.get());
  *aUnitType = SVGLength::GetUnitTypeForString(units);
  return *aUnitType != SVGLength_Binding::SVG_LENGTHTYPE_UNKNOWN;
}

static float FixAxisLength(float aLength) {
  if (aLength == 0.0f) {
    LAYOUT_WARNING("zero axis length");
    return 1e-20f;
  }
  return aLength;
}

GeckoFontMetrics UserSpaceMetrics::DefaultFontMetrics() {
  return {StyleLength::FromPixels(16.0f),
          StyleLength::FromPixels(-1),
          StyleLength::FromPixels(-1),
          StyleLength::FromPixels(-1),
          StyleLength::FromPixels(-1),
          StyleLength::FromPixels(16.0f),
          0.0f,
          0.0f};
}

GeckoFontMetrics UserSpaceMetrics::GetFontMetrics(const Element* aElement) {
  GeckoFontMetrics metrics = DefaultFontMetrics();
  auto* presContext =
      aElement ? nsContentUtils::GetContextForContent(aElement) : nullptr;
  if (presContext) {
    SVGGeometryProperty::DoForComputedStyle(
        aElement, [&](const ComputedStyle* style) {
          metrics = Gecko_GetFontMetrics(
              presContext, WritingMode(style).IsVertical(), style->StyleFont(),
              style->StyleFont()->mFont.size,
              /* aUseUserFontSet = */ true,
              /* aRetrieveMathScales */ false);
        });
  }
  return metrics;
}

WritingMode UserSpaceMetrics::GetWritingMode(const Element* aElement) {
  WritingMode writingMode;
  SVGGeometryProperty::DoForComputedStyle(
      aElement,
      [&](const ComputedStyle* style) { writingMode = WritingMode(style); });
  return writingMode;
}

float UserSpaceMetrics::GetExLength(Type aType) const {
  return GetFontMetricsForType(aType).mXSize.ToCSSPixels();
}

float UserSpaceMetrics::GetChSize(Type aType) const {
  auto metrics = GetFontMetricsForType(aType);
  if (metrics.mChSize.ToCSSPixels() > 0.0) {
    return metrics.mChSize.ToCSSPixels();
  }
  auto emLength = GetEmLength(aType);
  WritingMode writingMode = GetWritingModeForType(aType);
  return writingMode.IsVertical() && !writingMode.IsSideways()
             ? emLength
             : emLength * 0.5f;
}

float UserSpaceMetrics::GetIcWidth(Type aType) const {
  auto metrics = GetFontMetricsForType(aType);
  if (metrics.mIcWidth.ToCSSPixels() > 0.0) {
    return metrics.mIcWidth.ToCSSPixels();
  }
  return GetEmLength(aType);
}

float UserSpaceMetrics::GetCapHeight(Type aType) const {
  auto metrics = GetFontMetricsForType(aType);
  if (metrics.mCapHeight.ToCSSPixels() > 0.0) {
    return metrics.mCapHeight.ToCSSPixels();
  }
  return GetEmLength(aType);
}

CSSSize UserSpaceMetrics::GetCSSViewportSizeFromContext(
    const nsPresContext* aContext) {
  return CSSPixel::FromAppUnits(aContext->GetSizeForViewportUnits());
}

SVGElementMetrics::SVGElementMetrics(const SVGElement* aSVGElement,
                                     const SVGViewportElement* aCtx)
    : mSVGElement(aSVGElement), mCtx(aCtx) {}

const Element* SVGElementMetrics::GetElementForType(Type aType) const {
  switch (aType) {
    case Type::This:
      return mSVGElement;
    case Type::Root:
      return mSVGElement ? mSVGElement->OwnerDoc()->GetRootElement() : nullptr;
    default:
      MOZ_ASSERT_UNREACHABLE("Was a new value added to the enumeration?");
      return nullptr;
  }
}

GeckoFontMetrics SVGElementMetrics::GetFontMetricsForType(Type aType) const {
  return GetFontMetrics(GetElementForType(aType));
}

WritingMode SVGElementMetrics::GetWritingModeForType(Type aType) const {
  return GetWritingMode(GetElementForType(aType));
}

float SVGElementMetrics::GetAxisLength(uint8_t aCtxType) const {
  if (!EnsureCtx()) {
    return 1.0f;
  }

  return FixAxisLength(mCtx->GetLength(aCtxType));
}

CSSSize SVGElementMetrics::GetCSSViewportSize() const {
  if (!mSVGElement) {
    return {0.0f, 0.0f};
  }
  nsPresContext* context = nsContentUtils::GetContextForContent(mSVGElement);
  if (!context) {
    return {0.0f, 0.0f};
  }
  return GetCSSViewportSizeFromContext(context);
}

bool SVGElementMetrics::EnsureCtx() const {
  if (!mCtx && mSVGElement) {
    mCtx = mSVGElement->GetCtx();
    if (!mCtx && mSVGElement->IsSVGElement(nsGkAtoms::svg)) {
      const auto* e = static_cast<const SVGViewportElement*>(mSVGElement);

      if (!e->IsInner()) {
        // mSVGElement must be the outer svg element
        mCtx = e;
      }
    }
  }
  return mCtx != nullptr;
}

NonSVGFrameUserSpaceMetrics::NonSVGFrameUserSpaceMetrics(nsIFrame* aFrame)
    : mFrame(aFrame) {
  MOZ_ASSERT(mFrame, "Need a frame");
}

float NonSVGFrameUserSpaceMetrics::GetEmLength(Type aType) const {
  switch (aType) {
    case Type::This:
      return SVGContentUtils::GetFontSize(mFrame);
    case Type::Root:
      return SVGContentUtils::GetFontSize(
          mFrame->PresContext()->Document()->GetRootElement());
    default:
      MOZ_ASSERT_UNREACHABLE("Was a new value added to the enumeration?");
      return 1.0f;
  }
}

GeckoFontMetrics NonSVGFrameUserSpaceMetrics::GetFontMetricsForType(
    Type aType) const {
  switch (aType) {
    case Type::This:
      return Gecko_GetFontMetrics(
          mFrame->PresContext(), mFrame->GetWritingMode().IsVertical(),
          mFrame->StyleFont(), mFrame->StyleFont()->mFont.size,
          /* aUseUserFontSet = */ true,
          /* aRetrieveMathScales */ false);
    case Type::Root:
      return GetFontMetrics(
          mFrame->PresContext()->Document()->GetRootElement());
    default:
      MOZ_ASSERT_UNREACHABLE("Was a new value added to the enumeration?");
      return DefaultFontMetrics();
  }
}

WritingMode NonSVGFrameUserSpaceMetrics::GetWritingModeForType(
    Type aType) const {
  switch (aType) {
    case Type::This:
      return mFrame->GetWritingMode();
    case Type::Root:
      return GetWritingMode(
          mFrame->PresContext()->Document()->GetRootElement());
    default:
      MOZ_ASSERT_UNREACHABLE("Was a new value added to the enumeration?");
      return WritingMode();
  }
}

gfx::Size NonSVGFrameUserSpaceMetrics::GetSize() const {
  return SVGIntegrationUtils::GetSVGCoordContextForNonSVGFrame(mFrame);
}

CSSSize NonSVGFrameUserSpaceMetrics::GetCSSViewportSize() const {
  return GetCSSViewportSizeFromContext(mFrame->PresContext());
}

float UserSpaceMetricsWithSize::GetAxisLength(uint8_t aCtxType) const {
  gfx::Size size = GetSize();
  float length;
  switch (aCtxType) {
    case SVGContentUtils::X:
      length = size.width;
      break;
    case SVGContentUtils::Y:
      length = size.height;
      break;
    case SVGContentUtils::XY:
      length =
          SVGContentUtils::ComputeNormalizedHypotenuse(size.width, size.height);
      break;
    default:
      MOZ_ASSERT_UNREACHABLE("Unknown axis type");
      length = 1;
      break;
  }
  return FixAxisLength(length);
}

float SVGAnimatedLength::GetPixelsPerUnit(const SVGElement* aSVGElement,
                                          uint8_t aUnitType) const {
  return SVGLength::GetPixelsPerUnit(SVGElementMetrics(aSVGElement), aUnitType,
                                     mCtxType);
}

float SVGAnimatedLength::GetPixelsPerUnit(const SVGViewportElement* aCtx,
                                          uint8_t aUnitType) const {
  return SVGLength::GetPixelsPerUnit(SVGElementMetrics(aCtx, aCtx), aUnitType,
                                     mCtxType);
}

float SVGAnimatedLength::GetPixelsPerUnit(nsIFrame* aFrame,
                                          uint8_t aUnitType) const {
  const nsIContent* content = aFrame->GetContent();
  MOZ_ASSERT(!content->IsText(), "Not expecting text content");
  if (content->IsSVGElement()) {
    return SVGLength::GetPixelsPerUnit(
        SVGElementMetrics(static_cast<const SVGElement*>(content)), aUnitType,
        mCtxType);
  }
  return SVGLength::GetPixelsPerUnit(NonSVGFrameUserSpaceMetrics(aFrame),
                                     aUnitType, mCtxType);
}

float SVGAnimatedLength::GetPixelsPerUnit(const UserSpaceMetrics& aMetrics,
                                          uint8_t aUnitType) const {
  return SVGLength::GetPixelsPerUnit(aMetrics, aUnitType, mCtxType);
}

void SVGAnimatedLength::SetBaseValueInSpecifiedUnits(float aValue,
                                                     SVGElement* aSVGElement,
                                                     bool aDoSetAttr) {
  if (mIsBaseSet && mBaseVal == aValue) {
    return;
  }

  AutoChangeLengthNotifier notifier(this, aSVGElement, aDoSetAttr);

  mBaseVal = aValue;
  mIsBaseSet = true;
  if (!mIsAnimated) {
    mAnimVal = mBaseVal;
  }
}

nsresult SVGAnimatedLength::ConvertToSpecifiedUnits(uint16_t unitType,
                                                    SVGElement* aSVGElement) {
  if (!SVGLength::IsValidUnitType(unitType)) {
    return NS_ERROR_DOM_NOT_SUPPORTED_ERR;
  }

  if (mIsBaseSet && mBaseUnitType == uint8_t(unitType)) return NS_OK;

  float valueInSpecifiedUnits;

  if (SVGLength::IsAbsoluteUnit(unitType) &&
      SVGLength::IsAbsoluteUnit(mBaseUnitType)) {
    valueInSpecifiedUnits =
        mBaseVal * SVGLength::GetAbsUnitsPerAbsUnit(unitType, mBaseUnitType);
  } else {
    float pixelsPerUnit = GetPixelsPerUnit(aSVGElement, unitType);
    if (pixelsPerUnit == 0.0f) {
      return NS_ERROR_ILLEGAL_VALUE;
    }
    float valueInPixels =
        mBaseVal * GetPixelsPerUnit(aSVGElement, mBaseUnitType);
    valueInSpecifiedUnits = valueInPixels / pixelsPerUnit;
  }

  if (!std::isfinite(valueInSpecifiedUnits)) {
    return NS_ERROR_ILLEGAL_VALUE;
  }

  // Even though we're not changing the visual effect this length will have
  // on the document, we still need to send out notifications in case we have
  // mutation listeners, since the actual string value of the attribute will
  // change.
  AutoChangeLengthNotifier notifier(this, aSVGElement);

  mBaseUnitType = uint8_t(unitType);
  if (!mIsAnimated) {
    mAnimUnitType = mBaseUnitType;
  }
  // Setting aDoSetAttr to false here will ensure we don't call
  // Will/DidChangeAngle a second time (and dispatch duplicate notifications).
  SetBaseValueInSpecifiedUnits(valueInSpecifiedUnits, aSVGElement, false);

  return NS_OK;
}

nsresult SVGAnimatedLength::NewValueSpecifiedUnits(uint16_t aUnitType,
                                                   float aValueInSpecifiedUnits,
                                                   SVGElement* aSVGElement) {
  NS_ENSURE_FINITE(aValueInSpecifiedUnits, NS_ERROR_ILLEGAL_VALUE);

  if (!SVGLength::IsValidUnitType(aUnitType)) {
    return NS_ERROR_DOM_NOT_SUPPORTED_ERR;
  }

  if (mIsBaseSet && mBaseVal == aValueInSpecifiedUnits &&
      mBaseUnitType == uint8_t(aUnitType)) {
    return NS_OK;
  }

  AutoChangeLengthNotifier notifier(this, aSVGElement);

  mBaseVal = aValueInSpecifiedUnits;
  mIsBaseSet = true;
  mBaseUnitType = uint8_t(aUnitType);
  if (!mIsAnimated) {
    mAnimVal = mBaseVal;
    mAnimUnitType = mBaseUnitType;
  }
  return NS_OK;
}

already_AddRefed<DOMSVGLength> SVGAnimatedLength::ToDOMBaseVal(
    SVGElement* aSVGElement) {
  return DOMSVGLength::GetTearOff(this, aSVGElement, false);
}

already_AddRefed<DOMSVGLength> SVGAnimatedLength::ToDOMAnimVal(
    SVGElement* aSVGElement) {
  return DOMSVGLength::GetTearOff(this, aSVGElement, true);
}

/* Implementation */

nsresult SVGAnimatedLength::SetBaseValueString(const nsAString& aValueAsString,
                                               SVGElement* aSVGElement,
                                               bool aDoSetAttr) {
  float value;
  uint16_t unitType;

  if (!GetValueFromString(aValueAsString, value, &unitType)) {
    return NS_ERROR_DOM_SYNTAX_ERR;
  }

  if (mIsBaseSet && mBaseVal == float(value) &&
      mBaseUnitType == uint8_t(unitType)) {
    return NS_OK;
  }

  AutoChangeLengthNotifier notifier(this, aSVGElement, aDoSetAttr);

  mBaseVal = value;
  mIsBaseSet = true;
  mBaseUnitType = uint8_t(unitType);
  if (!mIsAnimated) {
    mAnimVal = mBaseVal;
    mAnimUnitType = mBaseUnitType;
  }

  return NS_OK;
}

void SVGAnimatedLength::GetBaseValueString(nsAString& aValueAsString) const {
  GetValueString(aValueAsString, mBaseVal, mBaseUnitType);
}

void SVGAnimatedLength::GetAnimValueString(nsAString& aValueAsString) const {
  GetValueString(aValueAsString, mAnimVal, mAnimUnitType);
}

nsresult SVGAnimatedLength::SetBaseValue(float aValue, SVGElement* aSVGElement,
                                         bool aDoSetAttr) {
  float pixelsPerUnit = GetPixelsPerUnit(aSVGElement, mBaseUnitType);
  if (pixelsPerUnit == 0.0f) {
    return NS_ERROR_ILLEGAL_VALUE;
  }

  float valueInSpecifiedUnits = aValue / pixelsPerUnit;
  if (!std::isfinite(valueInSpecifiedUnits)) {
    return NS_ERROR_ILLEGAL_VALUE;
  }

  SetBaseValueInSpecifiedUnits(valueInSpecifiedUnits, aSVGElement, aDoSetAttr);
  return NS_OK;
}

void SVGAnimatedLength::SetAnimValueInSpecifiedUnits(float aValue,
                                                     SVGElement* aSVGElement) {
  if (mAnimVal == aValue && mIsAnimated) {
    return;
  }
  mAnimVal = aValue;
  mIsAnimated = true;
  aSVGElement->DidAnimateLength(mAttrEnum);
}

void SVGAnimatedLength::SetAnimValue(float aValue, uint16_t aUnitType,
                                     SVGElement* aSVGElement) {
  if (mIsAnimated && mAnimVal == aValue && mAnimUnitType == aUnitType) {
    return;
  }
  mAnimVal = aValue;
  mAnimUnitType = aUnitType;
  mIsAnimated = true;
  aSVGElement->DidAnimateLength(mAttrEnum);
}

already_AddRefed<DOMSVGAnimatedLength> SVGAnimatedLength::ToDOMAnimatedLength(
    SVGElement* aSVGElement) {
  RefPtr<DOMSVGAnimatedLength> svgAnimatedLength =
      sSVGAnimatedLengthTearoffTable.GetTearoff(this);
  if (!svgAnimatedLength) {
    svgAnimatedLength = new DOMSVGAnimatedLength(this, aSVGElement);
    sSVGAnimatedLengthTearoffTable.AddTearoff(this, svgAnimatedLength);
  }

  return svgAnimatedLength.forget();
}

DOMSVGAnimatedLength::~DOMSVGAnimatedLength() {
  sSVGAnimatedLengthTearoffTable.RemoveTearoff(mVal);
}

UniquePtr<SMILAttr> SVGAnimatedLength::ToSMILAttr(SVGElement* aSVGElement) {
  return MakeUnique<SMILLength>(this, aSVGElement);
}

nsresult SVGAnimatedLength::SMILLength::ValueFromString(
    const nsAString& aStr, const SVGAnimationElement* /*aSrcElement*/,
    SMILValue& aValue, bool& aPreventCachingOfSandwich) const {
  float value;
  uint16_t unitType;

  if (!GetValueFromString(aStr, value, &unitType)) {
    return NS_ERROR_DOM_SYNTAX_ERR;
  }

  SMILValue val(SVGLengthSMILType::Singleton());
  SVGLengthAndInfo* lai = static_cast<SVGLengthAndInfo*>(val.mU.mPtr);
  lai->Set(value, unitType, mVal->GetCtxType());
  lai->SetInfo(mSVGElement);
  aValue = std::move(val);
  aPreventCachingOfSandwich = !SVGLength::IsAbsoluteUnit(unitType);

  return NS_OK;
}

SMILValue SVGAnimatedLength::SMILLength::GetBaseValue() const {
  SMILValue val(SVGLengthSMILType::Singleton());
  auto* lai = static_cast<SVGLengthAndInfo*>(val.mU.mPtr);
  lai->CopyBaseFrom(*mVal);
  lai->SetInfo(mSVGElement);
  return val;
}

void SVGAnimatedLength::SMILLength::ClearAnimValue() {
  if (mVal->mIsAnimated) {
    mVal->mIsAnimated = false;
    mVal->mAnimVal = mVal->mBaseVal;
    mVal->mAnimUnitType = mVal->mBaseUnitType;
    mSVGElement->DidAnimateLength(mVal->mAttrEnum);
  }
}

nsresult SVGAnimatedLength::SMILLength::SetAnimValue(const SMILValue& aValue) {
  NS_ASSERTION(aValue.mType == SVGLengthSMILType::Singleton(),
               "Unexpected type to assign animated value");
  if (aValue.mType == SVGLengthSMILType::Singleton()) {
    SVGLengthAndInfo* lai = static_cast<SVGLengthAndInfo*>(aValue.mU.mPtr);
    mVal->SetAnimValue(lai->Value(), lai->UnitType(), mSVGElement);
  }
  return NS_OK;
}

float SVGLengthAndInfo::ConvertUnits(const SVGLengthAndInfo& aTo) const {
  if (aTo.mUnitType == mUnitType) {
    return mValue;
  }
  return SVGLength(mValue, mUnitType)
      .GetValueInSpecifiedUnit(aTo.mUnitType, aTo.Element(), aTo.mCtxType);
}

float SVGLengthAndInfo::ValueInPixels(const UserSpaceMetrics& aMetrics) const {
  return mValue == 0.0f ? 0.0f
                        : mValue * SVGLength::GetPixelsPerUnit(
                                       aMetrics, mUnitType, mCtxType);
}

void SVGLengthAndInfo::Add(const SVGLengthAndInfo& aValueToAdd,
                           uint32_t aCount) {
  mElement = aValueToAdd.mElement;

  SVGElementMetrics metrics(Element());

  // We may be dealing with two different length units, so we normalize to
  // pixels for the add:

  float currentLength = ValueInPixels(metrics);
  float lengthToAdd = aValueToAdd.ValueInPixels(metrics) * aCount;

  // And then we give the resulting value the same units as the value
  // that we're animating to/by (i.e. the same as aValueToAdd):
  mUnitType = aValueToAdd.mUnitType;
  mCtxType = aValueToAdd.mCtxType;
  mValue = (currentLength + lengthToAdd) /
           SVGLength::GetPixelsPerUnit(metrics, mUnitType, mCtxType);
}

void SVGLengthAndInfo::Interpolate(const SVGLengthAndInfo& aStart,
                                   const SVGLengthAndInfo& aEnd,
                                   double aUnitDistance,
                                   SVGLengthAndInfo& aResult) {
  MOZ_ASSERT(!aStart.mElement || aStart.mElement == aEnd.mElement,
             "Should not be interpolating between different elements");

  float startValue, endValue;
  if (!aStart.mElement || aUnitDistance > 0.5) {
    aResult.mUnitType = aEnd.mUnitType;
    aResult.mCtxType = aEnd.mCtxType;
    startValue = aStart.ConvertUnits(aEnd);
    endValue = aEnd.mValue;
  } else {
    aResult.mUnitType = aStart.mUnitType;
    aResult.mCtxType = aStart.mCtxType;
    startValue = aStart.mValue;
    endValue = aEnd.ConvertUnits(aStart);
  }
  aResult.mElement = aEnd.mElement;
  aResult.mValue = startValue + (endValue - startValue) * aUnitDistance;
}

}  // namespace mozilla