• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
3  * Copyright (C) 2007 Alp Toker <alp@atoker.com>
4  * Copyright (C) 2008 Torch Mobile, Inc.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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.
26  */
27 
28 #ifndef Gradient_h
29 #define Gradient_h
30 
31 #include "FloatPoint.h"
32 #include "Generator.h"
33 #include "GraphicsTypes.h"
34 #include "TransformationMatrix.h"
35 #include <wtf/PassRefPtr.h>
36 #include <wtf/Vector.h>
37 
38 #if PLATFORM(CG)
39 typedef struct CGShading* CGShadingRef;
40 typedef CGShadingRef PlatformGradient;
41 #elif PLATFORM(QT)
42 QT_BEGIN_NAMESPACE
43 class QGradient;
44 QT_END_NAMESPACE
45 typedef QGradient* PlatformGradient;
46 #elif PLATFORM(CAIRO)
47 typedef struct _cairo_pattern cairo_pattern_t;
48 typedef cairo_pattern_t* PlatformGradient;
49 #elif PLATFORM(ANDROID) && PLATFORM(SGL)
50 #include "SkShader.h"
51 typedef class PlatformGradientRec* PlatformGradient;
52 #elif PLATFORM(SKIA)
53 class SkShader;
54 typedef class SkShader* PlatformGradient;
55 typedef class SkShader* PlatformPattern;
56 #else
57 typedef void* PlatformGradient;
58 #endif
59 
60 namespace WebCore {
61 
62     class Color;
63 
64     class Gradient : public Generator {
65     public:
create(const FloatPoint & p0,const FloatPoint & p1)66         static PassRefPtr<Gradient> create(const FloatPoint& p0, const FloatPoint& p1)
67         {
68             return adoptRef(new Gradient(p0, p1));
69         }
create(const FloatPoint & p0,float r0,const FloatPoint & p1,float r1)70         static PassRefPtr<Gradient> create(const FloatPoint& p0, float r0, const FloatPoint& p1, float r1)
71         {
72             return adoptRef(new Gradient(p0, r0, p1, r1));
73         }
74         virtual ~Gradient();
75 
76         void addColorStop(float, const Color&);
77 
78         void getColor(float value, float* r, float* g, float* b, float* a) const;
79 
80 #if PLATFORM(WINCE) && !PLATFORM(QT)
p0()81         const FloatPoint& p0() const { return m_p0; }
p1()82         const FloatPoint& p1() const { return m_p1; }
r0()83         float r0() const { return m_r0; }
r1()84         float r1() const { return m_r1; }
isRadial()85         bool isRadial() const { return m_radial; }
86         struct ColorStop;
87         const Vector<ColorStop>& getStops() const;
88 #else
89 
90 #if PLATFORM(ANDROID) && PLATFORM(SGL)
91         SkShader* getShader(SkShader::TileMode);
92 #endif
93         PlatformGradient platformGradient();
94 #endif
95 
96         struct ColorStop {
97             float stop;
98             float red;
99             float green;
100             float blue;
101             float alpha;
102 
ColorStopColorStop103             ColorStop() : stop(0), red(0), green(0), blue(0), alpha(0) { }
ColorStopColorStop104             ColorStop(float s, float r, float g, float b, float a) : stop(s), red(r), green(g), blue(b), alpha(a) { }
105         };
106 
setStopsSorted(bool s)107         void setStopsSorted(bool s) { m_stopsSorted = s; }
108 
109         void setSpreadMethod(GradientSpreadMethod);
spreadMethod()110         GradientSpreadMethod spreadMethod() { return m_spreadMethod; }
111         void setGradientSpaceTransform(const TransformationMatrix& gradientSpaceTransformation);
112         // Qt and CG transform the gradient at draw time
gradientSpaceTransform()113         TransformationMatrix gradientSpaceTransform() { return m_gradientSpaceTransformation; }
114 
115         virtual void fill(GraphicsContext*, const FloatRect&);
116         void setPlatformGradientSpaceTransform(const TransformationMatrix& gradientSpaceTransformation);
117 
118     private:
119         Gradient(const FloatPoint& p0, const FloatPoint& p1);
120         Gradient(const FloatPoint& p0, float r0, const FloatPoint& p1, float r1);
121 
platformInit()122         void platformInit() { m_gradient = 0; }
123         void platformDestroy();
124 
125         int findStop(float value) const;
126 
127         bool m_radial;
128         FloatPoint m_p0;
129         FloatPoint m_p1;
130         float m_r0;
131         float m_r1;
132         mutable Vector<ColorStop> m_stops;
133         mutable bool m_stopsSorted;
134         mutable int m_lastStop;
135         GradientSpreadMethod m_spreadMethod;
136         TransformationMatrix m_gradientSpaceTransformation;
137 
138         PlatformGradient m_gradient;
139     };
140 
141 } //namespace
142 
143 #endif
144