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

Side by Side Diff: experimental/SkV8Example/BaseContext.cpp

Issue 186783004: Factor out a BaseContext from JsContext. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 6 years, 9 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 unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright 2014 Google Inc.
3 *
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 *
8 */
9 #include <v8.h>
10
11 using namespace v8;
12
13 #include "Global.h"
14 #include "BaseContext.h"
15 #include "Path2D.h"
16 #include "SkCanvas.h"
17
18
19 BaseContext* BaseContext::Unwrap(Handle<Object> obj) {
20 Handle<External> field = Handle<External>::Cast(obj->GetInternalField(0));
21 void* ptr = field->Value();
22 return static_cast<BaseContext*>(ptr);
23 }
24
25 void BaseContext::FillRect(const v8::FunctionCallbackInfo<Value>& args) {
26 BaseContext* BaseContext = Unwrap(args.This());
27 SkCanvas* canvas = BaseContext->getCanvas();
robertphillips 2014/03/04 20:05:03 if () {\n }
jcgregorio 2014/03/04 20:29:31 Done, here and all below. On 2014/03/04 20:05:03,
28 if (NULL == canvas) { return; }
29
30 if (args.Length() != 4) {
31 args.GetIsolate()->ThrowException(
32 v8::String::NewFromUtf8(
33 args.GetIsolate(), "Error: 4 arguments required."));
34 return;
35 }
36 double x = args[0]->NumberValue();
37 double y = args[1]->NumberValue();
38 double w = args[2]->NumberValue();
39 double h = args[3]->NumberValue();
40
41 SkRect rect = {
42 SkDoubleToScalar(x),
43 SkDoubleToScalar(y),
44 SkDoubleToScalar(x) + SkDoubleToScalar(w),
45 SkDoubleToScalar(y) + SkDoubleToScalar(h)
46 };
47 canvas->drawRect(rect, BaseContext->fFillStyle);
48 }
49
50 void BaseContext::Save(const v8::FunctionCallbackInfo<Value>& args) {
51 BaseContext* BaseContext = Unwrap(args.This());
52 SkCanvas* canvas = BaseContext->getCanvas();
robertphillips 2014/03/04 20:05:03 same here
53 if (NULL == canvas) { return; }
54
55 canvas->save();
56 }
57
58 void BaseContext::Restore(const v8::FunctionCallbackInfo<Value>& args) {
59 BaseContext* BaseContext = Unwrap(args.This());
60 SkCanvas* canvas = BaseContext->getCanvas();
robertphillips 2014/03/04 20:05:03 same
61 if (NULL == canvas) { return; }
62
63 canvas->restore();
64 }
65
66 void BaseContext::Rotate(const v8::FunctionCallbackInfo<Value>& args) {
67 BaseContext* BaseContext = Unwrap(args.This());
68 SkCanvas* canvas = BaseContext->getCanvas();
69 if (NULL == canvas) { return; }
70
71 if (args.Length() != 1) {
72 args.GetIsolate()->ThrowException(
73 v8::String::NewFromUtf8(
74 args.GetIsolate(), "Error: 1 arguments required."));
75 return;
76 }
77 double angle = args[0]->NumberValue();
78 canvas->rotate(SkRadiansToDegrees(angle));
79 }
80
81 void BaseContext::Translate(const v8::FunctionCallbackInfo<Value>& args) {
82 BaseContext* BaseContext = Unwrap(args.This());
83 SkCanvas* canvas = BaseContext->getCanvas();
84 if (NULL == canvas) { return; }
85
86 if (args.Length() != 2) {
87 args.GetIsolate()->ThrowException(
88 v8::String::NewFromUtf8(
89 args.GetIsolate(), "Error: 2 arguments required."));
90 return;
91 }
92 double dx = args[0]->NumberValue();
93 double dy = args[1]->NumberValue();
94 canvas->translate(SkDoubleToScalar(dx), SkDoubleToScalar(dy));
95 }
96
97 void BaseContext::ResetTransform(const v8::FunctionCallbackInfo<Value>& args) {
98 BaseContext* BaseContext = Unwrap(args.This());
99 SkCanvas* canvas = BaseContext->getCanvas();
100 if (NULL == canvas) { return; }
101
102 canvas->resetMatrix();
103 }
104
105 void BaseContext::Stroke(const v8::FunctionCallbackInfo<Value>& args) {
106 BaseContext* BaseContext = Unwrap(args.This());
107 SkCanvas* canvas = BaseContext->getCanvas();
108 if (NULL == canvas) { return; }
109
110 if (args.Length() != 1) {
111 args.GetIsolate()->ThrowException(
112 v8::String::NewFromUtf8(
113 args.GetIsolate(), "Error: 1 arguments required."));
114 return;
115 }
116
117 Handle<External> field = Handle<External>::Cast(
118 args[0]->ToObject()->GetInternalField(0));
119 void* ptr = field->Value();
120 Path2D* path = static_cast<Path2D*>(ptr);
121
122 canvas->drawPath(path->getSkPath(), BaseContext->fStrokeStyle);
123 }
124
125 void BaseContext::Fill(const v8::FunctionCallbackInfo<Value>& args) {
126 BaseContext* BaseContext = Unwrap(args.This());
127 SkCanvas* canvas = BaseContext->getCanvas();
128 if (NULL == canvas) { return; }
129
130 if (args.Length() != 1) {
131 args.GetIsolate()->ThrowException(
132 v8::String::NewFromUtf8(
133 args.GetIsolate(), "Error: 1 arguments required."));
134 return;
135 }
136
137 Handle<External> field = Handle<External>::Cast(
138 args[0]->ToObject()->GetInternalField(0));
139 void* ptr = field->Value();
140 Path2D* path = static_cast<Path2D*>(ptr);
141
142 canvas->drawPath(path->getSkPath(), BaseContext->fFillStyle);
143 }
144
145 void BaseContext::GetStyle(Local<String> name,
146 const PropertyCallbackInfo<Value>& info,
147 const SkPaint& style) {
148 char buf[8];
149 SkColor color = style.getColor();
150 sprintf(buf, "#%02X%02X%02X", SkColorGetR(color), SkColorGetG(color),
151 SkColorGetB(color));
152
153 info.GetReturnValue().Set(String::NewFromUtf8(info.GetIsolate(), buf));
154 }
155
156 void BaseContext::SetStyle(Local<String> name, Local<Value> value,
157 const PropertyCallbackInfo<void>& info,
158 SkPaint& style) {
159 Local<String> s = value->ToString();
160 if (s->Length() != 7 && s->Length() != 9) {
161 info.GetIsolate()->ThrowException(
162 v8::String::NewFromUtf8(
163 info.GetIsolate(),
164 "Invalid fill style format length."));
165 return;
166 }
167 char buf[10];
168 s->WriteUtf8(buf, sizeof(buf));
169
170 if (buf[0] != '#') {
171 info.GetIsolate()->ThrowException(
172 v8::String::NewFromUtf8(
173 info.GetIsolate(), "Invalid fill style format."));
174 return;
175 }
176
177 // Colors can be RRGGBBAA, but SkColor uses ARGB.
178 long color = strtol(buf+1, NULL, 16);
179 uint32_t alpha = SK_AlphaOPAQUE;
180 if (s->Length() == 9) {
181 alpha = color & 0xFF;
182 color >>= 8;
183 }
184 style.setColor(SkColorSetA(SkColor(color), alpha));
185 }
186
187 void BaseContext::GetFillStyle(Local<String> name,
188 const PropertyCallbackInfo<Value>& info) {
robertphillips 2014/03/04 20:05:03 baseContext
jcgregorio 2014/03/04 20:29:31 Done, here and all below. On 2014/03/04 20:05:03,
189 BaseContext* BaseContext = Unwrap(info.This());
robertphillips 2014/03/04 20:05:03 this->
190 GetStyle(name, info, BaseContext->fFillStyle);
191 }
192
193 void BaseContext::GetStrokeStyle(Local<String> name,
194 const PropertyCallbackInfo<Value>& info) {
robertphillips 2014/03/04 20:05:03 baseContext
195 BaseContext* BaseContext = Unwrap(info.This());
robertphillips 2014/03/04 20:05:03 this->
196 GetStyle(name, info, BaseContext->fStrokeStyle);
197 }
198
199 void BaseContext::SetFillStyle(Local<String> name, Local<Value> value,
200 const PropertyCallbackInfo<void>& info) {
robertphillips 2014/03/04 20:05:03 baseContext
201 BaseContext* BaseContext = Unwrap(info.This());
robertphillips 2014/03/04 20:05:03 this->
202 SetStyle(name, value, info, BaseContext->fFillStyle);
203 }
204
205 void BaseContext::SetStrokeStyle(Local<String> name, Local<Value> value,
206 const PropertyCallbackInfo<void>& info) {
robertphillips 2014/03/04 20:05:03 baseContext
207 BaseContext* BaseContext = Unwrap(info.This());
robertphillips 2014/03/04 20:05:03 this->
208 SetStyle(name, value, info, BaseContext->fStrokeStyle);
209 }
210
211
212 void BaseContext::GetWidth(Local<String> name,
213 const PropertyCallbackInfo<Value>& info) {
robertphillips 2014/03/04 20:05:03 baseContext
214 BaseContext* BaseContext = Unwrap(info.This());
215 SkCanvas* canvas = BaseContext->getCanvas();
216 if (NULL == canvas) { return; }
217 SkISize size = canvas->getDeviceSize();
218
219 info.GetReturnValue().Set(
220 Int32::New(BaseContext->fGlobal->getIsolate(), size.fWidth));
221 }
222
223 void BaseContext::GetHeight(Local<String> name,
224 const PropertyCallbackInfo<Value>& info) {
robertphillips 2014/03/04 20:05:03 baseContext
225 BaseContext* BaseContext = Unwrap(info.This());
226 SkCanvas* canvas = BaseContext->getCanvas();
227 if (NULL == canvas) { return; }
228 SkISize size = canvas->getDeviceSize();
229
230 info.GetReturnValue().Set(
231 Int32::New(BaseContext->fGlobal->getIsolate(), size.fHeight));
232 }
233
234 #define ADD_METHOD(name, fn) \
235 tmpl->Set(String::NewFromUtf8( \
236 fGlobal->getIsolate(), name, \
237 String::kInternalizedString), \
238 FunctionTemplate::New(fGlobal->getIsolate(), fn))
239
240 void BaseContext::addAttributesAndMethods(Handle<ObjectTemplate> tmpl) {
241 HandleScope scope(fGlobal->getIsolate());
242
243 // Add accessors for each of the fields of the context object.
244 tmpl->SetAccessor(String::NewFromUtf8(
245 fGlobal->getIsolate(), "fillStyle", String::kInternalizedString),
246 GetFillStyle, SetFillStyle);
247 tmpl->SetAccessor(String::NewFromUtf8(
248 fGlobal->getIsolate(), "strokeStyle", String::kInternalizedString),
249 GetStrokeStyle, SetStrokeStyle);
250 tmpl->SetAccessor(String::NewFromUtf8(
251 fGlobal->getIsolate(), "width", String::kInternalizedString),
252 GetWidth);
253 tmpl->SetAccessor(String::NewFromUtf8(
254 fGlobal->getIsolate(), "height", String::kInternalizedString),
255 GetHeight);
256
257 // Add methods.
258 ADD_METHOD("fillRect", FillRect);
259 ADD_METHOD("stroke", Stroke);
260 ADD_METHOD("fill", Fill);
261 ADD_METHOD("rotate", Rotate);
262 ADD_METHOD("save", Save);
263 ADD_METHOD("restore", Restore);
264 ADD_METHOD("translate", Translate);
265 ADD_METHOD("resetTransform", ResetTransform);
266 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698