diff options
Diffstat (limited to 'experimental')
-rw-r--r-- | experimental/SkV8Example/BaseContext.cpp | 284 | ||||
-rw-r--r-- | experimental/SkV8Example/BaseContext.h | 82 | ||||
-rw-r--r-- | experimental/SkV8Example/DrawingMethods.cpp | 172 | ||||
-rw-r--r-- | experimental/SkV8Example/DrawingMethods.h | 57 | ||||
-rw-r--r-- | experimental/SkV8Example/JsContext.h | 6 | ||||
-rw-r--r-- | experimental/SkV8Example/js/sample.js | 9 |
6 files changed, 239 insertions, 371 deletions
diff --git a/experimental/SkV8Example/BaseContext.cpp b/experimental/SkV8Example/BaseContext.cpp deleted file mode 100644 index d617d78c18..0000000000 --- a/experimental/SkV8Example/BaseContext.cpp +++ /dev/null @@ -1,284 +0,0 @@ -/* - * Copyright 2014 Google Inc. - * - * - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - * - */ -#include <v8.h> - -#include "Global.h" -#include "BaseContext.h" -#include "Path2D.h" -#include "SkCanvas.h" - - -BaseContext* BaseContext::Unwrap(v8::Handle<v8::Object> obj) { - v8::Handle<v8::External> field = v8::Handle<v8::External>::Cast(obj->GetInternalField(0)); - void* ptr = field->Value(); - return static_cast<BaseContext*>(ptr); -} - -void BaseContext::FillRect(const v8::FunctionCallbackInfo<v8::Value>& args) { - BaseContext* BaseContext = Unwrap(args.This()); - SkCanvas* canvas = BaseContext->getCanvas(); - if (NULL == canvas) { - return; - } - - if (args.Length() != 4) { - args.GetIsolate()->ThrowException( - v8::String::NewFromUtf8( - args.GetIsolate(), "Error: 4 arguments required.")); - return; - } - double x = args[0]->NumberValue(); - double y = args[1]->NumberValue(); - double w = args[2]->NumberValue(); - double h = args[3]->NumberValue(); - - SkRect rect = { - SkDoubleToScalar(x), - SkDoubleToScalar(y), - SkDoubleToScalar(x) + SkDoubleToScalar(w), - SkDoubleToScalar(y) + SkDoubleToScalar(h) - }; - canvas->drawRect(rect, BaseContext->fFillStyle); -} - -void BaseContext::Save(const v8::FunctionCallbackInfo<v8::Value>& args) { - BaseContext* BaseContext = Unwrap(args.This()); - SkCanvas* canvas = BaseContext->getCanvas(); - if (NULL == canvas) { - return; - } - - canvas->save(); -} - -void BaseContext::Restore(const v8::FunctionCallbackInfo<v8::Value>& args) { - BaseContext* BaseContext = Unwrap(args.This()); - SkCanvas* canvas = BaseContext->getCanvas(); - if (NULL == canvas) { - return; - } - - canvas->restore(); -} - -void BaseContext::Rotate(const v8::FunctionCallbackInfo<v8::Value>& args) { - BaseContext* BaseContext = Unwrap(args.This()); - SkCanvas* canvas = BaseContext->getCanvas(); - if (NULL == canvas) { - return; - } - - if (args.Length() != 1) { - args.GetIsolate()->ThrowException( - v8::String::NewFromUtf8( - args.GetIsolate(), "Error: 1 arguments required.")); - return; - } - double angle = args[0]->NumberValue(); - canvas->rotate(SkRadiansToDegrees(angle)); -} - -void BaseContext::Translate(const v8::FunctionCallbackInfo<v8::Value>& args) { - BaseContext* BaseContext = Unwrap(args.This()); - SkCanvas* canvas = BaseContext->getCanvas(); - if (NULL == canvas) { - return; - } - - if (args.Length() != 2) { - args.GetIsolate()->ThrowException( - v8::String::NewFromUtf8( - args.GetIsolate(), "Error: 2 arguments required.")); - return; - } - double dx = args[0]->NumberValue(); - double dy = args[1]->NumberValue(); - canvas->translate(SkDoubleToScalar(dx), SkDoubleToScalar(dy)); -} - -void BaseContext::ResetTransform(const v8::FunctionCallbackInfo<v8::Value>& args) { - BaseContext* BaseContext = Unwrap(args.This()); - SkCanvas* canvas = BaseContext->getCanvas(); - if (NULL == canvas) { - return; - } - - canvas->resetMatrix(); -} - -void BaseContext::Stroke(const v8::FunctionCallbackInfo<v8::Value>& args) { - BaseContext* BaseContext = Unwrap(args.This()); - SkCanvas* canvas = BaseContext->getCanvas(); - if (NULL == canvas) { - return; - } - - if (args.Length() != 1) { - args.GetIsolate()->ThrowException( - v8::String::NewFromUtf8( - args.GetIsolate(), "Error: 1 arguments required.")); - return; - } - - v8::Handle<v8::External> field = v8::Handle<v8::External>::Cast( - args[0]->ToObject()->GetInternalField(0)); - void* ptr = field->Value(); - Path2D* path = static_cast<Path2D*>(ptr); - - canvas->drawPath(path->getSkPath(), BaseContext->fStrokeStyle); -} - -void BaseContext::Fill(const v8::FunctionCallbackInfo<v8::Value>& args) { - BaseContext* BaseContext = Unwrap(args.This()); - SkCanvas* canvas = BaseContext->getCanvas(); - if (NULL == canvas) { - return; - } - - if (args.Length() != 1) { - args.GetIsolate()->ThrowException( - v8::String::NewFromUtf8( - args.GetIsolate(), "Error: 1 arguments required.")); - return; - } - - v8::Handle<v8::External> field = v8::Handle<v8::External>::Cast( - args[0]->ToObject()->GetInternalField(0)); - void* ptr = field->Value(); - Path2D* path = static_cast<Path2D*>(ptr); - - canvas->drawPath(path->getSkPath(), BaseContext->fFillStyle); -} - -void BaseContext::GetStyle(v8::Local<v8::String> name, - const v8::PropertyCallbackInfo<v8::Value>& info, - const SkPaint& style) { - char buf[8]; - SkColor color = style.getColor(); - sprintf(buf, "#%02X%02X%02X", SkColorGetR(color), SkColorGetG(color), - SkColorGetB(color)); - - info.GetReturnValue().Set(v8::String::NewFromUtf8(info.GetIsolate(), buf)); -} - -void BaseContext::SetStyle(v8::Local<v8::String> name, v8::Local<v8::Value> value, - const v8::PropertyCallbackInfo<void>& info, - SkPaint& style) { - v8::Local<v8::String> s = value->ToString(); - if (s->Length() != 7 && s->Length() != 9) { - info.GetIsolate()->ThrowException( - v8::String::NewFromUtf8( - info.GetIsolate(), - "Invalid fill style format length.")); - return; - } - char buf[10]; - s->WriteUtf8(buf, sizeof(buf)); - - if (buf[0] != '#') { - info.GetIsolate()->ThrowException( - v8::String::NewFromUtf8( - info.GetIsolate(), "Invalid fill style format.")); - return; - } - - // Colors can be RRGGBBAA, but SkColor uses ARGB. - long color = strtol(buf+1, NULL, 16); - uint32_t alpha = SK_AlphaOPAQUE; - if (s->Length() == 9) { - alpha = color & 0xFF; - color >>= 8; - } - style.setColor(SkColorSetA(SkColor(color), alpha)); -} - -void BaseContext::GetFillStyle(v8::Local<v8::String> name, - const v8::PropertyCallbackInfo<v8::Value>& info) { - BaseContext* baseContext = Unwrap(info.This()); - GetStyle(name, info, baseContext->fFillStyle); -} - -void BaseContext::GetStrokeStyle(v8::Local<v8::String> name, - const v8::PropertyCallbackInfo<v8::Value>& info) { - BaseContext* baseContext = Unwrap(info.This()); - GetStyle(name, info, baseContext->fStrokeStyle); -} - -void BaseContext::SetFillStyle(v8::Local<v8::String> name, v8::Local<v8::Value> value, - const v8::PropertyCallbackInfo<void>& info) { - BaseContext* baseContext = Unwrap(info.This()); - SetStyle(name, value, info, baseContext->fFillStyle); -} - -void BaseContext::SetStrokeStyle(v8::Local<v8::String> name, v8::Local<v8::Value> value, - const v8::PropertyCallbackInfo<void>& info) { - BaseContext* baseContext = Unwrap(info.This()); - SetStyle(name, value, info, baseContext->fStrokeStyle); -} - - -void BaseContext::GetWidth(v8::Local<v8::String> name, - const v8::PropertyCallbackInfo<v8::Value>& info) { - BaseContext* baseContext = Unwrap(info.This()); - SkCanvas* canvas = baseContext->getCanvas(); - if (NULL == canvas) { - return; - } - SkISize size = canvas->getDeviceSize(); - - info.GetReturnValue().Set( - v8::Int32::New(baseContext->fGlobal->getIsolate(), size.fWidth)); -} - -void BaseContext::GetHeight(v8::Local<v8::String> name, - const v8::PropertyCallbackInfo<v8::Value>& info) { - BaseContext* baseContext = Unwrap(info.This()); - SkCanvas* canvas = baseContext->getCanvas(); - if (NULL == canvas) { - return; - } - SkISize size = canvas->getDeviceSize(); - - info.GetReturnValue().Set( - v8::Int32::New(baseContext->fGlobal->getIsolate(), size.fHeight)); -} - -#define ADD_METHOD(name, fn) \ - tmpl->Set(v8::String::NewFromUtf8( \ - fGlobal->getIsolate(), name, \ - v8::String::kInternalizedString), \ - v8::FunctionTemplate::New(fGlobal->getIsolate(), fn)) - -void BaseContext::addAttributesAndMethods(v8::Handle<v8::ObjectTemplate> tmpl) { - v8::HandleScope scope(fGlobal->getIsolate()); - - // Add accessors for each of the fields of the context object. - tmpl->SetAccessor(v8::String::NewFromUtf8( - fGlobal->getIsolate(), "fillStyle", v8::String::kInternalizedString), - GetFillStyle, SetFillStyle); - tmpl->SetAccessor(v8::String::NewFromUtf8( - fGlobal->getIsolate(), "strokeStyle", v8::String::kInternalizedString), - GetStrokeStyle, SetStrokeStyle); - tmpl->SetAccessor(v8::String::NewFromUtf8( - fGlobal->getIsolate(), "width", v8::String::kInternalizedString), - GetWidth); - tmpl->SetAccessor(v8::String::NewFromUtf8( - fGlobal->getIsolate(), "height", v8::String::kInternalizedString), - GetHeight); - - // Add methods. - ADD_METHOD("fillRect", FillRect); - ADD_METHOD("stroke", Stroke); - ADD_METHOD("fill", Fill); - ADD_METHOD("rotate", Rotate); - ADD_METHOD("save", Save); - ADD_METHOD("restore", Restore); - ADD_METHOD("translate", Translate); - ADD_METHOD("resetTransform", ResetTransform); -} diff --git a/experimental/SkV8Example/BaseContext.h b/experimental/SkV8Example/BaseContext.h deleted file mode 100644 index 5462c85751..0000000000 --- a/experimental/SkV8Example/BaseContext.h +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright 2014 Google Inc. - * - * - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - * - */ - -#ifndef SkV8Example_BaseContext_DEFINED -#define SkV8Example_BaseContext_DEFINED - -#include <v8.h> - -#include "SkPaint.h" - -class SkCanvas; -class Global; - -// BaseContext contains common functionality for both JsContext -// and DisplayList. -class BaseContext { -public: - BaseContext(Global* global) - : fGlobal(global) - { - fFillStyle.setColor(SK_ColorBLACK); - fFillStyle.setAntiAlias(true); - fFillStyle.setStyle(SkPaint::kFill_Style); - fStrokeStyle.setColor(SK_ColorBLACK); - fStrokeStyle.setAntiAlias(true); - fStrokeStyle.setStyle(SkPaint::kStroke_Style); - } - virtual ~BaseContext() {} - - // Retrieve the SkCanvas to draw on. May return NULL. - virtual SkCanvas* getCanvas() = 0; - - // Add the Javascript attributes and methods that BaseContext implements to the ObjectTemplate. - void addAttributesAndMethods(v8::Handle<v8::ObjectTemplate> tmpl); - -protected: - // Get the pointer out of obj. - static BaseContext* Unwrap(v8::Handle<v8::Object> obj); - - Global* fGlobal; - SkPaint fFillStyle; - SkPaint fStrokeStyle; - -private: - static void GetStyle(v8::Local<v8::String> name, - const v8::PropertyCallbackInfo<v8::Value>& info, - const SkPaint& style); - static void SetStyle(v8::Local<v8::String> name, v8::Local<v8::Value> value, - const v8::PropertyCallbackInfo<void>& info, - SkPaint& style); - // JS Attributes - static void GetFillStyle(v8::Local<v8::String> name, - const v8::PropertyCallbackInfo<v8::Value>& info); - static void SetFillStyle(v8::Local<v8::String> name, v8::Local<v8::Value> value, - const v8::PropertyCallbackInfo<void>& info); - static void GetStrokeStyle(v8::Local<v8::String> name, - const v8::PropertyCallbackInfo<v8::Value>& info); - static void SetStrokeStyle(v8::Local<v8::String> name, v8::Local<v8::Value> value, - const v8::PropertyCallbackInfo<void>& info); - static void GetWidth(v8::Local<v8::String> name, - const v8::PropertyCallbackInfo<v8::Value>& info); - static void GetHeight(v8::Local<v8::String> name, - const v8::PropertyCallbackInfo<v8::Value>& info); - - // JS Methods - static void FillRect(const v8::FunctionCallbackInfo<v8::Value>& args); - static void Stroke(const v8::FunctionCallbackInfo<v8::Value>& args); - static void Fill(const v8::FunctionCallbackInfo<v8::Value>& args); - static void Rotate(const v8::FunctionCallbackInfo<v8::Value>& args); - static void Save(const v8::FunctionCallbackInfo<v8::Value>& args); - static void Restore(const v8::FunctionCallbackInfo<v8::Value>& args); - static void Translate(const v8::FunctionCallbackInfo<v8::Value>& args); - static void ResetTransform(const v8::FunctionCallbackInfo<v8::Value>& args); -}; - -#endif diff --git a/experimental/SkV8Example/DrawingMethods.cpp b/experimental/SkV8Example/DrawingMethods.cpp new file mode 100644 index 0000000000..fdc8dc7d63 --- /dev/null +++ b/experimental/SkV8Example/DrawingMethods.cpp @@ -0,0 +1,172 @@ +/* + * Copyright 2014 Google Inc. + * + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + * + */ +#include <v8.h> + +#include "Global.h" +#include "DrawingMethods.h" +#include "Path2D.h" +#include "SkCanvas.h" +#include "SkPaint.h" + + +DrawingMethods* DrawingMethods::Unwrap(v8::Handle<v8::Object> obj) { + v8::Handle<v8::External> field = v8::Handle<v8::External>::Cast(obj->GetInternalField(0)); + void* ptr = field->Value(); + return static_cast<DrawingMethods*>(ptr); +} + + +void DrawingMethods::Save(const v8::FunctionCallbackInfo<v8::Value>& args) { + DrawingMethods* drawingMethods = Unwrap(args.This()); + SkCanvas* canvas = drawingMethods->getCanvas(); + if (NULL == canvas) { + return; + } + + canvas->save(); +} + +void DrawingMethods::Restore(const v8::FunctionCallbackInfo<v8::Value>& args) { + DrawingMethods* drawingMethods = Unwrap(args.This()); + SkCanvas* canvas = drawingMethods->getCanvas(); + if (NULL == canvas) { + return; + } + + canvas->restore(); +} + +void DrawingMethods::Rotate(const v8::FunctionCallbackInfo<v8::Value>& args) { + DrawingMethods* drawingMethods = Unwrap(args.This()); + SkCanvas* canvas = drawingMethods->getCanvas(); + if (NULL == canvas) { + return; + } + + if (args.Length() != 1) { + args.GetIsolate()->ThrowException( + v8::String::NewFromUtf8( + args.GetIsolate(), "Error: 1 arguments required.")); + return; + } + double angle = args[0]->NumberValue(); + canvas->rotate(SkRadiansToDegrees(angle)); +} + +void DrawingMethods::Translate(const v8::FunctionCallbackInfo<v8::Value>& args) { + DrawingMethods* drawingMethods = Unwrap(args.This()); + SkCanvas* canvas = drawingMethods->getCanvas(); + if (NULL == canvas) { + return; + } + + if (args.Length() != 2) { + args.GetIsolate()->ThrowException( + v8::String::NewFromUtf8( + args.GetIsolate(), "Error: 2 arguments required.")); + return; + } + double dx = args[0]->NumberValue(); + double dy = args[1]->NumberValue(); + canvas->translate(SkDoubleToScalar(dx), SkDoubleToScalar(dy)); +} + +void DrawingMethods::ResetTransform(const v8::FunctionCallbackInfo<v8::Value>& args) { + DrawingMethods* drawingMethods = Unwrap(args.This()); + SkCanvas* canvas = drawingMethods->getCanvas(); + if (NULL == canvas) { + return; + } + + canvas->resetMatrix(); +} + +void DrawingMethods::DrawPath(const v8::FunctionCallbackInfo<v8::Value>& args) { + DrawingMethods* drawingMethods = Unwrap(args.This()); + SkCanvas* canvas = drawingMethods->getCanvas(); + if (NULL == canvas) { + return; + } + + if (args.Length() != 1) { + args.GetIsolate()->ThrowException( + v8::String::NewFromUtf8( + args.GetIsolate(), "Error: 1 argument required.")); + return; + } + + v8::Handle<v8::External> field = v8::Handle<v8::External>::Cast( + args[0]->ToObject()->GetInternalField(0)); + void* ptr = field->Value(); + Path2D* path = static_cast<Path2D*>(ptr); + if (NULL == path) { + return; + } + // TODO(jcgregorio) Add support for Paint2D parameter after Paint2D is + // implemented. + SkPaint fillStyle; + fillStyle.setColor(SK_ColorBLACK); + fillStyle.setAntiAlias(true); + fillStyle.setStyle(SkPaint::kFill_Style); + canvas->drawPath(path->getSkPath(), fillStyle); +} + + +void DrawingMethods::GetWidth(v8::Local<v8::String> name, + const v8::PropertyCallbackInfo<v8::Value>& info) { + DrawingMethods* drawingMethods = Unwrap(info.This()); + SkCanvas* canvas = drawingMethods->getCanvas(); + if (NULL == canvas) { + return; + } + + info.GetReturnValue().Set( + v8::Int32::New( + drawingMethods->fGlobal->getIsolate(), canvas->imageInfo().width())); +} + +void DrawingMethods::GetHeight(v8::Local<v8::String> name, + const v8::PropertyCallbackInfo<v8::Value>& info) { + DrawingMethods* drawingMethods = Unwrap(info.This()); + SkCanvas* canvas = drawingMethods->getCanvas(); + if (NULL == canvas) { + return; + } + + info.GetReturnValue().Set( + v8::Int32::New( + drawingMethods->fGlobal->getIsolate(), canvas->imageInfo().height())); +} + +#define ADD_METHOD(name, fn) \ + tmpl->Set(v8::String::NewFromUtf8( \ + fGlobal->getIsolate(), name, \ + v8::String::kInternalizedString), \ + v8::FunctionTemplate::New(fGlobal->getIsolate(), fn)) + +void DrawingMethods::addAttributesAndMethods(v8::Handle<v8::ObjectTemplate> tmpl) { + v8::HandleScope scope(fGlobal->getIsolate()); + + // Add accessors for each of the fields of the context object. + tmpl->SetAccessor(v8::String::NewFromUtf8( + fGlobal->getIsolate(), "width", v8::String::kInternalizedString), + GetWidth); + tmpl->SetAccessor(v8::String::NewFromUtf8( + fGlobal->getIsolate(), "height", v8::String::kInternalizedString), + GetHeight); + + // Add methods. + ADD_METHOD("save", Save); + ADD_METHOD("restore", Restore); + ADD_METHOD("rotate", Rotate); + ADD_METHOD("translate", Translate); + ADD_METHOD("resetTransform", ResetTransform); + + ADD_METHOD("drawPath", DrawPath); +} diff --git a/experimental/SkV8Example/DrawingMethods.h b/experimental/SkV8Example/DrawingMethods.h new file mode 100644 index 0000000000..d72a3df018 --- /dev/null +++ b/experimental/SkV8Example/DrawingMethods.h @@ -0,0 +1,57 @@ +/* + * Copyright 2014 Google Inc. + * + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + * + */ + +#ifndef SkV8Example_DrawingMethods_DEFINED +#define SkV8Example_DrawingMethods_DEFINED + +#include <v8.h> + +class SkCanvas; +class Global; + +// DrawingMethods contains common functionality for both Context, Image2Builder, +// and DisplayListBuiler. +class DrawingMethods { +public: + DrawingMethods(Global* global) + : fGlobal(global) + {} + virtual ~DrawingMethods() {} + + // Retrieve the SkCanvas to draw on. May return NULL. + virtual SkCanvas* getCanvas() = 0; + + // Add the Javascript attributes and methods that DrawingMethods + // implements to the ObjectTemplate. + void addAttributesAndMethods(v8::Handle<v8::ObjectTemplate> tmpl); + +protected: + // Get the pointer out of obj. + static DrawingMethods* Unwrap(v8::Handle<v8::Object> obj); + + Global* fGlobal; + +private: + // JS Attributes + static void GetWidth(v8::Local<v8::String> name, + const v8::PropertyCallbackInfo<v8::Value>& info); + static void GetHeight(v8::Local<v8::String> name, + const v8::PropertyCallbackInfo<v8::Value>& info); + + // JS Methods + static void Save(const v8::FunctionCallbackInfo<v8::Value>& args); + static void Restore(const v8::FunctionCallbackInfo<v8::Value>& args); + static void Rotate(const v8::FunctionCallbackInfo<v8::Value>& args); + static void Translate(const v8::FunctionCallbackInfo<v8::Value>& args); + static void ResetTransform(const v8::FunctionCallbackInfo<v8::Value>& args); + + static void DrawPath(const v8::FunctionCallbackInfo<v8::Value>& args); +}; + +#endif diff --git a/experimental/SkV8Example/JsContext.h b/experimental/SkV8Example/JsContext.h index 60446f887f..a6b5e4cae5 100644 --- a/experimental/SkV8Example/JsContext.h +++ b/experimental/SkV8Example/JsContext.h @@ -13,7 +13,7 @@ #include <v8.h> #include "SkPaint.h" -#include "BaseContext.h" +#include "DrawingMethods.h" class SkCanvas; class Global; @@ -25,7 +25,7 @@ class Global; // context.fillStyle="#FF0000"; // context.fillRect(x, y, w, h); // } -class JsContext : public BaseContext { +class JsContext : public DrawingMethods { public: JsContext(Global* global) : INHERITED(global) @@ -57,7 +57,7 @@ private: // Only valid when inside OnDraw(). SkCanvas* fCanvas; - typedef BaseContext INHERITED; + typedef DrawingMethods INHERITED; }; #endif diff --git a/experimental/SkV8Example/js/sample.js b/experimental/SkV8Example/js/sample.js index 81c717c7e8..7e7e40a918 100644 --- a/experimental/SkV8Example/js/sample.js +++ b/experimental/SkV8Example/js/sample.js @@ -3,10 +3,15 @@ */ var onDraw = function(){ var tick = 0; + var p = new Path2D(); + p.rect(0, 0, 200, 200); + function f(context) { tick += 0.1; - context.fillStyle = '#0000ff'; - context.fillRect(100, 100, Math.sin(tick)*100, Math.cos(tick)*100); + + context.translate(context.width/2, context.height/2); + context.rotate(tick); + context.drawPath(p); }; return f; }(); |