Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
      3  * Copyright (C) 2007 Alp Toker <alp (at) 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 "AffineTransform.h"
     32 #include "FloatPoint.h"
     33 #include "Generator.h"
     34 #include "GraphicsTypes.h"
     35 #include <wtf/PassRefPtr.h>
     36 #include <wtf/Vector.h>
     37 
     38 #if PLATFORM(CG)
     39 
     40 #ifdef BUILDING_ON_TIGER
     41 typedef struct CGShading* CGShadingRef;
     42 typedef CGShadingRef PlatformGradient;
     43 #else
     44 typedef struct CGGradient* CGGradientRef;
     45 typedef CGGradientRef PlatformGradient;
     46 #endif
     47 
     48 #elif PLATFORM(QT)
     49 QT_BEGIN_NAMESPACE
     50 class QGradient;
     51 QT_END_NAMESPACE
     52 typedef QGradient* PlatformGradient;
     53 #elif PLATFORM(CAIRO)
     54 typedef struct _cairo_pattern cairo_pattern_t;
     55 typedef cairo_pattern_t* PlatformGradient;
     56 #elif PLATFORM(SKIA)
     57 #if PLATFORM(ANDROID)
     58 #include "SkShader.h"
     59 typedef class PlatformGradientRec* PlatformGradient;
     60 #else
     61 class SkShader;
     62 typedef class SkShader* PlatformGradient;
     63 typedef class SkShader* PlatformPattern;
     64 #endif
     65 #else
     66 typedef void* PlatformGradient;
     67 #endif
     68 
     69 namespace WebCore {
     70 
     71     class Color;
     72 
     73     class Gradient : public Generator {
     74     public:
     75         static PassRefPtr<Gradient> create(const FloatPoint& p0, const FloatPoint& p1)
     76         {
     77             return adoptRef(new Gradient(p0, p1));
     78         }
     79         static PassRefPtr<Gradient> create(const FloatPoint& p0, float r0, const FloatPoint& p1, float r1)
     80         {
     81             return adoptRef(new Gradient(p0, r0, p1, r1));
     82         }
     83         virtual ~Gradient();
     84 
     85         void addColorStop(float, const Color&);
     86 
     87         void getColor(float value, float* r, float* g, float* b, float* a) const;
     88 
     89 #if OS(WINCE) && !PLATFORM(QT)
     90         const FloatPoint& p0() const { return m_p0; }
     91         const FloatPoint& p1() const { return m_p1; }
     92         float r0() const { return m_r0; }
     93         float r1() const { return m_r1; }
     94         bool isRadial() const { return m_radial; }
     95         struct ColorStop;
     96         const Vector<ColorStop>& getStops() const;
     97 #else
     98 #if PLATFORM(ANDROID)
     99         SkShader* getShader(SkShader::TileMode);
    100 #endif
    101         PlatformGradient platformGradient();
    102 #endif
    103         struct ColorStop {
    104             float stop;
    105             float red;
    106             float green;
    107             float blue;
    108             float alpha;
    109 
    110             ColorStop() : stop(0), red(0), green(0), blue(0), alpha(0) { }
    111             ColorStop(float s, float r, float g, float b, float a) : stop(s), red(r), green(g), blue(b), alpha(a) { }
    112         };
    113 
    114         void setStopsSorted(bool s) { m_stopsSorted = s; }
    115 
    116         void setSpreadMethod(GradientSpreadMethod);
    117         GradientSpreadMethod spreadMethod() { return m_spreadMethod; }
    118         void setGradientSpaceTransform(const AffineTransform& gradientSpaceTransformation);
    119         // Qt and CG transform the gradient at draw time
    120         AffineTransform gradientSpaceTransform() { return m_gradientSpaceTransformation; }
    121 
    122         virtual void fill(GraphicsContext*, const FloatRect&);
    123         virtual void adjustParametersForTiledDrawing(IntSize& size, FloatRect& srcRect);
    124 
    125         void setPlatformGradientSpaceTransform(const AffineTransform& gradientSpaceTransformation);
    126 
    127 #if PLATFORM(CG)
    128         void paint(GraphicsContext*);
    129 #endif
    130     private:
    131         Gradient(const FloatPoint& p0, const FloatPoint& p1);
    132         Gradient(const FloatPoint& p0, float r0, const FloatPoint& p1, float r1);
    133 
    134         void platformInit() { m_gradient = 0; }
    135         void platformDestroy();
    136 
    137         int findStop(float value) const;
    138         void sortStopsIfNecessary();
    139 
    140         bool m_radial;
    141         FloatPoint m_p0;
    142         FloatPoint m_p1;
    143         float m_r0;
    144         float m_r1;
    145         mutable Vector<ColorStop> m_stops;
    146         mutable bool m_stopsSorted;
    147         mutable int m_lastStop;
    148         GradientSpreadMethod m_spreadMethod;
    149         AffineTransform m_gradientSpaceTransformation;
    150 
    151         PlatformGradient m_gradient;
    152     };
    153 
    154 } //namespace
    155 
    156 #endif
    157