2
Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3
Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
4
Copyright (C) 2011 Rik Cabanier (cabanier@adobe.com)
5
Copyright (C) 2011 Adobe Systems Incorporated. All rights reserved.
7
This library is free software; you can redistribute it and/or
8
modify it under the terms of the GNU Library General Public
9
License as published by the Free Software Foundation; either
10
version 2 of the License, or (at your option) any later version.
12
This library is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
Library General Public License for more details.
17
You should have received a copy of the GNU Library General Public License
18
along with this library; see the file COPYING.LIB. If not, write to
19
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20
Boston, MA 02110-1301, USA.
26
#include "AnimationUtilities.h"
27
#include <wtf/Assertions.h>
28
#include <wtf/FastAllocBase.h>
29
#include <wtf/Forward.h>
30
#include <wtf/HashMap.h>
31
#include <wtf/MathExtras.h>
32
#include <wtf/PassOwnArrayPtr.h>
37
Auto, Relative, Percent, Fixed,
38
Intrinsic, MinIntrinsic,
39
MinContent, MaxContent, FillAvailable, FitContent,
41
ViewportPercentageWidth, ViewportPercentageHeight, ViewportPercentageMin,
45
class CalculationValue;
48
WTF_MAKE_FAST_ALLOCATED;
51
: m_intValue(0), m_quirk(false), m_type(Auto), m_isFloat(false)
56
: m_intValue(0), m_quirk(false), m_type(t), m_isFloat(false)
58
ASSERT(t != Calculated);
61
Length(int v, LengthType t, bool q = false)
62
: m_intValue(v), m_quirk(q), m_type(t), m_isFloat(false)
64
ASSERT(t != Calculated);
67
Length(LayoutUnit v, LengthType t, bool q = false)
68
: m_floatValue(v.toFloat()), m_quirk(q), m_type(t), m_isFloat(true)
70
ASSERT(t != Calculated);
73
Length(float v, LengthType t, bool q = false)
74
: m_floatValue(v), m_quirk(q), m_type(t), m_isFloat(true)
76
ASSERT(t != Calculated);
79
Length(double v, LengthType t, bool q = false)
80
: m_quirk(q), m_type(t), m_isFloat(true)
82
m_floatValue = static_cast<float>(v);
85
explicit Length(PassRefPtr<CalculationValue>);
87
Length(const Length& length)
89
initFromLength(length);
92
Length& operator=(const Length& length)
94
initFromLength(length);
101
decrementCalculatedRef();
104
bool operator==(const Length& o) const { return (m_type == o.m_type) && (m_quirk == o.m_quirk) && (isUndefined() || (getFloatValue() == o.getFloatValue()) || isCalculatedEqual(o)); }
105
bool operator!=(const Length& o) const { return !(*this == o); }
107
const Length& operator*=(float v)
109
if (isCalculated()) {
110
ASSERT_NOT_REACHED();
115
m_floatValue = static_cast<float>(m_floatValue * v);
117
m_intValue = static_cast<int>(m_intValue * v);
122
inline float value() const
124
return getFloatValue();
129
if (isCalculated()) {
130
ASSERT_NOT_REACHED();
133
return getIntValue();
136
float percent() const
138
ASSERT(type() == Percent);
139
return getFloatValue();
142
PassRefPtr<CalculationValue> calculationValue() const;
144
LengthType type() const { return static_cast<LengthType>(m_type); }
145
bool quirk() const { return m_quirk; }
147
void setQuirk(bool quirk)
152
void setValue(LengthType t, int value)
159
void setValue(int value)
161
if (isCalculated()) {
162
ASSERT_NOT_REACHED();
165
setValue(Fixed, value);
168
void setValue(LengthType t, float value)
171
m_floatValue = value;
175
void setValue(LengthType t, LayoutUnit value)
178
m_floatValue = value;
182
void setValue(float value)
184
*this = Length(value, Fixed);
187
bool isUndefined() const { return type() == Undefined; }
189
// FIXME calc: https://bugs.webkit.org/show_bug.cgi?id=80357. A calculated Length
190
// always contains a percentage, and without a maxValue passed to these functions
191
// it's impossible to determine the sign or zero-ness. We assume all calc values
192
// are positive and non-zero for now.
195
ASSERT(!isUndefined());
199
return m_isFloat ? !m_floatValue : !m_intValue;
201
bool isPositive() const
208
return getFloatValue() > 0;
210
bool isNegative() const
212
if (isUndefined() || isCalculated())
215
return getFloatValue() < 0;
218
bool isAuto() const { return type() == Auto; }
219
bool isRelative() const { return type() == Relative; }
220
bool isPercent() const { return type() == Percent || type() == Calculated; }
221
bool isFixed() const { return type() == Fixed; }
222
bool isIntrinsicOrAuto() const { return type() == Auto || isLegacyIntrinsic() || isIntrinsic(); }
223
bool isLegacyIntrinsic() const { return type() == Intrinsic || type() == MinIntrinsic; }
224
bool isIntrinsic() const { return type() == MinContent || type() == MaxContent || type() == FillAvailable || type() == FitContent; }
225
bool isSpecified() const { return type() == Fixed || type() == Percent || type() == Calculated || isViewportPercentage(); }
226
bool isCalculated() const { return type() == Calculated; }
227
bool isCalculatedEqual(const Length&) const;
229
Length blend(const Length& from, double progress) const
231
// Blend two lengths to produce a new length that is in between them. Used for animation.
232
if (from.type() == Calculated || type() == Calculated)
233
return blendMixedTypes(from, progress);
235
if (!from.isZero() && !isZero() && from.type() != type())
236
return blendMixedTypes(from, progress);
238
if (from.isZero() && isZero())
241
LengthType resultType = type();
243
resultType = from.type();
245
if (resultType == Percent) {
246
float fromPercent = from.isZero() ? 0 : from.percent();
247
float toPercent = isZero() ? 0 : percent();
248
return Length(WebCore::blend(fromPercent, toPercent, progress), Percent);
251
float fromValue = from.isZero() ? 0 : from.value();
252
float toValue = isZero() ? 0 : value();
253
return Length(WebCore::blend(fromValue, toValue, progress), resultType);
256
float getFloatValue() const
258
ASSERT(!isUndefined());
259
return m_isFloat ? m_floatValue : m_intValue;
261
float nonNanCalculatedValue(int maxValue) const;
263
bool isViewportPercentage() const
265
LengthType lengthType = type();
266
return lengthType >= ViewportPercentageWidth && lengthType <= ViewportPercentageMin;
268
float viewportPercentageLength() const
270
ASSERT(isViewportPercentage());
271
return getFloatValue();
274
int getIntValue() const
276
ASSERT(!isUndefined());
277
return m_isFloat ? static_cast<int>(m_floatValue) : m_intValue;
279
void initFromLength(const Length &length)
281
m_quirk = length.m_quirk;
282
m_type = length.m_type;
283
m_isFloat = length.m_isFloat;
286
m_floatValue = length.m_floatValue;
288
m_intValue = length.m_intValue;
291
incrementCalculatedRef();
294
Length blendMixedTypes(const Length& from, double progress) const;
296
int calculationHandle() const
298
ASSERT(isCalculated());
299
return getIntValue();
301
void incrementCalculatedRef() const;
302
void decrementCalculatedRef() const;
309
unsigned char m_type;
313
PassOwnArrayPtr<Length> newCoordsArray(const String&, int& len);
314
PassOwnArrayPtr<Length> newLengthArray(const String&, int& len);
316
} // namespace WebCore