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 "HTMLCanvasElement.h" 29#include "HTMLImageElement.h" 30#include "ImageData.h" 31#include "JSCanvasGradient.h" 32#include "JSCanvasPattern.h" 33#include "JSHTMLCanvasElement.h" 34#include "JSHTMLImageElement.h" 35#include "JSImageData.h" 36 37using namespace JSC; 38 39namespace WebCore { 40 41static JSValue toJS(ExecState* exec, JSDOMGlobalObject* globalObject, const CanvasStyle& style) 42{ 43 if (style.canvasGradient()) 44 return toJS(exec, globalObject, style.canvasGradient()); 45 if (style.canvasPattern()) 46 return toJS(exec, globalObject, style.canvasPattern()); 47 return jsStringWithCache(exec, style.color()); 48} 49 50static CanvasStyle toHTMLCanvasStyle(ExecState*, JSValue value) 51{ 52 if (!value.isObject()) 53 return CanvasStyle(); 54 JSObject* object = asObject(value); 55 if (object->inherits(JSCanvasGradient::info())) 56 return CanvasStyle(&jsCast<JSCanvasGradient*>(object)->impl()); 57 if (object->inherits(JSCanvasPattern::info())) 58 return CanvasStyle(&jsCast<JSCanvasPattern*>(object)->impl()); 59 return CanvasStyle(); 60} 61 62JSValue JSCanvasRenderingContext2D::strokeStyle(ExecState* exec) const 63{ 64 return toJS(exec, globalObject(), impl().strokeStyle()); 65} 66 67void JSCanvasRenderingContext2D::setStrokeStyle(ExecState* exec, JSValue value) 68{ 69 CanvasRenderingContext2D& context = impl(); 70 if (value.isString()) { 71 context.setStrokeColor(asString(value)->value(exec)); 72 return; 73 } 74 context.setStrokeStyle(toHTMLCanvasStyle(exec, value)); 75} 76 77JSValue JSCanvasRenderingContext2D::fillStyle(ExecState* exec) const 78{ 79 return toJS(exec, globalObject(), impl().fillStyle()); 80} 81 82void JSCanvasRenderingContext2D::setFillStyle(ExecState* exec, JSValue value) 83{ 84 CanvasRenderingContext2D& context = impl(); 85 if (value.isString()) { 86 context.setFillColor(asString(value)->value(exec)); 87 return; 88 } 89 context.setFillStyle(toHTMLCanvasStyle(exec, value)); 90} 91 92JSValue JSCanvasRenderingContext2D::webkitLineDash(ExecState* exec) const 93{ 94 const Vector<float>& dash = impl().getLineDash(); 95 96 MarkedArgumentBuffer list; 97 Vector<float>::const_iterator end = dash.end(); 98 for (Vector<float>::const_iterator it = dash.begin(); it != end; ++it) 99 list.append(JSValue(*it)); 100 return constructArray(exec, 0, globalObject(), list); 101} 102 103void JSCanvasRenderingContext2D::setWebkitLineDash(ExecState* exec, JSValue value) 104{ 105 if (!isJSArray(value)) 106 return; 107 108 Vector<float> dash; 109 JSArray* valueArray = asArray(value); 110 for (unsigned i = 0; i < valueArray->length(); ++i) { 111 float elem = valueArray->getIndex(exec, i).toFloat(exec); 112 if (elem <= 0 || !std::isfinite(elem)) 113 return; 114 115 dash.append(elem); 116 } 117 118 impl().setWebkitLineDash(dash); 119} 120 121} // namespace WebCore 122