blob: f34273067822caa8af38fadfb123eb1438280dbe [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
Kevin Lubickd3b1fe62019-10-21 10:50:26 -040027 // TODO(kjlubick): strutStyle
28 s['disableHinting'] = s['disableHinting'] || false;
29 if (s['ellipsis']) {
30 var str = s['ellipsis'];
31 var strLen = lengthBytesUTF8(str) + 1;
32 var strPtr = CanvasKit._malloc(strLen);
33 stringToUTF8(str, strPtr, strLen);
34 s['_ellipsisPtr'] = strPtr;
35 s['_ellipsisLen'] = strLen;
36 } else {
37 s['_ellipsisPtr'] = nullptr;
38 s['_ellipsisLen'] = 0;
39 }
40
Kevin Lubick369f6a52019-10-03 11:22:08 -040041 s['heightMultiplier'] = s['heightMultiplier'] || 0;
42 s['maxLines'] = s['maxLines'] || 0;
43 s['textAlign'] = s['textAlign'] || CanvasKit.TextAlign.Start;
Kevin Lubickd3b1fe62019-10-21 10:50:26 -040044 s['textDirection'] = s['textDirection'] || CanvasKit.TextDirection.LTR;
Kevin Lubick369f6a52019-10-03 11:22:08 -040045 s['textStyle'] = CanvasKit.TextStyle(s['textStyle']);
46 return s;
47 }
48
Kevin Lubickd3b1fe62019-10-21 10:50:26 -040049 function fontStyle(s) {
50 s = s || {};
51 // Can't check for falsey as 0 width means "invisible".
52 if (s['weight'] === undefined) {
53 s['weight'] = CanvasKit.FontWeight.Normal;
54 }
55 s['width'] = s['width'] || CanvasKit.FontWidth.Normal;
56 s['slant'] = s['slant'] || CanvasKit.FontSlant.Upright;
57 return s;
58 }
59
Kevin Lubick369f6a52019-10-03 11:22:08 -040060 CanvasKit.TextStyle = function(s) {
61 // Use [''] to tell closure not to minify the names
62 s['backgroundColor'] = s['backgroundColor'] || 0;
Kevin Lubickd3b1fe62019-10-21 10:50:26 -040063 // Can't check for falsey as 0 is "white".
64 if (s['color'] === undefined) {
65 s['color'] = CanvasKit.BLACK;
66 }
Kevin Lubick369f6a52019-10-03 11:22:08 -040067 s['decoration'] = s['decoration'] || 0;
68 s['decorationThickness'] = s['decorationThickness'] || 0;
69 s['fontSize'] = s['fontSize'] || 0;
70 if (Array.isArray(s['fontFamilies']) && s['fontFamilies'].length) {
71 var sPtr = naiveCopyStrArray(s['fontFamilies']);
72 s['_fontFamilies'] = sPtr;
73 s['_numFontFamilies'] = s['fontFamilies'].length;
74 } else {
75 s['_fontFamilies'] = nullptr;
76 s['_numFontFamilies'] = 0;
Kevin Lubickd3b1fe62019-10-21 10:50:26 -040077 SkDebug("no font families provided, text may draw wrong or not at all")
Kevin Lubick369f6a52019-10-03 11:22:08 -040078 }
Kevin Lubickd3b1fe62019-10-21 10:50:26 -040079 s['fontStyle'] = fontStyle(s['fontStyle']);
Kevin Lubick369f6a52019-10-03 11:22:08 -040080 s['foregroundColor'] = s['foregroundColor'] || 0;
81 return s;
82 }
83
84 // returns a pointer to a place on the heap that has an array
85 // of char* (effectively a char**). For now, this does the naive thing
86 // and depends on the string being null-terminated. This should be used
87 // for simple, well-formed things (e.g. font-families), not arbitrary
88 // text that should be drawn. If we need this to handle more complex
89 // strings, it should return two pointers, a pointer of the
90 // string array and a pointer to an array of the strings byte lengths.
91 function naiveCopyStrArray(strings) {
92 if (!strings || !strings.length) {
93 return nullptr;
94 }
95 var sPtrs = [];
96 for (var i = 0; i < strings.length; i++) {
97 var str = strings[i];
98 // Add 1 for null terminator, which we need when copying/converting
99 var strLen = lengthBytesUTF8(str) + 1;
100 var strPtr = CanvasKit._malloc(strLen);
101 stringToUTF8(str, strPtr, strLen);
102 sPtrs.push(strPtr);
103 }
104 return copy1dArray(sPtrs, CanvasKit.HEAPU32);
105 }
106});
107}(Module)); // When this file is loaded in, the high level object is "Module";