konva 10.5.0 → 10.7.0
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/README.md +16 -12
- package/konva.js +3623 -3819
- package/konva.min.js +4 -4
- package/lib/Animation.d.ts +2 -2
- package/lib/Animation.js +2 -2
- package/lib/BezierFunctions.d.ts +1 -1
- package/lib/BezierFunctions.js +76 -116
- package/lib/Canvas.d.ts +10 -3
- package/lib/Canvas.js +91 -10
- package/lib/Container.d.ts +3 -2
- package/lib/Container.js +31 -17
- package/lib/Context.d.ts +9 -7
- package/lib/Context.js +99 -73
- package/lib/DragAndDrop.d.ts +4 -3
- package/lib/DragAndDrop.js +39 -25
- package/lib/Factory.d.ts +3 -2
- package/lib/Factory.js +10 -9
- package/lib/Global.d.ts +2 -7
- package/lib/Global.js +8 -9
- package/lib/Group.d.ts +7 -1
- package/lib/Group.js +61 -0
- package/lib/Layer.d.ts +1 -1
- package/lib/Layer.js +13 -8
- package/lib/Node.d.ts +25 -19
- package/lib/Node.js +178 -169
- package/lib/PointerEvents.d.ts +1 -2
- package/lib/PointerEvents.js +20 -18
- package/lib/Shape.d.ts +9 -15
- package/lib/Shape.js +190 -86
- package/lib/Stage.d.ts +19 -15
- package/lib/Stage.js +184 -99
- package/lib/Tween.js +59 -66
- package/lib/Util.d.ts +3 -2
- package/lib/Util.js +46 -16
- package/lib/_CoreInternals.d.ts +23 -11
- package/lib/_FullInternals.d.ts +23 -11
- package/lib/filters/Blur.js +16 -12
- package/lib/filters/Brighten.d.ts +1 -0
- package/lib/filters/Brighten.js +1 -0
- package/lib/filters/Brightness.d.ts +2 -1
- package/lib/filters/Brightness.js +4 -4
- package/lib/filters/Emboss.js +16 -58
- package/lib/filters/Kaleidoscope.js +8 -15
- package/lib/filters/Mask.js +8 -8
- package/lib/filters/Pixelate.js +16 -29
- package/lib/filters/Threshold.d.ts +1 -1
- package/lib/filters/Threshold.js +1 -1
- package/lib/shapes/Arc.js +8 -6
- package/lib/shapes/Arrow.d.ts +3 -6
- package/lib/shapes/Arrow.js +75 -47
- package/lib/shapes/Circle.d.ts +1 -0
- package/lib/shapes/Circle.js +4 -1
- package/lib/shapes/Ellipse.d.ts +1 -0
- package/lib/shapes/Ellipse.js +4 -1
- package/lib/shapes/Image.d.ts +2 -1
- package/lib/shapes/Image.js +22 -22
- package/lib/shapes/Label.d.ts +2 -3
- package/lib/shapes/Label.js +25 -41
- package/lib/shapes/Line.d.ts +2 -1
- package/lib/shapes/Line.js +5 -2
- package/lib/shapes/Path.d.ts +11 -2
- package/lib/shapes/Path.js +133 -143
- package/lib/shapes/Rect.d.ts +1 -0
- package/lib/shapes/Rect.js +4 -1
- package/lib/shapes/RegularPolygon.js +2 -2
- package/lib/shapes/Ring.d.ts +7 -0
- package/lib/shapes/Ring.js +8 -1
- package/lib/shapes/Sprite.d.ts +2 -1
- package/lib/shapes/Sprite.js +8 -9
- package/lib/shapes/Star.d.ts +6 -0
- package/lib/shapes/Star.js +5 -1
- package/lib/shapes/Text.d.ts +16 -5
- package/lib/shapes/Text.js +134 -65
- package/lib/shapes/TextPath.d.ts +11 -3
- package/lib/shapes/TextPath.js +56 -27
- package/lib/shapes/Transformer.js +95 -96
- package/lib/shapes/Wedge.js +1 -1
- package/lib/types.d.ts +2 -2
- package/package.json +5 -20
package/lib/shapes/Text.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Context } from '../Context.ts';
|
|
1
|
+
import type { Context, SceneContext } from '../Context.ts';
|
|
2
2
|
import type { ShapeConfig } from '../Shape.ts';
|
|
3
3
|
import { Shape } from '../Shape.ts';
|
|
4
4
|
import type { GetSet } from '../types.ts';
|
|
@@ -79,8 +79,18 @@ export declare class Text extends Shape<TextConfig> {
|
|
|
79
79
|
_partialStrokeStyle?: string | CanvasGradient | CanvasPattern;
|
|
80
80
|
textWidth: number;
|
|
81
81
|
textHeight: number;
|
|
82
|
+
_baselineShift: number;
|
|
82
83
|
constructor(config?: TextConfig);
|
|
83
|
-
_sceneFunc(context:
|
|
84
|
+
_sceneFunc(context: SceneContext): void;
|
|
85
|
+
_getTextTop(): number;
|
|
86
|
+
_getUnderlineOffset(): number;
|
|
87
|
+
getSelfRect(): import("../types.ts").IRect;
|
|
88
|
+
_getSelfRectForDrawing(): {
|
|
89
|
+
x: number;
|
|
90
|
+
y: number;
|
|
91
|
+
width: number;
|
|
92
|
+
height: number;
|
|
93
|
+
};
|
|
84
94
|
_hitFunc(context: Context): void;
|
|
85
95
|
setText(text: string): this;
|
|
86
96
|
getWidth(): any;
|
|
@@ -130,7 +140,8 @@ export declare class Text extends Shape<TextConfig> {
|
|
|
130
140
|
_shouldHandleEllipsis(currentHeightPx: number): boolean;
|
|
131
141
|
_tryToAddEllipsisToLastLine(): void;
|
|
132
142
|
getStrokeScaleEnabled(): boolean;
|
|
133
|
-
|
|
143
|
+
_getStrokePadding(): number;
|
|
144
|
+
_useBufferCanvas(forceFill?: boolean, opacity?: any): boolean;
|
|
134
145
|
direction: GetSet<string, this>;
|
|
135
146
|
fontFamily: GetSet<string, this>;
|
|
136
147
|
fontSize: GetSet<number, this>;
|
|
@@ -147,6 +158,6 @@ export declare class Text extends Shape<TextConfig> {
|
|
|
147
158
|
wrap: GetSet<string, this>;
|
|
148
159
|
ellipsis: GetSet<boolean, this>;
|
|
149
160
|
charRenderFunc: GetSet<null | ((props: CharRenderProps) => void), this>;
|
|
150
|
-
width: GetSet<number, this, number | 'auto'>;
|
|
151
|
-
height: GetSet<number, this, number | 'auto'>;
|
|
161
|
+
width: GetSet<number, this, number | 'auto' | null | undefined>;
|
|
162
|
+
height: GetSet<number, this, number | 'auto' | null | undefined>;
|
|
152
163
|
}
|
package/lib/shapes/Text.js
CHANGED
|
@@ -124,13 +124,12 @@ function _strokeFunc(context) {
|
|
|
124
124
|
}
|
|
125
125
|
function checkDefaultFill(config) {
|
|
126
126
|
config = config || {};
|
|
127
|
-
// set default color to black
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
config.
|
|
132
|
-
}
|
|
133
|
-
return config;
|
|
127
|
+
// set default color to black, without mutating the caller's config
|
|
128
|
+
const hasFill = config.fill ||
|
|
129
|
+
config.fillLinearGradientColorStops ||
|
|
130
|
+
config.fillRadialGradientColorStops ||
|
|
131
|
+
config.fillPatternImage;
|
|
132
|
+
return hasFill ? config : { ...config, fill: 'black' };
|
|
134
133
|
}
|
|
135
134
|
/**
|
|
136
135
|
* Text constructor
|
|
@@ -169,6 +168,8 @@ export class Text extends Shape {
|
|
|
169
168
|
super(checkDefaultFill(config));
|
|
170
169
|
this._partialTextX = 0;
|
|
171
170
|
this._partialTextY = 0;
|
|
171
|
+
// distance from the line box center to the baseline, see _setTextData
|
|
172
|
+
this._baselineShift = 0;
|
|
172
173
|
this._setTextData();
|
|
173
174
|
}
|
|
174
175
|
_sceneFunc(context) {
|
|
@@ -177,8 +178,7 @@ export class Text extends Shape {
|
|
|
177
178
|
if (!this.text()) {
|
|
178
179
|
return;
|
|
179
180
|
}
|
|
180
|
-
let padding = this.padding(), fontSize = this.fontSize(), lineHeightPx = this.lineHeight() * fontSize,
|
|
181
|
-
direction = direction === INHERIT ? context.direction : direction;
|
|
181
|
+
let padding = this.padding(), fontSize = this.fontSize(), lineHeightPx = this.lineHeight() * fontSize, direction = this.direction(), align = this.align(), totalWidth = this.getWidth(), letterSpacing = this.letterSpacing(), charRenderFunc = this.charRenderFunc(), textDecoration = this.textDecoration(), shouldUnderline = textDecoration.indexOf('underline') !== -1, shouldLineThrough = textDecoration.indexOf('line-through') !== -1, n;
|
|
182
182
|
let translateY = lineHeightPx / 2;
|
|
183
183
|
let baseline = MIDDLE;
|
|
184
184
|
if (!Konva.legacyTextRendering) {
|
|
@@ -188,20 +188,27 @@ export class Text extends Shape {
|
|
|
188
188
|
const descent = (_b = metrics.fontBoundingBoxDescent) !== null && _b !== void 0 ? _b : metrics.actualBoundingBoxDescent;
|
|
189
189
|
translateY = (ascent - descent) / 2 + lineHeightPx / 2;
|
|
190
190
|
}
|
|
191
|
-
if (direction
|
|
191
|
+
if (direction !== INHERIT) {
|
|
192
192
|
context.setAttr('direction', direction);
|
|
193
193
|
}
|
|
194
|
+
else {
|
|
195
|
+
// 'inherit' resolves to the direction the canvas already has, so that
|
|
196
|
+
// an inherited rtl still takes the single native run below
|
|
197
|
+
direction = context.direction;
|
|
198
|
+
}
|
|
194
199
|
context.setAttr('font', this._getContextFont());
|
|
195
200
|
context.setAttr('textBaseline', baseline);
|
|
196
201
|
context.setAttr('textAlign', LEFT);
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
202
|
+
context.translate(padding, this._getTextTop());
|
|
203
|
+
// Start the callback with the same fill that normal drawing would use.
|
|
204
|
+
// Assigning that color again then needs no override, including black.
|
|
205
|
+
if (charRenderFunc) {
|
|
206
|
+
const style = context._getFillStyle(this);
|
|
207
|
+
if (style !== undefined)
|
|
208
|
+
context.fillStyle = style;
|
|
209
|
+
if (this.hasStroke())
|
|
210
|
+
context._applyStrokeStyle(this);
|
|
203
211
|
}
|
|
204
|
-
context.translate(padding, alignY + padding);
|
|
205
212
|
// context styles entering the loop, used to detect styles set by
|
|
206
213
|
// charRenderFunc; every character is drawn inside save()/restore(),
|
|
207
214
|
// so the entry state is the same for each one
|
|
@@ -225,28 +232,23 @@ export class Text extends Shape {
|
|
|
225
232
|
if (shouldUnderline) {
|
|
226
233
|
context.save();
|
|
227
234
|
context.beginPath();
|
|
228
|
-
const yOffset =
|
|
229
|
-
? Math.round(fontSize / 4)
|
|
230
|
-
: Math.round(fontSize / 2));
|
|
235
|
+
const yOffset = this._getUnderlineOffset();
|
|
231
236
|
const x = lineTranslateX;
|
|
232
237
|
const y = translateY + lineTranslateY + yOffset;
|
|
233
238
|
context.moveTo(x, y);
|
|
234
239
|
const lineWidth = align === JUSTIFY && !lastLine ? totalWidth - padding * 2 : width;
|
|
235
240
|
context.lineTo(x + Math.round(lineWidth), y);
|
|
236
241
|
context.lineWidth = getDecorationLineWidth(fontSize);
|
|
237
|
-
|
|
238
|
-
context.strokeStyle = gradient || fill;
|
|
242
|
+
context.strokeStyle = context._getFillStyle(this);
|
|
239
243
|
context.stroke();
|
|
240
244
|
context.restore();
|
|
241
245
|
}
|
|
242
246
|
// store the starting x position for line-through which is drawn after text
|
|
243
247
|
const lineThroughStartX = lineTranslateX;
|
|
244
|
-
//
|
|
245
|
-
//
|
|
246
|
-
// be supported otherwise.
|
|
248
|
+
// Render graphemes separately for spacing, justification and callbacks.
|
|
249
|
+
// Keep RTL text in one native run to preserve shaping.
|
|
247
250
|
if (direction !== RTL &&
|
|
248
251
|
(letterSpacing !== 0 || align === JUSTIFY || charRenderFunc)) {
|
|
249
|
-
// var words = text.split(' ');
|
|
250
252
|
const spacesNumber = text.split(' ').length - 1;
|
|
251
253
|
const array = stringToArray(text);
|
|
252
254
|
for (let li = 0; li < array.length; li++) {
|
|
@@ -254,10 +256,6 @@ export class Text extends Shape {
|
|
|
254
256
|
// skip justify for the last line
|
|
255
257
|
if (letter === ' ' && !lastLine && align === JUSTIFY) {
|
|
256
258
|
lineTranslateX += (totalWidth - padding * 2 - width) / spacesNumber;
|
|
257
|
-
// context.translate(
|
|
258
|
-
// Math.floor((totalWidth - padding * 2 - width) / spacesNumber),
|
|
259
|
-
// 0
|
|
260
|
-
// );
|
|
261
259
|
}
|
|
262
260
|
this._partialTextX = lineTranslateX;
|
|
263
261
|
this._partialTextY = translateY + lineTranslateY;
|
|
@@ -320,8 +318,7 @@ export class Text extends Shape {
|
|
|
320
318
|
const lineWidth = align === JUSTIFY && !lastLine ? totalWidth - padding * 2 : width;
|
|
321
319
|
context.lineTo(x + Math.round(lineWidth), translateY + lineTranslateY + yOffset);
|
|
322
320
|
context.lineWidth = getDecorationLineWidth(fontSize);
|
|
323
|
-
|
|
324
|
-
context.strokeStyle = gradient || fill;
|
|
321
|
+
context.strokeStyle = context._getFillStyle(this);
|
|
325
322
|
context.stroke();
|
|
326
323
|
context.restore();
|
|
327
324
|
}
|
|
@@ -331,6 +328,62 @@ export class Text extends Shape {
|
|
|
331
328
|
}
|
|
332
329
|
}
|
|
333
330
|
}
|
|
331
|
+
_getTextTop() {
|
|
332
|
+
const padding = this.padding(), free = this.getHeight() -
|
|
333
|
+
this.textArr.length * this.lineHeight() * this.fontSize() -
|
|
334
|
+
padding * 2, verticalAlign = this.verticalAlign();
|
|
335
|
+
if (verticalAlign === MIDDLE)
|
|
336
|
+
return padding + free / 2;
|
|
337
|
+
if (verticalAlign === BOTTOM)
|
|
338
|
+
return padding + free;
|
|
339
|
+
return padding;
|
|
340
|
+
}
|
|
341
|
+
_getUnderlineOffset() {
|
|
342
|
+
var _a;
|
|
343
|
+
return ((_a = this.underlineOffset()) !== null && _a !== void 0 ? _a : Math.round(this.fontSize() / (!Konva.legacyTextRendering ? 4 : 2)));
|
|
344
|
+
}
|
|
345
|
+
getSelfRect() {
|
|
346
|
+
const rect = super.getSelfRect();
|
|
347
|
+
const lines = this.textArr.length;
|
|
348
|
+
if (!lines || this.textDecoration().indexOf('underline') === -1) {
|
|
349
|
+
return rect;
|
|
350
|
+
}
|
|
351
|
+
// the underline of the last line is drawn below the text block
|
|
352
|
+
const fontSize = this.fontSize(), lineHeightPx = this.lineHeight() * fontSize;
|
|
353
|
+
let bottom = this._getTextTop() + lines * lineHeightPx - lineHeightPx / 2;
|
|
354
|
+
if (!Konva.legacyTextRendering) {
|
|
355
|
+
bottom += this._baselineShift;
|
|
356
|
+
}
|
|
357
|
+
bottom += this._getUnderlineOffset() + getDecorationLineWidth(fontSize) / 2;
|
|
358
|
+
rect.height = Math.max(rect.y + rect.height, bottom) - rect.y;
|
|
359
|
+
return rect;
|
|
360
|
+
}
|
|
361
|
+
_getSelfRectForDrawing() {
|
|
362
|
+
// Lines can be wider than the box, negative spacing moves the pen back,
|
|
363
|
+
// and glyph ink passes the advance box (italics, accents): up to 0.6em
|
|
364
|
+
// in common fonts, so pad by 1em.
|
|
365
|
+
const rect = this.getSelfRect(), fontSize = this.fontSize(), padding = this.padding(), lineHeightPx = this.lineHeight() * fontSize, available = this.getWidth() - padding * 2, align = this.align(), blockHeight = this.textArr.length * lineHeightPx, top = this._getTextTop();
|
|
366
|
+
let left = rect.x, right = rect.x + rect.width;
|
|
367
|
+
for (const { text, width } of this.textArr) {
|
|
368
|
+
const x = padding +
|
|
369
|
+
(align === RIGHT
|
|
370
|
+
? available - width
|
|
371
|
+
: align === CENTER
|
|
372
|
+
? (available - width) / 2
|
|
373
|
+
: 0);
|
|
374
|
+
const back = Math.min(0, this.letterSpacing()) * text.length;
|
|
375
|
+
left = Math.min(left, x + back);
|
|
376
|
+
right = Math.max(right, x + width - back);
|
|
377
|
+
}
|
|
378
|
+
const underline = this.textDecoration().includes('underline');
|
|
379
|
+
const y = Math.min(rect.y, top + (underline ? Math.min(0, this._getUnderlineOffset()) : 0));
|
|
380
|
+
return {
|
|
381
|
+
x: left - fontSize,
|
|
382
|
+
y: y - fontSize,
|
|
383
|
+
width: right - left + fontSize * 2,
|
|
384
|
+
height: Math.max(rect.y + rect.height, top + blockHeight) - y + fontSize * 2,
|
|
385
|
+
};
|
|
386
|
+
}
|
|
334
387
|
_hitFunc(context) {
|
|
335
388
|
const width = this.getWidth(), height = this.getHeight();
|
|
336
389
|
context.beginPath();
|
|
@@ -435,11 +488,26 @@ export class Text extends Shape {
|
|
|
435
488
|
return getDummyContext().measureText(text).width + spacing;
|
|
436
489
|
}
|
|
437
490
|
_setTextData() {
|
|
438
|
-
let lines = this.text().split('\n'), fontSize = +this.fontSize(),
|
|
491
|
+
let lines = this.text().split('\n'), fontSize = +this.fontSize(), 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(),
|
|
439
492
|
// align = this.align(),
|
|
440
493
|
shouldWrap = wrap !== NONE, wrapAtWord = wrap !== CHAR && shouldWrap, shouldAddEllipsis = this.ellipsis();
|
|
494
|
+
// measured here so getSelfRect, which runs per drag frame, needs no
|
|
495
|
+
// measureText of its own
|
|
496
|
+
const sample = this.measureSize('M');
|
|
497
|
+
this._baselineShift =
|
|
498
|
+
(sample.fontBoundingBoxAscent - sample.fontBoundingBoxDescent) / 2;
|
|
441
499
|
this.textArr = [];
|
|
442
|
-
|
|
500
|
+
const dummyContext = getDummyContext();
|
|
501
|
+
dummyContext.font = this._getContextFont();
|
|
502
|
+
// the per-character draw path (same condition as in _sceneFunc) advances
|
|
503
|
+
// by unkerned glyph widths, so measure the line the same way
|
|
504
|
+
dummyContext.fontKerning =
|
|
505
|
+
this.direction() !== RTL &&
|
|
506
|
+
(this.letterSpacing() !== 0 ||
|
|
507
|
+
this.align() === JUSTIFY ||
|
|
508
|
+
!!this.charRenderFunc())
|
|
509
|
+
? 'none'
|
|
510
|
+
: 'auto';
|
|
443
511
|
const additionalWidth = shouldAddEllipsis
|
|
444
512
|
? this._getTextWidth(ELLIPSIS)
|
|
445
513
|
: 0;
|
|
@@ -466,14 +534,11 @@ export class Text extends Shape {
|
|
|
466
534
|
currentHeightPx + lineHeightPx > maxHeightPx
|
|
467
535
|
? additionalWidth
|
|
468
536
|
: 0;
|
|
469
|
-
// width of the longest fitting prefix found so far
|
|
470
|
-
let matchWidth = 0;
|
|
471
537
|
const fits = (end) => {
|
|
472
538
|
const width = this._getTextWidth(text(end), end - start);
|
|
473
539
|
if (width + extraWidth > maxWidth) {
|
|
474
540
|
return false;
|
|
475
541
|
}
|
|
476
|
-
matchWidth = width;
|
|
477
542
|
return true;
|
|
478
543
|
};
|
|
479
544
|
/*
|
|
@@ -508,7 +573,11 @@ export class Text extends Shape {
|
|
|
508
573
|
// without the ellipsis check, like a paragraph that never wrapped
|
|
509
574
|
this._addTextLine(text(length));
|
|
510
575
|
currentHeightPx += lineHeightPx;
|
|
511
|
-
|
|
576
|
+
if (fixedHeight &&
|
|
577
|
+
currentHeightPx + lineHeightPx > maxHeightPx &&
|
|
578
|
+
i < max - 1) {
|
|
579
|
+
this._tryToAddEllipsisToLastLine();
|
|
580
|
+
}
|
|
512
581
|
break;
|
|
513
582
|
}
|
|
514
583
|
if (wrapAtWord && !isBreak(graphemes[low])) {
|
|
@@ -519,11 +588,9 @@ export class Text extends Shape {
|
|
|
519
588
|
}
|
|
520
589
|
if (wrapIndex >= start) {
|
|
521
590
|
low = wrapIndex + 1;
|
|
522
|
-
matchWidth = this._getTextWidth(text(low), low - start);
|
|
523
591
|
}
|
|
524
592
|
}
|
|
525
593
|
this._addTextLine(text(low).trimRight());
|
|
526
|
-
textWidth = Math.max(textWidth, matchWidth);
|
|
527
594
|
currentHeightPx += lineHeightPx;
|
|
528
595
|
if (this._shouldHandleEllipsis(currentHeightPx)) {
|
|
529
596
|
this._tryToAddEllipsisToLastLine();
|
|
@@ -544,8 +611,9 @@ export class Text extends Shape {
|
|
|
544
611
|
// element width is automatically adjusted to max line width
|
|
545
612
|
this._addTextLine(line);
|
|
546
613
|
currentHeightPx += lineHeightPx;
|
|
547
|
-
|
|
548
|
-
|
|
614
|
+
if (fixedHeight &&
|
|
615
|
+
currentHeightPx + lineHeightPx > maxHeightPx &&
|
|
616
|
+
i < max - 1) {
|
|
549
617
|
this._tryToAddEllipsisToLastLine();
|
|
550
618
|
}
|
|
551
619
|
}
|
|
@@ -558,11 +626,7 @@ export class Text extends Shape {
|
|
|
558
626
|
}
|
|
559
627
|
}
|
|
560
628
|
this.textHeight = fontSize;
|
|
561
|
-
|
|
562
|
-
// for(var j = 0; j < this.textArr.length; j++) {
|
|
563
|
-
// maxTextWidth = Math.max(maxTextWidth, this.textArr[j].width);
|
|
564
|
-
// }
|
|
565
|
-
this.textWidth = textWidth;
|
|
629
|
+
this.textWidth = this.textArr.reduce((width, line) => Math.max(width, line.width), 0);
|
|
566
630
|
}
|
|
567
631
|
/**
|
|
568
632
|
* whether to handle ellipsis, there are two cases:
|
|
@@ -582,45 +646,50 @@ export class Text extends Shape {
|
|
|
582
646
|
if (!lastLine || !shouldAddEllipsis) {
|
|
583
647
|
return;
|
|
584
648
|
}
|
|
649
|
+
let text = lastLine.text + ELLIPSIS;
|
|
585
650
|
if (fixedWidth) {
|
|
586
|
-
const
|
|
587
|
-
|
|
588
|
-
|
|
651
|
+
const graphemes = stringToArray(lastLine.text);
|
|
652
|
+
while (graphemes.length && this._getTextWidth(text) > maxWidth) {
|
|
653
|
+
graphemes.pop();
|
|
654
|
+
text = graphemes.join('') + ELLIPSIS;
|
|
589
655
|
}
|
|
656
|
+
if (this._getTextWidth(text) > maxWidth)
|
|
657
|
+
text = '';
|
|
590
658
|
}
|
|
591
|
-
|
|
592
|
-
this.
|
|
659
|
+
lastLine.text = text;
|
|
660
|
+
lastLine.width = this._getTextWidth(text);
|
|
593
661
|
}
|
|
594
662
|
// for text we can't disable stroke scaling
|
|
595
663
|
// if we do, the result will be unexpected
|
|
596
664
|
getStrokeScaleEnabled() {
|
|
597
665
|
return true;
|
|
598
666
|
}
|
|
599
|
-
|
|
667
|
+
_getStrokePadding() {
|
|
668
|
+
// _strokeFunc uses a miter limit of two for glyph outlines.
|
|
669
|
+
return super._getStrokePadding(2);
|
|
670
|
+
}
|
|
671
|
+
_useBufferCanvas(forceFill, opacity = this.getAbsoluteOpacity()) {
|
|
672
|
+
if (this.attrs.perfectDrawEnabled === false)
|
|
673
|
+
return false;
|
|
600
674
|
const hasLine = this.textDecoration().indexOf('underline') !== -1 ||
|
|
601
675
|
this.textDecoration().indexOf('line-through') !== -1;
|
|
602
676
|
const hasShadow = this.hasShadow();
|
|
603
677
|
if (hasLine && hasShadow) {
|
|
604
678
|
return true;
|
|
605
679
|
}
|
|
606
|
-
if (hasShadow &&
|
|
680
|
+
if (hasShadow && opacity !== 1 && hasShadowOpacityBug()) {
|
|
607
681
|
return true;
|
|
608
682
|
}
|
|
609
|
-
return super._useBufferCanvas();
|
|
683
|
+
return super._useBufferCanvas(forceFill, opacity);
|
|
610
684
|
}
|
|
611
685
|
}
|
|
612
686
|
Text.prototype._fillFunc = _fillFunc;
|
|
613
687
|
Text.prototype._strokeFunc = _strokeFunc;
|
|
614
688
|
Text.prototype.className = TEXT_UPPER;
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
'wrap',
|
|
620
|
-
'lineHeight',
|
|
621
|
-
'letterSpacing',
|
|
622
|
-
];
|
|
623
|
-
_registerNode(Text);
|
|
689
|
+
// the decoration attrs are not in ATTR_CHANGE_LIST: they change the bounds
|
|
690
|
+
// through getSelfRect, but need no relayout
|
|
691
|
+
Text.prototype._attrsAffectingSize = ATTR_CHANGE_LIST.filter((attr) => attr !== 'width' && attr !== 'height').concat(['textDecoration', 'underlineOffset']);
|
|
692
|
+
_registerNode(Text, true);
|
|
624
693
|
// update text data for certain attr changes
|
|
625
694
|
Text.prototype.on(ATTR_CHANGE_LIST.map((attr) => attr + CHANGE_KONVA).join(' '), function () {
|
|
626
695
|
this._setTextData();
|
|
@@ -880,7 +949,7 @@ Factory.addGetterSetter(Text, 'textDecoration', '');
|
|
|
880
949
|
*/
|
|
881
950
|
Factory.addGetterSetter(Text, 'underlineOffset', undefined, getNumberValidator());
|
|
882
951
|
/**
|
|
883
|
-
* get/set per-character render hook. The callback is invoked for each grapheme before drawing.
|
|
952
|
+
* get/set per-character render hook. The callback is invoked for each grapheme before drawing, except for RTL text.
|
|
884
953
|
* It can mutate the provided context (e.g. translate, rotate, change styles) and should return void.
|
|
885
954
|
* Note: per-character rendering may disable native kerning/ligatures.
|
|
886
955
|
* @name Konva.Text#charRenderFunc
|
package/lib/shapes/TextPath.d.ts
CHANGED
|
@@ -64,8 +64,6 @@ export interface TextPathConfig extends ShapeConfig {
|
|
|
64
64
|
export declare class TextPath extends Shape<TextPathConfig> {
|
|
65
65
|
dataArray: PathSegment[];
|
|
66
66
|
glyphInfo: Array<{
|
|
67
|
-
transposeX: number;
|
|
68
|
-
transposeY: number;
|
|
69
67
|
text: string;
|
|
70
68
|
rotation: number;
|
|
71
69
|
p0: Vector2d;
|
|
@@ -78,7 +76,10 @@ export declare class TextPath extends Shape<TextPathConfig> {
|
|
|
78
76
|
textHeight: number;
|
|
79
77
|
constructor(config?: TextPathConfig);
|
|
80
78
|
_getTextPathLength(): number;
|
|
81
|
-
_getPointAtLength(length: number
|
|
79
|
+
_getPointAtLength(length: number, cursor?: {
|
|
80
|
+
index: number;
|
|
81
|
+
offset: number;
|
|
82
|
+
}): {
|
|
82
83
|
x: number;
|
|
83
84
|
y: number;
|
|
84
85
|
} | null;
|
|
@@ -105,6 +106,13 @@ export declare class TextPath extends Shape<TextPathConfig> {
|
|
|
105
106
|
width: number;
|
|
106
107
|
height: number;
|
|
107
108
|
};
|
|
109
|
+
getStrokeScaleEnabled(): boolean;
|
|
110
|
+
_getSelfRectForDrawing(): {
|
|
111
|
+
x: number;
|
|
112
|
+
y: number;
|
|
113
|
+
width: number;
|
|
114
|
+
height: number;
|
|
115
|
+
};
|
|
108
116
|
fontFamily: GetSet<string, this>;
|
|
109
117
|
fontSize: GetSet<number, this>;
|
|
110
118
|
fontStyle: GetSet<string, this>;
|
package/lib/shapes/TextPath.js
CHANGED
|
@@ -67,7 +67,7 @@ export class TextPath extends Shape {
|
|
|
67
67
|
_getTextPathLength() {
|
|
68
68
|
return Path.getPathLength(this.dataArray);
|
|
69
69
|
}
|
|
70
|
-
_getPointAtLength(length) {
|
|
70
|
+
_getPointAtLength(length, cursor) {
|
|
71
71
|
// if path is not defined yet, do nothing
|
|
72
72
|
if (!this.attrs.data) {
|
|
73
73
|
return null;
|
|
@@ -76,7 +76,7 @@ export class TextPath extends Shape {
|
|
|
76
76
|
if (length > totalLength) {
|
|
77
77
|
return null;
|
|
78
78
|
}
|
|
79
|
-
return Path.getPointAtLengthOfDataArray(length, this.dataArray);
|
|
79
|
+
return Path.getPointAtLengthOfDataArray(length, this.dataArray, cursor);
|
|
80
80
|
}
|
|
81
81
|
_readDataAttribute() {
|
|
82
82
|
this.dataArray = Path.parsePathData(this.attrs.data);
|
|
@@ -180,6 +180,8 @@ export class TextPath extends Shape {
|
|
|
180
180
|
const _context = getDummyContext();
|
|
181
181
|
_context.save();
|
|
182
182
|
_context.font = this._getContextFont();
|
|
183
|
+
// Text leaves its own kerning mode on the shared dummy context
|
|
184
|
+
_context.fontKerning = 'auto';
|
|
183
185
|
const metrics = _context.measureText(text);
|
|
184
186
|
_context.restore();
|
|
185
187
|
return {
|
|
@@ -198,14 +200,24 @@ export class TextPath extends Shape {
|
|
|
198
200
|
if (this.direction() === 'rtl') {
|
|
199
201
|
charArr.reverse();
|
|
200
202
|
}
|
|
203
|
+
const kerningFunc = this.kerningFunc();
|
|
201
204
|
const chars = [];
|
|
202
205
|
let width = 0;
|
|
203
206
|
for (let i = 0; i < charArr.length; i++) {
|
|
207
|
+
let kern = 0;
|
|
208
|
+
if (kerningFunc && i > 0) {
|
|
209
|
+
try {
|
|
210
|
+
// kerningFunc is a user provided getter. Make sure it never breaks our logic
|
|
211
|
+
kern = kerningFunc(charArr[i - 1], charArr[i]) * this.fontSize();
|
|
212
|
+
}
|
|
213
|
+
catch (e) { }
|
|
214
|
+
}
|
|
204
215
|
chars.push({
|
|
205
216
|
char: charArr[i],
|
|
206
217
|
width: this._getTextSize(charArr[i]).width,
|
|
218
|
+
kern: kern,
|
|
207
219
|
});
|
|
208
|
-
width += chars[i].width;
|
|
220
|
+
width += chars[i].width + kern;
|
|
209
221
|
}
|
|
210
222
|
const { width: fullTextWidth, height } = this._getTextSize(this.attrs.text);
|
|
211
223
|
this.textWidth = width;
|
|
@@ -216,7 +228,7 @@ export class TextPath extends Shape {
|
|
|
216
228
|
}
|
|
217
229
|
const letterSpacing = this.letterSpacing();
|
|
218
230
|
const align = this.align();
|
|
219
|
-
const
|
|
231
|
+
const numberOfSpaces = align === 'justify' ? this.text().split(' ').length - 1 : 0;
|
|
220
232
|
// The sum of individual character widths can exceed the whole-string width
|
|
221
233
|
// due to kerning (browsers place adjacent glyphs closer together than the
|
|
222
234
|
// individual measurements suggest). This difference is the maximum amount
|
|
@@ -238,14 +250,18 @@ export class TextPath extends Shape {
|
|
|
238
250
|
// 3. Calculate the rotation, width, and midpoint of the glyph using the start and end points,
|
|
239
251
|
// 4. Add glyph width to the offsetToGlyph and repeat
|
|
240
252
|
let offsetToGlyph = offset;
|
|
253
|
+
// Keep the original boundary selection across gaps between subpaths.
|
|
254
|
+
const cursor = this.dataArray.some((segment, index) => index > 0 && segment.command === 'M')
|
|
255
|
+
? undefined
|
|
256
|
+
: { index: 0, offset: 0 };
|
|
241
257
|
for (let i = 0; i < chars.length; i++) {
|
|
242
|
-
|
|
258
|
+
offsetToGlyph += chars[i].kern;
|
|
259
|
+
const charStartPoint = this._getPointAtLength(offsetToGlyph, cursor);
|
|
243
260
|
if (!charStartPoint)
|
|
244
261
|
return;
|
|
245
262
|
const char = chars[i].char;
|
|
246
|
-
let glyphWidth = chars[i].width + letterSpacing;
|
|
263
|
+
let glyphWidth = chars[i].width + (i < chars.length - 1 ? letterSpacing : 0);
|
|
247
264
|
if (char === ' ' && align === 'justify') {
|
|
248
|
-
const numberOfSpaces = this.text().split(' ').length - 1;
|
|
249
265
|
glyphWidth += (this.pathLength - textWidth) / numberOfSpaces;
|
|
250
266
|
}
|
|
251
267
|
const charEndLength = offsetToGlyph + glyphWidth;
|
|
@@ -256,29 +272,13 @@ export class TextPath extends Shape {
|
|
|
256
272
|
const charEndPoint = this._getPointAtLength(charEndLength > this.pathLength &&
|
|
257
273
|
charEndLength - this.pathLength <= kerningAdjustment
|
|
258
274
|
? this.pathLength
|
|
259
|
-
: charEndLength);
|
|
275
|
+
: charEndLength, cursor);
|
|
260
276
|
if (!charEndPoint) {
|
|
261
277
|
return;
|
|
262
278
|
}
|
|
263
279
|
const width = Path.getLineLength(charStartPoint.x, charStartPoint.y, charEndPoint.x, charEndPoint.y);
|
|
264
|
-
let kern = 0;
|
|
265
|
-
if (kerningFunc && i > 0) {
|
|
266
|
-
try {
|
|
267
|
-
// getKerning is a user provided getter. Make sure it never breaks our logic
|
|
268
|
-
kern = kerningFunc(chars[i - 1].char, char) * this.fontSize();
|
|
269
|
-
}
|
|
270
|
-
catch (e) {
|
|
271
|
-
kern = 0;
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
charStartPoint.x += kern;
|
|
275
|
-
charEndPoint.x += kern;
|
|
276
|
-
this.textWidth += kern;
|
|
277
|
-
const midpoint = Path.getPointOnLine(kern + width / 2.0, charStartPoint.x, charStartPoint.y, charEndPoint.x, charEndPoint.y);
|
|
278
280
|
const rotation = Math.atan2(charEndPoint.y - charStartPoint.y, charEndPoint.x - charStartPoint.x);
|
|
279
281
|
this.glyphInfo.push({
|
|
280
|
-
transposeX: midpoint.x,
|
|
281
|
-
transposeY: midpoint.y,
|
|
282
282
|
text: charArr[i],
|
|
283
283
|
rotation: rotation,
|
|
284
284
|
p0: charStartPoint,
|
|
@@ -313,20 +313,49 @@ export class TextPath extends Shape {
|
|
|
313
313
|
height: rect.height + fontSize,
|
|
314
314
|
};
|
|
315
315
|
}
|
|
316
|
+
// Like Text: a glyph stroke is drawn with the text, so it always scales.
|
|
317
|
+
getStrokeScaleEnabled() {
|
|
318
|
+
return true;
|
|
319
|
+
}
|
|
320
|
+
_getSelfRectForDrawing() {
|
|
321
|
+
// getSelfRect() pads glyph anchors by half the font size; ascenders,
|
|
322
|
+
// accents and other baselines reach further from the path.
|
|
323
|
+
const rect = this.getSelfRect(), pad = this.fontSize();
|
|
324
|
+
return {
|
|
325
|
+
x: rect.x - pad,
|
|
326
|
+
y: rect.y - pad,
|
|
327
|
+
width: rect.width + pad * 2,
|
|
328
|
+
height: rect.height + pad * 2,
|
|
329
|
+
};
|
|
330
|
+
}
|
|
316
331
|
}
|
|
317
332
|
TextPath.prototype._fillFunc = _fillFunc;
|
|
318
333
|
TextPath.prototype._strokeFunc = _strokeFunc;
|
|
319
334
|
TextPath.prototype._fillFuncHit = _fillFunc;
|
|
320
335
|
TextPath.prototype._strokeFuncHit = _strokeFunc;
|
|
321
336
|
TextPath.prototype.className = 'TextPath';
|
|
322
|
-
TextPath.prototype._attrsAffectingSize = [
|
|
323
|
-
|
|
337
|
+
TextPath.prototype._attrsAffectingSize = [
|
|
338
|
+
'text',
|
|
339
|
+
'fontSize',
|
|
340
|
+
'data',
|
|
341
|
+
'align',
|
|
342
|
+
'letterSpacing',
|
|
343
|
+
'kerningFunc',
|
|
344
|
+
'fontFamily',
|
|
345
|
+
'fontStyle',
|
|
346
|
+
'fontVariant',
|
|
347
|
+
'direction',
|
|
348
|
+
];
|
|
349
|
+
_registerNode(TextPath, true);
|
|
324
350
|
TextPath.prototype.on('dataChange.konva', function () {
|
|
325
351
|
this._readDataAttribute();
|
|
326
352
|
this._setTextData();
|
|
327
353
|
});
|
|
328
354
|
// update text data for certain attr changes
|
|
329
|
-
TextPath.prototype.on(
|
|
355
|
+
TextPath.prototype.on(TextPath.prototype._attrsAffectingSize
|
|
356
|
+
.filter((attr) => attr !== 'data')
|
|
357
|
+
.map((attr) => attr + 'Change.konva')
|
|
358
|
+
.join(' '), function () {
|
|
330
359
|
this._setTextData();
|
|
331
360
|
});
|
|
332
361
|
/**
|