blob: a2876d9f406e8b04b97519e812168dc490d7d5eb [file] [log] [blame]
Kevin Lubick22647d02018-07-06 14:31:23 -04001/*
2 * Copyright 2018 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkFloatingPoint.h"
9#include "SkParsePath.h"
10#include "SkPath.h"
11#include "SkPathOps.h"
12#include "SkString.h"
13
14#include <emscripten/emscripten.h>
15#include <emscripten/bind.h>
16
17using namespace emscripten;
18
19static const int MOVE = 0;
20static const int LINE = 1;
21static const int QUAD = 2;
22static const int CUBIC = 4;
23static const int CLOSE = 5;
24
25// =================================================================================
26// Creating/Exporting Paths
27// =================================================================================
28
Florin Malitae1824da2018-07-12 10:33:39 -040029template <typename VisitFunc>
30void VisitPath(const SkPath& p, VisitFunc&& f) {
31 SkPath::RawIter iter(p);
Kevin Lubick22647d02018-07-06 14:31:23 -040032 SkPoint pts[4];
33 SkPath::Verb verb;
Florin Malitae1824da2018-07-12 10:33:39 -040034 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
35 f(verb, pts);
Kevin Lubick22647d02018-07-06 14:31:23 -040036 }
37}
38
Florin Malitae1824da2018-07-12 10:33:39 -040039void EMSCRIPTEN_KEEPALIVE SkPathToVerbsArgsArray(const SkPath& path,
40 emscripten::val /*Array*/ verbs,
41 emscripten::val /*Array*/ args) {
42 VisitPath(path, [&verbs, &args](SkPath::Verb verb, const SkPoint pts[4]) {
43 switch (verb) {
44 case SkPath::kMove_Verb:
45 verbs.call<void>("push", MOVE);
46 args.call<void>("push", pts[0].x(), pts[0].y());
47 break;
48 case SkPath::kLine_Verb:
49 verbs.call<void>("push", LINE);
50 args.call<void>("push", pts[1].x(), pts[1].y());
51 break;
52 case SkPath::kQuad_Verb:
53 verbs.call<void>("push", QUAD);
54 args.call<void>("push", pts[1].x(), pts[1].y(), pts[2].x(), pts[2].y());
55 break;
56 case SkPath::kConic_Verb:
57 printf("unsupported conic verb\n");
58 // TODO(kjlubick): Port in the logic from SkParsePath::ToSVGString?
59 break;
60 case SkPath::kCubic_Verb:
61 verbs.call<void>("push", CUBIC);
62 args.call<void>("push",
63 pts[1].x(), pts[1].y(),
64 pts[2].x(), pts[2].y(),
65 pts[3].x(), pts[3].y());
66 break;
67 case SkPath::kClose_Verb:
68 verbs.call<void>("push", CLOSE);
69 break;
70 case SkPath::kDone_Verb:
71 SkASSERT(false);
72 break;
73 }
74 });
75}
76
Kevin Lubick22647d02018-07-06 14:31:23 -040077emscripten::val JSArray = emscripten::val::global("Array");
78
79emscripten::val EMSCRIPTEN_KEEPALIVE SkPathToCmdArray(SkPath path) {
80 val cmds = JSArray.new_();
81
Florin Malitae1824da2018-07-12 10:33:39 -040082 VisitPath(path, [&cmds](SkPath::Verb verb, const SkPoint pts[4]) {
Kevin Lubick22647d02018-07-06 14:31:23 -040083 val cmd = JSArray.new_();
84 switch (verb) {
Florin Malitae1824da2018-07-12 10:33:39 -040085 case SkPath::kMove_Verb:
86 cmd.call<void>("push", MOVE, pts[0].x(), pts[0].y());
87 break;
88 case SkPath::kLine_Verb:
89 cmd.call<void>("push", LINE, pts[1].x(), pts[1].y());
90 break;
91 case SkPath::kQuad_Verb:
92 cmd.call<void>("push", QUAD, pts[1].x(), pts[1].y(), pts[2].x(), pts[2].y());
93 break;
94 case SkPath::kConic_Verb:
95 printf("unsupported conic verb\n");
96 // TODO(kjlubick): Port in the logic from SkParsePath::ToSVGString?
97 break;
98 case SkPath::kCubic_Verb:
99 cmd.call<void>("push", CUBIC,
100 pts[1].x(), pts[1].y(),
101 pts[2].x(), pts[2].y(),
102 pts[3].x(), pts[3].y());
103 break;
104 case SkPath::kClose_Verb:
105 cmd.call<void>("push", CLOSE);
106 break;
107 case SkPath::kDone_Verb:
108 SkASSERT(false);
109 break;
Kevin Lubick22647d02018-07-06 14:31:23 -0400110 }
111 cmds.call<void>("push", cmd);
Florin Malitae1824da2018-07-12 10:33:39 -0400112 });
Kevin Lubick22647d02018-07-06 14:31:23 -0400113 return cmds;
114}
115
116// This type signature is a mess, but it's necessary. See, we can't use "bind" (EMSCRIPTEN_BINDINGS)
117// and pointers to primitive types (Only bound types like SkPoint). We could if we used
118// cwrap (see https://becominghuman.ai/passing-and-returning-webassembly-array-parameters-a0f572c65d97)
119// but that requires us to stick to C code and, AFAIK, doesn't allow us to return nice things like
120// SkPath or SkOpBuilder.
121//
122// So, basically, if we are using C++ and EMSCRIPTEN_BINDINGS, we can't have primative pointers
123// in our function type signatures. (this gives an error message like "Cannot call foo due to unbound
124// types Pi, Pf"). But, we can just pretend they are numbers and cast them to be pointers and
125// the compiler is happy.
Florin Malitae1824da2018-07-12 10:33:39 -0400126SkPath EMSCRIPTEN_KEEPALIVE SkPathFromVerbsArgsTyped(uintptr_t /* uint8_t* */ vptr, int numVerbs,
127 uintptr_t /* float* */ aptr, int numArgs) {
128 const auto* verbs = reinterpret_cast<const uint8_t*>(vptr);
129 const auto* args = reinterpret_cast<const float*>(aptr);
Kevin Lubick22647d02018-07-06 14:31:23 -0400130 SkPath path;
131 int argsIndex = 0;
132 float x1, y1, x2, y2, x3, y3;
133
134 // if there are not enough arguments, bail with the path we've constructed so far.
135 #define CHECK_NUM_ARGS(n) \
136 if ((argsIndex + n) > numArgs) { \
137 SkDebugf("Not enough args to match the verbs. Saw %d args\n", numArgs); \
138 return path; \
139 }
140
141 for(int i = 0; i < numVerbs; i++){
142 switch (verbs[i]) {
143 case MOVE:
144 CHECK_NUM_ARGS(2);
145 x1 = args[argsIndex++], y1 = args[argsIndex++];
146 path.moveTo(x1, y1);
147 break;
148 case LINE:
149 CHECK_NUM_ARGS(2);
150 x1 = args[argsIndex++], y1 = args[argsIndex++];
151 path.lineTo(x1, y1);
152 break;
153 case QUAD:
154 CHECK_NUM_ARGS(4);
155 x1 = args[argsIndex++], y1 = args[argsIndex++];
156 x2 = args[argsIndex++], y2 = args[argsIndex++];
157 path.quadTo(x1, y1, x2, y2);
158 break;
159 case CUBIC:
160 CHECK_NUM_ARGS(6);
161 x1 = args[argsIndex++], y1 = args[argsIndex++];
162 x2 = args[argsIndex++], y2 = args[argsIndex++];
163 x3 = args[argsIndex++], y3 = args[argsIndex++];
164 path.cubicTo(x1, y1, x2, y2, x3, y3);
165 break;
166 case CLOSE:
167 path.close();
168 break;
169 default:
170 SkDebugf(" path: UNKNOWN VERB %d, aborting dump...\n", verbs[i]);
171 return path;
172 }
173 }
174
175 #undef CHECK_NUM_ARGS
176
177 return path;
178}
179
180// See above comment for rational of pointer mess
Florin Malitae1824da2018-07-12 10:33:39 -0400181SkPath EMSCRIPTEN_KEEPALIVE SkPathFromCmdTyped(uintptr_t /* float* */ cptr, int numCmds) {
182 const auto* cmds = reinterpret_cast<const float*>(cptr);
Kevin Lubick22647d02018-07-06 14:31:23 -0400183 SkPath path;
184 float x1, y1, x2, y2, x3, y3;
185
186 // if there are not enough arguments, bail with the path we've constructed so far.
187 #define CHECK_NUM_ARGS(n) \
188 if ((i + n) > numCmds) { \
189 SkDebugf("Not enough args to match the verbs. Saw %d commands\n", numCmds); \
190 return path; \
191 }
192
193 for(int i = 0; i < numCmds;){
194 switch (sk_float_floor2int(cmds[i++])) {
195 case MOVE:
196 CHECK_NUM_ARGS(2);
197 x1 = cmds[i++], y1 = cmds[i++];
198 path.moveTo(x1, y1);
199 break;
200 case LINE:
201 CHECK_NUM_ARGS(2);
202 x1 = cmds[i++], y1 = cmds[i++];
203 path.lineTo(x1, y1);
204 break;
205 case QUAD:
206 CHECK_NUM_ARGS(4);
207 x1 = cmds[i++], y1 = cmds[i++];
208 x2 = cmds[i++], y2 = cmds[i++];
209 path.quadTo(x1, y1, x2, y2);
210 break;
211 case CUBIC:
212 CHECK_NUM_ARGS(6);
213 x1 = cmds[i++], y1 = cmds[i++];
214 x2 = cmds[i++], y2 = cmds[i++];
215 x3 = cmds[i++], y3 = cmds[i++];
216 path.cubicTo(x1, y1, x2, y2, x3, y3);
217 break;
218 case CLOSE:
219 path.close();
220 break;
221 default:
222 SkDebugf(" path: UNKNOWN command %f, aborting dump...\n", cmds[i-1]);
223 return path;
224 }
225 }
226
227 #undef CHECK_NUM_ARGS
228
229 return path;
230}
231
232//========================================================================================
233// SVG THINGS
234//========================================================================================
235
236val EMSCRIPTEN_KEEPALIVE ToSVGString(SkPath path) {
237 SkString s;
238 SkParsePath::ToSVGString(path, &s);
239 // Wrapping it in val automatically turns it into a JS string.
240 // Not too sure on performance implications, but is is simpler than
241 // returning a raw pointer to const char * and then using
242 // Pointer_stringify() on the calling side.
243 return val(s.c_str());
244}
245
246
247SkPath EMSCRIPTEN_KEEPALIVE FromSVGString(std::string str) {
248 SkPath path;
249 SkParsePath::FromSVGString(str.c_str(), &path);
250 return path;
251}
252
253//========================================================================================
254// PATHOP THINGS
255//========================================================================================
256
257SkPath EMSCRIPTEN_KEEPALIVE SimplifyPath(SkPath path) {
258 SkPath simple;
259 Simplify(path, &simple);
260 return simple;
261}
262
263SkPath EMSCRIPTEN_KEEPALIVE ApplyPathOp(SkPath pathOne, SkPath pathTwo, SkPathOp op) {
264 SkPath path;
265 Op(pathOne, pathTwo, op, &path);
266 return path;
267}
268
269SkPath EMSCRIPTEN_KEEPALIVE ResolveBuilder(SkOpBuilder builder) {
270 SkPath path;
271 builder.resolve(&path);
272 return path;
273}
274
275//========================================================================================
276// Canvas THINGS
277//========================================================================================
278
279emscripten::val EMSCRIPTEN_KEEPALIVE ToPath2D(SkPath path, val/* Path2D&*/ retVal) {
280 SkPath::Iter iter(path, false);
281 SkPoint pts[4];
282 SkPath::Verb verb;
283 while ((verb = iter.next(pts, false)) != SkPath::kDone_Verb) {
284 switch (verb) {
285 case SkPath::kMove_Verb:
286 retVal.call<void>("moveTo", pts[0].x(), pts[0].y());
287 break;
288 case SkPath::kLine_Verb:
289 retVal.call<void>("lineTo", pts[1].x(), pts[1].y());
290 break;
291 case SkPath::kQuad_Verb:
292 retVal.call<void>("quadraticCurveTo", pts[1].x(), pts[1].y(), pts[2].x(), pts[2].y());
293 break;
294 case SkPath::kConic_Verb:
295 printf("unsupported conic verb\n");
296 // TODO(kjlubick): Port in the logic from SkParsePath::ToSVGString?
297 break;
298 case SkPath::kCubic_Verb:
299 retVal.call<void>("bezierCurveTo", pts[1].x(), pts[1].y(), pts[2].x(), pts[2].y(),
300 pts[3].x(), pts[3].y());
301 break;
302 case SkPath::kClose_Verb:
303 retVal.call<void>("closePath");
304 break;
305 case SkPath::kDone_Verb:
306 break;
307 }
308 }
309 return retVal;
310}
311
312// Binds the classes to the JS
313EMSCRIPTEN_BINDINGS(skia) {
314 class_<SkPath>("SkPath")
315 .constructor<>()
316
317 .function("moveTo",
318 select_overload<void(SkScalar, SkScalar)>(&SkPath::moveTo))
319 .function("lineTo",
320 select_overload<void(SkScalar, SkScalar)>(&SkPath::lineTo))
321 .function("quadTo",
322 select_overload<void(SkScalar, SkScalar, SkScalar, SkScalar)>(&SkPath::quadTo))
323 .function("cubicTo",
324 select_overload<void(SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar)>(&SkPath::cubicTo))
325 .function("close", &SkPath::close);
326 // Uncomment below for debugging.
327 //.function("dump", select_overload<void() const>(&SkPath::dump));
328
329 class_<SkOpBuilder>("SkOpBuilder")
330 .constructor<>()
331
332 .function("add", &SkOpBuilder::add);
333
334 // Without this, module._ToPath2D (yes with an underscore)
335 // would be exposed, but be unable to correctly handle the SkPath type.
336 function("ToPath2D", &ToPath2D);
337 function("ToSVGString", &ToSVGString);
338 function("FromSVGString", &FromSVGString);
339
340 function("SkPathToVerbsArgsArray", &SkPathToVerbsArgsArray);
341 function("SkPathFromVerbsArgsTyped", &SkPathFromVerbsArgsTyped);
342
343 function("SkPathFromCmdTyped", &SkPathFromCmdTyped);
344 function("SkPathToCmdArray", &SkPathToCmdArray);
345
346 function("SimplifyPath", &SimplifyPath);
347 function("ApplyPathOp", &ApplyPathOp);
348 function("ResolveBuilder", &ResolveBuilder);
349
350 enum_<SkPathOp>("PathOp")
351 .value("DIFFERENCE", SkPathOp::kDifference_SkPathOp)
352 .value("INTERSECT", SkPathOp::kIntersect_SkPathOp)
353 .value("UNION", SkPathOp::kUnion_SkPathOp)
354 .value("XOR", SkPathOp::kXOR_SkPathOp)
355 .value("REVERSE_DIFFERENCE", SkPathOp::kReverseDifference_SkPathOp);
356
357 constant("MOVE_VERB", MOVE);
358 constant("LINE_VERB", LINE);
359 constant("QUAD_VERB", QUAD);
360 constant("CUBIC_VERB", CUBIC);
361 constant("CLOSE_VERB", CLOSE);
362}