JSCanvasRenderingContext2DCustom.cpp 13.6 KB
Newer Older
1
/*
weinig's avatar
weinig committed
2
 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
ddkilzer's avatar
ddkilzer committed
16 17
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
18
 */
weinig's avatar
weinig committed
19

20
#include "config.h"
weinig's avatar
weinig committed
21
#include "JSCanvasRenderingContext2D.h"
22 23 24 25 26 27

#include "CanvasGradient.h"
#include "CanvasPattern.h"
#include "CanvasRenderingContext2D.h"
#include "CanvasStyle.h"
#include "ExceptionCode.h"
bdakin's avatar
bdakin committed
28
#include "FloatRect.h"
29 30
#include "HTMLCanvasElement.h"
#include "HTMLImageElement.h"
31
#include "ImageData.h"
32 33 34
#include "JSCanvasGradient.h"
#include "JSCanvasPattern.h"
#include "JSHTMLCanvasElement.h"
35
#include "JSHTMLImageElement.h"
36
#include "JSImageData.h"
37 38 39 40 41 42 43 44
#include "kjs_html.h"

using namespace KJS;

namespace WebCore {

static JSValue* toJS(ExecState* exec, CanvasStyle* style)
{
45 46
    if (style->canvasGradient())
        return toJS(exec, style->canvasGradient());
47 48
    if (style->pattern())
        return toJS(exec, style->pattern());
49
    return jsString(style->color());
50 51 52 53 54
}

static PassRefPtr<CanvasStyle> toHTMLCanvasStyle(ExecState* exec, JSValue* value)
{
    if (value->isString())
darin@apple.com's avatar
darin@apple.com committed
55
        return CanvasStyle::create(value->toString(exec));
56 57 58
    if (!value->isObject())
        return 0;
    JSObject* object = static_cast<JSObject*>(value);
59
    if (object->inherits(&JSCanvasGradient::s_info))
darin@apple.com's avatar
darin@apple.com committed
60
        return CanvasStyle::create(static_cast<JSCanvasGradient*>(object)->impl());
61
    if (object->inherits(&JSCanvasPattern::s_info))
darin@apple.com's avatar
darin@apple.com committed
62
        return CanvasStyle::create(static_cast<JSCanvasPattern*>(object)->impl());
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    return 0;
}

JSValue* JSCanvasRenderingContext2D::strokeStyle(ExecState* exec) const
{
    return toJS(exec, impl()->strokeStyle());        
}

void JSCanvasRenderingContext2D::setStrokeStyle(ExecState* exec, JSValue* value)
{
    impl()->setStrokeStyle(toHTMLCanvasStyle(exec, value));
}

JSValue* JSCanvasRenderingContext2D::fillStyle(ExecState* exec) const
{
    return toJS(exec, impl()->fillStyle());
}

void JSCanvasRenderingContext2D::setFillStyle(ExecState* exec, JSValue* value)
{
    impl()->setFillStyle(toHTMLCanvasStyle(exec, value));
}

JSValue* JSCanvasRenderingContext2D::setFillColor(ExecState* exec, const List& args)
{
    CanvasRenderingContext2D* context = impl();

    // string arg = named color
    // number arg = gray color
    // string arg, number arg = named color, alpha
    // number arg, number arg = gray color, alpha
    // 4 args = r, g, b, a
    // 5 args = c, m, y, k, a
    switch (args.size()) {
        case 1:
            if (args[0]->isString())
                context->setFillColor(args[0]->toString(exec));
            else
weinig's avatar
weinig committed
101
                context->setFillColor(args[0]->toFloat(exec));
102 103 104
            break;
        case 2:
            if (args[0]->isString())
weinig's avatar
weinig committed
105
                context->setFillColor(args[0]->toString(exec), args[1]->toFloat(exec));
106
            else
weinig's avatar
weinig committed
107
                context->setFillColor(args[0]->toFloat(exec), args[1]->toFloat(exec));
108 109
            break;
        case 4:
weinig's avatar
weinig committed
110 111
            context->setFillColor(args[0]->toFloat(exec), args[1]->toFloat(exec),
                                  args[2]->toFloat(exec), args[3]->toFloat(exec));
112 113
            break;
        case 5:
weinig's avatar
weinig committed
114 115
            context->setFillColor(args[0]->toFloat(exec), args[1]->toFloat(exec),
                                  args[2]->toFloat(exec), args[3]->toFloat(exec), args[4]->toFloat(exec));
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
            break;
        default:
            return throwError(exec, SyntaxError);
    }
    return jsUndefined();
}    

JSValue* JSCanvasRenderingContext2D::setStrokeColor(ExecState* exec, const List& args)
{ 
    CanvasRenderingContext2D* context = impl();

    // string arg = named color
    // number arg = gray color
    // string arg, number arg = named color, alpha
    // number arg, number arg = gray color, alpha
    // 4 args = r, g, b, a
    // 5 args = c, m, y, k, a
    switch (args.size()) {
        case 1:
            if (args[0]->isString())
                context->setStrokeColor(args[0]->toString(exec));
            else
weinig's avatar
weinig committed
138
                context->setStrokeColor(args[0]->toFloat(exec));
139 140 141
            break;
        case 2:
            if (args[0]->isString())
weinig's avatar
weinig committed
142
                context->setStrokeColor(args[0]->toString(exec), args[1]->toFloat(exec));
143
            else
weinig's avatar
weinig committed
144
                context->setStrokeColor(args[0]->toFloat(exec), args[1]->toFloat(exec));
145 146
            break;
        case 4:
weinig's avatar
weinig committed
147 148
            context->setStrokeColor(args[0]->toFloat(exec), args[1]->toFloat(exec),
                                    args[2]->toFloat(exec), args[3]->toFloat(exec));
149 150
            break;
        case 5:
weinig's avatar
weinig committed
151 152
            context->setStrokeColor(args[0]->toFloat(exec), args[1]->toFloat(exec),
                                    args[2]->toFloat(exec), args[3]->toFloat(exec), args[4]->toFloat(exec));
153 154 155 156 157 158 159 160 161 162
            break;
        default:
            return throwError(exec, SyntaxError);
    }
    
    return jsUndefined();
}

JSValue* JSCanvasRenderingContext2D::strokeRect(ExecState* exec, const List& args)
{ 
163
    CanvasRenderingContext2D* context = impl();
164 165
    
    if (args.size() <= 4)
weinig's avatar
weinig committed
166
        context->strokeRect(args[0]->toFloat(exec), args[1]->toFloat(exec),
167
                            args[2]->toFloat(exec), args[3]->toFloat(exec));
168
    else
weinig's avatar
weinig committed
169
        context->strokeRect(args[0]->toFloat(exec), args[1]->toFloat(exec),
170 171
                            args[2]->toFloat(exec), args[3]->toFloat(exec), args[4]->toFloat(exec));

172 173 174 175 176 177 178 179 180 181 182 183 184
    return jsUndefined();    
}

JSValue* JSCanvasRenderingContext2D::drawImage(ExecState* exec, const List& args)
{ 
    CanvasRenderingContext2D* context = impl();

    // DrawImage has three variants:
    //     drawImage(img, dx, dy)
    //     drawImage(img, dx, dy, dw, dh)
    //     drawImage(img, sx, sy, sw, sh, dx, dy, dw, dh)
    // Composite operation is specified with globalCompositeOperation.
    // The img parameter can be a <img> or <canvas> element.
185 186
    JSValue* value = args[0];
    if (!value->isObject())
187
        return throwError(exec, TypeError);
188 189
    JSObject* o = static_cast<JSObject*>(value);
    
thatcher's avatar
thatcher committed
190
    ExceptionCode ec = 0;
191
    if (o->inherits(&JSHTMLImageElement::s_info)) {
192 193 194
        HTMLImageElement* imgElt = static_cast<HTMLImageElement*>(static_cast<JSHTMLElement*>(args[0])->impl());
        switch (args.size()) {
            case 3:
weinig's avatar
weinig committed
195
                context->drawImage(imgElt, args[1]->toFloat(exec), args[2]->toFloat(exec));
196 197
                break;
            case 5:
weinig's avatar
weinig committed
198 199
                context->drawImage(imgElt, args[1]->toFloat(exec), args[2]->toFloat(exec),
                                   args[3]->toFloat(exec), args[4]->toFloat(exec), ec);
200 201 202
                setDOMException(exec, ec);
                break;
            case 9:
weinig's avatar
weinig committed
203 204 205 206
                context->drawImage(imgElt, FloatRect(args[1]->toFloat(exec), args[2]->toFloat(exec),
                                   args[3]->toFloat(exec), args[4]->toFloat(exec)),
                                   FloatRect(args[5]->toFloat(exec), args[6]->toFloat(exec),
                                   args[7]->toFloat(exec), args[8]->toFloat(exec)), ec);
207 208 209 210 211
                setDOMException(exec, ec);
                break;
            default:
                return throwError(exec, SyntaxError);
        }
212
    } else if (o->inherits(&JSHTMLCanvasElement::s_info)) {
213 214 215
        HTMLCanvasElement* canvas = static_cast<HTMLCanvasElement*>(static_cast<JSHTMLElement*>(args[0])->impl());
        switch (args.size()) {
            case 3:
weinig's avatar
weinig committed
216
                context->drawImage(canvas, args[1]->toFloat(exec), args[2]->toFloat(exec));
217 218
                break;
            case 5:
weinig's avatar
weinig committed
219 220
                context->drawImage(canvas, args[1]->toFloat(exec), args[2]->toFloat(exec),
                                   args[3]->toFloat(exec), args[4]->toFloat(exec), ec);
221 222 223
                setDOMException(exec, ec);
                break;
            case 9:
weinig's avatar
weinig committed
224 225 226 227
                context->drawImage(canvas, FloatRect(args[1]->toFloat(exec), args[2]->toFloat(exec),
                                   args[3]->toFloat(exec), args[4]->toFloat(exec)),
                                   FloatRect(args[5]->toFloat(exec), args[6]->toFloat(exec),
                                   args[7]->toFloat(exec), args[8]->toFloat(exec)), ec);
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
                setDOMException(exec, ec);
                break;
            default:
                return throwError(exec, SyntaxError);
        }
    } else {
        setDOMException(exec, TYPE_MISMATCH_ERR);
        return 0;
    }
    
    return jsUndefined();    
}

JSValue* JSCanvasRenderingContext2D::drawImageFromRect(ExecState* exec, const List& args)
{ 
    CanvasRenderingContext2D* context = impl();
244 245 246
    
    JSValue* value = args[0];
    if (!value->isObject())
247
        return throwError(exec, TypeError);
248 249
    JSObject* o = static_cast<JSObject*>(value);
    
250
    if (!o->inherits(&JSHTMLImageElement::s_info))
251 252
        return throwError(exec, TypeError);
    context->drawImageFromRect(static_cast<HTMLImageElement*>(static_cast<JSHTMLElement*>(args[0])->impl()),
weinig's avatar
weinig committed
253 254 255 256
                               args[1]->toFloat(exec), args[2]->toFloat(exec),
                               args[3]->toFloat(exec), args[4]->toFloat(exec),
                               args[5]->toFloat(exec), args[6]->toFloat(exec),
                               args[7]->toFloat(exec), args[8]->toFloat(exec),
257 258 259 260 261 262 263 264 265 266
                               args[9]->toString(exec));    
    return jsUndefined();    
}

JSValue* JSCanvasRenderingContext2D::setShadow(ExecState* exec, const List& args)
{ 
    CanvasRenderingContext2D* context = impl();

    switch (args.size()) {
        case 3:
weinig's avatar
weinig committed
267 268
            context->setShadow(args[0]->toFloat(exec), args[1]->toFloat(exec),
                               args[2]->toFloat(exec));
269 270 271
            break;
        case 4:
            if (args[3]->isString())
weinig's avatar
weinig committed
272 273
                context->setShadow(args[0]->toFloat(exec), args[1]->toFloat(exec),
                                   args[2]->toFloat(exec), args[3]->toString(exec));
274
            else
weinig's avatar
weinig committed
275 276
                context->setShadow(args[0]->toFloat(exec), args[1]->toFloat(exec),
                                   args[2]->toFloat(exec), args[3]->toFloat(exec));
277 278 279
            break;
        case 5:
            if (args[3]->isString())
weinig's avatar
weinig committed
280 281 282
                context->setShadow(args[0]->toFloat(exec), args[1]->toFloat(exec),
                                   args[2]->toFloat(exec), args[3]->toString(exec),
                                   args[4]->toFloat(exec));
283
            else
weinig's avatar
weinig committed
284 285 286
                context->setShadow(args[0]->toFloat(exec), args[1]->toFloat(exec),
                                   args[2]->toFloat(exec), args[3]->toFloat(exec),
                                   args[4]->toFloat(exec));
287 288
            break;
        case 7:
weinig's avatar
weinig committed
289 290 291 292
            context->setShadow(args[0]->toFloat(exec), args[1]->toFloat(exec),
                               args[2]->toFloat(exec), args[3]->toFloat(exec),
                               args[4]->toFloat(exec), args[5]->toFloat(exec),
                               args[6]->toFloat(exec));
293 294
            break;
        case 8:
weinig's avatar
weinig committed
295 296 297 298
            context->setShadow(args[0]->toFloat(exec), args[1]->toFloat(exec),
                               args[2]->toFloat(exec), args[3]->toFloat(exec),
                               args[4]->toFloat(exec), args[5]->toFloat(exec),
                               args[6]->toFloat(exec), args[7]->toFloat(exec));
299 300 301 302 303 304 305 306 307 308 309 310
            break;
        default:
            return throwError(exec, SyntaxError);
    }
    
    return jsUndefined();    
}

JSValue* JSCanvasRenderingContext2D::createPattern(ExecState* exec, const List& args)
{ 
    CanvasRenderingContext2D* context = impl();

311 312
    JSValue* value = args[0];
    if (!value->isObject())
313
        return throwError(exec, TypeError);
314
    JSObject* o = static_cast<JSObject*>(value);
315

316
    if (o->inherits(&JSHTMLImageElement::s_info)) {
317 318 319
        ExceptionCode ec;
        JSValue* pattern = toJS(exec,
            context->createPattern(static_cast<HTMLImageElement*>(static_cast<JSHTMLElement*>(args[0])->impl()),
320
                                   valueToStringWithNullCheck(exec, args[1]), ec).get());
321 322 323
        setDOMException(exec, ec);
        return pattern;
    }
324
    if (o->inherits(&JSHTMLCanvasElement::s_info)) {
325 326 327
        ExceptionCode ec;
        JSValue* pattern = toJS(exec,
            context->createPattern(static_cast<HTMLCanvasElement*>(static_cast<JSHTMLElement*>(args[0])->impl()),
328
                valueToStringWithNullCheck(exec, args[1]), ec).get());
329 330 331 332 333 334 335
        setDOMException(exec, ec);
        return pattern;
    }
    setDOMException(exec, TYPE_MISMATCH_ERR);
    return 0;
}

336 337
JSValue* JSCanvasRenderingContext2D::putImageData(ExecState* exec, const List& args)
{
338 339 340 341 342 343 344 345 346 347 348 349 350
    // putImageData has two variants
    // putImageData(ImageData, x, y)
    // putImageData(ImageData, x, y, dirtyX, dirtyY, dirtyWidth, dirtyHeight)
    CanvasRenderingContext2D* context = impl();

    ExceptionCode ec = 0;
    if (args.size() >= 7)
        context->putImageData(toImageData(args[0]), args[1]->toFloat(exec), args[2]->toFloat(exec), 
                              args[3]->toFloat(exec), args[4]->toFloat(exec), args[5]->toFloat(exec), args[6]->toFloat(exec), ec);
    else
        context->putImageData(toImageData(args[0]), args[1]->toFloat(exec), args[2]->toFloat(exec), ec);

    setDOMException(exec, ec);
351 352 353
    return jsUndefined();
}

weinig's avatar
weinig committed
354
} // namespace WebCore