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

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

Issue 676423002: Start moving to the new canvas structure. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: rebase and cleaning 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
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 "DrawingMethods.h"
13 #include "Path2D.h"
14 #include "SkCanvas.h"
15
16
17 DrawingMethods* DrawingMethods::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<DrawingMethods*>(ptr);
21 }
22
23
24 void DrawingMethods::Save(const v8::FunctionCallbackInfo<v8::Value>& args) {
25 DrawingMethods* drawingMethods = Unwrap(args.This());
26 SkCanvas* canvas = drawingMethods->getCanvas();
27 if (NULL == canvas) {
28 return;
29 }
30
31 canvas->save();
32 }
33
34 void DrawingMethods::Restore(const v8::FunctionCallbackInfo<v8::Value>& args) {
35 DrawingMethods* drawingMethods = Unwrap(args.This());
36 SkCanvas* canvas = drawingMethods->getCanvas();
37 if (NULL == canvas) {
38 return;
39 }
40
41 canvas->restore();
42 }
43
44 void DrawingMethods::Rotate(const v8::FunctionCallbackInfo<v8::Value>& args) {
45 DrawingMethods* drawingMethods = Unwrap(args.This());
46 SkCanvas* canvas = drawingMethods->getCanvas();
47 if (NULL == canvas) {
48 return;
49 }
50
51 if (args.Length() != 1) {
52 args.GetIsolate()->ThrowException(
53 v8::String::NewFromUtf8(
54 args.GetIsolate(), "Error: 1 arguments required."));
55 return;
56 }
57 double angle = args[0]->NumberValue();
58 canvas->rotate(SkRadiansToDegrees(angle));
59 }
60
61 void DrawingMethods::Translate(const v8::FunctionCallbackInfo<v8::Value>& args) {
62 DrawingMethods* drawingMethods = Unwrap(args.This());
63 SkCanvas* canvas = drawingMethods->getCanvas();
64 if (NULL == canvas) {
65 return;
66 }
67
68 if (args.Length() != 2) {
69 args.GetIsolate()->ThrowException(
70 v8::String::NewFromUtf8(
71 args.GetIsolate(), "Error: 2 arguments required."));
72 return;
73 }
74 double dx = args[0]->NumberValue();
75 double dy = args[1]->NumberValue();
76 canvas->translate(SkDoubleToScalar(dx), SkDoubleToScalar(dy));
77 }
78
79 void DrawingMethods::ResetTransform(const v8::FunctionCallbackInfo<v8::Value>& a rgs) {
80 DrawingMethods* drawingMethods = Unwrap(args.This());
81 SkCanvas* canvas = drawingMethods->getCanvas();
82 if (NULL == canvas) {
83 return;
84 }
85
86 canvas->resetMatrix();
87 }
88
89 void DrawingMethods::DrawPath(const v8::FunctionCallbackInfo<v8::Value>& args) {
90 DrawingMethods* drawingMethods = Unwrap(args.This());
91 SkCanvas* canvas = drawingMethods->getCanvas();
92 if (NULL == canvas) {
93 return;
94 }
95
96 if (args.Length() != 1) {
97 args.GetIsolate()->ThrowException(
98 v8::String::NewFromUtf8(
99 args.GetIsolate(), "Error: 1 argument required."));
100 return;
101 }
102
103 v8::Handle<v8::External> field = v8::Handle<v8::External>::Cast(
104 args[0]->ToObject()->GetInternalField(0));
105 void* ptr = field->Value();
106 Path2D* path = static_cast<Path2D*>(ptr);
107 if (NULL == path) {
108 return;
109 }
110 // TODO(jcgregorio) Add support for Paint2D parameter after Paint2D is
111 // implemented.
112 SkPaint fillStyle;
113 fillStyle.setColor(SK_ColorBLACK);
114 fillStyle.setAntiAlias(true);
115 fillStyle.setStyle(SkPaint::kFill_Style);
116 canvas->drawPath(path->getSkPath(), fillStyle);
117 }
118
119
120 void DrawingMethods::GetWidth(v8::Local<v8::String> name,
robertphillips 2014/10/27 16:11:17 align this guy?
jcgregorio 2014/10/27 16:30:23 Done.
jcgregorio 2014/10/27 16:30:23 Done.
121 const v8::PropertyCallbackInfo<v8::Value>& info) {
122 DrawingMethods* drawingMethods = Unwrap(info.This());
123 SkCanvas* canvas = drawingMethods->getCanvas();
124 if (NULL == canvas) {
125 return;
126 }
robertphillips 2014/10/27 16:11:17 Should probably use imageInfo here.
jcgregorio 2014/10/27 16:30:23 Done.
127 SkISize size = canvas->getDeviceSize();
128
129 info.GetReturnValue().Set(
130 v8::Int32::New(drawingMethods->fGlobal->getIsolate(), size.fWidth));
131 }
132
133 void DrawingMethods::GetHeight(v8::Local<v8::String> name,
134 const v8::PropertyCallbackInfo<v8::Value>& info) {
135 DrawingMethods* drawingMethods = Unwrap(info.This());
136 SkCanvas* canvas = drawingMethods->getCanvas();
137 if (NULL == canvas) {
138 return;
139 }
140 SkISize size = canvas->getDeviceSize();
141
142 info.GetReturnValue().Set(
143 v8::Int32::New(drawingMethods->fGlobal->getIsolate(), size.fHeight)) ;
144 }
145
146 #define ADD_METHOD(name, fn) \
147 tmpl->Set(v8::String::NewFromUtf8( \
148 fGlobal->getIsolate(), name, \
149 v8::String::kInternalizedString), \
150 v8::FunctionTemplate::New(fGlobal->getIsolate(), fn))
151
152 void DrawingMethods::addAttributesAndMethods(v8::Handle<v8::ObjectTemplate> tmpl ) {
153 v8::HandleScope scope(fGlobal->getIsolate());
154
155 // Add accessors for each of the fields of the context object.
156 tmpl->SetAccessor(v8::String::NewFromUtf8(
157 fGlobal->getIsolate(), "width", v8::String::kInternalizedString),
158 GetWidth);
159 tmpl->SetAccessor(v8::String::NewFromUtf8(
160 fGlobal->getIsolate(), "height", v8::String::kInternalizedString),
161 GetHeight);
162
163 // Add methods.
164 ADD_METHOD("save", Save);
165 ADD_METHOD("restore", Restore);
166 ADD_METHOD("rotate", Rotate);
167 ADD_METHOD("translate", Translate);
168 ADD_METHOD("resetTransform", ResetTransform);
169
170 ADD_METHOD("drawPath", DrawPath);
171 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698