Move WebCore into Source
[WebKit-https.git] / Source / WebCore / bindings / v8 / custom / V8CanvasRenderingContext2DCustom.cpp
1 /*
2  * Copyright (C) 2009 Google Inc. All rights reserved.
3  * 
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  * 
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  * 
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "V8CanvasRenderingContext2D.h"
33
34 #include "CanvasGradient.h"
35 #include "CanvasRenderingContext2D.h"
36 #include "CanvasPattern.h"
37 #include "CanvasStyle.h"
38 #include "ExceptionCode.h"
39 #include "FloatRect.h"
40
41 #include "V8Binding.h"
42 #include "V8CanvasGradient.h"
43 #include "V8CanvasPattern.h"
44 #include "V8HTMLCanvasElement.h"
45 #include "V8HTMLImageElement.h"
46 #include "V8HTMLVideoElement.h"
47 #include "V8ImageData.h"
48 #include "V8Proxy.h"
49
50 namespace WebCore {
51
52 static v8::Handle<v8::Value> toV8Object(CanvasStyle* style)
53 {
54     if (style->canvasGradient())
55         return toV8(style->canvasGradient());
56
57     if (style->canvasPattern())
58         return toV8(style->canvasPattern());
59
60     return v8String(style->color());
61 }
62
63 static PassRefPtr<CanvasStyle> toCanvasStyle(v8::Handle<v8::Value> value)
64 {
65     if (value->IsString())
66         return CanvasStyle::createFromString(toWebCoreString(value));
67
68     if (V8CanvasGradient::HasInstance(value))
69         return CanvasStyle::createFromGradient(V8CanvasGradient::toNative(v8::Handle<v8::Object>::Cast(value)));
70
71     if (V8CanvasPattern::HasInstance(value))
72         return CanvasStyle::createFromPattern(V8CanvasPattern::toNative(v8::Handle<v8::Object>::Cast(value)));
73
74     return 0;
75 }
76
77 v8::Handle<v8::Value> V8CanvasRenderingContext2D::strokeStyleAccessorGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
78 {
79     CanvasRenderingContext2D* impl = V8CanvasRenderingContext2D::toNative(info.Holder());
80     return toV8Object(impl->strokeStyle());
81 }
82
83 void V8CanvasRenderingContext2D::strokeStyleAccessorSetter(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
84 {
85     CanvasRenderingContext2D* impl = V8CanvasRenderingContext2D::toNative(info.Holder());
86     impl->setStrokeStyle(toCanvasStyle(value));
87 }
88
89 v8::Handle<v8::Value> V8CanvasRenderingContext2D::fillStyleAccessorGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
90 {
91     CanvasRenderingContext2D* impl = V8CanvasRenderingContext2D::toNative(info.Holder());
92     return toV8Object(impl->fillStyle());
93 }
94
95 void V8CanvasRenderingContext2D::fillStyleAccessorSetter(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
96 {
97     CanvasRenderingContext2D* impl = V8CanvasRenderingContext2D::toNative(info.Holder());
98     impl->setFillStyle(toCanvasStyle(value));
99 }
100
101 } // namespace WebCore