Home | History | Annotate | Download | only in css
      1 /*
      2  * Copyright (C) 2008 Apple Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #ifndef CSSGradientValue_h
     27 #define CSSGradientValue_h
     28 
     29 #include "CSSImageGeneratorValue.h"
     30 #include "CSSPrimitiveValue.h"
     31 #include <wtf/RefPtr.h>
     32 #include <wtf/Vector.h>
     33 
     34 namespace WebCore {
     35 
     36 class FloatPoint;
     37 class Gradient;
     38 
     39 enum CSSGradientType { CSSLinearGradient, CSSRadialGradient };
     40 enum CSSGradientRepeat { NonRepeating, Repeating };
     41 
     42 struct CSSGradientColorStop {
     43     RefPtr<CSSPrimitiveValue> m_position; // percentage or length
     44     RefPtr<CSSPrimitiveValue> m_color;
     45 };
     46 
     47 class CSSGradientValue : public CSSImageGeneratorValue {
     48 public:
     49     virtual PassRefPtr<Image> image(RenderObject*, const IntSize&);
     50 
     51     void setFirstX(PassRefPtr<CSSPrimitiveValue> val) { m_firstX = val; }
     52     void setFirstY(PassRefPtr<CSSPrimitiveValue> val) { m_firstY = val; }
     53     void setSecondX(PassRefPtr<CSSPrimitiveValue> val) { m_secondX = val; }
     54     void setSecondY(PassRefPtr<CSSPrimitiveValue> val) { m_secondY = val; }
     55 
     56     void addStop(const CSSGradientColorStop& stop) { m_stops.append(stop); }
     57 
     58     Vector<CSSGradientColorStop>& stops() { return m_stops; }
     59 
     60     void sortStopsIfNeeded();
     61 
     62     virtual bool isLinearGradient() const { return false; }
     63     virtual bool isRadialGradient() const { return false; }
     64 
     65     bool isRepeating() const { return m_repeating; }
     66 
     67     bool deprecatedType() const { return m_deprecatedType; } // came from -webkit-gradient
     68 
     69 protected:
     70     CSSGradientValue(CSSGradientRepeat repeat, bool deprecatedType = false)
     71         : m_stopsSorted(false)
     72         , m_deprecatedType(deprecatedType)
     73         , m_repeating(repeat == Repeating)
     74     {
     75     }
     76 
     77     void addStops(Gradient*, RenderObject*, RenderStyle* rootStyle, float maxLengthForRepeat = 0);
     78 
     79     // Create the gradient for a given size.
     80     virtual PassRefPtr<Gradient> createGradient(RenderObject*, const IntSize&) = 0;
     81 
     82     // Resolve points/radii to front end values.
     83     FloatPoint computeEndPoint(CSSPrimitiveValue*, CSSPrimitiveValue*, RenderStyle*, RenderStyle* rootStyle, const IntSize&);
     84 
     85     bool isCacheable() const;
     86 
     87     // Points. Some of these may be null for linear gradients.
     88     RefPtr<CSSPrimitiveValue> m_firstX;
     89     RefPtr<CSSPrimitiveValue> m_firstY;
     90 
     91     RefPtr<CSSPrimitiveValue> m_secondX;
     92     RefPtr<CSSPrimitiveValue> m_secondY;
     93 
     94     // Stops
     95     Vector<CSSGradientColorStop> m_stops;
     96     bool m_stopsSorted;
     97     bool m_deprecatedType; // -webkit-gradient()
     98     bool m_repeating;
     99 };
    100 
    101 
    102 class CSSLinearGradientValue : public CSSGradientValue {
    103 public:
    104     static PassRefPtr<CSSLinearGradientValue> create(CSSGradientRepeat repeat, bool deprecatedType = false)
    105     {
    106         return adoptRef(new CSSLinearGradientValue(repeat, deprecatedType));
    107     }
    108 
    109     void setAngle(PassRefPtr<CSSPrimitiveValue> val) { m_angle = val; }
    110 
    111     virtual String cssText() const;
    112 
    113 private:
    114     CSSLinearGradientValue(CSSGradientRepeat repeat, bool deprecatedType = false)
    115         : CSSGradientValue(repeat, deprecatedType)
    116     {
    117     }
    118 
    119     virtual bool isLinearGradient() const { return true; }
    120 
    121     // Create the gradient for a given size.
    122     virtual PassRefPtr<Gradient> createGradient(RenderObject*, const IntSize&);
    123 
    124     RefPtr<CSSPrimitiveValue> m_angle; // may be null.
    125 };
    126 
    127 class CSSRadialGradientValue : public CSSGradientValue {
    128 public:
    129     static PassRefPtr<CSSRadialGradientValue> create(CSSGradientRepeat repeat, bool deprecatedType = false)
    130     {
    131         return adoptRef(new CSSRadialGradientValue(repeat, deprecatedType));
    132     }
    133 
    134     virtual String cssText() const;
    135 
    136     void setFirstRadius(PassRefPtr<CSSPrimitiveValue> val) { m_firstRadius = val; }
    137     void setSecondRadius(PassRefPtr<CSSPrimitiveValue> val) { m_secondRadius = val; }
    138 
    139     void setShape(PassRefPtr<CSSPrimitiveValue> val) { m_shape = val; }
    140     void setSizingBehavior(PassRefPtr<CSSPrimitiveValue> val) { m_sizingBehavior = val; }
    141 
    142     void setEndHorizontalSize(PassRefPtr<CSSPrimitiveValue> val) { m_endHorizontalSize = val; }
    143     void setEndVerticalSize(PassRefPtr<CSSPrimitiveValue> val) { m_endVerticalSize = val; }
    144 
    145 private:
    146     CSSRadialGradientValue(CSSGradientRepeat repeat, bool deprecatedType = false)
    147         : CSSGradientValue(repeat, deprecatedType)
    148     {
    149     }
    150 
    151     virtual bool isRadialGradient() const { return true; }
    152 
    153     // Create the gradient for a given size.
    154     virtual PassRefPtr<Gradient> createGradient(RenderObject*, const IntSize&);
    155 
    156     // Resolve points/radii to front end values.
    157     float resolveRadius(CSSPrimitiveValue*, RenderStyle*, RenderStyle* rootStyle, float* widthOrHeight = 0);
    158 
    159     // These may be null for non-deprecated gradients.
    160     RefPtr<CSSPrimitiveValue> m_firstRadius;
    161     RefPtr<CSSPrimitiveValue> m_secondRadius;
    162 
    163     // The below are only used for non-deprecated gradients.
    164     RefPtr<CSSPrimitiveValue> m_shape;
    165     RefPtr<CSSPrimitiveValue> m_sizingBehavior;
    166 
    167     RefPtr<CSSPrimitiveValue> m_endHorizontalSize;
    168     RefPtr<CSSPrimitiveValue> m_endVerticalSize;
    169 };
    170 
    171 } // namespace WebCore
    172 
    173 #endif // CSSGradientValue_h
    174