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