konva 10.0.10 → 10.0.12
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.
- package/konva.js +20 -11
- package/konva.min.js +3 -3
- package/lib/Animation.d.ts +24 -0
- package/lib/Animation.js +142 -0
- package/lib/BezierFunctions.d.ts +6 -0
- package/lib/BezierFunctions.js +784 -0
- package/lib/Canvas.d.ts +33 -0
- package/lib/Canvas.js +107 -0
- package/lib/Container.d.ts +54 -0
- package/lib/Container.js +316 -0
- package/lib/Context.d.ts +95 -0
- package/lib/Context.js +561 -0
- package/lib/Core.d.ts +3 -0
- package/lib/Core.js +3 -0
- package/lib/DragAndDrop.d.ts +17 -0
- package/lib/DragAndDrop.js +110 -0
- package/lib/Factory.d.ts +22 -0
- package/lib/Factory.js +123 -0
- package/lib/FastLayer.d.ts +4 -0
- package/lib/FastLayer.js +12 -0
- package/lib/Global.d.ts +37 -0
- package/lib/Global.js +66 -0
- package/lib/Group.d.ts +9 -0
- package/lib/Group.js +13 -0
- package/lib/Layer.d.ts +62 -0
- package/lib/Layer.js +305 -0
- package/lib/Node.d.ts +323 -0
- package/lib/Node.js +1532 -0
- package/lib/PointerEvents.d.ts +11 -0
- package/lib/PointerEvents.js +37 -0
- package/lib/Shape.d.ts +196 -0
- package/lib/Shape.js +555 -0
- package/lib/Stage.d.ts +86 -0
- package/lib/Stage.js +715 -0
- package/lib/Tween.d.ts +90 -0
- package/lib/Tween.js +531 -0
- package/lib/Util.d.ts +137 -0
- package/lib/Util.js +918 -0
- package/lib/Validators.d.ts +11 -0
- package/lib/Validators.js +178 -0
- package/lib/_CoreInternals.d.ts +224 -0
- package/lib/_CoreInternals.js +34 -0
- package/lib/_FullInternals.d.ts +306 -0
- package/lib/_FullInternals.js +80 -0
- package/lib/canvas-backend.d.ts +2 -0
- package/lib/canvas-backend.js +27 -0
- package/lib/filters/Blur.d.ts +2 -0
- package/lib/filters/Blur.js +235 -0
- package/lib/filters/Brighten.d.ts +2 -0
- package/lib/filters/Brighten.js +12 -0
- package/lib/filters/Brightness.d.ts +2 -0
- package/lib/filters/Brightness.js +8 -0
- package/lib/filters/Contrast.d.ts +2 -0
- package/lib/filters/Contrast.js +35 -0
- package/lib/filters/Emboss.d.ts +2 -0
- package/lib/filters/Emboss.js +101 -0
- package/lib/filters/Enhance.d.ts +2 -0
- package/lib/filters/Enhance.js +84 -0
- package/lib/filters/Grayscale.d.ts +2 -0
- package/lib/filters/Grayscale.js +9 -0
- package/lib/filters/HSL.d.ts +2 -0
- package/lib/filters/HSL.js +24 -0
- package/lib/filters/HSV.d.ts +2 -0
- package/lib/filters/HSV.js +23 -0
- package/lib/filters/Invert.d.ts +2 -0
- package/lib/filters/Invert.js +8 -0
- package/lib/filters/Kaleidoscope.d.ts +2 -0
- package/lib/filters/Kaleidoscope.js +133 -0
- package/lib/filters/Mask.d.ts +2 -0
- package/lib/filters/Mask.js +151 -0
- package/lib/filters/Noise.d.ts +2 -0
- package/lib/filters/Noise.js +12 -0
- package/lib/filters/Pixelate.d.ts +2 -0
- package/lib/filters/Pixelate.js +60 -0
- package/lib/filters/Posterize.d.ts +2 -0
- package/lib/filters/Posterize.js +10 -0
- package/lib/filters/RGB.d.ts +2 -0
- package/lib/filters/RGB.js +38 -0
- package/lib/filters/RGBA.d.ts +2 -0
- package/lib/filters/RGBA.js +49 -0
- package/lib/filters/Sepia.d.ts +2 -0
- package/lib/filters/Sepia.js +11 -0
- package/lib/filters/Solarize.d.ts +2 -0
- package/lib/filters/Solarize.js +14 -0
- package/lib/filters/Threshold.d.ts +2 -0
- package/lib/filters/Threshold.js +10 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +2 -0
- package/lib/shapes/Arc.d.ts +27 -0
- package/lib/shapes/Arc.js +60 -0
- package/lib/shapes/Arrow.d.ts +27 -0
- package/lib/shapes/Arrow.js +99 -0
- package/lib/shapes/Circle.d.ts +15 -0
- package/lib/shapes/Circle.js +33 -0
- package/lib/shapes/Ellipse.d.ts +18 -0
- package/lib/shapes/Ellipse.js +37 -0
- package/lib/shapes/Image.d.ts +29 -0
- package/lib/shapes/Image.js +131 -0
- package/lib/shapes/Label.d.ts +39 -0
- package/lib/shapes/Label.js +173 -0
- package/lib/shapes/Line.d.ts +29 -0
- package/lib/shapes/Line.js +156 -0
- package/lib/shapes/Path.d.ts +50 -0
- package/lib/shapes/Path.js +670 -0
- package/lib/shapes/Rect.d.ts +11 -0
- package/lib/shapes/Rect.js +22 -0
- package/lib/shapes/RegularPolygon.d.ts +26 -0
- package/lib/shapes/RegularPolygon.js +72 -0
- package/lib/shapes/Ring.d.ts +17 -0
- package/lib/shapes/Ring.js +33 -0
- package/lib/shapes/Sprite.d.ts +32 -0
- package/lib/shapes/Sprite.js +108 -0
- package/lib/shapes/Star.d.ts +19 -0
- package/lib/shapes/Star.js +38 -0
- package/lib/shapes/Text.d.ts +90 -0
- package/lib/shapes/Text.js +461 -0
- package/lib/shapes/TextPath.d.ts +72 -0
- package/lib/shapes/TextPath.js +268 -0
- package/lib/shapes/Transformer.d.ts +130 -0
- package/lib/shapes/Transformer.js +1009 -0
- package/lib/shapes/Wedge.d.ts +19 -0
- package/lib/shapes/Wedge.js +38 -0
- package/lib/skia-backend.d.ts +2 -0
- package/lib/skia-backend.js +24 -0
- package/lib/types.d.ts +56 -0
- package/lib/types.js +21 -0
- package/package.json +1 -1
|
@@ -0,0 +1,461 @@
|
|
|
1
|
+
import { Util } from "../Util.js";
|
|
2
|
+
import { Factory } from "../Factory.js";
|
|
3
|
+
import { Shape } from "../Shape.js";
|
|
4
|
+
import { Konva } from "../Global.js";
|
|
5
|
+
import { getNumberValidator, getStringValidator, getNumberOrAutoValidator, getBooleanValidator, } from "../Validators.js";
|
|
6
|
+
import { _registerNode } from "../Global.js";
|
|
7
|
+
export function stringToArray(string) {
|
|
8
|
+
return [...string].reduce((acc, char, index, array) => {
|
|
9
|
+
if (/\p{Emoji}/u.test(char)) {
|
|
10
|
+
const nextChar = array[index + 1];
|
|
11
|
+
if (nextChar && /\p{Emoji_Modifier}|\u200D/u.test(nextChar)) {
|
|
12
|
+
acc.push(char + nextChar);
|
|
13
|
+
array[index + 1] = '';
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
acc.push(char);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
else if (/\p{Regional_Indicator}{2}/u.test(char + (array[index + 1] || ''))) {
|
|
20
|
+
acc.push(char + array[index + 1]);
|
|
21
|
+
}
|
|
22
|
+
else if (index > 0 && /\p{Mn}|\p{Me}|\p{Mc}/u.test(char)) {
|
|
23
|
+
acc[acc.length - 1] += char;
|
|
24
|
+
}
|
|
25
|
+
else if (char) {
|
|
26
|
+
acc.push(char);
|
|
27
|
+
}
|
|
28
|
+
return acc;
|
|
29
|
+
}, []);
|
|
30
|
+
}
|
|
31
|
+
const AUTO = 'auto', CENTER = 'center', INHERIT = 'inherit', JUSTIFY = 'justify', CHANGE_KONVA = 'Change.konva', CONTEXT_2D = '2d', DASH = '-', LEFT = 'left', TEXT = 'text', TEXT_UPPER = 'Text', TOP = 'top', BOTTOM = 'bottom', MIDDLE = 'middle', NORMAL = 'normal', PX_SPACE = 'px ', SPACE = ' ', RIGHT = 'right', RTL = 'rtl', WORD = 'word', CHAR = 'char', NONE = 'none', ELLIPSIS = '…', ATTR_CHANGE_LIST = [
|
|
32
|
+
'direction',
|
|
33
|
+
'fontFamily',
|
|
34
|
+
'fontSize',
|
|
35
|
+
'fontStyle',
|
|
36
|
+
'fontVariant',
|
|
37
|
+
'padding',
|
|
38
|
+
'align',
|
|
39
|
+
'verticalAlign',
|
|
40
|
+
'lineHeight',
|
|
41
|
+
'text',
|
|
42
|
+
'width',
|
|
43
|
+
'height',
|
|
44
|
+
'wrap',
|
|
45
|
+
'ellipsis',
|
|
46
|
+
'letterSpacing',
|
|
47
|
+
], attrChangeListLen = ATTR_CHANGE_LIST.length;
|
|
48
|
+
function normalizeFontFamily(fontFamily) {
|
|
49
|
+
return fontFamily
|
|
50
|
+
.split(',')
|
|
51
|
+
.map((family) => {
|
|
52
|
+
family = family.trim();
|
|
53
|
+
const hasSpace = family.indexOf(' ') >= 0;
|
|
54
|
+
const hasQuotes = family.indexOf('"') >= 0 || family.indexOf("'") >= 0;
|
|
55
|
+
if (hasSpace && !hasQuotes) {
|
|
56
|
+
family = `"${family}"`;
|
|
57
|
+
}
|
|
58
|
+
return family;
|
|
59
|
+
})
|
|
60
|
+
.join(', ');
|
|
61
|
+
}
|
|
62
|
+
let dummyContext;
|
|
63
|
+
function getDummyContext() {
|
|
64
|
+
if (dummyContext) {
|
|
65
|
+
return dummyContext;
|
|
66
|
+
}
|
|
67
|
+
dummyContext = Util.createCanvasElement().getContext(CONTEXT_2D);
|
|
68
|
+
return dummyContext;
|
|
69
|
+
}
|
|
70
|
+
function _fillFunc(context) {
|
|
71
|
+
context.fillText(this._partialText, this._partialTextX, this._partialTextY);
|
|
72
|
+
}
|
|
73
|
+
function _strokeFunc(context) {
|
|
74
|
+
context.setAttr('miterLimit', 2);
|
|
75
|
+
context.strokeText(this._partialText, this._partialTextX, this._partialTextY);
|
|
76
|
+
}
|
|
77
|
+
function checkDefaultFill(config) {
|
|
78
|
+
config = config || {};
|
|
79
|
+
if (!config.fillLinearGradientColorStops &&
|
|
80
|
+
!config.fillRadialGradientColorStops &&
|
|
81
|
+
!config.fillPatternImage) {
|
|
82
|
+
config.fill = config.fill || 'black';
|
|
83
|
+
}
|
|
84
|
+
return config;
|
|
85
|
+
}
|
|
86
|
+
export class Text extends Shape {
|
|
87
|
+
constructor(config) {
|
|
88
|
+
super(checkDefaultFill(config));
|
|
89
|
+
this._partialTextX = 0;
|
|
90
|
+
this._partialTextY = 0;
|
|
91
|
+
for (let n = 0; n < attrChangeListLen; n++) {
|
|
92
|
+
this.on(ATTR_CHANGE_LIST[n] + CHANGE_KONVA, this._setTextData);
|
|
93
|
+
}
|
|
94
|
+
this._setTextData();
|
|
95
|
+
}
|
|
96
|
+
_sceneFunc(context) {
|
|
97
|
+
var _a, _b;
|
|
98
|
+
const textArr = this.textArr, textArrLen = textArr.length;
|
|
99
|
+
if (!this.text()) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
let padding = this.padding(), fontSize = this.fontSize(), lineHeightPx = this.lineHeight() * fontSize, verticalAlign = this.verticalAlign(), direction = this.direction(), alignY = 0, align = this.align(), totalWidth = this.getWidth(), letterSpacing = this.letterSpacing(), charRenderFunc = this.charRenderFunc(), fill = this.fill(), textDecoration = this.textDecoration(), shouldUnderline = textDecoration.indexOf('underline') !== -1, shouldLineThrough = textDecoration.indexOf('line-through') !== -1, n;
|
|
103
|
+
direction = direction === INHERIT ? context.direction : direction;
|
|
104
|
+
let translateY = lineHeightPx / 2;
|
|
105
|
+
let baseline = MIDDLE;
|
|
106
|
+
if (!Konva.legacyTextRendering) {
|
|
107
|
+
const metrics = this.measureSize('M');
|
|
108
|
+
baseline = 'alphabetic';
|
|
109
|
+
const ascent = (_a = metrics.fontBoundingBoxAscent) !== null && _a !== void 0 ? _a : metrics.actualBoundingBoxAscent;
|
|
110
|
+
const descent = (_b = metrics.fontBoundingBoxDescent) !== null && _b !== void 0 ? _b : metrics.actualBoundingBoxDescent;
|
|
111
|
+
translateY = (ascent - descent) / 2 + lineHeightPx / 2;
|
|
112
|
+
}
|
|
113
|
+
if (direction === RTL) {
|
|
114
|
+
context.setAttr('direction', direction);
|
|
115
|
+
}
|
|
116
|
+
context.setAttr('font', this._getContextFont());
|
|
117
|
+
context.setAttr('textBaseline', baseline);
|
|
118
|
+
context.setAttr('textAlign', LEFT);
|
|
119
|
+
if (verticalAlign === MIDDLE) {
|
|
120
|
+
alignY = (this.getHeight() - textArrLen * lineHeightPx - padding * 2) / 2;
|
|
121
|
+
}
|
|
122
|
+
else if (verticalAlign === BOTTOM) {
|
|
123
|
+
alignY = this.getHeight() - textArrLen * lineHeightPx - padding * 2;
|
|
124
|
+
}
|
|
125
|
+
context.translate(padding, alignY + padding);
|
|
126
|
+
for (n = 0; n < textArrLen; n++) {
|
|
127
|
+
let lineTranslateX = 0;
|
|
128
|
+
let lineTranslateY = 0;
|
|
129
|
+
const obj = textArr[n], text = obj.text, width = obj.width, lastLine = obj.lastInParagraph;
|
|
130
|
+
context.save();
|
|
131
|
+
if (align === RIGHT) {
|
|
132
|
+
lineTranslateX += totalWidth - width - padding * 2;
|
|
133
|
+
}
|
|
134
|
+
else if (align === CENTER) {
|
|
135
|
+
lineTranslateX += (totalWidth - width - padding * 2) / 2;
|
|
136
|
+
}
|
|
137
|
+
if (shouldUnderline) {
|
|
138
|
+
context.save();
|
|
139
|
+
context.beginPath();
|
|
140
|
+
const yOffset = !Konva.legacyTextRendering
|
|
141
|
+
? Math.round(fontSize / 4)
|
|
142
|
+
: Math.round(fontSize / 2);
|
|
143
|
+
const x = lineTranslateX;
|
|
144
|
+
const y = translateY + lineTranslateY + yOffset;
|
|
145
|
+
context.moveTo(x, y);
|
|
146
|
+
const lineWidth = align === JUSTIFY && !lastLine ? totalWidth - padding * 2 : width;
|
|
147
|
+
context.lineTo(x + Math.round(lineWidth), y);
|
|
148
|
+
context.lineWidth = fontSize / 15;
|
|
149
|
+
const gradient = this._getLinearGradient();
|
|
150
|
+
context.strokeStyle = gradient || fill;
|
|
151
|
+
context.stroke();
|
|
152
|
+
context.restore();
|
|
153
|
+
}
|
|
154
|
+
const lineThroughStartX = lineTranslateX;
|
|
155
|
+
if (direction !== RTL &&
|
|
156
|
+
(letterSpacing !== 0 || align === JUSTIFY || charRenderFunc)) {
|
|
157
|
+
const spacesNumber = text.split(' ').length - 1;
|
|
158
|
+
const array = stringToArray(text);
|
|
159
|
+
for (let li = 0; li < array.length; li++) {
|
|
160
|
+
const letter = array[li];
|
|
161
|
+
if (letter === ' ' && !lastLine && align === JUSTIFY) {
|
|
162
|
+
lineTranslateX += (totalWidth - padding * 2 - width) / spacesNumber;
|
|
163
|
+
}
|
|
164
|
+
this._partialTextX = lineTranslateX;
|
|
165
|
+
this._partialTextY = translateY + lineTranslateY;
|
|
166
|
+
this._partialText = letter;
|
|
167
|
+
if (charRenderFunc) {
|
|
168
|
+
context.save();
|
|
169
|
+
const previousLines = textArr.slice(0, n);
|
|
170
|
+
const previousGraphemes = previousLines.reduce((acc, line) => acc + stringToArray(line.text).length, 0);
|
|
171
|
+
const charIndex = li + previousGraphemes;
|
|
172
|
+
charRenderFunc({
|
|
173
|
+
char: letter,
|
|
174
|
+
index: charIndex,
|
|
175
|
+
x: lineTranslateX,
|
|
176
|
+
y: translateY + lineTranslateY,
|
|
177
|
+
lineIndex: n,
|
|
178
|
+
column: li,
|
|
179
|
+
isLastInLine: lastLine,
|
|
180
|
+
width: this.measureSize(letter).width,
|
|
181
|
+
context,
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
context.fillStrokeShape(this);
|
|
185
|
+
if (charRenderFunc) {
|
|
186
|
+
context.restore();
|
|
187
|
+
}
|
|
188
|
+
lineTranslateX += this.measureSize(letter).width + letterSpacing;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
if (letterSpacing !== 0) {
|
|
193
|
+
context.setAttr('letterSpacing', `${letterSpacing}px`);
|
|
194
|
+
}
|
|
195
|
+
this._partialTextX = lineTranslateX;
|
|
196
|
+
this._partialTextY = translateY + lineTranslateY;
|
|
197
|
+
this._partialText = text;
|
|
198
|
+
context.fillStrokeShape(this);
|
|
199
|
+
}
|
|
200
|
+
if (shouldLineThrough) {
|
|
201
|
+
context.save();
|
|
202
|
+
context.beginPath();
|
|
203
|
+
const yOffset = !Konva.legacyTextRendering
|
|
204
|
+
? -Math.round(fontSize / 4)
|
|
205
|
+
: 0;
|
|
206
|
+
const x = lineThroughStartX;
|
|
207
|
+
context.moveTo(x, translateY + lineTranslateY + yOffset);
|
|
208
|
+
const lineWidth = align === JUSTIFY && !lastLine ? totalWidth - padding * 2 : width;
|
|
209
|
+
context.lineTo(x + Math.round(lineWidth), translateY + lineTranslateY + yOffset);
|
|
210
|
+
context.lineWidth = fontSize / 15;
|
|
211
|
+
const gradient = this._getLinearGradient();
|
|
212
|
+
context.strokeStyle = gradient || fill;
|
|
213
|
+
context.stroke();
|
|
214
|
+
context.restore();
|
|
215
|
+
}
|
|
216
|
+
context.restore();
|
|
217
|
+
if (textArrLen > 1) {
|
|
218
|
+
translateY += lineHeightPx;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
_hitFunc(context) {
|
|
223
|
+
const width = this.getWidth(), height = this.getHeight();
|
|
224
|
+
context.beginPath();
|
|
225
|
+
context.rect(0, 0, width, height);
|
|
226
|
+
context.closePath();
|
|
227
|
+
context.fillStrokeShape(this);
|
|
228
|
+
}
|
|
229
|
+
setText(text) {
|
|
230
|
+
const str = Util._isString(text)
|
|
231
|
+
? text
|
|
232
|
+
: text === null || text === undefined
|
|
233
|
+
? ''
|
|
234
|
+
: text + '';
|
|
235
|
+
this._setAttr(TEXT, str);
|
|
236
|
+
return this;
|
|
237
|
+
}
|
|
238
|
+
getWidth() {
|
|
239
|
+
const isAuto = this.attrs.width === AUTO || this.attrs.width === undefined;
|
|
240
|
+
return isAuto ? this.getTextWidth() + this.padding() * 2 : this.attrs.width;
|
|
241
|
+
}
|
|
242
|
+
getHeight() {
|
|
243
|
+
const isAuto = this.attrs.height === AUTO || this.attrs.height === undefined;
|
|
244
|
+
return isAuto
|
|
245
|
+
? this.fontSize() * this.textArr.length * this.lineHeight() +
|
|
246
|
+
this.padding() * 2
|
|
247
|
+
: this.attrs.height;
|
|
248
|
+
}
|
|
249
|
+
getTextWidth() {
|
|
250
|
+
return this.textWidth;
|
|
251
|
+
}
|
|
252
|
+
getTextHeight() {
|
|
253
|
+
Util.warn('text.getTextHeight() method is deprecated. Use text.height() - for full height and text.fontSize() - for one line height.');
|
|
254
|
+
return this.textHeight;
|
|
255
|
+
}
|
|
256
|
+
measureSize(text) {
|
|
257
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
|
258
|
+
let _context = getDummyContext(), fontSize = this.fontSize(), metrics;
|
|
259
|
+
_context.save();
|
|
260
|
+
_context.font = this._getContextFont();
|
|
261
|
+
metrics = _context.measureText(text);
|
|
262
|
+
_context.restore();
|
|
263
|
+
const scaleFactor = fontSize / 100;
|
|
264
|
+
return {
|
|
265
|
+
actualBoundingBoxAscent: (_a = metrics.actualBoundingBoxAscent) !== null && _a !== void 0 ? _a : 71.58203125 * scaleFactor,
|
|
266
|
+
actualBoundingBoxDescent: (_b = metrics.actualBoundingBoxDescent) !== null && _b !== void 0 ? _b : 0,
|
|
267
|
+
actualBoundingBoxLeft: (_c = metrics.actualBoundingBoxLeft) !== null && _c !== void 0 ? _c : -7.421875 * scaleFactor,
|
|
268
|
+
actualBoundingBoxRight: (_d = metrics.actualBoundingBoxRight) !== null && _d !== void 0 ? _d : 75.732421875 * scaleFactor,
|
|
269
|
+
alphabeticBaseline: (_e = metrics.alphabeticBaseline) !== null && _e !== void 0 ? _e : 0,
|
|
270
|
+
emHeightAscent: (_f = metrics.emHeightAscent) !== null && _f !== void 0 ? _f : 100 * scaleFactor,
|
|
271
|
+
emHeightDescent: (_g = metrics.emHeightDescent) !== null && _g !== void 0 ? _g : -20 * scaleFactor,
|
|
272
|
+
fontBoundingBoxAscent: (_h = metrics.fontBoundingBoxAscent) !== null && _h !== void 0 ? _h : 91 * scaleFactor,
|
|
273
|
+
fontBoundingBoxDescent: (_j = metrics.fontBoundingBoxDescent) !== null && _j !== void 0 ? _j : 21 * scaleFactor,
|
|
274
|
+
hangingBaseline: (_k = metrics.hangingBaseline) !== null && _k !== void 0 ? _k : 72.80000305175781 * scaleFactor,
|
|
275
|
+
ideographicBaseline: (_l = metrics.ideographicBaseline) !== null && _l !== void 0 ? _l : -21 * scaleFactor,
|
|
276
|
+
width: metrics.width,
|
|
277
|
+
height: fontSize,
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
_getContextFont() {
|
|
281
|
+
return (this.fontStyle() +
|
|
282
|
+
SPACE +
|
|
283
|
+
this.fontVariant() +
|
|
284
|
+
SPACE +
|
|
285
|
+
(this.fontSize() + PX_SPACE) +
|
|
286
|
+
normalizeFontFamily(this.fontFamily()));
|
|
287
|
+
}
|
|
288
|
+
_addTextLine(line) {
|
|
289
|
+
const align = this.align();
|
|
290
|
+
if (align === JUSTIFY) {
|
|
291
|
+
line = line.trim();
|
|
292
|
+
}
|
|
293
|
+
const width = this._getTextWidth(line);
|
|
294
|
+
return this.textArr.push({
|
|
295
|
+
text: line,
|
|
296
|
+
width: width,
|
|
297
|
+
lastInParagraph: false,
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
_getTextWidth(text) {
|
|
301
|
+
const letterSpacing = this.letterSpacing();
|
|
302
|
+
const length = text.length;
|
|
303
|
+
return getDummyContext().measureText(text).width + letterSpacing * length;
|
|
304
|
+
}
|
|
305
|
+
_setTextData() {
|
|
306
|
+
let lines = this.text().split('\n'), fontSize = +this.fontSize(), textWidth = 0, lineHeightPx = this.lineHeight() * fontSize, width = this.attrs.width, height = this.attrs.height, fixedWidth = width !== AUTO && width !== undefined, fixedHeight = height !== AUTO && height !== undefined, padding = this.padding(), maxWidth = width - padding * 2, maxHeightPx = height - padding * 2, currentHeightPx = 0, wrap = this.wrap(), shouldWrap = wrap !== NONE, wrapAtWord = wrap !== CHAR && shouldWrap, shouldAddEllipsis = this.ellipsis();
|
|
307
|
+
this.textArr = [];
|
|
308
|
+
getDummyContext().font = this._getContextFont();
|
|
309
|
+
const additionalWidth = shouldAddEllipsis
|
|
310
|
+
? this._getTextWidth(ELLIPSIS)
|
|
311
|
+
: 0;
|
|
312
|
+
for (let i = 0, max = lines.length; i < max; ++i) {
|
|
313
|
+
let line = lines[i];
|
|
314
|
+
let lineWidth = this._getTextWidth(line);
|
|
315
|
+
if (fixedWidth && lineWidth > maxWidth) {
|
|
316
|
+
while (line.length > 0) {
|
|
317
|
+
let low = 0, high = stringToArray(line).length, match = '', matchWidth = 0;
|
|
318
|
+
while (low < high) {
|
|
319
|
+
const mid = (low + high) >>> 1, lineArray = stringToArray(line), substr = lineArray.slice(0, mid + 1).join(''), substrWidth = this._getTextWidth(substr);
|
|
320
|
+
const shouldConsiderEllipsis = shouldAddEllipsis &&
|
|
321
|
+
fixedHeight &&
|
|
322
|
+
currentHeightPx + lineHeightPx > maxHeightPx;
|
|
323
|
+
const effectiveWidth = shouldConsiderEllipsis
|
|
324
|
+
? substrWidth + additionalWidth
|
|
325
|
+
: substrWidth;
|
|
326
|
+
if (effectiveWidth <= maxWidth) {
|
|
327
|
+
low = mid + 1;
|
|
328
|
+
match = substr;
|
|
329
|
+
matchWidth = substrWidth;
|
|
330
|
+
}
|
|
331
|
+
else {
|
|
332
|
+
high = mid;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
if (match) {
|
|
336
|
+
if (wrapAtWord) {
|
|
337
|
+
const lineArray = stringToArray(line);
|
|
338
|
+
const matchArray = stringToArray(match);
|
|
339
|
+
const nextChar = lineArray[matchArray.length];
|
|
340
|
+
const nextIsSpaceOrDash = nextChar === SPACE || nextChar === DASH;
|
|
341
|
+
let wrapIndex;
|
|
342
|
+
if (nextIsSpaceOrDash && matchWidth <= maxWidth) {
|
|
343
|
+
wrapIndex = matchArray.length;
|
|
344
|
+
}
|
|
345
|
+
else {
|
|
346
|
+
const lastSpaceIndex = matchArray.lastIndexOf(SPACE);
|
|
347
|
+
const lastDashIndex = matchArray.lastIndexOf(DASH);
|
|
348
|
+
wrapIndex = Math.max(lastSpaceIndex, lastDashIndex) + 1;
|
|
349
|
+
}
|
|
350
|
+
if (wrapIndex > 0) {
|
|
351
|
+
low = wrapIndex;
|
|
352
|
+
match = lineArray.slice(0, low).join('');
|
|
353
|
+
matchWidth = this._getTextWidth(match);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
match = match.trimRight();
|
|
357
|
+
this._addTextLine(match);
|
|
358
|
+
textWidth = Math.max(textWidth, matchWidth);
|
|
359
|
+
currentHeightPx += lineHeightPx;
|
|
360
|
+
const shouldHandleEllipsis = this._shouldHandleEllipsis(currentHeightPx);
|
|
361
|
+
if (shouldHandleEllipsis) {
|
|
362
|
+
this._tryToAddEllipsisToLastLine();
|
|
363
|
+
break;
|
|
364
|
+
}
|
|
365
|
+
const lineArray = stringToArray(line);
|
|
366
|
+
line = lineArray.slice(low).join('').trimLeft();
|
|
367
|
+
if (line.length > 0) {
|
|
368
|
+
lineWidth = this._getTextWidth(line);
|
|
369
|
+
if (lineWidth <= maxWidth) {
|
|
370
|
+
this._addTextLine(line);
|
|
371
|
+
currentHeightPx += lineHeightPx;
|
|
372
|
+
textWidth = Math.max(textWidth, lineWidth);
|
|
373
|
+
break;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
else {
|
|
378
|
+
break;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
else {
|
|
383
|
+
this._addTextLine(line);
|
|
384
|
+
currentHeightPx += lineHeightPx;
|
|
385
|
+
textWidth = Math.max(textWidth, lineWidth);
|
|
386
|
+
if (this._shouldHandleEllipsis(currentHeightPx) && i < max - 1) {
|
|
387
|
+
this._tryToAddEllipsisToLastLine();
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
if (this.textArr[this.textArr.length - 1]) {
|
|
391
|
+
this.textArr[this.textArr.length - 1].lastInParagraph = true;
|
|
392
|
+
}
|
|
393
|
+
if (fixedHeight && currentHeightPx + lineHeightPx > maxHeightPx) {
|
|
394
|
+
break;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
this.textHeight = fontSize;
|
|
398
|
+
this.textWidth = textWidth;
|
|
399
|
+
}
|
|
400
|
+
_shouldHandleEllipsis(currentHeightPx) {
|
|
401
|
+
const fontSize = +this.fontSize(), lineHeightPx = this.lineHeight() * fontSize, height = this.attrs.height, fixedHeight = height !== AUTO && height !== undefined, padding = this.padding(), maxHeightPx = height - padding * 2, wrap = this.wrap(), shouldWrap = wrap !== NONE;
|
|
402
|
+
return (!shouldWrap ||
|
|
403
|
+
(fixedHeight && currentHeightPx + lineHeightPx > maxHeightPx));
|
|
404
|
+
}
|
|
405
|
+
_tryToAddEllipsisToLastLine() {
|
|
406
|
+
const width = this.attrs.width, fixedWidth = width !== AUTO && width !== undefined, padding = this.padding(), maxWidth = width - padding * 2, shouldAddEllipsis = this.ellipsis();
|
|
407
|
+
const lastLine = this.textArr[this.textArr.length - 1];
|
|
408
|
+
if (!lastLine || !shouldAddEllipsis) {
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
if (fixedWidth) {
|
|
412
|
+
const haveSpace = this._getTextWidth(lastLine.text + ELLIPSIS) < maxWidth;
|
|
413
|
+
if (!haveSpace) {
|
|
414
|
+
lastLine.text = lastLine.text.slice(0, lastLine.text.length - 3);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
this.textArr.splice(this.textArr.length - 1, 1);
|
|
418
|
+
this._addTextLine(lastLine.text + ELLIPSIS);
|
|
419
|
+
}
|
|
420
|
+
getStrokeScaleEnabled() {
|
|
421
|
+
return true;
|
|
422
|
+
}
|
|
423
|
+
_useBufferCanvas() {
|
|
424
|
+
const hasLine = this.textDecoration().indexOf('underline') !== -1 ||
|
|
425
|
+
this.textDecoration().indexOf('line-through') !== -1;
|
|
426
|
+
const hasShadow = this.hasShadow();
|
|
427
|
+
if (hasLine && hasShadow) {
|
|
428
|
+
return true;
|
|
429
|
+
}
|
|
430
|
+
return super._useBufferCanvas();
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
Text.prototype._fillFunc = _fillFunc;
|
|
434
|
+
Text.prototype._strokeFunc = _strokeFunc;
|
|
435
|
+
Text.prototype.className = TEXT_UPPER;
|
|
436
|
+
Text.prototype._attrsAffectingSize = [
|
|
437
|
+
'text',
|
|
438
|
+
'fontSize',
|
|
439
|
+
'padding',
|
|
440
|
+
'wrap',
|
|
441
|
+
'lineHeight',
|
|
442
|
+
'letterSpacing',
|
|
443
|
+
];
|
|
444
|
+
_registerNode(Text);
|
|
445
|
+
Factory.overWriteSetter(Text, 'width', getNumberOrAutoValidator());
|
|
446
|
+
Factory.overWriteSetter(Text, 'height', getNumberOrAutoValidator());
|
|
447
|
+
Factory.addGetterSetter(Text, 'direction', INHERIT);
|
|
448
|
+
Factory.addGetterSetter(Text, 'fontFamily', 'Arial');
|
|
449
|
+
Factory.addGetterSetter(Text, 'fontSize', 12, getNumberValidator());
|
|
450
|
+
Factory.addGetterSetter(Text, 'fontStyle', NORMAL);
|
|
451
|
+
Factory.addGetterSetter(Text, 'fontVariant', NORMAL);
|
|
452
|
+
Factory.addGetterSetter(Text, 'padding', 0, getNumberValidator());
|
|
453
|
+
Factory.addGetterSetter(Text, 'align', LEFT);
|
|
454
|
+
Factory.addGetterSetter(Text, 'verticalAlign', TOP);
|
|
455
|
+
Factory.addGetterSetter(Text, 'lineHeight', 1, getNumberValidator());
|
|
456
|
+
Factory.addGetterSetter(Text, 'wrap', WORD);
|
|
457
|
+
Factory.addGetterSetter(Text, 'ellipsis', false, getBooleanValidator());
|
|
458
|
+
Factory.addGetterSetter(Text, 'letterSpacing', 0, getNumberValidator());
|
|
459
|
+
Factory.addGetterSetter(Text, 'text', '', getStringValidator());
|
|
460
|
+
Factory.addGetterSetter(Text, 'textDecoration', '');
|
|
461
|
+
Factory.addGetterSetter(Text, 'charRenderFunc', undefined);
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { Context } from '../Context.ts';
|
|
2
|
+
import type { ShapeConfig } from '../Shape.ts';
|
|
3
|
+
import { Shape } from '../Shape.ts';
|
|
4
|
+
import { Text } from './Text.ts';
|
|
5
|
+
import type { GetSet, PathSegment, Vector2d } from '../types.ts';
|
|
6
|
+
export interface TextPathConfig extends ShapeConfig {
|
|
7
|
+
text?: string;
|
|
8
|
+
data?: string;
|
|
9
|
+
fontFamily?: string;
|
|
10
|
+
fontSize?: number;
|
|
11
|
+
fontStyle?: string;
|
|
12
|
+
fontVariant?: string;
|
|
13
|
+
align?: string;
|
|
14
|
+
letterSpacing?: number;
|
|
15
|
+
textBaseline?: string;
|
|
16
|
+
textDecoration?: string;
|
|
17
|
+
kerningFunc?: (leftChar: string, rightChar: string) => number;
|
|
18
|
+
lineHeight?: number;
|
|
19
|
+
}
|
|
20
|
+
export declare class TextPath extends Shape<TextPathConfig> {
|
|
21
|
+
dummyCanvas: HTMLCanvasElement;
|
|
22
|
+
dataArray: PathSegment[];
|
|
23
|
+
glyphInfo: Array<{
|
|
24
|
+
transposeX: number;
|
|
25
|
+
transposeY: number;
|
|
26
|
+
text: string;
|
|
27
|
+
rotation: number;
|
|
28
|
+
p0: Vector2d;
|
|
29
|
+
p1: Vector2d;
|
|
30
|
+
width: number;
|
|
31
|
+
}>;
|
|
32
|
+
partialText: string;
|
|
33
|
+
pathLength: number;
|
|
34
|
+
textWidth: number;
|
|
35
|
+
textHeight: number;
|
|
36
|
+
constructor(config?: TextPathConfig);
|
|
37
|
+
_getTextPathLength(): number;
|
|
38
|
+
_getPointAtLength(length: number): {
|
|
39
|
+
x: number;
|
|
40
|
+
y: number;
|
|
41
|
+
} | null;
|
|
42
|
+
_readDataAttribute(): void;
|
|
43
|
+
_sceneFunc(context: Context): void;
|
|
44
|
+
_hitFunc(context: Context): void;
|
|
45
|
+
getTextWidth(): number;
|
|
46
|
+
getTextHeight(): number;
|
|
47
|
+
setText(text: string): Text;
|
|
48
|
+
_getContextFont(): string;
|
|
49
|
+
_getTextSize(text: string): {
|
|
50
|
+
width: number;
|
|
51
|
+
height: number;
|
|
52
|
+
};
|
|
53
|
+
_setTextData(): null | undefined;
|
|
54
|
+
getSelfRect(): {
|
|
55
|
+
x: number;
|
|
56
|
+
y: number;
|
|
57
|
+
width: number;
|
|
58
|
+
height: number;
|
|
59
|
+
};
|
|
60
|
+
destroy(): this;
|
|
61
|
+
fontFamily: GetSet<string, this>;
|
|
62
|
+
fontSize: GetSet<number, this>;
|
|
63
|
+
fontStyle: GetSet<string, this>;
|
|
64
|
+
fontVariant: GetSet<string, this>;
|
|
65
|
+
align: GetSet<string, this>;
|
|
66
|
+
letterSpacing: GetSet<number, this>;
|
|
67
|
+
text: GetSet<string, this>;
|
|
68
|
+
data: GetSet<string, this>;
|
|
69
|
+
kerningFunc: GetSet<(leftChar: string, rightChar: string) => number, this>;
|
|
70
|
+
textBaseline: GetSet<string, this>;
|
|
71
|
+
textDecoration: GetSet<string, this>;
|
|
72
|
+
}
|