blob: 5909e39f2d5cc2fcde1be26f8446dbfdf6c88851 [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"
14#include "SkPath.h"
15#include "SkPathOps.h"
Kevin Lubick641bf872018-08-06 14:49:39 -040016#include "SkRect.h"
Kevin Lubick22647d02018-07-06 14:31:23 -040017#include "SkString.h"
Kevin Lubick97d6d982018-08-10 15:53:16 -040018#include "SkTrimPathEffect.h"
Kevin Lubick22647d02018-07-06 14:31:23 -040019
20#include <emscripten/emscripten.h>
21#include <emscripten/bind.h>
22
23using namespace emscripten;
24
25static const int MOVE = 0;
26static const int LINE = 1;
27static const int QUAD = 2;
Kevin Lubick97d6d982018-08-10 15:53:16 -040028static const int CONIC = 3;
Kevin Lubick22647d02018-07-06 14:31:23 -040029static const int CUBIC = 4;
30static const int CLOSE = 5;
31
Kevin Lubick5f0e3a12018-08-07 11:30:12 -040032// Just for self-documenting purposes where the main thing being returned is an
Kevin Lubick97d6d982018-08-10 15:53:16 -040033// SkPath, but in an error case, something of type null (which is val) could also be
Kevin Lubick5f0e3a12018-08-07 11:30:12 -040034// returned;
Kevin Lubick97d6d982018-08-10 15:53:16 -040035using SkPathOrNull = emscripten::val;
36// Self-documenting for when we return a string
37using JSString = emscripten::val;
Kevin Lubick5f0e3a12018-08-07 11:30:12 -040038
Kevin Lubick22647d02018-07-06 14:31:23 -040039// =================================================================================
Kevin Lubick641bf872018-08-06 14:49:39 -040040// Creating/Exporting Paths with cmd arrays
Kevin Lubick22647d02018-07-06 14:31:23 -040041// =================================================================================
42
Florin Malitae1824da2018-07-12 10:33:39 -040043template <typename VisitFunc>
44void VisitPath(const SkPath& p, VisitFunc&& f) {
45 SkPath::RawIter iter(p);
Kevin Lubick22647d02018-07-06 14:31:23 -040046 SkPoint pts[4];
47 SkPath::Verb verb;
Florin Malitae1824da2018-07-12 10:33:39 -040048 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
Kevin Lubick641bf872018-08-06 14:49:39 -040049 f(verb, pts, iter);
Kevin Lubick22647d02018-07-06 14:31:23 -040050 }
51}
52
Kevin Lubick641bf872018-08-06 14:49:39 -040053emscripten::val EMSCRIPTEN_KEEPALIVE ToCmds(const SkPath& path) {
Kevin Lubick5f0e3a12018-08-07 11:30:12 -040054 emscripten::val cmds = emscripten::val::array();
Kevin Lubick22647d02018-07-06 14:31:23 -040055
Kevin Lubick641bf872018-08-06 14:49:39 -040056 VisitPath(path, [&cmds](SkPath::Verb verb, const SkPoint pts[4], SkPath::RawIter iter) {
Kevin Lubick5f0e3a12018-08-07 11:30:12 -040057 emscripten::val cmd = emscripten::val::array();
Kevin Lubick22647d02018-07-06 14:31:23 -040058 switch (verb) {
Florin Malitae1824da2018-07-12 10:33:39 -040059 case SkPath::kMove_Verb:
60 cmd.call<void>("push", MOVE, pts[0].x(), pts[0].y());
61 break;
62 case SkPath::kLine_Verb:
63 cmd.call<void>("push", LINE, pts[1].x(), pts[1].y());
64 break;
65 case SkPath::kQuad_Verb:
66 cmd.call<void>("push", QUAD, pts[1].x(), pts[1].y(), pts[2].x(), pts[2].y());
67 break;
68 case SkPath::kConic_Verb:
Kevin Lubick97d6d982018-08-10 15:53:16 -040069 cmd.call<void>("push", CONIC,
70 pts[1].x(), pts[1].y(),
71 pts[2].x(), pts[2].y(), iter.conicWeight());
Florin Malitae1824da2018-07-12 10:33:39 -040072 break;
73 case SkPath::kCubic_Verb:
74 cmd.call<void>("push", CUBIC,
75 pts[1].x(), pts[1].y(),
76 pts[2].x(), pts[2].y(),
77 pts[3].x(), pts[3].y());
78 break;
79 case SkPath::kClose_Verb:
80 cmd.call<void>("push", CLOSE);
81 break;
82 case SkPath::kDone_Verb:
83 SkASSERT(false);
84 break;
Kevin Lubick22647d02018-07-06 14:31:23 -040085 }
86 cmds.call<void>("push", cmd);
Florin Malitae1824da2018-07-12 10:33:39 -040087 });
Kevin Lubick22647d02018-07-06 14:31:23 -040088 return cmds;
89}
90
91// This type signature is a mess, but it's necessary. See, we can't use "bind" (EMSCRIPTEN_BINDINGS)
92// and pointers to primitive types (Only bound types like SkPoint). We could if we used
93// cwrap (see https://becominghuman.ai/passing-and-returning-webassembly-array-parameters-a0f572c65d97)
94// but that requires us to stick to C code and, AFAIK, doesn't allow us to return nice things like
95// SkPath or SkOpBuilder.
96//
97// So, basically, if we are using C++ and EMSCRIPTEN_BINDINGS, we can't have primative pointers
98// in our function type signatures. (this gives an error message like "Cannot call foo due to unbound
99// types Pi, Pf"). But, we can just pretend they are numbers and cast them to be pointers and
100// the compiler is happy.
Kevin Lubick97d6d982018-08-10 15:53:16 -0400101SkPathOrNull EMSCRIPTEN_KEEPALIVE FromCmds(uintptr_t /* float* */ cptr, int numCmds) {
Florin Malitae1824da2018-07-12 10:33:39 -0400102 const auto* cmds = reinterpret_cast<const float*>(cptr);
Kevin Lubick22647d02018-07-06 14:31:23 -0400103 SkPath path;
104 float x1, y1, x2, y2, x3, y3;
105
106 // if there are not enough arguments, bail with the path we've constructed so far.
107 #define CHECK_NUM_ARGS(n) \
108 if ((i + n) > numCmds) { \
109 SkDebugf("Not enough args to match the verbs. Saw %d commands\n", numCmds); \
Kevin Lubick5f0e3a12018-08-07 11:30:12 -0400110 return emscripten::val::null(); \
Kevin Lubick22647d02018-07-06 14:31:23 -0400111 }
112
113 for(int i = 0; i < numCmds;){
114 switch (sk_float_floor2int(cmds[i++])) {
115 case MOVE:
116 CHECK_NUM_ARGS(2);
117 x1 = cmds[i++], y1 = cmds[i++];
118 path.moveTo(x1, y1);
119 break;
120 case LINE:
121 CHECK_NUM_ARGS(2);
122 x1 = cmds[i++], y1 = cmds[i++];
123 path.lineTo(x1, y1);
124 break;
125 case QUAD:
126 CHECK_NUM_ARGS(4);
127 x1 = cmds[i++], y1 = cmds[i++];
128 x2 = cmds[i++], y2 = cmds[i++];
129 path.quadTo(x1, y1, x2, y2);
130 break;
131 case CUBIC:
132 CHECK_NUM_ARGS(6);
133 x1 = cmds[i++], y1 = cmds[i++];
134 x2 = cmds[i++], y2 = cmds[i++];
135 x3 = cmds[i++], y3 = cmds[i++];
136 path.cubicTo(x1, y1, x2, y2, x3, y3);
137 break;
138 case CLOSE:
139 path.close();
140 break;
141 default:
142 SkDebugf(" path: UNKNOWN command %f, aborting dump...\n", cmds[i-1]);
Kevin Lubick5f0e3a12018-08-07 11:30:12 -0400143 return emscripten::val::null();
Kevin Lubick22647d02018-07-06 14:31:23 -0400144 }
145 }
146
147 #undef CHECK_NUM_ARGS
148
Kevin Lubick5f0e3a12018-08-07 11:30:12 -0400149 return emscripten::val(path);
Kevin Lubick22647d02018-07-06 14:31:23 -0400150}
151
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400152SkPath EMSCRIPTEN_KEEPALIVE NewPath() {
153 return SkPath();
154}
155
Kevin Lubick22647d02018-07-06 14:31:23 -0400156//========================================================================================
Kevin Lubick641bf872018-08-06 14:49:39 -0400157// SVG things
Kevin Lubick22647d02018-07-06 14:31:23 -0400158//========================================================================================
159
Kevin Lubick97d6d982018-08-10 15:53:16 -0400160JSString EMSCRIPTEN_KEEPALIVE ToSVGString(const SkPath& path) {
Kevin Lubick22647d02018-07-06 14:31:23 -0400161 SkString s;
162 SkParsePath::ToSVGString(path, &s);
163 // Wrapping it in val automatically turns it into a JS string.
164 // Not too sure on performance implications, but is is simpler than
165 // returning a raw pointer to const char * and then using
166 // Pointer_stringify() on the calling side.
Kevin Lubick5f0e3a12018-08-07 11:30:12 -0400167 return emscripten::val(s.c_str());
Kevin Lubick22647d02018-07-06 14:31:23 -0400168}
169
170
Kevin Lubick97d6d982018-08-10 15:53:16 -0400171SkPathOrNull EMSCRIPTEN_KEEPALIVE FromSVGString(std::string str) {
Kevin Lubick22647d02018-07-06 14:31:23 -0400172 SkPath path;
Kevin Lubick5f0e3a12018-08-07 11:30:12 -0400173 if (SkParsePath::FromSVGString(str.c_str(), &path)) {
174 return emscripten::val(path);
175 }
176 return emscripten::val::null();
Kevin Lubick22647d02018-07-06 14:31:23 -0400177}
178
179//========================================================================================
Kevin Lubick641bf872018-08-06 14:49:39 -0400180// PATHOP things
Kevin Lubick22647d02018-07-06 14:31:23 -0400181//========================================================================================
182
Kevin Lubick97d6d982018-08-10 15:53:16 -0400183SkPathOrNull EMSCRIPTEN_KEEPALIVE SimplifyPath(const SkPath& path) {
Kevin Lubick22647d02018-07-06 14:31:23 -0400184 SkPath simple;
Kevin Lubick5f0e3a12018-08-07 11:30:12 -0400185 if (Simplify(path, &simple)) {
186 return emscripten::val(simple);
187 }
188 return emscripten::val::null();
Kevin Lubick22647d02018-07-06 14:31:23 -0400189}
190
Kevin Lubick97d6d982018-08-10 15:53:16 -0400191SkPathOrNull EMSCRIPTEN_KEEPALIVE ApplyPathOp(const SkPath& pathOne, const SkPath& pathTwo, SkPathOp op) {
Kevin Lubick22647d02018-07-06 14:31:23 -0400192 SkPath path;
Kevin Lubick5f0e3a12018-08-07 11:30:12 -0400193 if (Op(pathOne, pathTwo, op, &path)) {
194 return emscripten::val(path);
195 }
196 return emscripten::val::null();
Kevin Lubick22647d02018-07-06 14:31:23 -0400197}
198
Kevin Lubick97d6d982018-08-10 15:53:16 -0400199SkPathOrNull EMSCRIPTEN_KEEPALIVE ResolveBuilder(SkOpBuilder& builder) {
Kevin Lubick22647d02018-07-06 14:31:23 -0400200 SkPath path;
Kevin Lubick5f0e3a12018-08-07 11:30:12 -0400201 if (builder.resolve(&path)) {
202 return emscripten::val(path);
203 }
204 return emscripten::val::null();
Kevin Lubick22647d02018-07-06 14:31:23 -0400205}
206
207//========================================================================================
Kevin Lubick641bf872018-08-06 14:49:39 -0400208// Canvas things
Kevin Lubick22647d02018-07-06 14:31:23 -0400209//========================================================================================
210
Kevin Lubick5f0e3a12018-08-07 11:30:12 -0400211void EMSCRIPTEN_KEEPALIVE ToCanvas(const SkPath& path, emscripten::val /* Path2D or Canvas*/ ctx) {
Kevin Lubick22647d02018-07-06 14:31:23 -0400212 SkPath::Iter iter(path, false);
213 SkPoint pts[4];
214 SkPath::Verb verb;
215 while ((verb = iter.next(pts, false)) != SkPath::kDone_Verb) {
216 switch (verb) {
217 case SkPath::kMove_Verb:
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400218 ctx.call<void>("moveTo", pts[0].x(), pts[0].y());
Kevin Lubick22647d02018-07-06 14:31:23 -0400219 break;
220 case SkPath::kLine_Verb:
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400221 ctx.call<void>("lineTo", pts[1].x(), pts[1].y());
Kevin Lubick22647d02018-07-06 14:31:23 -0400222 break;
223 case SkPath::kQuad_Verb:
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400224 ctx.call<void>("quadraticCurveTo", pts[1].x(), pts[1].y(), pts[2].x(), pts[2].y());
Kevin Lubick22647d02018-07-06 14:31:23 -0400225 break;
226 case SkPath::kConic_Verb:
Kevin Lubick641bf872018-08-06 14:49:39 -0400227 SkPoint quads[5];
228 // approximate with 2^1=2 quads.
229 SkPath::ConvertConicToQuads(pts[0], pts[1], pts[2], iter.conicWeight(), quads, 1);
Kevin Lubick641bf872018-08-06 14:49:39 -0400230 ctx.call<void>("quadraticCurveTo", quads[1].x(), quads[1].y(), quads[2].x(), quads[2].y());
231 ctx.call<void>("quadraticCurveTo", quads[3].x(), quads[3].y(), quads[4].x(), quads[4].y());
Kevin Lubick22647d02018-07-06 14:31:23 -0400232 break;
233 case SkPath::kCubic_Verb:
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400234 ctx.call<void>("bezierCurveTo", pts[1].x(), pts[1].y(), pts[2].x(), pts[2].y(),
Kevin Lubick22647d02018-07-06 14:31:23 -0400235 pts[3].x(), pts[3].y());
236 break;
237 case SkPath::kClose_Verb:
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400238 ctx.call<void>("closePath");
Kevin Lubick22647d02018-07-06 14:31:23 -0400239 break;
240 case SkPath::kDone_Verb:
241 break;
242 }
243 }
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400244}
245
246emscripten::val JSPath2D = emscripten::val::global("Path2D");
247
Kevin Lubick641bf872018-08-06 14:49:39 -0400248emscripten::val EMSCRIPTEN_KEEPALIVE ToPath2D(const SkPath& path) {
Kevin Lubick5f0e3a12018-08-07 11:30:12 -0400249 emscripten::val retVal = JSPath2D.new_();
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400250 ToCanvas(path, retVal);
Kevin Lubick22647d02018-07-06 14:31:23 -0400251 return retVal;
252}
253
Kevin Lubick641bf872018-08-06 14:49:39 -0400254// ======================================================================================
255// Path2D API things
256// ======================================================================================
257void Path2DAddRect(SkPath& path, SkScalar x, SkScalar y, SkScalar width, SkScalar height) {
258 path.addRect(x, y, x+width, y+height);
259}
260
261void Path2DAddArc(SkPath& path, SkScalar x, SkScalar y, SkScalar radius,
262 SkScalar startAngle, SkScalar endAngle, bool ccw) {
263 SkPath temp;
264 SkRect bounds = SkRect::MakeLTRB(x-radius, y-radius, x+radius, y+radius);
265 const auto sweep = SkRadiansToDegrees(endAngle - startAngle) - 360 * ccw;
266 temp.addArc(bounds, SkRadiansToDegrees(startAngle), sweep);
267 path.addPath(temp, SkPath::kExtend_AddPathMode);
268}
269
270void Path2DAddArc(SkPath& path, SkScalar x, SkScalar y, SkScalar radius,
271 SkScalar startAngle, SkScalar endAngle) {
272 Path2DAddArc(path, x, y, radius, startAngle, endAngle, false);
273}
274
275void Path2DAddEllipse(SkPath& path, SkScalar x, SkScalar y, SkScalar radiusX, SkScalar radiusY,
276 SkScalar rotation, SkScalar startAngle, SkScalar endAngle, bool ccw) {
277 // This is easiest to do by making a new path and then extending the current path
278 // (this properly catches the cases of if there's a moveTo before this call or not).
279 SkRect bounds = SkRect::MakeLTRB(x-radiusX, y-radiusY, x+radiusX, y+radiusY);
280 SkPath temp;
281 const auto sweep = SkRadiansToDegrees(endAngle - startAngle) - (360 * ccw);
282 temp.addArc(bounds, SkRadiansToDegrees(startAngle), sweep);
283
284 SkMatrix m;
285 m.setRotate(SkRadiansToDegrees(rotation), x, y);
286 path.addPath(temp, m, SkPath::kExtend_AddPathMode);
287}
288
289void Path2DAddEllipse(SkPath& path, SkScalar x, SkScalar y, SkScalar radiusX, SkScalar radiusY,
290 SkScalar rotation, SkScalar startAngle, SkScalar endAngle) {
291 Path2DAddEllipse(path, x, y, radiusX, radiusY, rotation, startAngle, endAngle, false);
292}
293
294void Path2DAddPath(SkPath& orig, const SkPath& newPath) {
295 orig.addPath(newPath);
296}
297
Kevin Lubick5f0e3a12018-08-07 11:30:12 -0400298void Path2DAddPath(SkPath& orig, const SkPath& newPath, emscripten::val /* SVGMatrix*/ t) {
Kevin Lubick641bf872018-08-06 14:49:39 -0400299 SkMatrix m = SkMatrix::MakeAll(
300 t["a"].as<SkScalar>(), t["c"].as<SkScalar>(), t["e"].as<SkScalar>(),
301 t["b"].as<SkScalar>(), t["d"].as<SkScalar>(), t["f"].as<SkScalar>(),
302 0 , 0 , 1);
303 orig.addPath(newPath, m);
304}
305
306// Mimics the order of SVGMatrix, just w/o the SVG Matrix
307// This order is scaleX, skewY, skewX, scaleY, transX, transY
308// https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/transform#Transform_functions
309void Path2DAddPath(SkPath& orig, const SkPath& newPath, SkScalar a, SkScalar b, SkScalar c, SkScalar d, SkScalar e, SkScalar f) {
310 SkMatrix m = SkMatrix::MakeAll(a, c, e,
311 b, d, f,
312 0, 0, 1);
313 orig.addPath(newPath, m);
314}
315
316// Allows for full matix control.
317void Path2DAddPath(SkPath& orig, const SkPath& newPath,
318 SkScalar scaleX, SkScalar skewX, SkScalar transX,
319 SkScalar skewY, SkScalar scaleY, SkScalar transY,
320 SkScalar pers0, SkScalar pers1, SkScalar pers2) {
321 SkMatrix m = SkMatrix::MakeAll(scaleX, skewX , transX,
322 skewY , scaleY, transY,
323 pers0 , pers1 , pers2);
324 orig.addPath(newPath, m);
325}
326
Kevin Lubick97d6d982018-08-10 15:53:16 -0400327JSString GetCanvasFillType(const SkPath& path) {
328 if (path.getFillType() == SkPath::FillType::kWinding_FillType) {
329 return emscripten::val("nonzero");
330 } else if (path.getFillType() == SkPath::FillType::kEvenOdd_FillType) {
331 return emscripten::val("evenodd");
332 } else {
333 SkDebugf("warning: can't translate inverted filltype to HTML Canvas\n");
334 return emscripten::val("nonzero"); //Use default
335 }
336}
337
338//========================================================================================
339// Path Effects
340//========================================================================================
341
342SkPathOrNull PathEffectDash(const SkPath& path, SkScalar on, SkScalar off, SkScalar phase) {
343 SkPath output;
344 SkScalar intervals[] = { on, off };
345 auto pe = SkDashPathEffect::Make(intervals, 2, phase);
346 if (!pe) {
347 SkDebugf("Invalid args to dash()\n");
348 return emscripten::val::null();
349 }
350 if (pe->filterPath(&output, path, nullptr, nullptr)) {
351 return emscripten::val(output);
352 }
353 SkDebugf("Could not make dashed path\n");
354 return emscripten::val::null();
355}
356
357SkPathOrNull PathEffectTrim(const SkPath& path, SkScalar startT, SkScalar stopT, bool isComplement) {
358 SkPath output;
359 auto mode = isComplement ? SkTrimPathEffect::Mode::kInverted : SkTrimPathEffect::Mode::kNormal;
360 auto pe = SkTrimPathEffect::Make(startT, stopT, mode);
361 if (!pe) {
362 SkDebugf("Invalid args to trim(): startT and stopT must be in [0,1]\n");
363 return emscripten::val::null();
364 }
365 if (pe->filterPath(&output, path, nullptr, nullptr)) {
366 return emscripten::val(output);
367 }
368 SkDebugf("Could not trim path\n");
369 return emscripten::val::null();
370}
371
372SkPathOrNull PathEffectTrim(const SkPath& path, SkScalar startT, SkScalar stopT) {
373 return PathEffectTrim(path, startT, stopT, false);
374}
375
376SkPathOrNull PathEffectStroke(const SkPath& path, SkScalar width, SkPaint::Join join, SkPaint::Cap cap) {
377 SkPath output;
378 SkPaint p;
379 p.setStyle(SkPaint::kStroke_Style);
380 p.setStrokeCap(cap);
381 p.setStrokeJoin(join);
382 p.setStrokeWidth(width);
383
384 if (p.getFillPath(path, &output)) {
385 return emscripten::val(output);
386 }
387 SkDebugf("Could not stroke path\n");
388 return emscripten::val::null();
389}
390
Kevin Lubick92eaa3c2018-07-16 21:00:52 -0400391//========================================================================================
Kevin Lubick644d8e72018-08-09 13:58:04 -0400392// Testing things
Kevin Lubick92eaa3c2018-07-16 21:00:52 -0400393//========================================================================================
394
Kevin Lubick644d8e72018-08-09 13:58:04 -0400395// The use case for this is on the JS side is something like:
396// PathKit.SkBits2FloatUnsigned(parseInt("0xc0a00000"))
397// to have precise float values for tests. In the C++ tests, we can use SkBits2Float because
398// it takes int32_t, but the JS parseInt basically returns an unsigned int. So, we add in
399// this helper which casts for us on the way to SkBits2Float.
400float SkBits2FloatUnsigned(uint32_t floatAsBits) {
401 return SkBits2Float((int32_t) floatAsBits);
Kevin Lubick92eaa3c2018-07-16 21:00:52 -0400402}
403
Kevin Lubick22647d02018-07-06 14:31:23 -0400404// Binds the classes to the JS
Kevin Lubick641bf872018-08-06 14:49:39 -0400405//
406// See https://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/embind.html#non-member-functions-on-the-javascript-prototype
407// for more on binding non-member functions to the JS object, allowing us to rewire
408// various functions. That is, we can make the SkPath we expose appear to have methods
409// that the original SkPath does not, like rect(x, y, width, height) and toPath2D().
410//
411// An important detail for binding non-member functions is that the first argument
412// must be SkPath& (the reference part is very important).
Kevin Lubick22647d02018-07-06 14:31:23 -0400413EMSCRIPTEN_BINDINGS(skia) {
414 class_<SkPath>("SkPath")
415 .constructor<>()
416
Kevin Lubick641bf872018-08-06 14:49:39 -0400417 // Path2D API
418 .function("addPath",
419 select_overload<void(SkPath&, const SkPath&)>(&Path2DAddPath))
420 .function("addPath",
Kevin Lubick5f0e3a12018-08-07 11:30:12 -0400421 select_overload<void(SkPath&, const SkPath&, emscripten::val)>(&Path2DAddPath))
Kevin Lubick641bf872018-08-06 14:49:39 -0400422 .function("arc",
423 select_overload<void(SkPath&, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar)>(&Path2DAddArc))
424 .function("arc",
425 select_overload<void(SkPath&, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, bool)>(&Path2DAddArc))
426 .function("arcTo",
427 select_overload<void(SkScalar, SkScalar, SkScalar, SkScalar, SkScalar)>(&SkPath::arcTo))
428 .function("bezierCurveTo",
429 select_overload<void(SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar)>(&SkPath::cubicTo))
430 .function("closePath", &SkPath::close)
431 .function("ellipse",
432 select_overload<void(SkPath&, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar)>(&Path2DAddEllipse))
433 .function("ellipse",
434 select_overload<void(SkPath&, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, bool)>(&Path2DAddEllipse))
Kevin Lubick22647d02018-07-06 14:31:23 -0400435 .function("lineTo",
436 select_overload<void(SkScalar, SkScalar)>(&SkPath::lineTo))
Kevin Lubick641bf872018-08-06 14:49:39 -0400437 .function("moveTo",
438 select_overload<void(SkScalar, SkScalar)>(&SkPath::moveTo))
439 .function("quadraticCurveTo",
Kevin Lubick22647d02018-07-06 14:31:23 -0400440 select_overload<void(SkScalar, SkScalar, SkScalar, SkScalar)>(&SkPath::quadTo))
Kevin Lubick641bf872018-08-06 14:49:39 -0400441 .function("rect", &Path2DAddRect)
442
443 // Some shorthand helpers, to mirror SkPath.cpp's API
444 .function("addPath",
445 select_overload<void(SkPath&, const SkPath&, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar)>(&Path2DAddPath))
446 .function("addPath",
447 select_overload<void(SkPath&, const SkPath&, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar)>(&Path2DAddPath))
448 .function("close", &SkPath::close)
Kevin Lubick97d6d982018-08-10 15:53:16 -0400449 .function("conicTo",
450 select_overload<void(SkScalar, SkScalar, SkScalar, SkScalar, SkScalar)>(&SkPath::conicTo))
Kevin Lubick22647d02018-07-06 14:31:23 -0400451 .function("cubicTo",
452 select_overload<void(SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar)>(&SkPath::cubicTo))
Kevin Lubick641bf872018-08-06 14:49:39 -0400453 .function("quadTo",
454 select_overload<void(SkScalar, SkScalar, SkScalar, SkScalar)>(&SkPath::quadTo))
455
Kevin Lubick644d8e72018-08-09 13:58:04 -0400456 // Extra features
457 .function("setFillType", &SkPath::setFillType)
458 .function("getFillType", &SkPath::getFillType)
Kevin Lubick97d6d982018-08-10 15:53:16 -0400459 .function("getCanvasFillType", &GetCanvasFillType)
Kevin Lubick644d8e72018-08-09 13:58:04 -0400460 .function("getBounds", &SkPath::getBounds)
461 .function("computeTightBounds", &SkPath::computeTightBounds)
462
Kevin Lubick97d6d982018-08-10 15:53:16 -0400463 // PathEffects
464 .function("dash", &PathEffectDash)
465 .function("trim", select_overload<SkPathOrNull(const SkPath&, SkScalar, SkScalar)>(&PathEffectTrim))
466 .function("trim", select_overload<SkPathOrNull(const SkPath&, SkScalar, SkScalar, bool)>(&PathEffectTrim))
467 .function("stroke", &PathEffectStroke)
468
Kevin Lubick641bf872018-08-06 14:49:39 -0400469 // PathOps
470 .function("simplify", &SimplifyPath)
471 .function("op", &ApplyPathOp)
472
473 // Exporting
474 .function("toCmds", &ToCmds)
475 .function("toPath2D", &ToPath2D)
476 .function("toCanvas", &ToCanvas)
477 .function("toSVGString", &ToSVGString)
478
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400479#ifdef PATHKIT_TESTING
480 .function("dump", select_overload<void() const>(&SkPath::dump))
Kevin Lubick97d6d982018-08-10 15:53:16 -0400481 .function("dumpHex", select_overload<void() const>(&SkPath::dumpHex))
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400482#endif
483 ;
Kevin Lubick22647d02018-07-06 14:31:23 -0400484
485 class_<SkOpBuilder>("SkOpBuilder")
486 .constructor<>()
487
Kevin Lubick641bf872018-08-06 14:49:39 -0400488 .function("add", &SkOpBuilder::add)
489 .function("resolve", &ResolveBuilder);
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400490
491 // Without these function() bindings, the function would be exposed but oblivious to
492 // our types (e.g. SkPath)
493
494 // Import
495 function("FromSVGString", &FromSVGString);
496 function("FromCmds", &FromCmds);
497 function("NewPath", &NewPath);
498 // Path2D is opaque, so we can't read in from it.
499
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400500 // PathOps
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400501 function("ApplyPathOp", &ApplyPathOp);
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400502
503 enum_<SkPathOp>("PathOp")
504 .value("DIFFERENCE", SkPathOp::kDifference_SkPathOp)
505 .value("INTERSECT", SkPathOp::kIntersect_SkPathOp)
506 .value("UNION", SkPathOp::kUnion_SkPathOp)
507 .value("XOR", SkPathOp::kXOR_SkPathOp)
508 .value("REVERSE_DIFFERENCE", SkPathOp::kReverseDifference_SkPathOp);
509
Kevin Lubick644d8e72018-08-09 13:58:04 -0400510 enum_<SkPath::FillType>("FillType")
511 .value("WINDING", SkPath::FillType::kWinding_FillType)
512 .value("EVENODD", SkPath::FillType::kEvenOdd_FillType)
513 .value("INVERSE_WINDING", SkPath::FillType::kInverseWinding_FillType)
514 .value("INVERSE_EVENODD", SkPath::FillType::kInverseEvenOdd_FillType);
515
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400516 constant("MOVE_VERB", MOVE);
517 constant("LINE_VERB", LINE);
518 constant("QUAD_VERB", QUAD);
Kevin Lubick97d6d982018-08-10 15:53:16 -0400519 constant("CONIC_VERB", CONIC);
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400520 constant("CUBIC_VERB", CUBIC);
521 constant("CLOSE_VERB", CLOSE);
522
Kevin Lubick644d8e72018-08-09 13:58:04 -0400523 // A value object is much simpler than a class - it is returned as a JS
524 // object and does not require delete().
525 // https://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/embind.html#value-types
526 value_object<SkRect>("SkRect")
527 .field("fLeft", &SkRect::fLeft)
528 .field("fTop", &SkRect::fTop)
529 .field("fRight", &SkRect::fRight)
530 .field("fBottom", &SkRect::fBottom);
531
532 function("MakeLTRBRect", &SkRect::MakeLTRB);
533
Kevin Lubick97d6d982018-08-10 15:53:16 -0400534 // Stroke
535 enum_<SkPaint::Join>("StrokeJoin")
536 .value("MITER", SkPaint::Join::kMiter_Join)
537 .value("ROUND", SkPaint::Join::kRound_Join)
538 .value("BEVEL", SkPaint::Join::kBevel_Join);
539
540 enum_<SkPaint::Cap>("StrokeCap")
541 .value("BUTT", SkPaint::Cap::kButt_Cap)
542 .value("ROUND", SkPaint::Cap::kRound_Cap)
543 .value("SQUARE", SkPaint::Cap::kSquare_Cap);
544
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400545
546 // coming soon - Matrix
547
Kevin Lubick644d8e72018-08-09 13:58:04 -0400548 // Test Utils
549 function("SkBits2FloatUnsigned", &SkBits2FloatUnsigned);
Kevin Lubick22647d02018-07-06 14:31:23 -0400550}