Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(118)

Unified Diff: experimental/SkV8Example/DrawingMethods.cpp

Issue 676423002: Start moving to the new canvas structure. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « experimental/SkV8Example/DrawingMethods.h ('k') | experimental/SkV8Example/JsContext.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: experimental/SkV8Example/DrawingMethods.cpp
diff --git a/experimental/SkV8Example/DrawingMethods.cpp b/experimental/SkV8Example/DrawingMethods.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..fdc8dc7d63bb07b03f5d9f8f944b04b5dde7c938
--- /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);
+}
« no previous file with comments | « experimental/SkV8Example/DrawingMethods.h ('k') | experimental/SkV8Example/JsContext.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698