OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved. | 2 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved. |
3 * Copyright (C) 2008, 2010 Nokia Corporation and/or its subsidiary(-ies) | 3 * Copyright (C) 2008, 2010 Nokia Corporation and/or its subsidiary(-ies) |
4 * Copyright (C) 2007 Alp Toker <alp@atoker.com> | 4 * Copyright (C) 2007 Alp Toker <alp@atoker.com> |
5 * Copyright (C) 2008 Eric Seidel <eric@webkit.org> | 5 * Copyright (C) 2008 Eric Seidel <eric@webkit.org> |
6 * | 6 * |
7 * Redistribution and use in source and binary forms, with or without | 7 * Redistribution and use in source and binary forms, with or without |
8 * modification, are permitted provided that the following conditions | 8 * modification, are permitted provided that the following conditions |
9 * are met: | 9 * are met: |
10 * 1. Redistributions of source code must retain the above copyright | 10 * 1. Redistributions of source code must retain the above copyright |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 if (equalIgnoringCase(colorString, "currentcolor")) | 47 if (equalIgnoringCase(colorString, "currentcolor")) |
48 return ParsedCurrentColor; | 48 return ParsedCurrentColor; |
49 const bool useStrictParsing = true; | 49 const bool useStrictParsing = true; |
50 if (CSSParser::parseColor(parsedColor, colorString, useStrictParsing)) | 50 if (CSSParser::parseColor(parsedColor, colorString, useStrictParsing)) |
51 return ParsedRGBA; | 51 return ParsedRGBA; |
52 if (CSSParser::parseSystemColor(parsedColor, colorString)) | 52 if (CSSParser::parseSystemColor(parsedColor, colorString)) |
53 return ParsedSystemColor; | 53 return ParsedSystemColor; |
54 return ParseFailed; | 54 return ParseFailed; |
55 } | 55 } |
56 | 56 |
57 RGBA32 currentColor(HTMLCanvasElement* canvas) | 57 static RGBA32 currentColor(HTMLCanvasElement* canvas) |
58 { | 58 { |
59 if (!canvas || !canvas->inDocument() || !canvas->inlineStyle()) | 59 if (!canvas || !canvas->inDocument() || !canvas->inlineStyle()) |
60 return Color::black; | 60 return Color::black; |
61 RGBA32 rgba = Color::black; | 61 RGBA32 rgba = Color::black; |
62 CSSParser::parseColor(rgba, canvas->inlineStyle()->getPropertyValue(CSSPrope
rtyColor)); | 62 CSSParser::parseColor(rgba, canvas->inlineStyle()->getPropertyValue(CSSPrope
rtyColor)); |
63 return rgba; | 63 return rgba; |
64 } | 64 } |
65 | 65 |
66 bool parseColorOrCurrentColor(RGBA32& parsedColor, const String& colorString, HT
MLCanvasElement* canvas) | 66 bool parseColorOrCurrentColor(RGBA32& parsedColor, const String& colorString, HT
MLCanvasElement* canvas) |
67 { | 67 { |
68 ColorParseResult parseResult = parseColor(parsedColor, colorString); | 68 ColorParseResult parseResult = parseColor(parsedColor, colorString); |
69 switch (parseResult) { | 69 switch (parseResult) { |
70 case ParsedRGBA: | 70 case ParsedRGBA: |
71 case ParsedSystemColor: | 71 case ParsedSystemColor: |
72 return true; | 72 return true; |
73 case ParsedCurrentColor: | 73 case ParsedCurrentColor: |
74 parsedColor = currentColor(canvas); | 74 parsedColor = currentColor(canvas); |
75 return true; | 75 return true; |
76 case ParseFailed: | 76 case ParseFailed: |
77 return false; | 77 return false; |
78 default: | 78 default: |
79 ASSERT_NOT_REACHED(); | 79 ASSERT_NOT_REACHED(); |
80 return false; | 80 return false; |
81 } | 81 } |
82 } | 82 } |
83 | 83 |
84 CanvasStyle::CanvasStyle(Type type, float overrideAlpha) | |
85 : m_type(type) | |
86 , m_overrideAlpha(overrideAlpha) | |
87 { | |
88 } | |
89 | |
90 CanvasStyle::CanvasStyle(RGBA32 rgba) | 84 CanvasStyle::CanvasStyle(RGBA32 rgba) |
91 : m_type(RGBA) | 85 : m_type(ColorRGBA) |
92 , m_rgba(rgba) | 86 , m_rgba(rgba) |
93 { | 87 { |
94 } | 88 } |
95 | 89 |
96 CanvasStyle::CanvasStyle(float grayLevel, float alpha) | |
97 : m_type(RGBA) | |
98 , m_rgba(makeRGBA32FromFloats(grayLevel, grayLevel, grayLevel, alpha)) | |
99 { | |
100 } | |
101 | |
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 | |
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 | |
115 CanvasStyle::CanvasStyle(PassRefPtrWillBeRawPtr<CanvasGradient> gradient) | 90 CanvasStyle::CanvasStyle(PassRefPtrWillBeRawPtr<CanvasGradient> gradient) |
116 : m_type(Gradient) | 91 : m_type(Gradient) |
117 , m_gradient(gradient) | 92 , m_gradient(gradient) |
118 { | 93 { |
119 } | 94 } |
120 | 95 |
121 CanvasStyle::CanvasStyle(PassRefPtrWillBeRawPtr<CanvasPattern> pattern) | 96 CanvasStyle::CanvasStyle(PassRefPtrWillBeRawPtr<CanvasPattern> pattern) |
122 : m_type(ImagePattern) | 97 : m_type(ImagePattern) |
123 , m_pattern(pattern) | 98 , m_pattern(pattern) |
124 { | 99 { |
125 } | 100 } |
126 | 101 |
127 PassRefPtrWillBeRawPtr<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 adoptRefWillBeNoop(new CanvasStyle(rgba)); | |
135 case ParsedCurrentColor: | |
136 return adoptRefWillBeNoop(new CanvasStyle(CurrentColor)); | |
137 case ParseFailed: | |
138 return nullptr; | |
139 default: | |
140 ASSERT_NOT_REACHED(); | |
141 return nullptr; | |
142 } | |
143 } | |
144 | |
145 PassRefPtrWillBeRawPtr<CanvasStyle> CanvasStyle::createFromStringWithOverrideAlp
ha(const String& color, float alpha) | |
146 { | |
147 RGBA32 rgba; | |
148 ColorParseResult parseResult = parseColor(rgba, color); | |
149 switch (parseResult) { | |
150 case ParsedRGBA: | |
151 case ParsedSystemColor: | |
152 return adoptRefWillBeNoop(new CanvasStyle(colorWithOverrideAlpha(rgba, a
lpha))); | |
153 case ParsedCurrentColor: | |
154 return adoptRefWillBeNoop(new CanvasStyle(CurrentColorWithOverrideAlpha,
alpha)); | |
155 case ParseFailed: | |
156 return nullptr; | |
157 default: | |
158 ASSERT_NOT_REACHED(); | |
159 return nullptr; | |
160 } | |
161 } | |
162 | |
163 PassRefPtrWillBeRawPtr<CanvasStyle> CanvasStyle::createFromGradient(PassRefPtrWi
llBeRawPtr<CanvasGradient> gradient) | 102 PassRefPtrWillBeRawPtr<CanvasStyle> CanvasStyle::createFromGradient(PassRefPtrWi
llBeRawPtr<CanvasGradient> gradient) |
164 { | 103 { |
165 if (!gradient) | 104 ASSERT(gradient); |
166 return nullptr; | |
167 return adoptRefWillBeNoop(new CanvasStyle(gradient)); | 105 return adoptRefWillBeNoop(new CanvasStyle(gradient)); |
168 } | 106 } |
169 | 107 |
170 PassRefPtrWillBeRawPtr<CanvasStyle> CanvasStyle::createFromPattern(PassRefPtrWil
lBeRawPtr<CanvasPattern> pattern) | 108 PassRefPtrWillBeRawPtr<CanvasStyle> CanvasStyle::createFromPattern(PassRefPtrWil
lBeRawPtr<CanvasPattern> pattern) |
171 { | 109 { |
172 if (!pattern) | 110 ASSERT(pattern); |
173 return nullptr; | |
174 return adoptRefWillBeNoop(new CanvasStyle(pattern)); | 111 return adoptRefWillBeNoop(new CanvasStyle(pattern)); |
175 } | 112 } |
176 | 113 |
177 bool CanvasStyle::isEquivalentColor(const CanvasStyle& other) const | |
178 { | |
179 if (m_type != other.m_type) | |
180 return false; | |
181 | |
182 switch (m_type) { | |
183 case RGBA: | |
184 return m_rgba == other.m_rgba; | |
185 case CMYKA: | |
186 return m_cmyka.c == other.m_cmyka.c | |
187 && m_cmyka.m == other.m_cmyka.m | |
188 && m_cmyka.y == other.m_cmyka.y | |
189 && m_cmyka.k == other.m_cmyka.k | |
190 && m_cmyka.a == other.m_cmyka.a; | |
191 case Gradient: | |
192 case ImagePattern: | |
193 case CurrentColor: | |
194 case CurrentColorWithOverrideAlpha: | |
195 return false; | |
196 } | |
197 | |
198 ASSERT_NOT_REACHED(); | |
199 return false; | |
200 } | |
201 | |
202 bool CanvasStyle::isEquivalentRGBA(float r, float g, float b, float a) const | |
203 { | |
204 if (m_type != RGBA) | |
205 return false; | |
206 | |
207 return m_rgba == makeRGBA32FromFloats(r, g, b, a); | |
208 } | |
209 | |
210 bool CanvasStyle::isEquivalentCMYKA(float c, float m, float y, float k, float a)
const | |
211 { | |
212 if (m_type != CMYKA) | |
213 return false; | |
214 | |
215 return c == m_cmyka.c | |
216 && m == m_cmyka.m | |
217 && y == m_cmyka.y | |
218 && k == m_cmyka.k | |
219 && a == m_cmyka.a; | |
220 } | |
221 | |
222 void CanvasStyle::applyStrokeColor(GraphicsContext* context) | 114 void CanvasStyle::applyStrokeColor(GraphicsContext* context) |
223 { | 115 { |
224 if (!context) | 116 if (!context) |
225 return; | 117 return; |
226 switch (m_type) { | 118 switch (m_type) { |
227 case RGBA: | 119 case ColorRGBA: |
228 context->setStrokeColor(m_rgba); | 120 context->setStrokeColor(m_rgba); |
229 break; | 121 break; |
230 case CMYKA: { | |
231 // FIXME: Do this through platform-independent GraphicsContext API. | |
232 // We'll need a fancier Color abstraction to support CMYKA correctly | |
233 context->setStrokeColor(m_rgba); | |
234 break; | |
235 } | |
236 case Gradient: | 122 case Gradient: |
237 context->setStrokeGradient(canvasGradient()->gradient()); | 123 context->setStrokeGradient(canvasGradient()->gradient()); |
238 break; | 124 break; |
239 case ImagePattern: | 125 case ImagePattern: |
240 context->setStrokePattern(canvasPattern()->pattern()); | 126 context->setStrokePattern(canvasPattern()->pattern()); |
241 break; | 127 break; |
242 case CurrentColor: | |
243 case CurrentColorWithOverrideAlpha: | |
244 ASSERT_NOT_REACHED(); | |
245 break; | |
246 } | 128 } |
247 } | 129 } |
248 | 130 |
249 void CanvasStyle::applyFillColor(GraphicsContext* context) | 131 void CanvasStyle::applyFillColor(GraphicsContext* context) |
250 { | 132 { |
251 if (!context) | 133 if (!context) |
252 return; | 134 return; |
253 switch (m_type) { | 135 switch (m_type) { |
254 case RGBA: | 136 case ColorRGBA: |
255 context->setFillColor(m_rgba); | 137 context->setFillColor(m_rgba); |
256 break; | 138 break; |
257 case CMYKA: { | |
258 // FIXME: Do this through platform-independent GraphicsContext API. | |
259 // We'll need a fancier Color abstraction to support CMYKA correctly | |
260 context->setFillColor(m_rgba); | |
261 break; | |
262 } | |
263 case Gradient: | 139 case Gradient: |
264 context->setFillGradient(canvasGradient()->gradient()); | 140 context->setFillGradient(canvasGradient()->gradient()); |
265 break; | 141 break; |
266 case ImagePattern: | 142 case ImagePattern: |
267 context->setFillPattern(canvasPattern()->pattern()); | 143 context->setFillPattern(canvasPattern()->pattern()); |
268 break; | 144 break; |
269 case CurrentColor: | |
270 case CurrentColorWithOverrideAlpha: | |
271 ASSERT_NOT_REACHED(); | |
272 break; | |
273 } | 145 } |
274 } | 146 } |
275 | 147 |
276 void CanvasStyle::trace(Visitor* visitor) | 148 void CanvasStyle::trace(Visitor* visitor) |
277 { | 149 { |
278 visitor->trace(m_gradient); | 150 visitor->trace(m_gradient); |
279 visitor->trace(m_pattern); | 151 visitor->trace(m_pattern); |
280 } | 152 } |
281 | 153 |
282 } | 154 } |
OLD | NEW |