• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
3  * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
4  * Copyright (C) 2007 Alp Toker <alp@atoker.com>
5  * Copyright (C) 2008 Eric Seidel <eric@webkit.org>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "config.h"
30 #include "CanvasStyle.h"
31 
32 #include "CSSParser.h"
33 #include "CanvasGradient.h"
34 #include "CanvasPattern.h"
35 #include "GraphicsContext.h"
36 #include <wtf/PassRefPtr.h>
37 
38 #if PLATFORM(CG)
39 #include <CoreGraphics/CGContext.h>
40 #endif
41 
42 #if PLATFORM(QT)
43 #include <QPainter>
44 #include <QBrush>
45 #include <QPen>
46 #include <QColor>
47 #elif PLATFORM(CAIRO)
48 #include "NotImplemented.h"
49 #endif
50 
51 namespace WebCore {
52 
CanvasStyle(const String & color)53 CanvasStyle::CanvasStyle(const String& color)
54     : m_type(ColorString)
55     , m_color(color)
56 {
57 }
58 
CanvasStyle(float grayLevel)59 CanvasStyle::CanvasStyle(float grayLevel)
60     : m_type(GrayLevel)
61     , m_alpha(1)
62     , m_grayLevel(grayLevel)
63 {
64 }
65 
CanvasStyle(const String & color,float alpha)66 CanvasStyle::CanvasStyle(const String& color, float alpha)
67     : m_type(ColorStringWithAlpha)
68     , m_color(color)
69     , m_alpha(alpha)
70 {
71 }
72 
CanvasStyle(float grayLevel,float alpha)73 CanvasStyle::CanvasStyle(float grayLevel, float alpha)
74     : m_type(GrayLevel)
75     , m_alpha(alpha)
76     , m_grayLevel(grayLevel)
77 {
78 }
79 
CanvasStyle(float r,float g,float b,float a)80 CanvasStyle::CanvasStyle(float r, float g, float b, float a)
81     : m_type(RGBA), m_alpha(a), m_red(r), m_green(g), m_blue(b)
82 {
83 }
84 
CanvasStyle(float c,float m,float y,float k,float a)85 CanvasStyle::CanvasStyle(float c, float m, float y, float k, float a)
86     : m_type(CMYKA), m_alpha(a), m_cyan(c), m_magenta(m), m_yellow(y), m_black(k)
87 {
88 }
89 
CanvasStyle(PassRefPtr<CanvasGradient> gradient)90 CanvasStyle::CanvasStyle(PassRefPtr<CanvasGradient> gradient)
91     : m_type(gradient ? Gradient : ColorString)
92     , m_gradient(gradient)
93 {
94 }
95 
CanvasStyle(PassRefPtr<CanvasPattern> pattern)96 CanvasStyle::CanvasStyle(PassRefPtr<CanvasPattern> pattern)
97     : m_type(pattern ? ImagePattern : ColorString)
98     , m_pattern(pattern)
99 {
100 }
101 
applyStrokeColor(GraphicsContext * context)102 void CanvasStyle::applyStrokeColor(GraphicsContext* context)
103 {
104     if (!context)
105         return;
106     switch (m_type) {
107         case ColorString: {
108             Color c = Color(m_color);
109             if (c.isValid()) {
110                 context->setStrokeColor(c.rgb());
111                 break;
112             }
113             RGBA32 color = 0; // default is transparent black
114             if (CSSParser::parseColor(color, m_color))
115                 context->setStrokeColor(color);
116             break;
117         }
118         case ColorStringWithAlpha: {
119             Color c = Color(m_color);
120             if (c.isValid()) {
121                 context->setStrokeColor(colorWithOverrideAlpha(c.rgb(), m_alpha));
122                 break;
123             }
124             RGBA32 color = 0; // default is transparent black
125             if (CSSParser::parseColor(color, m_color))
126                 context->setStrokeColor(colorWithOverrideAlpha(color, m_alpha));
127             break;
128         }
129         case GrayLevel:
130             // We're only supporting 255 levels of gray here.  Since this isn't
131             // even part of HTML5, I don't expect anyone will care.  If they do
132             // we'll make a fancier Color abstraction.
133             context->setStrokeColor(Color(m_grayLevel, m_grayLevel, m_grayLevel, m_alpha));
134             break;
135         case RGBA:
136             context->setStrokeColor(Color(m_red, m_green, m_blue, m_alpha));
137             break;
138         case CMYKA: {
139             // FIXME: Do this through platform-independent GraphicsContext API.
140             // We'll need a fancier Color abstraction to support CYMKA correctly
141 #if PLATFORM(CG)
142             CGContextSetCMYKStrokeColor(context->platformContext(), m_cyan, m_magenta, m_yellow, m_black, m_alpha);
143 #elif PLATFORM(QT)
144             QPen currentPen = context->platformContext()->pen();
145             QColor clr;
146             clr.setCmykF(m_cyan, m_magenta, m_yellow, m_black, m_alpha);
147             currentPen.setColor(clr);
148             context->platformContext()->setPen(currentPen);
149 #elif PLATFORM(CAIRO)
150             notImplemented();
151 #elif PLATFORM(SGL)
152             context->setCMYKAStrokeColor(m_cyan, m_magenta, m_yellow, m_black, m_alpha);
153 #endif
154             break;
155         }
156         case Gradient:
157             context->setStrokeGradient(canvasGradient()->gradient());
158             break;
159         case ImagePattern:
160             context->setStrokePattern(canvasPattern()->pattern());
161             break;
162     }
163 }
164 
applyFillColor(GraphicsContext * context)165 void CanvasStyle::applyFillColor(GraphicsContext* context)
166 {
167     if (!context)
168         return;
169     switch (m_type) {
170         case ColorString: {
171             Color c = Color(m_color);
172             if (c.isValid()){
173                 context->setFillColor(c.rgb());
174                 break;
175             }
176             RGBA32 rgba = 0; // default is transparent black
177             if (CSSParser::parseColor(rgba, m_color))
178                 context->setFillColor(rgba);
179             break;
180         }
181         case ColorStringWithAlpha: {
182             Color c = Color(m_color);
183             if (c.isValid()){
184                 context->setFillColor(colorWithOverrideAlpha(c.rgb(), m_alpha));
185                 break;
186             }
187             RGBA32 color = 0; // default is transparent black
188             if (CSSParser::parseColor(color, m_color))
189                 context->setFillColor(colorWithOverrideAlpha(color, m_alpha));
190             break;
191         }
192         case GrayLevel:
193             // We're only supporting 255 levels of gray here.  Since this isn't
194             // even part of HTML5, I don't expect anyone will care.  If they do
195             // we'll make a fancier Color abstraction.
196             context->setFillColor(Color(m_grayLevel, m_grayLevel, m_grayLevel, m_alpha));
197             break;
198         case RGBA:
199             context->setFillColor(Color(m_red, m_green, m_blue, m_alpha));
200             break;
201         case CMYKA: {
202             // FIXME: Do this through platform-independent GraphicsContext API.
203             // We'll need a fancier Color abstraction to support CYMKA correctly
204 #if PLATFORM(CG)
205             CGContextSetCMYKFillColor(context->platformContext(), m_cyan, m_magenta, m_yellow, m_black, m_alpha);
206 #elif PLATFORM(QT)
207             QBrush currentBrush = context->platformContext()->brush();
208             QColor clr;
209             clr.setCmykF(m_cyan, m_magenta, m_yellow, m_black, m_alpha);
210             currentBrush.setColor(clr);
211             context->platformContext()->setBrush(currentBrush);
212 #elif PLATFORM(SGL)
213             context->setCMYKAFillColor(m_cyan, m_magenta, m_yellow, m_black, m_alpha);
214 #endif
215             break;
216         }
217         case Gradient:
218             context->setFillGradient(canvasGradient()->gradient());
219             break;
220         case ImagePattern:
221             context->setFillPattern(canvasPattern()->pattern());
222             break;
223     }
224 }
225 
226 }
227