blob: d0e8d1931b8af95f88cad021c5491d999cdd2218 [file] [log] [blame]
Kevin Lubick217056c2018-09-20 17:39:31 -04001// Adds JS functions to augment the CanvasKit interface.
2// For example, if there is a wrapper around the C++ call or logic to allow
3// chaining, it should go here.
Kevin Lubick1a05fce2018-11-20 12:51:16 -05004
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05005// CanvasKit.onRuntimeInitialized is called after the WASM library has loaded.
6// Anything that modifies an exposed class (e.g. SkPath) should be set
7// after onRuntimeInitialized, otherwise, it can happen outside of that scope.
8CanvasKit.onRuntimeInitialized = function() {
9 // All calls to 'this' need to go in externs.js so closure doesn't minify them away.
Kevin Lubick1a05fce2018-11-20 12:51:16 -050010
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050011 // Add some helpers for matrices. This is ported from SkMatrix.cpp
12 // to save complexity and overhead of going back and forth between
13 // C++ and JS layers.
14 // I would have liked to use something like DOMMatrix, except it
15 // isn't widely supported (would need polyfills) and it doesn't
16 // have a mapPoints() function (which could maybe be tacked on here).
17 // If DOMMatrix catches on, it would be worth re-considering this usage.
18 CanvasKit.SkMatrix = {};
19 function sdot(a, b, c, d, e, f) {
20 e = e || 0;
21 f = f || 0;
22 return a * b + c * d + e * f;
23 }
24
25 CanvasKit.SkMatrix.identity = function() {
26 return [
27 1, 0, 0,
28 0, 1, 0,
29 0, 0, 1,
30 ];
31 };
32
33 // Return the inverse (if it exists) of this matrix.
34 // Otherwise, return the identity.
35 CanvasKit.SkMatrix.invert = function(m) {
36 var det = m[0]*m[4]*m[8] + m[1]*m[5]*m[6] + m[2]*m[3]*m[7]
37 - m[2]*m[4]*m[6] - m[1]*m[3]*m[8] - m[0]*m[5]*m[7];
38 if (!det) {
39 SkDebug('Warning, uninvertible matrix');
40 return CanvasKit.SkMatrix.identity();
Kevin Lubick1a05fce2018-11-20 12:51:16 -050041 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050042 return [
43 (m[4]*m[8] - m[5]*m[7])/det, (m[2]*m[7] - m[1]*m[8])/det, (m[1]*m[5] - m[2]*m[4])/det,
44 (m[5]*m[6] - m[3]*m[8])/det, (m[0]*m[8] - m[2]*m[6])/det, (m[2]*m[3] - m[0]*m[5])/det,
45 (m[3]*m[7] - m[4]*m[6])/det, (m[1]*m[6] - m[0]*m[7])/det, (m[0]*m[4] - m[1]*m[3])/det,
46 ];
47 };
Kevin Lubick1a05fce2018-11-20 12:51:16 -050048
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050049 // Maps the given points according to the passed in matrix.
50 // Results are done in place.
51 // See SkMatrix.h::mapPoints for the docs on the math.
52 CanvasKit.SkMatrix.mapPoints = function(matrix, ptArr) {
53 if (ptArr.length % 2) {
54 throw 'mapPoints requires an even length arr';
Kevin Lubickb9db3902018-11-26 11:47:54 -050055 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050056 for (var i = 0; i < ptArr.length; i+=2) {
57 var x = ptArr[i], y = ptArr[i+1];
58 // Gx+Hy+I
59 var denom = matrix[6]*x + matrix[7]*y + matrix[8];
60 // Ax+By+C
61 var xTrans = matrix[0]*x + matrix[1]*y + matrix[2];
62 // Dx+Ey+F
63 var yTrans = matrix[3]*x + matrix[4]*y + matrix[5];
64 ptArr[i] = xTrans/denom;
65 ptArr[i+1] = yTrans/denom;
66 }
67 return ptArr;
68 };
Kevin Lubickb9db3902018-11-26 11:47:54 -050069
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050070 CanvasKit.SkMatrix.multiply = function(m1, m2) {
71 var result = [0,0,0, 0,0,0, 0,0,0];
72 for (var r = 0; r < 3; r++) {
73 for (var c = 0; c < 3; c++) {
74 // m1 and m2 are 1D arrays pretending to be 2D arrays
75 result[3*r + c] = sdot(m1[3*r + 0], m2[3*0 + c],
76 m1[3*r + 1], m2[3*1 + c],
77 m1[3*r + 2], m2[3*2 + c]);
Kevin Lubick1a05fce2018-11-20 12:51:16 -050078 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050079 }
80 return result;
81 }
Kevin Lubick1a05fce2018-11-20 12:51:16 -050082
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050083 // Return a matrix representing a rotation by n radians.
84 // px, py optionally say which point the rotation should be around
85 // with the default being (0, 0);
86 CanvasKit.SkMatrix.rotated = function(radians, px, py) {
87 px = px || 0;
88 py = py || 0;
89 var sinV = Math.sin(radians);
90 var cosV = Math.cos(radians);
91 return [
92 cosV, -sinV, sdot( sinV, py, 1 - cosV, px),
93 sinV, cosV, sdot(-sinV, px, 1 - cosV, py),
94 0, 0, 1,
95 ];
96 };
Kevin Lubick217056c2018-09-20 17:39:31 -040097
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050098 CanvasKit.SkMatrix.scaled = function(sx, sy, px, py) {
99 px = px || 0;
100 py = py || 0;
101 return [
102 sx, 0, px - sx * px,
103 0, sy, py - sy * py,
104 0, 0, 1,
105 ];
106 };
Kevin Lubickda3d8ac2019-01-07 11:08:55 -0500107
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500108 CanvasKit.SkMatrix.skewed = function(kx, ky, px, py) {
109 px = px || 0;
110 py = py || 0;
111 return [
112 1, kx, -kx * px,
113 ky, 1, -ky * py,
114 0, 0, 1,
115 ];
116 };
Alexander Khovansky3e119332018-11-15 02:01:19 +0300117
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500118 CanvasKit.SkMatrix.translated = function(dx, dy) {
119 return [
120 1, 0, dx,
121 0, 1, dy,
122 0, 0, 1,
123 ];
124 };
Kevin Lubick1646e7d2018-12-07 13:03:08 -0500125
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500126 CanvasKit.SkPath.prototype.addArc = function(oval, startAngle, sweepAngle) {
127 // see arc() for the HTMLCanvas version
128 // note input angles are degrees.
129 this._addArc(oval, startAngle, sweepAngle);
130 return this;
131 };
Kevin Lubick217056c2018-09-20 17:39:31 -0400132
Kevin Lubicke384df42019-08-26 15:48:09 -0400133 CanvasKit.SkPath.prototype.addOval = function(oval, isCCW, startIndex) {
134 if (startIndex === undefined) {
135 startIndex = 1;
136 }
137 this._addOval(oval, !!isCCW, startIndex);
138 return this;
139 };
140
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500141 CanvasKit.SkPath.prototype.addPath = function() {
142 // Takes 1, 2, 7, or 10 required args, where the first arg is always the path.
143 // The last arg is optional and chooses between add or extend mode.
144 // The options for the remaining args are:
145 // - an array of 6 or 9 parameters (perspective is optional)
146 // - the 9 parameters of a full matrix or
147 // the 6 non-perspective params of a matrix.
148 var args = Array.prototype.slice.call(arguments);
149 var path = args[0];
150 var extend = false;
151 if (typeof args[args.length-1] === "boolean") {
152 extend = args.pop();
153 }
154 if (args.length === 1) {
155 // Add path, unchanged. Use identity matrix
156 this._addPath(path, 1, 0, 0,
157 0, 1, 0,
158 0, 0, 1,
159 extend);
160 } else if (args.length === 2) {
161 // User provided the 9 params of a full matrix as an array.
162 var a = args[1];
163 this._addPath(path, a[0], a[1], a[2],
164 a[3], a[4], a[5],
165 a[6] || 0, a[7] || 0, a[8] || 1,
166 extend);
167 } else if (args.length === 7 || args.length === 10) {
168 // User provided the 9 params of a (full) matrix directly.
169 // (or just the 6 non perspective ones)
170 // These are in the same order as what Skia expects.
171 var a = args;
172 this._addPath(path, a[1], a[2], a[3],
173 a[4], a[5], a[6],
174 a[7] || 0, a[8] || 0, a[9] || 1,
175 extend);
176 } else {
177 SkDebug('addPath expected to take 1, 2, 7, or 10 required args. Got ' + args.length);
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400178 return null;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500179 }
180 return this;
181 };
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400182
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500183 CanvasKit.SkPath.prototype.addRect = function() {
184 // Takes 1, 2, 4 or 5 args
185 // - SkRect
186 // - SkRect, isCCW
187 // - left, top, right, bottom
188 // - left, top, right, bottom, isCCW
189 if (arguments.length === 1 || arguments.length === 2) {
190 var r = arguments[0];
191 var ccw = arguments[1] || false;
192 this._addRect(r.fLeft, r.fTop, r.fRight, r.fBottom, ccw);
193 } else if (arguments.length === 4 || arguments.length === 5) {
194 var a = arguments;
195 this._addRect(a[0], a[1], a[2], a[3], a[4] || false);
196 } else {
197 SkDebug('addRect expected to take 1, 2, 4, or 5 args. Got ' + arguments.length);
Kevin Lubick217056c2018-09-20 17:39:31 -0400198 return null;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500199 }
200 return this;
201 };
Kevin Lubick217056c2018-09-20 17:39:31 -0400202
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500203 CanvasKit.SkPath.prototype.addRoundRect = function() {
204 // Takes 3, 4, 6 or 7 args
205 // - SkRect, radii, ccw
206 // - SkRect, rx, ry, ccw
207 // - left, top, right, bottom, radii, ccw
208 // - left, top, right, bottom, rx, ry, ccw
209 var args = arguments;
210 if (args.length === 3 || args.length === 6) {
211 var radii = args[args.length-2];
212 } else if (args.length === 6 || args.length === 7){
213 // duplicate the given (rx, ry) pairs for each corner.
214 var rx = args[args.length-3];
215 var ry = args[args.length-2];
216 var radii = [rx, ry, rx, ry, rx, ry, rx, ry];
217 } else {
218 SkDebug('addRoundRect expected to take 3, 4, 6, or 7 args. Got ' + args.length);
219 return null;
220 }
221 if (radii.length !== 8) {
222 SkDebug('addRoundRect needs 8 radii provided. Got ' + radii.length);
223 return null;
224 }
225 var rptr = copy1dArray(radii, CanvasKit.HEAPF32);
226 if (args.length === 3 || args.length === 4) {
227 var r = args[0];
228 var ccw = args[args.length - 1];
229 this._addRoundRect(r.fLeft, r.fTop, r.fRight, r.fBottom, rptr, ccw);
230 } else if (args.length === 6 || args.length === 7) {
231 var a = args;
232 this._addRoundRect(a[0], a[1], a[2], a[3], rptr, ccw);
233 }
234 CanvasKit._free(rptr);
235 return this;
236 };
237
238 CanvasKit.SkPath.prototype.arc = function(x, y, radius, startAngle, endAngle, ccw) {
239 // emulates the HTMLCanvas behavior. See addArc() for the SkPath version.
240 // Note input angles are radians.
241 var bounds = CanvasKit.LTRBRect(x-radius, y-radius, x+radius, y+radius);
242 var sweep = radiansToDegrees(endAngle - startAngle) - (360 * !!ccw);
243 var temp = new CanvasKit.SkPath();
244 temp.addArc(bounds, radiansToDegrees(startAngle), sweep);
245 this.addPath(temp, true);
246 temp.delete();
247 return this;
248 };
249
250 CanvasKit.SkPath.prototype.arcTo = function() {
251 // takes 4, 5 or 7 args
252 // - 5 x1, y1, x2, y2, radius
253 // - 4 oval (as Rect), startAngle, sweepAngle, forceMoveTo
Kevin Lubicke384df42019-08-26 15:48:09 -0400254 // - 7 rx, ry, xAxisRotate, useSmallArc, isCCW, x, y
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500255 var args = arguments;
256 if (args.length === 5) {
257 this._arcTo(args[0], args[1], args[2], args[3], args[4]);
258 } else if (args.length === 4) {
259 this._arcTo(args[0], args[1], args[2], args[3]);
260 } else if (args.length === 7) {
Kevin Lubicke384df42019-08-26 15:48:09 -0400261 this._arcTo(args[0], args[1], args[2], !!args[3], !!args[4], args[5], args[6]);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500262 } else {
263 throw 'Invalid args for arcTo. Expected 4, 5, or 7, got '+ args.length;
264 }
265
266 return this;
267 };
268
269 CanvasKit.SkPath.prototype.close = function() {
270 this._close();
271 return this;
272 };
273
274 CanvasKit.SkPath.prototype.conicTo = function(x1, y1, x2, y2, w) {
275 this._conicTo(x1, y1, x2, y2, w);
276 return this;
277 };
278
279 CanvasKit.SkPath.prototype.cubicTo = function(cp1x, cp1y, cp2x, cp2y, x, y) {
280 this._cubicTo(cp1x, cp1y, cp2x, cp2y, x, y);
281 return this;
282 };
283
284 CanvasKit.SkPath.prototype.dash = function(on, off, phase) {
285 if (this._dash(on, off, phase)) {
Kevin Lubick217056c2018-09-20 17:39:31 -0400286 return this;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500287 }
288 return null;
289 };
Kevin Lubick217056c2018-09-20 17:39:31 -0400290
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500291 CanvasKit.SkPath.prototype.lineTo = function(x, y) {
292 this._lineTo(x, y);
293 return this;
294 };
Kevin Lubick217056c2018-09-20 17:39:31 -0400295
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500296 CanvasKit.SkPath.prototype.moveTo = function(x, y) {
297 this._moveTo(x, y);
298 return this;
299 };
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400300
Kevin Lubicke384df42019-08-26 15:48:09 -0400301 CanvasKit.SkPath.prototype.offset = function(dx, dy) {
302 this._transform(1, 0, dx,
303 0, 1, dy,
304 0, 0, 1);
305 return this;
306 };
307
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500308 CanvasKit.SkPath.prototype.op = function(otherPath, op) {
309 if (this._op(otherPath, op)) {
Kevin Lubick217056c2018-09-20 17:39:31 -0400310 return this;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500311 }
312 return null;
313 };
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400314
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500315 CanvasKit.SkPath.prototype.quadTo = function(cpx, cpy, x, y) {
316 this._quadTo(cpx, cpy, x, y);
317 return this;
318 };
319
320 CanvasKit.SkPath.prototype.simplify = function() {
321 if (this._simplify()) {
322 return this;
323 }
324 return null;
325 };
326
327 CanvasKit.SkPath.prototype.stroke = function(opts) {
328 // Fill out any missing values with the default values.
329 /**
330 * See externs.js for this definition
331 * @type {StrokeOpts}
332 */
333 opts = opts || {};
334 opts.width = opts.width || 1;
335 opts.miter_limit = opts.miter_limit || 4;
336 opts.cap = opts.cap || CanvasKit.StrokeCap.Butt;
337 opts.join = opts.join || CanvasKit.StrokeJoin.Miter;
338 opts.precision = opts.precision || 1;
339 if (this._stroke(opts)) {
340 return this;
341 }
342 return null;
343 };
344
345 CanvasKit.SkPath.prototype.transform = function() {
346 // Takes 1 or 9 args
347 if (arguments.length === 1) {
348 // argument 1 should be a 6 or 9 element array.
349 var a = arguments[0];
350 this._transform(a[0], a[1], a[2],
351 a[3], a[4], a[5],
352 a[6] || 0, a[7] || 0, a[8] || 1);
353 } else if (arguments.length === 6 || arguments.length === 9) {
354 // these arguments are the 6 or 9 members of the matrix
355 var a = arguments;
356 this._transform(a[0], a[1], a[2],
357 a[3], a[4], a[5],
358 a[6] || 0, a[7] || 0, a[8] || 1);
359 } else {
360 throw 'transform expected to take 1 or 9 arguments. Got ' + arguments.length;
361 }
362 return this;
363 };
364 // isComplement is optional, defaults to false
365 CanvasKit.SkPath.prototype.trim = function(startT, stopT, isComplement) {
366 if (this._trim(startT, stopT, !!isComplement)) {
367 return this;
368 }
369 return null;
370 };
371
372 // bones should be a 3d array.
373 // Each bone is a 3x2 transformation matrix in column major order:
374 // | scaleX skewX transX |
375 // | skewY scaleY transY |
376 // and bones is an array of those matrices.
377 // Returns a copy of this (SkVertices) with the bones applied.
378 CanvasKit.SkVertices.prototype.applyBones = function(bones) {
379 var bPtr = copy3dArray(bones, CanvasKit.HEAPF32);
380 var vert = this._applyBones(bPtr, bones.length);
381 CanvasKit._free(bPtr);
382 return vert;
383 }
384
385 CanvasKit.SkImage.prototype.encodeToData = function() {
386 if (!arguments.length) {
387 return this._encodeToData();
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400388 }
Kevin Lubick53965c92018-10-11 08:51:55 -0400389
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500390 if (arguments.length === 2) {
391 var a = arguments;
392 return this._encodeToDataWithFormat(a[0], a[1]);
Alexander Khovansky3e119332018-11-15 02:01:19 +0300393 }
394
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500395 throw 'encodeToData expected to take 0 or 2 arguments. Got ' + arguments.length;
396 }
Kevin Lubick1ba9c4d2019-02-22 10:04:06 -0500397
Kevin Lubicka064c282019-04-04 09:28:53 -0400398 CanvasKit.SkImage.prototype.makeShader = function(xTileMode, yTileMode, localMatrix) {
399 if (localMatrix) {
400 // Add perspective args if not provided.
401 if (localMatrix.length === 6) {
402 localMatrix.push(0, 0, 1);
403 }
404 return this._makeShader(xTileMode, yTileMode, localMatrix);
405 } else {
406 return this._makeShader(xTileMode, yTileMode);
407 }
408 }
409
Kevin Lubickd6b32ed2019-05-06 13:04:03 -0400410 CanvasKit.SkImage.prototype.readPixels = function(imageInfo, srcX, srcY) {
411 var rowBytes;
412 switch (imageInfo.colorType){
413 case CanvasKit.ColorType.RGBA_8888:
414 rowBytes = imageInfo.width * 4; // 1 byte per channel == 4 bytes per pixel in 8888
415 break;
416 case CanvasKit.ColorType.RGBA_F32:
417 rowBytes = imageInfo.width * 16; // 4 bytes per channel == 16 bytes per pixel in F32
418 break;
419 default:
420 SkDebug("Colortype not yet supported");
421 return;
422 }
423 var pBytes = rowBytes * imageInfo.height;
424 var pPtr = CanvasKit._malloc(pBytes);
425
426 if (!this._readPixels(imageInfo, pPtr, rowBytes, srcX, srcY)) {
427 SkDebug("Could not read pixels with the given inputs");
428 return null;
429 }
430
431 // Put those pixels into a typed array of the right format and then
432 // make a copy with slice() that we can return.
433 var retVal = null;
434 switch (imageInfo.colorType){
435 case CanvasKit.ColorType.RGBA_8888:
436 retVal = new Uint8Array(CanvasKit.buffer, pPtr, pBytes).slice();
437 break;
438 case CanvasKit.ColorType.RGBA_F32:
439 retVal = new Float32Array(CanvasKit.buffer, pPtr, pBytes).slice();
440 break;
441 }
442
443 // Free the allocated pixels in the WASM memory
444 CanvasKit._free(pPtr);
445 return retVal;
446
447 }
448
Kevin Lubickee91c072019-03-29 10:39:52 -0400449 // atlas is an SkImage, e.g. from CanvasKit.MakeImageFromEncoded
450 // srcRects and dstXforms should be CanvasKit.SkRectBuilder and CanvasKit.RSXFormBuilder
451 // or just arrays of floats in groups of 4.
452 // colors, if provided, should be a CanvasKit.SkColorBuilder or array of SkColor
453 // (from CanvasKit.Color)
454 CanvasKit.SkCanvas.prototype.drawAtlas = function(atlas, srcRects, dstXforms, paint,
455 /*optional*/ blendMode, colors) {
456 if (!atlas || !paint || !srcRects || !dstXforms) {
457 SkDebug('Doing nothing since missing a required input');
458 return;
459 }
460 if (srcRects.length !== dstXforms.length || (colors && colors.length !== dstXforms.length)) {
461 SkDebug('Doing nothing since input arrays length mismatches');
462 }
463 if (!blendMode) {
464 blendMode = CanvasKit.BlendMode.SrcOver;
465 }
466
467 var srcRectPtr;
468 if (srcRects.build) {
469 srcRectPtr = srcRects.build();
470 } else {
471 srcRectPtr = copy1dArray(srcRects, CanvasKit.HEAPF32);
472 }
473
474 var dstXformPtr;
475 if (dstXforms.build) {
476 dstXformPtr = dstXforms.build();
477 } else {
478 dstXformPtr = copy1dArray(dstXforms, CanvasKit.HEAPF32);
479 }
480
481 var colorPtr = 0; // enscriptem doesn't like undefined for nullptr
482 if (colors) {
483 if (colors.build) {
484 colorPtr = colors.build();
485 } else {
486 colorPtr = copy1dArray(colors, CanvasKit.HEAPU32);
487 }
488 }
489
490 this._drawAtlas(atlas, dstXformPtr, srcRectPtr, colorPtr, dstXforms.length,
491 blendMode, paint);
492
493 if (srcRectPtr && !srcRects.build) {
494 CanvasKit._free(srcRectPtr);
495 }
496 if (dstXformPtr && !dstXforms.build) {
497 CanvasKit._free(dstXformPtr);
498 }
499 if (colorPtr && !colors.build) {
500 CanvasKit._free(colorPtr);
501 }
502
503 }
504
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500505 // str can be either a text string or a ShapedText object
506 CanvasKit.SkCanvas.prototype.drawText = function(str, x, y, paint, font) {
507 if (typeof str === 'string') {
Kevin Lubickec4903d2019-01-14 08:36:08 -0500508 // lengthBytesUTF8 and stringToUTF8Array are defined in the emscripten
509 // JS. See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#stringToUTF8
510 // Add 1 for null terminator
511 var strLen = lengthBytesUTF8(str) + 1;
512 var strPtr = CanvasKit._malloc(strLen);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500513
Kevin Lubickec4903d2019-01-14 08:36:08 -0500514 stringToUTF8(str, strPtr, strLen);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500515 this._drawSimpleText(strPtr, strLen, x, y, font, paint);
Kevin Lubickda3d8ac2019-01-07 11:08:55 -0500516 } else {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500517 this._drawShapedText(str, x, y, paint);
Kevin Lubickd29edd72018-12-07 08:29:52 -0500518 }
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400519 }
520
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500521 // returns Uint8Array
522 CanvasKit.SkCanvas.prototype.readPixels = function(x, y, w, h, alphaType,
523 colorType, dstRowBytes) {
524 // supply defaults (which are compatible with HTMLCanvas's getImageData)
525 alphaType = alphaType || CanvasKit.AlphaType.Unpremul;
526 colorType = colorType || CanvasKit.ColorType.RGBA_8888;
527 dstRowBytes = dstRowBytes || (4 * w);
528
529 var len = h * dstRowBytes
530 var pptr = CanvasKit._malloc(len);
531 var ok = this._readPixels({
532 'width': w,
533 'height': h,
Kevin Lubick52b9f372018-12-04 13:57:36 -0500534 'colorType': colorType,
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500535 'alphaType': alphaType,
536 }, pptr, dstRowBytes, x, y);
537 if (!ok) {
538 CanvasKit._free(pptr);
539 return null;
540 }
541
542 // The first typed array is just a view into memory. Because we will
543 // be free-ing that, we call slice to make a persistent copy.
544 var pixels = new Uint8Array(CanvasKit.HEAPU8.buffer, pptr, len).slice();
545 CanvasKit._free(pptr);
546 return pixels;
547 }
548
549 // pixels is a TypedArray. No matter the input size, it will be treated as
550 // a Uint8Array (essentially, a byte array).
551 CanvasKit.SkCanvas.prototype.writePixels = function(pixels, srcWidth, srcHeight,
552 destX, destY, alphaType, colorType) {
553 if (pixels.byteLength % (srcWidth * srcHeight)) {
554 throw 'pixels length must be a multiple of the srcWidth * srcHeight';
555 }
556 var bytesPerPixel = pixels.byteLength / (srcWidth * srcHeight);
557 // supply defaults (which are compatible with HTMLCanvas's putImageData)
558 alphaType = alphaType || CanvasKit.AlphaType.Unpremul;
559 colorType = colorType || CanvasKit.ColorType.RGBA_8888;
560 var srcRowBytes = bytesPerPixel * srcWidth;
561
Kevin Lubick52b9f372018-12-04 13:57:36 -0500562 var pptr = CanvasKit._malloc(pixels.byteLength);
563 CanvasKit.HEAPU8.set(pixels, pptr);
Kevin Lubick52b9f372018-12-04 13:57:36 -0500564
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500565 var ok = this._writePixels({
566 'width': srcWidth,
567 'height': srcHeight,
568 'colorType': colorType,
569 'alphaType': alphaType,
570 }, pptr, srcRowBytes, destX, destY);
571
572 CanvasKit._free(pptr);
573 return ok;
Kevin Lubick52b9f372018-12-04 13:57:36 -0500574 }
575
Kevin Lubickd3cfbca2019-03-15 15:36:29 -0400576 // Returns an array of the widths of the glyphs in this string.
577 CanvasKit.SkFont.prototype.getWidths = function(str) {
578 // add 1 for null terminator
579 var codePoints = str.length + 1;
580 // lengthBytesUTF8 and stringToUTF8Array are defined in the emscripten
581 // JS. See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#stringToUTF8
582 // Add 1 for null terminator
583 var strBytes = lengthBytesUTF8(str) + 1;
584 var strPtr = CanvasKit._malloc(strBytes);
585 stringToUTF8(str, strPtr, strBytes);
586
587 var bytesPerFloat = 4;
588 // allocate widths == numCodePoints
589 var widthPtr = CanvasKit._malloc(codePoints * bytesPerFloat);
590 if (!this._getWidths(strPtr, strBytes, codePoints, widthPtr)) {
591 SkDebug('Could not compute widths');
592 CanvasKit._free(strPtr);
593 CanvasKit._free(widthPtr);
594 return null;
595 }
596 // reminder, this shouldn't copy the data, just is a nice way to
597 // wrap 4 bytes together into a float.
598 var widths = new Float32Array(CanvasKit.buffer, widthPtr, codePoints);
599 // This copies the data so we can free the CanvasKit memory
600 var retVal = Array.from(widths);
601 CanvasKit._free(strPtr);
602 CanvasKit._free(widthPtr);
603 return retVal;
604 }
605
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500606 // fontData should be an arrayBuffer
607 CanvasKit.SkFontMgr.prototype.MakeTypefaceFromData = function(fontData) {
608 var data = new Uint8Array(fontData);
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400609
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500610 var fptr = CanvasKit._malloc(data.byteLength);
611 CanvasKit.HEAPU8.set(data, fptr);
612 var font = this._makeTypefaceFromData(fptr, data.byteLength);
613 if (!font) {
614 SkDebug('Could not decode font data');
615 // We do not need to free the data since the C++ will do that for us
616 // when the font is deleted (or fails to decode);
617 return null;
618 }
619 return font;
620 }
621
Kevin Lubickcc13fd32019-04-05 13:00:01 -0400622 // The serialized format of an SkPicture (informally called an "skp"), is not something
623 // that clients should ever rely on. It is useful when filing bug reports, but that's
624 // about it. The format may change at anytime and no promises are made for backwards
625 // or forward compatibility.
626 CanvasKit.SkPicture.prototype.DEBUGONLY_saveAsFile = function(skpName) {
627 var data = this.DEBUGONLY_serialize();
628 if (!data) {
629 SkDebug('Could not serialize to skpicture.');
630 return;
631 }
632 var bytes = CanvasKit.getSkDataBytes(data);
633 saveBytesToFile(bytes, skpName);
634 data.delete();
635 }
636
637 CanvasKit.SkSurface.prototype.captureFrameAsSkPicture = function(drawFrame) {
638 // Set up SkPictureRecorder
639 var spr = new CanvasKit.SkPictureRecorder();
640 var canvas = spr.beginRecording(
641 CanvasKit.LTRBRect(0, 0, this.width(), this.height()));
642 drawFrame(canvas);
643 var pic = spr.finishRecordingAsPicture();
644 spr.delete();
645 // TODO: do we need to clean up the memory for canvas?
646 // If we delete it here, saveAsFile doesn't work correctly.
647 return pic;
648 }
649
Kevin Lubick359a7e32019-03-19 09:34:37 -0400650 CanvasKit.SkSurface.prototype.requestAnimationFrame = function(callback, dirtyRect) {
651 if (!this._cached_canvas) {
652 this._cached_canvas = this.getCanvas();
653 }
654 window.requestAnimationFrame(function() {
Kevin Lubick39026282019-03-28 12:46:40 -0400655 if (this._context !== undefined) {
656 CanvasKit.setCurrentContext(this._context);
657 }
Kevin Lubick359a7e32019-03-19 09:34:37 -0400658
659 callback(this._cached_canvas);
660
661 this.flush();
662 }.bind(this));
663 }
664
Kevin Lubickd3cfbca2019-03-15 15:36:29 -0400665 CanvasKit.SkTextBlob.MakeOnPath = function(str, path, font, initialOffset) {
666 if (!str || !str.length) {
667 SkDebug('ignoring 0 length string');
668 return;
669 }
670 if (!path || !path.countPoints()) {
671 SkDebug('ignoring empty path');
672 return;
673 }
674 if (path.countPoints() === 1) {
675 SkDebug('path has 1 point, returning normal textblob');
676 return this.MakeFromText(str, font);
677 }
678
679 if (!initialOffset) {
680 initialOffset = 0;
681 }
682
683 var widths = font.getWidths(str);
684
685 var rsx = new CanvasKit.RSXFormBuilder();
686 var meas = new CanvasKit.SkPathMeasure(path, false, 1);
687 var dist = initialOffset;
688 for (var i = 0; i < str.length; i++) {
689 var width = widths[i];
690 dist += width/2;
691 if (dist > meas.getLength()) {
692 // jump to next contour
693 if (!meas.nextContour()) {
694 // We have come to the end of the path - terminate the string
695 // right here.
696 str = str.substring(0, i);
697 break;
698 }
699 dist = width/2;
700 }
701
702 // Gives us the (x, y) coordinates as well as the cos/sin of the tangent
703 // line at that position.
704 var xycs = meas.getPosTan(dist);
705 var cx = xycs[0];
706 var cy = xycs[1];
707 var cosT = xycs[2];
708 var sinT = xycs[3];
709
710 var adjustedX = cx - (width/2 * cosT);
711 var adjustedY = cy - (width/2 * sinT);
712
713 rsx.push(cosT, sinT, adjustedX, adjustedY);
714 dist += width/2;
715 }
716 var retVal = this.MakeFromRSXform(str, rsx, font);
717 rsx.delete();
718 meas.delete();
719 return retVal;
720 }
721
722 CanvasKit.SkTextBlob.MakeFromRSXform = function(str, rsxBuilder, font) {
723 // lengthBytesUTF8 and stringToUTF8Array are defined in the emscripten
724 // JS. See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#stringToUTF8
725 // Add 1 for null terminator
726 var strLen = lengthBytesUTF8(str) + 1;
727 var strPtr = CanvasKit._malloc(strLen);
728 // Add 1 for the null terminator.
729 stringToUTF8(str, strPtr, strLen);
730 var rptr = rsxBuilder.build();
731
732 var blob = CanvasKit.SkTextBlob._MakeFromRSXform(strPtr, strLen - 1,
733 rptr, font, CanvasKit.TextEncoding.UTF8);
734 if (!blob) {
735 SkDebug('Could not make textblob from string "' + str + '"');
736 return null;
737 }
738
739 var origDelete = blob.delete.bind(blob);
740 blob.delete = function() {
741 CanvasKit._free(strPtr);
742 origDelete();
743 }
744 return blob;
745 }
746
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500747 CanvasKit.SkTextBlob.MakeFromText = function(str, font) {
748 // lengthBytesUTF8 and stringToUTF8Array are defined in the emscripten
749 // JS. See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#stringToUTF8
750 // Add 1 for null terminator
751 var strLen = lengthBytesUTF8(str) + 1;
752 var strPtr = CanvasKit._malloc(strLen);
753 // Add 1 for the null terminator.
754 stringToUTF8(str, strPtr, strLen);
755
756 var blob = CanvasKit.SkTextBlob._MakeFromText(strPtr, strLen - 1, font, CanvasKit.TextEncoding.UTF8);
757 if (!blob) {
758 SkDebug('Could not make textblob from string "' + str + '"');
759 return null;
Kevin Lubick217056c2018-09-20 17:39:31 -0400760 }
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400761
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500762 var origDelete = blob.delete.bind(blob);
763 blob.delete = function() {
764 CanvasKit._free(strPtr);
765 origDelete();
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400766 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500767 return blob;
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400768 }
769
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500770 // Run through the JS files that are added at compile time.
771 if (CanvasKit._extraInitializations) {
772 CanvasKit._extraInitializations.forEach(function(init) {
773 init();
774 });
Kevin Lubickeb2f6b02018-11-29 15:07:02 -0500775 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500776}; // end CanvasKit.onRuntimeInitialized, that is, anything changing prototypes or dynamic.
Kevin Lubickeb2f6b02018-11-29 15:07:02 -0500777
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500778CanvasKit.LTRBRect = function(l, t, r, b) {
779 return {
780 fLeft: l,
781 fTop: t,
782 fRight: r,
783 fBottom: b,
784 };
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500785}
786
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500787CanvasKit.XYWHRect = function(x, y, w, h) {
788 return {
789 fLeft: x,
790 fTop: y,
791 fRight: x+w,
792 fBottom: y+h,
793 };
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500794}
795
Kevin Lubick7d644e12019-09-11 14:22:22 -0400796// RRectXY returns an RRect with the given rect and a radiusX and radiusY for
797// all 4 corners.
798CanvasKit.RRectXY = function(rect, rx, ry) {
799 return {
800 rect: rect,
801 rx1: rx,
802 ry1: ry,
803 rx2: rx,
804 ry2: ry,
805 rx3: rx,
806 ry3: ry,
807 rx4: rx,
808 ry4: ry,
809 };
810}
811
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500812CanvasKit.MakePathFromCmds = function(cmds) {
813 var ptrLen = loadCmdsTypedArray(cmds);
814 var path = CanvasKit._MakePathFromCmds(ptrLen[0], ptrLen[1]);
815 CanvasKit._free(ptrLen[0]);
816 return path;
817}
818
819CanvasKit.MakeSkDashPathEffect = function(intervals, phase) {
820 if (!phase) {
821 phase = 0;
822 }
823 if (!intervals.length || intervals.length % 2 === 1) {
824 throw 'Intervals array must have even length';
825 }
826 var ptr = copy1dArray(intervals, CanvasKit.HEAPF32);
827 var dpe = CanvasKit._MakeSkDashPathEffect(ptr, intervals.length, phase);
828 CanvasKit._free(ptr);
829 return dpe;
830}
831
832// data is a TypedArray or ArrayBuffer e.g. from fetch().then(resp.arrayBuffer())
833CanvasKit.MakeImageFromEncoded = function(data) {
834 data = new Uint8Array(data);
835
836 var iptr = CanvasKit._malloc(data.byteLength);
837 CanvasKit.HEAPU8.set(data, iptr);
838 var img = CanvasKit._decodeImage(iptr, data.byteLength);
839 if (!img) {
840 SkDebug('Could not decode image');
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500841 return null;
842 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500843 return img;
844}
845
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500846// pixels is a Uint8Array
847CanvasKit.MakeImage = function(pixels, width, height, alphaType, colorType) {
848 var bytesPerPixel = pixels.byteLength / (width * height);
849 var info = {
850 'width': width,
851 'height': height,
852 'alphaType': alphaType,
853 'colorType': colorType,
854 };
855 var pptr = CanvasKit._malloc(pixels.byteLength);
856 CanvasKit.HEAPU8.set(pixels, pptr);
857 // No need to _free iptr, Image takes it with SkData::MakeFromMalloc
858
859 return CanvasKit._MakeImage(info, pptr, pixels.byteLength, width * bytesPerPixel);
860}
861
862CanvasKit.MakeLinearGradientShader = function(start, end, colors, pos, mode, localMatrix, flags) {
Kevin Lubickee91c072019-03-29 10:39:52 -0400863 var colorPtr = copy1dArray(colors, CanvasKit.HEAPU32);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500864 var posPtr = copy1dArray(pos, CanvasKit.HEAPF32);
865 flags = flags || 0;
866
867 if (localMatrix) {
868 // Add perspective args if not provided.
869 if (localMatrix.length === 6) {
870 localMatrix.push(0, 0, 1);
871 }
872 var lgs = CanvasKit._MakeLinearGradientShader(start, end, colorPtr, posPtr,
873 colors.length, mode, flags, localMatrix);
874 } else {
875 var lgs = CanvasKit._MakeLinearGradientShader(start, end, colorPtr, posPtr,
876 colors.length, mode, flags);
877 }
878
879 CanvasKit._free(colorPtr);
880 CanvasKit._free(posPtr);
881 return lgs;
882}
883
884CanvasKit.MakeRadialGradientShader = function(center, radius, colors, pos, mode, localMatrix, flags) {
Kevin Lubickee91c072019-03-29 10:39:52 -0400885 var colorPtr = copy1dArray(colors, CanvasKit.HEAPU32);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500886 var posPtr = copy1dArray(pos, CanvasKit.HEAPF32);
887 flags = flags || 0;
888
889 if (localMatrix) {
890 // Add perspective args if not provided.
891 if (localMatrix.length === 6) {
892 localMatrix.push(0, 0, 1);
893 }
894 var rgs = CanvasKit._MakeRadialGradientShader(center, radius, colorPtr, posPtr,
895 colors.length, mode, flags, localMatrix);
896 } else {
897 var rgs = CanvasKit._MakeRadialGradientShader(center, radius, colorPtr, posPtr,
898 colors.length, mode, flags);
899 }
900
901 CanvasKit._free(colorPtr);
902 CanvasKit._free(posPtr);
903 return rgs;
904}
905
906CanvasKit.MakeTwoPointConicalGradientShader = function(start, startRadius, end, endRadius,
907 colors, pos, mode, localMatrix, flags) {
Kevin Lubickee91c072019-03-29 10:39:52 -0400908 var colorPtr = copy1dArray(colors, CanvasKit.HEAPU32);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500909 var posPtr = copy1dArray(pos, CanvasKit.HEAPF32);
910 flags = flags || 0;
911
912 if (localMatrix) {
913 // Add perspective args if not provided.
914 if (localMatrix.length === 6) {
915 localMatrix.push(0, 0, 1);
916 }
917 var rgs = CanvasKit._MakeTwoPointConicalGradientShader(
918 start, startRadius, end, endRadius,
919 colorPtr, posPtr, colors.length, mode, flags, localMatrix);
920 } else {
921 var rgs = CanvasKit._MakeTwoPointConicalGradientShader(
922 start, startRadius, end, endRadius,
923 colorPtr, posPtr, colors.length, mode, flags);
924 }
925
926 CanvasKit._free(colorPtr);
927 CanvasKit._free(posPtr);
928 return rgs;
929}
930
931CanvasKit.MakeSkVertices = function(mode, positions, textureCoordinates, colors,
Kevin Lubickb3574c92019-03-06 08:25:36 -0500932 boneIndices, boneWeights, indices, isVolatile) {
Kevin Lubickb3574c92019-03-06 08:25:36 -0500933 // Default isVolitile to true if not set
934 isVolatile = isVolatile === undefined ? true : isVolatile;
Kevin Lubickd6ba7252019-06-03 14:38:05 -0400935 var idxCount = (indices && indices.length) || 0;
936
937 var flags = 0;
938 // These flags are from SkVertices.h and should be kept in sync with those.
939 if (textureCoordinates && textureCoordinates.length) {
940 flags |= (1 << 0);
941 }
942 if (colors && colors.length) {
943 flags |= (1 << 1);
944 }
945 if (boneIndices && boneIndices.length) {
946 flags |= (1 << 2);
947 }
948 if (!isVolatile) {
949 flags |= (1 << 3);
950 }
951
952 var builder = new CanvasKit._SkVerticesBuilder(mode, positions.length, idxCount, flags);
953
954 copy2dArray(positions, CanvasKit.HEAPF32, builder.positions());
955 if (builder.texCoords()) {
956 copy2dArray(textureCoordinates, CanvasKit.HEAPF32, builder.texCoords());
957 }
958 if (builder.colors()) {
959 copy1dArray(colors, CanvasKit.HEAPU32, builder.colors());
960 }
961 if (builder.boneIndices()) {
962 copy2dArray(boneIndices, CanvasKit.HEAP32, builder.boneIndices());
963 }
964 if (builder.boneWeights()) {
965 copy2dArray(boneWeights, CanvasKit.HEAPF32, builder.boneWeights());
966 }
967 if (builder.indices()) {
968 copy1dArray(indices, CanvasKit.HEAPU16, builder.indices());
969 }
Kevin Lubickb3574c92019-03-06 08:25:36 -0500970
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500971 var idxCount = (indices && indices.length) || 0;
Kevin Lubickd6ba7252019-06-03 14:38:05 -0400972 // Create the vertices, which owns the memory that the builder had allocated.
973 return builder.detach();
Kevin Lubicka064c282019-04-04 09:28:53 -0400974};