1(function(CanvasKit){ 2 CanvasKit._extraInitializations = CanvasKit._extraInitializations || []; 3 CanvasKit._extraInitializations.push(function() { 4 5 CanvasKit.Paragraph.prototype.getRectsForRange = function(start, end, hStyle, wStyle) { 6 /** 7 * This is bytes, but we'll want to think about them as float32s 8 * @type {Float32Array} 9 */ 10 var floatArray = this._getRectsForRange(start, end, hStyle, wStyle); 11 return floatArrayToRects(floatArray); 12 } 13 14 CanvasKit.Paragraph.prototype.getRectsForPlaceholders = function() { 15 /** 16 * This is bytes, but we'll want to think about them as float32s 17 * @type {Float32Array} 18 */ 19 var floatArray = this._getRectsForPlaceholders(); 20 return floatArrayToRects(floatArray); 21 } 22 23 function floatArrayToRects(floatArray) { 24 if (!floatArray || !floatArray.length) { 25 return []; 26 } 27 var ret = []; 28 for (var i = 0; i < floatArray.length; i+=5) { 29 var r = CanvasKit.LTRBRect(floatArray[i], floatArray[i+1], floatArray[i+2], floatArray[i+3]); 30 if (floatArray[i+4] === 0) { 31 r['direction'] = CanvasKit.TextDirection.RTL; 32 } else { 33 r['direction'] = CanvasKit.TextDirection.LTR; 34 } 35 ret.push(r); 36 } 37 CanvasKit._free(floatArray.byteOffset); 38 return ret; 39 } 40 41 // Registers the font (provided as an arrayBuffer) with the alias `family`. 42 CanvasKit.TypefaceFontProvider.prototype.registerFont = function(font, family) { 43 var typeface = CanvasKit.Typeface.MakeFreeTypeFaceFromData(font); 44 if (!typeface) { 45 Debug('Could not decode font data'); 46 // We do not need to free the data since the C++ will do that for us 47 // when the font is deleted (or fails to decode); 48 return null; 49 } 50 var familyPtr = cacheOrCopyString(family); 51 this._registerFont(typeface, familyPtr); 52 } 53 54 // These helpers fill out all fields, because emscripten complains if we 55 // have undefined and it expects, for example, a float. 56 // TODO(kjlubick) For efficiency, we should probably just return opaque WASM objects so we do 57 // not have to keep copying them across the wire. 58 CanvasKit.ParagraphStyle = function(s) { 59 // Use [''] to tell closure not to minify the names 60 s['disableHinting'] = s['disableHinting'] || false; 61 if (s['ellipsis']) { 62 var str = s['ellipsis']; 63 s['_ellipsisPtr'] = cacheOrCopyString(str); 64 s['_ellipsisLen'] = lengthBytesUTF8(str) + 1; // add 1 for the null terminator. 65 } else { 66 s['_ellipsisPtr'] = nullptr; 67 s['_ellipsisLen'] = 0; 68 } 69 70 s['heightMultiplier'] = s['heightMultiplier'] || 0; 71 s['maxLines'] = s['maxLines'] || 0; 72 s['strutStyle'] = strutStyle(s['strutStyle']); 73 s['textAlign'] = s['textAlign'] || CanvasKit.TextAlign.Start; 74 s['textDirection'] = s['textDirection'] || CanvasKit.TextDirection.LTR; 75 s['textHeightBehavior'] = s['textHeightBehavior'] || CanvasKit.TextHeightBehavior.All; 76 s['textStyle'] = CanvasKit.TextStyle(s['textStyle']); 77 return s; 78 }; 79 80 function fontStyle(s) { 81 s = s || {}; 82 // Can't check for falsey as 0 width means "invisible". 83 if (s['weight'] === undefined) { 84 s['weight'] = CanvasKit.FontWeight.Normal; 85 } 86 s['width'] = s['width'] || CanvasKit.FontWidth.Normal; 87 s['slant'] = s['slant'] || CanvasKit.FontSlant.Upright; 88 return s; 89 } 90 91 function strutStyle(s) { 92 s = s || {}; 93 s['strutEnabled'] = s['strutEnabled'] || false; 94 95 if (s['strutEnabled'] && Array.isArray(s['fontFamilies']) && s['fontFamilies'].length) { 96 s['_fontFamiliesPtr'] = naiveCopyStrArray(s['fontFamilies']); 97 s['_fontFamiliesLen'] = s['fontFamilies'].length; 98 } else { 99 s['_fontFamiliesPtr'] = nullptr; 100 s['_fontFamiliesLen'] = 0; 101 } 102 s['fontStyle'] = fontStyle(s['fontStyle']); 103 s['fontSize'] = s['fontSize'] || 0; 104 s['heightMultiplier'] = s['heightMultiplier'] || 0; 105 s['halfLeading'] = s['halfLeading'] || false; 106 s['leading'] = s['leading'] || 0; 107 s['forceStrutHeight'] = s['forceStrutHeight'] || false; 108 return s; 109 } 110 111 CanvasKit.TextStyle = function(s) { 112 // Use [''] to tell closure not to minify the names 113 if (!s['color']) { 114 s['color'] = CanvasKit.BLACK; 115 } 116 117 s['decoration'] = s['decoration'] || 0; 118 s['decorationThickness'] = s['decorationThickness'] || 0; 119 s['decorationStyle'] = s['decorationStyle'] || CanvasKit.DecorationStyle.Solid; 120 s['textBaseline'] = s['textBaseline'] || CanvasKit.TextBaseline.Alphabetic; 121 s['fontSize'] = s['fontSize'] || 0; 122 s['letterSpacing'] = s['letterSpacing'] || 0; 123 s['wordSpacing'] = s['wordSpacing'] || 0; 124 s['heightMultiplier'] = s['heightMultiplier'] || 0; 125 s['halfLeading'] = s['halfLeading'] || false; 126 s['fontStyle'] = fontStyle(s['fontStyle']); 127 128 // Properties which need to be Malloc'ed are set in `copyArrays`. 129 130 return s; 131 }; 132 133 // returns a pointer to a place on the heap that has an array 134 // of char* (effectively a char**). For now, this does the naive thing 135 // and depends on the string being null-terminated. This should be used 136 // for simple, well-formed things (e.g. font-families), not arbitrary 137 // text that should be drawn. If we need this to handle more complex 138 // strings, it should return two pointers, a pointer of the 139 // string array and a pointer to an array of the strings byte lengths. 140 function naiveCopyStrArray(strings) { 141 if (!strings || !strings.length) { 142 return nullptr; 143 } 144 var sPtrs = []; 145 for (var i = 0; i < strings.length; i++) { 146 var strPtr = cacheOrCopyString(strings[i]); 147 sPtrs.push(strPtr); 148 } 149 return copy1dArray(sPtrs, 'HEAPU32'); 150 } 151 152 // maps string -> malloc'd pointer 153 var stringCache = {}; 154 155 // cacheOrCopyString copies a string from JS into WASM on the heap and returns the pointer 156 // to the memory of the string. It is expected that a caller to this helper will *not* free 157 // that memory, so it is cached. Thus, if a future call to this function with the same string 158 // will return the cached pointer, preventing the memory usage from growing unbounded (in 159 // a normal use case). 160 function cacheOrCopyString(str) { 161 if (stringCache[str]) { 162 return stringCache[str]; 163 } 164 // Add 1 for null terminator, which we need when copying/converting 165 var strLen = lengthBytesUTF8(str) + 1; 166 var strPtr = CanvasKit._malloc(strLen); 167 stringToUTF8(str, strPtr, strLen); 168 stringCache[str] = strPtr; 169 return strPtr; 170 } 171 172 // These scratch arrays are allocated once to copy the color data into, which saves us 173 // having to free them after every invocation. 174 var scratchForegroundColorPtr = CanvasKit._malloc(4 * 4); // room for 4 32bit floats 175 var scratchBackgroundColorPtr = CanvasKit._malloc(4 * 4); // room for 4 32bit floats 176 var scratchDecorationColorPtr = CanvasKit._malloc(4 * 4); // room for 4 32bit floats 177 178 function copyArrays(textStyle) { 179 // These color fields were arrays, but will set to WASM pointers before we pass this 180 // object over the WASM interface. 181 textStyle['_colorPtr'] = copyColorToWasm(textStyle['color']); 182 textStyle['_foregroundColorPtr'] = nullptr; // nullptr is 0, from helper.js 183 textStyle['_backgroundColorPtr'] = nullptr; 184 textStyle['_decorationColorPtr'] = nullptr; 185 if (textStyle['foregroundColor']) { 186 textStyle['_foregroundColorPtr'] = copyColorToWasm(textStyle['foregroundColor'], scratchForegroundColorPtr); 187 } 188 if (textStyle['backgroundColor']) { 189 textStyle['_backgroundColorPtr'] = copyColorToWasm(textStyle['backgroundColor'], scratchBackgroundColorPtr); 190 } 191 if (textStyle['decorationColor']) { 192 textStyle['_decorationColorPtr'] = copyColorToWasm(textStyle['decorationColor'], scratchDecorationColorPtr); 193 } 194 195 if (Array.isArray(textStyle['fontFamilies']) && textStyle['fontFamilies'].length) { 196 textStyle['_fontFamiliesPtr'] = naiveCopyStrArray(textStyle['fontFamilies']); 197 textStyle['_fontFamiliesLen'] = textStyle['fontFamilies'].length; 198 } else { 199 textStyle['_fontFamiliesPtr'] = nullptr; 200 textStyle['_fontFamiliesLen'] = 0; 201 Debug('no font families provided, text may draw wrong or not at all'); 202 } 203 204 if (textStyle['locale']) { 205 var str = textStyle['locale']; 206 textStyle['_localePtr'] = cacheOrCopyString(str); 207 textStyle['_localeLen'] = lengthBytesUTF8(str) + 1; // add 1 for the null terminator. 208 } else { 209 textStyle['_localePtr'] = nullptr; 210 textStyle['_localeLen'] = 0; 211 } 212 213 if (Array.isArray(textStyle['shadows']) && textStyle['shadows'].length) { 214 var shadows = textStyle['shadows']; 215 var shadowColors = shadows.map(function (s) { return s['color'] || CanvasKit.BLACK; }); 216 var shadowBlurRadii = shadows.map(function (s) { return s['blurRadius'] || 0.0; }); 217 textStyle['_shadowLen'] = shadows.length; 218 // 2 floats per point, 4 bytes per float 219 var ptr = CanvasKit._malloc(shadows.length * 2 * 4); 220 var adjustedPtr = ptr / 4; // 4 bytes per float 221 for (var i = 0; i < shadows.length; i++) { 222 var offset = shadows[i]['offset'] || [0, 0]; 223 CanvasKit.HEAPF32[adjustedPtr] = offset[0]; 224 CanvasKit.HEAPF32[adjustedPtr + 1] = offset[1]; 225 adjustedPtr += 2; 226 } 227 textStyle['_shadowColorsPtr'] = copyFlexibleColorArray(shadowColors).colorPtr; 228 textStyle['_shadowOffsetsPtr'] = ptr; 229 textStyle['_shadowBlurRadiiPtr'] = copy1dArray(shadowBlurRadii, 'HEAPF32'); 230 } else { 231 textStyle['_shadowLen'] = 0; 232 textStyle['_shadowColorsPtr'] = nullptr; 233 textStyle['_shadowOffsetsPtr'] = nullptr; 234 textStyle['_shadowBlurRadiiPtr'] = nullptr; 235 } 236 237 if (Array.isArray(textStyle['fontFeatures']) && textStyle['fontFeatures'].length) { 238 var fontFeatures = textStyle['fontFeatures']; 239 var fontFeatureNames = fontFeatures.map(function (s) { return s['name']; }); 240 var fontFeatureValues = fontFeatures.map(function (s) { return s['value']; }); 241 textStyle['_fontFeatureLen'] = fontFeatures.length; 242 textStyle['_fontFeatureNamesPtr'] = naiveCopyStrArray(fontFeatureNames); 243 textStyle['_fontFeatureValuesPtr'] = copy1dArray(fontFeatureValues, 'HEAPU32'); 244 } else { 245 textStyle['_fontFeatureLen'] = 0; 246 textStyle['_fontFeatureNamesPtr'] = nullptr; 247 textStyle['_fontFeatureValuesPtr'] = nullptr; 248 } 249 } 250 251 function freeArrays(textStyle) { 252 // The font family strings will get copied to a vector on the C++ side, which is owned by 253 // the text style. 254 CanvasKit._free(textStyle['_fontFamiliesPtr']); 255 CanvasKit._free(textStyle['_shadowColorsPtr']); 256 CanvasKit._free(textStyle['_shadowOffsetsPtr']); 257 CanvasKit._free(textStyle['_shadowBlurRadiiPtr']); 258 CanvasKit._free(textStyle['_fontFeatureNamesPtr']); 259 CanvasKit._free(textStyle['_fontFeatureValuesPtr']); 260 } 261 262 CanvasKit.ParagraphBuilder.Make = function(paragraphStyle, fontManager) { 263 copyArrays(paragraphStyle['textStyle']); 264 265 var result = CanvasKit.ParagraphBuilder._Make(paragraphStyle, fontManager); 266 freeArrays(paragraphStyle['textStyle']); 267 return result; 268 }; 269 270 CanvasKit.ParagraphBuilder.MakeFromFontProvider = function(paragraphStyle, fontProvider) { 271 copyArrays(paragraphStyle['textStyle']); 272 273 var result = CanvasKit.ParagraphBuilder._MakeFromFontProvider(paragraphStyle, fontProvider); 274 freeArrays(paragraphStyle['textStyle']); 275 return result; 276 }; 277 278 CanvasKit.ParagraphBuilder.ShapeText = function(text, blocks, width) { 279 let length = 0; 280 for (const b of blocks) { 281 length += b.length; 282 } 283 if (length !== text.length) { 284 throw "Accumulated block lengths must equal text.length"; 285 } 286 return CanvasKit.ParagraphBuilder._ShapeText(text, blocks, width); 287 }; 288 289 CanvasKit.ParagraphBuilder.prototype.pushStyle = function(textStyle) { 290 copyArrays(textStyle); 291 this._pushStyle(textStyle); 292 freeArrays(textStyle); 293 }; 294 295 CanvasKit.ParagraphBuilder.prototype.pushPaintStyle = function(textStyle, fg, bg) { 296 copyArrays(textStyle); 297 this._pushPaintStyle(textStyle, fg, bg); 298 freeArrays(textStyle); 299 }; 300 301 CanvasKit.ParagraphBuilder.prototype.addPlaceholder = 302 function(width, height, alignment, baseline, offset) { 303 width = width || 0; 304 height = height || 0; 305 alignment = alignment || CanvasKit.PlaceholderAlignment.Baseline; 306 baseline = baseline || CanvasKit.TextBaseline.Alphabetic; 307 offset = offset || 0; 308 this._addPlaceholder(width, height, alignment, baseline, offset); 309 }; 310}); 311}(Module)); // When this file is loaded in, the high level object is "Module"; 312