LCOV - code coverage report
Current view: directory - layout/style - nsStyleAnimation.h (source / functions) Found Hit Coverage
Test: app.info Lines: 74 15 20.3 %
Date: 2012-06-02 Functions: 29 8 27.6 %

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

Generated by: LCOV version 1.7