2 * Copyright (C) 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef CSSGradientValue_h
27 #define CSSGradientValue_h
29 #include "CSSImageGeneratorValue.h"
30 #include "CSSPrimitiveValue.h"
31 #include <wtf/RefPtr.h>
32 #include <wtf/Vector.h>
39 enum CSSGradientType { CSSLinearGradient, CSSRadialGradient };
40 enum CSSGradientRepeat { NonRepeating, Repeating };
42 struct CSSGradientColorStop {
43 RefPtr<CSSPrimitiveValue> m_position; // percentage or length
44 RefPtr<CSSPrimitiveValue> m_color;
47 class CSSGradientValue : public CSSImageGeneratorValue {
49 virtual PassRefPtr<Image> image(RenderObject*, const IntSize&);
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; }
56 void addStop(const CSSGradientColorStop& stop) { m_stops.append(stop); }
58 Vector<CSSGradientColorStop>& stops() { return m_stops; }
60 void sortStopsIfNeeded();
62 virtual bool isLinearGradient() const { return false; }
63 virtual bool isRadialGradient() const { return false; }
65 bool isRepeating() const { return m_repeating; }
67 bool deprecatedType() const { return m_deprecatedType; } // came from -webkit-gradient
70 CSSGradientValue(CSSGradientRepeat repeat, bool deprecatedType = false)
71 : m_stopsSorted(false)
72 , m_deprecatedType(deprecatedType)
73 , m_repeating(repeat == Repeating)
77 void addStops(Gradient*, RenderObject*, RenderStyle* rootStyle, float maxLengthForRepeat = 0);
79 // Create the gradient for a given size.
80 virtual PassRefPtr<Gradient> createGradient(RenderObject*, const IntSize&) = 0;
82 // Resolve points/radii to front end values.
83 FloatPoint computeEndPoint(CSSPrimitiveValue*, CSSPrimitiveValue*, RenderStyle*, RenderStyle* rootStyle, const IntSize&);
85 bool isCacheable() const;
87 // Points. Some of these may be null for linear gradients.
88 RefPtr<CSSPrimitiveValue> m_firstX;
89 RefPtr<CSSPrimitiveValue> m_firstY;
91 RefPtr<CSSPrimitiveValue> m_secondX;
92 RefPtr<CSSPrimitiveValue> m_secondY;
95 Vector<CSSGradientColorStop> m_stops;
97 bool m_deprecatedType; // -webkit-gradient()
102 class CSSLinearGradientValue : public CSSGradientValue {
104 static PassRefPtr<CSSLinearGradientValue> create(CSSGradientRepeat repeat, bool deprecatedType = false)
106 return adoptRef(new CSSLinearGradientValue(repeat, deprecatedType));
109 void setAngle(PassRefPtr<CSSPrimitiveValue> val) { m_angle = val; }
111 virtual String cssText() const;
114 CSSLinearGradientValue(CSSGradientRepeat repeat, bool deprecatedType = false)
115 : CSSGradientValue(repeat, deprecatedType)
119 virtual bool isLinearGradient() const { return true; }
121 // Create the gradient for a given size.
122 virtual PassRefPtr<Gradient> createGradient(RenderObject*, const IntSize&);
124 RefPtr<CSSPrimitiveValue> m_angle; // may be null.
127 class CSSRadialGradientValue : public CSSGradientValue {
129 static PassRefPtr<CSSRadialGradientValue> create(CSSGradientRepeat repeat, bool deprecatedType = false)
131 return adoptRef(new CSSRadialGradientValue(repeat, deprecatedType));
134 virtual String cssText() const;
136 void setFirstRadius(PassRefPtr<CSSPrimitiveValue> val) { m_firstRadius = val; }
137 void setSecondRadius(PassRefPtr<CSSPrimitiveValue> val) { m_secondRadius = val; }
139 void setShape(PassRefPtr<CSSPrimitiveValue> val) { m_shape = val; }
140 void setSizingBehavior(PassRefPtr<CSSPrimitiveValue> val) { m_sizingBehavior = val; }
142 void setEndHorizontalSize(PassRefPtr<CSSPrimitiveValue> val) { m_endHorizontalSize = val; }
143 void setEndVerticalSize(PassRefPtr<CSSPrimitiveValue> val) { m_endVerticalSize = val; }
146 CSSRadialGradientValue(CSSGradientRepeat repeat, bool deprecatedType = false)
147 : CSSGradientValue(repeat, deprecatedType)
151 virtual bool isRadialGradient() const { return true; }
153 // Create the gradient for a given size.
154 virtual PassRefPtr<Gradient> createGradient(RenderObject*, const IntSize&);
156 // Resolve points/radii to front end values.
157 float resolveRadius(CSSPrimitiveValue*, RenderStyle*, RenderStyle* rootStyle, float* widthOrHeight = 0);
159 // These may be null for non-deprecated gradients.
160 RefPtr<CSSPrimitiveValue> m_firstRadius;
161 RefPtr<CSSPrimitiveValue> m_secondRadius;
163 // The below are only used for non-deprecated gradients.
164 RefPtr<CSSPrimitiveValue> m_shape;
165 RefPtr<CSSPrimitiveValue> m_sizingBehavior;
167 RefPtr<CSSPrimitiveValue> m_endHorizontalSize;
168 RefPtr<CSSPrimitiveValue> m_endVerticalSize;
171 } // namespace WebCore
173 #endif // CSSGradientValue_h