SMILType.h (8764B)
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 DOM_SMIL_SMILTYPE_H_ 8 #define DOM_SMIL_SMILTYPE_H_ 9 10 #include "nscore.h" 11 12 namespace mozilla { 13 14 class SMILValue; 15 16 ////////////////////////////////////////////////////////////////////////////// 17 // SMILType: Interface for defining the basic operations needed for animating 18 // a particular kind of data (e.g. lengths, colors, transformation matrices). 19 // 20 // This interface is never used directly but always through a SMILValue that 21 // bundles together a pointer to a concrete implementation of this interface and 22 // the data upon which it should operate. 23 // 24 // We keep the data and type separate rather than just providing different 25 // subclasses of SMILValue. This is so that sizeof(SMILValue) is the same 26 // for all value types, allowing us to have a type-agnostic nsTArray of 27 // SMILValue objects (actual objects, not pointers). It also allows most 28 // SMILValues (except those that need to allocate extra memory for their 29 // data) to be allocated on the stack and directly assigned to one another 30 // provided performance benefits for the animation code. 31 // 32 // Note that different types have different capabilities. Roughly speaking there 33 // are probably three main types: 34 // 35 // +---------------------+---------------+-------------+------------------+ 36 // | CATEGORY: | DISCRETE | LINEAR | ADDITIVE | 37 // +---------------------+---------------+-------------+------------------+ 38 // | Example: | strings, | path data? | lengths, | 39 // | | color k/words?| | RGB color values | 40 // | | | | | 41 // | -- Assign? | X | X | X | 42 // | -- Add? | - | X? | X | 43 // | -- SandwichAdd? | - | -? | X | 44 // | -- ComputeDistance? | - | - | X? | 45 // | -- Interpolate? | - | X | X | 46 // +---------------------+---------------+-------------+------------------+ 47 // 48 49 class SMILType { 50 /** 51 * Only give the SMILValue class access to this interface. 52 */ 53 friend class SMILValue; 54 55 protected: 56 /** 57 * Initialises aValue and sets it to some identity value such that adding 58 * aValue to another value of the same type has no effect. 59 * 60 * @pre aValue.IsNull() 61 * @post aValue.mType == this 62 */ 63 virtual void InitValue(SMILValue& aValue) const = 0; 64 65 /** 66 * Destroys any data associated with a value of this type. 67 * 68 * @pre aValue.mType == this 69 * @post aValue.IsNull() 70 */ 71 virtual void DestroyValue(SMILValue& aValue) const = 0; 72 73 /** 74 * Assign this object the value of another. Think of this as the assignment 75 * operator. 76 * 77 * @param aDest The left-hand side of the assignment. 78 * @param aSrc The right-hand side of the assignment. 79 * @return NS_OK on success, an error code on failure such as when the 80 * underlying type of the specified object differs. 81 * 82 * @pre aDest.mType == aSrc.mType == this 83 */ 84 virtual nsresult Assign(SMILValue& aDest, const SMILValue& aSrc) const = 0; 85 86 /** 87 * Test two SMILValue objects (of this SMILType) for equality. 88 * 89 * A return value of true represents a guarantee that aLeft and aRight are 90 * equal. (That is, they would behave identically if passed to the methods 91 * Add, SandwichAdd, ComputeDistance, and Interpolate). 92 * 93 * A return value of false simply indicates that we make no guarantee 94 * about equality. 95 * 96 * NOTE: It's perfectly legal for implementations of this method to return 97 * false in all cases. However, smarter implementations will make this 98 * method more useful for optimization. 99 * 100 * @param aLeft The left-hand side of the equality check. 101 * @param aRight The right-hand side of the equality check. 102 * @return true if we're sure the values are equal, false otherwise. 103 * 104 * @pre aDest.mType == aSrc.mType == this 105 */ 106 virtual bool IsEqual(const SMILValue& aLeft, 107 const SMILValue& aRight) const = 0; 108 109 /** 110 * Adds two values. 111 * 112 * The count parameter facilitates repetition. 113 * 114 * By equation, 115 * 116 * aDest += aValueToAdd * aCount 117 * 118 * Therefore, if aCount == 0, aDest will be unaltered. 119 * 120 * This method will fail if this data type is not additive or the value was 121 * not specified using an additive syntax. 122 * 123 * See SVG 1.1, section 19.2.5. In particular, 124 * 125 * "If a given attribute or property can take values of keywords (which are 126 * not additive) or numeric values (which are additive), then additive 127 * animations are possible if the subsequent animation uses a numeric value 128 * even if the base animation uses a keyword value; however, if the 129 * subsequent animation uses a keyword value, additive animation is not 130 * possible." 131 * 132 * If this method fails (e.g. because the data type is not additive), aDest 133 * will be unaltered. 134 * 135 * @param aDest The value to add to. 136 * @param aValueToAdd The value to add. 137 * @param aCount The number of times to add aValueToAdd. 138 * @return NS_OK on success, an error code on failure. 139 * 140 * @pre aValueToAdd.mType == aDest.mType == this 141 */ 142 virtual nsresult Add(SMILValue& aDest, const SMILValue& aValueToAdd, 143 uint32_t aCount) const = 0; 144 145 /** 146 * Adds aValueToAdd to the underlying value in the animation sandwich, aDest. 147 * 148 * For most types this operation is identical to a regular Add() but for some 149 * types (notably <animateTransform>) the operation differs. For 150 * <animateTransform> Add() corresponds to simply adding together the 151 * transform parameters and is used when calculating cumulative values or 152 * by-animation values. On the other hand SandwichAdd() is used when adding to 153 * the underlying value and requires matrix post-multiplication. (This 154 * distinction is most clearly indicated by the SVGT1.2 test suite. It is not 155 * obvious within the SMIL specifications.) 156 * 157 * @param aDest The value to add to. 158 * @param aValueToAdd The value to add. 159 * @return NS_OK on success, an error code on failure. 160 * 161 * @pre aValueToAdd.mType == aDest.mType == this 162 */ 163 virtual nsresult SandwichAdd(SMILValue& aDest, 164 const SMILValue& aValueToAdd) const { 165 return Add(aDest, aValueToAdd, 1); 166 } 167 168 /** 169 * Calculates the 'distance' between two values. This is the distance used in 170 * paced interpolation. 171 * 172 * @param aFrom The start of the interval for which the distance should 173 * be calculated. 174 * @param aTo The end of the interval for which the distance should be 175 * calculated. 176 * @param aDistance The result of the calculation. 177 * @return NS_OK on success, or an appropriate error code if there is no 178 * notion of distance for the underlying data type or the distance 179 * could not be calculated. 180 * 181 * @pre aFrom.mType == aTo.mType == this 182 */ 183 virtual nsresult ComputeDistance(const SMILValue& aFrom, const SMILValue& aTo, 184 double& aDistance) const = 0; 185 186 /** 187 * Calculates an interpolated value between two values using the specified 188 * proportion. 189 * 190 * @param aStartVal The value defining the start of the interval of 191 * interpolation. 192 * @param aEndVal The value defining the end of the interval of 193 * interpolation. 194 * @param aUnitDistance A number between 0.0 and 1.0 (inclusive) defining 195 * the distance of the interpolated value in the 196 * interval. 197 * @param aResult The interpolated value. 198 * @return NS_OK on success, NS_ERROR_FAILURE if this data type cannot be 199 * interpolated or NS_ERROR_OUT_OF_MEMORY if insufficient memory was 200 * available for storing the result. 201 * 202 * @pre aStartVal.mType == aEndVal.mType == aResult.mType == this 203 */ 204 virtual nsresult Interpolate(const SMILValue& aStartVal, 205 const SMILValue& aEndVal, double aUnitDistance, 206 SMILValue& aResult) const = 0; 207 }; 208 209 } // namespace mozilla 210 211 #endif // DOM_SMIL_SMILTYPE_H_