Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / platform / animation / TimingFunction.h
blob05a25a360d833fcae57e492d3c5dc0442a3ffd6f
1 /*
2 * Copyright (C) 2000 Lars Knoll (knoll@kde.org)
3 * (C) 2000 Antti Koivisto (koivisto@kde.org)
4 * (C) 2000 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2003, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
6 * Copyright (C) 2006 Graham Dennis (graham.dennis@gmail.com)
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
25 #ifndef TimingFunction_h
26 #define TimingFunction_h
28 #include "platform/animation/AnimationUtilities.h" // For blend()
29 #include "platform/animation/UnitBezier.h"
30 #include "platform/heap/Handle.h"
31 #include "platform/heap/Heap.h"
32 #include "wtf/OwnPtr.h"
33 #include "wtf/PassOwnPtr.h"
34 #include "wtf/PassRefPtr.h"
35 #include "wtf/RefCounted.h"
36 #include "wtf/StdLibExtras.h"
37 #include "wtf/text/StringBuilder.h"
38 #include "wtf/text/WTFString.h"
40 namespace blink {
42 class PLATFORM_EXPORT TimingFunction : public RefCounted<TimingFunction> {
43 public:
45 enum Type {
46 LinearFunction, CubicBezierFunction, StepsFunction
49 virtual ~TimingFunction() { }
51 Type type() const { return m_type; }
53 virtual String toString() const = 0;
55 // Evaluates the timing function at the given fraction. The accuracy parameter provides a hint as to the required
56 // accuracy and is not guaranteed.
57 virtual double evaluate(double fraction, double accuracy) const = 0;
59 // This function returns the minimum and maximum values obtainable when
60 // calling evaluate();
61 virtual void range(double* minValue, double* maxValue) const = 0;
63 enum RangeHalf {
64 Lower, // Timing function values < 0.5
65 Upper // Timing function values >= 0.5
68 struct PartitionRegion {
69 RangeHalf half;
70 double start; // inclusive
71 double end; // exclusive
73 PartitionRegion(RangeHalf half, double start, double end)
74 : half(half)
75 , start(start)
76 , end(end)
77 { }
80 // Partitions the timing function into a number of regions,
81 // representing the ranges in which the function's value is < 0.5
82 // and >= 0.5, and hence whether interpolation 0 or 1 should be
83 // used.
84 virtual void partition(Vector<PartitionRegion>& regions) const = 0;
86 protected:
87 TimingFunction(Type type)
88 : m_type(type)
92 private:
93 Type m_type;
96 class PLATFORM_EXPORT LinearTimingFunction final : public TimingFunction {
97 public:
98 static LinearTimingFunction* shared()
100 DEFINE_STATIC_REF(LinearTimingFunction, linear, (adoptRef(new LinearTimingFunction())));
101 return linear;
104 ~LinearTimingFunction() override { }
106 String toString() const override;
108 double evaluate(double fraction, double) const override;
109 void range(double* minValue, double* maxValue) const override;
110 void partition(Vector<PartitionRegion>& regions) const override;
111 private:
112 LinearTimingFunction()
113 : TimingFunction(LinearFunction)
118 class PLATFORM_EXPORT CubicBezierTimingFunction final : public TimingFunction {
119 public:
120 enum SubType {
121 Ease,
122 EaseIn,
123 EaseOut,
124 EaseInOut,
125 Custom
128 static PassRefPtr<CubicBezierTimingFunction> create(double x1, double y1, double x2, double y2)
130 return adoptRef(new CubicBezierTimingFunction(Custom, x1, y1, x2, y2));
133 static CubicBezierTimingFunction* preset(SubType subType)
135 switch (subType) {
136 case Ease:
138 DEFINE_STATIC_REF(CubicBezierTimingFunction, ease, (adoptRef(new CubicBezierTimingFunction(Ease, 0.25, 0.1, 0.25, 1.0))));
139 return ease;
141 case EaseIn:
143 DEFINE_STATIC_REF(CubicBezierTimingFunction, easeIn, (adoptRef(new CubicBezierTimingFunction(EaseIn, 0.42, 0.0, 1.0, 1.0))));
144 return easeIn;
146 case EaseOut:
148 DEFINE_STATIC_REF(CubicBezierTimingFunction, easeOut, (adoptRef(new CubicBezierTimingFunction(EaseOut, 0.0, 0.0, 0.58, 1.0))));
149 return easeOut;
151 case EaseInOut:
153 DEFINE_STATIC_REF(CubicBezierTimingFunction, easeInOut, (adoptRef(new CubicBezierTimingFunction(EaseInOut, 0.42, 0.0, 0.58, 1.0))));
154 return easeInOut;
156 default:
157 ASSERT_NOT_REACHED();
158 return 0;
162 ~CubicBezierTimingFunction() override { }
164 String toString() const override;
166 double evaluate(double fraction, double accuracy) const override;
167 void range(double* minValue, double* maxValue) const override;
168 void partition(Vector<PartitionRegion>& regions) const override;
170 double x1() const { return m_x1; }
171 double y1() const { return m_y1; }
172 double x2() const { return m_x2; }
173 double y2() const { return m_y2; }
175 SubType subType() const { return m_subType; }
177 private:
178 explicit CubicBezierTimingFunction(SubType subType, double x1, double y1, double x2, double y2)
179 : TimingFunction(CubicBezierFunction)
180 , m_x1(x1)
181 , m_y1(y1)
182 , m_x2(x2)
183 , m_y2(y2)
184 , m_subType(subType)
188 // Finds points on the cubic bezier that cross the given horizontal
189 // line, storing their x values in solution1-3 and returning the
190 // number of solutions found.
191 size_t findIntersections(double intersectionY, double& solution1, double& solution2, double& solution3) const;
193 double m_x1;
194 double m_y1;
195 double m_x2;
196 double m_y2;
197 SubType m_subType;
198 mutable OwnPtr<UnitBezier> m_bezier;
201 class PLATFORM_EXPORT StepsTimingFunction final : public TimingFunction {
202 public:
203 enum StepAtPosition {
204 Start,
205 Middle,
209 static PassRefPtr<StepsTimingFunction> create(int steps, StepAtPosition stepAtPosition)
211 return adoptRef(new StepsTimingFunction(steps, stepAtPosition));
214 static StepsTimingFunction* preset(StepAtPosition position)
216 DEFINE_STATIC_REF(StepsTimingFunction, start, create(1, Start));
217 DEFINE_STATIC_REF(StepsTimingFunction, middle, create(1, Middle));
218 DEFINE_STATIC_REF(StepsTimingFunction, end, create(1, End));
219 switch (position) {
220 case Start:
221 return start;
222 case Middle:
223 return middle;
224 case End:
225 return end;
226 default:
227 ASSERT_NOT_REACHED();
228 return end;
233 ~StepsTimingFunction() override { }
235 String toString() const override;
237 double evaluate(double fraction, double) const override;
238 void range(double* minValue, double* maxValue) const override;
239 void partition(Vector<PartitionRegion>& regions) const override;
241 int numberOfSteps() const { return m_steps; }
242 StepAtPosition stepAtPosition() const { return m_stepAtPosition; }
244 private:
245 StepsTimingFunction(int steps, StepAtPosition stepAtPosition)
246 : TimingFunction(StepsFunction)
247 , m_steps(steps)
248 , m_stepAtPosition(stepAtPosition)
252 int m_steps;
253 StepAtPosition m_stepAtPosition;
256 PLATFORM_EXPORT bool operator==(const LinearTimingFunction&, const TimingFunction&);
257 PLATFORM_EXPORT bool operator==(const CubicBezierTimingFunction&, const TimingFunction&);
258 PLATFORM_EXPORT bool operator==(const StepsTimingFunction&, const TimingFunction&);
260 PLATFORM_EXPORT bool operator==(const TimingFunction&, const TimingFunction&);
261 PLATFORM_EXPORT bool operator!=(const TimingFunction&, const TimingFunction&);
263 #define DEFINE_TIMING_FUNCTION_TYPE_CASTS(typeName) \
264 DEFINE_TYPE_CASTS( \
265 typeName##TimingFunction, TimingFunction, value, \
266 value->type() == TimingFunction::typeName##Function, \
267 value.type() == TimingFunction::typeName##Function)
269 DEFINE_TIMING_FUNCTION_TYPE_CASTS(Linear);
270 DEFINE_TIMING_FUNCTION_TYPE_CASTS(CubicBezier);
271 DEFINE_TIMING_FUNCTION_TYPE_CASTS(Steps);
273 } // namespace blink
275 #endif // TimingFunction_h