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