OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2013 Google Inc. | 3 * Copyright 2013 Google Inc. |
4 * | 4 * |
5 * | 5 * |
6 * Use of this source code is governed by a BSD-style license that can be | 6 * Use of this source code is governed by a BSD-style license that can be |
7 * found in the LICENSE file. | 7 * found in the LICENSE file. |
8 * | 8 * |
9 */ | 9 */ |
10 #include <v8.h> | 10 #include <v8.h> |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 double y = args[1]->NumberValue(); | 45 double y = args[1]->NumberValue(); |
46 double w = args[2]->NumberValue(); | 46 double w = args[2]->NumberValue(); |
47 double h = args[3]->NumberValue(); | 47 double h = args[3]->NumberValue(); |
48 | 48 |
49 SkRect rect = { | 49 SkRect rect = { |
50 SkDoubleToScalar(x), | 50 SkDoubleToScalar(x), |
51 SkDoubleToScalar(y), | 51 SkDoubleToScalar(y), |
52 SkDoubleToScalar(x) + SkDoubleToScalar(w), | 52 SkDoubleToScalar(x) + SkDoubleToScalar(w), |
53 SkDoubleToScalar(y) + SkDoubleToScalar(h) | 53 SkDoubleToScalar(y) + SkDoubleToScalar(h) |
54 }; | 54 }; |
55 jsContext->fFillStyle.setStyle(SkPaint::kFill_Style); | |
56 canvas->drawRect(rect, jsContext->fFillStyle); | 55 canvas->drawRect(rect, jsContext->fFillStyle); |
57 } | 56 } |
58 | 57 |
| 58 void JsContext::Save(const v8::FunctionCallbackInfo<Value>& args) { |
| 59 JsContext* jsContext = Unwrap(args.This()); |
| 60 SkCanvas* canvas = jsContext->fCanvas; |
| 61 |
| 62 canvas->save(); |
| 63 } |
| 64 |
| 65 void JsContext::Restore(const v8::FunctionCallbackInfo<Value>& args) { |
| 66 JsContext* jsContext = Unwrap(args.This()); |
| 67 SkCanvas* canvas = jsContext->fCanvas; |
| 68 |
| 69 canvas->restore(); |
| 70 } |
| 71 |
| 72 void JsContext::Rotate(const v8::FunctionCallbackInfo<Value>& args) { |
| 73 JsContext* jsContext = Unwrap(args.This()); |
| 74 SkCanvas* canvas = jsContext->fCanvas; |
| 75 |
| 76 if (args.Length() != 1) { |
| 77 args.GetIsolate()->ThrowException( |
| 78 v8::String::NewFromUtf8( |
| 79 args.GetIsolate(), "Error: 1 arguments required.")); |
| 80 return; |
| 81 } |
| 82 double angle = args[0]->NumberValue(); |
| 83 canvas->rotate(SkRadiansToDegrees(angle)); |
| 84 } |
| 85 |
59 void JsContext::Translate(const v8::FunctionCallbackInfo<Value>& args) { | 86 void JsContext::Translate(const v8::FunctionCallbackInfo<Value>& args) { |
60 JsContext* jsContext = Unwrap(args.This()); | 87 JsContext* jsContext = Unwrap(args.This()); |
61 SkCanvas* canvas = jsContext->fCanvas; | 88 SkCanvas* canvas = jsContext->fCanvas; |
62 | 89 |
63 if (args.Length() != 2) { | 90 if (args.Length() != 2) { |
64 args.GetIsolate()->ThrowException( | 91 args.GetIsolate()->ThrowException( |
65 v8::String::NewFromUtf8( | 92 v8::String::NewFromUtf8( |
66 args.GetIsolate(), "Error: 2 arguments required.")); | 93 args.GetIsolate(), "Error: 2 arguments required.")); |
67 return; | 94 return; |
68 } | 95 } |
(...skipping 18 matching lines...) Expand all Loading... |
87 v8::String::NewFromUtf8( | 114 v8::String::NewFromUtf8( |
88 args.GetIsolate(), "Error: 1 arguments required.")); | 115 args.GetIsolate(), "Error: 1 arguments required.")); |
89 return; | 116 return; |
90 } | 117 } |
91 | 118 |
92 Handle<External> field = Handle<External>::Cast( | 119 Handle<External> field = Handle<External>::Cast( |
93 args[0]->ToObject()->GetInternalField(0)); | 120 args[0]->ToObject()->GetInternalField(0)); |
94 void* ptr = field->Value(); | 121 void* ptr = field->Value(); |
95 Path* path = static_cast<Path*>(ptr); | 122 Path* path = static_cast<Path*>(ptr); |
96 | 123 |
97 jsContext->fFillStyle.setStyle(SkPaint::kStroke_Style); | 124 canvas->drawPath(path->getSkPath(), jsContext->fStrokeStyle); |
98 canvas->drawPath(path->getSkPath(), jsContext->fFillStyle); | |
99 } | 125 } |
100 | 126 |
101 | |
102 void JsContext::Fill(const v8::FunctionCallbackInfo<Value>& args) { | 127 void JsContext::Fill(const v8::FunctionCallbackInfo<Value>& args) { |
103 JsContext* jsContext = Unwrap(args.This()); | 128 JsContext* jsContext = Unwrap(args.This()); |
104 SkCanvas* canvas = jsContext->fCanvas; | 129 SkCanvas* canvas = jsContext->fCanvas; |
105 | 130 |
106 if (args.Length() != 1) { | 131 if (args.Length() != 1) { |
107 args.GetIsolate()->ThrowException( | 132 args.GetIsolate()->ThrowException( |
108 v8::String::NewFromUtf8( | 133 v8::String::NewFromUtf8( |
109 args.GetIsolate(), "Error: 1 arguments required.")); | 134 args.GetIsolate(), "Error: 1 arguments required.")); |
110 return; | 135 return; |
111 } | 136 } |
112 | 137 |
113 Handle<External> field = Handle<External>::Cast( | 138 Handle<External> field = Handle<External>::Cast( |
114 args[0]->ToObject()->GetInternalField(0)); | 139 args[0]->ToObject()->GetInternalField(0)); |
115 void* ptr = field->Value(); | 140 void* ptr = field->Value(); |
116 Path* path = static_cast<Path*>(ptr); | 141 Path* path = static_cast<Path*>(ptr); |
117 | 142 |
118 jsContext->fFillStyle.setStyle(SkPaint::kFill_Style); | |
119 canvas->drawPath(path->getSkPath(), jsContext->fFillStyle); | 143 canvas->drawPath(path->getSkPath(), jsContext->fFillStyle); |
120 } | 144 } |
121 | 145 |
122 | 146 void JsContext::GetStyle(Local<String> name, |
123 void JsContext::GetFillStyle(Local<String> name, | 147 const PropertyCallbackInfo<Value>& info, |
124 const PropertyCallbackInfo<Value>& info) { | 148 const SkPaint& style) { |
125 JsContext* jsContext = Unwrap(info.This()); | |
126 SkColor color = jsContext->fFillStyle.getColor(); | |
127 char buf[8]; | 149 char buf[8]; |
| 150 SkColor color = style.getColor(); |
128 sprintf(buf, "#%02X%02X%02X", SkColorGetR(color), SkColorGetG(color), | 151 sprintf(buf, "#%02X%02X%02X", SkColorGetR(color), SkColorGetG(color), |
129 SkColorGetB(color)); | 152 SkColorGetB(color)); |
130 | 153 |
131 info.GetReturnValue().Set(String::NewFromUtf8(info.GetIsolate(), buf)); | 154 info.GetReturnValue().Set(String::NewFromUtf8(info.GetIsolate(), buf)); |
132 } | 155 } |
133 | 156 |
134 void JsContext::SetFillStyle(Local<String> name, Local<Value> value, | 157 void JsContext::SetStyle(Local<String> name, Local<Value> value, |
135 const PropertyCallbackInfo<void>& info) { | 158 const PropertyCallbackInfo<void>& info, |
136 JsContext* jsContext = Unwrap(info.This()); | 159 SkPaint& style) { |
137 Local<String> s = value->ToString(); | 160 Local<String> s = value->ToString(); |
138 if (s->Length() != 7) { | 161 if (s->Length() != 7) { |
139 info.GetIsolate()->ThrowException( | 162 info.GetIsolate()->ThrowException( |
140 v8::String::NewFromUtf8( | 163 v8::String::NewFromUtf8( |
141 info.GetIsolate(), "Invalid fill style format.")); | 164 info.GetIsolate(), "Invalid fill style format.")); |
142 return; | 165 return; |
143 } | 166 } |
144 char buf[8]; | 167 char buf[8]; |
145 s->WriteUtf8(buf, sizeof(buf)); | 168 s->WriteUtf8(buf, sizeof(buf)); |
146 | 169 |
147 if (buf[0] != '#') { | 170 if (buf[0] != '#') { |
148 info.GetIsolate()->ThrowException( | 171 info.GetIsolate()->ThrowException( |
149 v8::String::NewFromUtf8( | 172 v8::String::NewFromUtf8( |
150 info.GetIsolate(), "Invalid fill style format.")); | 173 info.GetIsolate(), "Invalid fill style format.")); |
151 return; | 174 return; |
152 } | 175 } |
153 | 176 |
154 long color = strtol(buf+1, NULL, 16); | 177 long color = strtol(buf+1, NULL, 16); |
155 jsContext->fFillStyle.setColor(SkColorSetA(SkColor(color), SK_AlphaOPAQUE)); | 178 style.setColor(SkColorSetA(SkColor(color), SK_AlphaOPAQUE)); |
| 179 } |
| 180 |
| 181 void JsContext::GetFillStyle(Local<String> name, |
| 182 const PropertyCallbackInfo<Value>& info) { |
| 183 JsContext* jsContext = Unwrap(info.This()); |
| 184 GetStyle(name, info, jsContext->fFillStyle); |
| 185 } |
| 186 |
| 187 void JsContext::GetStrokeStyle(Local<String> name, |
| 188 const PropertyCallbackInfo<Value>& info) { |
| 189 JsContext* jsContext = Unwrap(info.This()); |
| 190 GetStyle(name, info, jsContext->fStrokeStyle); |
| 191 } |
| 192 |
| 193 void JsContext::SetFillStyle(Local<String> name, Local<Value> value, |
| 194 const PropertyCallbackInfo<void>& info) { |
| 195 JsContext* jsContext = Unwrap(info.This()); |
| 196 SetStyle(name, value, info, jsContext->fFillStyle); |
| 197 } |
| 198 |
| 199 void JsContext::SetStrokeStyle(Local<String> name, Local<Value> value, |
| 200 const PropertyCallbackInfo<void>& info) { |
| 201 JsContext* jsContext = Unwrap(info.This()); |
| 202 SetStyle(name, value, info, jsContext->fStrokeStyle); |
156 } | 203 } |
157 | 204 |
158 | 205 |
159 void JsContext::GetWidth(Local<String> name, | 206 void JsContext::GetWidth(Local<String> name, |
160 const PropertyCallbackInfo<Value>& info) { | 207 const PropertyCallbackInfo<Value>& info) { |
161 JsContext* jsContext = Unwrap(info.This()); | 208 JsContext* jsContext = Unwrap(info.This()); |
162 SkISize size = jsContext->fCanvas->getDeviceSize(); | 209 SkISize size = jsContext->fCanvas->getDeviceSize(); |
163 | 210 |
164 info.GetReturnValue().Set(Int32::New(size.fWidth)); | 211 info.GetReturnValue().Set(Int32::New(size.fWidth)); |
165 } | 212 } |
166 | 213 |
167 void JsContext::GetHeight(Local<String> name, | 214 void JsContext::GetHeight(Local<String> name, |
168 const PropertyCallbackInfo<Value>& info) { | 215 const PropertyCallbackInfo<Value>& info) { |
169 JsContext* jsContext = Unwrap(info.This()); | 216 JsContext* jsContext = Unwrap(info.This()); |
170 SkISize size = jsContext->fCanvas->getDeviceSize(); | 217 SkISize size = jsContext->fCanvas->getDeviceSize(); |
171 | 218 |
172 info.GetReturnValue().Set(Int32::New(size.fHeight)); | 219 info.GetReturnValue().Set(Int32::New(size.fHeight)); |
173 } | 220 } |
174 | 221 |
175 | 222 |
176 Persistent<ObjectTemplate> JsContext::gContextTemplate; | 223 Persistent<ObjectTemplate> JsContext::gContextTemplate; |
177 | 224 |
| 225 #define ADD_METHOD(name, fn) \ |
| 226 result->Set(String::NewFromUtf8( \ |
| 227 fGlobal->getIsolate(), name, \ |
| 228 String::kInternalizedString), \ |
| 229 FunctionTemplate::New(fn)) |
| 230 |
178 Handle<ObjectTemplate> JsContext::makeContextTemplate() { | 231 Handle<ObjectTemplate> JsContext::makeContextTemplate() { |
179 EscapableHandleScope handleScope(fGlobal->getIsolate()); | 232 EscapableHandleScope handleScope(fGlobal->getIsolate()); |
180 | 233 |
181 Local<ObjectTemplate> result = ObjectTemplate::New(); | 234 Local<ObjectTemplate> result = ObjectTemplate::New(); |
182 | 235 |
183 // Add a field to store the pointer to a JsContext instance. | 236 // Add a field to store the pointer to a JsContext instance. |
184 result->SetInternalFieldCount(1); | 237 result->SetInternalFieldCount(1); |
185 | 238 |
186 // Add accessors for each of the fields of the context object. | 239 // Add accessors for each of the fields of the context object. |
187 result->SetAccessor(String::NewFromUtf8( | 240 result->SetAccessor(String::NewFromUtf8( |
188 fGlobal->getIsolate(), "fillStyle", String::kInternalizedString), | 241 fGlobal->getIsolate(), "fillStyle", String::kInternalizedString), |
189 GetFillStyle, SetFillStyle); | 242 GetFillStyle, SetFillStyle); |
190 result->SetAccessor(String::NewFromUtf8( | 243 result->SetAccessor(String::NewFromUtf8( |
| 244 fGlobal->getIsolate(), "strokeStyle", String::kInternalizedString), |
| 245 GetStrokeStyle, SetStrokeStyle); |
| 246 result->SetAccessor(String::NewFromUtf8( |
191 fGlobal->getIsolate(), "width", String::kInternalizedString), | 247 fGlobal->getIsolate(), "width", String::kInternalizedString), |
192 GetWidth); | 248 GetWidth); |
193 result->SetAccessor(String::NewFromUtf8( | 249 result->SetAccessor(String::NewFromUtf8( |
194 fGlobal->getIsolate(), "height", String::kInternalizedString), | 250 fGlobal->getIsolate(), "height", String::kInternalizedString), |
195 GetHeight); | 251 GetHeight); |
196 | 252 |
197 // Add methods. | 253 // Add methods. |
198 result->Set( | 254 ADD_METHOD("fillRect", FillRect); |
199 String::NewFromUtf8( | 255 ADD_METHOD("stroke", Stroke); |
200 fGlobal->getIsolate(), "fillRect", | 256 ADD_METHOD("fill", Fill); |
201 String::kInternalizedString), | 257 ADD_METHOD("rotate", Rotate); |
202 FunctionTemplate::New(FillRect)); | 258 ADD_METHOD("save", Save); |
203 result->Set( | 259 ADD_METHOD("restore", Restore); |
204 String::NewFromUtf8( | 260 ADD_METHOD("translate", Translate); |
205 fGlobal->getIsolate(), "stroke", | 261 ADD_METHOD("resetTransform", ResetTransform); |
206 String::kInternalizedString), | |
207 FunctionTemplate::New(Stroke)); | |
208 result->Set( | |
209 String::NewFromUtf8( | |
210 fGlobal->getIsolate(), "fill", | |
211 String::kInternalizedString), | |
212 FunctionTemplate::New(Fill)); | |
213 result->Set( | |
214 String::NewFromUtf8( | |
215 fGlobal->getIsolate(), "translate", | |
216 String::kInternalizedString), | |
217 FunctionTemplate::New(Translate)); | |
218 result->Set( | |
219 String::NewFromUtf8( | |
220 fGlobal->getIsolate(), "resetTransform", | |
221 String::kInternalizedString), | |
222 FunctionTemplate::New(ResetTransform)); | |
223 | 262 |
224 // Return the result through the current handle scope. | 263 // Return the result through the current handle scope. |
225 return handleScope.Escape(result); | 264 return handleScope.Escape(result); |
226 } | 265 } |
227 | 266 |
228 | 267 |
229 // Wraps 'this' in a Javascript object. | 268 // Wraps 'this' in a Javascript object. |
230 Handle<Object> JsContext::wrap() { | 269 Handle<Object> JsContext::wrap() { |
231 // Handle scope for temporary handles. | 270 // Handle scope for temporary handles. |
232 EscapableHandleScope handleScope(fGlobal->getIsolate()); | 271 EscapableHandleScope handleScope(fGlobal->getIsolate()); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 // It is a function; cast it to a Function. | 366 // It is a function; cast it to a Function. |
328 Handle<Function> fn_fun = Handle<Function>::Cast(fn_val); | 367 Handle<Function> fn_fun = Handle<Function>::Cast(fn_val); |
329 | 368 |
330 // Store the function in a Persistent handle, since we also want that to | 369 // Store the function in a Persistent handle, since we also want that to |
331 // remain after this call returns. | 370 // remain after this call returns. |
332 fOnDraw.Reset(fGlobal->getIsolate(), fn_fun); | 371 fOnDraw.Reset(fGlobal->getIsolate(), fn_fun); |
333 | 372 |
334 return true; | 373 return true; |
335 } | 374 } |
336 | 375 |
OLD | NEW |