|
1 /* |
|
2 * Copyright (C) 2006, 2007, 2009 Apple Inc. All rights reserved. |
|
3 * |
|
4 * This library is free software; you can redistribute it and/or |
|
5 * modify it under the terms of the GNU Library General Public |
|
6 * License as published by the Free Software Foundation; either |
|
7 * version 2 of the License, or (at your option) any later version. |
|
8 * |
|
9 * This library is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
12 * Library General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU Library General Public License |
|
15 * along with this library; see the file COPYING.LIB. If not, write to |
|
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
|
17 * Boston, MA 02110-1301, USA. |
|
18 */ |
|
19 |
|
20 #include "config.h" |
|
21 #include "JSCanvasRenderingContext2D.h" |
|
22 |
|
23 #include "CanvasGradient.h" |
|
24 #include "CanvasPattern.h" |
|
25 #include "CanvasRenderingContext2D.h" |
|
26 #include "CanvasStyle.h" |
|
27 #include "ExceptionCode.h" |
|
28 #include "FloatRect.h" |
|
29 #include "HTMLCanvasElement.h" |
|
30 #include "HTMLImageElement.h" |
|
31 #include "HTMLVideoElement.h" |
|
32 #include "ImageData.h" |
|
33 #include "JSCanvasGradient.h" |
|
34 #include "JSCanvasPattern.h" |
|
35 #include "JSHTMLCanvasElement.h" |
|
36 #include "JSHTMLImageElement.h" |
|
37 #include "JSHTMLVideoElement.h" |
|
38 #include "JSImageData.h" |
|
39 #include <runtime/Error.h> |
|
40 |
|
41 using namespace JSC; |
|
42 |
|
43 namespace WebCore { |
|
44 |
|
45 static JSValue toJS(ExecState* exec, CanvasStyle* style) |
|
46 { |
|
47 if (style->canvasGradient()) |
|
48 return toJS(exec, style->canvasGradient()); |
|
49 if (style->canvasPattern()) |
|
50 return toJS(exec, style->canvasPattern()); |
|
51 return jsString(exec, style->color()); |
|
52 } |
|
53 |
|
54 static PassRefPtr<CanvasStyle> toHTMLCanvasStyle(ExecState*, JSValue value) |
|
55 { |
|
56 if (!value.isObject()) |
|
57 return 0; |
|
58 JSObject* object = asObject(value); |
|
59 if (object->inherits(&JSCanvasGradient::s_info)) |
|
60 return CanvasStyle::create(static_cast<JSCanvasGradient*>(object)->impl()); |
|
61 if (object->inherits(&JSCanvasPattern::s_info)) |
|
62 return CanvasStyle::create(static_cast<JSCanvasPattern*>(object)->impl()); |
|
63 return 0; |
|
64 } |
|
65 |
|
66 JSValue JSCanvasRenderingContext2D::strokeStyle(ExecState* exec) const |
|
67 { |
|
68 CanvasRenderingContext2D* context = static_cast<CanvasRenderingContext2D*>(impl()); |
|
69 return toJS(exec, context->strokeStyle()); |
|
70 } |
|
71 |
|
72 void JSCanvasRenderingContext2D::setStrokeStyle(ExecState* exec, JSValue value) |
|
73 { |
|
74 CanvasRenderingContext2D* context = static_cast<CanvasRenderingContext2D*>(impl()); |
|
75 if (value.isString()) { |
|
76 context->setStrokeColor(ustringToString(asString(value)->value(exec))); |
|
77 return; |
|
78 } |
|
79 context->setStrokeStyle(toHTMLCanvasStyle(exec, value)); |
|
80 } |
|
81 |
|
82 JSValue JSCanvasRenderingContext2D::fillStyle(ExecState* exec) const |
|
83 { |
|
84 CanvasRenderingContext2D* context = static_cast<CanvasRenderingContext2D*>(impl()); |
|
85 return toJS(exec, context->fillStyle()); |
|
86 } |
|
87 |
|
88 void JSCanvasRenderingContext2D::setFillStyle(ExecState* exec, JSValue value) |
|
89 { |
|
90 CanvasRenderingContext2D* context = static_cast<CanvasRenderingContext2D*>(impl()); |
|
91 if (value.isString()) { |
|
92 context->setFillColor(ustringToString(asString(value)->value(exec))); |
|
93 return; |
|
94 } |
|
95 context->setFillStyle(toHTMLCanvasStyle(exec, value)); |
|
96 } |
|
97 |
|
98 JSValue JSCanvasRenderingContext2D::setFillColor(ExecState* exec) |
|
99 { |
|
100 CanvasRenderingContext2D* context = static_cast<CanvasRenderingContext2D*>(impl()); |
|
101 |
|
102 // string arg = named color |
|
103 // number arg = gray color |
|
104 // string arg, number arg = named color, alpha |
|
105 // number arg, number arg = gray color, alpha |
|
106 // 4 args = r, g, b, a |
|
107 // 5 args = c, m, y, k, a |
|
108 switch (exec->argumentCount()) { |
|
109 case 1: |
|
110 if (exec->argument(0).isString()) |
|
111 context->setFillColor(ustringToString(asString(exec->argument(0))->value(exec))); |
|
112 else |
|
113 context->setFillColor(exec->argument(0).toFloat(exec)); |
|
114 break; |
|
115 case 2: |
|
116 if (exec->argument(0).isString()) |
|
117 context->setFillColor(ustringToString(asString(exec->argument(0))->value(exec)), exec->argument(1).toFloat(exec)); |
|
118 else |
|
119 context->setFillColor(exec->argument(0).toFloat(exec), exec->argument(1).toFloat(exec)); |
|
120 break; |
|
121 case 4: |
|
122 context->setFillColor(exec->argument(0).toFloat(exec), exec->argument(1).toFloat(exec), |
|
123 exec->argument(2).toFloat(exec), exec->argument(3).toFloat(exec)); |
|
124 break; |
|
125 case 5: |
|
126 context->setFillColor(exec->argument(0).toFloat(exec), exec->argument(1).toFloat(exec), |
|
127 exec->argument(2).toFloat(exec), exec->argument(3).toFloat(exec), exec->argument(4).toFloat(exec)); |
|
128 break; |
|
129 default: |
|
130 return throwSyntaxError(exec); |
|
131 } |
|
132 return jsUndefined(); |
|
133 } |
|
134 |
|
135 JSValue JSCanvasRenderingContext2D::setStrokeColor(ExecState* exec) |
|
136 { |
|
137 CanvasRenderingContext2D* context = static_cast<CanvasRenderingContext2D*>(impl()); |
|
138 |
|
139 // string arg = named color |
|
140 // number arg = gray color |
|
141 // string arg, number arg = named color, alpha |
|
142 // number arg, number arg = gray color, alpha |
|
143 // 4 args = r, g, b, a |
|
144 // 5 args = c, m, y, k, a |
|
145 switch (exec->argumentCount()) { |
|
146 case 1: |
|
147 if (exec->argument(0).isString()) |
|
148 context->setStrokeColor(ustringToString(asString(exec->argument(0))->value(exec))); |
|
149 else |
|
150 context->setStrokeColor(exec->argument(0).toFloat(exec)); |
|
151 break; |
|
152 case 2: |
|
153 if (exec->argument(0).isString()) |
|
154 context->setStrokeColor(ustringToString(asString(exec->argument(0))->value(exec)), exec->argument(1).toFloat(exec)); |
|
155 else |
|
156 context->setStrokeColor(exec->argument(0).toFloat(exec), exec->argument(1).toFloat(exec)); |
|
157 break; |
|
158 case 4: |
|
159 context->setStrokeColor(exec->argument(0).toFloat(exec), exec->argument(1).toFloat(exec), |
|
160 exec->argument(2).toFloat(exec), exec->argument(3).toFloat(exec)); |
|
161 break; |
|
162 case 5: |
|
163 context->setStrokeColor(exec->argument(0).toFloat(exec), exec->argument(1).toFloat(exec), |
|
164 exec->argument(2).toFloat(exec), exec->argument(3).toFloat(exec), exec->argument(4).toFloat(exec)); |
|
165 break; |
|
166 default: |
|
167 return throwSyntaxError(exec); |
|
168 } |
|
169 |
|
170 return jsUndefined(); |
|
171 } |
|
172 |
|
173 JSValue JSCanvasRenderingContext2D::strokeRect(ExecState* exec) |
|
174 { |
|
175 CanvasRenderingContext2D* context = static_cast<CanvasRenderingContext2D*>(impl()); |
|
176 |
|
177 if (exec->argumentCount() <= 4) |
|
178 context->strokeRect(exec->argument(0).toFloat(exec), exec->argument(1).toFloat(exec), |
|
179 exec->argument(2).toFloat(exec), exec->argument(3).toFloat(exec)); |
|
180 else |
|
181 context->strokeRect(exec->argument(0).toFloat(exec), exec->argument(1).toFloat(exec), |
|
182 exec->argument(2).toFloat(exec), exec->argument(3).toFloat(exec), exec->argument(4).toFloat(exec)); |
|
183 |
|
184 return jsUndefined(); |
|
185 } |
|
186 |
|
187 JSValue JSCanvasRenderingContext2D::drawImage(ExecState* exec) |
|
188 { |
|
189 CanvasRenderingContext2D* context = static_cast<CanvasRenderingContext2D*>(impl()); |
|
190 |
|
191 // DrawImage has three variants: |
|
192 // drawImage(img, dx, dy) |
|
193 // drawImage(img, dx, dy, dw, dh) |
|
194 // drawImage(img, sx, sy, sw, sh, dx, dy, dw, dh) |
|
195 // Composite operation is specified with globalCompositeOperation. |
|
196 // The img parameter can be a <img> or <canvas> element. |
|
197 JSValue value = exec->argument(0); |
|
198 if (!value.isObject()) |
|
199 return throwTypeError(exec); |
|
200 JSObject* o = asObject(value); |
|
201 |
|
202 ExceptionCode ec = 0; |
|
203 if (o->inherits(&JSHTMLImageElement::s_info)) { |
|
204 HTMLImageElement* imgElt = static_cast<HTMLImageElement*>(static_cast<JSHTMLElement*>(o)->impl()); |
|
205 switch (exec->argumentCount()) { |
|
206 case 3: |
|
207 context->drawImage(imgElt, exec->argument(1).toFloat(exec), exec->argument(2).toFloat(exec), ec); |
|
208 break; |
|
209 case 5: |
|
210 context->drawImage(imgElt, exec->argument(1).toFloat(exec), exec->argument(2).toFloat(exec), |
|
211 exec->argument(3).toFloat(exec), exec->argument(4).toFloat(exec), ec); |
|
212 setDOMException(exec, ec); |
|
213 break; |
|
214 case 9: |
|
215 context->drawImage(imgElt, FloatRect(exec->argument(1).toFloat(exec), exec->argument(2).toFloat(exec), |
|
216 exec->argument(3).toFloat(exec), exec->argument(4).toFloat(exec)), |
|
217 FloatRect(exec->argument(5).toFloat(exec), exec->argument(6).toFloat(exec), |
|
218 exec->argument(7).toFloat(exec), exec->argument(8).toFloat(exec)), ec); |
|
219 setDOMException(exec, ec); |
|
220 break; |
|
221 default: |
|
222 return throwSyntaxError(exec); |
|
223 } |
|
224 } else if (o->inherits(&JSHTMLCanvasElement::s_info)) { |
|
225 HTMLCanvasElement* canvas = static_cast<HTMLCanvasElement*>(static_cast<JSHTMLElement*>(o)->impl()); |
|
226 switch (exec->argumentCount()) { |
|
227 case 3: |
|
228 context->drawImage(canvas, exec->argument(1).toFloat(exec), exec->argument(2).toFloat(exec), ec); |
|
229 setDOMException(exec, ec); |
|
230 break; |
|
231 case 5: |
|
232 context->drawImage(canvas, exec->argument(1).toFloat(exec), exec->argument(2).toFloat(exec), |
|
233 exec->argument(3).toFloat(exec), exec->argument(4).toFloat(exec), ec); |
|
234 setDOMException(exec, ec); |
|
235 break; |
|
236 case 9: |
|
237 context->drawImage(canvas, FloatRect(exec->argument(1).toFloat(exec), exec->argument(2).toFloat(exec), |
|
238 exec->argument(3).toFloat(exec), exec->argument(4).toFloat(exec)), |
|
239 FloatRect(exec->argument(5).toFloat(exec), exec->argument(6).toFloat(exec), |
|
240 exec->argument(7).toFloat(exec), exec->argument(8).toFloat(exec)), ec); |
|
241 setDOMException(exec, ec); |
|
242 break; |
|
243 default: |
|
244 return throwSyntaxError(exec); |
|
245 } |
|
246 #if ENABLE(VIDEO) |
|
247 } else if (o->inherits(&JSHTMLVideoElement::s_info)) { |
|
248 HTMLVideoElement* video = static_cast<HTMLVideoElement*>(static_cast<JSHTMLElement*>(o)->impl()); |
|
249 switch (exec->argumentCount()) { |
|
250 case 3: |
|
251 context->drawImage(video, exec->argument(1).toFloat(exec), exec->argument(2).toFloat(exec), ec); |
|
252 break; |
|
253 case 5: |
|
254 context->drawImage(video, exec->argument(1).toFloat(exec), exec->argument(2).toFloat(exec), |
|
255 exec->argument(3).toFloat(exec), exec->argument(4).toFloat(exec), ec); |
|
256 setDOMException(exec, ec); |
|
257 break; |
|
258 case 9: |
|
259 context->drawImage(video, FloatRect(exec->argument(1).toFloat(exec), exec->argument(2).toFloat(exec), |
|
260 exec->argument(3).toFloat(exec), exec->argument(4).toFloat(exec)), |
|
261 FloatRect(exec->argument(5).toFloat(exec), exec->argument(6).toFloat(exec), |
|
262 exec->argument(7).toFloat(exec), exec->argument(8).toFloat(exec)), ec); |
|
263 setDOMException(exec, ec); |
|
264 break; |
|
265 default: |
|
266 return throwSyntaxError(exec); |
|
267 } |
|
268 #endif |
|
269 } else |
|
270 return throwTypeError(exec); |
|
271 |
|
272 return jsUndefined(); |
|
273 } |
|
274 |
|
275 JSValue JSCanvasRenderingContext2D::drawImageFromRect(ExecState* exec) |
|
276 { |
|
277 CanvasRenderingContext2D* context = static_cast<CanvasRenderingContext2D*>(impl()); |
|
278 |
|
279 JSValue value = exec->argument(0); |
|
280 if (!value.isObject()) |
|
281 return throwTypeError(exec); |
|
282 JSObject* o = asObject(value); |
|
283 |
|
284 if (!o->inherits(&JSHTMLImageElement::s_info)) |
|
285 return throwTypeError(exec); |
|
286 context->drawImageFromRect(static_cast<HTMLImageElement*>(static_cast<JSHTMLElement*>(o)->impl()), |
|
287 exec->argument(1).toFloat(exec), exec->argument(2).toFloat(exec), |
|
288 exec->argument(3).toFloat(exec), exec->argument(4).toFloat(exec), |
|
289 exec->argument(5).toFloat(exec), exec->argument(6).toFloat(exec), |
|
290 exec->argument(7).toFloat(exec), exec->argument(8).toFloat(exec), |
|
291 ustringToString(exec->argument(9).toString(exec))); |
|
292 return jsUndefined(); |
|
293 } |
|
294 |
|
295 JSValue JSCanvasRenderingContext2D::setShadow(ExecState* exec) |
|
296 { |
|
297 CanvasRenderingContext2D* context = static_cast<CanvasRenderingContext2D*>(impl()); |
|
298 |
|
299 switch (exec->argumentCount()) { |
|
300 case 3: |
|
301 context->setShadow(exec->argument(0).toFloat(exec), exec->argument(1).toFloat(exec), |
|
302 exec->argument(2).toFloat(exec)); |
|
303 break; |
|
304 case 4: |
|
305 if (exec->argument(3).isString()) |
|
306 context->setShadow(exec->argument(0).toFloat(exec), exec->argument(1).toFloat(exec), |
|
307 exec->argument(2).toFloat(exec), ustringToString(asString(exec->argument(3))->value(exec))); |
|
308 else |
|
309 context->setShadow(exec->argument(0).toFloat(exec), exec->argument(1).toFloat(exec), |
|
310 exec->argument(2).toFloat(exec), exec->argument(3).toFloat(exec)); |
|
311 break; |
|
312 case 5: |
|
313 if (exec->argument(3).isString()) |
|
314 context->setShadow(exec->argument(0).toFloat(exec), exec->argument(1).toFloat(exec), |
|
315 exec->argument(2).toFloat(exec), ustringToString(asString(exec->argument(3))->value(exec)), |
|
316 exec->argument(4).toFloat(exec)); |
|
317 else |
|
318 context->setShadow(exec->argument(0).toFloat(exec), exec->argument(1).toFloat(exec), |
|
319 exec->argument(2).toFloat(exec), exec->argument(3).toFloat(exec), |
|
320 exec->argument(4).toFloat(exec)); |
|
321 break; |
|
322 case 7: |
|
323 context->setShadow(exec->argument(0).toFloat(exec), exec->argument(1).toFloat(exec), |
|
324 exec->argument(2).toFloat(exec), exec->argument(3).toFloat(exec), |
|
325 exec->argument(4).toFloat(exec), exec->argument(5).toFloat(exec), |
|
326 exec->argument(6).toFloat(exec)); |
|
327 break; |
|
328 case 8: |
|
329 context->setShadow(exec->argument(0).toFloat(exec), exec->argument(1).toFloat(exec), |
|
330 exec->argument(2).toFloat(exec), exec->argument(3).toFloat(exec), |
|
331 exec->argument(4).toFloat(exec), exec->argument(5).toFloat(exec), |
|
332 exec->argument(6).toFloat(exec), exec->argument(7).toFloat(exec)); |
|
333 break; |
|
334 default: |
|
335 return throwSyntaxError(exec); |
|
336 } |
|
337 |
|
338 return jsUndefined(); |
|
339 } |
|
340 |
|
341 JSValue JSCanvasRenderingContext2D::createPattern(ExecState* exec) |
|
342 { |
|
343 CanvasRenderingContext2D* context = static_cast<CanvasRenderingContext2D*>(impl()); |
|
344 |
|
345 JSValue value = exec->argument(0); |
|
346 if (!value.isObject()) |
|
347 return throwTypeError(exec); |
|
348 JSObject* o = asObject(value); |
|
349 |
|
350 if (o->inherits(&JSHTMLImageElement::s_info)) { |
|
351 ExceptionCode ec; |
|
352 JSValue pattern = toJS(exec, |
|
353 context->createPattern(static_cast<HTMLImageElement*>(static_cast<JSHTMLElement*>(o)->impl()), |
|
354 valueToStringWithNullCheck(exec, exec->argument(1)), ec).get()); |
|
355 setDOMException(exec, ec); |
|
356 return pattern; |
|
357 } |
|
358 if (o->inherits(&JSHTMLCanvasElement::s_info)) { |
|
359 ExceptionCode ec; |
|
360 JSValue pattern = toJS(exec, |
|
361 context->createPattern(static_cast<HTMLCanvasElement*>(static_cast<JSHTMLElement*>(o)->impl()), |
|
362 valueToStringWithNullCheck(exec, exec->argument(1)), ec).get()); |
|
363 setDOMException(exec, ec); |
|
364 return pattern; |
|
365 } |
|
366 setDOMException(exec, TYPE_MISMATCH_ERR); |
|
367 return jsUndefined(); |
|
368 } |
|
369 |
|
370 JSValue JSCanvasRenderingContext2D::createImageData(ExecState* exec) |
|
371 { |
|
372 // createImageData has two variants |
|
373 // createImageData(ImageData) |
|
374 // createImageData(width, height) |
|
375 CanvasRenderingContext2D* context = static_cast<CanvasRenderingContext2D*>(impl()); |
|
376 RefPtr<ImageData> imageData = 0; |
|
377 |
|
378 ExceptionCode ec = 0; |
|
379 if (exec->argumentCount() == 1) |
|
380 imageData = context->createImageData(toImageData(exec->argument(0)), ec); |
|
381 else if (exec->argumentCount() == 2) |
|
382 imageData = context->createImageData(exec->argument(0).toFloat(exec), exec->argument(1).toFloat(exec), ec); |
|
383 |
|
384 setDOMException(exec, ec); |
|
385 return toJS(exec, globalObject(), WTF::getPtr(imageData)); |
|
386 } |
|
387 |
|
388 JSValue JSCanvasRenderingContext2D::putImageData(ExecState* exec) |
|
389 { |
|
390 // putImageData has two variants |
|
391 // putImageData(ImageData, x, y) |
|
392 // putImageData(ImageData, x, y, dirtyX, dirtyY, dirtyWidth, dirtyHeight) |
|
393 CanvasRenderingContext2D* context = static_cast<CanvasRenderingContext2D*>(impl()); |
|
394 |
|
395 ExceptionCode ec = 0; |
|
396 if (exec->argumentCount() >= 7) |
|
397 context->putImageData(toImageData(exec->argument(0)), exec->argument(1).toFloat(exec), exec->argument(2).toFloat(exec), |
|
398 exec->argument(3).toFloat(exec), exec->argument(4).toFloat(exec), exec->argument(5).toFloat(exec), exec->argument(6).toFloat(exec), ec); |
|
399 else |
|
400 context->putImageData(toImageData(exec->argument(0)), exec->argument(1).toFloat(exec), exec->argument(2).toFloat(exec), ec); |
|
401 |
|
402 setDOMException(exec, ec); |
|
403 return jsUndefined(); |
|
404 } |
|
405 |
|
406 JSValue JSCanvasRenderingContext2D::fillText(ExecState* exec) |
|
407 { |
|
408 CanvasRenderingContext2D* context = static_cast<CanvasRenderingContext2D*>(impl()); |
|
409 |
|
410 // string arg = text to draw |
|
411 // number arg = x |
|
412 // number arg = y |
|
413 // optional number arg = maxWidth |
|
414 if (exec->argumentCount() < 3 || exec->argumentCount() > 4) |
|
415 return throwSyntaxError(exec); |
|
416 |
|
417 if (exec->argumentCount() == 4) |
|
418 context->fillText(ustringToString(exec->argument(0).toString(exec)), exec->argument(1).toFloat(exec), exec->argument(2).toFloat(exec), exec->argument(3).toFloat(exec)); |
|
419 else |
|
420 context->fillText(ustringToString(exec->argument(0).toString(exec)), exec->argument(1).toFloat(exec), exec->argument(2).toFloat(exec)); |
|
421 return jsUndefined(); |
|
422 } |
|
423 |
|
424 JSValue JSCanvasRenderingContext2D::strokeText(ExecState* exec) |
|
425 { |
|
426 CanvasRenderingContext2D* context = static_cast<CanvasRenderingContext2D*>(impl()); |
|
427 |
|
428 // string arg = text to draw |
|
429 // number arg = x |
|
430 // number arg = y |
|
431 // optional number arg = maxWidth |
|
432 if (exec->argumentCount() < 3 || exec->argumentCount() > 4) |
|
433 return throwSyntaxError(exec); |
|
434 |
|
435 if (exec->argumentCount() == 4) |
|
436 context->strokeText(ustringToString(exec->argument(0).toString(exec)), exec->argument(1).toFloat(exec), exec->argument(2).toFloat(exec), exec->argument(3).toFloat(exec)); |
|
437 else |
|
438 context->strokeText(ustringToString(exec->argument(0).toString(exec)), exec->argument(1).toFloat(exec), exec->argument(2).toFloat(exec)); |
|
439 return jsUndefined(); |
|
440 } |
|
441 |
|
442 } // namespace WebCore |