2
* Copyright (C) 2006, 2007, 2008, 2011 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 "Generator.h"
34
#include "GraphicsTypes.h"
35
#include <wtf/PassRefPtr.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;
55
typedef class SkShader* PlatformGradient;
56
typedef class SkShader* PlatformPattern;
58
class wxGraphicsBrush;
59
typedef wxGraphicsBrush* PlatformGradient;
61
typedef void* PlatformGradient;
68
class Gradient : public Generator {
70
static PassRefPtr<Gradient> create(const FloatPoint& p0, const FloatPoint& p1)
72
return adoptRef(new Gradient(p0, p1));
74
static PassRefPtr<Gradient> create(const FloatPoint& p0, float r0, const FloatPoint& p1, float r1, float aspectRatio = 1)
76
return adoptRef(new Gradient(p0, r0, p1, r1, aspectRatio));
81
void addColorStop(const ColorStop&);
82
void addColorStop(float, const Color&);
84
void getColor(float value, float* r, float* g, float* b, float* a) const;
85
bool hasAlpha() const;
87
bool isRadial() const { return m_radial; }
88
bool isZeroSize() const { return m_p0.x() == m_p1.x() && m_p0.y() == m_p1.y() && (!m_radial || m_r0 == m_r1); }
90
const FloatPoint& p0() const { return m_p0; }
91
const FloatPoint& p1() const { return m_p1; }
93
void setP0(const FloatPoint& p)
103
void setP1(const FloatPoint& p)
113
float startRadius() const { return m_r0; }
114
float endRadius() const { return m_r1; }
116
void setStartRadius(float r)
126
void setEndRadius(float r)
136
float aspectRatio() const { return m_aspectRatio; }
138
#if OS(WINCE) && !PLATFORM(QT)
139
const Vector<ColorStop, 2>& getStops() const;
141
PlatformGradient platformGradient();
151
ColorStop() : stop(0), red(0), green(0), blue(0), alpha(0) { }
152
ColorStop(float s, float r, float g, float b, float a) : stop(s), red(r), green(g), blue(b), alpha(a) { }
155
void setStopsSorted(bool s) { m_stopsSorted = s; }
157
void setSpreadMethod(GradientSpreadMethod);
158
GradientSpreadMethod spreadMethod() { return m_spreadMethod; }
159
void setGradientSpaceTransform(const AffineTransform& gradientSpaceTransformation);
160
// Qt and CG transform the gradient at draw time
161
AffineTransform gradientSpaceTransform() { return m_gradientSpaceTransformation; }
163
virtual void fill(GraphicsContext*, const FloatRect&);
164
virtual void adjustParametersForTiledDrawing(IntSize& size, FloatRect& srcRect);
166
void setPlatformGradientSpaceTransform(const AffineTransform& gradientSpaceTransformation);
168
virtual unsigned hash() const OVERRIDE;
169
void invalidateHash() { m_cachedHash = 0; }
172
void paint(CGContextRef);
173
void paint(GraphicsContext*);
175
PlatformGradient platformGradient(float globalAlpha);
179
Gradient(const FloatPoint& p0, const FloatPoint& p1);
180
Gradient(const FloatPoint& p0, float r0, const FloatPoint& p1, float r1, float aspectRatio);
182
void platformInit() { m_gradient = 0; }
183
void platformDestroy();
185
int findStop(float value) const;
186
void sortStopsIfNecessary();
188
// Keep any parameters relevant to rendering in sync with the structure in Gradient::hash().
194
float m_aspectRatio; // For elliptical gradient, width / height.
195
mutable Vector<ColorStop, 2> m_stops;
196
mutable bool m_stopsSorted;
197
mutable int m_lastStop;
198
GradientSpreadMethod m_spreadMethod;
199
AffineTransform m_gradientSpaceTransformation;
201
mutable unsigned m_cachedHash;
203
PlatformGradient m_gradient;
206
float m_platformGradientAlpha;