@meta2d/core 1.0.55 → 1.0.57

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (223) hide show
  1. package/index.d.ts +9 -9
  2. package/index.js +9 -9
  3. package/package.json +38 -39
  4. package/src/canvas/canvas.d.ts +456 -456
  5. package/src/canvas/canvas.js +7583 -8186
  6. package/src/canvas/canvas.js.map +1 -1
  7. package/src/canvas/canvasImage.d.ts +27 -28
  8. package/src/canvas/canvasImage.js +440 -502
  9. package/src/canvas/canvasImage.js.map +1 -1
  10. package/src/canvas/canvasTemplate.d.ts +18 -19
  11. package/src/canvas/canvasTemplate.js +208 -228
  12. package/src/canvas/canvasTemplate.js.map +1 -1
  13. package/src/canvas/index.d.ts +2 -2
  14. package/src/canvas/index.js +2 -2
  15. package/src/canvas/magnifierCanvas.d.ts +19 -20
  16. package/src/canvas/magnifierCanvas.js +101 -100
  17. package/src/canvas/magnifierCanvas.js.map +1 -1
  18. package/src/canvas/offscreen.d.ts +1 -2
  19. package/src/canvas/offscreen.js +13 -13
  20. package/src/canvas/offscreen.js.map +1 -1
  21. package/src/core.d.ts +479 -479
  22. package/src/core.js +4589 -5198
  23. package/src/core.js.map +1 -1
  24. package/src/data.d.ts +34 -34
  25. package/src/data.js +84 -84
  26. package/src/data.js.map +1 -1
  27. package/src/diagrams/arrow.d.ts +4 -4
  28. package/src/diagrams/arrow.js +46 -46
  29. package/src/diagrams/arrow.js.map +1 -1
  30. package/src/diagrams/circle.d.ts +2 -2
  31. package/src/diagrams/circle.js +8 -8
  32. package/src/diagrams/circle.js.map +1 -1
  33. package/src/diagrams/cloud.d.ts +2 -2
  34. package/src/diagrams/cloud.js +11 -11
  35. package/src/diagrams/cloud.js.map +1 -1
  36. package/src/diagrams/cube.d.ts +2 -2
  37. package/src/diagrams/cube.js +67 -69
  38. package/src/diagrams/cube.js.map +1 -1
  39. package/src/diagrams/diamond.d.ts +2 -2
  40. package/src/diagrams/diamond.js +12 -12
  41. package/src/diagrams/diamond.js.map +1 -1
  42. package/src/diagrams/file.d.ts +2 -2
  43. package/src/diagrams/file.js +17 -17
  44. package/src/diagrams/file.js.map +1 -1
  45. package/src/diagrams/gif.d.ts +5 -5
  46. package/src/diagrams/gif.js +88 -89
  47. package/src/diagrams/gif.js.map +1 -1
  48. package/src/diagrams/hexagon.d.ts +2 -2
  49. package/src/diagrams/hexagon.js +54 -54
  50. package/src/diagrams/hexagon.js.map +1 -1
  51. package/src/diagrams/iframe.d.ts +2 -2
  52. package/src/diagrams/iframe.js +337 -355
  53. package/src/diagrams/iframe.js.map +1 -1
  54. package/src/diagrams/index.d.ts +71 -71
  55. package/src/diagrams/index.js +76 -76
  56. package/src/diagrams/index.js.map +1 -1
  57. package/src/diagrams/line/arrow.d.ts +2 -2
  58. package/src/diagrams/line/arrow.js +127 -127
  59. package/src/diagrams/line/arrow.js.map +1 -1
  60. package/src/diagrams/line/curve.d.ts +16 -16
  61. package/src/diagrams/line/curve.js +226 -235
  62. package/src/diagrams/line/curve.js.map +1 -1
  63. package/src/diagrams/line/index.d.ts +5 -5
  64. package/src/diagrams/line/index.js +5 -5
  65. package/src/diagrams/line/line.d.ts +42 -42
  66. package/src/diagrams/line/line.js +369 -430
  67. package/src/diagrams/line/line.js.map +1 -1
  68. package/src/diagrams/line/polyline.d.ts +10 -10
  69. package/src/diagrams/line/polyline.js +626 -656
  70. package/src/diagrams/line/polyline.js.map +1 -1
  71. package/src/diagrams/line/smooth.d.ts +3 -3
  72. package/src/diagrams/line/smooth.js +135 -173
  73. package/src/diagrams/line/smooth.js.map +1 -1
  74. package/src/diagrams/message.d.ts +2 -2
  75. package/src/diagrams/message.js +14 -14
  76. package/src/diagrams/message.js.map +1 -1
  77. package/src/diagrams/mindLine.d.ts +3 -3
  78. package/src/diagrams/mindLine.js +29 -30
  79. package/src/diagrams/mindLine.js.map +1 -1
  80. package/src/diagrams/mindNode.d.ts +3 -3
  81. package/src/diagrams/mindNode.js +160 -188
  82. package/src/diagrams/mindNode.js.map +1 -1
  83. package/src/diagrams/panel.d.ts +2 -2
  84. package/src/diagrams/panel.js +130 -130
  85. package/src/diagrams/panel.js.map +1 -1
  86. package/src/diagrams/pentagon.d.ts +3 -3
  87. package/src/diagrams/pentagon.js +44 -45
  88. package/src/diagrams/pentagon.js.map +1 -1
  89. package/src/diagrams/pentagram.d.ts +3 -3
  90. package/src/diagrams/pentagram.js +50 -76
  91. package/src/diagrams/pentagram.js.map +1 -1
  92. package/src/diagrams/people.d.ts +2 -2
  93. package/src/diagrams/people.js +18 -18
  94. package/src/diagrams/people.js.map +1 -1
  95. package/src/diagrams/rectangle.d.ts +3 -3
  96. package/src/diagrams/rectangle.js +25 -25
  97. package/src/diagrams/rectangle.js.map +1 -1
  98. package/src/diagrams/svg/parse.d.ts +15 -15
  99. package/src/diagrams/svg/parse.js +278 -325
  100. package/src/diagrams/svg/parse.js.map +1 -1
  101. package/src/diagrams/svgPath.d.ts +2 -2
  102. package/src/diagrams/svgPath.js +28 -29
  103. package/src/diagrams/svgPath.js.map +1 -1
  104. package/src/diagrams/triangle.d.ts +3 -3
  105. package/src/diagrams/triangle.js +39 -40
  106. package/src/diagrams/triangle.js.map +1 -1
  107. package/src/diagrams/video.d.ts +5 -5
  108. package/src/diagrams/video.js +183 -184
  109. package/src/diagrams/video.js.map +1 -1
  110. package/src/dialog/dialog.d.ts +21 -21
  111. package/src/dialog/dialog.js +156 -97
  112. package/src/dialog/dialog.js.map +1 -1
  113. package/src/dialog/index.d.ts +1 -1
  114. package/src/dialog/index.js +1 -1
  115. package/src/event/event.d.ts +102 -102
  116. package/src/event/event.js +21 -21
  117. package/src/event/index.d.ts +1 -1
  118. package/src/event/index.js +1 -1
  119. package/src/map/index.d.ts +1 -1
  120. package/src/map/index.js +1 -1
  121. package/src/map/map.d.ts +21 -21
  122. package/src/map/map.js +211 -209
  123. package/src/map/map.js.map +1 -1
  124. package/src/options.d.ts +130 -130
  125. package/src/options.js +79 -79
  126. package/src/options.js.map +1 -1
  127. package/src/pen/arrow.d.ts +4 -4
  128. package/src/pen/arrow.js +187 -187
  129. package/src/pen/arrow.js.map +1 -1
  130. package/src/pen/index.d.ts +6 -6
  131. package/src/pen/index.js +6 -6
  132. package/src/pen/math.d.ts +28 -28
  133. package/src/pen/math.js +212 -303
  134. package/src/pen/math.js.map +1 -1
  135. package/src/pen/model.d.ts +514 -514
  136. package/src/pen/model.js +209 -209
  137. package/src/pen/model.js.map +1 -1
  138. package/src/pen/plugin.d.ts +5 -5
  139. package/src/pen/plugin.js +57 -87
  140. package/src/pen/plugin.js.map +1 -1
  141. package/src/pen/render.d.ts +146 -147
  142. package/src/pen/render.js +3233 -3235
  143. package/src/pen/render.js.map +1 -1
  144. package/src/pen/text.d.ts +8 -8
  145. package/src/pen/text.js +315 -374
  146. package/src/pen/text.js.map +1 -1
  147. package/src/pen/utils.d.ts +2 -2
  148. package/src/pen/utils.js +18 -40
  149. package/src/pen/utils.js.map +1 -1
  150. package/src/point/index.d.ts +1 -1
  151. package/src/point/index.js +1 -1
  152. package/src/point/point.d.ts +65 -65
  153. package/src/point/point.js +177 -178
  154. package/src/point/point.js.map +1 -1
  155. package/src/rect/index.d.ts +1 -1
  156. package/src/rect/index.js +1 -1
  157. package/src/rect/rect.d.ts +52 -52
  158. package/src/rect/rect.js +426 -485
  159. package/src/rect/rect.js.map +1 -1
  160. package/src/rect/triangle.d.ts +2 -2
  161. package/src/rect/triangle.js +9 -9
  162. package/src/rect/triangle.js.map +1 -1
  163. package/src/scroll/index.d.ts +1 -1
  164. package/src/scroll/index.js +1 -1
  165. package/src/scroll/scroll.d.ts +35 -35
  166. package/src/scroll/scroll.js +233 -220
  167. package/src/scroll/scroll.js.map +1 -1
  168. package/src/store/global.d.ts +25 -25
  169. package/src/store/global.js +17 -17
  170. package/src/store/global.js.map +1 -1
  171. package/src/store/index.d.ts +2 -2
  172. package/src/store/index.js +2 -2
  173. package/src/store/store.d.ts +228 -228
  174. package/src/store/store.js +86 -120
  175. package/src/store/store.js.map +1 -1
  176. package/src/theme.d.ts +13 -13
  177. package/src/theme.js +22 -22
  178. package/src/theme.js.map +1 -1
  179. package/src/title/index.d.ts +1 -1
  180. package/src/title/index.js +1 -1
  181. package/src/title/title.d.ts +30 -30
  182. package/src/title/title.js +98 -98
  183. package/src/title/title.js.map +1 -1
  184. package/src/tooltip/index.d.ts +1 -1
  185. package/src/tooltip/index.js +1 -1
  186. package/src/tooltip/tooltip.d.ts +40 -40
  187. package/src/tooltip/tooltip.js +171 -166
  188. package/src/tooltip/tooltip.js.map +1 -1
  189. package/src/utils/clone.d.ts +8 -8
  190. package/src/utils/clone.js +83 -87
  191. package/src/utils/clone.js.map +1 -1
  192. package/src/utils/color.d.ts +3 -3
  193. package/src/utils/color.js +109 -125
  194. package/src/utils/color.js.map +1 -1
  195. package/src/utils/error.d.ts +2 -2
  196. package/src/utils/error.js +5 -5
  197. package/src/utils/error.js.map +1 -1
  198. package/src/utils/file.d.ts +3 -3
  199. package/src/utils/file.js +39 -91
  200. package/src/utils/file.js.map +1 -1
  201. package/src/utils/index.d.ts +9 -8
  202. package/src/utils/index.js +9 -8
  203. package/src/utils/index.js.map +1 -1
  204. package/src/utils/math.d.ts +18 -18
  205. package/src/utils/math.js +113 -151
  206. package/src/utils/math.js.map +1 -1
  207. package/src/utils/object.d.ts +2 -2
  208. package/src/utils/object.js +20 -20
  209. package/src/utils/object.js.map +1 -1
  210. package/src/utils/padding.d.ts +7 -7
  211. package/src/utils/padding.js +46 -46
  212. package/src/utils/padding.js.map +1 -1
  213. package/src/utils/time.d.ts +1 -1
  214. package/src/utils/time.js +16 -16
  215. package/src/utils/time.js.map +1 -1
  216. package/src/utils/url.d.ts +4 -4
  217. package/src/utils/url.js +26 -26
  218. package/src/utils/url.js.map +1 -1
  219. package/src/utils/uuid.d.ts +4 -4
  220. package/src/utils/uuid.js +12 -12
  221. package/src/utils/browser.d.ts +0 -1
  222. package/src/utils/browser.js +0 -4
  223. package/src/utils/browser.js.map +0 -1
package/src/pen/text.js CHANGED
@@ -1,375 +1,316 @@
1
- var __values = (this && this.__values) || function(o) {
2
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
3
- if (m) return m.call(o);
4
- if (o && typeof o.length === "number") return {
5
- next: function () {
6
- if (o && i >= o.length) o = void 0;
7
- return { value: o && o[i++], done: !o };
8
- }
9
- };
10
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
11
- };
12
- var __read = (this && this.__read) || function (o, n) {
13
- var m = typeof Symbol === "function" && o[Symbol.iterator];
14
- if (!m) return o;
15
- var i = m.call(o), r, ar = [], e;
16
- try {
17
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
18
- }
19
- catch (error) { e = { error: error }; }
20
- finally {
21
- try {
22
- if (r && !r.done && (m = i["return"])) m.call(i);
23
- }
24
- finally { if (e) throw e.error; }
25
- }
26
- return ar;
27
- };
28
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
29
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
30
- if (ar || !(i in from)) {
31
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
32
- ar[i] = from[i];
33
- }
34
- }
35
- return to.concat(ar || Array.prototype.slice.call(from));
36
- };
37
- import { calcRightBottom } from '../rect';
38
- import { getFont } from './render';
39
- export function calcTextRect(pen) {
40
- var _a = pen.calculative, paddingTop = _a.paddingTop, paddingBottom = _a.paddingBottom, paddingLeft = _a.paddingLeft, paddingRight = _a.paddingRight, worldRect = _a.worldRect, canvas = _a.canvas;
41
- var _b = pen.calculative, textLeft = _b.textLeft, textTop = _b.textTop, textWidth = _b.textWidth, textHeight = _b.textHeight;
42
- var x = paddingLeft;
43
- var y = paddingTop;
44
- // if (textLeft && Math.abs(textLeft) < 1) {
45
- // textLeft *= worldRect.width;
46
- // }
47
- // if (textTop && Math.abs(textTop) < 1) {
48
- // textTop *= worldRect.height;
49
- // }
50
- var width = worldRect.width -
51
- paddingLeft -
52
- paddingRight -
53
- ((pen.textAlign === 'right' ? -textLeft : textLeft) || 0);
54
- var height = worldRect.height -
55
- paddingTop -
56
- paddingBottom -
57
- ((pen.textBaseline === 'bottom' ? -textTop : textTop) || 0);
58
- if (textWidth && textWidth < 1) {
59
- textWidth *= worldRect.width;
60
- }
61
- if (textHeight && textHeight < 1) {
62
- textHeight *= worldRect.height;
63
- }
64
- if (textWidth < pen.calculative.fontSize) {
65
- textWidth = pen.calculative.fontSize;
66
- }
67
- // 默认居左,居上
68
- x += (textLeft || 0) + worldRect.x;
69
- y += (textTop || 0) + worldRect.y;
70
- var textAlign = pen.textAlign || canvas.store.options.textAlign;
71
- var textBaseline = pen.textBaseline || canvas.store.options.textBaseline;
72
- switch (textAlign) {
73
- case 'center':
74
- x += (width - (textWidth || width)) / 2;
75
- break;
76
- case 'right':
77
- x += width - (textWidth || width);
78
- break;
79
- }
80
- switch (textBaseline) {
81
- case 'middle':
82
- y += (height - (textHeight || height)) / 2;
83
- break;
84
- case 'bottom':
85
- y += height - (textHeight || height);
86
- break;
87
- }
88
- var rect = {
89
- x: x,
90
- y: y,
91
- width: textWidth || width,
92
- height: textHeight || height,
93
- };
94
- calcRightBottom(rect);
95
- pen.calculative.worldTextRect = rect;
96
- calcTextLines(pen);
97
- pen.calculative.textDrawRect = undefined;
98
- }
99
- export function calcTextDrawRect(ctx, pen) {
100
- // By default, the text is center aligned.
101
- var lineHeight = pen.calculative.fontSize * pen.calculative.lineHeight;
102
- var h = pen.calculative.textLines.length * lineHeight;
103
- var textWidth = calcTextAdaptionWidth(ctx, pen); // 多行文本最大宽度
104
- var rect = pen.calculative.worldTextRect;
105
- var x = rect.x + (rect.width - textWidth) / 2;
106
- var y = rect.y + (rect.height - h) / 2;
107
- var options = pen.calculative.canvas.store.options;
108
- var textAlign = pen.textAlign || options.textAlign;
109
- switch (textAlign) {
110
- case 'left':
111
- x = rect.x;
112
- break;
113
- case 'right':
114
- x = rect.x + rect.width - textWidth;
115
- break;
116
- }
117
- var textBaseline = pen.textBaseline || options.textBaseline;
118
- switch (textBaseline) {
119
- case 'top':
120
- y = rect.y;
121
- break;
122
- case 'bottom':
123
- y = rect.ey - h;
124
- break;
125
- }
126
- pen.calculative.textDrawRect = {
127
- x: x,
128
- y: y,
129
- width: textWidth,
130
- height: h,
131
- };
132
- calcRightBottom(pen.calculative.textDrawRect);
133
- }
134
- export function calcTextLines(pen, text) {
135
- var e_1, _a, e_2, _b;
136
- if (text === void 0) { text = pen.calculative.text; }
137
- if (text == undefined) {
138
- pen.calculative.textLines = [];
139
- return;
140
- }
141
- text = text.toString();
142
- var lines = [];
143
- var oneRowHeight = pen.calculative.fontSize * pen.calculative.lineHeight;
144
- var textHeight = pen.calculative.worldTextRect.height;
145
- var calcRows = Math.floor(textHeight / oneRowHeight);
146
- // 最小值为 1
147
- var maxRows = calcRows > 1 ? calcRows : 1;
148
- switch (pen.whiteSpace) {
149
- case 'nowrap':
150
- if (pen.ellipsis !== false) {
151
- var allLines = wrapLines(text.split(''), pen);
152
- if (allLines[0]) {
153
- lines.push(allLines[0]);
154
- if (allLines.length > 1) {
155
- // 存在第二行,说明宽度超出
156
- setEllipsisOnLastLine(lines);
157
- }
158
- }
159
- }
160
- else {
161
- lines.push(text);
162
- }
163
- break;
164
- case 'pre-line':
165
- lines = text.split(/[\n]/g);
166
- if (pen.ellipsis !== false && lines.length > maxRows) {
167
- lines = lines.slice(0, maxRows);
168
- setEllipsisOnLastLine(lines);
169
- }
170
- break;
171
- case 'break-all':
172
- default:
173
- var paragraphs = text.split(/[\n]/g);
174
- var currentRow = 0;
175
- try {
176
- outer: for (var paragraphs_1 = __values(paragraphs), paragraphs_1_1 = paragraphs_1.next(); !paragraphs_1_1.done; paragraphs_1_1 = paragraphs_1.next()) {
177
- var paragraph = paragraphs_1_1.value;
178
- var words = pen.whiteSpace === 'break-all'
179
- ? paragraph.split('')
180
- : getWords(paragraph);
181
- var items = wrapLines(words, pen);
182
- // 空行换行的情况
183
- if (items.length === 0)
184
- items = [''];
185
- if (pen.ellipsis != false) {
186
- try {
187
- for (var items_1 = (e_2 = void 0, __values(items)), items_1_1 = items_1.next(); !items_1_1.done; items_1_1 = items_1.next()) {
188
- var l = items_1_1.value;
189
- currentRow++;
190
- if (currentRow > maxRows) {
191
- setEllipsisOnLastLine(lines);
192
- break outer;
193
- }
194
- else {
195
- lines.push(l);
196
- }
197
- }
198
- }
199
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
200
- finally {
201
- try {
202
- if (items_1_1 && !items_1_1.done && (_b = items_1.return)) _b.call(items_1);
203
- }
204
- finally { if (e_2) throw e_2.error; }
205
- }
206
- }
207
- else {
208
- lines.push.apply(lines, __spreadArray([], __read(items), false));
209
- }
210
- }
211
- }
212
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
213
- finally {
214
- try {
215
- if (paragraphs_1_1 && !paragraphs_1_1.done && (_a = paragraphs_1.return)) _a.call(paragraphs_1);
216
- }
217
- finally { if (e_1) throw e_1.error; }
218
- }
219
- break;
220
- }
221
- var keepDecimal = pen.calculative.keepDecimal;
222
- if (keepDecimal != undefined) {
223
- lines.forEach(function (text, i) {
224
- var textNum = Number(text);
225
- if (!isNaN(textNum)) {
226
- lines[i] = textNum.toFixed(keepDecimal);
227
- }
228
- });
229
- }
230
- pen.calculative.textLines = lines;
231
- return lines;
232
- }
233
- export function getWords(txt) {
234
- if (txt === void 0) { txt = ''; }
235
- var words = [];
236
- var word = '';
237
- for (var i = 0; i < txt.length; ++i) {
238
- var ch = txt.charCodeAt(i);
239
- if (ch < 33 || ch > 126) {
240
- if (word) {
241
- words.push(word);
242
- word = '';
243
- }
244
- words.push(txt[i]);
245
- }
246
- else {
247
- word += txt[i];
248
- }
249
- }
250
- if (word) {
251
- words.push(word);
252
- }
253
- return words;
254
- }
255
- export function wrapLines(words, pen) {
256
- var canvas = pen.calculative.canvas;
257
- var ctx = canvas.offscreen.getContext('2d');
258
- var _a = pen.calculative, fontStyle = _a.fontStyle, fontWeight = _a.fontWeight, fontSize = _a.fontSize, fontFamily = _a.fontFamily, lineHeight = _a.lineHeight;
259
- ctx.save();
260
- var lines = [];
261
- var currentLine = words[0] || '';
262
- for (var i = 1; i < words.length; ++i) {
263
- var word = words[i] || '';
264
- var text = currentLine + word;
265
- var currentWidth = 0;
266
- if (canvas.store.options.measureTextWidth) {
267
- ctx.font = getFont({
268
- fontStyle: fontStyle,
269
- fontWeight: fontWeight,
270
- fontFamily: fontFamily || canvas.store.options.fontFamily,
271
- fontSize: fontSize,
272
- lineHeight: lineHeight,
273
- });
274
- currentWidth = ctx.measureText(text).width;
275
- }
276
- else {
277
- // 近似计算
278
- var chinese = text.match(/[^\x00-\xff]/g) || '';
279
- var chineseWidth = chinese.length * fontSize; // 中文占用的宽度
280
- var spaces = text.match(/\s/g) || '';
281
- var spaceWidth = spaces.length * fontSize * 0.3; // 空格占用的宽度
282
- var otherWidth = (text.length - chinese.length - spaces.length) * fontSize * 0.6; // 其他字符占用的宽度
283
- currentWidth = chineseWidth + spaceWidth + otherWidth;
284
- }
285
- var textWidth = pen.calculative.worldTextRect.width;
286
- if (currentWidth <= textWidth + 0.1) {
287
- currentLine += word;
288
- }
289
- else {
290
- currentLine.length && lines.push(currentLine);
291
- currentLine = word;
292
- }
293
- }
294
- currentLine.length && lines.push(currentLine);
295
- ctx.restore();
296
- return lines;
297
- }
298
- export function calcTextAdaptionWidth(ctx, pen) {
299
- var maxWidth = 0;
300
- pen.calculative.textLineWidths = [];
301
- pen.calculative.textLines.forEach(function (text) {
302
- var width = ctx.measureText(text).width;
303
- pen.calculative.textLineWidths.push(width);
304
- maxWidth < width && (maxWidth = width);
305
- });
306
- return maxWidth;
307
- }
308
- /**
309
- * 副作用函数,会修改传入的参数
310
- * 把最后一行的最后变成 ...
311
- * TODO: 中文的三个字符宽度比 . 大,显示起来像是删多了
312
- * @param lines
313
- */
314
- function setEllipsisOnLastLine(lines) {
315
- lines[lines.length - 1] = lines[lines.length - 1].slice(0, -3) + '...';
316
- }
317
- export function calcTextAutoWidth(pen) {
318
- var arr = pen.text.split('\n');
319
- var canvas = pen.calculative.canvas;
320
- var ctx = canvas.offscreen.getContext('2d');
321
- var _a = pen.calculative, fontStyle = _a.fontStyle, fontWeight = _a.fontWeight, fontSize = _a.fontSize, fontFamily = _a.fontFamily, lineHeight = _a.lineHeight;
322
- var textWidth = 0; // pen.calculative.worldTextRect.width;
323
- var currentWidth = 0; // textWidth;
324
- ctx.save();
325
- for (var i = 0; i < arr.length; i++) {
326
- if (canvas.store.options.measureTextWidth) {
327
- ctx.font = getFont({
328
- fontStyle: fontStyle,
329
- fontWeight: fontWeight,
330
- fontFamily: fontFamily || canvas.store.options.fontFamily,
331
- fontSize: fontSize,
332
- lineHeight: lineHeight,
333
- });
334
- currentWidth = ctx.measureText(arr[i]).width; //* scale;
335
- }
336
- else {
337
- // 近似计算
338
- var chinese = arr[i].match(/[^\x00-\xff]/g) || '';
339
- var chineseWidth = chinese.length * fontSize; // 中文占用的宽度
340
- var spaces = arr[i].match(/\s/g) || '';
341
- var spaceWidth = spaces.length * fontSize * 0.3; // 空格占用的宽度
342
- var otherWidth = (arr[i].length - chinese.length - spaces.length) * fontSize * 0.6; // 其他字符占用的宽度
343
- currentWidth = chineseWidth + spaceWidth + otherWidth;
344
- }
345
- if (currentWidth > textWidth) {
346
- textWidth = currentWidth; //* scale;
347
- }
348
- }
349
- ctx.restore();
350
- var textHeight = arr.length * fontSize * lineHeight;
351
- if (pen.textAlign === 'left') {
352
- // pen.x = pen.x;
353
- }
354
- else if (pen.textAlign === 'right') {
355
- pen.x = pen.x - (textWidth - pen.width);
356
- }
357
- else {
358
- pen.x = pen.x - (textWidth - pen.width) / 2;
359
- }
360
- if (pen.textBaseline === 'top') {
361
- }
362
- else if (pen.textBaseline === 'bottom') {
363
- pen.y = pen.y - (textHeight - pen.height);
364
- }
365
- else {
366
- pen.y = pen.y - (textHeight - pen.height) / 2;
367
- }
368
- // if (textHeight > pen.height) {
369
- pen.height = textHeight + 2;
370
- // }
371
- pen.width = textWidth + 2; //误差
372
- pen.calculative.canvas.updatePenRect(pen);
373
- pen.calculative.canvas.calcActiveRect();
374
- }
1
+ import { calcRightBottom } from '../rect';
2
+ import { getFont } from './render';
3
+ export function calcTextRect(pen) {
4
+ const { paddingTop, paddingBottom, paddingLeft, paddingRight, worldRect, canvas, } = pen.calculative;
5
+ let { textLeft, textTop, textWidth, textHeight } = pen.calculative;
6
+ let x = paddingLeft;
7
+ let y = paddingTop;
8
+ // if (textLeft && Math.abs(textLeft) < 1) {
9
+ // textLeft *= worldRect.width;
10
+ // }
11
+ // if (textTop && Math.abs(textTop) < 1) {
12
+ // textTop *= worldRect.height;
13
+ // }
14
+ const width = worldRect.width -
15
+ paddingLeft -
16
+ paddingRight -
17
+ ((pen.textAlign === 'right' ? -textLeft : textLeft) || 0);
18
+ const height = worldRect.height -
19
+ paddingTop -
20
+ paddingBottom -
21
+ ((pen.textBaseline === 'bottom' ? -textTop : textTop) || 0);
22
+ if (textWidth && textWidth < 1) {
23
+ textWidth *= worldRect.width;
24
+ }
25
+ if (textHeight && textHeight < 1) {
26
+ textHeight *= worldRect.height;
27
+ }
28
+ if (textWidth < pen.calculative.fontSize) {
29
+ textWidth = pen.calculative.fontSize;
30
+ }
31
+ // 默认居左,居上
32
+ x += (textLeft || 0) + worldRect.x;
33
+ y += (textTop || 0) + worldRect.y;
34
+ const textAlign = pen.textAlign || canvas.store.options.textAlign;
35
+ const textBaseline = pen.textBaseline || canvas.store.options.textBaseline;
36
+ switch (textAlign) {
37
+ case 'center':
38
+ x += (width - (textWidth || width)) / 2;
39
+ break;
40
+ case 'right':
41
+ x += width - (textWidth || width);
42
+ break;
43
+ }
44
+ switch (textBaseline) {
45
+ case 'middle':
46
+ y += (height - (textHeight || height)) / 2;
47
+ break;
48
+ case 'bottom':
49
+ y += height - (textHeight || height);
50
+ break;
51
+ }
52
+ const rect = {
53
+ x,
54
+ y,
55
+ width: textWidth || width,
56
+ height: textHeight || height,
57
+ };
58
+ calcRightBottom(rect);
59
+ pen.calculative.worldTextRect = rect;
60
+ calcTextLines(pen);
61
+ pen.calculative.textDrawRect = undefined;
62
+ }
63
+ export function calcTextDrawRect(ctx, pen) {
64
+ // By default, the text is center aligned.
65
+ const lineHeight = pen.calculative.fontSize * pen.calculative.lineHeight;
66
+ const h = pen.calculative.textLines.length * lineHeight;
67
+ const textWidth = calcTextAdaptionWidth(ctx, pen); // 多行文本最大宽度
68
+ const rect = pen.calculative.worldTextRect;
69
+ let x = rect.x + (rect.width - textWidth) / 2;
70
+ let y = rect.y + (rect.height - h) / 2;
71
+ const options = pen.calculative.canvas.store.options;
72
+ const textAlign = pen.textAlign || options.textAlign;
73
+ switch (textAlign) {
74
+ case 'left':
75
+ x = rect.x;
76
+ break;
77
+ case 'right':
78
+ x = rect.x + rect.width - textWidth;
79
+ break;
80
+ }
81
+ const textBaseline = pen.textBaseline || options.textBaseline;
82
+ switch (textBaseline) {
83
+ case 'top':
84
+ y = rect.y;
85
+ break;
86
+ case 'bottom':
87
+ y = rect.ey - h;
88
+ break;
89
+ }
90
+ pen.calculative.textDrawRect = {
91
+ x,
92
+ y,
93
+ width: textWidth,
94
+ height: h,
95
+ };
96
+ calcRightBottom(pen.calculative.textDrawRect);
97
+ }
98
+ export function calcTextLines(pen, text = pen.calculative.text) {
99
+ if (text == undefined) {
100
+ pen.calculative.textLines = [];
101
+ return;
102
+ }
103
+ text = text.toString();
104
+ let lines = [];
105
+ const oneRowHeight = pen.calculative.fontSize * pen.calculative.lineHeight;
106
+ const textHeight = pen.calculative.worldTextRect.height;
107
+ const calcRows = Math.floor(textHeight / oneRowHeight);
108
+ // 最小值为 1
109
+ const maxRows = calcRows > 1 ? calcRows : 1;
110
+ switch (pen.whiteSpace) {
111
+ case 'nowrap':
112
+ if (pen.ellipsis !== false) {
113
+ const allLines = wrapLines(text.split(''), pen);
114
+ if (allLines[0]) {
115
+ lines.push(allLines[0]);
116
+ if (allLines.length > 1) {
117
+ // 存在第二行,说明宽度超出
118
+ setEllipsisOnLastLine(lines);
119
+ }
120
+ }
121
+ }
122
+ else {
123
+ lines.push(text);
124
+ }
125
+ break;
126
+ case 'pre-line':
127
+ lines = text.split(/[\n]/g);
128
+ if (pen.ellipsis !== false && lines.length > maxRows) {
129
+ lines = lines.slice(0, maxRows);
130
+ setEllipsisOnLastLine(lines);
131
+ }
132
+ break;
133
+ case 'break-all':
134
+ default:
135
+ const paragraphs = text.split(/[\n]/g);
136
+ let currentRow = 0;
137
+ outer: for (const paragraph of paragraphs) {
138
+ const words = pen.whiteSpace === 'break-all'
139
+ ? paragraph.split('')
140
+ : getWords(paragraph);
141
+ let items = wrapLines(words, pen);
142
+ // 空行换行的情况
143
+ if (items.length === 0)
144
+ items = [''];
145
+ if (pen.ellipsis != false) {
146
+ for (const l of items) {
147
+ currentRow++;
148
+ if (currentRow > maxRows) {
149
+ setEllipsisOnLastLine(lines);
150
+ break outer;
151
+ }
152
+ else {
153
+ lines.push(l);
154
+ }
155
+ }
156
+ }
157
+ else {
158
+ lines.push(...items);
159
+ }
160
+ }
161
+ break;
162
+ }
163
+ const keepDecimal = pen.calculative.keepDecimal;
164
+ if (keepDecimal != undefined) {
165
+ lines.forEach((text, i) => {
166
+ const textNum = Number(text);
167
+ if (!isNaN(textNum)) {
168
+ lines[i] = textNum.toFixed(keepDecimal);
169
+ }
170
+ });
171
+ }
172
+ pen.calculative.textLines = lines;
173
+ return lines;
174
+ }
175
+ export function getWords(txt = '') {
176
+ const words = [];
177
+ let word = '';
178
+ for (let i = 0; i < txt.length; ++i) {
179
+ const ch = txt.charCodeAt(i);
180
+ if (ch < 33 || ch > 126) {
181
+ if (word) {
182
+ words.push(word);
183
+ word = '';
184
+ }
185
+ words.push(txt[i]);
186
+ }
187
+ else {
188
+ word += txt[i];
189
+ }
190
+ }
191
+ if (word) {
192
+ words.push(word);
193
+ }
194
+ return words;
195
+ }
196
+ export function wrapLines(words, pen) {
197
+ const canvas = pen.calculative.canvas;
198
+ const ctx = canvas.offscreen.getContext('2d');
199
+ const { fontStyle, fontWeight, fontSize, fontFamily, lineHeight } = pen.calculative;
200
+ ctx.save();
201
+ const lines = [];
202
+ let currentLine = words[0] || '';
203
+ for (let i = 1; i < words.length; ++i) {
204
+ const word = words[i] || '';
205
+ const text = currentLine + word;
206
+ let currentWidth = 0;
207
+ if (canvas.store.options.measureTextWidth) {
208
+ ctx.font = getFont({
209
+ fontStyle,
210
+ fontWeight,
211
+ fontFamily: fontFamily || canvas.store.options.fontFamily,
212
+ fontSize,
213
+ lineHeight,
214
+ });
215
+ currentWidth = ctx.measureText(text).width;
216
+ }
217
+ else {
218
+ // 近似计算
219
+ const chinese = text.match(/[^\x00-\xff]/g) || '';
220
+ const chineseWidth = chinese.length * fontSize; // 中文占用的宽度
221
+ const spaces = text.match(/\s/g) || '';
222
+ const spaceWidth = spaces.length * fontSize * 0.3; // 空格占用的宽度
223
+ const otherWidth = (text.length - chinese.length - spaces.length) * fontSize * 0.6; // 其他字符占用的宽度
224
+ currentWidth = chineseWidth + spaceWidth + otherWidth;
225
+ }
226
+ const textWidth = pen.calculative.worldTextRect.width;
227
+ if (currentWidth <= textWidth + 0.1) {
228
+ currentLine += word;
229
+ }
230
+ else {
231
+ currentLine.length && lines.push(currentLine);
232
+ currentLine = word;
233
+ }
234
+ }
235
+ currentLine.length && lines.push(currentLine);
236
+ ctx.restore();
237
+ return lines;
238
+ }
239
+ export function calcTextAdaptionWidth(ctx, pen) {
240
+ let maxWidth = 0;
241
+ pen.calculative.textLineWidths = [];
242
+ pen.calculative.textLines.forEach((text) => {
243
+ const width = ctx.measureText(text).width;
244
+ pen.calculative.textLineWidths.push(width);
245
+ maxWidth < width && (maxWidth = width);
246
+ });
247
+ return maxWidth;
248
+ }
249
+ /**
250
+ * 副作用函数,会修改传入的参数
251
+ * 把最后一行的最后变成 ...
252
+ * TODO: 中文的三个字符宽度比 . 大,显示起来像是删多了
253
+ * @param lines
254
+ */
255
+ function setEllipsisOnLastLine(lines) {
256
+ lines[lines.length - 1] = lines[lines.length - 1].slice(0, -3) + '...';
257
+ }
258
+ export function calcTextAutoWidth(pen) {
259
+ let arr = pen.text.split('\n');
260
+ const canvas = pen.calculative.canvas;
261
+ const ctx = canvas.offscreen.getContext('2d');
262
+ const { fontStyle, fontWeight, fontSize, fontFamily, lineHeight } = pen.calculative;
263
+ let textWidth = 0; // pen.calculative.worldTextRect.width;
264
+ let currentWidth = 0; // textWidth;
265
+ ctx.save();
266
+ for (let i = 0; i < arr.length; i++) {
267
+ if (canvas.store.options.measureTextWidth) {
268
+ ctx.font = getFont({
269
+ fontStyle,
270
+ fontWeight,
271
+ fontFamily: fontFamily || canvas.store.options.fontFamily,
272
+ fontSize,
273
+ lineHeight,
274
+ });
275
+ currentWidth = ctx.measureText(arr[i]).width; //* scale;
276
+ }
277
+ else {
278
+ // 近似计算
279
+ const chinese = arr[i].match(/[^\x00-\xff]/g) || '';
280
+ const chineseWidth = chinese.length * fontSize; // 中文占用的宽度
281
+ const spaces = arr[i].match(/\s/g) || '';
282
+ const spaceWidth = spaces.length * fontSize * 0.3; // 空格占用的宽度
283
+ const otherWidth = (arr[i].length - chinese.length - spaces.length) * fontSize * 0.6; // 其他字符占用的宽度
284
+ currentWidth = chineseWidth + spaceWidth + otherWidth;
285
+ }
286
+ if (currentWidth > textWidth) {
287
+ textWidth = currentWidth; //* scale;
288
+ }
289
+ }
290
+ ctx.restore();
291
+ let textHeight = arr.length * fontSize * lineHeight;
292
+ if (pen.textAlign === 'left') {
293
+ // pen.x = pen.x;
294
+ }
295
+ else if (pen.textAlign === 'right') {
296
+ pen.x = pen.x - (textWidth - pen.width);
297
+ }
298
+ else {
299
+ pen.x = pen.x - (textWidth - pen.width) / 2;
300
+ }
301
+ if (pen.textBaseline === 'top') {
302
+ }
303
+ else if (pen.textBaseline === 'bottom') {
304
+ pen.y = pen.y - (textHeight - pen.height);
305
+ }
306
+ else {
307
+ pen.y = pen.y - (textHeight - pen.height) / 2;
308
+ }
309
+ // if (textHeight > pen.height) {
310
+ pen.height = textHeight + 2;
311
+ // }
312
+ pen.width = textWidth + 2; //误差
313
+ pen.calculative.canvas.updatePenRect(pen);
314
+ pen.calculative.canvas.calcActiveRect();
315
+ }
375
316
  //# sourceMappingURL=text.js.map