blob: db540d195bcad7a096903bb7b62dccdac1247841 [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 Lubick97d6d982018-08-10 15:53:16 -04008#include "SkDashPathEffect.h"
Kevin Lubick92eaa3c2018-07-16 21:00:52 -04009#include "SkFloatBits.h"
Kevin Lubick22647d02018-07-06 14:31:23 -040010#include "SkFloatingPoint.h"
Kevin Lubick641bf872018-08-06 14:49:39 -040011#include "SkMatrix.h"
Kevin Lubick97d6d982018-08-10 15:53:16 -040012#include "SkPaint.h"
Kevin Lubick22647d02018-07-06 14:31:23 -040013#include "SkParsePath.h"
Kevin Lubick11194ab2018-08-17 13:52:56 -040014#include "SkStrokeRec.h"
Kevin Lubick22647d02018-07-06 14:31:23 -040015#include "SkPath.h"
16#include "SkPathOps.h"
Kevin Lubick641bf872018-08-06 14:49:39 -040017#include "SkRect.h"
Kevin Lubick11194ab2018-08-17 13:52:56 -040018#include "SkPaintDefaults.h"
Kevin Lubick22647d02018-07-06 14:31:23 -040019#include "SkString.h"
Kevin Lubick97d6d982018-08-10 15:53:16 -040020#include "SkTrimPathEffect.h"
Kevin Lubick22647d02018-07-06 14:31:23 -040021
22#include <emscripten/emscripten.h>
23#include <emscripten/bind.h>
24
25using namespace emscripten;
26
27static const int MOVE = 0;
28static const int LINE = 1;
29static const int QUAD = 2;
Kevin Lubick97d6d982018-08-10 15:53:16 -040030static const int CONIC = 3;
Kevin Lubick22647d02018-07-06 14:31:23 -040031static const int CUBIC = 4;
32static const int CLOSE = 5;
33
Kevin Lubick5f0e3a12018-08-07 11:30:12 -040034// Just for self-documenting purposes where the main thing being returned is an
Kevin Lubick97d6d982018-08-10 15:53:16 -040035// SkPath, but in an error case, something of type null (which is val) could also be
Kevin Lubick5f0e3a12018-08-07 11:30:12 -040036// returned;
Kevin Lubick97d6d982018-08-10 15:53:16 -040037using SkPathOrNull = emscripten::val;
38// Self-documenting for when we return a string
39using JSString = emscripten::val;
Kevin Lubick5f0e3a12018-08-07 11:30:12 -040040
Kevin Lubick22647d02018-07-06 14:31:23 -040041// =================================================================================
Kevin Lubick641bf872018-08-06 14:49:39 -040042// Creating/Exporting Paths with cmd arrays
Kevin Lubick22647d02018-07-06 14:31:23 -040043// =================================================================================
44
Florin Malitae1824da2018-07-12 10:33:39 -040045template <typename VisitFunc>
46void VisitPath(const SkPath& p, VisitFunc&& f) {
47 SkPath::RawIter iter(p);
Kevin Lubick22647d02018-07-06 14:31:23 -040048 SkPoint pts[4];
49 SkPath::Verb verb;
Florin Malitae1824da2018-07-12 10:33:39 -040050 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
Kevin Lubick641bf872018-08-06 14:49:39 -040051 f(verb, pts, iter);
Kevin Lubick22647d02018-07-06 14:31:23 -040052 }
53}
54
Kevin Lubick641bf872018-08-06 14:49:39 -040055emscripten::val EMSCRIPTEN_KEEPALIVE ToCmds(const SkPath& path) {
Kevin Lubick5f0e3a12018-08-07 11:30:12 -040056 emscripten::val cmds = emscripten::val::array();
Kevin Lubick22647d02018-07-06 14:31:23 -040057
Kevin Lubick641bf872018-08-06 14:49:39 -040058 VisitPath(path, [&cmds](SkPath::Verb verb, const SkPoint pts[4], SkPath::RawIter iter) {
Kevin Lubick5f0e3a12018-08-07 11:30:12 -040059 emscripten::val cmd = emscripten::val::array();
Kevin Lubick22647d02018-07-06 14:31:23 -040060 switch (verb) {
Florin Malitae1824da2018-07-12 10:33:39 -040061 case SkPath::kMove_Verb:
62 cmd.call<void>("push", MOVE, pts[0].x(), pts[0].y());
63 break;
64 case SkPath::kLine_Verb:
65 cmd.call<void>("push", LINE, pts[1].x(), pts[1].y());
66 break;
67 case SkPath::kQuad_Verb:
68 cmd.call<void>("push", QUAD, pts[1].x(), pts[1].y(), pts[2].x(), pts[2].y());
69 break;
70 case SkPath::kConic_Verb:
Kevin Lubick97d6d982018-08-10 15:53:16 -040071 cmd.call<void>("push", CONIC,
72 pts[1].x(), pts[1].y(),
73 pts[2].x(), pts[2].y(), iter.conicWeight());
Florin Malitae1824da2018-07-12 10:33:39 -040074 break;
75 case SkPath::kCubic_Verb:
76 cmd.call<void>("push", CUBIC,
77 pts[1].x(), pts[1].y(),
78 pts[2].x(), pts[2].y(),
79 pts[3].x(), pts[3].y());
80 break;
81 case SkPath::kClose_Verb:
82 cmd.call<void>("push", CLOSE);
83 break;
84 case SkPath::kDone_Verb:
85 SkASSERT(false);
86 break;
Kevin Lubick22647d02018-07-06 14:31:23 -040087 }
88 cmds.call<void>("push", cmd);
Florin Malitae1824da2018-07-12 10:33:39 -040089 });
Kevin Lubick22647d02018-07-06 14:31:23 -040090 return cmds;
91}
92
93// This type signature is a mess, but it's necessary. See, we can't use "bind" (EMSCRIPTEN_BINDINGS)
94// and pointers to primitive types (Only bound types like SkPoint). We could if we used
95// cwrap (see https://becominghuman.ai/passing-and-returning-webassembly-array-parameters-a0f572c65d97)
96// but that requires us to stick to C code and, AFAIK, doesn't allow us to return nice things like
97// SkPath or SkOpBuilder.
98//
99// So, basically, if we are using C++ and EMSCRIPTEN_BINDINGS, we can't have primative pointers
100// in our function type signatures. (this gives an error message like "Cannot call foo due to unbound
101// types Pi, Pf"). But, we can just pretend they are numbers and cast them to be pointers and
102// the compiler is happy.
Kevin Lubick97d6d982018-08-10 15:53:16 -0400103SkPathOrNull EMSCRIPTEN_KEEPALIVE FromCmds(uintptr_t /* float* */ cptr, int numCmds) {
Florin Malitae1824da2018-07-12 10:33:39 -0400104 const auto* cmds = reinterpret_cast<const float*>(cptr);
Kevin Lubick22647d02018-07-06 14:31:23 -0400105 SkPath path;
106 float x1, y1, x2, y2, x3, y3;
107
108 // if there are not enough arguments, bail with the path we've constructed so far.
109 #define CHECK_NUM_ARGS(n) \
110 if ((i + n) > numCmds) { \
111 SkDebugf("Not enough args to match the verbs. Saw %d commands\n", numCmds); \
Kevin Lubick5f0e3a12018-08-07 11:30:12 -0400112 return emscripten::val::null(); \
Kevin Lubick22647d02018-07-06 14:31:23 -0400113 }
114
115 for(int i = 0; i < numCmds;){
116 switch (sk_float_floor2int(cmds[i++])) {
117 case MOVE:
118 CHECK_NUM_ARGS(2);
119 x1 = cmds[i++], y1 = cmds[i++];
120 path.moveTo(x1, y1);
121 break;
122 case LINE:
123 CHECK_NUM_ARGS(2);
124 x1 = cmds[i++], y1 = cmds[i++];
125 path.lineTo(x1, y1);
126 break;
127 case QUAD:
128 CHECK_NUM_ARGS(4);
129 x1 = cmds[i++], y1 = cmds[i++];
130 x2 = cmds[i++], y2 = cmds[i++];
131 path.quadTo(x1, y1, x2, y2);
132 break;
133 case CUBIC:
134 CHECK_NUM_ARGS(6);
135 x1 = cmds[i++], y1 = cmds[i++];
136 x2 = cmds[i++], y2 = cmds[i++];
137 x3 = cmds[i++], y3 = cmds[i++];
138 path.cubicTo(x1, y1, x2, y2, x3, y3);
139 break;
140 case CLOSE:
141 path.close();
142 break;
143 default:
144 SkDebugf(" path: UNKNOWN command %f, aborting dump...\n", cmds[i-1]);
Kevin Lubick5f0e3a12018-08-07 11:30:12 -0400145 return emscripten::val::null();
Kevin Lubick22647d02018-07-06 14:31:23 -0400146 }
147 }
148
149 #undef CHECK_NUM_ARGS
150
Kevin Lubick5f0e3a12018-08-07 11:30:12 -0400151 return emscripten::val(path);
Kevin Lubick22647d02018-07-06 14:31:23 -0400152}
153
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400154SkPath EMSCRIPTEN_KEEPALIVE NewPath() {
155 return SkPath();
156}
157
Kevin Lubick084d9962018-08-15 13:28:27 -0400158SkPath EMSCRIPTEN_KEEPALIVE CopyPath(const SkPath& a) {
159 SkPath copy(a);
160 return copy;
161}
162
163bool EMSCRIPTEN_KEEPALIVE Equals(const SkPath& a, const SkPath& b) {
164 return a == b;
165}
166
Kevin Lubick22647d02018-07-06 14:31:23 -0400167//========================================================================================
Kevin Lubick11194ab2018-08-17 13:52:56 -0400168// Path things
169//========================================================================================
170
171// All these Apply* methods are simple wrappers to avoid returning an object.
172// The default WASM bindings produce code that will leak if a return value
173// isn't assigned to a JS variable and has delete() called on it.
174// These Apply methods, combined with the smarter binding code allow for chainable
175// commands that don't leak if the return value is ignored (i.e. when used intuitively).
176
177void ApplyArcTo(SkPath& p, SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2,
178 SkScalar radius) {
179 p.arcTo(x1, y1, x2, y2, radius);
180}
181
182void ApplyClose(SkPath& p) {
183 p.close();
184}
185
186void ApplyConicTo(SkPath& p, SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2,
187 SkScalar w) {
188 p.conicTo(x1, y1, x2, y2, w);
189}
190
191void ApplyCubicTo(SkPath& p, SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2,
192 SkScalar x3, SkScalar y3) {
193 p.cubicTo(x1, y1, x2, y2, x3, y3);
194}
195
196void ApplyLineTo(SkPath& p, SkScalar x, SkScalar y) {
197 p.lineTo(x, y);
198}
199
200void ApplyMoveTo(SkPath& p, SkScalar x, SkScalar y) {
201 p.moveTo(x, y);
202}
203
204void ApplyQuadTo(SkPath& p, SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
205 p.quadTo(x1, y1, x2, y2);
206}
207
208
209
210//========================================================================================
Kevin Lubick641bf872018-08-06 14:49:39 -0400211// SVG things
Kevin Lubick22647d02018-07-06 14:31:23 -0400212//========================================================================================
213
Kevin Lubick97d6d982018-08-10 15:53:16 -0400214JSString EMSCRIPTEN_KEEPALIVE ToSVGString(const SkPath& path) {
Kevin Lubick22647d02018-07-06 14:31:23 -0400215 SkString s;
216 SkParsePath::ToSVGString(path, &s);
217 // Wrapping it in val automatically turns it into a JS string.
218 // Not too sure on performance implications, but is is simpler than
219 // returning a raw pointer to const char * and then using
220 // Pointer_stringify() on the calling side.
Kevin Lubick5f0e3a12018-08-07 11:30:12 -0400221 return emscripten::val(s.c_str());
Kevin Lubick22647d02018-07-06 14:31:23 -0400222}
223
224
Kevin Lubick97d6d982018-08-10 15:53:16 -0400225SkPathOrNull EMSCRIPTEN_KEEPALIVE FromSVGString(std::string str) {
Kevin Lubick22647d02018-07-06 14:31:23 -0400226 SkPath path;
Kevin Lubick5f0e3a12018-08-07 11:30:12 -0400227 if (SkParsePath::FromSVGString(str.c_str(), &path)) {
228 return emscripten::val(path);
229 }
230 return emscripten::val::null();
Kevin Lubick22647d02018-07-06 14:31:23 -0400231}
232
233//========================================================================================
Kevin Lubick641bf872018-08-06 14:49:39 -0400234// PATHOP things
Kevin Lubick22647d02018-07-06 14:31:23 -0400235//========================================================================================
236
Kevin Lubick11194ab2018-08-17 13:52:56 -0400237bool EMSCRIPTEN_KEEPALIVE ApplySimplify(SkPath& path) {
238 return Simplify(path, &path);
Kevin Lubick22647d02018-07-06 14:31:23 -0400239}
240
Kevin Lubick11194ab2018-08-17 13:52:56 -0400241bool EMSCRIPTEN_KEEPALIVE ApplyPathOp(SkPath& pathOne, const SkPath& pathTwo, SkPathOp op) {
242 return Op(pathOne, pathTwo, op, &pathOne);
Kevin Lubick22647d02018-07-06 14:31:23 -0400243}
244
Kevin Lubick97d6d982018-08-10 15:53:16 -0400245SkPathOrNull EMSCRIPTEN_KEEPALIVE ResolveBuilder(SkOpBuilder& builder) {
Kevin Lubick22647d02018-07-06 14:31:23 -0400246 SkPath path;
Kevin Lubick5f0e3a12018-08-07 11:30:12 -0400247 if (builder.resolve(&path)) {
248 return emscripten::val(path);
249 }
250 return emscripten::val::null();
Kevin Lubick22647d02018-07-06 14:31:23 -0400251}
252
253//========================================================================================
Kevin Lubick641bf872018-08-06 14:49:39 -0400254// Canvas things
Kevin Lubick22647d02018-07-06 14:31:23 -0400255//========================================================================================
256
Kevin Lubick5f0e3a12018-08-07 11:30:12 -0400257void EMSCRIPTEN_KEEPALIVE ToCanvas(const SkPath& path, emscripten::val /* Path2D or Canvas*/ ctx) {
Kevin Lubick22647d02018-07-06 14:31:23 -0400258 SkPath::Iter iter(path, false);
259 SkPoint pts[4];
260 SkPath::Verb verb;
261 while ((verb = iter.next(pts, false)) != SkPath::kDone_Verb) {
262 switch (verb) {
263 case SkPath::kMove_Verb:
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400264 ctx.call<void>("moveTo", pts[0].x(), pts[0].y());
Kevin Lubick22647d02018-07-06 14:31:23 -0400265 break;
266 case SkPath::kLine_Verb:
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400267 ctx.call<void>("lineTo", pts[1].x(), pts[1].y());
Kevin Lubick22647d02018-07-06 14:31:23 -0400268 break;
269 case SkPath::kQuad_Verb:
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400270 ctx.call<void>("quadraticCurveTo", pts[1].x(), pts[1].y(), pts[2].x(), pts[2].y());
Kevin Lubick22647d02018-07-06 14:31:23 -0400271 break;
272 case SkPath::kConic_Verb:
Kevin Lubick641bf872018-08-06 14:49:39 -0400273 SkPoint quads[5];
274 // approximate with 2^1=2 quads.
275 SkPath::ConvertConicToQuads(pts[0], pts[1], pts[2], iter.conicWeight(), quads, 1);
Kevin Lubick641bf872018-08-06 14:49:39 -0400276 ctx.call<void>("quadraticCurveTo", quads[1].x(), quads[1].y(), quads[2].x(), quads[2].y());
277 ctx.call<void>("quadraticCurveTo", quads[3].x(), quads[3].y(), quads[4].x(), quads[4].y());
Kevin Lubick22647d02018-07-06 14:31:23 -0400278 break;
279 case SkPath::kCubic_Verb:
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400280 ctx.call<void>("bezierCurveTo", pts[1].x(), pts[1].y(), pts[2].x(), pts[2].y(),
Kevin Lubick22647d02018-07-06 14:31:23 -0400281 pts[3].x(), pts[3].y());
282 break;
283 case SkPath::kClose_Verb:
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400284 ctx.call<void>("closePath");
Kevin Lubick22647d02018-07-06 14:31:23 -0400285 break;
286 case SkPath::kDone_Verb:
287 break;
288 }
289 }
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400290}
291
292emscripten::val JSPath2D = emscripten::val::global("Path2D");
293
Kevin Lubick641bf872018-08-06 14:49:39 -0400294emscripten::val EMSCRIPTEN_KEEPALIVE ToPath2D(const SkPath& path) {
Kevin Lubick5f0e3a12018-08-07 11:30:12 -0400295 emscripten::val retVal = JSPath2D.new_();
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400296 ToCanvas(path, retVal);
Kevin Lubick22647d02018-07-06 14:31:23 -0400297 return retVal;
298}
299
Kevin Lubick641bf872018-08-06 14:49:39 -0400300// ======================================================================================
301// Path2D API things
302// ======================================================================================
Kevin Lubick11194ab2018-08-17 13:52:56 -0400303void ApplyAddRect(SkPath& path, SkScalar x, SkScalar y, SkScalar width, SkScalar height) {
Kevin Lubick641bf872018-08-06 14:49:39 -0400304 path.addRect(x, y, x+width, y+height);
305}
306
Kevin Lubick11194ab2018-08-17 13:52:56 -0400307void ApplyAddArc(SkPath& path, SkScalar x, SkScalar y, SkScalar radius,
308 SkScalar startAngle, SkScalar endAngle, bool ccw) {
Kevin Lubick641bf872018-08-06 14:49:39 -0400309 SkPath temp;
310 SkRect bounds = SkRect::MakeLTRB(x-radius, y-radius, x+radius, y+radius);
311 const auto sweep = SkRadiansToDegrees(endAngle - startAngle) - 360 * ccw;
312 temp.addArc(bounds, SkRadiansToDegrees(startAngle), sweep);
313 path.addPath(temp, SkPath::kExtend_AddPathMode);
314}
315
Kevin Lubick11194ab2018-08-17 13:52:56 -0400316void ApplyEllipse(SkPath& path, SkScalar x, SkScalar y, SkScalar radiusX, SkScalar radiusY,
Kevin Lubick641bf872018-08-06 14:49:39 -0400317 SkScalar rotation, SkScalar startAngle, SkScalar endAngle, bool ccw) {
318 // This is easiest to do by making a new path and then extending the current path
319 // (this properly catches the cases of if there's a moveTo before this call or not).
320 SkRect bounds = SkRect::MakeLTRB(x-radiusX, y-radiusY, x+radiusX, y+radiusY);
321 SkPath temp;
322 const auto sweep = SkRadiansToDegrees(endAngle - startAngle) - (360 * ccw);
323 temp.addArc(bounds, SkRadiansToDegrees(startAngle), sweep);
324
325 SkMatrix m;
326 m.setRotate(SkRadiansToDegrees(rotation), x, y);
327 path.addPath(temp, m, SkPath::kExtend_AddPathMode);
328}
329
Kevin Lubick641bf872018-08-06 14:49:39 -0400330// Allows for full matix control.
Kevin Lubick11194ab2018-08-17 13:52:56 -0400331void ApplyAddPath(SkPath& orig, const SkPath& newPath,
Kevin Lubick641bf872018-08-06 14:49:39 -0400332 SkScalar scaleX, SkScalar skewX, SkScalar transX,
333 SkScalar skewY, SkScalar scaleY, SkScalar transY,
334 SkScalar pers0, SkScalar pers1, SkScalar pers2) {
335 SkMatrix m = SkMatrix::MakeAll(scaleX, skewX , transX,
336 skewY , scaleY, transY,
337 pers0 , pers1 , pers2);
338 orig.addPath(newPath, m);
339}
340
Kevin Lubick084d9962018-08-15 13:28:27 -0400341JSString GetFillTypeString(const SkPath& path) {
Kevin Lubick97d6d982018-08-10 15:53:16 -0400342 if (path.getFillType() == SkPath::FillType::kWinding_FillType) {
343 return emscripten::val("nonzero");
344 } else if (path.getFillType() == SkPath::FillType::kEvenOdd_FillType) {
345 return emscripten::val("evenodd");
346 } else {
347 SkDebugf("warning: can't translate inverted filltype to HTML Canvas\n");
348 return emscripten::val("nonzero"); //Use default
349 }
350}
351
352//========================================================================================
353// Path Effects
354//========================================================================================
355
Kevin Lubick11194ab2018-08-17 13:52:56 -0400356bool ApplyDash(SkPath& path, SkScalar on, SkScalar off, SkScalar phase) {
Kevin Lubick97d6d982018-08-10 15:53:16 -0400357 SkScalar intervals[] = { on, off };
358 auto pe = SkDashPathEffect::Make(intervals, 2, phase);
359 if (!pe) {
360 SkDebugf("Invalid args to dash()\n");
Kevin Lubick11194ab2018-08-17 13:52:56 -0400361 return false;
Kevin Lubick97d6d982018-08-10 15:53:16 -0400362 }
Kevin Lubick11194ab2018-08-17 13:52:56 -0400363 SkStrokeRec rec(SkStrokeRec::InitStyle::kHairline_InitStyle);
364 if (pe->filterPath(&path, path, &rec, nullptr)) {
365 return true;
Kevin Lubick97d6d982018-08-10 15:53:16 -0400366 }
367 SkDebugf("Could not make dashed path\n");
Kevin Lubick11194ab2018-08-17 13:52:56 -0400368 return false;
Kevin Lubick97d6d982018-08-10 15:53:16 -0400369}
370
Kevin Lubick11194ab2018-08-17 13:52:56 -0400371bool ApplyTrim(SkPath& path, SkScalar startT, SkScalar stopT, bool isComplement) {
Kevin Lubick97d6d982018-08-10 15:53:16 -0400372 auto mode = isComplement ? SkTrimPathEffect::Mode::kInverted : SkTrimPathEffect::Mode::kNormal;
373 auto pe = SkTrimPathEffect::Make(startT, stopT, mode);
374 if (!pe) {
375 SkDebugf("Invalid args to trim(): startT and stopT must be in [0,1]\n");
Kevin Lubick11194ab2018-08-17 13:52:56 -0400376 return false;
Kevin Lubick97d6d982018-08-10 15:53:16 -0400377 }
Kevin Lubick11194ab2018-08-17 13:52:56 -0400378 SkStrokeRec rec(SkStrokeRec::InitStyle::kHairline_InitStyle);
379 if (pe->filterPath(&path, path, &rec, nullptr)) {
380 return true;
Kevin Lubick97d6d982018-08-10 15:53:16 -0400381 }
382 SkDebugf("Could not trim path\n");
Kevin Lubick11194ab2018-08-17 13:52:56 -0400383 return false;
Kevin Lubick97d6d982018-08-10 15:53:16 -0400384}
385
Kevin Lubick11194ab2018-08-17 13:52:56 -0400386struct StrokeOpts {
387 // Default values are set in chaining.js which allows clients
388 // to set any number of them. Otherwise, the binding code complains if
389 // any are omitted.
390 SkScalar width;
391 SkScalar miter_limit;
392 SkPaint::Join join;
393 SkPaint::Cap cap;
394};
Kevin Lubick97d6d982018-08-10 15:53:16 -0400395
Kevin Lubick11194ab2018-08-17 13:52:56 -0400396bool ApplyStroke(SkPath& path, StrokeOpts opts) {
Kevin Lubick97d6d982018-08-10 15:53:16 -0400397 SkPaint p;
398 p.setStyle(SkPaint::kStroke_Style);
Kevin Lubick11194ab2018-08-17 13:52:56 -0400399 p.setStrokeCap(opts.cap);
400 p.setStrokeJoin(opts.join);
401 p.setStrokeWidth(opts.width);
402 p.setStrokeMiter(opts.miter_limit);
Kevin Lubick97d6d982018-08-10 15:53:16 -0400403
Kevin Lubick11194ab2018-08-17 13:52:56 -0400404 return p.getFillPath(path, &path);
Kevin Lubick97d6d982018-08-10 15:53:16 -0400405}
406
Kevin Lubick92eaa3c2018-07-16 21:00:52 -0400407//========================================================================================
Kevin Lubick084d9962018-08-15 13:28:27 -0400408// Matrix things
409//========================================================================================
410
411struct SimpleMatrix {
412 SkScalar scaleX, skewX, transX;
413 SkScalar skewY, scaleY, transY;
414 SkScalar pers0, pers1, pers2;
415};
416
417SkMatrix toSkMatrix(const SimpleMatrix& sm) {
418 return SkMatrix::MakeAll(sm.scaleX, sm.skewX , sm.transX,
419 sm.skewY , sm.scaleY, sm.transY,
420 sm.pers0 , sm.pers1 , sm.pers2);
421}
422
Kevin Lubick11194ab2018-08-17 13:52:56 -0400423void ApplyTransform(SkPath& orig, const SimpleMatrix& sm) {
424 orig.transform(toSkMatrix(sm));
Kevin Lubick084d9962018-08-15 13:28:27 -0400425}
426
Kevin Lubick11194ab2018-08-17 13:52:56 -0400427void ApplyTransform(SkPath& orig,
428 SkScalar scaleX, SkScalar skewX, SkScalar transX,
429 SkScalar skewY, SkScalar scaleY, SkScalar transY,
430 SkScalar pers0, SkScalar pers1, SkScalar pers2) {
Kevin Lubick084d9962018-08-15 13:28:27 -0400431 SkMatrix m = SkMatrix::MakeAll(scaleX, skewX , transX,
432 skewY , scaleY, transY,
433 pers0 , pers1 , pers2);
Kevin Lubick11194ab2018-08-17 13:52:56 -0400434 orig.transform(m);
Kevin Lubick084d9962018-08-15 13:28:27 -0400435}
436
437//========================================================================================
Kevin Lubick644d8e72018-08-09 13:58:04 -0400438// Testing things
Kevin Lubick92eaa3c2018-07-16 21:00:52 -0400439//========================================================================================
440
Kevin Lubick644d8e72018-08-09 13:58:04 -0400441// The use case for this is on the JS side is something like:
442// PathKit.SkBits2FloatUnsigned(parseInt("0xc0a00000"))
443// to have precise float values for tests. In the C++ tests, we can use SkBits2Float because
444// it takes int32_t, but the JS parseInt basically returns an unsigned int. So, we add in
445// this helper which casts for us on the way to SkBits2Float.
446float SkBits2FloatUnsigned(uint32_t floatAsBits) {
447 return SkBits2Float((int32_t) floatAsBits);
Kevin Lubick92eaa3c2018-07-16 21:00:52 -0400448}
449
Kevin Lubick22647d02018-07-06 14:31:23 -0400450// Binds the classes to the JS
Kevin Lubick641bf872018-08-06 14:49:39 -0400451//
452// See https://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/embind.html#non-member-functions-on-the-javascript-prototype
453// for more on binding non-member functions to the JS object, allowing us to rewire
454// various functions. That is, we can make the SkPath we expose appear to have methods
455// that the original SkPath does not, like rect(x, y, width, height) and toPath2D().
456//
457// An important detail for binding non-member functions is that the first argument
458// must be SkPath& (the reference part is very important).
Kevin Lubick11194ab2018-08-17 13:52:56 -0400459//
460// Note that we can't expose default or optional arguments, but we can have multiple
461// declarations of the same function that take different amounts of arguments.
462// For example, see _transform
463// Additionally, we are perfectly happy to handle default arguments and function
464// overloads in the JS glue code (see chaining.js::addPath() for an example).
Kevin Lubick22647d02018-07-06 14:31:23 -0400465EMSCRIPTEN_BINDINGS(skia) {
466 class_<SkPath>("SkPath")
467 .constructor<>()
Kevin Lubick084d9962018-08-15 13:28:27 -0400468 .constructor<const SkPath&>()
Kevin Lubick22647d02018-07-06 14:31:23 -0400469
Kevin Lubick641bf872018-08-06 14:49:39 -0400470 // Path2D API
Kevin Lubick11194ab2018-08-17 13:52:56 -0400471 .function("_addPath", &ApplyAddPath)
472 // 3 additional overloads of addPath are handled in JS bindings
473 .function("_arc", &ApplyAddArc)
474 .function("_arcTo", &ApplyArcTo)
475 //"bezierCurveTo" alias handled in JS bindings
476 .function("_close", &ApplyClose)
477 .function("_conicTo", &ApplyConicTo)
478 .function("_cubicTo", &ApplyCubicTo)
479 //"closePath" alias handled in JS bindings
Kevin Lubick641bf872018-08-06 14:49:39 -0400480
Kevin Lubick11194ab2018-08-17 13:52:56 -0400481 .function("_ellipse", &ApplyEllipse)
482 .function("_lineTo", &ApplyLineTo)
483 .function("_moveTo", &ApplyMoveTo)
484 // "quadraticCurveTo" alias handled in JS bindings
485 .function("_quadTo", &ApplyQuadTo)
486 .function("_rect", &ApplyAddRect)
Kevin Lubick641bf872018-08-06 14:49:39 -0400487
Kevin Lubick644d8e72018-08-09 13:58:04 -0400488 // Extra features
489 .function("setFillType", &SkPath::setFillType)
490 .function("getFillType", &SkPath::getFillType)
Kevin Lubick084d9962018-08-15 13:28:27 -0400491 .function("getFillTypeString", &GetFillTypeString)
Kevin Lubick644d8e72018-08-09 13:58:04 -0400492 .function("getBounds", &SkPath::getBounds)
493 .function("computeTightBounds", &SkPath::computeTightBounds)
Kevin Lubick084d9962018-08-15 13:28:27 -0400494 .function("equals", &Equals)
495 .function("copy", &CopyPath)
Kevin Lubick644d8e72018-08-09 13:58:04 -0400496
Kevin Lubick97d6d982018-08-10 15:53:16 -0400497 // PathEffects
Kevin Lubick11194ab2018-08-17 13:52:56 -0400498 .function("_dash", &ApplyDash)
499 .function("_trim", &ApplyTrim)
500 .function("_stroke", &ApplyStroke)
Kevin Lubick97d6d982018-08-10 15:53:16 -0400501
Kevin Lubick084d9962018-08-15 13:28:27 -0400502 // Matrix
Kevin Lubick11194ab2018-08-17 13:52:56 -0400503 .function("_transform", select_overload<void(SkPath& orig, const SimpleMatrix& sm)>(&ApplyTransform))
504 .function("_transform", select_overload<void(SkPath& orig, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar)>(&ApplyTransform))
Kevin Lubick084d9962018-08-15 13:28:27 -0400505
Kevin Lubick641bf872018-08-06 14:49:39 -0400506 // PathOps
Kevin Lubick11194ab2018-08-17 13:52:56 -0400507 .function("_simplify", &ApplySimplify)
508 .function("_op", &ApplyPathOp)
Kevin Lubick641bf872018-08-06 14:49:39 -0400509
510 // Exporting
511 .function("toCmds", &ToCmds)
512 .function("toPath2D", &ToPath2D)
513 .function("toCanvas", &ToCanvas)
514 .function("toSVGString", &ToSVGString)
515
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400516#ifdef PATHKIT_TESTING
517 .function("dump", select_overload<void() const>(&SkPath::dump))
Kevin Lubick97d6d982018-08-10 15:53:16 -0400518 .function("dumpHex", select_overload<void() const>(&SkPath::dumpHex))
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400519#endif
520 ;
Kevin Lubick22647d02018-07-06 14:31:23 -0400521
522 class_<SkOpBuilder>("SkOpBuilder")
523 .constructor<>()
524
Kevin Lubick641bf872018-08-06 14:49:39 -0400525 .function("add", &SkOpBuilder::add)
526 .function("resolve", &ResolveBuilder);
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400527
528 // Without these function() bindings, the function would be exposed but oblivious to
529 // our types (e.g. SkPath)
530
531 // Import
532 function("FromSVGString", &FromSVGString);
533 function("FromCmds", &FromCmds);
534 function("NewPath", &NewPath);
Kevin Lubick084d9962018-08-15 13:28:27 -0400535 function("NewPath", &CopyPath);
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400536 // Path2D is opaque, so we can't read in from it.
537
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400538 // PathOps
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400539 function("ApplyPathOp", &ApplyPathOp);
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400540
541 enum_<SkPathOp>("PathOp")
542 .value("DIFFERENCE", SkPathOp::kDifference_SkPathOp)
543 .value("INTERSECT", SkPathOp::kIntersect_SkPathOp)
544 .value("UNION", SkPathOp::kUnion_SkPathOp)
545 .value("XOR", SkPathOp::kXOR_SkPathOp)
546 .value("REVERSE_DIFFERENCE", SkPathOp::kReverseDifference_SkPathOp);
547
Kevin Lubick644d8e72018-08-09 13:58:04 -0400548 enum_<SkPath::FillType>("FillType")
549 .value("WINDING", SkPath::FillType::kWinding_FillType)
550 .value("EVENODD", SkPath::FillType::kEvenOdd_FillType)
551 .value("INVERSE_WINDING", SkPath::FillType::kInverseWinding_FillType)
552 .value("INVERSE_EVENODD", SkPath::FillType::kInverseEvenOdd_FillType);
553
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400554 constant("MOVE_VERB", MOVE);
555 constant("LINE_VERB", LINE);
556 constant("QUAD_VERB", QUAD);
Kevin Lubick97d6d982018-08-10 15:53:16 -0400557 constant("CONIC_VERB", CONIC);
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400558 constant("CUBIC_VERB", CUBIC);
559 constant("CLOSE_VERB", CLOSE);
560
Kevin Lubick644d8e72018-08-09 13:58:04 -0400561 // A value object is much simpler than a class - it is returned as a JS
562 // object and does not require delete().
563 // https://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/embind.html#value-types
564 value_object<SkRect>("SkRect")
565 .field("fLeft", &SkRect::fLeft)
566 .field("fTop", &SkRect::fTop)
567 .field("fRight", &SkRect::fRight)
568 .field("fBottom", &SkRect::fBottom);
569
570 function("MakeLTRBRect", &SkRect::MakeLTRB);
571
Kevin Lubick97d6d982018-08-10 15:53:16 -0400572 // Stroke
573 enum_<SkPaint::Join>("StrokeJoin")
574 .value("MITER", SkPaint::Join::kMiter_Join)
575 .value("ROUND", SkPaint::Join::kRound_Join)
576 .value("BEVEL", SkPaint::Join::kBevel_Join);
577
578 enum_<SkPaint::Cap>("StrokeCap")
579 .value("BUTT", SkPaint::Cap::kButt_Cap)
580 .value("ROUND", SkPaint::Cap::kRound_Cap)
581 .value("SQUARE", SkPaint::Cap::kSquare_Cap);
582
Kevin Lubick11194ab2018-08-17 13:52:56 -0400583 value_object<StrokeOpts>("StrokeOpts")
584 .field("width", &StrokeOpts::width)
585 .field("miter_limit", &StrokeOpts::miter_limit)
586 .field("join", &StrokeOpts::join)
587 .field("cap", &StrokeOpts::cap);
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400588
Kevin Lubick084d9962018-08-15 13:28:27 -0400589 // Matrix
590 // Allows clients to supply a 1D array of 9 elements and the bindings
591 // will automatically turn it into a 3x3 2D matrix.
592 // e.g. path.transform([0,1,2,3,4,5,6,7,8])
593 // This is likely simpler for the client than exposing SkMatrix
594 // directly and requiring them to do a lot of .delete().
595 value_array<SimpleMatrix>("SkMatrix")
596 .element(&SimpleMatrix::scaleX)
597 .element(&SimpleMatrix::skewX)
598 .element(&SimpleMatrix::transX)
599
600 .element(&SimpleMatrix::skewY)
601 .element(&SimpleMatrix::scaleY)
602 .element(&SimpleMatrix::transY)
603
604 .element(&SimpleMatrix::pers0)
605 .element(&SimpleMatrix::pers1)
606 .element(&SimpleMatrix::pers2);
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400607
Kevin Lubick644d8e72018-08-09 13:58:04 -0400608 // Test Utils
609 function("SkBits2FloatUnsigned", &SkBits2FloatUnsigned);
Kevin Lubick22647d02018-07-06 14:31:23 -0400610}