| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 3 * | 3 * |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 * | 7 * |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 #include "Global.h" |
| 11 #include "Path2DBuilder.h" |
| 10 #include "Path2D.h" | 12 #include "Path2D.h" |
| 11 #include "Global.h" | 13 #include "SkPath.h" |
| 12 | 14 |
| 13 Global* Path2D::gGlobal = NULL; | 15 Global* Path2DBuilder::gGlobal = NULL; |
| 14 | 16 |
| 15 void Path2D::ConstructPath(const v8::FunctionCallbackInfo<v8::Value>& args) { | 17 void Path2DBuilder::ConstructPath(const v8::FunctionCallbackInfo<v8::Value>& arg
s) { |
| 16 v8::HandleScope handleScope(gGlobal->getIsolate()); | 18 v8::HandleScope handleScope(gGlobal->getIsolate()); |
| 17 Path2D* path = new Path2D(); | 19 Path2DBuilder* path = new Path2DBuilder(); |
| 18 args.This()->SetInternalField( | 20 args.This()->SetInternalField( |
| 19 0, v8::External::New(gGlobal->getIsolate(), path)); | 21 0, v8::External::New(gGlobal->getIsolate(), path)); |
| 20 } | 22 } |
| 21 | 23 |
| 22 #define ADD_METHOD(name, fn) \ | 24 #define ADD_METHOD(name, fn) \ |
| 23 constructor->InstanceTemplate()->Set( \ | 25 constructor->InstanceTemplate()->Set( \ |
| 24 v8::String::NewFromUtf8( \ | 26 v8::String::NewFromUtf8( \ |
| 25 global->getIsolate(), name, \ | 27 global->getIsolate(), name, \ |
| 26 v8::String::kInternalizedString), \ | 28 v8::String::kInternalizedString), \ |
| 27 v8::FunctionTemplate::New(global->getIsolate(), fn)) | 29 v8::FunctionTemplate::New(global->getIsolate(), fn)) |
| 28 | 30 |
| 29 // Install the constructor in the global scope so Path2Ds can be constructed | 31 // Install the constructor in the global scope so Path2DBuilders can be construc
ted |
| 30 // in JS. | 32 // in JS. |
| 31 void Path2D::AddToGlobal(Global* global) { | 33 void Path2DBuilder::AddToGlobal(Global* global) { |
| 32 gGlobal = global; | 34 gGlobal = global; |
| 33 | 35 |
| 34 // Create a stack-allocated handle scope. | 36 // Create a stack-allocated handle scope. |
| 35 v8::HandleScope handleScope(gGlobal->getIsolate()); | 37 v8::HandleScope handleScope(gGlobal->getIsolate()); |
| 36 | 38 |
| 37 v8::Handle<v8::Context> context = gGlobal->getContext(); | 39 v8::Handle<v8::Context> context = gGlobal->getContext(); |
| 38 | 40 |
| 39 // Enter the scope so all operations take place in the scope. | 41 // Enter the scope so all operations take place in the scope. |
| 40 v8::Context::Scope contextScope(context); | 42 v8::Context::Scope contextScope(context); |
| 41 | 43 |
| 42 v8::Local<v8::FunctionTemplate> constructor = v8::FunctionTemplate::New( | 44 v8::Local<v8::FunctionTemplate> constructor = v8::FunctionTemplate::New( |
| 43 gGlobal->getIsolate(), Path2D::ConstructPath); | 45 gGlobal->getIsolate(), Path2DBuilder::ConstructPath); |
| 44 constructor->InstanceTemplate()->SetInternalFieldCount(1); | 46 constructor->InstanceTemplate()->SetInternalFieldCount(1); |
| 45 | 47 |
| 46 ADD_METHOD("closePath", ClosePath); | 48 ADD_METHOD("close", ClosePath); |
| 47 ADD_METHOD("moveTo", MoveTo); | 49 ADD_METHOD("moveTo", MoveTo); |
| 48 ADD_METHOD("lineTo", LineTo); | 50 ADD_METHOD("lineTo", LineTo); |
| 49 ADD_METHOD("quadraticCurveTo", QuadraticCurveTo); | 51 ADD_METHOD("quadraticCurveTo", QuadraticCurveTo); |
| 50 ADD_METHOD("bezierCurveTo", BezierCurveTo); | 52 ADD_METHOD("bezierCurveTo", BezierCurveTo); |
| 51 ADD_METHOD("arc", Arc); | 53 ADD_METHOD("arc", Arc); |
| 52 ADD_METHOD("rect", Rect); | 54 ADD_METHOD("rect", Rect); |
| 53 ADD_METHOD("oval", Oval); | 55 ADD_METHOD("oval", Oval); |
| 54 ADD_METHOD("conicTo", ConicTo); | 56 ADD_METHOD("conicTo", ConicTo); |
| 55 | 57 |
| 58 ADD_METHOD("finalize", Finalize); |
| 59 |
| 56 context->Global()->Set(v8::String::NewFromUtf8( | 60 context->Global()->Set(v8::String::NewFromUtf8( |
| 57 gGlobal->getIsolate(), "Path2D"), constructor->GetFunction()); | 61 gGlobal->getIsolate(), "Path2DBuilder"), constructor->GetFunction())
; |
| 58 } | 62 } |
| 59 | 63 |
| 60 Path2D* Path2D::Unwrap(const v8::FunctionCallbackInfo<v8::Value>& args) { | 64 Path2DBuilder* Path2DBuilder::Unwrap(const v8::FunctionCallbackInfo<v8::Value>&
args) { |
| 61 v8::Handle<v8::External> field = v8::Handle<v8::External>::Cast( | 65 v8::Handle<v8::External> field = v8::Handle<v8::External>::Cast( |
| 62 args.This()->GetInternalField(0)); | 66 args.This()->GetInternalField(0)); |
| 63 void* ptr = field->Value(); | 67 void* ptr = field->Value(); |
| 64 return static_cast<Path2D*>(ptr); | 68 return static_cast<Path2DBuilder*>(ptr); |
| 65 } | 69 } |
| 66 | 70 |
| 67 void Path2D::ClosePath(const v8::FunctionCallbackInfo<v8::Value>& args) { | 71 void Path2DBuilder::ClosePath(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 68 Path2D* path = Unwrap(args); | 72 Path2DBuilder* path = Unwrap(args); |
| 69 path->fSkPath.close(); | 73 path->fSkPath.close(); |
| 70 } | 74 } |
| 71 | 75 |
| 72 void Path2D::MoveTo(const v8::FunctionCallbackInfo<v8::Value>& args) { | 76 void Path2DBuilder::MoveTo(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 73 if (args.Length() != 2) { | 77 if (args.Length() != 2) { |
| 74 args.GetIsolate()->ThrowException( | 78 args.GetIsolate()->ThrowException( |
| 75 v8::String::NewFromUtf8( | 79 v8::String::NewFromUtf8( |
| 76 args.GetIsolate(), "Error: 2 arguments required.")); | 80 args.GetIsolate(), "Error: 2 arguments required.")); |
| 77 return; | 81 return; |
| 78 } | 82 } |
| 79 double x = args[0]->NumberValue(); | 83 double x = args[0]->NumberValue(); |
| 80 double y = args[1]->NumberValue(); | 84 double y = args[1]->NumberValue(); |
| 81 Path2D* path = Unwrap(args); | 85 Path2DBuilder* path = Unwrap(args); |
| 82 path->fSkPath.moveTo(SkDoubleToScalar(x), SkDoubleToScalar(y)); | 86 path->fSkPath.moveTo(SkDoubleToScalar(x), SkDoubleToScalar(y)); |
| 83 } | 87 } |
| 84 | 88 |
| 85 void Path2D::LineTo(const v8::FunctionCallbackInfo<v8::Value>& args) { | 89 void Path2DBuilder::LineTo(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 86 if (args.Length() != 2) { | 90 if (args.Length() != 2) { |
| 87 args.GetIsolate()->ThrowException( | 91 args.GetIsolate()->ThrowException( |
| 88 v8::String::NewFromUtf8( | 92 v8::String::NewFromUtf8( |
| 89 args.GetIsolate(), "Error: 2 arguments required.")); | 93 args.GetIsolate(), "Error: 2 arguments required.")); |
| 90 return; | 94 return; |
| 91 } | 95 } |
| 92 double x = args[0]->NumberValue(); | 96 double x = args[0]->NumberValue(); |
| 93 double y = args[1]->NumberValue(); | 97 double y = args[1]->NumberValue(); |
| 94 Path2D* path = Unwrap(args); | 98 Path2DBuilder* path = Unwrap(args); |
| 95 path->fSkPath.lineTo(SkDoubleToScalar(x), SkDoubleToScalar(y)); | 99 path->fSkPath.lineTo(SkDoubleToScalar(x), SkDoubleToScalar(y)); |
| 96 } | 100 } |
| 97 | 101 |
| 98 void Path2D::QuadraticCurveTo(const v8::FunctionCallbackInfo<v8::Value>& args) { | 102 void Path2DBuilder::QuadraticCurveTo(const v8::FunctionCallbackInfo<v8::Value>&
args) { |
| 99 if (args.Length() != 4) { | 103 if (args.Length() != 4) { |
| 100 args.GetIsolate()->ThrowException( | 104 args.GetIsolate()->ThrowException( |
| 101 v8::String::NewFromUtf8( | 105 v8::String::NewFromUtf8( |
| 102 args.GetIsolate(), "Error: 4 arguments required.")); | 106 args.GetIsolate(), "Error: 4 arguments required.")); |
| 103 return; | 107 return; |
| 104 } | 108 } |
| 105 double cpx = args[0]->NumberValue(); | 109 double cpx = args[0]->NumberValue(); |
| 106 double cpy = args[1]->NumberValue(); | 110 double cpy = args[1]->NumberValue(); |
| 107 double x = args[2]->NumberValue(); | 111 double x = args[2]->NumberValue(); |
| 108 double y = args[3]->NumberValue(); | 112 double y = args[3]->NumberValue(); |
| 109 Path2D* path = Unwrap(args); | 113 Path2DBuilder* path = Unwrap(args); |
| 110 // TODO(jcgregorio) Doesn't handle the empty last path case correctly per | 114 // TODO(jcgregorio) Doesn't handle the empty last path case correctly per |
| 111 // the HTML 5 spec. | 115 // the HTML 5 spec. |
| 112 path->fSkPath.quadTo( | 116 path->fSkPath.quadTo( |
| 113 SkDoubleToScalar(cpx), SkDoubleToScalar(cpy), | 117 SkDoubleToScalar(cpx), SkDoubleToScalar(cpy), |
| 114 SkDoubleToScalar(x), SkDoubleToScalar(y)); | 118 SkDoubleToScalar(x), SkDoubleToScalar(y)); |
| 115 } | 119 } |
| 116 | 120 |
| 117 void Path2D::BezierCurveTo(const v8::FunctionCallbackInfo<v8::Value>& args) { | 121 void Path2DBuilder::BezierCurveTo(const v8::FunctionCallbackInfo<v8::Value>& arg
s) { |
| 118 if (args.Length() != 6) { | 122 if (args.Length() != 6) { |
| 119 args.GetIsolate()->ThrowException( | 123 args.GetIsolate()->ThrowException( |
| 120 v8::String::NewFromUtf8( | 124 v8::String::NewFromUtf8( |
| 121 args.GetIsolate(), "Error: 6 arguments required.")); | 125 args.GetIsolate(), "Error: 6 arguments required.")); |
| 122 return; | 126 return; |
| 123 } | 127 } |
| 124 double cp1x = args[0]->NumberValue(); | 128 double cp1x = args[0]->NumberValue(); |
| 125 double cp1y = args[1]->NumberValue(); | 129 double cp1y = args[1]->NumberValue(); |
| 126 double cp2x = args[2]->NumberValue(); | 130 double cp2x = args[2]->NumberValue(); |
| 127 double cp2y = args[3]->NumberValue(); | 131 double cp2y = args[3]->NumberValue(); |
| 128 double x = args[4]->NumberValue(); | 132 double x = args[4]->NumberValue(); |
| 129 double y = args[5]->NumberValue(); | 133 double y = args[5]->NumberValue(); |
| 130 Path2D* path = Unwrap(args); | 134 Path2DBuilder* path = Unwrap(args); |
| 131 // TODO(jcgregorio) Doesn't handle the empty last path case correctly per | 135 // TODO(jcgregorio) Doesn't handle the empty last path case correctly per |
| 132 // the HTML 5 spec. | 136 // the HTML 5 spec. |
| 133 path->fSkPath.cubicTo( | 137 path->fSkPath.cubicTo( |
| 134 SkDoubleToScalar(cp1x), SkDoubleToScalar(cp1y), | 138 SkDoubleToScalar(cp1x), SkDoubleToScalar(cp1y), |
| 135 SkDoubleToScalar(cp2x), SkDoubleToScalar(cp2y), | 139 SkDoubleToScalar(cp2x), SkDoubleToScalar(cp2y), |
| 136 SkDoubleToScalar(x), SkDoubleToScalar(y)); | 140 SkDoubleToScalar(x), SkDoubleToScalar(y)); |
| 137 } | 141 } |
| 138 | 142 |
| 139 void Path2D::Arc(const v8::FunctionCallbackInfo<v8::Value>& args) { | 143 void Path2DBuilder::Arc(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 140 if (args.Length() != 5 && args.Length() != 6) { | 144 if (args.Length() != 5 && args.Length() != 6) { |
| 141 args.GetIsolate()->ThrowException( | 145 args.GetIsolate()->ThrowException( |
| 142 v8::String::NewFromUtf8( | 146 v8::String::NewFromUtf8( |
| 143 args.GetIsolate(), "Error: 5 or 6 args required.")); | 147 args.GetIsolate(), "Error: 5 or 6 args required.")); |
| 144 return; | 148 return; |
| 145 } | 149 } |
| 146 double x = args[0]->NumberValue(); | 150 double x = args[0]->NumberValue(); |
| 147 double y = args[1]->NumberValue(); | 151 double y = args[1]->NumberValue(); |
| 148 double radius = args[2]->NumberValue(); | 152 double radius = args[2]->NumberValue(); |
| 149 double startAngle = args[3]->NumberValue(); | 153 double startAngle = args[3]->NumberValue(); |
| 150 double endAngle = args[4]->NumberValue(); | 154 double endAngle = args[4]->NumberValue(); |
| 151 bool antiClockwise = false; | 155 bool antiClockwise = false; |
| 152 if (args.Length() == 6) { | 156 if (args.Length() == 6) { |
| 153 antiClockwise = args[5]->BooleanValue(); | 157 antiClockwise = args[5]->BooleanValue(); |
| 154 } | 158 } |
| 155 double sweepAngle; | 159 double sweepAngle; |
| 156 if (!antiClockwise) { | 160 if (!antiClockwise) { |
| 157 sweepAngle = endAngle - startAngle; | 161 sweepAngle = endAngle - startAngle; |
| 158 } else { | 162 } else { |
| 159 sweepAngle = startAngle - endAngle; | 163 sweepAngle = startAngle - endAngle; |
| 160 startAngle = endAngle; | 164 startAngle = endAngle; |
| 161 } | 165 } |
| 162 | 166 |
| 163 Path2D* path = Unwrap(args); | 167 Path2DBuilder* path = Unwrap(args); |
| 164 SkRect rect = { | 168 SkRect rect = { |
| 165 SkDoubleToScalar(x-radius), | 169 SkDoubleToScalar(x-radius), |
| 166 SkDoubleToScalar(y-radius), | 170 SkDoubleToScalar(y-radius), |
| 167 SkDoubleToScalar(x+radius), | 171 SkDoubleToScalar(x+radius), |
| 168 SkDoubleToScalar(y+radius) | 172 SkDoubleToScalar(y+radius) |
| 169 }; | 173 }; |
| 170 | 174 |
| 171 path->fSkPath.addArc(rect, SkRadiansToDegrees(startAngle), | 175 path->fSkPath.addArc(rect, SkRadiansToDegrees(startAngle), |
| 172 SkRadiansToDegrees(sweepAngle)); | 176 SkRadiansToDegrees(sweepAngle)); |
| 173 } | 177 } |
| 174 | 178 |
| 175 void Path2D::Rect(const v8::FunctionCallbackInfo<v8::Value>& args) { | 179 void Path2DBuilder::Rect(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 176 if (args.Length() != 4) { | 180 if (args.Length() != 4) { |
| 177 args.GetIsolate()->ThrowException( | 181 args.GetIsolate()->ThrowException( |
| 178 v8::String::NewFromUtf8( | 182 v8::String::NewFromUtf8( |
| 179 args.GetIsolate(), "Error: 4 arguments required.")); | 183 args.GetIsolate(), "Error: 4 arguments required.")); |
| 180 return; | 184 return; |
| 181 } | 185 } |
| 182 double x = args[0]->NumberValue(); | 186 double x = args[0]->NumberValue(); |
| 183 double y = args[1]->NumberValue(); | 187 double y = args[1]->NumberValue(); |
| 184 double w = args[2]->NumberValue(); | 188 double w = args[2]->NumberValue(); |
| 185 double h = args[3]->NumberValue(); | 189 double h = args[3]->NumberValue(); |
| 186 | 190 |
| 187 SkRect rect = { | 191 SkRect rect = { |
| 188 SkDoubleToScalar(x), | 192 SkDoubleToScalar(x), |
| 189 SkDoubleToScalar(y), | 193 SkDoubleToScalar(y), |
| 190 SkDoubleToScalar(x) + SkDoubleToScalar(w), | 194 SkDoubleToScalar(x) + SkDoubleToScalar(w), |
| 191 SkDoubleToScalar(y) + SkDoubleToScalar(h) | 195 SkDoubleToScalar(y) + SkDoubleToScalar(h) |
| 192 }; | 196 }; |
| 193 Path2D* path = Unwrap(args); | 197 Path2DBuilder* path = Unwrap(args); |
| 194 path->fSkPath.addRect(rect); | 198 path->fSkPath.addRect(rect); |
| 195 } | 199 } |
| 196 | 200 |
| 197 void Path2D::Oval(const v8::FunctionCallbackInfo<v8::Value>& args) { | 201 void Path2DBuilder::Oval(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 198 if (args.Length() != 4 && args.Length() != 5) { | 202 if (args.Length() != 4 && args.Length() != 5) { |
| 199 args.GetIsolate()->ThrowException( | 203 args.GetIsolate()->ThrowException( |
| 200 v8::String::NewFromUtf8( | 204 v8::String::NewFromUtf8( |
| 201 args.GetIsolate(), "Error: 4 or 5 args required.")); | 205 args.GetIsolate(), "Error: 4 or 5 args required.")); |
| 202 return; | 206 return; |
| 203 } | 207 } |
| 204 double x = args[0]->NumberValue(); | 208 double x = args[0]->NumberValue(); |
| 205 double y = args[1]->NumberValue(); | 209 double y = args[1]->NumberValue(); |
| 206 double radiusX = args[2]->NumberValue(); | 210 double radiusX = args[2]->NumberValue(); |
| 207 double radiusY = args[3]->NumberValue(); | 211 double radiusY = args[3]->NumberValue(); |
| 208 SkPath::Direction dir = SkPath::kCW_Direction; | 212 SkPath::Direction dir = SkPath::kCW_Direction; |
| 209 if (args.Length() == 5 && !args[4]->BooleanValue()) { | 213 if (args.Length() == 5 && !args[4]->BooleanValue()) { |
| 210 dir = SkPath::kCCW_Direction; | 214 dir = SkPath::kCCW_Direction; |
| 211 } | 215 } |
| 212 Path2D* path = Unwrap(args); | 216 Path2DBuilder* path = Unwrap(args); |
| 213 SkRect rect = { | 217 SkRect rect = { |
| 214 SkDoubleToScalar(x-radiusX), | 218 SkDoubleToScalar(x-radiusX), |
| 215 SkDoubleToScalar(y-radiusX), | 219 SkDoubleToScalar(y-radiusX), |
| 216 SkDoubleToScalar(x+radiusY), | 220 SkDoubleToScalar(x+radiusY), |
| 217 SkDoubleToScalar(y+radiusY) | 221 SkDoubleToScalar(y+radiusY) |
| 218 }; | 222 }; |
| 219 | 223 |
| 220 path->fSkPath.addOval(rect, dir); | 224 path->fSkPath.addOval(rect, dir); |
| 221 } | 225 } |
| 222 | 226 |
| 223 void Path2D::ConicTo(const v8::FunctionCallbackInfo<v8::Value>& args) { | 227 void Path2DBuilder::ConicTo(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 224 if (args.Length() != 5) { | 228 if (args.Length() != 5) { |
| 225 args.GetIsolate()->ThrowException( | 229 args.GetIsolate()->ThrowException( |
| 226 v8::String::NewFromUtf8( | 230 v8::String::NewFromUtf8( |
| 227 args.GetIsolate(), "Error: 5 args required.")); | 231 args.GetIsolate(), "Error: 5 args required.")); |
| 228 return; | 232 return; |
| 229 } | 233 } |
| 230 double x1 = args[0]->NumberValue(); | 234 double x1 = args[0]->NumberValue(); |
| 231 double y1 = args[1]->NumberValue(); | 235 double y1 = args[1]->NumberValue(); |
| 232 double x2 = args[2]->NumberValue(); | 236 double x2 = args[2]->NumberValue(); |
| 233 double y2 = args[3]->NumberValue(); | 237 double y2 = args[3]->NumberValue(); |
| 234 double w = args[4]->NumberValue(); | 238 double w = args[4]->NumberValue(); |
| 235 Path2D* path = Unwrap(args); | 239 Path2DBuilder* path = Unwrap(args); |
| 236 | 240 |
| 237 path->fSkPath.conicTo( | 241 path->fSkPath.conicTo( |
| 238 SkDoubleToScalar(x1), | 242 SkDoubleToScalar(x1), |
| 239 SkDoubleToScalar(y1), | 243 SkDoubleToScalar(y1), |
| 240 SkDoubleToScalar(x2), | 244 SkDoubleToScalar(x2), |
| 241 SkDoubleToScalar(y2), | 245 SkDoubleToScalar(y2), |
| 242 SkDoubleToScalar(w) | 246 SkDoubleToScalar(w) |
| 243 ); | 247 ); |
| 244 } | 248 } |
| 249 |
| 250 void Path2DBuilder::Finalize(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 251 Path2DBuilder* path = Unwrap(args); |
| 252 |
| 253 // Build Path2D from out fSkPath and return it. |
| 254 SkPath* skPath = new SkPath(path->fSkPath); |
| 255 |
| 256 path->fSkPath.reset(); |
| 257 |
| 258 Path2D* pathWrap = new Path2D(skPath); |
| 259 |
| 260 args.GetReturnValue().Set(pathWrap->persistent()); |
| 261 } |
| OLD | NEW |