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 "core/css/CSSImageGeneratorValue.h"
30 #include "core/css/CSSPrimitiveValue.h"
31 #include "wtf/RefPtr.h"
32 #include "wtf/Vector.h"
40 enum CSSGradientType {
41 CSSDeprecatedLinearGradient,
42 CSSDeprecatedRadialGradient,
43 CSSPrefixedLinearGradient,
44 CSSPrefixedRadialGradient,
48 enum CSSGradientRepeat { NonRepeating, Repeating };
50 // This struct is stack allocated and allocated as part of vectors.
51 // When allocated on the stack its members are found by conservative
52 // stack scanning. When allocated as part of Vectors in heap-allocated
53 // objects its members are visited via the containing object's
54 // (CSSGradientValue) traceAfterDispatch method.
55 struct CSSGradientColorStop {
56 ALLOW_ONLY_INLINE_ALLOCATION();
58 CSSGradientColorStop() : m_colorIsDerivedFromElement(false) { };
59 RefPtrWillBeMember<CSSPrimitiveValue> m_position; // percentage or length
60 RefPtrWillBeMember<CSSPrimitiveValue> m_color;
61 Color m_resolvedColor;
62 bool m_colorIsDerivedFromElement;
63 bool operator==(const CSSGradientColorStop& other) const
65 return compareCSSValuePtr(m_color, other.m_color)
66 && compareCSSValuePtr(m_position, other.m_position);
75 // We have to declare the VectorTraits specialization before CSSGradientValue
76 // declares its inline capacity vector below.
77 WTF_ALLOW_MOVE_AND_INIT_WITH_MEM_FUNCTIONS(blink::CSSGradientColorStop);
81 class CSSGradientValue : public CSSImageGeneratorValue {
83 PassRefPtr<Image> image(RenderObject*, const IntSize&);
85 void setFirstX(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> val) { m_firstX = val; }
86 void setFirstY(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> val) { m_firstY = val; }
87 void setSecondX(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> val) { m_secondX = val; }
88 void setSecondY(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> val) { m_secondY = val; }
90 void addStop(const CSSGradientColorStop& stop) { m_stops.append(stop); }
92 unsigned stopCount() const { return m_stops.size(); }
94 void sortStopsIfNeeded();
96 bool isRepeating() const { return m_repeating; }
98 CSSGradientType gradientType() const { return m_gradientType; }
100 bool isFixedSize() const { return false; }
101 IntSize fixedSize(const RenderObject*) const { return IntSize(); }
103 bool isPending() const { return false; }
104 bool knownToBeOpaque(const RenderObject*) const;
106 void loadSubimages(ResourceFetcher*) { }
107 PassRefPtrWillBeRawPtr<CSSGradientValue> gradientWithStylesResolved(const TextLinkColors&, Color currentColor);
109 void traceAfterDispatch(Visitor*);
112 CSSGradientValue(ClassType classType, CSSGradientRepeat repeat, CSSGradientType gradientType)
113 : CSSImageGeneratorValue(classType)
114 , m_stopsSorted(false)
115 , m_gradientType(gradientType)
116 , m_repeating(repeat == Repeating)
120 CSSGradientValue(const CSSGradientValue& other, ClassType classType, CSSGradientType gradientType)
121 : CSSImageGeneratorValue(classType)
122 , m_firstX(other.m_firstX)
123 , m_firstY(other.m_firstY)
124 , m_secondX(other.m_secondX)
125 , m_secondY(other.m_secondY)
126 , m_stops(other.m_stops)
127 , m_stopsSorted(other.m_stopsSorted)
128 , m_gradientType(gradientType)
129 , m_repeating(other.isRepeating() ? Repeating : NonRepeating)
133 void addStops(Gradient*, const CSSToLengthConversionData&, float maxLengthForRepeat = 0);
135 // Resolve points/radii to front end values.
136 FloatPoint computeEndPoint(CSSPrimitiveValue*, CSSPrimitiveValue*, const CSSToLengthConversionData&, const IntSize&);
138 bool isCacheable() const;
140 // Points. Some of these may be null.
141 RefPtrWillBeMember<CSSPrimitiveValue> m_firstX;
142 RefPtrWillBeMember<CSSPrimitiveValue> m_firstY;
144 RefPtrWillBeMember<CSSPrimitiveValue> m_secondX;
145 RefPtrWillBeMember<CSSPrimitiveValue> m_secondY;
148 WillBeHeapVector<CSSGradientColorStop, 2> m_stops;
150 CSSGradientType m_gradientType;
154 DEFINE_CSS_VALUE_TYPE_CASTS(CSSGradientValue, isGradientValue());
156 class CSSLinearGradientValue : public CSSGradientValue {
159 static PassRefPtrWillBeRawPtr<CSSLinearGradientValue> create(CSSGradientRepeat repeat, CSSGradientType gradientType = CSSLinearGradient)
161 return adoptRefWillBeNoop(new CSSLinearGradientValue(repeat, gradientType));
164 void setAngle(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> val) { m_angle = val; }
166 String customCSSText() const;
168 // Create the gradient for a given size.
169 PassRefPtr<Gradient> createGradient(const CSSToLengthConversionData&, const IntSize&);
171 PassRefPtrWillBeRawPtr<CSSLinearGradientValue> clone() const
173 return adoptRefWillBeNoop(new CSSLinearGradientValue(*this));
176 bool equals(const CSSLinearGradientValue&) const;
178 void traceAfterDispatch(Visitor*);
181 CSSLinearGradientValue(CSSGradientRepeat repeat, CSSGradientType gradientType = CSSLinearGradient)
182 : CSSGradientValue(LinearGradientClass, repeat, gradientType)
186 explicit CSSLinearGradientValue(const CSSLinearGradientValue& other)
187 : CSSGradientValue(other, LinearGradientClass, other.gradientType())
188 , m_angle(other.m_angle)
192 RefPtrWillBeMember<CSSPrimitiveValue> m_angle; // may be null.
195 DEFINE_CSS_VALUE_TYPE_CASTS(CSSLinearGradientValue, isLinearGradientValue());
197 class CSSRadialGradientValue : public CSSGradientValue {
199 static PassRefPtrWillBeRawPtr<CSSRadialGradientValue> create(CSSGradientRepeat repeat, CSSGradientType gradientType = CSSRadialGradient)
201 return adoptRefWillBeNoop(new CSSRadialGradientValue(repeat, gradientType));
204 PassRefPtrWillBeRawPtr<CSSRadialGradientValue> clone() const
206 return adoptRefWillBeNoop(new CSSRadialGradientValue(*this));
209 String customCSSText() const;
211 void setFirstRadius(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> val) { m_firstRadius = val; }
212 void setSecondRadius(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> val) { m_secondRadius = val; }
214 void setShape(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> val) { m_shape = val; }
215 void setSizingBehavior(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> val) { m_sizingBehavior = val; }
217 void setEndHorizontalSize(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> val) { m_endHorizontalSize = val; }
218 void setEndVerticalSize(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> val) { m_endVerticalSize = val; }
220 // Create the gradient for a given size.
221 PassRefPtr<Gradient> createGradient(const CSSToLengthConversionData&, const IntSize&);
223 bool equals(const CSSRadialGradientValue&) const;
225 void traceAfterDispatch(Visitor*);
228 CSSRadialGradientValue(CSSGradientRepeat repeat, CSSGradientType gradientType = CSSRadialGradient)
229 : CSSGradientValue(RadialGradientClass, repeat, gradientType)
233 explicit CSSRadialGradientValue(const CSSRadialGradientValue& other)
234 : CSSGradientValue(other, RadialGradientClass, other.gradientType())
235 , m_firstRadius(other.m_firstRadius)
236 , m_secondRadius(other.m_secondRadius)
237 , m_shape(other.m_shape)
238 , m_sizingBehavior(other.m_sizingBehavior)
239 , m_endHorizontalSize(other.m_endHorizontalSize)
240 , m_endVerticalSize(other.m_endVerticalSize)
245 // Resolve points/radii to front end values.
246 float resolveRadius(CSSPrimitiveValue*, const CSSToLengthConversionData&, float* widthOrHeight = 0);
248 // These may be null for non-deprecated gradients.
249 RefPtrWillBeMember<CSSPrimitiveValue> m_firstRadius;
250 RefPtrWillBeMember<CSSPrimitiveValue> m_secondRadius;
252 // The below are only used for non-deprecated gradients. Any of them may be null.
253 RefPtrWillBeMember<CSSPrimitiveValue> m_shape;
254 RefPtrWillBeMember<CSSPrimitiveValue> m_sizingBehavior;
256 RefPtrWillBeMember<CSSPrimitiveValue> m_endHorizontalSize;
257 RefPtrWillBeMember<CSSPrimitiveValue> m_endVerticalSize;
260 DEFINE_CSS_VALUE_TYPE_CASTS(CSSRadialGradientValue, isRadialGradientValue());
264 #endif // CSSGradientValue_h