• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Adds JS functions to allow for chaining w/o leaking by re-using 'this' path.
2(function(PathKit){
3  // PathKit.onRuntimeInitialized is called after the WASM library has loaded.
4  // when onRuntimeInitialized is called, PathKit.SkPath is defined with many
5  // functions on it (see pathkit_wasm_bindings.cpp@EMSCRIPTEN_BINDINGS)
6  PathKit.onRuntimeInitialized = function() {
7    // All calls to 'this' need to go in externs.js so closure doesn't minify them away.
8
9    PathKit.SkPath.prototype.addPath = function() {
10      // Takes 1, 2, 7 or 10 args, where the first arg is always the path.
11      // The options for the remaining args are:
12      //   - an SVGMatrix
13      //   - the 6 parameters of an SVG Matrix
14      //   - the 9 parameters of a full Matrix
15      var path = arguments[0];
16      if (arguments.length === 1) {
17        // Add path, unchanged.  Use identity matrix
18        this._addPath(path, 1, 0, 0,
19                            0, 1, 0,
20                            0, 0, 1);
21      } else if (arguments.length === 2) {
22        // Takes SVGMatrix, which has its args in a counter-intuitive order
23        // https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/transform#Transform_functions
24        /**
25         * @type {SVGMatrix}
26         */
27        var sm = arguments[1];
28        this._addPath(path, sm.a, sm.c, sm.e,
29                            sm.b, sm.d, sm.f,
30                               0,    0,    1);
31      } else if (arguments.length === 7) {
32        // User provided the 6 params for an SVGMatrix directly.
33        var a = arguments;
34        this._addPath(path, a[1], a[3], a[5],
35                            a[2], a[4], a[6],
36                              0 ,   0 ,   1 );
37      } else if (arguments.length === 10) {
38        // User provided the 9 params of a (full) matrix directly.
39        // These are in the same order as what Skia expects.
40        var a = arguments;
41        this._addPath(path, a[1], a[2], a[3],
42                            a[4], a[5], a[6],
43                            a[7], a[8], a[9]);
44      } else {
45        console.err('addPath expected to take 1, 2, 7, or 10 args. Got ' + arguments.length);
46        return null;
47      }
48      return this;
49    };
50
51    // ccw (counter clock wise) is optional and defaults to false.
52    PathKit.SkPath.prototype.arc = function(x, y, radius, startAngle, endAngle, ccw) {
53      this._arc(x, y, radius, startAngle, endAngle, !!ccw);
54      return this;
55    };
56
57    PathKit.SkPath.prototype.arcTo = function(x1, y1, x2, y2, radius) {
58      this._arcTo(x1, y1, x2, y2, radius);
59      return this;
60    };
61
62    PathKit.SkPath.prototype.bezierCurveTo = function(cp1x, cp1y, cp2x, cp2y, x, y) {
63      this._cubicTo(cp1x, cp1y, cp2x, cp2y, x, y);
64      return this;
65    };
66
67    PathKit.SkPath.prototype.close = function() {
68      this._close();
69      return this;
70    };
71
72    // Reminder, we have some duplicate definitions because we want to be a
73    // superset of Path2D and also work like the original SkPath C++ object.
74    PathKit.SkPath.prototype.closePath = function() {
75      this._close();
76      return this;
77    };
78
79    PathKit.SkPath.prototype.conicTo = function(x1, y1, x2, y2, w) {
80      this._conicTo(x1, y1, x2, y2, w);
81      return this;
82    };
83
84    PathKit.SkPath.prototype.cubicTo = function(cp1x, cp1y, cp2x, cp2y, x, y) {
85      this._cubicTo(cp1x, cp1y, cp2x, cp2y, x, y);
86      return this;
87    };
88
89    PathKit.SkPath.prototype.dash = function(on, off, phase) {
90      if (this._dash(on, off, phase)) {
91        return this;
92      }
93      return null;
94    };
95
96    // ccw (counter clock wise) is optional and defaults to false.
97    PathKit.SkPath.prototype.ellipse = function(x, y, radiusX, radiusY, rotation, startAngle, endAngle, ccw) {
98      this._ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle, !!ccw);
99      return this;
100    };
101
102    PathKit.SkPath.prototype.lineTo = function(x, y) {
103      this._lineTo(x, y);
104      return this;
105    };
106
107    PathKit.SkPath.prototype.moveTo = function(x, y) {
108      this._moveTo(x, y);
109      return this;
110    };
111
112    PathKit.SkPath.prototype.op = function(otherPath, op) {
113      if (this._op(otherPath, op)) {
114        return this;
115      }
116      return null;
117    };
118
119    PathKit.SkPath.prototype.quadraticCurveTo = function(cpx, cpy, x, y) {
120      this._quadTo(cpx, cpy, x, y);
121      return this;
122    };
123
124    PathKit.SkPath.prototype.quadTo = function(cpx, cpy, x, y) {
125      this._quadTo(cpx, cpy, x, y);
126      return this;
127    };
128
129    PathKit.SkPath.prototype.rect = function(x, y, w, h) {
130      this._rect(x, y, w, h);
131      return this;
132    };
133
134    PathKit.SkPath.prototype.simplify = function() {
135      if (this._simplify()) {
136        return this;
137      }
138      return null;
139    };
140
141    PathKit.SkPath.prototype.stroke = function(opts) {
142      // Fill out any missing values with the default values.
143      /**
144       * See externs.js for this definition
145       * @type {StrokeOpts}
146       */
147      opts = opts || {};
148      opts.width = opts.width || 1;
149      opts.miter_limit = opts.miter_limit || 4;
150      opts.cap = opts.cap || PathKit.StrokeCap.BUTT;
151      opts.join = opts.join || PathKit.StrokeJoin.MITER;
152      if (this._stroke(opts)) {
153        return this;
154      }
155      return null;
156    };
157
158    PathKit.SkPath.prototype.transform = function() {
159      // Takes 1 or 9 args
160      if (arguments.length === 1) {
161        // argument 1 should be a 9 element array (which is transformed on the C++ side
162        // to a SimpleMatrix)
163        this._transform(arguments[0]);
164      } else if (arguments.length === 9) {
165        // these arguments are the 9 members of the matrix
166        var a = arguments;
167        this._transform(a[0], a[1], a[2],
168                        a[3], a[4], a[5],
169                        a[6], a[7], a[8]);
170      } else {
171        console.err('transform expected to take 1 or 9 arguments. Got ' + arguments.length);
172        return null;
173      }
174      return this;
175    };
176
177    // isComplement is optional, defaults to false
178    PathKit.SkPath.prototype.trim = function(startT, stopT, isComplement) {
179      if (this._trim(startT, stopT, !!isComplement)) {
180        return this;
181      }
182      return null;
183    };
184  };
185
186}(Module)); // When this file is loaded in, the high level object is "Module";
187
188