blob: 43f2506fa313ffe94510627600f23e437d0cf07d [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 Lubickf5ea37f2019-02-28 10:06:18 -0500133 CanvasKit.SkPath.prototype.addPath = function() {
134 // Takes 1, 2, 7, or 10 required args, where the first arg is always the path.
135 // The last arg is optional and chooses between add or extend mode.
136 // The options for the remaining args are:
137 // - an array of 6 or 9 parameters (perspective is optional)
138 // - the 9 parameters of a full matrix or
139 // the 6 non-perspective params of a matrix.
140 var args = Array.prototype.slice.call(arguments);
141 var path = args[0];
142 var extend = false;
143 if (typeof args[args.length-1] === "boolean") {
144 extend = args.pop();
145 }
146 if (args.length === 1) {
147 // Add path, unchanged. Use identity matrix
148 this._addPath(path, 1, 0, 0,
149 0, 1, 0,
150 0, 0, 1,
151 extend);
152 } else if (args.length === 2) {
153 // User provided the 9 params of a full matrix as an array.
154 var a = args[1];
155 this._addPath(path, a[0], a[1], a[2],
156 a[3], a[4], a[5],
157 a[6] || 0, a[7] || 0, a[8] || 1,
158 extend);
159 } else if (args.length === 7 || args.length === 10) {
160 // User provided the 9 params of a (full) matrix directly.
161 // (or just the 6 non perspective ones)
162 // These are in the same order as what Skia expects.
163 var a = args;
164 this._addPath(path, a[1], a[2], a[3],
165 a[4], a[5], a[6],
166 a[7] || 0, a[8] || 0, a[9] || 1,
167 extend);
168 } else {
169 SkDebug('addPath expected to take 1, 2, 7, or 10 required args. Got ' + args.length);
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400170 return null;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500171 }
172 return this;
173 };
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400174
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500175 CanvasKit.SkPath.prototype.addRect = function() {
176 // Takes 1, 2, 4 or 5 args
177 // - SkRect
178 // - SkRect, isCCW
179 // - left, top, right, bottom
180 // - left, top, right, bottom, isCCW
181 if (arguments.length === 1 || arguments.length === 2) {
182 var r = arguments[0];
183 var ccw = arguments[1] || false;
184 this._addRect(r.fLeft, r.fTop, r.fRight, r.fBottom, ccw);
185 } else if (arguments.length === 4 || arguments.length === 5) {
186 var a = arguments;
187 this._addRect(a[0], a[1], a[2], a[3], a[4] || false);
188 } else {
189 SkDebug('addRect expected to take 1, 2, 4, or 5 args. Got ' + arguments.length);
Kevin Lubick217056c2018-09-20 17:39:31 -0400190 return null;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500191 }
192 return this;
193 };
Kevin Lubick217056c2018-09-20 17:39:31 -0400194
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500195 CanvasKit.SkPath.prototype.addRoundRect = function() {
196 // Takes 3, 4, 6 or 7 args
197 // - SkRect, radii, ccw
198 // - SkRect, rx, ry, ccw
199 // - left, top, right, bottom, radii, ccw
200 // - left, top, right, bottom, rx, ry, ccw
201 var args = arguments;
202 if (args.length === 3 || args.length === 6) {
203 var radii = args[args.length-2];
204 } else if (args.length === 6 || args.length === 7){
205 // duplicate the given (rx, ry) pairs for each corner.
206 var rx = args[args.length-3];
207 var ry = args[args.length-2];
208 var radii = [rx, ry, rx, ry, rx, ry, rx, ry];
209 } else {
210 SkDebug('addRoundRect expected to take 3, 4, 6, or 7 args. Got ' + args.length);
211 return null;
212 }
213 if (radii.length !== 8) {
214 SkDebug('addRoundRect needs 8 radii provided. Got ' + radii.length);
215 return null;
216 }
217 var rptr = copy1dArray(radii, CanvasKit.HEAPF32);
218 if (args.length === 3 || args.length === 4) {
219 var r = args[0];
220 var ccw = args[args.length - 1];
221 this._addRoundRect(r.fLeft, r.fTop, r.fRight, r.fBottom, rptr, ccw);
222 } else if (args.length === 6 || args.length === 7) {
223 var a = args;
224 this._addRoundRect(a[0], a[1], a[2], a[3], rptr, ccw);
225 }
226 CanvasKit._free(rptr);
227 return this;
228 };
229
230 CanvasKit.SkPath.prototype.arc = function(x, y, radius, startAngle, endAngle, ccw) {
231 // emulates the HTMLCanvas behavior. See addArc() for the SkPath version.
232 // Note input angles are radians.
233 var bounds = CanvasKit.LTRBRect(x-radius, y-radius, x+radius, y+radius);
234 var sweep = radiansToDegrees(endAngle - startAngle) - (360 * !!ccw);
235 var temp = new CanvasKit.SkPath();
236 temp.addArc(bounds, radiansToDegrees(startAngle), sweep);
237 this.addPath(temp, true);
238 temp.delete();
239 return this;
240 };
241
242 CanvasKit.SkPath.prototype.arcTo = function() {
243 // takes 4, 5 or 7 args
244 // - 5 x1, y1, x2, y2, radius
245 // - 4 oval (as Rect), startAngle, sweepAngle, forceMoveTo
246 // - 7 x1, y1, x2, y2, startAngle, sweepAngle, forceMoveTo
247 var args = arguments;
248 if (args.length === 5) {
249 this._arcTo(args[0], args[1], args[2], args[3], args[4]);
250 } else if (args.length === 4) {
251 this._arcTo(args[0], args[1], args[2], args[3]);
252 } else if (args.length === 7) {
253 this._arcTo(CanvasKit.LTRBRect(args[0], args[1], args[2], args[3]),
254 args[4], args[5], args[6]);
255 } else {
256 throw 'Invalid args for arcTo. Expected 4, 5, or 7, got '+ args.length;
257 }
258
259 return this;
260 };
261
262 CanvasKit.SkPath.prototype.close = function() {
263 this._close();
264 return this;
265 };
266
267 CanvasKit.SkPath.prototype.conicTo = function(x1, y1, x2, y2, w) {
268 this._conicTo(x1, y1, x2, y2, w);
269 return this;
270 };
271
272 CanvasKit.SkPath.prototype.cubicTo = function(cp1x, cp1y, cp2x, cp2y, x, y) {
273 this._cubicTo(cp1x, cp1y, cp2x, cp2y, x, y);
274 return this;
275 };
276
277 CanvasKit.SkPath.prototype.dash = function(on, off, phase) {
278 if (this._dash(on, off, phase)) {
Kevin Lubick217056c2018-09-20 17:39:31 -0400279 return this;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500280 }
281 return null;
282 };
Kevin Lubick217056c2018-09-20 17:39:31 -0400283
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500284 CanvasKit.SkPath.prototype.lineTo = function(x, y) {
285 this._lineTo(x, y);
286 return this;
287 };
Kevin Lubick217056c2018-09-20 17:39:31 -0400288
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500289 CanvasKit.SkPath.prototype.moveTo = function(x, y) {
290 this._moveTo(x, y);
291 return this;
292 };
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400293
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500294 CanvasKit.SkPath.prototype.op = function(otherPath, op) {
295 if (this._op(otherPath, op)) {
Kevin Lubick217056c2018-09-20 17:39:31 -0400296 return this;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500297 }
298 return null;
299 };
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400300
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500301 CanvasKit.SkPath.prototype.quadTo = function(cpx, cpy, x, y) {
302 this._quadTo(cpx, cpy, x, y);
303 return this;
304 };
305
306 CanvasKit.SkPath.prototype.simplify = function() {
307 if (this._simplify()) {
308 return this;
309 }
310 return null;
311 };
312
313 CanvasKit.SkPath.prototype.stroke = function(opts) {
314 // Fill out any missing values with the default values.
315 /**
316 * See externs.js for this definition
317 * @type {StrokeOpts}
318 */
319 opts = opts || {};
320 opts.width = opts.width || 1;
321 opts.miter_limit = opts.miter_limit || 4;
322 opts.cap = opts.cap || CanvasKit.StrokeCap.Butt;
323 opts.join = opts.join || CanvasKit.StrokeJoin.Miter;
324 opts.precision = opts.precision || 1;
325 if (this._stroke(opts)) {
326 return this;
327 }
328 return null;
329 };
330
331 CanvasKit.SkPath.prototype.transform = function() {
332 // Takes 1 or 9 args
333 if (arguments.length === 1) {
334 // argument 1 should be a 6 or 9 element array.
335 var a = arguments[0];
336 this._transform(a[0], a[1], a[2],
337 a[3], a[4], a[5],
338 a[6] || 0, a[7] || 0, a[8] || 1);
339 } else if (arguments.length === 6 || arguments.length === 9) {
340 // these arguments are the 6 or 9 members of the matrix
341 var a = arguments;
342 this._transform(a[0], a[1], a[2],
343 a[3], a[4], a[5],
344 a[6] || 0, a[7] || 0, a[8] || 1);
345 } else {
346 throw 'transform expected to take 1 or 9 arguments. Got ' + arguments.length;
347 }
348 return this;
349 };
350 // isComplement is optional, defaults to false
351 CanvasKit.SkPath.prototype.trim = function(startT, stopT, isComplement) {
352 if (this._trim(startT, stopT, !!isComplement)) {
353 return this;
354 }
355 return null;
356 };
357
358 // bones should be a 3d array.
359 // Each bone is a 3x2 transformation matrix in column major order:
360 // | scaleX skewX transX |
361 // | skewY scaleY transY |
362 // and bones is an array of those matrices.
363 // Returns a copy of this (SkVertices) with the bones applied.
364 CanvasKit.SkVertices.prototype.applyBones = function(bones) {
365 var bPtr = copy3dArray(bones, CanvasKit.HEAPF32);
366 var vert = this._applyBones(bPtr, bones.length);
367 CanvasKit._free(bPtr);
368 return vert;
369 }
370
371 CanvasKit.SkImage.prototype.encodeToData = function() {
372 if (!arguments.length) {
373 return this._encodeToData();
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400374 }
Kevin Lubick53965c92018-10-11 08:51:55 -0400375
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500376 if (arguments.length === 2) {
377 var a = arguments;
378 return this._encodeToDataWithFormat(a[0], a[1]);
Alexander Khovansky3e119332018-11-15 02:01:19 +0300379 }
380
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500381 throw 'encodeToData expected to take 0 or 2 arguments. Got ' + arguments.length;
382 }
Kevin Lubick1ba9c4d2019-02-22 10:04:06 -0500383
Kevin Lubicka064c282019-04-04 09:28:53 -0400384 CanvasKit.SkImage.prototype.makeShader = function(xTileMode, yTileMode, localMatrix) {
385 if (localMatrix) {
386 // Add perspective args if not provided.
387 if (localMatrix.length === 6) {
388 localMatrix.push(0, 0, 1);
389 }
390 return this._makeShader(xTileMode, yTileMode, localMatrix);
391 } else {
392 return this._makeShader(xTileMode, yTileMode);
393 }
394 }
395
Kevin Lubickee91c072019-03-29 10:39:52 -0400396 // atlas is an SkImage, e.g. from CanvasKit.MakeImageFromEncoded
397 // srcRects and dstXforms should be CanvasKit.SkRectBuilder and CanvasKit.RSXFormBuilder
398 // or just arrays of floats in groups of 4.
399 // colors, if provided, should be a CanvasKit.SkColorBuilder or array of SkColor
400 // (from CanvasKit.Color)
401 CanvasKit.SkCanvas.prototype.drawAtlas = function(atlas, srcRects, dstXforms, paint,
402 /*optional*/ blendMode, colors) {
403 if (!atlas || !paint || !srcRects || !dstXforms) {
404 SkDebug('Doing nothing since missing a required input');
405 return;
406 }
407 if (srcRects.length !== dstXforms.length || (colors && colors.length !== dstXforms.length)) {
408 SkDebug('Doing nothing since input arrays length mismatches');
409 }
410 if (!blendMode) {
411 blendMode = CanvasKit.BlendMode.SrcOver;
412 }
413
414 var srcRectPtr;
415 if (srcRects.build) {
416 srcRectPtr = srcRects.build();
417 } else {
418 srcRectPtr = copy1dArray(srcRects, CanvasKit.HEAPF32);
419 }
420
421 var dstXformPtr;
422 if (dstXforms.build) {
423 dstXformPtr = dstXforms.build();
424 } else {
425 dstXformPtr = copy1dArray(dstXforms, CanvasKit.HEAPF32);
426 }
427
428 var colorPtr = 0; // enscriptem doesn't like undefined for nullptr
429 if (colors) {
430 if (colors.build) {
431 colorPtr = colors.build();
432 } else {
433 colorPtr = copy1dArray(colors, CanvasKit.HEAPU32);
434 }
435 }
436
437 this._drawAtlas(atlas, dstXformPtr, srcRectPtr, colorPtr, dstXforms.length,
438 blendMode, paint);
439
440 if (srcRectPtr && !srcRects.build) {
441 CanvasKit._free(srcRectPtr);
442 }
443 if (dstXformPtr && !dstXforms.build) {
444 CanvasKit._free(dstXformPtr);
445 }
446 if (colorPtr && !colors.build) {
447 CanvasKit._free(colorPtr);
448 }
449
450 }
451
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500452 // str can be either a text string or a ShapedText object
453 CanvasKit.SkCanvas.prototype.drawText = function(str, x, y, paint, font) {
454 if (typeof str === 'string') {
Kevin Lubickec4903d2019-01-14 08:36:08 -0500455 // lengthBytesUTF8 and stringToUTF8Array are defined in the emscripten
456 // JS. See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#stringToUTF8
457 // Add 1 for null terminator
458 var strLen = lengthBytesUTF8(str) + 1;
459 var strPtr = CanvasKit._malloc(strLen);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500460
Kevin Lubickec4903d2019-01-14 08:36:08 -0500461 stringToUTF8(str, strPtr, strLen);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500462 this._drawSimpleText(strPtr, strLen, x, y, font, paint);
Kevin Lubickda3d8ac2019-01-07 11:08:55 -0500463 } else {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500464 this._drawShapedText(str, x, y, paint);
Kevin Lubickd29edd72018-12-07 08:29:52 -0500465 }
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400466 }
467
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500468 // returns Uint8Array
469 CanvasKit.SkCanvas.prototype.readPixels = function(x, y, w, h, alphaType,
470 colorType, dstRowBytes) {
471 // supply defaults (which are compatible with HTMLCanvas's getImageData)
472 alphaType = alphaType || CanvasKit.AlphaType.Unpremul;
473 colorType = colorType || CanvasKit.ColorType.RGBA_8888;
474 dstRowBytes = dstRowBytes || (4 * w);
475
476 var len = h * dstRowBytes
477 var pptr = CanvasKit._malloc(len);
478 var ok = this._readPixels({
479 'width': w,
480 'height': h,
Kevin Lubick52b9f372018-12-04 13:57:36 -0500481 'colorType': colorType,
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500482 'alphaType': alphaType,
483 }, pptr, dstRowBytes, x, y);
484 if (!ok) {
485 CanvasKit._free(pptr);
486 return null;
487 }
488
489 // The first typed array is just a view into memory. Because we will
490 // be free-ing that, we call slice to make a persistent copy.
491 var pixels = new Uint8Array(CanvasKit.HEAPU8.buffer, pptr, len).slice();
492 CanvasKit._free(pptr);
493 return pixels;
494 }
495
496 // pixels is a TypedArray. No matter the input size, it will be treated as
497 // a Uint8Array (essentially, a byte array).
498 CanvasKit.SkCanvas.prototype.writePixels = function(pixels, srcWidth, srcHeight,
499 destX, destY, alphaType, colorType) {
500 if (pixels.byteLength % (srcWidth * srcHeight)) {
501 throw 'pixels length must be a multiple of the srcWidth * srcHeight';
502 }
503 var bytesPerPixel = pixels.byteLength / (srcWidth * srcHeight);
504 // supply defaults (which are compatible with HTMLCanvas's putImageData)
505 alphaType = alphaType || CanvasKit.AlphaType.Unpremul;
506 colorType = colorType || CanvasKit.ColorType.RGBA_8888;
507 var srcRowBytes = bytesPerPixel * srcWidth;
508
Kevin Lubick52b9f372018-12-04 13:57:36 -0500509 var pptr = CanvasKit._malloc(pixels.byteLength);
510 CanvasKit.HEAPU8.set(pixels, pptr);
Kevin Lubick52b9f372018-12-04 13:57:36 -0500511
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500512 var ok = this._writePixels({
513 'width': srcWidth,
514 'height': srcHeight,
515 'colorType': colorType,
516 'alphaType': alphaType,
517 }, pptr, srcRowBytes, destX, destY);
518
519 CanvasKit._free(pptr);
520 return ok;
Kevin Lubick52b9f372018-12-04 13:57:36 -0500521 }
522
Kevin Lubickd3cfbca2019-03-15 15:36:29 -0400523 // Returns an array of the widths of the glyphs in this string.
524 CanvasKit.SkFont.prototype.getWidths = function(str) {
525 // add 1 for null terminator
526 var codePoints = str.length + 1;
527 // lengthBytesUTF8 and stringToUTF8Array are defined in the emscripten
528 // JS. See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#stringToUTF8
529 // Add 1 for null terminator
530 var strBytes = lengthBytesUTF8(str) + 1;
531 var strPtr = CanvasKit._malloc(strBytes);
532 stringToUTF8(str, strPtr, strBytes);
533
534 var bytesPerFloat = 4;
535 // allocate widths == numCodePoints
536 var widthPtr = CanvasKit._malloc(codePoints * bytesPerFloat);
537 if (!this._getWidths(strPtr, strBytes, codePoints, widthPtr)) {
538 SkDebug('Could not compute widths');
539 CanvasKit._free(strPtr);
540 CanvasKit._free(widthPtr);
541 return null;
542 }
543 // reminder, this shouldn't copy the data, just is a nice way to
544 // wrap 4 bytes together into a float.
545 var widths = new Float32Array(CanvasKit.buffer, widthPtr, codePoints);
546 // This copies the data so we can free the CanvasKit memory
547 var retVal = Array.from(widths);
548 CanvasKit._free(strPtr);
549 CanvasKit._free(widthPtr);
550 return retVal;
551 }
552
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500553 // fontData should be an arrayBuffer
554 CanvasKit.SkFontMgr.prototype.MakeTypefaceFromData = function(fontData) {
555 var data = new Uint8Array(fontData);
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400556
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500557 var fptr = CanvasKit._malloc(data.byteLength);
558 CanvasKit.HEAPU8.set(data, fptr);
559 var font = this._makeTypefaceFromData(fptr, data.byteLength);
560 if (!font) {
561 SkDebug('Could not decode font data');
562 // We do not need to free the data since the C++ will do that for us
563 // when the font is deleted (or fails to decode);
564 return null;
565 }
566 return font;
567 }
568
Kevin Lubickcc13fd32019-04-05 13:00:01 -0400569 // The serialized format of an SkPicture (informally called an "skp"), is not something
570 // that clients should ever rely on. It is useful when filing bug reports, but that's
571 // about it. The format may change at anytime and no promises are made for backwards
572 // or forward compatibility.
573 CanvasKit.SkPicture.prototype.DEBUGONLY_saveAsFile = function(skpName) {
574 var data = this.DEBUGONLY_serialize();
575 if (!data) {
576 SkDebug('Could not serialize to skpicture.');
577 return;
578 }
579 var bytes = CanvasKit.getSkDataBytes(data);
580 saveBytesToFile(bytes, skpName);
581 data.delete();
582 }
583
584 CanvasKit.SkSurface.prototype.captureFrameAsSkPicture = function(drawFrame) {
585 // Set up SkPictureRecorder
586 var spr = new CanvasKit.SkPictureRecorder();
587 var canvas = spr.beginRecording(
588 CanvasKit.LTRBRect(0, 0, this.width(), this.height()));
589 drawFrame(canvas);
590 var pic = spr.finishRecordingAsPicture();
591 spr.delete();
592 // TODO: do we need to clean up the memory for canvas?
593 // If we delete it here, saveAsFile doesn't work correctly.
594 return pic;
595 }
596
Kevin Lubick359a7e32019-03-19 09:34:37 -0400597 CanvasKit.SkSurface.prototype.requestAnimationFrame = function(callback, dirtyRect) {
598 if (!this._cached_canvas) {
599 this._cached_canvas = this.getCanvas();
600 }
601 window.requestAnimationFrame(function() {
Kevin Lubick39026282019-03-28 12:46:40 -0400602 if (this._context !== undefined) {
603 CanvasKit.setCurrentContext(this._context);
604 }
Kevin Lubick359a7e32019-03-19 09:34:37 -0400605
606 callback(this._cached_canvas);
607
608 this.flush();
609 }.bind(this));
610 }
611
Kevin Lubickd3cfbca2019-03-15 15:36:29 -0400612 CanvasKit.SkTextBlob.MakeOnPath = function(str, path, font, initialOffset) {
613 if (!str || !str.length) {
614 SkDebug('ignoring 0 length string');
615 return;
616 }
617 if (!path || !path.countPoints()) {
618 SkDebug('ignoring empty path');
619 return;
620 }
621 if (path.countPoints() === 1) {
622 SkDebug('path has 1 point, returning normal textblob');
623 return this.MakeFromText(str, font);
624 }
625
626 if (!initialOffset) {
627 initialOffset = 0;
628 }
629
630 var widths = font.getWidths(str);
631
632 var rsx = new CanvasKit.RSXFormBuilder();
633 var meas = new CanvasKit.SkPathMeasure(path, false, 1);
634 var dist = initialOffset;
635 for (var i = 0; i < str.length; i++) {
636 var width = widths[i];
637 dist += width/2;
638 if (dist > meas.getLength()) {
639 // jump to next contour
640 if (!meas.nextContour()) {
641 // We have come to the end of the path - terminate the string
642 // right here.
643 str = str.substring(0, i);
644 break;
645 }
646 dist = width/2;
647 }
648
649 // Gives us the (x, y) coordinates as well as the cos/sin of the tangent
650 // line at that position.
651 var xycs = meas.getPosTan(dist);
652 var cx = xycs[0];
653 var cy = xycs[1];
654 var cosT = xycs[2];
655 var sinT = xycs[3];
656
657 var adjustedX = cx - (width/2 * cosT);
658 var adjustedY = cy - (width/2 * sinT);
659
660 rsx.push(cosT, sinT, adjustedX, adjustedY);
661 dist += width/2;
662 }
663 var retVal = this.MakeFromRSXform(str, rsx, font);
664 rsx.delete();
665 meas.delete();
666 return retVal;
667 }
668
669 CanvasKit.SkTextBlob.MakeFromRSXform = function(str, rsxBuilder, font) {
670 // lengthBytesUTF8 and stringToUTF8Array are defined in the emscripten
671 // JS. See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#stringToUTF8
672 // Add 1 for null terminator
673 var strLen = lengthBytesUTF8(str) + 1;
674 var strPtr = CanvasKit._malloc(strLen);
675 // Add 1 for the null terminator.
676 stringToUTF8(str, strPtr, strLen);
677 var rptr = rsxBuilder.build();
678
679 var blob = CanvasKit.SkTextBlob._MakeFromRSXform(strPtr, strLen - 1,
680 rptr, font, CanvasKit.TextEncoding.UTF8);
681 if (!blob) {
682 SkDebug('Could not make textblob from string "' + str + '"');
683 return null;
684 }
685
686 var origDelete = blob.delete.bind(blob);
687 blob.delete = function() {
688 CanvasKit._free(strPtr);
689 origDelete();
690 }
691 return blob;
692 }
693
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500694 CanvasKit.SkTextBlob.MakeFromText = function(str, font) {
695 // lengthBytesUTF8 and stringToUTF8Array are defined in the emscripten
696 // JS. See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#stringToUTF8
697 // Add 1 for null terminator
698 var strLen = lengthBytesUTF8(str) + 1;
699 var strPtr = CanvasKit._malloc(strLen);
700 // Add 1 for the null terminator.
701 stringToUTF8(str, strPtr, strLen);
702
703 var blob = CanvasKit.SkTextBlob._MakeFromText(strPtr, strLen - 1, font, CanvasKit.TextEncoding.UTF8);
704 if (!blob) {
705 SkDebug('Could not make textblob from string "' + str + '"');
706 return null;
Kevin Lubick217056c2018-09-20 17:39:31 -0400707 }
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400708
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500709 var origDelete = blob.delete.bind(blob);
710 blob.delete = function() {
711 CanvasKit._free(strPtr);
712 origDelete();
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400713 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500714 return blob;
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400715 }
716
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500717 // Run through the JS files that are added at compile time.
718 if (CanvasKit._extraInitializations) {
719 CanvasKit._extraInitializations.forEach(function(init) {
720 init();
721 });
Kevin Lubickeb2f6b02018-11-29 15:07:02 -0500722 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500723}; // end CanvasKit.onRuntimeInitialized, that is, anything changing prototypes or dynamic.
Kevin Lubickeb2f6b02018-11-29 15:07:02 -0500724
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500725CanvasKit.LTRBRect = function(l, t, r, b) {
726 return {
727 fLeft: l,
728 fTop: t,
729 fRight: r,
730 fBottom: b,
731 };
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500732}
733
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500734CanvasKit.XYWHRect = function(x, y, w, h) {
735 return {
736 fLeft: x,
737 fTop: y,
738 fRight: x+w,
739 fBottom: y+h,
740 };
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500741}
742
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500743CanvasKit.MakePathFromCmds = function(cmds) {
744 var ptrLen = loadCmdsTypedArray(cmds);
745 var path = CanvasKit._MakePathFromCmds(ptrLen[0], ptrLen[1]);
746 CanvasKit._free(ptrLen[0]);
747 return path;
748}
749
750CanvasKit.MakeSkDashPathEffect = function(intervals, phase) {
751 if (!phase) {
752 phase = 0;
753 }
754 if (!intervals.length || intervals.length % 2 === 1) {
755 throw 'Intervals array must have even length';
756 }
757 var ptr = copy1dArray(intervals, CanvasKit.HEAPF32);
758 var dpe = CanvasKit._MakeSkDashPathEffect(ptr, intervals.length, phase);
759 CanvasKit._free(ptr);
760 return dpe;
761}
762
763// data is a TypedArray or ArrayBuffer e.g. from fetch().then(resp.arrayBuffer())
764CanvasKit.MakeImageFromEncoded = function(data) {
765 data = new Uint8Array(data);
766
767 var iptr = CanvasKit._malloc(data.byteLength);
768 CanvasKit.HEAPU8.set(data, iptr);
769 var img = CanvasKit._decodeImage(iptr, data.byteLength);
770 if (!img) {
771 SkDebug('Could not decode image');
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500772 return null;
773 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500774 return img;
775}
776
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500777// pixels is a Uint8Array
778CanvasKit.MakeImage = function(pixels, width, height, alphaType, colorType) {
779 var bytesPerPixel = pixels.byteLength / (width * height);
780 var info = {
781 'width': width,
782 'height': height,
783 'alphaType': alphaType,
784 'colorType': colorType,
785 };
786 var pptr = CanvasKit._malloc(pixels.byteLength);
787 CanvasKit.HEAPU8.set(pixels, pptr);
788 // No need to _free iptr, Image takes it with SkData::MakeFromMalloc
789
790 return CanvasKit._MakeImage(info, pptr, pixels.byteLength, width * bytesPerPixel);
791}
792
793CanvasKit.MakeLinearGradientShader = function(start, end, colors, pos, mode, localMatrix, flags) {
Kevin Lubickee91c072019-03-29 10:39:52 -0400794 var colorPtr = copy1dArray(colors, CanvasKit.HEAPU32);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500795 var posPtr = copy1dArray(pos, CanvasKit.HEAPF32);
796 flags = flags || 0;
797
798 if (localMatrix) {
799 // Add perspective args if not provided.
800 if (localMatrix.length === 6) {
801 localMatrix.push(0, 0, 1);
802 }
803 var lgs = CanvasKit._MakeLinearGradientShader(start, end, colorPtr, posPtr,
804 colors.length, mode, flags, localMatrix);
805 } else {
806 var lgs = CanvasKit._MakeLinearGradientShader(start, end, colorPtr, posPtr,
807 colors.length, mode, flags);
808 }
809
810 CanvasKit._free(colorPtr);
811 CanvasKit._free(posPtr);
812 return lgs;
813}
814
815CanvasKit.MakeRadialGradientShader = function(center, radius, colors, pos, mode, localMatrix, flags) {
Kevin Lubickee91c072019-03-29 10:39:52 -0400816 var colorPtr = copy1dArray(colors, CanvasKit.HEAPU32);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500817 var posPtr = copy1dArray(pos, CanvasKit.HEAPF32);
818 flags = flags || 0;
819
820 if (localMatrix) {
821 // Add perspective args if not provided.
822 if (localMatrix.length === 6) {
823 localMatrix.push(0, 0, 1);
824 }
825 var rgs = CanvasKit._MakeRadialGradientShader(center, radius, colorPtr, posPtr,
826 colors.length, mode, flags, localMatrix);
827 } else {
828 var rgs = CanvasKit._MakeRadialGradientShader(center, radius, colorPtr, posPtr,
829 colors.length, mode, flags);
830 }
831
832 CanvasKit._free(colorPtr);
833 CanvasKit._free(posPtr);
834 return rgs;
835}
836
837CanvasKit.MakeTwoPointConicalGradientShader = function(start, startRadius, end, endRadius,
838 colors, pos, mode, localMatrix, flags) {
Kevin Lubickee91c072019-03-29 10:39:52 -0400839 var colorPtr = copy1dArray(colors, CanvasKit.HEAPU32);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500840 var posPtr = copy1dArray(pos, CanvasKit.HEAPF32);
841 flags = flags || 0;
842
843 if (localMatrix) {
844 // Add perspective args if not provided.
845 if (localMatrix.length === 6) {
846 localMatrix.push(0, 0, 1);
847 }
848 var rgs = CanvasKit._MakeTwoPointConicalGradientShader(
849 start, startRadius, end, endRadius,
850 colorPtr, posPtr, colors.length, mode, flags, localMatrix);
851 } else {
852 var rgs = CanvasKit._MakeTwoPointConicalGradientShader(
853 start, startRadius, end, endRadius,
854 colorPtr, posPtr, colors.length, mode, flags);
855 }
856
857 CanvasKit._free(colorPtr);
858 CanvasKit._free(posPtr);
859 return rgs;
860}
861
862CanvasKit.MakeSkVertices = function(mode, positions, textureCoordinates, colors,
Kevin Lubickb3574c92019-03-06 08:25:36 -0500863 boneIndices, boneWeights, indices, isVolatile) {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500864 var positionPtr = copy2dArray(positions, CanvasKit.HEAPF32);
865 var texPtr = copy2dArray(textureCoordinates, CanvasKit.HEAPF32);
Kevin Lubickee91c072019-03-29 10:39:52 -0400866 var colorPtr = copy1dArray(colors, CanvasKit.HEAPU32);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500867
868 var boneIdxPtr = copy2dArray(boneIndices, CanvasKit.HEAP32);
869 var boneWtPtr = copy2dArray(boneWeights, CanvasKit.HEAPF32);
870 var idxPtr = copy1dArray(indices, CanvasKit.HEAPU16);
871
Kevin Lubickb3574c92019-03-06 08:25:36 -0500872 // Default isVolitile to true if not set
873 isVolatile = isVolatile === undefined ? true : isVolatile;
874
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500875 var idxCount = (indices && indices.length) || 0;
876 // _MakeVertices will copy all the values in, so we are free to release
877 // the memory after.
878 var vertices = CanvasKit._MakeSkVertices(mode, positions.length, positionPtr,
879 texPtr, colorPtr, boneIdxPtr, boneWtPtr,
Kevin Lubickb3574c92019-03-06 08:25:36 -0500880 idxCount, idxPtr, isVolatile);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500881 positionPtr && CanvasKit._free(positionPtr);
882 texPtr && CanvasKit._free(texPtr);
883 colorPtr && CanvasKit._free(colorPtr);
884 idxPtr && CanvasKit._free(idxPtr);
885 boneIdxPtr && CanvasKit._free(boneIdxPtr);
886 boneWtPtr && CanvasKit._free(boneWtPtr);
887 return vertices;
Kevin Lubicka064c282019-04-04 09:28:53 -0400888};