2014-01-06 18:17:24 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2013 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>
|
|
|
|
|
|
|
|
using namespace v8;
|
|
|
|
|
|
|
|
#include "Global.h"
|
|
|
|
#include "JsContext.h"
|
2014-02-27 20:20:21 +00:00
|
|
|
#include "Path2D.h"
|
2014-01-06 18:17:24 +00:00
|
|
|
#include "SkCanvas.h"
|
|
|
|
|
|
|
|
|
|
|
|
// Extracts a C string from a V8 Utf8Value.
|
|
|
|
// TODO(jcgregrio) Currently dup'd in two files, fix.
|
|
|
|
static const char* to_cstring(const v8::String::Utf8Value& value) {
|
|
|
|
return *value ? *value : "<string conversion failed>";
|
|
|
|
}
|
|
|
|
|
|
|
|
JsContext* JsContext::Unwrap(Handle<Object> obj) {
|
|
|
|
Handle<External> field = Handle<External>::Cast(obj->GetInternalField(0));
|
|
|
|
void* ptr = field->Value();
|
|
|
|
return static_cast<JsContext*>(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsContext::FillRect(const v8::FunctionCallbackInfo<Value>& args) {
|
|
|
|
JsContext* jsContext = Unwrap(args.This());
|
|
|
|
SkCanvas* canvas = jsContext->fCanvas;
|
|
|
|
|
|
|
|
if (args.Length() != 4) {
|
|
|
|
args.GetIsolate()->ThrowException(
|
|
|
|
v8::String::NewFromUtf8(
|
|
|
|
args.GetIsolate(), "Error: 4 arguments required."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// TODO(jcgregorio) Really figure out the conversion from JS numbers to
|
|
|
|
// SkScalars. Maybe test if int first? Not sure of the performance impact.
|
|
|
|
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, jsContext->fFillStyle);
|
|
|
|
}
|
|
|
|
|
2014-01-07 19:00:27 +00:00
|
|
|
void JsContext::Save(const v8::FunctionCallbackInfo<Value>& args) {
|
|
|
|
JsContext* jsContext = Unwrap(args.This());
|
|
|
|
SkCanvas* canvas = jsContext->fCanvas;
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsContext::Restore(const v8::FunctionCallbackInfo<Value>& args) {
|
|
|
|
JsContext* jsContext = Unwrap(args.This());
|
|
|
|
SkCanvas* canvas = jsContext->fCanvas;
|
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsContext::Rotate(const v8::FunctionCallbackInfo<Value>& args) {
|
|
|
|
JsContext* jsContext = Unwrap(args.This());
|
|
|
|
SkCanvas* canvas = jsContext->fCanvas;
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2014-01-06 18:17:24 +00:00
|
|
|
void JsContext::Translate(const v8::FunctionCallbackInfo<Value>& args) {
|
|
|
|
JsContext* jsContext = Unwrap(args.This());
|
|
|
|
SkCanvas* canvas = jsContext->fCanvas;
|
|
|
|
|
|
|
|
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 JsContext::ResetTransform(const v8::FunctionCallbackInfo<Value>& args) {
|
|
|
|
JsContext* jsContext = Unwrap(args.This());
|
|
|
|
SkCanvas* canvas = jsContext->fCanvas;
|
|
|
|
|
|
|
|
canvas->resetMatrix();
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsContext::Stroke(const v8::FunctionCallbackInfo<Value>& args) {
|
|
|
|
JsContext* jsContext = Unwrap(args.This());
|
|
|
|
SkCanvas* canvas = jsContext->fCanvas;
|
|
|
|
|
|
|
|
if (args.Length() != 1) {
|
|
|
|
args.GetIsolate()->ThrowException(
|
|
|
|
v8::String::NewFromUtf8(
|
|
|
|
args.GetIsolate(), "Error: 1 arguments required."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle<External> field = Handle<External>::Cast(
|
|
|
|
args[0]->ToObject()->GetInternalField(0));
|
|
|
|
void* ptr = field->Value();
|
2014-02-27 20:20:21 +00:00
|
|
|
Path2D* path = static_cast<Path2D*>(ptr);
|
2014-01-06 18:17:24 +00:00
|
|
|
|
2014-01-07 19:00:27 +00:00
|
|
|
canvas->drawPath(path->getSkPath(), jsContext->fStrokeStyle);
|
2014-01-06 18:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void JsContext::Fill(const v8::FunctionCallbackInfo<Value>& args) {
|
|
|
|
JsContext* jsContext = Unwrap(args.This());
|
|
|
|
SkCanvas* canvas = jsContext->fCanvas;
|
|
|
|
|
|
|
|
if (args.Length() != 1) {
|
|
|
|
args.GetIsolate()->ThrowException(
|
|
|
|
v8::String::NewFromUtf8(
|
|
|
|
args.GetIsolate(), "Error: 1 arguments required."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle<External> field = Handle<External>::Cast(
|
|
|
|
args[0]->ToObject()->GetInternalField(0));
|
|
|
|
void* ptr = field->Value();
|
2014-02-27 20:20:21 +00:00
|
|
|
Path2D* path = static_cast<Path2D*>(ptr);
|
2014-01-06 18:17:24 +00:00
|
|
|
|
|
|
|
canvas->drawPath(path->getSkPath(), jsContext->fFillStyle);
|
|
|
|
}
|
|
|
|
|
2014-01-07 19:00:27 +00:00
|
|
|
void JsContext::GetStyle(Local<String> name,
|
|
|
|
const PropertyCallbackInfo<Value>& info,
|
|
|
|
const SkPaint& style) {
|
2014-01-06 18:17:24 +00:00
|
|
|
char buf[8];
|
2014-01-07 19:00:27 +00:00
|
|
|
SkColor color = style.getColor();
|
2014-01-06 18:17:24 +00:00
|
|
|
sprintf(buf, "#%02X%02X%02X", SkColorGetR(color), SkColorGetG(color),
|
|
|
|
SkColorGetB(color));
|
|
|
|
|
|
|
|
info.GetReturnValue().Set(String::NewFromUtf8(info.GetIsolate(), buf));
|
|
|
|
}
|
|
|
|
|
2014-01-07 19:00:27 +00:00
|
|
|
void JsContext::SetStyle(Local<String> name, Local<Value> value,
|
|
|
|
const PropertyCallbackInfo<void>& info,
|
|
|
|
SkPaint& style) {
|
2014-01-06 18:17:24 +00:00
|
|
|
Local<String> s = value->ToString();
|
2014-01-13 18:28:21 +00:00
|
|
|
if (s->Length() != 7 && s->Length() != 9) {
|
2014-01-06 18:17:24 +00:00
|
|
|
info.GetIsolate()->ThrowException(
|
|
|
|
v8::String::NewFromUtf8(
|
2014-01-13 18:28:21 +00:00
|
|
|
info.GetIsolate(),
|
|
|
|
"Invalid fill style format length."));
|
2014-01-06 18:17:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-01-13 18:28:21 +00:00
|
|
|
char buf[10];
|
2014-01-06 18:17:24 +00:00
|
|
|
s->WriteUtf8(buf, sizeof(buf));
|
|
|
|
|
|
|
|
if (buf[0] != '#') {
|
|
|
|
info.GetIsolate()->ThrowException(
|
|
|
|
v8::String::NewFromUtf8(
|
|
|
|
info.GetIsolate(), "Invalid fill style format."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-13 18:28:21 +00:00
|
|
|
// Colors can be RRGGBBAA, but SkColor uses ARGB.
|
2014-01-06 18:17:24 +00:00
|
|
|
long color = strtol(buf+1, NULL, 16);
|
2014-01-13 18:28:21 +00:00
|
|
|
uint32_t alpha = SK_AlphaOPAQUE;
|
|
|
|
if (s->Length() == 9) {
|
|
|
|
alpha = color & 0xFF;
|
|
|
|
color >>= 8;
|
|
|
|
}
|
|
|
|
style.setColor(SkColorSetA(SkColor(color), alpha));
|
2014-01-07 19:00:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void JsContext::GetFillStyle(Local<String> name,
|
|
|
|
const PropertyCallbackInfo<Value>& info) {
|
|
|
|
JsContext* jsContext = Unwrap(info.This());
|
|
|
|
GetStyle(name, info, jsContext->fFillStyle);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsContext::GetStrokeStyle(Local<String> name,
|
|
|
|
const PropertyCallbackInfo<Value>& info) {
|
|
|
|
JsContext* jsContext = Unwrap(info.This());
|
|
|
|
GetStyle(name, info, jsContext->fStrokeStyle);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsContext::SetFillStyle(Local<String> name, Local<Value> value,
|
|
|
|
const PropertyCallbackInfo<void>& info) {
|
|
|
|
JsContext* jsContext = Unwrap(info.This());
|
|
|
|
SetStyle(name, value, info, jsContext->fFillStyle);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsContext::SetStrokeStyle(Local<String> name, Local<Value> value,
|
|
|
|
const PropertyCallbackInfo<void>& info) {
|
|
|
|
JsContext* jsContext = Unwrap(info.This());
|
|
|
|
SetStyle(name, value, info, jsContext->fStrokeStyle);
|
2014-01-06 18:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void JsContext::GetWidth(Local<String> name,
|
|
|
|
const PropertyCallbackInfo<Value>& info) {
|
|
|
|
JsContext* jsContext = Unwrap(info.This());
|
|
|
|
SkISize size = jsContext->fCanvas->getDeviceSize();
|
|
|
|
|
2014-01-08 15:14:09 +00:00
|
|
|
info.GetReturnValue().Set(
|
|
|
|
Int32::New(jsContext->fGlobal->getIsolate(), size.fWidth));
|
2014-01-06 18:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void JsContext::GetHeight(Local<String> name,
|
|
|
|
const PropertyCallbackInfo<Value>& info) {
|
|
|
|
JsContext* jsContext = Unwrap(info.This());
|
|
|
|
SkISize size = jsContext->fCanvas->getDeviceSize();
|
|
|
|
|
2014-01-08 15:14:09 +00:00
|
|
|
info.GetReturnValue().Set(
|
|
|
|
Int32::New(jsContext->fGlobal->getIsolate(), size.fHeight));
|
2014-01-06 18:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Persistent<ObjectTemplate> JsContext::gContextTemplate;
|
|
|
|
|
2014-01-07 19:00:27 +00:00
|
|
|
#define ADD_METHOD(name, fn) \
|
|
|
|
result->Set(String::NewFromUtf8( \
|
|
|
|
fGlobal->getIsolate(), name, \
|
|
|
|
String::kInternalizedString), \
|
2014-01-08 15:14:09 +00:00
|
|
|
FunctionTemplate::New(fGlobal->getIsolate(), fn))
|
2014-01-07 19:00:27 +00:00
|
|
|
|
2014-01-06 18:17:24 +00:00
|
|
|
Handle<ObjectTemplate> JsContext::makeContextTemplate() {
|
|
|
|
EscapableHandleScope handleScope(fGlobal->getIsolate());
|
|
|
|
|
|
|
|
Local<ObjectTemplate> result = ObjectTemplate::New();
|
|
|
|
|
|
|
|
// Add a field to store the pointer to a JsContext instance.
|
|
|
|
result->SetInternalFieldCount(1);
|
|
|
|
|
|
|
|
// Add accessors for each of the fields of the context object.
|
|
|
|
result->SetAccessor(String::NewFromUtf8(
|
|
|
|
fGlobal->getIsolate(), "fillStyle", String::kInternalizedString),
|
|
|
|
GetFillStyle, SetFillStyle);
|
2014-01-07 19:00:27 +00:00
|
|
|
result->SetAccessor(String::NewFromUtf8(
|
|
|
|
fGlobal->getIsolate(), "strokeStyle", String::kInternalizedString),
|
|
|
|
GetStrokeStyle, SetStrokeStyle);
|
2014-01-06 18:17:24 +00:00
|
|
|
result->SetAccessor(String::NewFromUtf8(
|
|
|
|
fGlobal->getIsolate(), "width", String::kInternalizedString),
|
|
|
|
GetWidth);
|
|
|
|
result->SetAccessor(String::NewFromUtf8(
|
|
|
|
fGlobal->getIsolate(), "height", String::kInternalizedString),
|
|
|
|
GetHeight);
|
|
|
|
|
|
|
|
// Add methods.
|
2014-01-07 19:00:27 +00:00
|
|
|
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);
|
2014-01-06 18:17:24 +00:00
|
|
|
|
|
|
|
// Return the result through the current handle scope.
|
|
|
|
return handleScope.Escape(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Wraps 'this' in a Javascript object.
|
|
|
|
Handle<Object> JsContext::wrap() {
|
|
|
|
// Handle scope for temporary handles.
|
|
|
|
EscapableHandleScope handleScope(fGlobal->getIsolate());
|
|
|
|
|
|
|
|
// Fetch the template for creating JavaScript JsContext wrappers.
|
|
|
|
// It only has to be created once, which we do on demand.
|
|
|
|
if (gContextTemplate.IsEmpty()) {
|
|
|
|
Handle<ObjectTemplate> raw_template = this->makeContextTemplate();
|
|
|
|
gContextTemplate.Reset(fGlobal->getIsolate(), raw_template);
|
|
|
|
}
|
|
|
|
Handle<ObjectTemplate> templ =
|
|
|
|
Local<ObjectTemplate>::New(fGlobal->getIsolate(), gContextTemplate);
|
|
|
|
|
|
|
|
// Create an empty JsContext wrapper.
|
|
|
|
Local<Object> result = templ->NewInstance();
|
|
|
|
|
|
|
|
// Wrap the raw C++ pointer in an External so it can be referenced
|
|
|
|
// from within JavaScript.
|
|
|
|
Handle<External> contextPtr = External::New(fGlobal->getIsolate(), this);
|
|
|
|
|
|
|
|
// Store the context pointer in the JavaScript wrapper.
|
|
|
|
result->SetInternalField(0, contextPtr);
|
|
|
|
|
|
|
|
// Return the result through the current handle scope. Since each
|
|
|
|
// of these handles will go away when the handle scope is deleted
|
|
|
|
// we need to call Close to let one, the result, escape into the
|
|
|
|
// outer handle scope.
|
|
|
|
return handleScope.Escape(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsContext::onDraw(SkCanvas* canvas) {
|
|
|
|
// Record canvas and window in this.
|
|
|
|
fCanvas = canvas;
|
|
|
|
|
|
|
|
// Create a handle scope to keep the temporary object references.
|
|
|
|
HandleScope handleScope(fGlobal->getIsolate());
|
|
|
|
|
|
|
|
// Create a local context from our global context.
|
|
|
|
Local<Context> context = fGlobal->getContext();
|
|
|
|
|
|
|
|
// Enter the context so all the remaining operations take place there.
|
|
|
|
Context::Scope contextScope(context);
|
|
|
|
|
|
|
|
// Wrap the C++ this pointer in a JavaScript wrapper.
|
|
|
|
Handle<Object> contextObj = this->wrap();
|
|
|
|
|
|
|
|
// Set up an exception handler before calling the Process function.
|
|
|
|
TryCatch tryCatch;
|
|
|
|
|
|
|
|
// Invoke the process function, giving the global object as 'this'
|
|
|
|
// and one argument, this JsContext.
|
|
|
|
const int argc = 1;
|
|
|
|
Handle<Value> argv[argc] = { contextObj };
|
|
|
|
Local<Function> onDraw =
|
|
|
|
Local<Function>::New(fGlobal->getIsolate(), fOnDraw);
|
|
|
|
Handle<Value> result = onDraw->Call(context->Global(), argc, argv);
|
|
|
|
|
|
|
|
// Handle any exceptions or output.
|
|
|
|
if (result.IsEmpty()) {
|
|
|
|
SkASSERT(tryCatch.HasCaught());
|
|
|
|
// Print errors that happened during execution.
|
|
|
|
fGlobal->reportException(&tryCatch);
|
|
|
|
} else {
|
|
|
|
SkASSERT(!tryCatch.HasCaught());
|
|
|
|
if (!result->IsUndefined()) {
|
|
|
|
// If all went well and the result wasn't undefined then print
|
|
|
|
// the returned value.
|
|
|
|
String::Utf8Value str(result);
|
|
|
|
const char* cstr = to_cstring(str);
|
|
|
|
printf("%s\n", cstr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the onDraw function from the global context.
|
|
|
|
bool JsContext::initialize() {
|
|
|
|
|
|
|
|
// Create a stack-allocated handle scope.
|
|
|
|
HandleScope handleScope(fGlobal->getIsolate());
|
|
|
|
|
|
|
|
// Create a local context from our global context.
|
|
|
|
Local<Context> context = fGlobal->getContext();
|
|
|
|
|
|
|
|
// Enter the scope so all operations take place in the scope.
|
|
|
|
Context::Scope contextScope(context);
|
|
|
|
|
|
|
|
v8::TryCatch try_catch;
|
|
|
|
|
|
|
|
Handle<String> fn_name = String::NewFromUtf8(
|
|
|
|
fGlobal->getIsolate(), "onDraw");
|
|
|
|
Handle<Value> fn_val = context->Global()->Get(fn_name);
|
|
|
|
|
|
|
|
if (!fn_val->IsFunction()) {
|
|
|
|
printf("Not a function.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// It is a function; cast it to a Function.
|
|
|
|
Handle<Function> fn_fun = Handle<Function>::Cast(fn_val);
|
|
|
|
|
|
|
|
// Store the function in a Persistent handle, since we also want that to
|
|
|
|
// remain after this call returns.
|
|
|
|
fOnDraw.Reset(fGlobal->getIsolate(), fn_fun);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|