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 "bindings/core/v8/V8CanvasRenderingContext2D.h" 33 34 #include "bindings/core/v8/V8CanvasGradient.h" 35 #include "bindings/core/v8/V8CanvasPattern.h" 36 #include "bindings/core/v8/V8HTMLCanvasElement.h" 37 #include "bindings/core/v8/V8HTMLImageElement.h" 38 #include "bindings/core/v8/V8HTMLVideoElement.h" 39 #include "bindings/core/v8/V8ImageData.h" 40 #include "bindings/v8/V8Binding.h" 41 #include "core/html/canvas/CanvasGradient.h" 42 #include "core/html/canvas/CanvasPattern.h" 43 #include "core/html/canvas/CanvasRenderingContext2D.h" 44 #include "core/html/canvas/CanvasStyle.h" 45 #include "platform/geometry/FloatRect.h" 46 47 namespace WebCore { 48 49 static v8::Handle<v8::Value> toV8Object(CanvasStyle* style, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate) 50 { 51 if (style->canvasGradient()) 52 return toV8(style->canvasGradient(), creationContext, isolate); 53 54 if (style->canvasPattern()) 55 return toV8(style->canvasPattern(), creationContext, isolate); 56 57 return v8String(isolate, style->color()); 58 } 59 60 static PassRefPtr<CanvasStyle> toCanvasStyle(v8::Handle<v8::Value> value, v8::Isolate* isolate) 61 { 62 RefPtr<CanvasStyle> canvasStyle = CanvasStyle::createFromGradient(V8CanvasGradient::toNativeWithTypeCheck(isolate, value)); 63 if (canvasStyle) 64 return canvasStyle; 65 return CanvasStyle::createFromPattern(V8CanvasPattern::toNativeWithTypeCheck(isolate, value)); 66 } 67 68 void V8CanvasRenderingContext2D::strokeStyleAttributeGetterCustom(const v8::PropertyCallbackInfo<v8::Value>& info) 69 { 70 CanvasRenderingContext2D* impl = V8CanvasRenderingContext2D::toNative(info.Holder()); 71 v8SetReturnValue(info, toV8Object(impl->strokeStyle(), info.Holder(), info.GetIsolate())); 72 } 73 74 void V8CanvasRenderingContext2D::strokeStyleAttributeSetterCustom(v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void>& info) 75 { 76 CanvasRenderingContext2D* impl = V8CanvasRenderingContext2D::toNative(info.Holder()); 77 if (RefPtr<CanvasStyle> canvasStyle = toCanvasStyle(value, info.GetIsolate())) { 78 impl->setStrokeStyle(canvasStyle); 79 } else { 80 TOSTRING_VOID(V8StringResource<>, colorString, value); 81 impl->setStrokeColor(colorString); 82 } 83 } 84 85 void V8CanvasRenderingContext2D::fillStyleAttributeGetterCustom(const v8::PropertyCallbackInfo<v8::Value>& info) 86 { 87 CanvasRenderingContext2D* impl = V8CanvasRenderingContext2D::toNative(info.Holder()); 88 v8SetReturnValue(info, toV8Object(impl->fillStyle(), info.Holder(), info.GetIsolate())); 89 } 90 91 void V8CanvasRenderingContext2D::fillStyleAttributeSetterCustom(v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void>& info) 92 { 93 CanvasRenderingContext2D* impl = V8CanvasRenderingContext2D::toNative(info.Holder()); 94 if (RefPtr<CanvasStyle> canvasStyle = toCanvasStyle(value, info.GetIsolate())) { 95 impl->setFillStyle(canvasStyle); 96 } else { 97 TOSTRING_VOID(V8StringResource<>, colorString, value); 98 impl->setFillColor(colorString); 99 } 100 } 101 102 } // namespace WebCore 103