[canvaskit] Allow users to load their own fonts
Instead of using the test font(s), now ship with a small
(100k) Monospace font. This can be disabled by:
compile.sh no_font ...
This saves about 350k (164k gzipped) in binary size.
Bug: skia:
Change-Id: I195e3b35bea86d0f096066c1c6a44a4b602571f3
Reviewed-on: https://skia-review.googlesource.com/c/176580
Reviewed-by: Ben Wagner <bungeman@google.com>
diff --git a/experimental/canvaskit/interface.js b/experimental/canvaskit/interface.js
index a40c568..704721a 100644
--- a/experimental/canvaskit/interface.js
+++ b/experimental/canvaskit/interface.js
@@ -402,6 +402,29 @@
return ok;
}
+ // fontData should be an arrayBuffer
+ CanvasKit.SkFontMgr.prototype.MakeTypefaceFromData = function(fontData) {
+ var data = new Uint8Array(fontData);
+
+ var fptr = CanvasKit._malloc(data.byteLength);
+ CanvasKit.HEAPU8.set(data, fptr);
+ var font = this._makeTypefaceFromData(fptr, data.byteLength);
+ if (!font) {
+ SkDebug('Could not decode font data');
+ // We do not need to free the data since the C++ will do that for us
+ // on a failed decode (at least, it appears to).
+ return null;
+ }
+ // We cannot free this data until after the font stops being used
+ // (otherwise nothing draws)
+ var realDelete = font.delete.bind(font);
+ font.delete = function() {
+ CanvasKit._free(fptr);
+ realDelete();
+ }
+ return font;
+ }
+
// Run through the JS files that are added at compile time.
if (CanvasKit._extraInitializations) {
CanvasKit._extraInitializations.forEach(function(init) {