2014-01-06 18:17:24 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-02-27 20:20:21 +00:00
|
|
|
#include "Path2D.h"
|
2014-01-06 18:17:24 +00:00
|
|
|
#include "Global.h"
|
|
|
|
|
2014-02-27 20:20:21 +00:00
|
|
|
Global* Path2D::gGlobal = NULL;
|
2014-01-06 18:17:24 +00:00
|
|
|
|
2014-10-24 19:49:17 +00:00
|
|
|
void Path2D::ConstructPath(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
v8::HandleScope handleScope(gGlobal->getIsolate());
|
2014-02-27 20:20:21 +00:00
|
|
|
Path2D* path = new Path2D();
|
2014-01-08 15:14:09 +00:00
|
|
|
args.This()->SetInternalField(
|
2014-10-24 19:49:17 +00:00
|
|
|
0, v8::External::New(gGlobal->getIsolate(), path));
|
2014-01-06 18:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define ADD_METHOD(name, fn) \
|
|
|
|
constructor->InstanceTemplate()->Set( \
|
2014-10-24 19:49:17 +00:00
|
|
|
v8::String::NewFromUtf8( \
|
2014-01-06 18:17:24 +00:00
|
|
|
global->getIsolate(), name, \
|
2014-10-24 19:49:17 +00:00
|
|
|
v8::String::kInternalizedString), \
|
|
|
|
v8::FunctionTemplate::New(global->getIsolate(), fn))
|
2014-01-06 18:17:24 +00:00
|
|
|
|
2014-02-27 20:20:21 +00:00
|
|
|
// Install the constructor in the global scope so Path2Ds can be constructed
|
2014-01-06 18:17:24 +00:00
|
|
|
// in JS.
|
2014-02-27 20:20:21 +00:00
|
|
|
void Path2D::AddToGlobal(Global* global) {
|
2014-01-06 18:17:24 +00:00
|
|
|
gGlobal = global;
|
|
|
|
|
|
|
|
// Create a stack-allocated handle scope.
|
2014-10-24 19:49:17 +00:00
|
|
|
v8::HandleScope handleScope(gGlobal->getIsolate());
|
2014-01-06 18:17:24 +00:00
|
|
|
|
2014-10-24 19:49:17 +00:00
|
|
|
v8::Handle<v8::Context> context = gGlobal->getContext();
|
2014-01-06 18:17:24 +00:00
|
|
|
|
|
|
|
// Enter the scope so all operations take place in the scope.
|
2014-10-24 19:49:17 +00:00
|
|
|
v8::Context::Scope contextScope(context);
|
2014-01-06 18:17:24 +00:00
|
|
|
|
2014-10-24 19:49:17 +00:00
|
|
|
v8::Local<v8::FunctionTemplate> constructor = v8::FunctionTemplate::New(
|
2014-02-27 20:20:21 +00:00
|
|
|
gGlobal->getIsolate(), Path2D::ConstructPath);
|
2014-01-06 18:17:24 +00:00
|
|
|
constructor->InstanceTemplate()->SetInternalFieldCount(1);
|
|
|
|
|
2014-01-10 21:33:01 +00:00
|
|
|
ADD_METHOD("closePath", ClosePath);
|
2014-01-06 18:17:24 +00:00
|
|
|
ADD_METHOD("moveTo", MoveTo);
|
|
|
|
ADD_METHOD("lineTo", LineTo);
|
|
|
|
ADD_METHOD("quadraticCurveTo", QuadraticCurveTo);
|
|
|
|
ADD_METHOD("bezierCurveTo", BezierCurveTo);
|
|
|
|
ADD_METHOD("arc", Arc);
|
|
|
|
ADD_METHOD("rect", Rect);
|
2014-01-10 21:33:01 +00:00
|
|
|
ADD_METHOD("oval", Oval);
|
2014-02-13 16:00:58 +00:00
|
|
|
ADD_METHOD("conicTo", ConicTo);
|
2014-01-06 18:17:24 +00:00
|
|
|
|
2014-10-24 19:49:17 +00:00
|
|
|
context->Global()->Set(v8::String::NewFromUtf8(
|
2014-02-27 20:20:21 +00:00
|
|
|
gGlobal->getIsolate(), "Path2D"), constructor->GetFunction());
|
2014-01-06 18:17:24 +00:00
|
|
|
}
|
|
|
|
|
2014-10-24 19:49:17 +00:00
|
|
|
Path2D* Path2D::Unwrap(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
v8::Handle<v8::External> field = v8::Handle<v8::External>::Cast(
|
2014-01-06 18:17:24 +00:00
|
|
|
args.This()->GetInternalField(0));
|
|
|
|
void* ptr = field->Value();
|
2014-02-27 20:20:21 +00:00
|
|
|
return static_cast<Path2D*>(ptr);
|
2014-01-06 18:17:24 +00:00
|
|
|
}
|
|
|
|
|
2014-10-24 19:49:17 +00:00
|
|
|
void Path2D::ClosePath(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2014-02-27 20:20:21 +00:00
|
|
|
Path2D* path = Unwrap(args);
|
2014-01-06 18:17:24 +00:00
|
|
|
path->fSkPath.close();
|
|
|
|
}
|
|
|
|
|
2014-10-24 19:49:17 +00:00
|
|
|
void Path2D::MoveTo(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2014-01-06 18:17:24 +00:00
|
|
|
if (args.Length() != 2) {
|
|
|
|
args.GetIsolate()->ThrowException(
|
|
|
|
v8::String::NewFromUtf8(
|
|
|
|
args.GetIsolate(), "Error: 2 arguments required."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
double x = args[0]->NumberValue();
|
|
|
|
double y = args[1]->NumberValue();
|
2014-02-27 20:20:21 +00:00
|
|
|
Path2D* path = Unwrap(args);
|
2014-01-06 18:17:24 +00:00
|
|
|
path->fSkPath.moveTo(SkDoubleToScalar(x), SkDoubleToScalar(y));
|
|
|
|
}
|
|
|
|
|
2014-10-24 19:49:17 +00:00
|
|
|
void Path2D::LineTo(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2014-01-06 18:17:24 +00:00
|
|
|
if (args.Length() != 2) {
|
|
|
|
args.GetIsolate()->ThrowException(
|
|
|
|
v8::String::NewFromUtf8(
|
|
|
|
args.GetIsolate(), "Error: 2 arguments required."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
double x = args[0]->NumberValue();
|
|
|
|
double y = args[1]->NumberValue();
|
2014-02-27 20:20:21 +00:00
|
|
|
Path2D* path = Unwrap(args);
|
2014-01-06 18:17:24 +00:00
|
|
|
path->fSkPath.lineTo(SkDoubleToScalar(x), SkDoubleToScalar(y));
|
|
|
|
}
|
|
|
|
|
2014-10-24 19:49:17 +00:00
|
|
|
void Path2D::QuadraticCurveTo(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2014-01-06 18:17:24 +00:00
|
|
|
if (args.Length() != 4) {
|
|
|
|
args.GetIsolate()->ThrowException(
|
|
|
|
v8::String::NewFromUtf8(
|
|
|
|
args.GetIsolate(), "Error: 4 arguments required."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
double cpx = args[0]->NumberValue();
|
|
|
|
double cpy = args[1]->NumberValue();
|
|
|
|
double x = args[2]->NumberValue();
|
|
|
|
double y = args[3]->NumberValue();
|
2014-02-27 20:20:21 +00:00
|
|
|
Path2D* path = Unwrap(args);
|
2014-01-06 18:17:24 +00:00
|
|
|
// TODO(jcgregorio) Doesn't handle the empty last path case correctly per
|
|
|
|
// the HTML 5 spec.
|
|
|
|
path->fSkPath.quadTo(
|
|
|
|
SkDoubleToScalar(cpx), SkDoubleToScalar(cpy),
|
|
|
|
SkDoubleToScalar(x), SkDoubleToScalar(y));
|
|
|
|
}
|
|
|
|
|
2014-10-24 19:49:17 +00:00
|
|
|
void Path2D::BezierCurveTo(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2014-01-06 18:17:24 +00:00
|
|
|
if (args.Length() != 6) {
|
|
|
|
args.GetIsolate()->ThrowException(
|
|
|
|
v8::String::NewFromUtf8(
|
|
|
|
args.GetIsolate(), "Error: 6 arguments required."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
double cp1x = args[0]->NumberValue();
|
|
|
|
double cp1y = args[1]->NumberValue();
|
|
|
|
double cp2x = args[2]->NumberValue();
|
|
|
|
double cp2y = args[3]->NumberValue();
|
|
|
|
double x = args[4]->NumberValue();
|
|
|
|
double y = args[5]->NumberValue();
|
2014-02-27 20:20:21 +00:00
|
|
|
Path2D* path = Unwrap(args);
|
2014-01-06 18:17:24 +00:00
|
|
|
// TODO(jcgregorio) Doesn't handle the empty last path case correctly per
|
|
|
|
// the HTML 5 spec.
|
|
|
|
path->fSkPath.cubicTo(
|
|
|
|
SkDoubleToScalar(cp1x), SkDoubleToScalar(cp1y),
|
|
|
|
SkDoubleToScalar(cp2x), SkDoubleToScalar(cp2y),
|
|
|
|
SkDoubleToScalar(x), SkDoubleToScalar(y));
|
|
|
|
}
|
|
|
|
|
2014-10-24 19:49:17 +00:00
|
|
|
void Path2D::Arc(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2014-01-06 18:17:24 +00:00
|
|
|
if (args.Length() != 5 && args.Length() != 6) {
|
|
|
|
args.GetIsolate()->ThrowException(
|
|
|
|
v8::String::NewFromUtf8(
|
|
|
|
args.GetIsolate(), "Error: 5 or 6 args required."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
double x = args[0]->NumberValue();
|
|
|
|
double y = args[1]->NumberValue();
|
|
|
|
double radius = args[2]->NumberValue();
|
|
|
|
double startAngle = args[3]->NumberValue();
|
|
|
|
double endAngle = args[4]->NumberValue();
|
|
|
|
bool antiClockwise = false;
|
|
|
|
if (args.Length() == 6) {
|
|
|
|
antiClockwise = args[5]->BooleanValue();
|
|
|
|
}
|
|
|
|
double sweepAngle;
|
|
|
|
if (!antiClockwise) {
|
|
|
|
sweepAngle = endAngle - startAngle;
|
|
|
|
} else {
|
|
|
|
sweepAngle = startAngle - endAngle;
|
|
|
|
startAngle = endAngle;
|
|
|
|
}
|
|
|
|
|
2014-02-27 20:20:21 +00:00
|
|
|
Path2D* path = Unwrap(args);
|
2014-01-06 18:17:24 +00:00
|
|
|
SkRect rect = {
|
|
|
|
SkDoubleToScalar(x-radius),
|
|
|
|
SkDoubleToScalar(y-radius),
|
|
|
|
SkDoubleToScalar(x+radius),
|
|
|
|
SkDoubleToScalar(y+radius)
|
|
|
|
};
|
|
|
|
|
|
|
|
path->fSkPath.addArc(rect, SkRadiansToDegrees(startAngle),
|
|
|
|
SkRadiansToDegrees(sweepAngle));
|
|
|
|
}
|
|
|
|
|
2014-10-24 19:49:17 +00:00
|
|
|
void Path2D::Rect(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2014-01-06 18:17:24 +00:00
|
|
|
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)
|
|
|
|
};
|
2014-02-27 20:20:21 +00:00
|
|
|
Path2D* path = Unwrap(args);
|
2014-01-06 18:17:24 +00:00
|
|
|
path->fSkPath.addRect(rect);
|
|
|
|
}
|
2014-01-10 21:33:01 +00:00
|
|
|
|
2014-10-24 19:49:17 +00:00
|
|
|
void Path2D::Oval(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2014-01-10 21:33:01 +00:00
|
|
|
if (args.Length() != 4 && args.Length() != 5) {
|
|
|
|
args.GetIsolate()->ThrowException(
|
|
|
|
v8::String::NewFromUtf8(
|
|
|
|
args.GetIsolate(), "Error: 4 or 5 args required."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
double x = args[0]->NumberValue();
|
|
|
|
double y = args[1]->NumberValue();
|
|
|
|
double radiusX = args[2]->NumberValue();
|
|
|
|
double radiusY = args[3]->NumberValue();
|
|
|
|
SkPath::Direction dir = SkPath::kCW_Direction;
|
|
|
|
if (args.Length() == 5 && !args[4]->BooleanValue()) {
|
|
|
|
dir = SkPath::kCCW_Direction;
|
|
|
|
}
|
2014-02-27 20:20:21 +00:00
|
|
|
Path2D* path = Unwrap(args);
|
2014-01-10 21:33:01 +00:00
|
|
|
SkRect rect = {
|
|
|
|
SkDoubleToScalar(x-radiusX),
|
|
|
|
SkDoubleToScalar(y-radiusX),
|
|
|
|
SkDoubleToScalar(x+radiusY),
|
|
|
|
SkDoubleToScalar(y+radiusY)
|
|
|
|
};
|
|
|
|
|
|
|
|
path->fSkPath.addOval(rect, dir);
|
|
|
|
}
|
2014-02-13 16:00:58 +00:00
|
|
|
|
2014-10-24 19:49:17 +00:00
|
|
|
void Path2D::ConicTo(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2014-02-13 16:00:58 +00:00
|
|
|
if (args.Length() != 5) {
|
|
|
|
args.GetIsolate()->ThrowException(
|
|
|
|
v8::String::NewFromUtf8(
|
|
|
|
args.GetIsolate(), "Error: 5 args required."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
double x1 = args[0]->NumberValue();
|
|
|
|
double y1 = args[1]->NumberValue();
|
|
|
|
double x2 = args[2]->NumberValue();
|
|
|
|
double y2 = args[3]->NumberValue();
|
|
|
|
double w = args[4]->NumberValue();
|
2014-02-27 20:20:21 +00:00
|
|
|
Path2D* path = Unwrap(args);
|
2014-02-13 16:00:58 +00:00
|
|
|
|
|
|
|
path->fSkPath.conicTo(
|
|
|
|
SkDoubleToScalar(x1),
|
|
|
|
SkDoubleToScalar(y1),
|
|
|
|
SkDoubleToScalar(x2),
|
|
|
|
SkDoubleToScalar(y2),
|
|
|
|
SkDoubleToScalar(w)
|
|
|
|
);
|
|
|
|
}
|