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" |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 14 | #include "SkStrokeRec.h" |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 15 | #include "SkPath.h" |
| 16 | #include "SkPathOps.h" |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 17 | #include "SkRect.h" |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 18 | #include "SkPaintDefaults.h" |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 19 | #include "SkString.h" |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 20 | #include "SkTrimPathEffect.h" |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 21 | |
| 22 | #include <emscripten/emscripten.h> |
| 23 | #include <emscripten/bind.h> |
| 24 | |
| 25 | using namespace emscripten; |
| 26 | |
| 27 | static const int MOVE = 0; |
| 28 | static const int LINE = 1; |
| 29 | static const int QUAD = 2; |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 30 | static const int CONIC = 3; |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 31 | static const int CUBIC = 4; |
| 32 | static const int CLOSE = 5; |
| 33 | |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 34 | // 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] | 35 | // 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] | 36 | // returned; |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 37 | using SkPathOrNull = emscripten::val; |
| 38 | // Self-documenting for when we return a string |
| 39 | using JSString = emscripten::val; |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 40 | |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 41 | // ================================================================================= |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 42 | // Creating/Exporting Paths with cmd arrays |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 43 | // ================================================================================= |
| 44 | |
Florin Malita | e1824da | 2018-07-12 10:33:39 -0400 | [diff] [blame] | 45 | template <typename VisitFunc> |
| 46 | void VisitPath(const SkPath& p, VisitFunc&& f) { |
| 47 | SkPath::RawIter iter(p); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 48 | SkPoint pts[4]; |
| 49 | SkPath::Verb verb; |
Florin Malita | e1824da | 2018-07-12 10:33:39 -0400 | [diff] [blame] | 50 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 51 | f(verb, pts, iter); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 55 | emscripten::val EMSCRIPTEN_KEEPALIVE ToCmds(const SkPath& path) { |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 56 | emscripten::val cmds = emscripten::val::array(); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 57 | |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 58 | 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] | 59 | emscripten::val cmd = emscripten::val::array(); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 60 | switch (verb) { |
Florin Malita | e1824da | 2018-07-12 10:33:39 -0400 | [diff] [blame] | 61 | case SkPath::kMove_Verb: |
| 62 | cmd.call<void>("push", MOVE, pts[0].x(), pts[0].y()); |
| 63 | break; |
| 64 | case SkPath::kLine_Verb: |
| 65 | cmd.call<void>("push", LINE, pts[1].x(), pts[1].y()); |
| 66 | break; |
| 67 | case SkPath::kQuad_Verb: |
| 68 | cmd.call<void>("push", QUAD, pts[1].x(), pts[1].y(), pts[2].x(), pts[2].y()); |
| 69 | break; |
| 70 | case SkPath::kConic_Verb: |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 71 | cmd.call<void>("push", CONIC, |
| 72 | pts[1].x(), pts[1].y(), |
| 73 | pts[2].x(), pts[2].y(), iter.conicWeight()); |
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 | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 103 | SkPathOrNull 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; |
Kevin Lubick | c623af2 | 2018-08-17 14:42:53 -0400 | [diff] [blame] | 133 | case CONIC: |
Kevin Lubick | c6c48aa | 2018-08-17 15:00:43 -0400 | [diff] [blame] | 134 | CHECK_NUM_ARGS(5); |
Kevin Lubick | c623af2 | 2018-08-17 14:42:53 -0400 | [diff] [blame] | 135 | x1 = cmds[i++], y1 = cmds[i++]; |
| 136 | x2 = cmds[i++], y2 = cmds[i++]; |
Kevin Lubick | d993648 | 2018-08-24 10:44:16 -0400 | [diff] [blame^] | 137 | x3 = cmds[i++]; // weight |
Kevin Lubick | c623af2 | 2018-08-17 14:42:53 -0400 | [diff] [blame] | 138 | path.conicTo(x1, y1, x2, y2, x3); |
| 139 | break; |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 140 | case CUBIC: |
| 141 | CHECK_NUM_ARGS(6); |
| 142 | x1 = cmds[i++], y1 = cmds[i++]; |
| 143 | x2 = cmds[i++], y2 = cmds[i++]; |
| 144 | x3 = cmds[i++], y3 = cmds[i++]; |
| 145 | path.cubicTo(x1, y1, x2, y2, x3, y3); |
| 146 | break; |
| 147 | case CLOSE: |
| 148 | path.close(); |
| 149 | break; |
| 150 | default: |
| 151 | SkDebugf(" path: UNKNOWN command %f, aborting dump...\n", cmds[i-1]); |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 152 | return emscripten::val::null(); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | #undef CHECK_NUM_ARGS |
| 157 | |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 158 | return emscripten::val(path); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 159 | } |
| 160 | |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 161 | SkPath EMSCRIPTEN_KEEPALIVE NewPath() { |
| 162 | return SkPath(); |
| 163 | } |
| 164 | |
Kevin Lubick | 084d996 | 2018-08-15 13:28:27 -0400 | [diff] [blame] | 165 | SkPath EMSCRIPTEN_KEEPALIVE CopyPath(const SkPath& a) { |
| 166 | SkPath copy(a); |
| 167 | return copy; |
| 168 | } |
| 169 | |
| 170 | bool EMSCRIPTEN_KEEPALIVE Equals(const SkPath& a, const SkPath& b) { |
| 171 | return a == b; |
| 172 | } |
| 173 | |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 174 | //======================================================================================== |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 175 | // Path things |
| 176 | //======================================================================================== |
| 177 | |
| 178 | // All these Apply* methods are simple wrappers to avoid returning an object. |
| 179 | // The default WASM bindings produce code that will leak if a return value |
| 180 | // isn't assigned to a JS variable and has delete() called on it. |
| 181 | // These Apply methods, combined with the smarter binding code allow for chainable |
| 182 | // commands that don't leak if the return value is ignored (i.e. when used intuitively). |
| 183 | |
| 184 | void ApplyArcTo(SkPath& p, SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, |
| 185 | SkScalar radius) { |
| 186 | p.arcTo(x1, y1, x2, y2, radius); |
| 187 | } |
| 188 | |
| 189 | void ApplyClose(SkPath& p) { |
| 190 | p.close(); |
| 191 | } |
| 192 | |
| 193 | void ApplyConicTo(SkPath& p, SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, |
| 194 | SkScalar w) { |
| 195 | p.conicTo(x1, y1, x2, y2, w); |
| 196 | } |
| 197 | |
| 198 | void ApplyCubicTo(SkPath& p, SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, |
| 199 | SkScalar x3, SkScalar y3) { |
| 200 | p.cubicTo(x1, y1, x2, y2, x3, y3); |
| 201 | } |
| 202 | |
| 203 | void ApplyLineTo(SkPath& p, SkScalar x, SkScalar y) { |
| 204 | p.lineTo(x, y); |
| 205 | } |
| 206 | |
| 207 | void ApplyMoveTo(SkPath& p, SkScalar x, SkScalar y) { |
| 208 | p.moveTo(x, y); |
| 209 | } |
| 210 | |
| 211 | void ApplyQuadTo(SkPath& p, SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) { |
| 212 | p.quadTo(x1, y1, x2, y2); |
| 213 | } |
| 214 | |
| 215 | |
| 216 | |
| 217 | //======================================================================================== |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 218 | // SVG things |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 219 | //======================================================================================== |
| 220 | |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 221 | JSString EMSCRIPTEN_KEEPALIVE ToSVGString(const SkPath& path) { |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 222 | SkString s; |
| 223 | SkParsePath::ToSVGString(path, &s); |
| 224 | // Wrapping it in val automatically turns it into a JS string. |
| 225 | // Not too sure on performance implications, but is is simpler than |
| 226 | // returning a raw pointer to const char * and then using |
| 227 | // Pointer_stringify() on the calling side. |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 228 | return emscripten::val(s.c_str()); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 232 | SkPathOrNull EMSCRIPTEN_KEEPALIVE FromSVGString(std::string str) { |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 233 | SkPath path; |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 234 | if (SkParsePath::FromSVGString(str.c_str(), &path)) { |
| 235 | return emscripten::val(path); |
| 236 | } |
| 237 | return emscripten::val::null(); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | //======================================================================================== |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 241 | // PATHOP things |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 242 | //======================================================================================== |
| 243 | |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 244 | bool EMSCRIPTEN_KEEPALIVE ApplySimplify(SkPath& path) { |
| 245 | return Simplify(path, &path); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 246 | } |
| 247 | |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 248 | bool EMSCRIPTEN_KEEPALIVE ApplyPathOp(SkPath& pathOne, const SkPath& pathTwo, SkPathOp op) { |
| 249 | return Op(pathOne, pathTwo, op, &pathOne); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 250 | } |
| 251 | |
Kevin Lubick | d993648 | 2018-08-24 10:44:16 -0400 | [diff] [blame^] | 252 | SkPathOrNull EMSCRIPTEN_KEEPALIVE MakeFromOp(const SkPath& pathOne, const SkPath& pathTwo, SkPathOp op) { |
| 253 | SkPath out; |
| 254 | if (Op(pathOne, pathTwo, op, &out)) { |
| 255 | return emscripten::val(out); |
| 256 | } |
| 257 | return emscripten::val::null(); |
| 258 | } |
| 259 | |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 260 | SkPathOrNull EMSCRIPTEN_KEEPALIVE ResolveBuilder(SkOpBuilder& builder) { |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 261 | SkPath path; |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 262 | if (builder.resolve(&path)) { |
| 263 | return emscripten::val(path); |
| 264 | } |
| 265 | return emscripten::val::null(); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | //======================================================================================== |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 269 | // Canvas things |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 270 | //======================================================================================== |
| 271 | |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 272 | 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] | 273 | SkPath::Iter iter(path, false); |
| 274 | SkPoint pts[4]; |
| 275 | SkPath::Verb verb; |
| 276 | while ((verb = iter.next(pts, false)) != SkPath::kDone_Verb) { |
| 277 | switch (verb) { |
| 278 | case SkPath::kMove_Verb: |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 279 | ctx.call<void>("moveTo", pts[0].x(), pts[0].y()); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 280 | break; |
| 281 | case SkPath::kLine_Verb: |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 282 | ctx.call<void>("lineTo", pts[1].x(), pts[1].y()); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 283 | break; |
| 284 | case SkPath::kQuad_Verb: |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 285 | 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] | 286 | break; |
| 287 | case SkPath::kConic_Verb: |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 288 | SkPoint quads[5]; |
| 289 | // approximate with 2^1=2 quads. |
| 290 | SkPath::ConvertConicToQuads(pts[0], pts[1], pts[2], iter.conicWeight(), quads, 1); |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 291 | ctx.call<void>("quadraticCurveTo", quads[1].x(), quads[1].y(), quads[2].x(), quads[2].y()); |
| 292 | 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] | 293 | break; |
| 294 | case SkPath::kCubic_Verb: |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 295 | 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] | 296 | pts[3].x(), pts[3].y()); |
| 297 | break; |
| 298 | case SkPath::kClose_Verb: |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 299 | ctx.call<void>("closePath"); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 300 | break; |
| 301 | case SkPath::kDone_Verb: |
| 302 | break; |
| 303 | } |
| 304 | } |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | emscripten::val JSPath2D = emscripten::val::global("Path2D"); |
| 308 | |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 309 | emscripten::val EMSCRIPTEN_KEEPALIVE ToPath2D(const SkPath& path) { |
Kevin Lubick | 5f0e3a1 | 2018-08-07 11:30:12 -0400 | [diff] [blame] | 310 | emscripten::val retVal = JSPath2D.new_(); |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 311 | ToCanvas(path, retVal); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 312 | return retVal; |
| 313 | } |
| 314 | |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 315 | // ====================================================================================== |
| 316 | // Path2D API things |
| 317 | // ====================================================================================== |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 318 | void ApplyAddRect(SkPath& path, SkScalar x, SkScalar y, SkScalar width, SkScalar height) { |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 319 | path.addRect(x, y, x+width, y+height); |
| 320 | } |
| 321 | |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 322 | void ApplyAddArc(SkPath& path, SkScalar x, SkScalar y, SkScalar radius, |
| 323 | SkScalar startAngle, SkScalar endAngle, bool ccw) { |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 324 | SkPath temp; |
| 325 | SkRect bounds = SkRect::MakeLTRB(x-radius, y-radius, x+radius, y+radius); |
| 326 | const auto sweep = SkRadiansToDegrees(endAngle - startAngle) - 360 * ccw; |
| 327 | temp.addArc(bounds, SkRadiansToDegrees(startAngle), sweep); |
| 328 | path.addPath(temp, SkPath::kExtend_AddPathMode); |
| 329 | } |
| 330 | |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 331 | void ApplyEllipse(SkPath& path, SkScalar x, SkScalar y, SkScalar radiusX, SkScalar radiusY, |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 332 | SkScalar rotation, SkScalar startAngle, SkScalar endAngle, bool ccw) { |
| 333 | // This is easiest to do by making a new path and then extending the current path |
| 334 | // (this properly catches the cases of if there's a moveTo before this call or not). |
| 335 | SkRect bounds = SkRect::MakeLTRB(x-radiusX, y-radiusY, x+radiusX, y+radiusY); |
| 336 | SkPath temp; |
| 337 | const auto sweep = SkRadiansToDegrees(endAngle - startAngle) - (360 * ccw); |
| 338 | temp.addArc(bounds, SkRadiansToDegrees(startAngle), sweep); |
| 339 | |
| 340 | SkMatrix m; |
| 341 | m.setRotate(SkRadiansToDegrees(rotation), x, y); |
| 342 | path.addPath(temp, m, SkPath::kExtend_AddPathMode); |
| 343 | } |
| 344 | |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 345 | // Allows for full matix control. |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 346 | void ApplyAddPath(SkPath& orig, const SkPath& newPath, |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 347 | SkScalar scaleX, SkScalar skewX, SkScalar transX, |
| 348 | SkScalar skewY, SkScalar scaleY, SkScalar transY, |
| 349 | SkScalar pers0, SkScalar pers1, SkScalar pers2) { |
| 350 | SkMatrix m = SkMatrix::MakeAll(scaleX, skewX , transX, |
| 351 | skewY , scaleY, transY, |
| 352 | pers0 , pers1 , pers2); |
| 353 | orig.addPath(newPath, m); |
| 354 | } |
| 355 | |
Kevin Lubick | 084d996 | 2018-08-15 13:28:27 -0400 | [diff] [blame] | 356 | JSString GetFillTypeString(const SkPath& path) { |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 357 | if (path.getFillType() == SkPath::FillType::kWinding_FillType) { |
| 358 | return emscripten::val("nonzero"); |
| 359 | } else if (path.getFillType() == SkPath::FillType::kEvenOdd_FillType) { |
| 360 | return emscripten::val("evenodd"); |
| 361 | } else { |
| 362 | SkDebugf("warning: can't translate inverted filltype to HTML Canvas\n"); |
| 363 | return emscripten::val("nonzero"); //Use default |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | //======================================================================================== |
| 368 | // Path Effects |
| 369 | //======================================================================================== |
| 370 | |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 371 | bool ApplyDash(SkPath& path, SkScalar on, SkScalar off, SkScalar phase) { |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 372 | SkScalar intervals[] = { on, off }; |
| 373 | auto pe = SkDashPathEffect::Make(intervals, 2, phase); |
| 374 | if (!pe) { |
| 375 | SkDebugf("Invalid args to dash()\n"); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 376 | return false; |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 377 | } |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 378 | SkStrokeRec rec(SkStrokeRec::InitStyle::kHairline_InitStyle); |
| 379 | if (pe->filterPath(&path, path, &rec, nullptr)) { |
| 380 | return true; |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 381 | } |
| 382 | SkDebugf("Could not make dashed path\n"); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 383 | return false; |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 384 | } |
| 385 | |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 386 | bool ApplyTrim(SkPath& path, SkScalar startT, SkScalar stopT, bool isComplement) { |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 387 | auto mode = isComplement ? SkTrimPathEffect::Mode::kInverted : SkTrimPathEffect::Mode::kNormal; |
| 388 | auto pe = SkTrimPathEffect::Make(startT, stopT, mode); |
| 389 | if (!pe) { |
| 390 | SkDebugf("Invalid args to trim(): startT and stopT must be in [0,1]\n"); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 391 | return false; |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 392 | } |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 393 | SkStrokeRec rec(SkStrokeRec::InitStyle::kHairline_InitStyle); |
| 394 | if (pe->filterPath(&path, path, &rec, nullptr)) { |
| 395 | return true; |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 396 | } |
| 397 | SkDebugf("Could not trim path\n"); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 398 | return false; |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 399 | } |
| 400 | |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 401 | struct StrokeOpts { |
| 402 | // Default values are set in chaining.js which allows clients |
| 403 | // to set any number of them. Otherwise, the binding code complains if |
| 404 | // any are omitted. |
| 405 | SkScalar width; |
| 406 | SkScalar miter_limit; |
| 407 | SkPaint::Join join; |
| 408 | SkPaint::Cap cap; |
| 409 | }; |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 410 | |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 411 | bool ApplyStroke(SkPath& path, StrokeOpts opts) { |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 412 | SkPaint p; |
| 413 | p.setStyle(SkPaint::kStroke_Style); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 414 | p.setStrokeCap(opts.cap); |
| 415 | p.setStrokeJoin(opts.join); |
| 416 | p.setStrokeWidth(opts.width); |
| 417 | p.setStrokeMiter(opts.miter_limit); |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 418 | |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 419 | return p.getFillPath(path, &path); |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 420 | } |
| 421 | |
Kevin Lubick | 92eaa3c | 2018-07-16 21:00:52 -0400 | [diff] [blame] | 422 | //======================================================================================== |
Kevin Lubick | 084d996 | 2018-08-15 13:28:27 -0400 | [diff] [blame] | 423 | // Matrix things |
| 424 | //======================================================================================== |
| 425 | |
| 426 | struct SimpleMatrix { |
| 427 | SkScalar scaleX, skewX, transX; |
| 428 | SkScalar skewY, scaleY, transY; |
| 429 | SkScalar pers0, pers1, pers2; |
| 430 | }; |
| 431 | |
| 432 | SkMatrix toSkMatrix(const SimpleMatrix& sm) { |
| 433 | return SkMatrix::MakeAll(sm.scaleX, sm.skewX , sm.transX, |
| 434 | sm.skewY , sm.scaleY, sm.transY, |
| 435 | sm.pers0 , sm.pers1 , sm.pers2); |
| 436 | } |
| 437 | |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 438 | void ApplyTransform(SkPath& orig, const SimpleMatrix& sm) { |
| 439 | orig.transform(toSkMatrix(sm)); |
Kevin Lubick | 084d996 | 2018-08-15 13:28:27 -0400 | [diff] [blame] | 440 | } |
| 441 | |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 442 | void ApplyTransform(SkPath& orig, |
| 443 | SkScalar scaleX, SkScalar skewX, SkScalar transX, |
| 444 | SkScalar skewY, SkScalar scaleY, SkScalar transY, |
| 445 | SkScalar pers0, SkScalar pers1, SkScalar pers2) { |
Kevin Lubick | 084d996 | 2018-08-15 13:28:27 -0400 | [diff] [blame] | 446 | SkMatrix m = SkMatrix::MakeAll(scaleX, skewX , transX, |
| 447 | skewY , scaleY, transY, |
| 448 | pers0 , pers1 , pers2); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 449 | orig.transform(m); |
Kevin Lubick | 084d996 | 2018-08-15 13:28:27 -0400 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | //======================================================================================== |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 453 | // Testing things |
Kevin Lubick | 92eaa3c | 2018-07-16 21:00:52 -0400 | [diff] [blame] | 454 | //======================================================================================== |
| 455 | |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 456 | // The use case for this is on the JS side is something like: |
| 457 | // PathKit.SkBits2FloatUnsigned(parseInt("0xc0a00000")) |
| 458 | // to have precise float values for tests. In the C++ tests, we can use SkBits2Float because |
| 459 | // it takes int32_t, but the JS parseInt basically returns an unsigned int. So, we add in |
| 460 | // this helper which casts for us on the way to SkBits2Float. |
| 461 | float SkBits2FloatUnsigned(uint32_t floatAsBits) { |
| 462 | return SkBits2Float((int32_t) floatAsBits); |
Kevin Lubick | 92eaa3c | 2018-07-16 21:00:52 -0400 | [diff] [blame] | 463 | } |
| 464 | |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 465 | // Binds the classes to the JS |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 466 | // |
| 467 | // See https://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/embind.html#non-member-functions-on-the-javascript-prototype |
| 468 | // for more on binding non-member functions to the JS object, allowing us to rewire |
| 469 | // various functions. That is, we can make the SkPath we expose appear to have methods |
| 470 | // that the original SkPath does not, like rect(x, y, width, height) and toPath2D(). |
| 471 | // |
| 472 | // An important detail for binding non-member functions is that the first argument |
| 473 | // must be SkPath& (the reference part is very important). |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 474 | // |
| 475 | // Note that we can't expose default or optional arguments, but we can have multiple |
| 476 | // declarations of the same function that take different amounts of arguments. |
| 477 | // For example, see _transform |
| 478 | // Additionally, we are perfectly happy to handle default arguments and function |
| 479 | // overloads in the JS glue code (see chaining.js::addPath() for an example). |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 480 | EMSCRIPTEN_BINDINGS(skia) { |
| 481 | class_<SkPath>("SkPath") |
| 482 | .constructor<>() |
Kevin Lubick | 084d996 | 2018-08-15 13:28:27 -0400 | [diff] [blame] | 483 | .constructor<const SkPath&>() |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 484 | |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 485 | // Path2D API |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 486 | .function("_addPath", &ApplyAddPath) |
| 487 | // 3 additional overloads of addPath are handled in JS bindings |
| 488 | .function("_arc", &ApplyAddArc) |
| 489 | .function("_arcTo", &ApplyArcTo) |
| 490 | //"bezierCurveTo" alias handled in JS bindings |
| 491 | .function("_close", &ApplyClose) |
Kevin Lubick | d993648 | 2018-08-24 10:44:16 -0400 | [diff] [blame^] | 492 | //"closePath" alias handled in JS bindings |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 493 | .function("_conicTo", &ApplyConicTo) |
| 494 | .function("_cubicTo", &ApplyCubicTo) |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 495 | |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 496 | .function("_ellipse", &ApplyEllipse) |
| 497 | .function("_lineTo", &ApplyLineTo) |
| 498 | .function("_moveTo", &ApplyMoveTo) |
| 499 | // "quadraticCurveTo" alias handled in JS bindings |
| 500 | .function("_quadTo", &ApplyQuadTo) |
| 501 | .function("_rect", &ApplyAddRect) |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 502 | |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 503 | // Extra features |
| 504 | .function("setFillType", &SkPath::setFillType) |
| 505 | .function("getFillType", &SkPath::getFillType) |
Kevin Lubick | 084d996 | 2018-08-15 13:28:27 -0400 | [diff] [blame] | 506 | .function("getFillTypeString", &GetFillTypeString) |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 507 | .function("getBounds", &SkPath::getBounds) |
| 508 | .function("computeTightBounds", &SkPath::computeTightBounds) |
Kevin Lubick | 084d996 | 2018-08-15 13:28:27 -0400 | [diff] [blame] | 509 | .function("equals", &Equals) |
| 510 | .function("copy", &CopyPath) |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 511 | |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 512 | // PathEffects |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 513 | .function("_dash", &ApplyDash) |
| 514 | .function("_trim", &ApplyTrim) |
| 515 | .function("_stroke", &ApplyStroke) |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 516 | |
Kevin Lubick | 084d996 | 2018-08-15 13:28:27 -0400 | [diff] [blame] | 517 | // Matrix |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 518 | .function("_transform", select_overload<void(SkPath& orig, const SimpleMatrix& sm)>(&ApplyTransform)) |
| 519 | .function("_transform", select_overload<void(SkPath& orig, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar)>(&ApplyTransform)) |
Kevin Lubick | 084d996 | 2018-08-15 13:28:27 -0400 | [diff] [blame] | 520 | |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 521 | // PathOps |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 522 | .function("_simplify", &ApplySimplify) |
| 523 | .function("_op", &ApplyPathOp) |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 524 | |
| 525 | // Exporting |
| 526 | .function("toCmds", &ToCmds) |
| 527 | .function("toPath2D", &ToPath2D) |
| 528 | .function("toCanvas", &ToCanvas) |
| 529 | .function("toSVGString", &ToSVGString) |
| 530 | |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 531 | #ifdef PATHKIT_TESTING |
| 532 | .function("dump", select_overload<void() const>(&SkPath::dump)) |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 533 | .function("dumpHex", select_overload<void() const>(&SkPath::dumpHex)) |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 534 | #endif |
| 535 | ; |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 536 | |
| 537 | class_<SkOpBuilder>("SkOpBuilder") |
| 538 | .constructor<>() |
| 539 | |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 540 | .function("add", &SkOpBuilder::add) |
Kevin Lubick | d993648 | 2018-08-24 10:44:16 -0400 | [diff] [blame^] | 541 | .function("make", &ResolveBuilder) |
Kevin Lubick | 641bf87 | 2018-08-06 14:49:39 -0400 | [diff] [blame] | 542 | .function("resolve", &ResolveBuilder); |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 543 | |
| 544 | // Without these function() bindings, the function would be exposed but oblivious to |
| 545 | // our types (e.g. SkPath) |
| 546 | |
| 547 | // Import |
| 548 | function("FromSVGString", &FromSVGString); |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 549 | function("NewPath", &NewPath); |
Kevin Lubick | 084d996 | 2018-08-15 13:28:27 -0400 | [diff] [blame] | 550 | function("NewPath", &CopyPath); |
Kevin Lubick | d993648 | 2018-08-24 10:44:16 -0400 | [diff] [blame^] | 551 | // FromCmds is defined in helper.js to make use of TypedArrays transparent. |
| 552 | function("_FromCmds", &FromCmds); |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 553 | // Path2D is opaque, so we can't read in from it. |
| 554 | |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 555 | // PathOps |
Kevin Lubick | d993648 | 2018-08-24 10:44:16 -0400 | [diff] [blame^] | 556 | function("MakeFromOp", &MakeFromOp); |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 557 | |
| 558 | enum_<SkPathOp>("PathOp") |
| 559 | .value("DIFFERENCE", SkPathOp::kDifference_SkPathOp) |
| 560 | .value("INTERSECT", SkPathOp::kIntersect_SkPathOp) |
| 561 | .value("UNION", SkPathOp::kUnion_SkPathOp) |
| 562 | .value("XOR", SkPathOp::kXOR_SkPathOp) |
| 563 | .value("REVERSE_DIFFERENCE", SkPathOp::kReverseDifference_SkPathOp); |
| 564 | |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 565 | enum_<SkPath::FillType>("FillType") |
| 566 | .value("WINDING", SkPath::FillType::kWinding_FillType) |
| 567 | .value("EVENODD", SkPath::FillType::kEvenOdd_FillType) |
| 568 | .value("INVERSE_WINDING", SkPath::FillType::kInverseWinding_FillType) |
| 569 | .value("INVERSE_EVENODD", SkPath::FillType::kInverseEvenOdd_FillType); |
| 570 | |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 571 | constant("MOVE_VERB", MOVE); |
| 572 | constant("LINE_VERB", LINE); |
| 573 | constant("QUAD_VERB", QUAD); |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 574 | constant("CONIC_VERB", CONIC); |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 575 | constant("CUBIC_VERB", CUBIC); |
| 576 | constant("CLOSE_VERB", CLOSE); |
| 577 | |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 578 | // A value object is much simpler than a class - it is returned as a JS |
| 579 | // object and does not require delete(). |
| 580 | // https://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/embind.html#value-types |
| 581 | value_object<SkRect>("SkRect") |
| 582 | .field("fLeft", &SkRect::fLeft) |
| 583 | .field("fTop", &SkRect::fTop) |
| 584 | .field("fRight", &SkRect::fRight) |
| 585 | .field("fBottom", &SkRect::fBottom); |
| 586 | |
Kevin Lubick | d993648 | 2018-08-24 10:44:16 -0400 | [diff] [blame^] | 587 | function("LTRBRect", &SkRect::MakeLTRB); |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 588 | |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 589 | // Stroke |
| 590 | enum_<SkPaint::Join>("StrokeJoin") |
| 591 | .value("MITER", SkPaint::Join::kMiter_Join) |
| 592 | .value("ROUND", SkPaint::Join::kRound_Join) |
| 593 | .value("BEVEL", SkPaint::Join::kBevel_Join); |
| 594 | |
| 595 | enum_<SkPaint::Cap>("StrokeCap") |
| 596 | .value("BUTT", SkPaint::Cap::kButt_Cap) |
| 597 | .value("ROUND", SkPaint::Cap::kRound_Cap) |
| 598 | .value("SQUARE", SkPaint::Cap::kSquare_Cap); |
| 599 | |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 600 | value_object<StrokeOpts>("StrokeOpts") |
| 601 | .field("width", &StrokeOpts::width) |
| 602 | .field("miter_limit", &StrokeOpts::miter_limit) |
| 603 | .field("join", &StrokeOpts::join) |
| 604 | .field("cap", &StrokeOpts::cap); |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 605 | |
Kevin Lubick | 084d996 | 2018-08-15 13:28:27 -0400 | [diff] [blame] | 606 | // Matrix |
| 607 | // Allows clients to supply a 1D array of 9 elements and the bindings |
| 608 | // will automatically turn it into a 3x3 2D matrix. |
| 609 | // e.g. path.transform([0,1,2,3,4,5,6,7,8]) |
| 610 | // This is likely simpler for the client than exposing SkMatrix |
| 611 | // directly and requiring them to do a lot of .delete(). |
| 612 | value_array<SimpleMatrix>("SkMatrix") |
| 613 | .element(&SimpleMatrix::scaleX) |
| 614 | .element(&SimpleMatrix::skewX) |
| 615 | .element(&SimpleMatrix::transX) |
| 616 | |
| 617 | .element(&SimpleMatrix::skewY) |
| 618 | .element(&SimpleMatrix::scaleY) |
| 619 | .element(&SimpleMatrix::transY) |
| 620 | |
| 621 | .element(&SimpleMatrix::pers0) |
| 622 | .element(&SimpleMatrix::pers1) |
| 623 | .element(&SimpleMatrix::pers2); |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 624 | |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 625 | // Test Utils |
| 626 | function("SkBits2FloatUnsigned", &SkBits2FloatUnsigned); |
Kevin Lubick | 22647d0 | 2018-07-06 14:31:23 -0400 | [diff] [blame] | 627 | } |