• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
3  * Copyright (C) 2008, 2010 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 "core/html/canvas/CanvasStyle.h"
31 
32 #include "core/CSSPropertyNames.h"
33 #include "core/css/parser/BisonCSSParser.h"
34 #include "core/css/StylePropertySet.h"
35 #include "core/html/HTMLCanvasElement.h"
36 #include "core/html/canvas/CanvasGradient.h"
37 #include "core/html/canvas/CanvasPattern.h"
38 #include "platform/graphics/GraphicsContext.h"
39 #include "wtf/PassRefPtr.h"
40 
41 namespace WebCore {
42 
43 enum ColorParseResult { ParsedRGBA, ParsedCurrentColor, ParsedSystemColor, ParseFailed };
44 
parseColor(RGBA32 & parsedColor,const String & colorString)45 static ColorParseResult parseColor(RGBA32& parsedColor, const String& colorString)
46 {
47     if (equalIgnoringCase(colorString, "currentcolor"))
48         return ParsedCurrentColor;
49     const bool useStrictParsing = true;
50     if (BisonCSSParser::parseColor(parsedColor, colorString, useStrictParsing))
51         return ParsedRGBA;
52     if (BisonCSSParser::parseSystemColor(parsedColor, colorString))
53         return ParsedSystemColor;
54     return ParseFailed;
55 }
56 
currentColor(HTMLCanvasElement * canvas)57 RGBA32 currentColor(HTMLCanvasElement* canvas)
58 {
59     if (!canvas || !canvas->inDocument() || !canvas->inlineStyle())
60         return Color::black;
61     RGBA32 rgba = Color::black;
62     BisonCSSParser::parseColor(rgba, canvas->inlineStyle()->getPropertyValue(CSSPropertyColor));
63     return rgba;
64 }
65 
parseColorOrCurrentColor(RGBA32 & parsedColor,const String & colorString,HTMLCanvasElement * canvas)66 bool parseColorOrCurrentColor(RGBA32& parsedColor, const String& colorString, HTMLCanvasElement* canvas)
67 {
68     ColorParseResult parseResult = parseColor(parsedColor, colorString);
69     switch (parseResult) {
70     case ParsedRGBA:
71     case ParsedSystemColor:
72         return true;
73     case ParsedCurrentColor:
74         parsedColor = currentColor(canvas);
75         return true;
76     case ParseFailed:
77         return false;
78     default:
79         ASSERT_NOT_REACHED();
80         return false;
81     }
82 }
83 
CanvasStyle(Type type,float overrideAlpha)84 CanvasStyle::CanvasStyle(Type type, float overrideAlpha)
85     : m_type(type)
86     , m_overrideAlpha(overrideAlpha)
87 {
88 }
89 
CanvasStyle(RGBA32 rgba)90 CanvasStyle::CanvasStyle(RGBA32 rgba)
91     : m_type(RGBA)
92     , m_rgba(rgba)
93 {
94 }
95 
CanvasStyle(float grayLevel,float alpha)96 CanvasStyle::CanvasStyle(float grayLevel, float alpha)
97     : m_type(RGBA)
98     , m_rgba(makeRGBA32FromFloats(grayLevel, grayLevel, grayLevel, alpha))
99 {
100 }
101 
CanvasStyle(float r,float g,float b,float a)102 CanvasStyle::CanvasStyle(float r, float g, float b, float a)
103     : m_type(RGBA)
104     , m_rgba(makeRGBA32FromFloats(r, g, b, a))
105 {
106 }
107 
CanvasStyle(float c,float m,float y,float k,float a)108 CanvasStyle::CanvasStyle(float c, float m, float y, float k, float a)
109     : m_type(CMYKA)
110     , m_rgba(makeRGBAFromCMYKA(c, m, y, k, a))
111     , m_cmyka(c, m, y, k, a)
112 {
113 }
114 
CanvasStyle(PassRefPtr<CanvasGradient> gradient)115 CanvasStyle::CanvasStyle(PassRefPtr<CanvasGradient> gradient)
116     : m_type(Gradient)
117     , m_gradient(gradient)
118 {
119 }
120 
CanvasStyle(PassRefPtr<CanvasPattern> pattern)121 CanvasStyle::CanvasStyle(PassRefPtr<CanvasPattern> pattern)
122     : m_type(ImagePattern)
123     , m_pattern(pattern)
124 {
125 }
126 
createFromString(const String & color)127 PassRefPtr<CanvasStyle> CanvasStyle::createFromString(const String& color)
128 {
129     RGBA32 rgba;
130     ColorParseResult parseResult = parseColor(rgba, color);
131     switch (parseResult) {
132     case ParsedRGBA:
133     case ParsedSystemColor:
134         return adoptRef(new CanvasStyle(rgba));
135     case ParsedCurrentColor:
136         return adoptRef(new CanvasStyle(CurrentColor));
137     case ParseFailed:
138         return nullptr;
139     default:
140         ASSERT_NOT_REACHED();
141         return nullptr;
142     }
143 }
144 
createFromStringWithOverrideAlpha(const String & color,float alpha)145 PassRefPtr<CanvasStyle> CanvasStyle::createFromStringWithOverrideAlpha(const String& color, float alpha)
146 {
147     RGBA32 rgba;
148     ColorParseResult parseResult = parseColor(rgba, color);
149     switch (parseResult) {
150     case ParsedRGBA:
151         return adoptRef(new CanvasStyle(colorWithOverrideAlpha(rgba, alpha)));
152     case ParsedCurrentColor:
153         return adoptRef(new CanvasStyle(CurrentColorWithOverrideAlpha, alpha));
154     case ParseFailed:
155         return nullptr;
156     default:
157         ASSERT_NOT_REACHED();
158         return nullptr;
159     }
160 }
161 
createFromGradient(PassRefPtr<CanvasGradient> gradient)162 PassRefPtr<CanvasStyle> CanvasStyle::createFromGradient(PassRefPtr<CanvasGradient> gradient)
163 {
164     if (!gradient)
165         return nullptr;
166     return adoptRef(new CanvasStyle(gradient));
167 }
168 
createFromPattern(PassRefPtr<CanvasPattern> pattern)169 PassRefPtr<CanvasStyle> CanvasStyle::createFromPattern(PassRefPtr<CanvasPattern> pattern)
170 {
171     if (!pattern)
172         return nullptr;
173     return adoptRef(new CanvasStyle(pattern));
174 }
175 
isEquivalentColor(const CanvasStyle & other) const176 bool CanvasStyle::isEquivalentColor(const CanvasStyle& other) const
177 {
178     if (m_type != other.m_type)
179         return false;
180 
181     switch (m_type) {
182     case RGBA:
183         return m_rgba == other.m_rgba;
184     case CMYKA:
185         return m_cmyka.c == other.m_cmyka.c
186             && m_cmyka.m == other.m_cmyka.m
187             && m_cmyka.y == other.m_cmyka.y
188             && m_cmyka.k == other.m_cmyka.k
189             && m_cmyka.a == other.m_cmyka.a;
190     case Gradient:
191     case ImagePattern:
192     case CurrentColor:
193     case CurrentColorWithOverrideAlpha:
194         return false;
195     }
196 
197     ASSERT_NOT_REACHED();
198     return false;
199 }
200 
isEquivalentRGBA(float r,float g,float b,float a) const201 bool CanvasStyle::isEquivalentRGBA(float r, float g, float b, float a) const
202 {
203     if (m_type != RGBA)
204         return false;
205 
206     return m_rgba == makeRGBA32FromFloats(r, g, b, a);
207 }
208 
isEquivalentCMYKA(float c,float m,float y,float k,float a) const209 bool CanvasStyle::isEquivalentCMYKA(float c, float m, float y, float k, float a) const
210 {
211     if (m_type != CMYKA)
212         return false;
213 
214     return c == m_cmyka.c
215         && m == m_cmyka.m
216         && y == m_cmyka.y
217         && k == m_cmyka.k
218         && a == m_cmyka.a;
219 }
220 
applyStrokeColor(GraphicsContext * context)221 void CanvasStyle::applyStrokeColor(GraphicsContext* context)
222 {
223     if (!context)
224         return;
225     switch (m_type) {
226     case RGBA:
227         context->setStrokeColor(m_rgba);
228         break;
229     case CMYKA: {
230         // FIXME: Do this through platform-independent GraphicsContext API.
231         // We'll need a fancier Color abstraction to support CMYKA correctly
232         context->setStrokeColor(m_rgba);
233         break;
234     }
235     case Gradient:
236         context->setStrokeGradient(canvasGradient()->gradient());
237         break;
238     case ImagePattern:
239         context->setStrokePattern(canvasPattern()->pattern());
240         break;
241     case CurrentColor:
242     case CurrentColorWithOverrideAlpha:
243         ASSERT_NOT_REACHED();
244         break;
245     }
246 }
247 
applyFillColor(GraphicsContext * context)248 void CanvasStyle::applyFillColor(GraphicsContext* context)
249 {
250     if (!context)
251         return;
252     switch (m_type) {
253     case RGBA:
254         context->setFillColor(m_rgba);
255         break;
256     case CMYKA: {
257         // FIXME: Do this through platform-independent GraphicsContext API.
258         // We'll need a fancier Color abstraction to support CMYKA correctly
259         context->setFillColor(m_rgba);
260         break;
261     }
262     case Gradient:
263         context->setFillGradient(canvasGradient()->gradient());
264         break;
265     case ImagePattern:
266         context->setFillPattern(canvasPattern()->pattern());
267         break;
268     case CurrentColor:
269     case CurrentColorWithOverrideAlpha:
270         ASSERT_NOT_REACHED();
271         break;
272     }
273 }
274 
275 }
276