blob: 2a8aa4b84852450a9861843d14537ef56f44d7db [file] [log] [blame]
Kevin Lubick369f6a52019-10-03 11:22:08 -04001(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
12 if (!floatArray || !floatArray.length) {
13 return [];
14 }
15 var ret = [];
16 for (var i = 0; i < floatArray.length; i+=4) {
17 ret.push(CanvasKit.LTRBRect(floatArray[i], floatArray[i+1], floatArray[i+2], floatArray[i+3]))
18 }
19 CanvasKit._free(floatArray.byteOffset);
20 return ret;
21 }
22
23 // These helpers fill out all fields, because emscripten complains if we
24 // have undefined and it expects, for example, a float.
25 CanvasKit.ParagraphStyle = function(s) {
26 // Use [''] to tell closure not to minify the names
27 s['heightMultiplier'] = s['heightMultiplier'] || 0;
28 s['maxLines'] = s['maxLines'] || 0;
29 s['textAlign'] = s['textAlign'] || CanvasKit.TextAlign.Start;
30 s['textStyle'] = CanvasKit.TextStyle(s['textStyle']);
31 return s;
32 }
33
34 CanvasKit.TextStyle = function(s) {
35 // Use [''] to tell closure not to minify the names
36 s['backgroundColor'] = s['backgroundColor'] || 0;
37 s['color'] = s['color'] || 0;
38 s['decoration'] = s['decoration'] || 0;
39 s['decorationThickness'] = s['decorationThickness'] || 0;
40 s['fontSize'] = s['fontSize'] || 0;
41 if (Array.isArray(s['fontFamilies']) && s['fontFamilies'].length) {
42 var sPtr = naiveCopyStrArray(s['fontFamilies']);
43 s['_fontFamilies'] = sPtr;
44 s['_numFontFamilies'] = s['fontFamilies'].length;
45 } else {
46 s['_fontFamilies'] = nullptr;
47 s['_numFontFamilies'] = 0;
48 }
49 s['foregroundColor'] = s['foregroundColor'] || 0;
50 return s;
51 }
52
53 // returns a pointer to a place on the heap that has an array
54 // of char* (effectively a char**). For now, this does the naive thing
55 // and depends on the string being null-terminated. This should be used
56 // for simple, well-formed things (e.g. font-families), not arbitrary
57 // text that should be drawn. If we need this to handle more complex
58 // strings, it should return two pointers, a pointer of the
59 // string array and a pointer to an array of the strings byte lengths.
60 function naiveCopyStrArray(strings) {
61 if (!strings || !strings.length) {
62 return nullptr;
63 }
64 var sPtrs = [];
65 for (var i = 0; i < strings.length; i++) {
66 var str = strings[i];
67 // Add 1 for null terminator, which we need when copying/converting
68 var strLen = lengthBytesUTF8(str) + 1;
69 var strPtr = CanvasKit._malloc(strLen);
70 stringToUTF8(str, strPtr, strLen);
71 sPtrs.push(strPtr);
72 }
73 return copy1dArray(sPtrs, CanvasKit.HEAPU32);
74 }
75});
76}(Module)); // When this file is loaded in, the high level object is "Module";