blob: 6efaf3c2617bebfd4f6485442464495672d55ba2 [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;
Kevin Lubickc623af22018-08-17 14:42:53 -0400133 case CONIC:
134 CHECK_NUM_ARGS(6);
135 x1 = cmds[i++], y1 = cmds[i++];
136 x2 = cmds[i++], y2 = cmds[i++];
137 x3 = cmds[i++]; // width
138 path.conicTo(x1, y1, x2, y2, x3);
139 break;
Kevin Lubick22647d02018-07-06 14:31:23 -0400140 case CUBIC:
141 CHECK_NUM_ARGS(6);
142 x1 = cmds[i++], y1 = cmds[i++];
143 x2 = cmds[i++], y2 = cmds[i++];
144 x3 = cmds[i++], y3 = cmds[i++];
145 path.cubicTo(x1, y1, x2, y2, x3, y3);
146 break;
147 case CLOSE:
148 path.close();
149 break;
150 default:
151 SkDebugf(" path: UNKNOWN command %f, aborting dump...\n", cmds[i-1]);
Kevin Lubick5f0e3a12018-08-07 11:30:12 -0400152 return emscripten::val::null();
Kevin Lubick22647d02018-07-06 14:31:23 -0400153 }
154 }
155
156 #undef CHECK_NUM_ARGS
157
Kevin Lubick5f0e3a12018-08-07 11:30:12 -0400158 return emscripten::val(path);
Kevin Lubick22647d02018-07-06 14:31:23 -0400159}
160
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400161SkPath EMSCRIPTEN_KEEPALIVE NewPath() {
162 return SkPath();
163}
164
Kevin Lubick084d9962018-08-15 13:28:27 -0400165SkPath EMSCRIPTEN_KEEPALIVE CopyPath(const SkPath& a) {
166 SkPath copy(a);
167 return copy;
168}
169
170bool EMSCRIPTEN_KEEPALIVE Equals(const SkPath& a, const SkPath& b) {
171 return a == b;
172}
173
Kevin Lubick22647d02018-07-06 14:31:23 -0400174//========================================================================================
Kevin Lubick11194ab2018-08-17 13:52:56 -0400175// Path things
176//========================================================================================
177
178// All these Apply* methods are simple wrappers to avoid returning an object.
179// The default WASM bindings produce code that will leak if a return value
180// isn't assigned to a JS variable and has delete() called on it.
181// These Apply methods, combined with the smarter binding code allow for chainable
182// commands that don't leak if the return value is ignored (i.e. when used intuitively).
183
184void ApplyArcTo(SkPath& p, SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2,
185 SkScalar radius) {
186 p.arcTo(x1, y1, x2, y2, radius);
187}
188
189void ApplyClose(SkPath& p) {
190 p.close();
191}
192
193void ApplyConicTo(SkPath& p, SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2,
194 SkScalar w) {
195 p.conicTo(x1, y1, x2, y2, w);
196}
197
198void ApplyCubicTo(SkPath& p, SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2,
199 SkScalar x3, SkScalar y3) {
200 p.cubicTo(x1, y1, x2, y2, x3, y3);
201}
202
203void ApplyLineTo(SkPath& p, SkScalar x, SkScalar y) {
204 p.lineTo(x, y);
205}
206
207void ApplyMoveTo(SkPath& p, SkScalar x, SkScalar y) {
208 p.moveTo(x, y);
209}
210
211void ApplyQuadTo(SkPath& p, SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
212 p.quadTo(x1, y1, x2, y2);
213}
214
215
216
217//========================================================================================
Kevin Lubick641bf872018-08-06 14:49:39 -0400218// SVG things
Kevin Lubick22647d02018-07-06 14:31:23 -0400219//========================================================================================
220
Kevin Lubick97d6d982018-08-10 15:53:16 -0400221JSString EMSCRIPTEN_KEEPALIVE ToSVGString(const SkPath& path) {
Kevin Lubick22647d02018-07-06 14:31:23 -0400222 SkString s;
223 SkParsePath::ToSVGString(path, &s);
224 // Wrapping it in val automatically turns it into a JS string.
225 // Not too sure on performance implications, but is is simpler than
226 // returning a raw pointer to const char * and then using
227 // Pointer_stringify() on the calling side.
Kevin Lubick5f0e3a12018-08-07 11:30:12 -0400228 return emscripten::val(s.c_str());
Kevin Lubick22647d02018-07-06 14:31:23 -0400229}
230
231
Kevin Lubick97d6d982018-08-10 15:53:16 -0400232SkPathOrNull EMSCRIPTEN_KEEPALIVE FromSVGString(std::string str) {
Kevin Lubick22647d02018-07-06 14:31:23 -0400233 SkPath path;
Kevin Lubick5f0e3a12018-08-07 11:30:12 -0400234 if (SkParsePath::FromSVGString(str.c_str(), &path)) {
235 return emscripten::val(path);
236 }
237 return emscripten::val::null();
Kevin Lubick22647d02018-07-06 14:31:23 -0400238}
239
240//========================================================================================
Kevin Lubick641bf872018-08-06 14:49:39 -0400241// PATHOP things
Kevin Lubick22647d02018-07-06 14:31:23 -0400242//========================================================================================
243
Kevin Lubick11194ab2018-08-17 13:52:56 -0400244bool EMSCRIPTEN_KEEPALIVE ApplySimplify(SkPath& path) {
245 return Simplify(path, &path);
Kevin Lubick22647d02018-07-06 14:31:23 -0400246}
247
Kevin Lubick11194ab2018-08-17 13:52:56 -0400248bool EMSCRIPTEN_KEEPALIVE ApplyPathOp(SkPath& pathOne, const SkPath& pathTwo, SkPathOp op) {
249 return Op(pathOne, pathTwo, op, &pathOne);
Kevin Lubick22647d02018-07-06 14:31:23 -0400250}
251
Kevin Lubick97d6d982018-08-10 15:53:16 -0400252SkPathOrNull EMSCRIPTEN_KEEPALIVE ResolveBuilder(SkOpBuilder& builder) {
Kevin Lubick22647d02018-07-06 14:31:23 -0400253 SkPath path;
Kevin Lubick5f0e3a12018-08-07 11:30:12 -0400254 if (builder.resolve(&path)) {
255 return emscripten::val(path);
256 }
257 return emscripten::val::null();
Kevin Lubick22647d02018-07-06 14:31:23 -0400258}
259
260//========================================================================================
Kevin Lubick641bf872018-08-06 14:49:39 -0400261// Canvas things
Kevin Lubick22647d02018-07-06 14:31:23 -0400262//========================================================================================
263
Kevin Lubick5f0e3a12018-08-07 11:30:12 -0400264void EMSCRIPTEN_KEEPALIVE ToCanvas(const SkPath& path, emscripten::val /* Path2D or Canvas*/ ctx) {
Kevin Lubick22647d02018-07-06 14:31:23 -0400265 SkPath::Iter iter(path, false);
266 SkPoint pts[4];
267 SkPath::Verb verb;
268 while ((verb = iter.next(pts, false)) != SkPath::kDone_Verb) {
269 switch (verb) {
270 case SkPath::kMove_Verb:
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400271 ctx.call<void>("moveTo", pts[0].x(), pts[0].y());
Kevin Lubick22647d02018-07-06 14:31:23 -0400272 break;
273 case SkPath::kLine_Verb:
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400274 ctx.call<void>("lineTo", pts[1].x(), pts[1].y());
Kevin Lubick22647d02018-07-06 14:31:23 -0400275 break;
276 case SkPath::kQuad_Verb:
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400277 ctx.call<void>("quadraticCurveTo", pts[1].x(), pts[1].y(), pts[2].x(), pts[2].y());
Kevin Lubick22647d02018-07-06 14:31:23 -0400278 break;
279 case SkPath::kConic_Verb:
Kevin Lubick641bf872018-08-06 14:49:39 -0400280 SkPoint quads[5];
281 // approximate with 2^1=2 quads.
282 SkPath::ConvertConicToQuads(pts[0], pts[1], pts[2], iter.conicWeight(), quads, 1);
Kevin Lubick641bf872018-08-06 14:49:39 -0400283 ctx.call<void>("quadraticCurveTo", quads[1].x(), quads[1].y(), quads[2].x(), quads[2].y());
284 ctx.call<void>("quadraticCurveTo", quads[3].x(), quads[3].y(), quads[4].x(), quads[4].y());
Kevin Lubick22647d02018-07-06 14:31:23 -0400285 break;
286 case SkPath::kCubic_Verb:
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400287 ctx.call<void>("bezierCurveTo", pts[1].x(), pts[1].y(), pts[2].x(), pts[2].y(),
Kevin Lubick22647d02018-07-06 14:31:23 -0400288 pts[3].x(), pts[3].y());
289 break;
290 case SkPath::kClose_Verb:
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400291 ctx.call<void>("closePath");
Kevin Lubick22647d02018-07-06 14:31:23 -0400292 break;
293 case SkPath::kDone_Verb:
294 break;
295 }
296 }
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400297}
298
299emscripten::val JSPath2D = emscripten::val::global("Path2D");
300
Kevin Lubick641bf872018-08-06 14:49:39 -0400301emscripten::val EMSCRIPTEN_KEEPALIVE ToPath2D(const SkPath& path) {
Kevin Lubick5f0e3a12018-08-07 11:30:12 -0400302 emscripten::val retVal = JSPath2D.new_();
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400303 ToCanvas(path, retVal);
Kevin Lubick22647d02018-07-06 14:31:23 -0400304 return retVal;
305}
306
Kevin Lubick641bf872018-08-06 14:49:39 -0400307// ======================================================================================
308// Path2D API things
309// ======================================================================================
Kevin Lubick11194ab2018-08-17 13:52:56 -0400310void ApplyAddRect(SkPath& path, SkScalar x, SkScalar y, SkScalar width, SkScalar height) {
Kevin Lubick641bf872018-08-06 14:49:39 -0400311 path.addRect(x, y, x+width, y+height);
312}
313
Kevin Lubick11194ab2018-08-17 13:52:56 -0400314void ApplyAddArc(SkPath& path, SkScalar x, SkScalar y, SkScalar radius,
315 SkScalar startAngle, SkScalar endAngle, bool ccw) {
Kevin Lubick641bf872018-08-06 14:49:39 -0400316 SkPath temp;
317 SkRect bounds = SkRect::MakeLTRB(x-radius, y-radius, x+radius, y+radius);
318 const auto sweep = SkRadiansToDegrees(endAngle - startAngle) - 360 * ccw;
319 temp.addArc(bounds, SkRadiansToDegrees(startAngle), sweep);
320 path.addPath(temp, SkPath::kExtend_AddPathMode);
321}
322
Kevin Lubick11194ab2018-08-17 13:52:56 -0400323void ApplyEllipse(SkPath& path, SkScalar x, SkScalar y, SkScalar radiusX, SkScalar radiusY,
Kevin Lubick641bf872018-08-06 14:49:39 -0400324 SkScalar rotation, SkScalar startAngle, SkScalar endAngle, bool ccw) {
325 // This is easiest to do by making a new path and then extending the current path
326 // (this properly catches the cases of if there's a moveTo before this call or not).
327 SkRect bounds = SkRect::MakeLTRB(x-radiusX, y-radiusY, x+radiusX, y+radiusY);
328 SkPath temp;
329 const auto sweep = SkRadiansToDegrees(endAngle - startAngle) - (360 * ccw);
330 temp.addArc(bounds, SkRadiansToDegrees(startAngle), sweep);
331
332 SkMatrix m;
333 m.setRotate(SkRadiansToDegrees(rotation), x, y);
334 path.addPath(temp, m, SkPath::kExtend_AddPathMode);
335}
336
Kevin Lubick641bf872018-08-06 14:49:39 -0400337// Allows for full matix control.
Kevin Lubick11194ab2018-08-17 13:52:56 -0400338void ApplyAddPath(SkPath& orig, const SkPath& newPath,
Kevin Lubick641bf872018-08-06 14:49:39 -0400339 SkScalar scaleX, SkScalar skewX, SkScalar transX,
340 SkScalar skewY, SkScalar scaleY, SkScalar transY,
341 SkScalar pers0, SkScalar pers1, SkScalar pers2) {
342 SkMatrix m = SkMatrix::MakeAll(scaleX, skewX , transX,
343 skewY , scaleY, transY,
344 pers0 , pers1 , pers2);
345 orig.addPath(newPath, m);
346}
347
Kevin Lubick084d9962018-08-15 13:28:27 -0400348JSString GetFillTypeString(const SkPath& path) {
Kevin Lubick97d6d982018-08-10 15:53:16 -0400349 if (path.getFillType() == SkPath::FillType::kWinding_FillType) {
350 return emscripten::val("nonzero");
351 } else if (path.getFillType() == SkPath::FillType::kEvenOdd_FillType) {
352 return emscripten::val("evenodd");
353 } else {
354 SkDebugf("warning: can't translate inverted filltype to HTML Canvas\n");
355 return emscripten::val("nonzero"); //Use default
356 }
357}
358
359//========================================================================================
360// Path Effects
361//========================================================================================
362
Kevin Lubick11194ab2018-08-17 13:52:56 -0400363bool ApplyDash(SkPath& path, SkScalar on, SkScalar off, SkScalar phase) {
Kevin Lubick97d6d982018-08-10 15:53:16 -0400364 SkScalar intervals[] = { on, off };
365 auto pe = SkDashPathEffect::Make(intervals, 2, phase);
366 if (!pe) {
367 SkDebugf("Invalid args to dash()\n");
Kevin Lubick11194ab2018-08-17 13:52:56 -0400368 return false;
Kevin Lubick97d6d982018-08-10 15:53:16 -0400369 }
Kevin Lubick11194ab2018-08-17 13:52:56 -0400370 SkStrokeRec rec(SkStrokeRec::InitStyle::kHairline_InitStyle);
371 if (pe->filterPath(&path, path, &rec, nullptr)) {
372 return true;
Kevin Lubick97d6d982018-08-10 15:53:16 -0400373 }
374 SkDebugf("Could not make dashed path\n");
Kevin Lubick11194ab2018-08-17 13:52:56 -0400375 return false;
Kevin Lubick97d6d982018-08-10 15:53:16 -0400376}
377
Kevin Lubick11194ab2018-08-17 13:52:56 -0400378bool ApplyTrim(SkPath& path, SkScalar startT, SkScalar stopT, bool isComplement) {
Kevin Lubick97d6d982018-08-10 15:53:16 -0400379 auto mode = isComplement ? SkTrimPathEffect::Mode::kInverted : SkTrimPathEffect::Mode::kNormal;
380 auto pe = SkTrimPathEffect::Make(startT, stopT, mode);
381 if (!pe) {
382 SkDebugf("Invalid args to trim(): startT and stopT must be in [0,1]\n");
Kevin Lubick11194ab2018-08-17 13:52:56 -0400383 return false;
Kevin Lubick97d6d982018-08-10 15:53:16 -0400384 }
Kevin Lubick11194ab2018-08-17 13:52:56 -0400385 SkStrokeRec rec(SkStrokeRec::InitStyle::kHairline_InitStyle);
386 if (pe->filterPath(&path, path, &rec, nullptr)) {
387 return true;
Kevin Lubick97d6d982018-08-10 15:53:16 -0400388 }
389 SkDebugf("Could not trim path\n");
Kevin Lubick11194ab2018-08-17 13:52:56 -0400390 return false;
Kevin Lubick97d6d982018-08-10 15:53:16 -0400391}
392
Kevin Lubick11194ab2018-08-17 13:52:56 -0400393struct StrokeOpts {
394 // Default values are set in chaining.js which allows clients
395 // to set any number of them. Otherwise, the binding code complains if
396 // any are omitted.
397 SkScalar width;
398 SkScalar miter_limit;
399 SkPaint::Join join;
400 SkPaint::Cap cap;
401};
Kevin Lubick97d6d982018-08-10 15:53:16 -0400402
Kevin Lubick11194ab2018-08-17 13:52:56 -0400403bool ApplyStroke(SkPath& path, StrokeOpts opts) {
Kevin Lubick97d6d982018-08-10 15:53:16 -0400404 SkPaint p;
405 p.setStyle(SkPaint::kStroke_Style);
Kevin Lubick11194ab2018-08-17 13:52:56 -0400406 p.setStrokeCap(opts.cap);
407 p.setStrokeJoin(opts.join);
408 p.setStrokeWidth(opts.width);
409 p.setStrokeMiter(opts.miter_limit);
Kevin Lubick97d6d982018-08-10 15:53:16 -0400410
Kevin Lubick11194ab2018-08-17 13:52:56 -0400411 return p.getFillPath(path, &path);
Kevin Lubick97d6d982018-08-10 15:53:16 -0400412}
413
Kevin Lubick92eaa3c2018-07-16 21:00:52 -0400414//========================================================================================
Kevin Lubick084d9962018-08-15 13:28:27 -0400415// Matrix things
416//========================================================================================
417
418struct SimpleMatrix {
419 SkScalar scaleX, skewX, transX;
420 SkScalar skewY, scaleY, transY;
421 SkScalar pers0, pers1, pers2;
422};
423
424SkMatrix toSkMatrix(const SimpleMatrix& sm) {
425 return SkMatrix::MakeAll(sm.scaleX, sm.skewX , sm.transX,
426 sm.skewY , sm.scaleY, sm.transY,
427 sm.pers0 , sm.pers1 , sm.pers2);
428}
429
Kevin Lubick11194ab2018-08-17 13:52:56 -0400430void ApplyTransform(SkPath& orig, const SimpleMatrix& sm) {
431 orig.transform(toSkMatrix(sm));
Kevin Lubick084d9962018-08-15 13:28:27 -0400432}
433
Kevin Lubick11194ab2018-08-17 13:52:56 -0400434void ApplyTransform(SkPath& orig,
435 SkScalar scaleX, SkScalar skewX, SkScalar transX,
436 SkScalar skewY, SkScalar scaleY, SkScalar transY,
437 SkScalar pers0, SkScalar pers1, SkScalar pers2) {
Kevin Lubick084d9962018-08-15 13:28:27 -0400438 SkMatrix m = SkMatrix::MakeAll(scaleX, skewX , transX,
439 skewY , scaleY, transY,
440 pers0 , pers1 , pers2);
Kevin Lubick11194ab2018-08-17 13:52:56 -0400441 orig.transform(m);
Kevin Lubick084d9962018-08-15 13:28:27 -0400442}
443
444//========================================================================================
Kevin Lubick644d8e72018-08-09 13:58:04 -0400445// Testing things
Kevin Lubick92eaa3c2018-07-16 21:00:52 -0400446//========================================================================================
447
Kevin Lubick644d8e72018-08-09 13:58:04 -0400448// The use case for this is on the JS side is something like:
449// PathKit.SkBits2FloatUnsigned(parseInt("0xc0a00000"))
450// to have precise float values for tests. In the C++ tests, we can use SkBits2Float because
451// it takes int32_t, but the JS parseInt basically returns an unsigned int. So, we add in
452// this helper which casts for us on the way to SkBits2Float.
453float SkBits2FloatUnsigned(uint32_t floatAsBits) {
454 return SkBits2Float((int32_t) floatAsBits);
Kevin Lubick92eaa3c2018-07-16 21:00:52 -0400455}
456
Kevin Lubick22647d02018-07-06 14:31:23 -0400457// Binds the classes to the JS
Kevin Lubick641bf872018-08-06 14:49:39 -0400458//
459// See https://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/embind.html#non-member-functions-on-the-javascript-prototype
460// for more on binding non-member functions to the JS object, allowing us to rewire
461// various functions. That is, we can make the SkPath we expose appear to have methods
462// that the original SkPath does not, like rect(x, y, width, height) and toPath2D().
463//
464// An important detail for binding non-member functions is that the first argument
465// must be SkPath& (the reference part is very important).
Kevin Lubick11194ab2018-08-17 13:52:56 -0400466//
467// Note that we can't expose default or optional arguments, but we can have multiple
468// declarations of the same function that take different amounts of arguments.
469// For example, see _transform
470// Additionally, we are perfectly happy to handle default arguments and function
471// overloads in the JS glue code (see chaining.js::addPath() for an example).
Kevin Lubick22647d02018-07-06 14:31:23 -0400472EMSCRIPTEN_BINDINGS(skia) {
473 class_<SkPath>("SkPath")
474 .constructor<>()
Kevin Lubick084d9962018-08-15 13:28:27 -0400475 .constructor<const SkPath&>()
Kevin Lubick22647d02018-07-06 14:31:23 -0400476
Kevin Lubick641bf872018-08-06 14:49:39 -0400477 // Path2D API
Kevin Lubick11194ab2018-08-17 13:52:56 -0400478 .function("_addPath", &ApplyAddPath)
479 // 3 additional overloads of addPath are handled in JS bindings
480 .function("_arc", &ApplyAddArc)
481 .function("_arcTo", &ApplyArcTo)
482 //"bezierCurveTo" alias handled in JS bindings
483 .function("_close", &ApplyClose)
484 .function("_conicTo", &ApplyConicTo)
485 .function("_cubicTo", &ApplyCubicTo)
486 //"closePath" alias handled in JS bindings
Kevin Lubick641bf872018-08-06 14:49:39 -0400487
Kevin Lubick11194ab2018-08-17 13:52:56 -0400488 .function("_ellipse", &ApplyEllipse)
489 .function("_lineTo", &ApplyLineTo)
490 .function("_moveTo", &ApplyMoveTo)
491 // "quadraticCurveTo" alias handled in JS bindings
492 .function("_quadTo", &ApplyQuadTo)
493 .function("_rect", &ApplyAddRect)
Kevin Lubick641bf872018-08-06 14:49:39 -0400494
Kevin Lubick644d8e72018-08-09 13:58:04 -0400495 // Extra features
496 .function("setFillType", &SkPath::setFillType)
497 .function("getFillType", &SkPath::getFillType)
Kevin Lubick084d9962018-08-15 13:28:27 -0400498 .function("getFillTypeString", &GetFillTypeString)
Kevin Lubick644d8e72018-08-09 13:58:04 -0400499 .function("getBounds", &SkPath::getBounds)
500 .function("computeTightBounds", &SkPath::computeTightBounds)
Kevin Lubick084d9962018-08-15 13:28:27 -0400501 .function("equals", &Equals)
502 .function("copy", &CopyPath)
Kevin Lubick644d8e72018-08-09 13:58:04 -0400503
Kevin Lubick97d6d982018-08-10 15:53:16 -0400504 // PathEffects
Kevin Lubick11194ab2018-08-17 13:52:56 -0400505 .function("_dash", &ApplyDash)
506 .function("_trim", &ApplyTrim)
507 .function("_stroke", &ApplyStroke)
Kevin Lubick97d6d982018-08-10 15:53:16 -0400508
Kevin Lubick084d9962018-08-15 13:28:27 -0400509 // Matrix
Kevin Lubick11194ab2018-08-17 13:52:56 -0400510 .function("_transform", select_overload<void(SkPath& orig, const SimpleMatrix& sm)>(&ApplyTransform))
511 .function("_transform", select_overload<void(SkPath& orig, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar)>(&ApplyTransform))
Kevin Lubick084d9962018-08-15 13:28:27 -0400512
Kevin Lubick641bf872018-08-06 14:49:39 -0400513 // PathOps
Kevin Lubick11194ab2018-08-17 13:52:56 -0400514 .function("_simplify", &ApplySimplify)
515 .function("_op", &ApplyPathOp)
Kevin Lubick641bf872018-08-06 14:49:39 -0400516
517 // Exporting
518 .function("toCmds", &ToCmds)
519 .function("toPath2D", &ToPath2D)
520 .function("toCanvas", &ToCanvas)
521 .function("toSVGString", &ToSVGString)
522
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400523#ifdef PATHKIT_TESTING
524 .function("dump", select_overload<void() const>(&SkPath::dump))
Kevin Lubick97d6d982018-08-10 15:53:16 -0400525 .function("dumpHex", select_overload<void() const>(&SkPath::dumpHex))
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400526#endif
527 ;
Kevin Lubick22647d02018-07-06 14:31:23 -0400528
529 class_<SkOpBuilder>("SkOpBuilder")
530 .constructor<>()
531
Kevin Lubick641bf872018-08-06 14:49:39 -0400532 .function("add", &SkOpBuilder::add)
533 .function("resolve", &ResolveBuilder);
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400534
535 // Without these function() bindings, the function would be exposed but oblivious to
536 // our types (e.g. SkPath)
537
538 // Import
539 function("FromSVGString", &FromSVGString);
540 function("FromCmds", &FromCmds);
541 function("NewPath", &NewPath);
Kevin Lubick084d9962018-08-15 13:28:27 -0400542 function("NewPath", &CopyPath);
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400543 // Path2D is opaque, so we can't read in from it.
544
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400545 // PathOps
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400546 function("ApplyPathOp", &ApplyPathOp);
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400547
548 enum_<SkPathOp>("PathOp")
549 .value("DIFFERENCE", SkPathOp::kDifference_SkPathOp)
550 .value("INTERSECT", SkPathOp::kIntersect_SkPathOp)
551 .value("UNION", SkPathOp::kUnion_SkPathOp)
552 .value("XOR", SkPathOp::kXOR_SkPathOp)
553 .value("REVERSE_DIFFERENCE", SkPathOp::kReverseDifference_SkPathOp);
554
Kevin Lubick644d8e72018-08-09 13:58:04 -0400555 enum_<SkPath::FillType>("FillType")
556 .value("WINDING", SkPath::FillType::kWinding_FillType)
557 .value("EVENODD", SkPath::FillType::kEvenOdd_FillType)
558 .value("INVERSE_WINDING", SkPath::FillType::kInverseWinding_FillType)
559 .value("INVERSE_EVENODD", SkPath::FillType::kInverseEvenOdd_FillType);
560
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400561 constant("MOVE_VERB", MOVE);
562 constant("LINE_VERB", LINE);
563 constant("QUAD_VERB", QUAD);
Kevin Lubick97d6d982018-08-10 15:53:16 -0400564 constant("CONIC_VERB", CONIC);
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400565 constant("CUBIC_VERB", CUBIC);
566 constant("CLOSE_VERB", CLOSE);
567
Kevin Lubick644d8e72018-08-09 13:58:04 -0400568 // A value object is much simpler than a class - it is returned as a JS
569 // object and does not require delete().
570 // https://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/embind.html#value-types
571 value_object<SkRect>("SkRect")
572 .field("fLeft", &SkRect::fLeft)
573 .field("fTop", &SkRect::fTop)
574 .field("fRight", &SkRect::fRight)
575 .field("fBottom", &SkRect::fBottom);
576
577 function("MakeLTRBRect", &SkRect::MakeLTRB);
578
Kevin Lubick97d6d982018-08-10 15:53:16 -0400579 // Stroke
580 enum_<SkPaint::Join>("StrokeJoin")
581 .value("MITER", SkPaint::Join::kMiter_Join)
582 .value("ROUND", SkPaint::Join::kRound_Join)
583 .value("BEVEL", SkPaint::Join::kBevel_Join);
584
585 enum_<SkPaint::Cap>("StrokeCap")
586 .value("BUTT", SkPaint::Cap::kButt_Cap)
587 .value("ROUND", SkPaint::Cap::kRound_Cap)
588 .value("SQUARE", SkPaint::Cap::kSquare_Cap);
589
Kevin Lubick11194ab2018-08-17 13:52:56 -0400590 value_object<StrokeOpts>("StrokeOpts")
591 .field("width", &StrokeOpts::width)
592 .field("miter_limit", &StrokeOpts::miter_limit)
593 .field("join", &StrokeOpts::join)
594 .field("cap", &StrokeOpts::cap);
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400595
Kevin Lubick084d9962018-08-15 13:28:27 -0400596 // Matrix
597 // Allows clients to supply a 1D array of 9 elements and the bindings
598 // will automatically turn it into a 3x3 2D matrix.
599 // e.g. path.transform([0,1,2,3,4,5,6,7,8])
600 // This is likely simpler for the client than exposing SkMatrix
601 // directly and requiring them to do a lot of .delete().
602 value_array<SimpleMatrix>("SkMatrix")
603 .element(&SimpleMatrix::scaleX)
604 .element(&SimpleMatrix::skewX)
605 .element(&SimpleMatrix::transX)
606
607 .element(&SimpleMatrix::skewY)
608 .element(&SimpleMatrix::scaleY)
609 .element(&SimpleMatrix::transY)
610
611 .element(&SimpleMatrix::pers0)
612 .element(&SimpleMatrix::pers1)
613 .element(&SimpleMatrix::pers2);
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400614
Kevin Lubick644d8e72018-08-09 13:58:04 -0400615 // Test Utils
616 function("SkBits2FloatUnsigned", &SkBits2FloatUnsigned);
Kevin Lubick22647d02018-07-06 14:31:23 -0400617}