blob: dd6245ebdeac4a4ce01615ba902c5530b0a4b92d [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
Kevin Lubick92eaa3c2018-07-16 21:00:52 -04008#include "SkFloatBits.h"
Kevin Lubick22647d02018-07-06 14:31:23 -04009#include "SkFloatingPoint.h"
10#include "SkParsePath.h"
11#include "SkPath.h"
12#include "SkPathOps.h"
Kevin Lubick92eaa3c2018-07-16 21:00:52 -040013#include "SkRegion.h"
Kevin Lubick22647d02018-07-06 14:31:23 -040014#include "SkString.h"
15
16#include <emscripten/emscripten.h>
17#include <emscripten/bind.h>
18
19using namespace emscripten;
20
21static const int MOVE = 0;
22static const int LINE = 1;
23static const int QUAD = 2;
24static const int CUBIC = 4;
25static const int CLOSE = 5;
26
27// =================================================================================
28// Creating/Exporting Paths
29// =================================================================================
30
Florin Malitae1824da2018-07-12 10:33:39 -040031template <typename VisitFunc>
32void VisitPath(const SkPath& p, VisitFunc&& f) {
33 SkPath::RawIter iter(p);
Kevin Lubick22647d02018-07-06 14:31:23 -040034 SkPoint pts[4];
35 SkPath::Verb verb;
Florin Malitae1824da2018-07-12 10:33:39 -040036 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
37 f(verb, pts);
Kevin Lubick22647d02018-07-06 14:31:23 -040038 }
39}
40
41emscripten::val JSArray = emscripten::val::global("Array");
42
Kevin Lubicke1b36fe2018-08-02 11:30:33 -040043emscripten::val EMSCRIPTEN_KEEPALIVE ToCmds(SkPath path) {
Kevin Lubick22647d02018-07-06 14:31:23 -040044 val cmds = JSArray.new_();
45
Florin Malitae1824da2018-07-12 10:33:39 -040046 VisitPath(path, [&cmds](SkPath::Verb verb, const SkPoint pts[4]) {
Kevin Lubick22647d02018-07-06 14:31:23 -040047 val cmd = JSArray.new_();
48 switch (verb) {
Florin Malitae1824da2018-07-12 10:33:39 -040049 case SkPath::kMove_Verb:
50 cmd.call<void>("push", MOVE, pts[0].x(), pts[0].y());
51 break;
52 case SkPath::kLine_Verb:
53 cmd.call<void>("push", LINE, pts[1].x(), pts[1].y());
54 break;
55 case SkPath::kQuad_Verb:
56 cmd.call<void>("push", QUAD, pts[1].x(), pts[1].y(), pts[2].x(), pts[2].y());
57 break;
58 case SkPath::kConic_Verb:
59 printf("unsupported conic verb\n");
60 // TODO(kjlubick): Port in the logic from SkParsePath::ToSVGString?
61 break;
62 case SkPath::kCubic_Verb:
63 cmd.call<void>("push", CUBIC,
64 pts[1].x(), pts[1].y(),
65 pts[2].x(), pts[2].y(),
66 pts[3].x(), pts[3].y());
67 break;
68 case SkPath::kClose_Verb:
69 cmd.call<void>("push", CLOSE);
70 break;
71 case SkPath::kDone_Verb:
72 SkASSERT(false);
73 break;
Kevin Lubick22647d02018-07-06 14:31:23 -040074 }
75 cmds.call<void>("push", cmd);
Florin Malitae1824da2018-07-12 10:33:39 -040076 });
Kevin Lubick22647d02018-07-06 14:31:23 -040077 return cmds;
78}
79
80// This type signature is a mess, but it's necessary. See, we can't use "bind" (EMSCRIPTEN_BINDINGS)
81// and pointers to primitive types (Only bound types like SkPoint). We could if we used
82// cwrap (see https://becominghuman.ai/passing-and-returning-webassembly-array-parameters-a0f572c65d97)
83// but that requires us to stick to C code and, AFAIK, doesn't allow us to return nice things like
84// SkPath or SkOpBuilder.
85//
86// So, basically, if we are using C++ and EMSCRIPTEN_BINDINGS, we can't have primative pointers
87// in our function type signatures. (this gives an error message like "Cannot call foo due to unbound
88// types Pi, Pf"). But, we can just pretend they are numbers and cast them to be pointers and
89// the compiler is happy.
Kevin Lubicke1b36fe2018-08-02 11:30:33 -040090SkPath EMSCRIPTEN_KEEPALIVE FromCmds(uintptr_t /* float* */ cptr, int numCmds) {
Florin Malitae1824da2018-07-12 10:33:39 -040091 const auto* cmds = reinterpret_cast<const float*>(cptr);
Kevin Lubick22647d02018-07-06 14:31:23 -040092 SkPath path;
93 float x1, y1, x2, y2, x3, y3;
94
95 // if there are not enough arguments, bail with the path we've constructed so far.
96 #define CHECK_NUM_ARGS(n) \
97 if ((i + n) > numCmds) { \
98 SkDebugf("Not enough args to match the verbs. Saw %d commands\n", numCmds); \
99 return path; \
100 }
101
102 for(int i = 0; i < numCmds;){
103 switch (sk_float_floor2int(cmds[i++])) {
104 case MOVE:
105 CHECK_NUM_ARGS(2);
106 x1 = cmds[i++], y1 = cmds[i++];
107 path.moveTo(x1, y1);
108 break;
109 case LINE:
110 CHECK_NUM_ARGS(2);
111 x1 = cmds[i++], y1 = cmds[i++];
112 path.lineTo(x1, y1);
113 break;
114 case QUAD:
115 CHECK_NUM_ARGS(4);
116 x1 = cmds[i++], y1 = cmds[i++];
117 x2 = cmds[i++], y2 = cmds[i++];
118 path.quadTo(x1, y1, x2, y2);
119 break;
120 case CUBIC:
121 CHECK_NUM_ARGS(6);
122 x1 = cmds[i++], y1 = cmds[i++];
123 x2 = cmds[i++], y2 = cmds[i++];
124 x3 = cmds[i++], y3 = cmds[i++];
125 path.cubicTo(x1, y1, x2, y2, x3, y3);
126 break;
127 case CLOSE:
128 path.close();
129 break;
130 default:
131 SkDebugf(" path: UNKNOWN command %f, aborting dump...\n", cmds[i-1]);
132 return path;
133 }
134 }
135
136 #undef CHECK_NUM_ARGS
137
138 return path;
139}
140
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400141SkPath EMSCRIPTEN_KEEPALIVE NewPath() {
142 return SkPath();
143}
144
Kevin Lubick22647d02018-07-06 14:31:23 -0400145//========================================================================================
146// SVG THINGS
147//========================================================================================
148
149val EMSCRIPTEN_KEEPALIVE ToSVGString(SkPath path) {
150 SkString s;
151 SkParsePath::ToSVGString(path, &s);
152 // Wrapping it in val automatically turns it into a JS string.
153 // Not too sure on performance implications, but is is simpler than
154 // returning a raw pointer to const char * and then using
155 // Pointer_stringify() on the calling side.
156 return val(s.c_str());
157}
158
159
160SkPath EMSCRIPTEN_KEEPALIVE FromSVGString(std::string str) {
161 SkPath path;
162 SkParsePath::FromSVGString(str.c_str(), &path);
163 return path;
164}
165
166//========================================================================================
167// PATHOP THINGS
168//========================================================================================
169
170SkPath EMSCRIPTEN_KEEPALIVE SimplifyPath(SkPath path) {
171 SkPath simple;
172 Simplify(path, &simple);
173 return simple;
174}
175
176SkPath EMSCRIPTEN_KEEPALIVE ApplyPathOp(SkPath pathOne, SkPath pathTwo, SkPathOp op) {
177 SkPath path;
178 Op(pathOne, pathTwo, op, &path);
179 return path;
180}
181
182SkPath EMSCRIPTEN_KEEPALIVE ResolveBuilder(SkOpBuilder builder) {
183 SkPath path;
184 builder.resolve(&path);
185 return path;
186}
187
188//========================================================================================
189// Canvas THINGS
190//========================================================================================
191
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400192void EMSCRIPTEN_KEEPALIVE ToCanvas(SkPath path, val/* Path2D or Canvas*/ ctx) {
Kevin Lubick22647d02018-07-06 14:31:23 -0400193 SkPath::Iter iter(path, false);
194 SkPoint pts[4];
195 SkPath::Verb verb;
196 while ((verb = iter.next(pts, false)) != SkPath::kDone_Verb) {
197 switch (verb) {
198 case SkPath::kMove_Verb:
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400199 ctx.call<void>("moveTo", pts[0].x(), pts[0].y());
Kevin Lubick22647d02018-07-06 14:31:23 -0400200 break;
201 case SkPath::kLine_Verb:
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400202 ctx.call<void>("lineTo", pts[1].x(), pts[1].y());
Kevin Lubick22647d02018-07-06 14:31:23 -0400203 break;
204 case SkPath::kQuad_Verb:
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400205 ctx.call<void>("quadraticCurveTo", pts[1].x(), pts[1].y(), pts[2].x(), pts[2].y());
Kevin Lubick22647d02018-07-06 14:31:23 -0400206 break;
207 case SkPath::kConic_Verb:
208 printf("unsupported conic verb\n");
209 // TODO(kjlubick): Port in the logic from SkParsePath::ToSVGString?
210 break;
211 case SkPath::kCubic_Verb:
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400212 ctx.call<void>("bezierCurveTo", pts[1].x(), pts[1].y(), pts[2].x(), pts[2].y(),
Kevin Lubick22647d02018-07-06 14:31:23 -0400213 pts[3].x(), pts[3].y());
214 break;
215 case SkPath::kClose_Verb:
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400216 ctx.call<void>("closePath");
Kevin Lubick22647d02018-07-06 14:31:23 -0400217 break;
218 case SkPath::kDone_Verb:
219 break;
220 }
221 }
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400222}
223
224emscripten::val JSPath2D = emscripten::val::global("Path2D");
225
226emscripten::val EMSCRIPTEN_KEEPALIVE ToPath2D(SkPath path) {
227 val retVal = JSPath2D.new_();
228 ToCanvas(path, retVal);
Kevin Lubick22647d02018-07-06 14:31:23 -0400229 return retVal;
230}
231
Kevin Lubick92eaa3c2018-07-16 21:00:52 -0400232//========================================================================================
233// Region things
234//========================================================================================
235
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400236#ifdef PATHKIT_TESTING
Kevin Lubick92eaa3c2018-07-16 21:00:52 -0400237SkPath GetBoundaryPathFromRegion(SkRegion region) {
238 SkPath p;
239 region.getBoundaryPath(&p);
240 return p;
241}
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400242#endif
Kevin Lubick92eaa3c2018-07-16 21:00:52 -0400243
Kevin Lubick22647d02018-07-06 14:31:23 -0400244// Binds the classes to the JS
245EMSCRIPTEN_BINDINGS(skia) {
246 class_<SkPath>("SkPath")
247 .constructor<>()
248
249 .function("moveTo",
250 select_overload<void(SkScalar, SkScalar)>(&SkPath::moveTo))
251 .function("lineTo",
252 select_overload<void(SkScalar, SkScalar)>(&SkPath::lineTo))
253 .function("quadTo",
254 select_overload<void(SkScalar, SkScalar, SkScalar, SkScalar)>(&SkPath::quadTo))
255 .function("cubicTo",
256 select_overload<void(SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar)>(&SkPath::cubicTo))
Kevin Lubick92eaa3c2018-07-16 21:00:52 -0400257 .function("close", &SkPath::close)
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400258#ifdef PATHKIT_TESTING
259 .function("dump", select_overload<void() const>(&SkPath::dump))
260#endif
261 ;
Kevin Lubick22647d02018-07-06 14:31:23 -0400262
263 class_<SkOpBuilder>("SkOpBuilder")
264 .constructor<>()
265
266 .function("add", &SkOpBuilder::add);
267
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400268
269 // Without these function() bindings, the function would be exposed but oblivious to
270 // our types (e.g. SkPath)
271
272 // Import
273 function("FromSVGString", &FromSVGString);
274 function("FromCmds", &FromCmds);
275 function("NewPath", &NewPath);
276 // Path2D is opaque, so we can't read in from it.
277
278 // Export
279 function("ToPath2D", &ToPath2D);
280 function("ToCanvas", &ToCanvas);
281 function("ToSVGString", &ToSVGString);
282 function("ToCmds", &ToCmds);
283
284 // PathOps
285 function("SimplifyPath", &SimplifyPath);
286 function("ApplyPathOp", &ApplyPathOp);
287 function("ResolveBuilder", &ResolveBuilder);
288
289 enum_<SkPathOp>("PathOp")
290 .value("DIFFERENCE", SkPathOp::kDifference_SkPathOp)
291 .value("INTERSECT", SkPathOp::kIntersect_SkPathOp)
292 .value("UNION", SkPathOp::kUnion_SkPathOp)
293 .value("XOR", SkPathOp::kXOR_SkPathOp)
294 .value("REVERSE_DIFFERENCE", SkPathOp::kReverseDifference_SkPathOp);
295
296 constant("MOVE_VERB", MOVE);
297 constant("LINE_VERB", LINE);
298 constant("QUAD_VERB", QUAD);
299 constant("CUBIC_VERB", CUBIC);
300 constant("CLOSE_VERB", CLOSE);
301
302 // coming soon - Stroke
303
304 // coming soon - Matrix
305
306 // coming soon - Bounds/Trim
307
308#ifdef PATHKIT_TESTING
309 function("SkBits2Float", &SkBits2Float);
310
311 function("GetBoundaryPathFromRegion", &GetBoundaryPathFromRegion);
312
313 enum_<SkRegion::Op>("RegionOp")
314 .value("DIFFERENCE", SkRegion::Op::kDifference_Op)
315 .value("INTERSECT", SkRegion::Op::kIntersect_Op)
316 .value("UNION", SkRegion::Op::kUnion_Op)
317 .value("XOR", SkRegion::Op::kXOR_Op)
318 .value("REVERSE_DIFFERENCE", SkRegion::Op::kReverseDifference_Op)
319 .value("REPLACE", SkRegion::Op::kReplace_Op);
320
Kevin Lubick92eaa3c2018-07-16 21:00:52 -0400321 class_<SkRegion>("SkRegion")
322 .constructor<>()
323
324 .function("setRect",
325 select_overload<bool(int32_t, int32_t, int32_t, int32_t)>(&SkRegion::setRect))
326 .function("setPath", &SkRegion::setPath)
327 .function("opLTRB",
328 select_overload<bool(int32_t, int32_t, int32_t, int32_t, SkRegion::Op)>(&SkRegion::op))
329 .function("opRegion",
330 select_overload<bool(const SkRegion&, SkRegion::Op)>(&SkRegion::op))
331 .function("opRegionAB",
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400332 select_overload<bool(const SkRegion&, const SkRegion&, SkRegion::Op)>(&SkRegion::op));
333#endif
Kevin Lubick22647d02018-07-06 14:31:23 -0400334}