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

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

Issue 676423002: Start moving to the new canvas structure. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 6 years, 1 month 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
« no previous file with comments | « experimental/SkV8Example/BaseContext.h ('k') | experimental/SkV8Example/DrawingMethods.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #include "Global.h"
12 #include "BaseContext.h"
13 #include "Path2D.h"
14 #include "SkCanvas.h"
15
16
17 BaseContext* BaseContext::Unwrap(v8::Handle<v8::Object> obj) {
18 v8::Handle<v8::External> field = v8::Handle<v8::External>::Cast(obj->GetInte rnalField(0));
19 void* ptr = field->Value();
20 return static_cast<BaseContext*>(ptr);
21 }
22
23 void BaseContext::FillRect(const v8::FunctionCallbackInfo<v8::Value>& args) {
24 BaseContext* BaseContext = Unwrap(args.This());
25 SkCanvas* canvas = BaseContext->getCanvas();
26 if (NULL == canvas) {
27 return;
28 }
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<v8::Value>& args) {
51 BaseContext* BaseContext = Unwrap(args.This());
52 SkCanvas* canvas = BaseContext->getCanvas();
53 if (NULL == canvas) {
54 return;
55 }
56
57 canvas->save();
58 }
59
60 void BaseContext::Restore(const v8::FunctionCallbackInfo<v8::Value>& args) {
61 BaseContext* BaseContext = Unwrap(args.This());
62 SkCanvas* canvas = BaseContext->getCanvas();
63 if (NULL == canvas) {
64 return;
65 }
66
67 canvas->restore();
68 }
69
70 void BaseContext::Rotate(const v8::FunctionCallbackInfo<v8::Value>& args) {
71 BaseContext* BaseContext = Unwrap(args.This());
72 SkCanvas* canvas = BaseContext->getCanvas();
73 if (NULL == canvas) {
74 return;
75 }
76
77 if (args.Length() != 1) {
78 args.GetIsolate()->ThrowException(
79 v8::String::NewFromUtf8(
80 args.GetIsolate(), "Error: 1 arguments required."));
81 return;
82 }
83 double angle = args[0]->NumberValue();
84 canvas->rotate(SkRadiansToDegrees(angle));
85 }
86
87 void BaseContext::Translate(const v8::FunctionCallbackInfo<v8::Value>& args) {
88 BaseContext* BaseContext = Unwrap(args.This());
89 SkCanvas* canvas = BaseContext->getCanvas();
90 if (NULL == canvas) {
91 return;
92 }
93
94 if (args.Length() != 2) {
95 args.GetIsolate()->ThrowException(
96 v8::String::NewFromUtf8(
97 args.GetIsolate(), "Error: 2 arguments required."));
98 return;
99 }
100 double dx = args[0]->NumberValue();
101 double dy = args[1]->NumberValue();
102 canvas->translate(SkDoubleToScalar(dx), SkDoubleToScalar(dy));
103 }
104
105 void BaseContext::ResetTransform(const v8::FunctionCallbackInfo<v8::Value>& args ) {
106 BaseContext* BaseContext = Unwrap(args.This());
107 SkCanvas* canvas = BaseContext->getCanvas();
108 if (NULL == canvas) {
109 return;
110 }
111
112 canvas->resetMatrix();
113 }
114
115 void BaseContext::Stroke(const v8::FunctionCallbackInfo<v8::Value>& args) {
116 BaseContext* BaseContext = Unwrap(args.This());
117 SkCanvas* canvas = BaseContext->getCanvas();
118 if (NULL == canvas) {
119 return;
120 }
121
122 if (args.Length() != 1) {
123 args.GetIsolate()->ThrowException(
124 v8::String::NewFromUtf8(
125 args.GetIsolate(), "Error: 1 arguments required."));
126 return;
127 }
128
129 v8::Handle<v8::External> field = v8::Handle<v8::External>::Cast(
130 args[0]->ToObject()->GetInternalField(0));
131 void* ptr = field->Value();
132 Path2D* path = static_cast<Path2D*>(ptr);
133
134 canvas->drawPath(path->getSkPath(), BaseContext->fStrokeStyle);
135 }
136
137 void BaseContext::Fill(const v8::FunctionCallbackInfo<v8::Value>& args) {
138 BaseContext* BaseContext = Unwrap(args.This());
139 SkCanvas* canvas = BaseContext->getCanvas();
140 if (NULL == canvas) {
141 return;
142 }
143
144 if (args.Length() != 1) {
145 args.GetIsolate()->ThrowException(
146 v8::String::NewFromUtf8(
147 args.GetIsolate(), "Error: 1 arguments required."));
148 return;
149 }
150
151 v8::Handle<v8::External> field = v8::Handle<v8::External>::Cast(
152 args[0]->ToObject()->GetInternalField(0));
153 void* ptr = field->Value();
154 Path2D* path = static_cast<Path2D*>(ptr);
155
156 canvas->drawPath(path->getSkPath(), BaseContext->fFillStyle);
157 }
158
159 void BaseContext::GetStyle(v8::Local<v8::String> name,
160 const v8::PropertyCallbackInfo<v8::Value>& info,
161 const SkPaint& style) {
162 char buf[8];
163 SkColor color = style.getColor();
164 sprintf(buf, "#%02X%02X%02X", SkColorGetR(color), SkColorGetG(color),
165 SkColorGetB(color));
166
167 info.GetReturnValue().Set(v8::String::NewFromUtf8(info.GetIsolate(), buf));
168 }
169
170 void BaseContext::SetStyle(v8::Local<v8::String> name, v8::Local<v8::Value> valu e,
171 const v8::PropertyCallbackInfo<void>& info,
172 SkPaint& style) {
173 v8::Local<v8::String> s = value->ToString();
174 if (s->Length() != 7 && s->Length() != 9) {
175 info.GetIsolate()->ThrowException(
176 v8::String::NewFromUtf8(
177 info.GetIsolate(),
178 "Invalid fill style format length."));
179 return;
180 }
181 char buf[10];
182 s->WriteUtf8(buf, sizeof(buf));
183
184 if (buf[0] != '#') {
185 info.GetIsolate()->ThrowException(
186 v8::String::NewFromUtf8(
187 info.GetIsolate(), "Invalid fill style format."));
188 return;
189 }
190
191 // Colors can be RRGGBBAA, but SkColor uses ARGB.
192 long color = strtol(buf+1, NULL, 16);
193 uint32_t alpha = SK_AlphaOPAQUE;
194 if (s->Length() == 9) {
195 alpha = color & 0xFF;
196 color >>= 8;
197 }
198 style.setColor(SkColorSetA(SkColor(color), alpha));
199 }
200
201 void BaseContext::GetFillStyle(v8::Local<v8::String> name,
202 const v8::PropertyCallbackInfo<v8::Value>& info) {
203 BaseContext* baseContext = Unwrap(info.This());
204 GetStyle(name, info, baseContext->fFillStyle);
205 }
206
207 void BaseContext::GetStrokeStyle(v8::Local<v8::String> name,
208 const v8::PropertyCallbackInfo<v8::Value>& info) {
209 BaseContext* baseContext = Unwrap(info.This());
210 GetStyle(name, info, baseContext->fStrokeStyle);
211 }
212
213 void BaseContext::SetFillStyle(v8::Local<v8::String> name, v8::Local<v8::Value> value,
214 const v8::PropertyCallbackInfo<void>& info) {
215 BaseContext* baseContext = Unwrap(info.This());
216 SetStyle(name, value, info, baseContext->fFillStyle);
217 }
218
219 void BaseContext::SetStrokeStyle(v8::Local<v8::String> name, v8::Local<v8::Value > value,
220 const v8::PropertyCallbackInfo<void>& info) {
221 BaseContext* baseContext = Unwrap(info.This());
222 SetStyle(name, value, info, baseContext->fStrokeStyle);
223 }
224
225
226 void BaseContext::GetWidth(v8::Local<v8::String> name,
227 const v8::PropertyCallbackInfo<v8::Value>& info) {
228 BaseContext* baseContext = Unwrap(info.This());
229 SkCanvas* canvas = baseContext->getCanvas();
230 if (NULL == canvas) {
231 return;
232 }
233 SkISize size = canvas->getDeviceSize();
234
235 info.GetReturnValue().Set(
236 v8::Int32::New(baseContext->fGlobal->getIsolate(), size.fWidth));
237 }
238
239 void BaseContext::GetHeight(v8::Local<v8::String> name,
240 const v8::PropertyCallbackInfo<v8::Value>& info) {
241 BaseContext* baseContext = Unwrap(info.This());
242 SkCanvas* canvas = baseContext->getCanvas();
243 if (NULL == canvas) {
244 return;
245 }
246 SkISize size = canvas->getDeviceSize();
247
248 info.GetReturnValue().Set(
249 v8::Int32::New(baseContext->fGlobal->getIsolate(), size.fHeight));
250 }
251
252 #define ADD_METHOD(name, fn) \
253 tmpl->Set(v8::String::NewFromUtf8( \
254 fGlobal->getIsolate(), name, \
255 v8::String::kInternalizedString), \
256 v8::FunctionTemplate::New(fGlobal->getIsolate(), fn))
257
258 void BaseContext::addAttributesAndMethods(v8::Handle<v8::ObjectTemplate> tmpl) {
259 v8::HandleScope scope(fGlobal->getIsolate());
260
261 // Add accessors for each of the fields of the context object.
262 tmpl->SetAccessor(v8::String::NewFromUtf8(
263 fGlobal->getIsolate(), "fillStyle", v8::String::kInternalizedString),
264 GetFillStyle, SetFillStyle);
265 tmpl->SetAccessor(v8::String::NewFromUtf8(
266 fGlobal->getIsolate(), "strokeStyle", v8::String::kInternalizedString),
267 GetStrokeStyle, SetStrokeStyle);
268 tmpl->SetAccessor(v8::String::NewFromUtf8(
269 fGlobal->getIsolate(), "width", v8::String::kInternalizedString),
270 GetWidth);
271 tmpl->SetAccessor(v8::String::NewFromUtf8(
272 fGlobal->getIsolate(), "height", v8::String::kInternalizedString),
273 GetHeight);
274
275 // Add methods.
276 ADD_METHOD("fillRect", FillRect);
277 ADD_METHOD("stroke", Stroke);
278 ADD_METHOD("fill", Fill);
279 ADD_METHOD("rotate", Rotate);
280 ADD_METHOD("save", Save);
281 ADD_METHOD("restore", Restore);
282 ADD_METHOD("translate", Translate);
283 ADD_METHOD("resetTransform", ResetTransform);
284 }
OLDNEW
« no previous file with comments | « experimental/SkV8Example/BaseContext.h ('k') | experimental/SkV8Example/DrawingMethods.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698