layout/style/nsStyleAnimation.h

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /* Utilities for animation of computed style values */
michael@0 7
michael@0 8 #ifndef nsStyleAnimation_h_
michael@0 9 #define nsStyleAnimation_h_
michael@0 10
michael@0 11 #include "nsStringFwd.h"
michael@0 12 #include "nsCRTGlue.h"
michael@0 13 #include "nsStringBuffer.h"
michael@0 14 #include "nsCSSProperty.h"
michael@0 15 #include "nsCoord.h"
michael@0 16 #include "nsColor.h"
michael@0 17 #include "nsCSSValue.h"
michael@0 18
michael@0 19 class nsStyleContext;
michael@0 20 class gfx3DMatrix;
michael@0 21
michael@0 22 namespace mozilla {
michael@0 23 namespace dom {
michael@0 24 class Element;
michael@0 25 } // namespace dom
michael@0 26 } // namespace mozilla
michael@0 27
michael@0 28 /**
michael@0 29 * Utility class to handle animated style values
michael@0 30 */
michael@0 31 class nsStyleAnimation {
michael@0 32 public:
michael@0 33 class Value;
michael@0 34
michael@0 35 // Mathematical methods
michael@0 36 // --------------------
michael@0 37 /**
michael@0 38 * Adds |aCount| copies of |aValueToAdd| to |aDest|. The result of this
michael@0 39 * addition is stored in aDest.
michael@0 40 *
michael@0 41 * Note that if |aCount| is 0, then |aDest| will be unchanged. Also, if
michael@0 42 * this method fails, then |aDest| will be unchanged.
michael@0 43 *
michael@0 44 * @param aDest The value to add to.
michael@0 45 * @param aValueToAdd The value to add.
michael@0 46 * @param aCount The number of times to add aValueToAdd.
michael@0 47 * @return true on success, false on failure.
michael@0 48 */
michael@0 49 static bool Add(nsCSSProperty aProperty, Value& aDest,
michael@0 50 const Value& aValueToAdd, uint32_t aCount) {
michael@0 51 return AddWeighted(aProperty, 1.0, aDest, aCount, aValueToAdd, aDest);
michael@0 52 }
michael@0 53
michael@0 54 /**
michael@0 55 * Calculates a measure of 'distance' between two values.
michael@0 56 *
michael@0 57 * This measure of Distance is guaranteed to be proportional to
michael@0 58 * portions passed to Interpolate, Add, or AddWeighted. However, for
michael@0 59 * some types of Value it may not produce sensible results for paced
michael@0 60 * animation.
michael@0 61 *
michael@0 62 * If this method succeeds, the returned distance value is guaranteed to be
michael@0 63 * non-negative.
michael@0 64 *
michael@0 65 * @param aStartValue The start of the interval for which the distance
michael@0 66 * should be calculated.
michael@0 67 * @param aEndValue The end of the interval for which the distance
michael@0 68 * should be calculated.
michael@0 69 * @param aDistance The result of the calculation.
michael@0 70 * @return true on success, false on failure.
michael@0 71 */
michael@0 72 static bool ComputeDistance(nsCSSProperty aProperty,
michael@0 73 const Value& aStartValue,
michael@0 74 const Value& aEndValue,
michael@0 75 double& aDistance);
michael@0 76
michael@0 77 /**
michael@0 78 * Calculates an interpolated value that is the specified |aPortion| between
michael@0 79 * the two given values.
michael@0 80 *
michael@0 81 * This really just does the following calculation:
michael@0 82 * aResultValue = (1.0 - aPortion) * aStartValue + aPortion * aEndValue
michael@0 83 *
michael@0 84 * @param aStartValue The value defining the start of the interval of
michael@0 85 * interpolation.
michael@0 86 * @param aEndValue The value defining the end of the interval of
michael@0 87 * interpolation.
michael@0 88 * @param aPortion A number in the range [0.0, 1.0] defining the
michael@0 89 * distance of the interpolated value in the interval.
michael@0 90 * @param [out] aResultValue The resulting interpolated value.
michael@0 91 * @return true on success, false on failure.
michael@0 92 */
michael@0 93 static bool Interpolate(nsCSSProperty aProperty,
michael@0 94 const Value& aStartValue,
michael@0 95 const Value& aEndValue,
michael@0 96 double aPortion,
michael@0 97 Value& aResultValue) {
michael@0 98 return AddWeighted(aProperty, 1.0 - aPortion, aStartValue,
michael@0 99 aPortion, aEndValue, aResultValue);
michael@0 100 }
michael@0 101
michael@0 102 /**
michael@0 103 * Does the calculation:
michael@0 104 * aResultValue = aCoeff1 * aValue1 + aCoeff2 * aValue2
michael@0 105 *
michael@0 106 * @param [out] aResultValue The resulting interpolated value. May be
michael@0 107 * the same as aValue1 or aValue2.
michael@0 108 * @return true on success, false on failure.
michael@0 109 *
michael@0 110 * NOTE: Current callers always pass aCoeff1 and aCoeff2 >= 0. They
michael@0 111 * are currently permitted to be negative; however, if, as we add
michael@0 112 * support more value types types, we find that this causes
michael@0 113 * difficulty, we might change this to restrict them to being
michael@0 114 * positive.
michael@0 115 */
michael@0 116 static bool AddWeighted(nsCSSProperty aProperty,
michael@0 117 double aCoeff1, const Value& aValue1,
michael@0 118 double aCoeff2, const Value& aValue2,
michael@0 119 Value& aResultValue);
michael@0 120
michael@0 121 // Type-conversion methods
michael@0 122 // -----------------------
michael@0 123 /**
michael@0 124 * Creates a computed value for the given specified value
michael@0 125 * (property ID + string). A style context is needed in case the
michael@0 126 * specified value depends on inherited style or on the values of other
michael@0 127 * properties.
michael@0 128 *
michael@0 129 * @param aProperty The property whose value we're computing.
michael@0 130 * @param aTargetElement The content node to which our computed value is
michael@0 131 * applicable.
michael@0 132 * @param aSpecifiedValue The specified value, from which we'll build our
michael@0 133 * computed value.
michael@0 134 * @param aUseSVGMode A flag to indicate whether we should parse
michael@0 135 * |aSpecifiedValue| in SVG mode.
michael@0 136 * @param [out] aComputedValue The resulting computed value.
michael@0 137 * @param [out] aIsContextSensitive
michael@0 138 * Set to true if |aSpecifiedValue| may produce
michael@0 139 * a different |aComputedValue| depending on other CSS
michael@0 140 * properties on |aTargetElement| or its ancestors.
michael@0 141 * false otherwise.
michael@0 142 * Note that the operation of this method is
michael@0 143 * significantly faster when |aIsContextSensitive| is
michael@0 144 * nullptr.
michael@0 145 * @return true on success, false on failure.
michael@0 146 */
michael@0 147 static bool ComputeValue(nsCSSProperty aProperty,
michael@0 148 mozilla::dom::Element* aTargetElement,
michael@0 149 const nsAString& aSpecifiedValue,
michael@0 150 bool aUseSVGMode,
michael@0 151 Value& aComputedValue,
michael@0 152 bool* aIsContextSensitive = nullptr);
michael@0 153
michael@0 154 /**
michael@0 155 * Creates a specified value for the given computed value.
michael@0 156 *
michael@0 157 * The first overload fills in an nsCSSValue object; the second
michael@0 158 * produces a string. The nsCSSValue result may depend on objects
michael@0 159 * owned by the |aComputedValue| object, so users of that variant
michael@0 160 * must keep |aComputedValue| alive longer than |aSpecifiedValue|.
michael@0 161 *
michael@0 162 * @param aProperty The property whose value we're uncomputing.
michael@0 163 * @param aComputedValue The computed value to be converted.
michael@0 164 * @param [out] aSpecifiedValue The resulting specified value.
michael@0 165 * @return true on success, false on failure.
michael@0 166 */
michael@0 167 static bool UncomputeValue(nsCSSProperty aProperty,
michael@0 168 const Value& aComputedValue,
michael@0 169 nsCSSValue& aSpecifiedValue);
michael@0 170 static bool UncomputeValue(nsCSSProperty aProperty,
michael@0 171 const Value& aComputedValue,
michael@0 172 nsAString& aSpecifiedValue);
michael@0 173
michael@0 174 /**
michael@0 175 * Gets the computed value for the given property from the given style
michael@0 176 * context.
michael@0 177 *
michael@0 178 * @param aProperty The property whose value we're looking up.
michael@0 179 * @param aStyleContext The style context to check for the computed value.
michael@0 180 * @param [out] aComputedValue The resulting computed value.
michael@0 181 * @return true on success, false on failure.
michael@0 182 */
michael@0 183 static bool ExtractComputedValue(nsCSSProperty aProperty,
michael@0 184 nsStyleContext* aStyleContext,
michael@0 185 Value& aComputedValue);
michael@0 186
michael@0 187 /**
michael@0 188 * Interpolates between 2 matrices by decomposing them.
michael@0 189 *
michael@0 190 * @param aMatrix1 First matrix, using CSS pixel units.
michael@0 191 * @param aMatrix2 Second matrix, using CSS pixel units.
michael@0 192 * @param aProgress Interpolation value in the range [0.0, 1.0]
michael@0 193 */
michael@0 194 static gfx3DMatrix InterpolateTransformMatrix(const gfx3DMatrix &aMatrix1,
michael@0 195 const gfx3DMatrix &aMatrix2,
michael@0 196 double aProgress);
michael@0 197
michael@0 198 static already_AddRefed<nsCSSValue::Array>
michael@0 199 AppendTransformFunction(nsCSSKeyword aTransformFunction,
michael@0 200 nsCSSValueList**& aListTail);
michael@0 201
michael@0 202 /**
michael@0 203 * The types and values for the values that we extract and animate.
michael@0 204 */
michael@0 205 enum Unit {
michael@0 206 eUnit_Null, // not initialized
michael@0 207 eUnit_Normal,
michael@0 208 eUnit_Auto,
michael@0 209 eUnit_None,
michael@0 210 eUnit_Enumerated,
michael@0 211 eUnit_Visibility, // special case for transitions (which converts
michael@0 212 // Enumerated to Visibility as needed)
michael@0 213 eUnit_Integer,
michael@0 214 eUnit_Coord,
michael@0 215 eUnit_Percent,
michael@0 216 eUnit_Float,
michael@0 217 eUnit_Color,
michael@0 218 eUnit_Calc, // nsCSSValue* (never null), always with a single
michael@0 219 // calc() expression that's either length or length+percent
michael@0 220 eUnit_CSSValuePair, // nsCSSValuePair* (never null)
michael@0 221 eUnit_CSSValueTriplet, // nsCSSValueTriplet* (never null)
michael@0 222 eUnit_CSSRect, // nsCSSRect* (never null)
michael@0 223 eUnit_Dasharray, // nsCSSValueList* (never null)
michael@0 224 eUnit_Filter, // nsCSSValueList* (may be null)
michael@0 225 eUnit_Shadow, // nsCSSValueList* (may be null)
michael@0 226 eUnit_Transform, // nsCSSValueList* (never null)
michael@0 227 eUnit_BackgroundPosition, // nsCSSValueList* (never null)
michael@0 228 eUnit_CSSValuePairList, // nsCSSValuePairList* (never null)
michael@0 229 eUnit_UnparsedString // nsStringBuffer* (never null)
michael@0 230 };
michael@0 231
michael@0 232 class Value {
michael@0 233 private:
michael@0 234 Unit mUnit;
michael@0 235 union {
michael@0 236 int32_t mInt;
michael@0 237 nscoord mCoord;
michael@0 238 float mFloat;
michael@0 239 nscolor mColor;
michael@0 240 nsCSSValue* mCSSValue;
michael@0 241 nsCSSValuePair* mCSSValuePair;
michael@0 242 nsCSSValueTriplet* mCSSValueTriplet;
michael@0 243 nsCSSRect* mCSSRect;
michael@0 244 nsCSSValueList* mCSSValueList;
michael@0 245 nsCSSValueSharedList* mCSSValueSharedList;
michael@0 246 nsCSSValuePairList* mCSSValuePairList;
michael@0 247 nsStringBuffer* mString;
michael@0 248 } mValue;
michael@0 249 public:
michael@0 250 Unit GetUnit() const {
michael@0 251 NS_ASSERTION(mUnit != eUnit_Null, "uninitialized");
michael@0 252 return mUnit;
michael@0 253 }
michael@0 254
michael@0 255 // Accessor to let us verify assumptions about presence of null unit,
michael@0 256 // without tripping the assertion in GetUnit().
michael@0 257 bool IsNull() const {
michael@0 258 return mUnit == eUnit_Null;
michael@0 259 }
michael@0 260
michael@0 261 int32_t GetIntValue() const {
michael@0 262 NS_ASSERTION(IsIntUnit(mUnit), "unit mismatch");
michael@0 263 return mValue.mInt;
michael@0 264 }
michael@0 265 nscoord GetCoordValue() const {
michael@0 266 NS_ASSERTION(mUnit == eUnit_Coord, "unit mismatch");
michael@0 267 return mValue.mCoord;
michael@0 268 }
michael@0 269 float GetPercentValue() const {
michael@0 270 NS_ASSERTION(mUnit == eUnit_Percent, "unit mismatch");
michael@0 271 return mValue.mFloat;
michael@0 272 }
michael@0 273 float GetFloatValue() const {
michael@0 274 NS_ASSERTION(mUnit == eUnit_Float, "unit mismatch");
michael@0 275 return mValue.mFloat;
michael@0 276 }
michael@0 277 nscolor GetColorValue() const {
michael@0 278 NS_ASSERTION(mUnit == eUnit_Color, "unit mismatch");
michael@0 279 return mValue.mColor;
michael@0 280 }
michael@0 281 nsCSSValue* GetCSSValueValue() const {
michael@0 282 NS_ASSERTION(IsCSSValueUnit(mUnit), "unit mismatch");
michael@0 283 return mValue.mCSSValue;
michael@0 284 }
michael@0 285 nsCSSValuePair* GetCSSValuePairValue() const {
michael@0 286 NS_ASSERTION(IsCSSValuePairUnit(mUnit), "unit mismatch");
michael@0 287 return mValue.mCSSValuePair;
michael@0 288 }
michael@0 289 nsCSSValueTriplet* GetCSSValueTripletValue() const {
michael@0 290 NS_ASSERTION(IsCSSValueTripletUnit(mUnit), "unit mismatch");
michael@0 291 return mValue.mCSSValueTriplet;
michael@0 292 }
michael@0 293 nsCSSRect* GetCSSRectValue() const {
michael@0 294 NS_ASSERTION(IsCSSRectUnit(mUnit), "unit mismatch");
michael@0 295 return mValue.mCSSRect;
michael@0 296 }
michael@0 297 nsCSSValueList* GetCSSValueListValue() const {
michael@0 298 NS_ASSERTION(IsCSSValueListUnit(mUnit), "unit mismatch");
michael@0 299 return mValue.mCSSValueList;
michael@0 300 }
michael@0 301 nsCSSValueSharedList* GetCSSValueSharedListValue() const {
michael@0 302 NS_ASSERTION(IsCSSValueSharedListValue(mUnit), "unit mismatch");
michael@0 303 return mValue.mCSSValueSharedList;
michael@0 304 }
michael@0 305 nsCSSValuePairList* GetCSSValuePairListValue() const {
michael@0 306 NS_ASSERTION(IsCSSValuePairListUnit(mUnit), "unit mismatch");
michael@0 307 return mValue.mCSSValuePairList;
michael@0 308 }
michael@0 309 const char16_t* GetStringBufferValue() const {
michael@0 310 NS_ASSERTION(IsStringUnit(mUnit), "unit mismatch");
michael@0 311 return GetBufferValue(mValue.mString);
michael@0 312 }
michael@0 313
michael@0 314 void GetStringValue(nsAString& aBuffer) const {
michael@0 315 NS_ASSERTION(IsStringUnit(mUnit), "unit mismatch");
michael@0 316 aBuffer.Truncate();
michael@0 317 uint32_t len = NS_strlen(GetBufferValue(mValue.mString));
michael@0 318 mValue.mString->ToString(len, aBuffer);
michael@0 319 }
michael@0 320
michael@0 321 explicit Value(Unit aUnit = eUnit_Null) : mUnit(aUnit) {
michael@0 322 NS_ASSERTION(aUnit == eUnit_Null || aUnit == eUnit_Normal ||
michael@0 323 aUnit == eUnit_Auto || aUnit == eUnit_None,
michael@0 324 "must be valueless unit");
michael@0 325 }
michael@0 326 Value(const Value& aOther) : mUnit(eUnit_Null) { *this = aOther; }
michael@0 327 enum IntegerConstructorType { IntegerConstructor };
michael@0 328 Value(int32_t aInt, Unit aUnit, IntegerConstructorType);
michael@0 329 enum CoordConstructorType { CoordConstructor };
michael@0 330 Value(nscoord aLength, CoordConstructorType);
michael@0 331 enum PercentConstructorType { PercentConstructor };
michael@0 332 Value(float aPercent, PercentConstructorType);
michael@0 333 enum FloatConstructorType { FloatConstructor };
michael@0 334 Value(float aFloat, FloatConstructorType);
michael@0 335 enum ColorConstructorType { ColorConstructor };
michael@0 336 Value(nscolor aColor, ColorConstructorType);
michael@0 337
michael@0 338 ~Value() { FreeValue(); }
michael@0 339
michael@0 340 void SetNormalValue();
michael@0 341 void SetAutoValue();
michael@0 342 void SetNoneValue();
michael@0 343 void SetIntValue(int32_t aInt, Unit aUnit);
michael@0 344 void SetCoordValue(nscoord aCoord);
michael@0 345 void SetPercentValue(float aPercent);
michael@0 346 void SetFloatValue(float aFloat);
michael@0 347 void SetColorValue(nscolor aColor);
michael@0 348 void SetUnparsedStringValue(const nsString& aString);
michael@0 349
michael@0 350 // These setters take ownership of |aValue|, and are therefore named
michael@0 351 // "SetAndAdopt*".
michael@0 352 void SetAndAdoptCSSValueValue(nsCSSValue *aValue, Unit aUnit);
michael@0 353 void SetAndAdoptCSSValuePairValue(nsCSSValuePair *aValue, Unit aUnit);
michael@0 354 void SetAndAdoptCSSValueTripletValue(nsCSSValueTriplet *aValue, Unit aUnit);
michael@0 355 void SetAndAdoptCSSRectValue(nsCSSRect *aValue, Unit aUnit);
michael@0 356 void SetAndAdoptCSSValueListValue(nsCSSValueList *aValue, Unit aUnit);
michael@0 357 void SetAndAdoptCSSValuePairListValue(nsCSSValuePairList *aValue);
michael@0 358
michael@0 359 void SetTransformValue(nsCSSValueSharedList* aList);
michael@0 360
michael@0 361 Value& operator=(const Value& aOther);
michael@0 362
michael@0 363 bool operator==(const Value& aOther) const;
michael@0 364 bool operator!=(const Value& aOther) const
michael@0 365 { return !(*this == aOther); }
michael@0 366
michael@0 367 private:
michael@0 368 void FreeValue();
michael@0 369
michael@0 370 static const char16_t* GetBufferValue(nsStringBuffer* aBuffer) {
michael@0 371 return static_cast<char16_t*>(aBuffer->Data());
michael@0 372 }
michael@0 373
michael@0 374 static bool IsIntUnit(Unit aUnit) {
michael@0 375 return aUnit == eUnit_Enumerated || aUnit == eUnit_Visibility ||
michael@0 376 aUnit == eUnit_Integer;
michael@0 377 }
michael@0 378 static bool IsCSSValueUnit(Unit aUnit) {
michael@0 379 return aUnit == eUnit_Calc;
michael@0 380 }
michael@0 381 static bool IsCSSValuePairUnit(Unit aUnit) {
michael@0 382 return aUnit == eUnit_CSSValuePair;
michael@0 383 }
michael@0 384 static bool IsCSSValueTripletUnit(Unit aUnit) {
michael@0 385 return aUnit == eUnit_CSSValueTriplet;
michael@0 386 }
michael@0 387 static bool IsCSSRectUnit(Unit aUnit) {
michael@0 388 return aUnit == eUnit_CSSRect;
michael@0 389 }
michael@0 390 static bool IsCSSValueListUnit(Unit aUnit) {
michael@0 391 return aUnit == eUnit_Dasharray || aUnit == eUnit_Filter ||
michael@0 392 aUnit == eUnit_Shadow ||
michael@0 393 aUnit == eUnit_BackgroundPosition;
michael@0 394 }
michael@0 395 static bool IsCSSValueSharedListValue(Unit aUnit) {
michael@0 396 return aUnit == eUnit_Transform;
michael@0 397 }
michael@0 398 static bool IsCSSValuePairListUnit(Unit aUnit) {
michael@0 399 return aUnit == eUnit_CSSValuePairList;
michael@0 400 }
michael@0 401 static bool IsStringUnit(Unit aUnit) {
michael@0 402 return aUnit == eUnit_UnparsedString;
michael@0 403 }
michael@0 404 };
michael@0 405 };
michael@0 406
michael@0 407 #endif

mercurial