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 | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame^] | 8 | #include "SkDashPathEffect.h" |
Kevin Lubick | 92eaa3c | 2018-07-16 21:00:52 -0400 | [diff] [blame] | 9 | #include "SkFloatBits.h" |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 10 | #include "SkFloatingPoint.h" |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 11 | #include "SkMatrix.h" |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame^] | 12 | #include "SkPaint.h" |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 13 | #include "SkParsePath.h" |
| 14 | #include "SkPath.h" |
| 15 | #include "SkPathOps.h" |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 16 | #include "SkRect.h" |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 17 | #include "SkString.h" |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame^] | 18 | #include "SkTrimPathEffect.h" |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 19 | |
| 20 | #include <emscripten/emscripten.h> |
| 21 | #include <emscripten/bind.h> |
| 22 | |
| 23 | using namespace emscripten; |
| 24 | |
| 25 | static const int MOVE = 0; |
| 26 | static const int LINE = 1; |
| 27 | static const int QUAD = 2; |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame^] | 28 | static const int CONIC = 3; |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 29 | static const int CUBIC = 4; |
| 30 | static const int CLOSE = 5; |
| 31 | |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 32 | // Just for self-documenting purposes where the main thing being returned is an |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame^] | 33 | // SkPath, but in an error case, something of type null (which is val) could also be |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 34 | // returned; |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame^] | 35 | using SkPathOrNull = emscripten::val; |
| 36 | // Self-documenting for when we return a string |
| 37 | using JSString = emscripten::val; |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 38 | |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 39 | // ================================================================================= |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 40 | // Creating/Exporting Paths with cmd arrays |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 41 | // ================================================================================= |
| 42 | |
Florin Malita | e1824da | 2018-07-12 10:33:39 -0400 | [diff] [blame] | 43 | template <typename VisitFunc> |
| 44 | void VisitPath(const SkPath& p, VisitFunc&& f) { |
| 45 | SkPath::RawIter iter(p); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 46 | SkPoint pts[4]; |
| 47 | SkPath::Verb verb; |
Florin Malita | e1824da | 2018-07-12 10:33:39 -0400 | [diff] [blame] | 48 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 49 | f(verb, pts, iter); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 53 | emscripten::val EMSCRIPTEN_KEEPALIVE ToCmds(const SkPath& path) { |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 54 | emscripten::val cmds = emscripten::val::array(); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 55 | |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 56 | 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] | 57 | emscripten::val cmd = emscripten::val::array(); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 58 | switch (verb) { |
Florin Malita | e1824da | 2018-07-12 10:33:39 -0400 | [diff] [blame] | 59 | 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 Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame^] | 69 | cmd.call<void>("push", CONIC, |
| 70 | pts[1].x(), pts[1].y(), |
| 71 | pts[2].x(), pts[2].y(), iter.conicWeight()); |
Florin Malita | e1824da | 2018-07-12 10:33:39 -0400 | [diff] [blame] | 72 | 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 Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 85 | } |
| 86 | cmds.call<void>("push", cmd); |
Florin Malita | e1824da | 2018-07-12 10:33:39 -0400 | [diff] [blame] | 87 | }); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 88 | 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 Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame^] | 101 | SkPathOrNull EMSCRIPTEN_KEEPALIVE FromCmds(uintptr_t /* float* */ cptr, int numCmds) { |
Florin Malita | e1824da | 2018-07-12 10:33:39 -0400 | [diff] [blame] | 102 | const auto* cmds = reinterpret_cast<const float*>(cptr); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 103 | 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 Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 110 | return emscripten::val::null(); \ |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 111 | } |
| 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 Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 143 | return emscripten::val::null(); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | #undef CHECK_NUM_ARGS |
| 148 | |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 149 | return emscripten::val(path); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 150 | } |
| 151 | |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 152 | SkPath EMSCRIPTEN_KEEPALIVE NewPath() { |
| 153 | return SkPath(); |
| 154 | } |
| 155 | |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 156 | //======================================================================================== |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 157 | // SVG things |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 158 | //======================================================================================== |
| 159 | |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame^] | 160 | JSString EMSCRIPTEN_KEEPALIVE ToSVGString(const SkPath& path) { |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 161 | 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 Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 167 | return emscripten::val(s.c_str()); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame^] | 171 | SkPathOrNull EMSCRIPTEN_KEEPALIVE FromSVGString(std::string str) { |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 172 | SkPath path; |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 173 | if (SkParsePath::FromSVGString(str.c_str(), &path)) { |
| 174 | return emscripten::val(path); |
| 175 | } |
| 176 | return emscripten::val::null(); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | //======================================================================================== |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 180 | // PATHOP things |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 181 | //======================================================================================== |
| 182 | |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame^] | 183 | SkPathOrNull EMSCRIPTEN_KEEPALIVE SimplifyPath(const SkPath& path) { |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 184 | SkPath simple; |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 185 | if (Simplify(path, &simple)) { |
| 186 | return emscripten::val(simple); |
| 187 | } |
| 188 | return emscripten::val::null(); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 189 | } |
| 190 | |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame^] | 191 | SkPathOrNull EMSCRIPTEN_KEEPALIVE ApplyPathOp(const SkPath& pathOne, const SkPath& pathTwo, SkPathOp op) { |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 192 | SkPath path; |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 193 | if (Op(pathOne, pathTwo, op, &path)) { |
| 194 | return emscripten::val(path); |
| 195 | } |
| 196 | return emscripten::val::null(); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 197 | } |
| 198 | |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame^] | 199 | SkPathOrNull EMSCRIPTEN_KEEPALIVE ResolveBuilder(SkOpBuilder& builder) { |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 200 | SkPath path; |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 201 | if (builder.resolve(&path)) { |
| 202 | return emscripten::val(path); |
| 203 | } |
| 204 | return emscripten::val::null(); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | //======================================================================================== |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 208 | // Canvas things |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 209 | //======================================================================================== |
| 210 | |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 211 | 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] | 212 | 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 Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 218 | ctx.call<void>("moveTo", pts[0].x(), pts[0].y()); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 219 | break; |
| 220 | case SkPath::kLine_Verb: |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 221 | ctx.call<void>("lineTo", pts[1].x(), pts[1].y()); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 222 | break; |
| 223 | case SkPath::kQuad_Verb: |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 224 | 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] | 225 | break; |
| 226 | case SkPath::kConic_Verb: |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 227 | SkPoint quads[5]; |
| 228 | // approximate with 2^1=2 quads. |
| 229 | SkPath::ConvertConicToQuads(pts[0], pts[1], pts[2], iter.conicWeight(), quads, 1); |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 230 | 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 Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 232 | break; |
| 233 | case SkPath::kCubic_Verb: |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 234 | 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] | 235 | pts[3].x(), pts[3].y()); |
| 236 | break; |
| 237 | case SkPath::kClose_Verb: |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 238 | ctx.call<void>("closePath"); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 239 | break; |
| 240 | case SkPath::kDone_Verb: |
| 241 | break; |
| 242 | } |
| 243 | } |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | emscripten::val JSPath2D = emscripten::val::global("Path2D"); |
| 247 | |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 248 | emscripten::val EMSCRIPTEN_KEEPALIVE ToPath2D(const SkPath& path) { |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 249 | emscripten::val retVal = JSPath2D.new_(); |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 250 | ToCanvas(path, retVal); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 251 | return retVal; |
| 252 | } |
| 253 | |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 254 | // ====================================================================================== |
| 255 | // Path2D API things |
| 256 | // ====================================================================================== |
| 257 | void Path2DAddRect(SkPath& path, SkScalar x, SkScalar y, SkScalar width, SkScalar height) { |
| 258 | path.addRect(x, y, x+width, y+height); |
| 259 | } |
| 260 | |
| 261 | void 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 | |
| 270 | void 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 | |
| 275 | void 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 | |
| 289 | void 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 | |
| 294 | void Path2DAddPath(SkPath& orig, const SkPath& newPath) { |
| 295 | orig.addPath(newPath); |
| 296 | } |
| 297 | |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 298 | void Path2DAddPath(SkPath& orig, const SkPath& newPath, emscripten::val /* SVGMatrix*/ t) { |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 299 | 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 |
| 309 | void 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. |
| 317 | void 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 Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame^] | 327 | JSString 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 | |
| 342 | SkPathOrNull 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 | |
| 357 | SkPathOrNull 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 | |
| 372 | SkPathOrNull PathEffectTrim(const SkPath& path, SkScalar startT, SkScalar stopT) { |
| 373 | return PathEffectTrim(path, startT, stopT, false); |
| 374 | } |
| 375 | |
| 376 | SkPathOrNull 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 Lubick | 92eaa3c | 2018-07-16 21:00:52 -0400 | [diff] [blame] | 391 | //======================================================================================== |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 392 | // Testing things |
Kevin Lubick | 92eaa3c | 2018-07-16 21:00:52 -0400 | [diff] [blame] | 393 | //======================================================================================== |
| 394 | |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 395 | // 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. |
| 400 | float SkBits2FloatUnsigned(uint32_t floatAsBits) { |
| 401 | return SkBits2Float((int32_t) floatAsBits); |
Kevin Lubick | 92eaa3c | 2018-07-16 21:00:52 -0400 | [diff] [blame] | 402 | } |
| 403 | |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 404 | // Binds the classes to the JS |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 405 | // |
| 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 Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 413 | EMSCRIPTEN_BINDINGS(skia) { |
| 414 | class_<SkPath>("SkPath") |
| 415 | .constructor<>() |
| 416 | |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 417 | // Path2D API |
| 418 | .function("addPath", |
| 419 | select_overload<void(SkPath&, const SkPath&)>(&Path2DAddPath)) |
| 420 | .function("addPath", |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 421 | select_overload<void(SkPath&, const SkPath&, emscripten::val)>(&Path2DAddPath)) |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 422 | .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 Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 435 | .function("lineTo", |
| 436 | select_overload<void(SkScalar, SkScalar)>(&SkPath::lineTo)) |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 437 | .function("moveTo", |
| 438 | select_overload<void(SkScalar, SkScalar)>(&SkPath::moveTo)) |
| 439 | .function("quadraticCurveTo", |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 440 | select_overload<void(SkScalar, SkScalar, SkScalar, SkScalar)>(&SkPath::quadTo)) |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 441 | .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 Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame^] | 449 | .function("conicTo", |
| 450 | select_overload<void(SkScalar, SkScalar, SkScalar, SkScalar, SkScalar)>(&SkPath::conicTo)) |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 451 | .function("cubicTo", |
| 452 | select_overload<void(SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar)>(&SkPath::cubicTo)) |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 453 | .function("quadTo", |
| 454 | select_overload<void(SkScalar, SkScalar, SkScalar, SkScalar)>(&SkPath::quadTo)) |
| 455 | |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 456 | // Extra features |
| 457 | .function("setFillType", &SkPath::setFillType) |
| 458 | .function("getFillType", &SkPath::getFillType) |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame^] | 459 | .function("getCanvasFillType", &GetCanvasFillType) |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 460 | .function("getBounds", &SkPath::getBounds) |
| 461 | .function("computeTightBounds", &SkPath::computeTightBounds) |
| 462 | |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame^] | 463 | // 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 Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 469 | // 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 Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 479 | #ifdef PATHKIT_TESTING |
| 480 | .function("dump", select_overload<void() const>(&SkPath::dump)) |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame^] | 481 | .function("dumpHex", select_overload<void() const>(&SkPath::dumpHex)) |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 482 | #endif |
| 483 | ; |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 484 | |
| 485 | class_<SkOpBuilder>("SkOpBuilder") |
| 486 | .constructor<>() |
| 487 | |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 488 | .function("add", &SkOpBuilder::add) |
| 489 | .function("resolve", &ResolveBuilder); |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 490 | |
| 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 Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 500 | // PathOps |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 501 | function("ApplyPathOp", &ApplyPathOp); |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 502 | |
| 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 Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 510 | 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 Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 516 | constant("MOVE_VERB", MOVE); |
| 517 | constant("LINE_VERB", LINE); |
| 518 | constant("QUAD_VERB", QUAD); |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame^] | 519 | constant("CONIC_VERB", CONIC); |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 520 | constant("CUBIC_VERB", CUBIC); |
| 521 | constant("CLOSE_VERB", CLOSE); |
| 522 | |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 523 | // 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 Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame^] | 534 | // 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 Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 545 | |
| 546 | // coming soon - Matrix |
| 547 | |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 548 | // Test Utils |
| 549 | function("SkBits2FloatUnsigned", &SkBits2FloatUnsigned); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 550 | } |