Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/layout/style/nsTimingFunction.h
Line
Count
Source (jump to first uncovered line)
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
#ifndef nsTimingFunction_h
8
#define nsTimingFunction_h
9
10
#include "nsStyleConsts.h"
11
12
struct nsTimingFunction
13
{
14
  enum class Type {
15
    Ease,         // ease
16
    Linear,       // linear
17
    EaseIn,       // ease-in
18
    EaseOut,      // ease-out
19
    EaseInOut,    // ease-in-out
20
    StepStart,    // step-start and steps(..., start)
21
    StepEnd,      // step-end, steps(..., end) and steps(...)
22
    CubicBezier,  // cubic-bezier()
23
    Frames,       // frames()
24
  };
25
26
  // Whether the timing function type is represented by a spline,
27
  // and thus will have mFunc filled in.
28
  static bool IsSplineType(Type aType)
29
0
  {
30
0
    return aType != Type::StepStart &&
31
0
           aType != Type::StepEnd &&
32
0
           aType != Type::Frames;
33
0
  }
34
35
  explicit nsTimingFunction(
36
    int32_t aTimingFunctionType = NS_STYLE_TRANSITION_TIMING_FUNCTION_EASE)
37
    : mType(Type::Ease)
38
    , mFunc{}
39
  {
40
    AssignFromKeyword(aTimingFunctionType);
41
  }
42
43
  nsTimingFunction(float x1, float y1, float x2, float y2)
44
    : mType(Type::CubicBezier)
45
  {
46
    mFunc.mX1 = x1;
47
    mFunc.mY1 = y1;
48
    mFunc.mX2 = x2;
49
    mFunc.mY2 = y2;
50
  }
51
52
  enum class Keyword { Implicit, Explicit };
53
54
  nsTimingFunction(Type aType, uint32_t aStepsOrFrames)
55
    : mType(aType)
56
  {
57
    MOZ_ASSERT(mType == Type::StepStart ||
58
               mType == Type::StepEnd ||
59
               mType == Type::Frames,
60
               "wrong type");
61
    mStepsOrFrames = aStepsOrFrames;
62
  }
63
64
  nsTimingFunction(const nsTimingFunction& aOther)
65
0
  {
66
0
    *this = aOther;
67
0
  }
68
69
  Type mType;
70
  union {
71
    struct {
72
      float mX1;
73
      float mY1;
74
      float mX2;
75
      float mY2;
76
    } mFunc;
77
    struct {
78
      uint32_t mStepsOrFrames;
79
    };
80
  };
81
82
  nsTimingFunction&
83
  operator=(const nsTimingFunction& aOther)
84
0
  {
85
0
    if (&aOther == this) {
86
0
      return *this;
87
0
    }
88
0
89
0
    mType = aOther.mType;
90
0
91
0
    if (HasSpline()) {
92
0
      mFunc.mX1 = aOther.mFunc.mX1;
93
0
      mFunc.mY1 = aOther.mFunc.mY1;
94
0
      mFunc.mX2 = aOther.mFunc.mX2;
95
0
      mFunc.mY2 = aOther.mFunc.mY2;
96
0
    } else {
97
0
      mStepsOrFrames = aOther.mStepsOrFrames;
98
0
    }
99
0
100
0
    return *this;
101
0
  }
102
103
  bool operator==(const nsTimingFunction& aOther) const
104
0
  {
105
0
    if (mType != aOther.mType) {
106
0
      return false;
107
0
    }
108
0
    if (HasSpline()) {
109
0
      return mFunc.mX1 == aOther.mFunc.mX1 && mFunc.mY1 == aOther.mFunc.mY1 &&
110
0
             mFunc.mX2 == aOther.mFunc.mX2 && mFunc.mY2 == aOther.mFunc.mY2;
111
0
    }
112
0
    return mStepsOrFrames == aOther.mStepsOrFrames;
113
0
  }
114
115
  bool operator!=(const nsTimingFunction& aOther) const
116
  {
117
    return !(*this == aOther);
118
  }
119
120
0
  bool HasSpline() const { return IsSplineType(mType); }
121
122
private:
123
  void AssignFromKeyword(int32_t aTimingFunctionType);
124
};
125
126
#endif // nsTimingFunction_h