2 * Copyright (C) 2006, 2007, 2008, 2011, 2012, 2013 Apple Inc. All rights reserved.
3 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
4 * Copyright (C) 2008 Torch Mobile, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "AffineTransform.h"
32 #include "FloatPoint.h"
33 #include "GraphicsTypes.h"
34 #include <wtf/PassRefPtr.h>
35 #include <wtf/RefCounted.h>
36 #include <wtf/Vector.h>
40 typedef struct CGContext* CGContextRef;
42 typedef struct CGGradient* CGGradientRef;
43 typedef CGGradientRef PlatformGradient;
49 typedef QGradient* PlatformGradient;
51 typedef struct _cairo_pattern cairo_pattern_t;
52 typedef cairo_pattern_t* PlatformGradient;
53 #elif PLATFORM(BLACKBERRY)
54 namespace BlackBerry {
61 typedef BlackBerry::Platform::Graphics::Gradient* PlatformGradient;
63 typedef void* PlatformGradient;
70 class GraphicsContext;
72 class Gradient : public RefCounted<Gradient> {
74 static PassRefPtr<Gradient> create(const FloatPoint& p0, const FloatPoint& p1)
76 return adoptRef(new Gradient(p0, p1));
78 static PassRefPtr<Gradient> create(const FloatPoint& p0, float r0, const FloatPoint& p1, float r1, float aspectRatio = 1)
80 return adoptRef(new Gradient(p0, r0, p1, r1, aspectRatio));
85 void addColorStop(const ColorStop&);
86 void addColorStop(float, const Color&);
88 void getColor(float value, float* r, float* g, float* b, float* a) const;
89 bool hasAlpha() const;
91 bool isRadial() const { return m_radial; }
92 bool isZeroSize() const { return m_p0.x() == m_p1.x() && m_p0.y() == m_p1.y() && (!m_radial || m_r0 == m_r1); }
94 const FloatPoint& p0() const { return m_p0; }
95 const FloatPoint& p1() const { return m_p1; }
97 void setP0(const FloatPoint& p)
107 void setP1(const FloatPoint& p)
117 float startRadius() const { return m_r0; }
118 float endRadius() const { return m_r1; }
120 void setStartRadius(float r)
130 void setEndRadius(float r)
140 float aspectRatio() const { return m_aspectRatio; }
142 #if OS(WINCE) && !PLATFORM(QT)
143 const Vector<ColorStop, 2>& getStops() const;
145 PlatformGradient platformGradient();
155 ColorStop() : stop(0), red(0), green(0), blue(0), alpha(0) { }
156 ColorStop(float s, float r, float g, float b, float a) : stop(s), red(r), green(g), blue(b), alpha(a) { }
159 void setStopsSorted(bool s) { m_stopsSorted = s; }
161 void setSpreadMethod(GradientSpreadMethod);
162 GradientSpreadMethod spreadMethod() { return m_spreadMethod; }
163 void setGradientSpaceTransform(const AffineTransform& gradientSpaceTransformation);
164 // Qt and CG transform the gradient at draw time
165 AffineTransform gradientSpaceTransform() { return m_gradientSpaceTransformation; }
167 void fill(GraphicsContext*, const FloatRect&);
168 void adjustParametersForTiledDrawing(IntSize&, FloatRect&);
170 void setPlatformGradientSpaceTransform(const AffineTransform& gradientSpaceTransformation);
172 unsigned hash() const;
173 void invalidateHash() { m_cachedHash = 0; }
176 void paint(CGContextRef);
177 void paint(GraphicsContext*);
179 PlatformGradient platformGradient(float globalAlpha);
183 Gradient(const FloatPoint& p0, const FloatPoint& p1);
184 Gradient(const FloatPoint& p0, float r0, const FloatPoint& p1, float r1, float aspectRatio);
186 void platformInit() { m_gradient = 0; }
187 void platformDestroy();
189 int findStop(float value) const;
190 void sortStopsIfNecessary();
192 // Keep any parameters relevant to rendering in sync with the structure in Gradient::hash().
198 float m_aspectRatio; // For elliptical gradient, width / height.
199 mutable Vector<ColorStop, 2> m_stops;
200 mutable bool m_stopsSorted;
201 mutable int m_lastStop;
202 GradientSpreadMethod m_spreadMethod;
203 AffineTransform m_gradientSpaceTransformation;
205 mutable unsigned m_cachedHash;
207 PlatformGradient m_gradient;
210 float m_platformGradientAlpha;