Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 1 | /* |
| 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 Lubick | 92eaa3c | 2018-07-16 21:00:52 -0400 | [diff] [blame] | 8 | #include "SkFloatBits.h" |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 9 | #include "SkFloatingPoint.h" |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 10 | #include "SkMatrix.h" |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 11 | #include "SkParsePath.h" |
| 12 | #include "SkPath.h" |
| 13 | #include "SkPathOps.h" |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 14 | #include "SkRect.h" |
Kevin Lubick | 92eaa3c | 2018-07-16 21:00:52 -0400 | [diff] [blame] | 15 | #include "SkRegion.h" |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 16 | #include "SkString.h" |
| 17 | |
| 18 | #include <emscripten/emscripten.h> |
| 19 | #include <emscripten/bind.h> |
| 20 | |
| 21 | using namespace emscripten; |
| 22 | |
| 23 | static const int MOVE = 0; |
| 24 | static const int LINE = 1; |
| 25 | static const int QUAD = 2; |
| 26 | static const int CUBIC = 4; |
| 27 | static const int CLOSE = 5; |
| 28 | |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 29 | // Just for self-documenting purposes where the main thing being returned is an |
| 30 | // SkPath, but in an error case, something of type val (e.g. null) could also be |
| 31 | // returned; |
| 32 | using SkPathOrVal = emscripten::val; |
| 33 | |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 34 | // ================================================================================= |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 35 | // Creating/Exporting Paths with cmd arrays |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 36 | // ================================================================================= |
| 37 | |
Florin Malita | e1824da | 2018-07-12 10:33:39 -0400 | [diff] [blame] | 38 | template <typename VisitFunc> |
| 39 | void VisitPath(const SkPath& p, VisitFunc&& f) { |
| 40 | SkPath::RawIter iter(p); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 41 | SkPoint pts[4]; |
| 42 | SkPath::Verb verb; |
Florin Malita | e1824da | 2018-07-12 10:33:39 -0400 | [diff] [blame] | 43 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 44 | f(verb, pts, iter); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 48 | emscripten::val EMSCRIPTEN_KEEPALIVE ToCmds(const SkPath& path) { |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 49 | emscripten::val cmds = emscripten::val::array(); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 50 | |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 51 | VisitPath(path, [&cmds](SkPath::Verb verb, const SkPoint pts[4], SkPath::RawIter iter) { |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 52 | emscripten::val cmd = emscripten::val::array(); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 53 | switch (verb) { |
Florin Malita | e1824da | 2018-07-12 10:33:39 -0400 | [diff] [blame] | 54 | case SkPath::kMove_Verb: |
| 55 | cmd.call<void>("push", MOVE, pts[0].x(), pts[0].y()); |
| 56 | break; |
| 57 | case SkPath::kLine_Verb: |
| 58 | cmd.call<void>("push", LINE, pts[1].x(), pts[1].y()); |
| 59 | break; |
| 60 | case SkPath::kQuad_Verb: |
| 61 | cmd.call<void>("push", QUAD, pts[1].x(), pts[1].y(), pts[2].x(), pts[2].y()); |
| 62 | break; |
| 63 | case SkPath::kConic_Verb: |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 64 | SkPoint quads[5]; |
| 65 | // approximate with 2^1=2 quads. |
| 66 | SkPath::ConvertConicToQuads(pts[0], pts[1], pts[2], iter.conicWeight(), quads, 1); |
| 67 | cmd.call<void>("push", MOVE, quads[0].x(), quads[0].y()); |
| 68 | cmds.call<void>("push", cmd); |
| 69 | cmd = emscripten::val::array(); |
| 70 | cmd.call<void>("push", QUAD, quads[1].x(), quads[1].y(), quads[2].x(), quads[2].y()); |
| 71 | cmds.call<void>("push", cmd); |
| 72 | cmd = emscripten::val::array(); |
| 73 | cmd.call<void>("push", QUAD, quads[3].x(), quads[3].y(), quads[4].x(), quads[4].y()); |
Florin Malita | e1824da | 2018-07-12 10:33:39 -0400 | [diff] [blame] | 74 | 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 Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 87 | } |
| 88 | cmds.call<void>("push", cmd); |
Florin Malita | e1824da | 2018-07-12 10:33:39 -0400 | [diff] [blame] | 89 | }); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 90 | 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 Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 103 | SkPathOrVal EMSCRIPTEN_KEEPALIVE FromCmds(uintptr_t /* float* */ cptr, int numCmds) { |
Florin Malita | e1824da | 2018-07-12 10:33:39 -0400 | [diff] [blame] | 104 | const auto* cmds = reinterpret_cast<const float*>(cptr); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 105 | 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 Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 112 | return emscripten::val::null(); \ |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 113 | } |
| 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 Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 145 | return emscripten::val::null(); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | #undef CHECK_NUM_ARGS |
| 150 | |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 151 | return emscripten::val(path); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 152 | } |
| 153 | |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 154 | SkPath EMSCRIPTEN_KEEPALIVE NewPath() { |
| 155 | return SkPath(); |
| 156 | } |
| 157 | |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 158 | //======================================================================================== |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 159 | // SVG things |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 160 | //======================================================================================== |
| 161 | |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 162 | emscripten::val EMSCRIPTEN_KEEPALIVE ToSVGString(const SkPath& path) { |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 163 | SkString s; |
| 164 | SkParsePath::ToSVGString(path, &s); |
| 165 | // Wrapping it in val automatically turns it into a JS string. |
| 166 | // Not too sure on performance implications, but is is simpler than |
| 167 | // returning a raw pointer to const char * and then using |
| 168 | // Pointer_stringify() on the calling side. |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 169 | return emscripten::val(s.c_str()); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 173 | SkPathOrVal EMSCRIPTEN_KEEPALIVE FromSVGString(std::string str) { |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 174 | SkPath path; |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 175 | if (SkParsePath::FromSVGString(str.c_str(), &path)) { |
| 176 | return emscripten::val(path); |
| 177 | } |
| 178 | return emscripten::val::null(); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | //======================================================================================== |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 182 | // PATHOP things |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 183 | //======================================================================================== |
| 184 | |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 185 | SkPathOrVal EMSCRIPTEN_KEEPALIVE SimplifyPath(const SkPath& path) { |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 186 | SkPath simple; |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 187 | if (Simplify(path, &simple)) { |
| 188 | return emscripten::val(simple); |
| 189 | } |
| 190 | return emscripten::val::null(); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 191 | } |
| 192 | |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 193 | SkPathOrVal EMSCRIPTEN_KEEPALIVE ApplyPathOp(const SkPath& pathOne, const SkPath& pathTwo, SkPathOp op) { |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 194 | SkPath path; |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 195 | if (Op(pathOne, pathTwo, op, &path)) { |
| 196 | return emscripten::val(path); |
| 197 | } |
| 198 | return emscripten::val::null(); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 199 | } |
| 200 | |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 201 | SkPathOrVal EMSCRIPTEN_KEEPALIVE ResolveBuilder(SkOpBuilder& builder) { |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 202 | SkPath path; |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 203 | if (builder.resolve(&path)) { |
| 204 | return emscripten::val(path); |
| 205 | } |
| 206 | return emscripten::val::null(); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | //======================================================================================== |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 210 | // Canvas things |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 211 | //======================================================================================== |
| 212 | |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 213 | void EMSCRIPTEN_KEEPALIVE ToCanvas(const SkPath& path, emscripten::val /* Path2D or Canvas*/ ctx) { |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 214 | SkPath::Iter iter(path, false); |
| 215 | SkPoint pts[4]; |
| 216 | SkPath::Verb verb; |
| 217 | while ((verb = iter.next(pts, false)) != SkPath::kDone_Verb) { |
| 218 | switch (verb) { |
| 219 | case SkPath::kMove_Verb: |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 220 | ctx.call<void>("moveTo", pts[0].x(), pts[0].y()); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 221 | break; |
| 222 | case SkPath::kLine_Verb: |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 223 | ctx.call<void>("lineTo", pts[1].x(), pts[1].y()); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 224 | break; |
| 225 | case SkPath::kQuad_Verb: |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 226 | ctx.call<void>("quadraticCurveTo", pts[1].x(), pts[1].y(), pts[2].x(), pts[2].y()); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 227 | break; |
| 228 | case SkPath::kConic_Verb: |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 229 | SkPoint quads[5]; |
| 230 | // approximate with 2^1=2 quads. |
| 231 | SkPath::ConvertConicToQuads(pts[0], pts[1], pts[2], iter.conicWeight(), quads, 1); |
| 232 | ctx.call<void>("moveTo", quads[0].x(), quads[0].y()); |
| 233 | ctx.call<void>("quadraticCurveTo", quads[1].x(), quads[1].y(), quads[2].x(), quads[2].y()); |
| 234 | ctx.call<void>("quadraticCurveTo", quads[3].x(), quads[3].y(), quads[4].x(), quads[4].y()); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 235 | break; |
| 236 | case SkPath::kCubic_Verb: |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 237 | ctx.call<void>("bezierCurveTo", pts[1].x(), pts[1].y(), pts[2].x(), pts[2].y(), |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 238 | pts[3].x(), pts[3].y()); |
| 239 | break; |
| 240 | case SkPath::kClose_Verb: |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 241 | ctx.call<void>("closePath"); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 242 | break; |
| 243 | case SkPath::kDone_Verb: |
| 244 | break; |
| 245 | } |
| 246 | } |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | emscripten::val JSPath2D = emscripten::val::global("Path2D"); |
| 250 | |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 251 | emscripten::val EMSCRIPTEN_KEEPALIVE ToPath2D(const SkPath& path) { |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 252 | emscripten::val retVal = JSPath2D.new_(); |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 253 | ToCanvas(path, retVal); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 254 | return retVal; |
| 255 | } |
| 256 | |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 257 | // ====================================================================================== |
| 258 | // Path2D API things |
| 259 | // ====================================================================================== |
| 260 | void Path2DAddRect(SkPath& path, SkScalar x, SkScalar y, SkScalar width, SkScalar height) { |
| 261 | path.addRect(x, y, x+width, y+height); |
| 262 | } |
| 263 | |
| 264 | void Path2DAddArc(SkPath& path, SkScalar x, SkScalar y, SkScalar radius, |
| 265 | SkScalar startAngle, SkScalar endAngle, bool ccw) { |
| 266 | SkPath temp; |
| 267 | SkRect bounds = SkRect::MakeLTRB(x-radius, y-radius, x+radius, y+radius); |
| 268 | const auto sweep = SkRadiansToDegrees(endAngle - startAngle) - 360 * ccw; |
| 269 | temp.addArc(bounds, SkRadiansToDegrees(startAngle), sweep); |
| 270 | path.addPath(temp, SkPath::kExtend_AddPathMode); |
| 271 | } |
| 272 | |
| 273 | void Path2DAddArc(SkPath& path, SkScalar x, SkScalar y, SkScalar radius, |
| 274 | SkScalar startAngle, SkScalar endAngle) { |
| 275 | Path2DAddArc(path, x, y, radius, startAngle, endAngle, false); |
| 276 | } |
| 277 | |
| 278 | void Path2DAddEllipse(SkPath& path, SkScalar x, SkScalar y, SkScalar radiusX, SkScalar radiusY, |
| 279 | SkScalar rotation, SkScalar startAngle, SkScalar endAngle, bool ccw) { |
| 280 | // This is easiest to do by making a new path and then extending the current path |
| 281 | // (this properly catches the cases of if there's a moveTo before this call or not). |
| 282 | SkRect bounds = SkRect::MakeLTRB(x-radiusX, y-radiusY, x+radiusX, y+radiusY); |
| 283 | SkPath temp; |
| 284 | const auto sweep = SkRadiansToDegrees(endAngle - startAngle) - (360 * ccw); |
| 285 | temp.addArc(bounds, SkRadiansToDegrees(startAngle), sweep); |
| 286 | |
| 287 | SkMatrix m; |
| 288 | m.setRotate(SkRadiansToDegrees(rotation), x, y); |
| 289 | path.addPath(temp, m, SkPath::kExtend_AddPathMode); |
| 290 | } |
| 291 | |
| 292 | void Path2DAddEllipse(SkPath& path, SkScalar x, SkScalar y, SkScalar radiusX, SkScalar radiusY, |
| 293 | SkScalar rotation, SkScalar startAngle, SkScalar endAngle) { |
| 294 | Path2DAddEllipse(path, x, y, radiusX, radiusY, rotation, startAngle, endAngle, false); |
| 295 | } |
| 296 | |
| 297 | void Path2DAddPath(SkPath& orig, const SkPath& newPath) { |
| 298 | orig.addPath(newPath); |
| 299 | } |
| 300 | |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 301 | void Path2DAddPath(SkPath& orig, const SkPath& newPath, emscripten::val /* SVGMatrix*/ t) { |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 302 | SkMatrix m = SkMatrix::MakeAll( |
| 303 | t["a"].as<SkScalar>(), t["c"].as<SkScalar>(), t["e"].as<SkScalar>(), |
| 304 | t["b"].as<SkScalar>(), t["d"].as<SkScalar>(), t["f"].as<SkScalar>(), |
| 305 | 0 , 0 , 1); |
| 306 | orig.addPath(newPath, m); |
| 307 | } |
| 308 | |
| 309 | // Mimics the order of SVGMatrix, just w/o the SVG Matrix |
| 310 | // This order is scaleX, skewY, skewX, scaleY, transX, transY |
| 311 | // https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/transform#Transform_functions |
| 312 | void Path2DAddPath(SkPath& orig, const SkPath& newPath, SkScalar a, SkScalar b, SkScalar c, SkScalar d, SkScalar e, SkScalar f) { |
| 313 | SkMatrix m = SkMatrix::MakeAll(a, c, e, |
| 314 | b, d, f, |
| 315 | 0, 0, 1); |
| 316 | orig.addPath(newPath, m); |
| 317 | } |
| 318 | |
| 319 | // Allows for full matix control. |
| 320 | void Path2DAddPath(SkPath& orig, const SkPath& newPath, |
| 321 | SkScalar scaleX, SkScalar skewX, SkScalar transX, |
| 322 | SkScalar skewY, SkScalar scaleY, SkScalar transY, |
| 323 | SkScalar pers0, SkScalar pers1, SkScalar pers2) { |
| 324 | SkMatrix m = SkMatrix::MakeAll(scaleX, skewX , transX, |
| 325 | skewY , scaleY, transY, |
| 326 | pers0 , pers1 , pers2); |
| 327 | orig.addPath(newPath, m); |
| 328 | } |
| 329 | |
Kevin Lubick | 92eaa3c | 2018-07-16 21:00:52 -0400 | [diff] [blame] | 330 | //======================================================================================== |
| 331 | // Region things |
| 332 | //======================================================================================== |
| 333 | |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 334 | #ifdef PATHKIT_TESTING |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 335 | SkPathOrVal GetBoundaryPathFromRegion(SkRegion& region) { |
Kevin Lubick | 92eaa3c | 2018-07-16 21:00:52 -0400 | [diff] [blame] | 336 | SkPath p; |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 337 | if (region.getBoundaryPath(&p)) { |
| 338 | return emscripten::val(p); |
| 339 | } |
| 340 | return emscripten::val::null(); |
Kevin Lubick | 92eaa3c | 2018-07-16 21:00:52 -0400 | [diff] [blame] | 341 | } |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 342 | #endif |
Kevin Lubick | 92eaa3c | 2018-07-16 21:00:52 -0400 | [diff] [blame] | 343 | |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 344 | // Binds the classes to the JS |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 345 | // |
| 346 | // See https://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/embind.html#non-member-functions-on-the-javascript-prototype |
| 347 | // for more on binding non-member functions to the JS object, allowing us to rewire |
| 348 | // various functions. That is, we can make the SkPath we expose appear to have methods |
| 349 | // that the original SkPath does not, like rect(x, y, width, height) and toPath2D(). |
| 350 | // |
| 351 | // An important detail for binding non-member functions is that the first argument |
| 352 | // must be SkPath& (the reference part is very important). |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 353 | EMSCRIPTEN_BINDINGS(skia) { |
| 354 | class_<SkPath>("SkPath") |
| 355 | .constructor<>() |
| 356 | |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 357 | // Path2D API |
| 358 | .function("addPath", |
| 359 | select_overload<void(SkPath&, const SkPath&)>(&Path2DAddPath)) |
| 360 | .function("addPath", |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 361 | select_overload<void(SkPath&, const SkPath&, emscripten::val)>(&Path2DAddPath)) |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 362 | .function("arc", |
| 363 | select_overload<void(SkPath&, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar)>(&Path2DAddArc)) |
| 364 | .function("arc", |
| 365 | select_overload<void(SkPath&, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, bool)>(&Path2DAddArc)) |
| 366 | .function("arcTo", |
| 367 | select_overload<void(SkScalar, SkScalar, SkScalar, SkScalar, SkScalar)>(&SkPath::arcTo)) |
| 368 | .function("bezierCurveTo", |
| 369 | select_overload<void(SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar)>(&SkPath::cubicTo)) |
| 370 | .function("closePath", &SkPath::close) |
| 371 | .function("ellipse", |
| 372 | select_overload<void(SkPath&, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar)>(&Path2DAddEllipse)) |
| 373 | .function("ellipse", |
| 374 | select_overload<void(SkPath&, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, bool)>(&Path2DAddEllipse)) |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 375 | .function("lineTo", |
| 376 | select_overload<void(SkScalar, SkScalar)>(&SkPath::lineTo)) |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 377 | .function("moveTo", |
| 378 | select_overload<void(SkScalar, SkScalar)>(&SkPath::moveTo)) |
| 379 | .function("quadraticCurveTo", |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 380 | select_overload<void(SkScalar, SkScalar, SkScalar, SkScalar)>(&SkPath::quadTo)) |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 381 | .function("rect", &Path2DAddRect) |
| 382 | |
| 383 | // Some shorthand helpers, to mirror SkPath.cpp's API |
| 384 | .function("addPath", |
| 385 | select_overload<void(SkPath&, const SkPath&, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar)>(&Path2DAddPath)) |
| 386 | .function("addPath", |
| 387 | select_overload<void(SkPath&, const SkPath&, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar)>(&Path2DAddPath)) |
| 388 | .function("close", &SkPath::close) |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 389 | .function("cubicTo", |
| 390 | select_overload<void(SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar)>(&SkPath::cubicTo)) |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 391 | .function("quadTo", |
| 392 | select_overload<void(SkScalar, SkScalar, SkScalar, SkScalar)>(&SkPath::quadTo)) |
| 393 | |
| 394 | // PathOps |
| 395 | .function("simplify", &SimplifyPath) |
| 396 | .function("op", &ApplyPathOp) |
| 397 | |
| 398 | // Exporting |
| 399 | .function("toCmds", &ToCmds) |
| 400 | .function("toPath2D", &ToPath2D) |
| 401 | .function("toCanvas", &ToCanvas) |
| 402 | .function("toSVGString", &ToSVGString) |
| 403 | |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 404 | #ifdef PATHKIT_TESTING |
| 405 | .function("dump", select_overload<void() const>(&SkPath::dump)) |
| 406 | #endif |
| 407 | ; |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 408 | |
| 409 | class_<SkOpBuilder>("SkOpBuilder") |
| 410 | .constructor<>() |
| 411 | |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 412 | .function("add", &SkOpBuilder::add) |
| 413 | .function("resolve", &ResolveBuilder); |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 414 | |
| 415 | // Without these function() bindings, the function would be exposed but oblivious to |
| 416 | // our types (e.g. SkPath) |
| 417 | |
| 418 | // Import |
| 419 | function("FromSVGString", &FromSVGString); |
| 420 | function("FromCmds", &FromCmds); |
| 421 | function("NewPath", &NewPath); |
| 422 | // Path2D is opaque, so we can't read in from it. |
| 423 | |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 424 | // PathOps |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 425 | function("ApplyPathOp", &ApplyPathOp); |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 426 | |
| 427 | enum_<SkPathOp>("PathOp") |
| 428 | .value("DIFFERENCE", SkPathOp::kDifference_SkPathOp) |
| 429 | .value("INTERSECT", SkPathOp::kIntersect_SkPathOp) |
| 430 | .value("UNION", SkPathOp::kUnion_SkPathOp) |
| 431 | .value("XOR", SkPathOp::kXOR_SkPathOp) |
| 432 | .value("REVERSE_DIFFERENCE", SkPathOp::kReverseDifference_SkPathOp); |
| 433 | |
| 434 | constant("MOVE_VERB", MOVE); |
| 435 | constant("LINE_VERB", LINE); |
| 436 | constant("QUAD_VERB", QUAD); |
| 437 | constant("CUBIC_VERB", CUBIC); |
| 438 | constant("CLOSE_VERB", CLOSE); |
| 439 | |
| 440 | // coming soon - Stroke |
| 441 | |
| 442 | // coming soon - Matrix |
| 443 | |
| 444 | // coming soon - Bounds/Trim |
| 445 | |
| 446 | #ifdef PATHKIT_TESTING |
| 447 | function("SkBits2Float", &SkBits2Float); |
| 448 | |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 449 | enum_<SkRegion::Op>("RegionOp") |
| 450 | .value("DIFFERENCE", SkRegion::Op::kDifference_Op) |
| 451 | .value("INTERSECT", SkRegion::Op::kIntersect_Op) |
| 452 | .value("UNION", SkRegion::Op::kUnion_Op) |
| 453 | .value("XOR", SkRegion::Op::kXOR_Op) |
| 454 | .value("REVERSE_DIFFERENCE", SkRegion::Op::kReverseDifference_Op) |
| 455 | .value("REPLACE", SkRegion::Op::kReplace_Op); |
| 456 | |
Kevin Lubick | 92eaa3c | 2018-07-16 21:00:52 -0400 | [diff] [blame] | 457 | class_<SkRegion>("SkRegion") |
| 458 | .constructor<>() |
| 459 | |
| 460 | .function("setRect", |
| 461 | select_overload<bool(int32_t, int32_t, int32_t, int32_t)>(&SkRegion::setRect)) |
| 462 | .function("setPath", &SkRegion::setPath) |
| 463 | .function("opLTRB", |
| 464 | select_overload<bool(int32_t, int32_t, int32_t, int32_t, SkRegion::Op)>(&SkRegion::op)) |
| 465 | .function("opRegion", |
| 466 | select_overload<bool(const SkRegion&, SkRegion::Op)>(&SkRegion::op)) |
| 467 | .function("opRegionAB", |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 468 | select_overload<bool(const SkRegion&, const SkRegion&, SkRegion::Op)>(&SkRegion::op)) |
| 469 | |
| 470 | .function("getBoundaryPath", &GetBoundaryPathFromRegion); |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 471 | #endif |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 472 | } |