2019-10-03 15:22:08 +00:00
|
|
|
(function(CanvasKit){
|
|
|
|
CanvasKit._extraInitializations = CanvasKit._extraInitializations || [];
|
|
|
|
CanvasKit._extraInitializations.push(function() {
|
|
|
|
|
|
|
|
CanvasKit.Paragraph.prototype.getRectsForRange = function(start, end, hStyle, wStyle) {
|
|
|
|
/**
|
|
|
|
* This is bytes, but we'll want to think about them as float32s
|
|
|
|
* @type {Float32Array}
|
|
|
|
*/
|
|
|
|
var floatArray = this._getRectsForRange(start, end, hStyle, wStyle);
|
2020-10-02 22:24:13 +00:00
|
|
|
return floatArrayToRects(floatArray);
|
|
|
|
}
|
2019-10-03 15:22:08 +00:00
|
|
|
|
2020-10-02 22:24:13 +00:00
|
|
|
CanvasKit.Paragraph.prototype.getRectsForPlaceholders = function() {
|
|
|
|
/**
|
|
|
|
* This is bytes, but we'll want to think about them as float32s
|
|
|
|
* @type {Float32Array}
|
|
|
|
*/
|
|
|
|
var floatArray = this._getRectsForPlaceholders();
|
|
|
|
return floatArrayToRects(floatArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
function floatArrayToRects(floatArray) {
|
|
|
|
if (!floatArray || !floatArray.length) {
|
|
|
|
return [];
|
2019-11-20 13:27:10 +00:00
|
|
|
}
|
2020-10-02 22:24:13 +00:00
|
|
|
var ret = [];
|
|
|
|
for (var i = 0; i < floatArray.length; i+=5) {
|
|
|
|
var r = CanvasKit.LTRBRect(floatArray[i], floatArray[i+1], floatArray[i+2], floatArray[i+3]);
|
|
|
|
if (floatArray[i+4] === 0) {
|
|
|
|
r['direction'] = CanvasKit.TextDirection.RTL;
|
|
|
|
} else {
|
|
|
|
r['direction'] = CanvasKit.TextDirection.LTR;
|
|
|
|
}
|
|
|
|
ret.push(r);
|
|
|
|
}
|
|
|
|
CanvasKit._free(floatArray.byteOffset);
|
|
|
|
return ret;
|
2019-10-03 15:22:08 +00:00
|
|
|
}
|
|
|
|
|
2020-08-04 20:21:09 +00:00
|
|
|
// Registers the font (provided as an arrayBuffer) with the alias `family`.
|
|
|
|
CanvasKit.TypefaceFontProvider.prototype.registerFont = function(font, family) {
|
2021-10-21 15:35:04 +00:00
|
|
|
var typeface = CanvasKit.Typeface.MakeFreeTypeFaceFromData(font);
|
2020-08-04 20:21:09 +00:00
|
|
|
if (!typeface) {
|
2020-10-07 20:09:22 +00:00
|
|
|
Debug('Could not decode font data');
|
2020-08-04 20:21:09 +00:00
|
|
|
// We do not need to free the data since the C++ will do that for us
|
|
|
|
// when the font is deleted (or fails to decode);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
var familyPtr = cacheOrCopyString(family);
|
|
|
|
this._registerFont(typeface, familyPtr);
|
|
|
|
}
|
|
|
|
|
2019-10-03 15:22:08 +00:00
|
|
|
// These helpers fill out all fields, because emscripten complains if we
|
|
|
|
// have undefined and it expects, for example, a float.
|
2020-10-06 12:24:00 +00:00
|
|
|
// TODO(kjlubick) For efficiency, we should probably just return opaque WASM objects so we do
|
|
|
|
// not have to keep copying them across the wire.
|
2019-10-03 15:22:08 +00:00
|
|
|
CanvasKit.ParagraphStyle = function(s) {
|
|
|
|
// Use [''] to tell closure not to minify the names
|
2019-10-21 14:50:26 +00:00
|
|
|
s['disableHinting'] = s['disableHinting'] || false;
|
|
|
|
if (s['ellipsis']) {
|
|
|
|
var str = s['ellipsis'];
|
2020-05-14 12:27:53 +00:00
|
|
|
s['_ellipsisPtr'] = cacheOrCopyString(str);
|
|
|
|
s['_ellipsisLen'] = lengthBytesUTF8(str) + 1; // add 1 for the null terminator.
|
2019-10-21 14:50:26 +00:00
|
|
|
} else {
|
|
|
|
s['_ellipsisPtr'] = nullptr;
|
|
|
|
s['_ellipsisLen'] = 0;
|
|
|
|
}
|
|
|
|
|
2019-10-03 15:22:08 +00:00
|
|
|
s['heightMultiplier'] = s['heightMultiplier'] || 0;
|
|
|
|
s['maxLines'] = s['maxLines'] || 0;
|
2021-04-22 17:53:35 +00:00
|
|
|
s['strutStyle'] = strutStyle(s['strutStyle']);
|
2019-10-03 15:22:08 +00:00
|
|
|
s['textAlign'] = s['textAlign'] || CanvasKit.TextAlign.Start;
|
2019-10-21 14:50:26 +00:00
|
|
|
s['textDirection'] = s['textDirection'] || CanvasKit.TextDirection.LTR;
|
2021-04-22 17:53:35 +00:00
|
|
|
s['textHeightBehavior'] = s['textHeightBehavior'] || CanvasKit.TextHeightBehavior.All;
|
2019-10-03 15:22:08 +00:00
|
|
|
s['textStyle'] = CanvasKit.TextStyle(s['textStyle']);
|
|
|
|
return s;
|
2020-05-14 12:27:53 +00:00
|
|
|
};
|
2019-10-03 15:22:08 +00:00
|
|
|
|
2019-10-21 14:50:26 +00:00
|
|
|
function fontStyle(s) {
|
|
|
|
s = s || {};
|
|
|
|
// Can't check for falsey as 0 width means "invisible".
|
|
|
|
if (s['weight'] === undefined) {
|
|
|
|
s['weight'] = CanvasKit.FontWeight.Normal;
|
|
|
|
}
|
|
|
|
s['width'] = s['width'] || CanvasKit.FontWidth.Normal;
|
|
|
|
s['slant'] = s['slant'] || CanvasKit.FontSlant.Upright;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2020-10-02 22:24:13 +00:00
|
|
|
function strutStyle(s) {
|
|
|
|
s = s || {};
|
|
|
|
s['strutEnabled'] = s['strutEnabled'] || false;
|
|
|
|
|
|
|
|
if (s['strutEnabled'] && Array.isArray(s['fontFamilies']) && s['fontFamilies'].length) {
|
|
|
|
s['_fontFamiliesPtr'] = naiveCopyStrArray(s['fontFamilies']);
|
|
|
|
s['_fontFamiliesLen'] = s['fontFamilies'].length;
|
|
|
|
} else {
|
|
|
|
s['_fontFamiliesPtr'] = nullptr;
|
|
|
|
s['_fontFamiliesLen'] = 0;
|
|
|
|
}
|
|
|
|
s['fontStyle'] = fontStyle(s['fontStyle']);
|
|
|
|
s['fontSize'] = s['fontSize'] || 0;
|
|
|
|
s['heightMultiplier'] = s['heightMultiplier'] || 0;
|
2021-04-20 17:55:49 +00:00
|
|
|
s['halfLeading'] = s['halfLeading'] || false;
|
2020-10-02 22:24:13 +00:00
|
|
|
s['leading'] = s['leading'] || 0;
|
|
|
|
s['forceStrutHeight'] = s['forceStrutHeight'] || false;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2019-10-03 15:22:08 +00:00
|
|
|
CanvasKit.TextStyle = function(s) {
|
|
|
|
// Use [''] to tell closure not to minify the names
|
2020-06-11 17:48:16 +00:00
|
|
|
if (!s['color']) {
|
2019-10-21 14:50:26 +00:00
|
|
|
s['color'] = CanvasKit.BLACK;
|
|
|
|
}
|
2020-05-04 20:46:17 +00:00
|
|
|
|
2019-10-03 15:22:08 +00:00
|
|
|
s['decoration'] = s['decoration'] || 0;
|
|
|
|
s['decorationThickness'] = s['decorationThickness'] || 0;
|
2020-10-02 22:24:13 +00:00
|
|
|
s['decorationStyle'] = s['decorationStyle'] || CanvasKit.DecorationStyle.Solid;
|
|
|
|
s['textBaseline'] = s['textBaseline'] || CanvasKit.TextBaseline.Alphabetic;
|
2019-10-03 15:22:08 +00:00
|
|
|
s['fontSize'] = s['fontSize'] || 0;
|
2020-10-02 22:24:13 +00:00
|
|
|
s['letterSpacing'] = s['letterSpacing'] || 0;
|
|
|
|
s['wordSpacing'] = s['wordSpacing'] || 0;
|
|
|
|
s['heightMultiplier'] = s['heightMultiplier'] || 0;
|
2021-04-20 17:55:49 +00:00
|
|
|
s['halfLeading'] = s['halfLeading'] || false;
|
2019-10-21 14:50:26 +00:00
|
|
|
s['fontStyle'] = fontStyle(s['fontStyle']);
|
2021-06-15 17:40:38 +00:00
|
|
|
|
|
|
|
// Properties which need to be Malloc'ed are set in `copyArrays`.
|
2020-10-02 22:24:13 +00:00
|
|
|
|
2019-10-03 15:22:08 +00:00
|
|
|
return s;
|
2020-05-14 12:27:53 +00:00
|
|
|
};
|
2019-10-03 15:22:08 +00:00
|
|
|
|
|
|
|
// returns a pointer to a place on the heap that has an array
|
|
|
|
// of char* (effectively a char**). For now, this does the naive thing
|
|
|
|
// and depends on the string being null-terminated. This should be used
|
|
|
|
// for simple, well-formed things (e.g. font-families), not arbitrary
|
|
|
|
// text that should be drawn. If we need this to handle more complex
|
|
|
|
// strings, it should return two pointers, a pointer of the
|
|
|
|
// string array and a pointer to an array of the strings byte lengths.
|
|
|
|
function naiveCopyStrArray(strings) {
|
|
|
|
if (!strings || !strings.length) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
var sPtrs = [];
|
|
|
|
for (var i = 0; i < strings.length; i++) {
|
2020-05-14 12:27:53 +00:00
|
|
|
var strPtr = cacheOrCopyString(strings[i]);
|
2019-10-03 15:22:08 +00:00
|
|
|
sPtrs.push(strPtr);
|
|
|
|
}
|
2020-10-02 22:24:13 +00:00
|
|
|
return copy1dArray(sPtrs, 'HEAPU32');
|
2019-10-03 15:22:08 +00:00
|
|
|
}
|
2020-05-04 20:46:17 +00:00
|
|
|
|
2020-05-14 12:27:53 +00:00
|
|
|
// maps string -> malloc'd pointer
|
|
|
|
var stringCache = {};
|
|
|
|
|
|
|
|
// cacheOrCopyString copies a string from JS into WASM on the heap and returns the pointer
|
|
|
|
// to the memory of the string. It is expected that a caller to this helper will *not* free
|
|
|
|
// that memory, so it is cached. Thus, if a future call to this function with the same string
|
|
|
|
// will return the cached pointer, preventing the memory usage from growing unbounded (in
|
|
|
|
// a normal use case).
|
|
|
|
function cacheOrCopyString(str) {
|
|
|
|
if (stringCache[str]) {
|
|
|
|
return stringCache[str];
|
|
|
|
}
|
|
|
|
// Add 1 for null terminator, which we need when copying/converting
|
|
|
|
var strLen = lengthBytesUTF8(str) + 1;
|
|
|
|
var strPtr = CanvasKit._malloc(strLen);
|
|
|
|
stringToUTF8(str, strPtr, strLen);
|
|
|
|
stringCache[str] = strPtr;
|
|
|
|
return strPtr;
|
|
|
|
}
|
|
|
|
|
2020-06-01 15:25:47 +00:00
|
|
|
// These scratch arrays are allocated once to copy the color data into, which saves us
|
|
|
|
// having to free them after every invocation.
|
|
|
|
var scratchForegroundColorPtr = CanvasKit._malloc(4 * 4); // room for 4 32bit floats
|
|
|
|
var scratchBackgroundColorPtr = CanvasKit._malloc(4 * 4); // room for 4 32bit floats
|
2020-10-02 22:24:13 +00:00
|
|
|
var scratchDecorationColorPtr = CanvasKit._malloc(4 * 4); // room for 4 32bit floats
|
2020-06-01 15:25:47 +00:00
|
|
|
|
2020-05-14 12:27:53 +00:00
|
|
|
function copyArrays(textStyle) {
|
|
|
|
// These color fields were arrays, but will set to WASM pointers before we pass this
|
2020-05-04 20:46:17 +00:00
|
|
|
// object over the WASM interface.
|
2020-06-01 15:25:47 +00:00
|
|
|
textStyle['_colorPtr'] = copyColorToWasm(textStyle['color']);
|
2020-05-14 12:27:53 +00:00
|
|
|
textStyle['_foregroundColorPtr'] = nullptr; // nullptr is 0, from helper.js
|
|
|
|
textStyle['_backgroundColorPtr'] = nullptr;
|
2020-10-02 22:24:13 +00:00
|
|
|
textStyle['_decorationColorPtr'] = nullptr;
|
2020-06-01 15:25:47 +00:00
|
|
|
if (textStyle['foregroundColor']) {
|
|
|
|
textStyle['_foregroundColorPtr'] = copyColorToWasm(textStyle['foregroundColor'], scratchForegroundColorPtr);
|
2020-05-04 20:46:17 +00:00
|
|
|
}
|
2020-06-01 15:25:47 +00:00
|
|
|
if (textStyle['backgroundColor']) {
|
|
|
|
textStyle['_backgroundColorPtr'] = copyColorToWasm(textStyle['backgroundColor'], scratchBackgroundColorPtr);
|
2020-05-14 12:27:53 +00:00
|
|
|
}
|
2020-10-02 22:24:13 +00:00
|
|
|
if (textStyle['decorationColor']) {
|
|
|
|
textStyle['_decorationColorPtr'] = copyColorToWasm(textStyle['decorationColor'], scratchDecorationColorPtr);
|
|
|
|
}
|
2020-05-14 12:27:53 +00:00
|
|
|
|
|
|
|
if (Array.isArray(textStyle['fontFamilies']) && textStyle['fontFamilies'].length) {
|
|
|
|
textStyle['_fontFamiliesPtr'] = naiveCopyStrArray(textStyle['fontFamilies']);
|
|
|
|
textStyle['_fontFamiliesLen'] = textStyle['fontFamilies'].length;
|
|
|
|
} else {
|
|
|
|
textStyle['_fontFamiliesPtr'] = nullptr;
|
|
|
|
textStyle['_fontFamiliesLen'] = 0;
|
2020-10-07 20:09:22 +00:00
|
|
|
Debug('no font families provided, text may draw wrong or not at all');
|
2020-05-04 20:46:17 +00:00
|
|
|
}
|
2021-06-15 17:40:38 +00:00
|
|
|
|
|
|
|
if (textStyle['locale']) {
|
|
|
|
var str = textStyle['locale'];
|
|
|
|
textStyle['_localePtr'] = cacheOrCopyString(str);
|
|
|
|
textStyle['_localeLen'] = lengthBytesUTF8(str) + 1; // add 1 for the null terminator.
|
|
|
|
} else {
|
|
|
|
textStyle['_localePtr'] = nullptr;
|
|
|
|
textStyle['_localeLen'] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(textStyle['shadows']) && textStyle['shadows'].length) {
|
|
|
|
var shadows = textStyle['shadows'];
|
|
|
|
var shadowColors = shadows.map(function (s) { return s['color'] || CanvasKit.BLACK; });
|
|
|
|
var shadowBlurRadii = shadows.map(function (s) { return s['blurRadius'] || 0.0; });
|
|
|
|
textStyle['_shadowLen'] = shadows.length;
|
|
|
|
// 2 floats per point, 4 bytes per float
|
|
|
|
var ptr = CanvasKit._malloc(shadows.length * 2 * 4);
|
|
|
|
var adjustedPtr = ptr / 4; // 4 bytes per float
|
|
|
|
for (var i = 0; i < shadows.length; i++) {
|
|
|
|
var offset = shadows[i]['offset'] || [0, 0];
|
|
|
|
CanvasKit.HEAPF32[adjustedPtr] = offset[0];
|
|
|
|
CanvasKit.HEAPF32[adjustedPtr + 1] = offset[1];
|
|
|
|
adjustedPtr += 2;
|
|
|
|
}
|
|
|
|
textStyle['_shadowColorsPtr'] = copyFlexibleColorArray(shadowColors).colorPtr;
|
|
|
|
textStyle['_shadowOffsetsPtr'] = ptr;
|
|
|
|
textStyle['_shadowBlurRadiiPtr'] = copy1dArray(shadowBlurRadii, 'HEAPF32');
|
|
|
|
} else {
|
|
|
|
textStyle['_shadowLen'] = 0;
|
|
|
|
textStyle['_shadowColorsPtr'] = nullptr;
|
|
|
|
textStyle['_shadowOffsetsPtr'] = nullptr;
|
|
|
|
textStyle['_shadowBlurRadiiPtr'] = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(textStyle['fontFeatures']) && textStyle['fontFeatures'].length) {
|
|
|
|
var fontFeatures = textStyle['fontFeatures'];
|
|
|
|
var fontFeatureNames = fontFeatures.map(function (s) { return s['name']; });
|
|
|
|
var fontFeatureValues = fontFeatures.map(function (s) { return s['value']; });
|
|
|
|
textStyle['_fontFeatureLen'] = fontFeatures.length;
|
|
|
|
textStyle['_fontFeatureNamesPtr'] = naiveCopyStrArray(fontFeatureNames);
|
|
|
|
textStyle['_fontFeatureValuesPtr'] = copy1dArray(fontFeatureValues, 'HEAPU32');
|
|
|
|
} else {
|
|
|
|
textStyle['_fontFeatureLen'] = 0;
|
|
|
|
textStyle['_fontFeatureNamesPtr'] = nullptr;
|
|
|
|
textStyle['_fontFeatureValuesPtr'] = nullptr;
|
|
|
|
}
|
2020-05-04 20:46:17 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 12:27:53 +00:00
|
|
|
function freeArrays(textStyle) {
|
|
|
|
// The font family strings will get copied to a vector on the C++ side, which is owned by
|
|
|
|
// the text style.
|
|
|
|
CanvasKit._free(textStyle['_fontFamiliesPtr']);
|
2021-06-15 17:40:38 +00:00
|
|
|
CanvasKit._free(textStyle['_shadowColorsPtr']);
|
|
|
|
CanvasKit._free(textStyle['_shadowOffsetsPtr']);
|
|
|
|
CanvasKit._free(textStyle['_shadowBlurRadiiPtr']);
|
|
|
|
CanvasKit._free(textStyle['_fontFeatureNamesPtr']);
|
|
|
|
CanvasKit._free(textStyle['_fontFeatureValuesPtr']);
|
2020-05-04 20:46:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CanvasKit.ParagraphBuilder.Make = function(paragraphStyle, fontManager) {
|
2020-05-14 12:27:53 +00:00
|
|
|
copyArrays(paragraphStyle['textStyle']);
|
2020-05-04 20:46:17 +00:00
|
|
|
|
|
|
|
var result = CanvasKit.ParagraphBuilder._Make(paragraphStyle, fontManager);
|
2020-05-14 12:27:53 +00:00
|
|
|
freeArrays(paragraphStyle['textStyle']);
|
2020-05-04 20:46:17 +00:00
|
|
|
return result;
|
2020-05-14 12:27:53 +00:00
|
|
|
};
|
2020-05-04 20:46:17 +00:00
|
|
|
|
2020-08-04 20:21:09 +00:00
|
|
|
CanvasKit.ParagraphBuilder.MakeFromFontProvider = function(paragraphStyle, fontProvider) {
|
|
|
|
copyArrays(paragraphStyle['textStyle']);
|
|
|
|
|
|
|
|
var result = CanvasKit.ParagraphBuilder._MakeFromFontProvider(paragraphStyle, fontProvider);
|
|
|
|
freeArrays(paragraphStyle['textStyle']);
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2021-05-06 22:12:27 +00:00
|
|
|
CanvasKit.ParagraphBuilder.ShapeText = function(text, blocks, width) {
|
|
|
|
let length = 0;
|
|
|
|
for (const b of blocks) {
|
|
|
|
length += b.length;
|
|
|
|
}
|
|
|
|
if (length !== text.length) {
|
|
|
|
throw "Accumulated block lengths must equal text.length";
|
|
|
|
}
|
|
|
|
return CanvasKit.ParagraphBuilder._ShapeText(text, blocks, width);
|
|
|
|
};
|
|
|
|
|
2020-05-04 20:46:17 +00:00
|
|
|
CanvasKit.ParagraphBuilder.prototype.pushStyle = function(textStyle) {
|
2020-05-14 12:27:53 +00:00
|
|
|
copyArrays(textStyle);
|
|
|
|
this._pushStyle(textStyle);
|
|
|
|
freeArrays(textStyle);
|
2020-10-02 22:24:13 +00:00
|
|
|
};
|
2020-08-04 13:06:54 +00:00
|
|
|
|
|
|
|
CanvasKit.ParagraphBuilder.prototype.pushPaintStyle = function(textStyle, fg, bg) {
|
|
|
|
copyArrays(textStyle);
|
|
|
|
this._pushPaintStyle(textStyle, fg, bg);
|
|
|
|
freeArrays(textStyle);
|
2020-10-02 22:24:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CanvasKit.ParagraphBuilder.prototype.addPlaceholder =
|
|
|
|
function(width, height, alignment, baseline, offset) {
|
|
|
|
width = width || 0;
|
|
|
|
height = height || 0;
|
|
|
|
alignment = alignment || CanvasKit.PlaceholderAlignment.Baseline;
|
|
|
|
baseline = baseline || CanvasKit.TextBaseline.Alphabetic;
|
|
|
|
offset = offset || 0;
|
|
|
|
this._addPlaceholder(width, height, alignment, baseline, offset);
|
|
|
|
};
|
2019-10-03 15:22:08 +00:00
|
|
|
});
|
2020-05-14 12:27:53 +00:00
|
|
|
}(Module)); // When this file is loaded in, the high level object is "Module";
|