blob: d50edfd877e426ab0a6cd83d73762543e836ee47 [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 = [];
Kevin Lubick4a5f4f22019-11-20 08:27:10 -050016 for (var i = 0; i < floatArray.length; i+=5) {
17 var r = CanvasKit.LTRBRect(floatArray[i], floatArray[i+1], floatArray[i+2], floatArray[i+3]);
18 if (floatArray[i+4] === 1) {
19 r['direction'] = CanvasKit.TextDirection.RTL;
20 } else {
21 r['direction'] = CanvasKit.TextDirection.LTR;
22 }
23 ret.push(r);
Kevin Lubick369f6a52019-10-03 11:22:08 -040024 }
25 CanvasKit._free(floatArray.byteOffset);
26 return ret;
27 }
28
29 // These helpers fill out all fields, because emscripten complains if we
30 // have undefined and it expects, for example, a float.
31 CanvasKit.ParagraphStyle = function(s) {
32 // Use [''] to tell closure not to minify the names
Kevin Lubickd3b1fe62019-10-21 10:50:26 -040033 // TODO(kjlubick): strutStyle
34 s['disableHinting'] = s['disableHinting'] || false;
35 if (s['ellipsis']) {
36 var str = s['ellipsis'];
37 var strLen = lengthBytesUTF8(str) + 1;
38 var strPtr = CanvasKit._malloc(strLen);
39 stringToUTF8(str, strPtr, strLen);
40 s['_ellipsisPtr'] = strPtr;
41 s['_ellipsisLen'] = strLen;
42 } else {
43 s['_ellipsisPtr'] = nullptr;
44 s['_ellipsisLen'] = 0;
45 }
46
Kevin Lubick369f6a52019-10-03 11:22:08 -040047 s['heightMultiplier'] = s['heightMultiplier'] || 0;
48 s['maxLines'] = s['maxLines'] || 0;
49 s['textAlign'] = s['textAlign'] || CanvasKit.TextAlign.Start;
Kevin Lubickd3b1fe62019-10-21 10:50:26 -040050 s['textDirection'] = s['textDirection'] || CanvasKit.TextDirection.LTR;
Kevin Lubick369f6a52019-10-03 11:22:08 -040051 s['textStyle'] = CanvasKit.TextStyle(s['textStyle']);
52 return s;
53 }
54
Kevin Lubickd3b1fe62019-10-21 10:50:26 -040055 function fontStyle(s) {
56 s = s || {};
57 // Can't check for falsey as 0 width means "invisible".
58 if (s['weight'] === undefined) {
59 s['weight'] = CanvasKit.FontWeight.Normal;
60 }
61 s['width'] = s['width'] || CanvasKit.FontWidth.Normal;
62 s['slant'] = s['slant'] || CanvasKit.FontSlant.Upright;
63 return s;
64 }
65
Kevin Lubick369f6a52019-10-03 11:22:08 -040066 CanvasKit.TextStyle = function(s) {
67 // Use [''] to tell closure not to minify the names
Nathaniel Nifonge5d32542020-03-26 09:27:48 -040068 if (!isCanvasKitColor(s['color'])) {
Kevin Lubickd3b1fe62019-10-21 10:50:26 -040069 s['color'] = CanvasKit.BLACK;
70 }
Nathaniel Nifonge5d32542020-03-26 09:27:48 -040071 s['foregroundColor'] = s['foregroundColor'] || CanvasKit.TRANSPARENT;
72 s['backgroundColor'] = s['backgroundColor'] || CanvasKit.TRANSPARENT;
Kevin Lubick369f6a52019-10-03 11:22:08 -040073 s['decoration'] = s['decoration'] || 0;
74 s['decorationThickness'] = s['decorationThickness'] || 0;
75 s['fontSize'] = s['fontSize'] || 0;
76 if (Array.isArray(s['fontFamilies']) && s['fontFamilies'].length) {
77 var sPtr = naiveCopyStrArray(s['fontFamilies']);
78 s['_fontFamilies'] = sPtr;
79 s['_numFontFamilies'] = s['fontFamilies'].length;
80 } else {
81 s['_fontFamilies'] = nullptr;
82 s['_numFontFamilies'] = 0;
Kevin Lubickd3b1fe62019-10-21 10:50:26 -040083 SkDebug("no font families provided, text may draw wrong or not at all")
Kevin Lubick369f6a52019-10-03 11:22:08 -040084 }
Kevin Lubickd3b1fe62019-10-21 10:50:26 -040085 s['fontStyle'] = fontStyle(s['fontStyle']);
Kevin Lubick369f6a52019-10-03 11:22:08 -040086 return s;
87 }
88
89 // returns a pointer to a place on the heap that has an array
90 // of char* (effectively a char**). For now, this does the naive thing
91 // and depends on the string being null-terminated. This should be used
92 // for simple, well-formed things (e.g. font-families), not arbitrary
93 // text that should be drawn. If we need this to handle more complex
94 // strings, it should return two pointers, a pointer of the
95 // string array and a pointer to an array of the strings byte lengths.
96 function naiveCopyStrArray(strings) {
97 if (!strings || !strings.length) {
98 return nullptr;
99 }
100 var sPtrs = [];
101 for (var i = 0; i < strings.length; i++) {
102 var str = strings[i];
103 // Add 1 for null terminator, which we need when copying/converting
104 var strLen = lengthBytesUTF8(str) + 1;
105 var strPtr = CanvasKit._malloc(strLen);
106 stringToUTF8(str, strPtr, strLen);
107 sPtrs.push(strPtr);
108 }
109 return copy1dArray(sPtrs, CanvasKit.HEAPU32);
110 }
111});
112}(Module)); // When this file is loaded in, the high level object is "Module";