@nasser-sw/fabric 7.0.1-beta7 → 7.0.1-beta9
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/debug/konva-master/CHANGELOG.md +1475 -0
- package/debug/konva-master/LICENSE +22 -0
- package/debug/konva-master/README.md +209 -0
- package/debug/konva-master/gulpfile.mjs +110 -0
- package/debug/konva-master/package.json +139 -0
- package/debug/konva-master/release.sh +62 -0
- package/debug/konva-master/resources/doc-includes/ContainerParams.txt +6 -0
- package/debug/konva-master/resources/doc-includes/NodeParams.txt +20 -0
- package/debug/konva-master/resources/doc-includes/ShapeParams.txt +53 -0
- package/debug/konva-master/resources/jsdoc.conf.json +28 -0
- package/debug/konva-master/rollup.config.mjs +32 -0
- package/debug/konva-master/src/Animation.ts +237 -0
- package/debug/konva-master/src/BezierFunctions.ts +826 -0
- package/debug/konva-master/src/Canvas.ts +230 -0
- package/debug/konva-master/src/Container.ts +649 -0
- package/debug/konva-master/src/Context.ts +1017 -0
- package/debug/konva-master/src/Core.ts +5 -0
- package/debug/konva-master/src/DragAndDrop.ts +173 -0
- package/debug/konva-master/src/Factory.ts +246 -0
- package/debug/konva-master/src/FastLayer.ts +29 -0
- package/debug/konva-master/src/Global.ts +210 -0
- package/debug/konva-master/src/Group.ts +31 -0
- package/debug/konva-master/src/Layer.ts +546 -0
- package/debug/konva-master/src/Node.ts +3477 -0
- package/debug/konva-master/src/PointerEvents.ts +67 -0
- package/debug/konva-master/src/Shape.ts +2081 -0
- package/debug/konva-master/src/Stage.ts +1000 -0
- package/debug/konva-master/src/Tween.ts +811 -0
- package/debug/konva-master/src/Util.ts +1123 -0
- package/debug/konva-master/src/Validators.ts +210 -0
- package/debug/konva-master/src/_CoreInternals.ts +85 -0
- package/debug/konva-master/src/_FullInternals.ts +171 -0
- package/debug/konva-master/src/canvas-backend.ts +36 -0
- package/debug/konva-master/src/filters/Blur.ts +388 -0
- package/debug/konva-master/src/filters/Brighten.ts +48 -0
- package/debug/konva-master/src/filters/Brightness.ts +30 -0
- package/debug/konva-master/src/filters/Contrast.ts +75 -0
- package/debug/konva-master/src/filters/Emboss.ts +207 -0
- package/debug/konva-master/src/filters/Enhance.ts +154 -0
- package/debug/konva-master/src/filters/Grayscale.ts +25 -0
- package/debug/konva-master/src/filters/HSL.ts +108 -0
- package/debug/konva-master/src/filters/HSV.ts +106 -0
- package/debug/konva-master/src/filters/Invert.ts +23 -0
- package/debug/konva-master/src/filters/Kaleidoscope.ts +274 -0
- package/debug/konva-master/src/filters/Mask.ts +220 -0
- package/debug/konva-master/src/filters/Noise.ts +44 -0
- package/debug/konva-master/src/filters/Pixelate.ts +107 -0
- package/debug/konva-master/src/filters/Posterize.ts +46 -0
- package/debug/konva-master/src/filters/RGB.ts +82 -0
- package/debug/konva-master/src/filters/RGBA.ts +103 -0
- package/debug/konva-master/src/filters/Sepia.ts +27 -0
- package/debug/konva-master/src/filters/Solarize.ts +29 -0
- package/debug/konva-master/src/filters/Threshold.ts +44 -0
- package/debug/konva-master/src/index.ts +3 -0
- package/debug/konva-master/src/shapes/Arc.ts +176 -0
- package/debug/konva-master/src/shapes/Arrow.ts +231 -0
- package/debug/konva-master/src/shapes/Circle.ts +76 -0
- package/debug/konva-master/src/shapes/Ellipse.ts +121 -0
- package/debug/konva-master/src/shapes/Image.ts +319 -0
- package/debug/konva-master/src/shapes/Label.ts +386 -0
- package/debug/konva-master/src/shapes/Line.ts +364 -0
- package/debug/konva-master/src/shapes/Path.ts +1013 -0
- package/debug/konva-master/src/shapes/Rect.ts +79 -0
- package/debug/konva-master/src/shapes/RegularPolygon.ts +167 -0
- package/debug/konva-master/src/shapes/Ring.ts +94 -0
- package/debug/konva-master/src/shapes/Sprite.ts +370 -0
- package/debug/konva-master/src/shapes/Star.ts +125 -0
- package/debug/konva-master/src/shapes/Text.ts +1065 -0
- package/debug/konva-master/src/shapes/TextPath.ts +583 -0
- package/debug/konva-master/src/shapes/Transformer.ts +1889 -0
- package/debug/konva-master/src/shapes/Wedge.ts +129 -0
- package/debug/konva-master/src/skia-backend.ts +35 -0
- package/debug/konva-master/src/types.ts +84 -0
- package/debug/konva-master/tsconfig.json +31 -0
- package/debug/konva-master/tsconfig.test.json +7 -0
- package/dist/index.js +977 -29
- package/dist/index.js.map +1 -1
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +1 -1
- package/dist/index.min.mjs +1 -1
- package/dist/index.min.mjs.map +1 -1
- package/dist/index.mjs +977 -29
- package/dist/index.mjs.map +1 -1
- package/dist/index.node.cjs +977 -29
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.mjs +977 -29
- package/dist/index.node.mjs.map +1 -1
- package/dist/package.json.min.mjs +1 -1
- package/dist/package.json.mjs +1 -1
- package/dist/src/shapes/Line.d.ts +1 -0
- package/dist/src/shapes/Line.d.ts.map +1 -1
- package/dist/src/shapes/Line.min.mjs +1 -1
- package/dist/src/shapes/Line.min.mjs.map +1 -1
- package/dist/src/shapes/Line.mjs +63 -6
- package/dist/src/shapes/Line.mjs.map +1 -1
- package/dist/src/shapes/Text/Text.d.ts +19 -0
- package/dist/src/shapes/Text/Text.d.ts.map +1 -1
- package/dist/src/shapes/Text/Text.min.mjs +1 -1
- package/dist/src/shapes/Text/Text.min.mjs.map +1 -1
- package/dist/src/shapes/Text/Text.mjs +238 -4
- package/dist/src/shapes/Text/Text.mjs.map +1 -1
- package/dist/src/shapes/Textbox.d.ts +38 -1
- package/dist/src/shapes/Textbox.d.ts.map +1 -1
- package/dist/src/shapes/Textbox.min.mjs +1 -1
- package/dist/src/shapes/Textbox.min.mjs.map +1 -1
- package/dist/src/shapes/Textbox.mjs +497 -15
- package/dist/src/shapes/Textbox.mjs.map +1 -1
- package/dist/src/text/examples/arabicTextExample.d.ts +60 -0
- package/dist/src/text/examples/arabicTextExample.d.ts.map +1 -0
- package/dist/src/text/measure.d.ts +9 -0
- package/dist/src/text/measure.d.ts.map +1 -1
- package/dist/src/text/measure.min.mjs +1 -1
- package/dist/src/text/measure.min.mjs.map +1 -1
- package/dist/src/text/measure.mjs +175 -4
- package/dist/src/text/measure.mjs.map +1 -1
- package/dist/src/text/overlayEditor.d.ts.map +1 -1
- package/dist/src/text/overlayEditor.min.mjs +1 -1
- package/dist/src/text/overlayEditor.min.mjs.map +1 -1
- package/dist/src/text/overlayEditor.mjs +7 -0
- package/dist/src/text/overlayEditor.mjs.map +1 -1
- package/dist/src/text/scriptUtils.d.ts +142 -0
- package/dist/src/text/scriptUtils.d.ts.map +1 -0
- package/dist/src/text/scriptUtils.min.mjs +2 -0
- package/dist/src/text/scriptUtils.min.mjs.map +1 -0
- package/dist/src/text/scriptUtils.mjs +212 -0
- package/dist/src/text/scriptUtils.mjs.map +1 -0
- package/dist-extensions/src/shapes/Line.d.ts +1 -0
- package/dist-extensions/src/shapes/Line.d.ts.map +1 -1
- package/dist-extensions/src/shapes/Text/Text.d.ts +19 -0
- package/dist-extensions/src/shapes/Text/Text.d.ts.map +1 -1
- package/dist-extensions/src/shapes/Textbox.d.ts +38 -1
- package/dist-extensions/src/shapes/Textbox.d.ts.map +1 -1
- package/dist-extensions/src/text/measure.d.ts +9 -0
- package/dist-extensions/src/text/measure.d.ts.map +1 -1
- package/dist-extensions/src/text/overlayEditor.d.ts.map +1 -1
- package/dist-extensions/src/text/scriptUtils.d.ts +142 -0
- package/dist-extensions/src/text/scriptUtils.d.ts.map +1 -0
- package/fabric-test-editor.html +2401 -46
- package/fabric-test2.html +43 -0
- package/fonts/STV Bold.ttf +0 -0
- package/fonts/STV Light.ttf +0 -0
- package/fonts/STV Regular.ttf +0 -0
- package/package.json +1 -1
- package/src/shapes/Line.ts +132 -46
- package/src/shapes/Text/Text.ts +238 -5
- package/src/shapes/Textbox.ts +521 -11
- package/src/text/measure.ts +200 -50
- package/src/text/overlayEditor.ts +7 -0
|
@@ -0,0 +1,583 @@
|
|
|
1
|
+
import { Util } from '../Util.ts';
|
|
2
|
+
import { Factory } from '../Factory.ts';
|
|
3
|
+
import type { Context } from '../Context.ts';
|
|
4
|
+
import type { ShapeConfig } from '../Shape.ts';
|
|
5
|
+
import { Shape } from '../Shape.ts';
|
|
6
|
+
import { Path } from './Path.ts';
|
|
7
|
+
import { Text, stringToArray } from './Text.ts';
|
|
8
|
+
import { getNumberValidator } from '../Validators.ts';
|
|
9
|
+
import { _registerNode } from '../Global.ts';
|
|
10
|
+
|
|
11
|
+
import type { GetSet, PathSegment, Vector2d } from '../types.ts';
|
|
12
|
+
|
|
13
|
+
export interface TextPathConfig extends ShapeConfig {
|
|
14
|
+
text?: string;
|
|
15
|
+
data?: string;
|
|
16
|
+
fontFamily?: string;
|
|
17
|
+
fontSize?: number;
|
|
18
|
+
fontStyle?: string;
|
|
19
|
+
letterSpacing?: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const EMPTY_STRING = '',
|
|
23
|
+
NORMAL = 'normal';
|
|
24
|
+
|
|
25
|
+
function _fillFunc(this: TextPath, context) {
|
|
26
|
+
context.fillText(this.partialText, 0, 0);
|
|
27
|
+
}
|
|
28
|
+
function _strokeFunc(this: TextPath, context) {
|
|
29
|
+
context.strokeText(this.partialText, 0, 0);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Path constructor.
|
|
34
|
+
* @author Jason Follas
|
|
35
|
+
* @constructor
|
|
36
|
+
* @memberof Konva
|
|
37
|
+
* @augments Konva.Shape
|
|
38
|
+
* @param {Object} config
|
|
39
|
+
* @param {String} [config.fontFamily] default is Arial
|
|
40
|
+
* @param {Number} [config.fontSize] default is 12
|
|
41
|
+
* @param {String} [config.fontStyle] Can be 'normal', 'italic', or 'bold', '500' or even 'italic bold'. 'normal' is the default.
|
|
42
|
+
* @param {String} [config.fontVariant] can be normal or small-caps. Default is normal
|
|
43
|
+
* @param {String} [config.textBaseline] Can be 'top', 'bottom', 'middle', 'alphabetic', 'hanging'. Default is middle
|
|
44
|
+
* @param {String} config.text
|
|
45
|
+
* @param {String} config.data SVG data string
|
|
46
|
+
* @param {Function} config.kerningFunc a getter for kerning values for the specified characters
|
|
47
|
+
* @@shapeParams
|
|
48
|
+
* @@nodeParams
|
|
49
|
+
* @example
|
|
50
|
+
* var kerningPairs = {
|
|
51
|
+
* 'A': {
|
|
52
|
+
* ' ': -0.05517578125,
|
|
53
|
+
* 'T': -0.07421875,
|
|
54
|
+
* 'V': -0.07421875
|
|
55
|
+
* }
|
|
56
|
+
* 'V': {
|
|
57
|
+
* ',': -0.091796875,
|
|
58
|
+
* ":": -0.037109375,
|
|
59
|
+
* ";": -0.037109375,
|
|
60
|
+
* "A": -0.07421875
|
|
61
|
+
* }
|
|
62
|
+
* }
|
|
63
|
+
* var textpath = new Konva.TextPath({
|
|
64
|
+
* x: 100,
|
|
65
|
+
* y: 50,
|
|
66
|
+
* fill: '#333',
|
|
67
|
+
* fontSize: '24',
|
|
68
|
+
* fontFamily: 'Arial',
|
|
69
|
+
* text: 'All the world\'s a stage, and all the men and women merely players.',
|
|
70
|
+
* data: 'M10,10 C0,0 10,150 100,100 S300,150 400,50',
|
|
71
|
+
* kerningFunc(leftChar, rightChar) {
|
|
72
|
+
* return kerningPairs.hasOwnProperty(leftChar) ? pairs[leftChar][rightChar] || 0 : 0
|
|
73
|
+
* }
|
|
74
|
+
* });
|
|
75
|
+
*/
|
|
76
|
+
export class TextPath extends Shape<TextPathConfig> {
|
|
77
|
+
dummyCanvas = Util.createCanvasElement();
|
|
78
|
+
dataArray: PathSegment[] = [];
|
|
79
|
+
glyphInfo: Array<{
|
|
80
|
+
transposeX: number;
|
|
81
|
+
transposeY: number;
|
|
82
|
+
text: string;
|
|
83
|
+
rotation: number;
|
|
84
|
+
p0: Vector2d;
|
|
85
|
+
p1: Vector2d;
|
|
86
|
+
}>;
|
|
87
|
+
partialText: string;
|
|
88
|
+
pathLength: number;
|
|
89
|
+
textWidth: number;
|
|
90
|
+
textHeight: number;
|
|
91
|
+
|
|
92
|
+
constructor(config?: TextPathConfig) {
|
|
93
|
+
// call super constructor
|
|
94
|
+
super(config);
|
|
95
|
+
|
|
96
|
+
this._readDataAttribute();
|
|
97
|
+
|
|
98
|
+
this.on('dataChange.konva', function () {
|
|
99
|
+
this._readDataAttribute();
|
|
100
|
+
this._setTextData();
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
// update text data for certain attr changes
|
|
104
|
+
this.on(
|
|
105
|
+
'textChange.konva alignChange.konva letterSpacingChange.konva kerningFuncChange.konva fontSizeChange.konva fontFamilyChange.konva',
|
|
106
|
+
this._setTextData
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
this._setTextData();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
_getTextPathLength() {
|
|
113
|
+
return Path.getPathLength(this.dataArray);
|
|
114
|
+
}
|
|
115
|
+
_getPointAtLength(length: number) {
|
|
116
|
+
// if path is not defined yet, do nothing
|
|
117
|
+
if (!this.attrs.data) {
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const totalLength = this.pathLength;
|
|
122
|
+
// -1px for rounding of the last symbol
|
|
123
|
+
if (length - 1 > totalLength) {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return Path.getPointAtLengthOfDataArray(length, this.dataArray);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
_readDataAttribute() {
|
|
131
|
+
this.dataArray = Path.parsePathData(this.attrs.data);
|
|
132
|
+
this.pathLength = this._getTextPathLength();
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
_sceneFunc(context: Context) {
|
|
136
|
+
context.setAttr('font', this._getContextFont());
|
|
137
|
+
context.setAttr('textBaseline', this.textBaseline());
|
|
138
|
+
context.setAttr('textAlign', 'left');
|
|
139
|
+
context.save();
|
|
140
|
+
|
|
141
|
+
const textDecoration = this.textDecoration();
|
|
142
|
+
const fill = this.fill();
|
|
143
|
+
const fontSize = this.fontSize();
|
|
144
|
+
|
|
145
|
+
const glyphInfo = this.glyphInfo;
|
|
146
|
+
if (textDecoration === 'underline') {
|
|
147
|
+
context.beginPath();
|
|
148
|
+
}
|
|
149
|
+
for (let i = 0; i < glyphInfo.length; i++) {
|
|
150
|
+
context.save();
|
|
151
|
+
|
|
152
|
+
const p0 = glyphInfo[i].p0;
|
|
153
|
+
|
|
154
|
+
context.translate(p0.x, p0.y);
|
|
155
|
+
context.rotate(glyphInfo[i].rotation);
|
|
156
|
+
this.partialText = glyphInfo[i].text;
|
|
157
|
+
|
|
158
|
+
context.fillStrokeShape(this);
|
|
159
|
+
if (textDecoration === 'underline') {
|
|
160
|
+
if (i === 0) {
|
|
161
|
+
context.moveTo(0, fontSize / 2 + 1);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
context.lineTo(fontSize, fontSize / 2 + 1);
|
|
165
|
+
}
|
|
166
|
+
context.restore();
|
|
167
|
+
|
|
168
|
+
//// To assist with debugging visually, uncomment following
|
|
169
|
+
//
|
|
170
|
+
// if (i % 2) context.strokeStyle = 'cyan';
|
|
171
|
+
// else context.strokeStyle = 'green';
|
|
172
|
+
// var p1 = glyphInfo[i].p1;
|
|
173
|
+
// context.moveTo(p0.x, p0.y);
|
|
174
|
+
// context.lineTo(p1.x, p1.y);
|
|
175
|
+
// context.stroke();
|
|
176
|
+
}
|
|
177
|
+
if (textDecoration === 'underline') {
|
|
178
|
+
context.strokeStyle = fill;
|
|
179
|
+
context.lineWidth = fontSize / 20;
|
|
180
|
+
context.stroke();
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
context.restore();
|
|
184
|
+
}
|
|
185
|
+
_hitFunc(context: Context) {
|
|
186
|
+
context.beginPath();
|
|
187
|
+
|
|
188
|
+
const glyphInfo = this.glyphInfo;
|
|
189
|
+
if (glyphInfo.length >= 1) {
|
|
190
|
+
const p0 = glyphInfo[0].p0;
|
|
191
|
+
context.moveTo(p0.x, p0.y);
|
|
192
|
+
}
|
|
193
|
+
for (let i = 0; i < glyphInfo.length; i++) {
|
|
194
|
+
const p1 = glyphInfo[i].p1;
|
|
195
|
+
context.lineTo(p1.x, p1.y);
|
|
196
|
+
}
|
|
197
|
+
context.setAttr('lineWidth', this.fontSize());
|
|
198
|
+
context.setAttr('strokeStyle', this.colorKey);
|
|
199
|
+
context.stroke();
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* get text width in pixels
|
|
203
|
+
* @method
|
|
204
|
+
* @name Konva.TextPath#getTextWidth
|
|
205
|
+
*/
|
|
206
|
+
getTextWidth() {
|
|
207
|
+
return this.textWidth;
|
|
208
|
+
}
|
|
209
|
+
getTextHeight() {
|
|
210
|
+
Util.warn(
|
|
211
|
+
'text.getTextHeight() method is deprecated. Use text.height() - for full height and text.fontSize() - for one line height.'
|
|
212
|
+
);
|
|
213
|
+
return this.textHeight;
|
|
214
|
+
}
|
|
215
|
+
setText(text: string) {
|
|
216
|
+
return Text.prototype.setText.call(this, text);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
_getContextFont() {
|
|
220
|
+
return Text.prototype._getContextFont.call(this);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
_getTextSize(text: string) {
|
|
224
|
+
const dummyCanvas = this.dummyCanvas;
|
|
225
|
+
const _context = dummyCanvas.getContext('2d')!;
|
|
226
|
+
|
|
227
|
+
_context.save();
|
|
228
|
+
|
|
229
|
+
_context.font = this._getContextFont();
|
|
230
|
+
const metrics = _context.measureText(text);
|
|
231
|
+
|
|
232
|
+
_context.restore();
|
|
233
|
+
|
|
234
|
+
return {
|
|
235
|
+
width: metrics.width,
|
|
236
|
+
height: parseInt(`${this.fontSize()}`, 10),
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
_setTextData() {
|
|
240
|
+
const charArr = stringToArray(this.text());
|
|
241
|
+
const chars: { char: string; width: number }[] = [];
|
|
242
|
+
let width = 0;
|
|
243
|
+
for (let i = 0; i < charArr.length; i++) {
|
|
244
|
+
chars.push({
|
|
245
|
+
char: charArr[i],
|
|
246
|
+
width: this._getTextSize(charArr[i]).width,
|
|
247
|
+
});
|
|
248
|
+
width += chars[i].width;
|
|
249
|
+
}
|
|
250
|
+
const { height } = this._getTextSize(this.attrs.text);
|
|
251
|
+
this.textWidth = width;
|
|
252
|
+
this.textHeight = height;
|
|
253
|
+
this.glyphInfo = [];
|
|
254
|
+
|
|
255
|
+
if (!this.attrs.data) {
|
|
256
|
+
return null;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const letterSpacing = this.letterSpacing();
|
|
260
|
+
const align = this.align();
|
|
261
|
+
const kerningFunc = this.kerningFunc();
|
|
262
|
+
|
|
263
|
+
// defines the width of the text on a straight line
|
|
264
|
+
const textWidth = Math.max(
|
|
265
|
+
this.textWidth + ((this.attrs.text || '').length - 1) * letterSpacing,
|
|
266
|
+
0
|
|
267
|
+
);
|
|
268
|
+
|
|
269
|
+
let offset = 0;
|
|
270
|
+
if (align === 'center') {
|
|
271
|
+
offset = Math.max(0, this.pathLength / 2 - textWidth / 2);
|
|
272
|
+
}
|
|
273
|
+
if (align === 'right') {
|
|
274
|
+
offset = Math.max(0, this.pathLength - textWidth);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// Algorithm for calculating glyph positions:
|
|
278
|
+
// 1. Get the begging point of the glyph on the path using the offsetToGlyph,
|
|
279
|
+
// 2. Get the ending point of the glyph on the path using the offsetToGlyph plus glyph width,
|
|
280
|
+
// 3. Calculate the rotation, width, and midpoint of the glyph using the start and end points,
|
|
281
|
+
// 4. Add glyph width to the offsetToGlyph and repeat
|
|
282
|
+
let offsetToGlyph = offset;
|
|
283
|
+
for (let i = 0; i < chars.length; i++) {
|
|
284
|
+
const charStartPoint = this._getPointAtLength(offsetToGlyph);
|
|
285
|
+
if (!charStartPoint) return;
|
|
286
|
+
|
|
287
|
+
const char = chars[i].char;
|
|
288
|
+
let glyphWidth = chars[i].width + letterSpacing;
|
|
289
|
+
if (char === ' ' && align === 'justify') {
|
|
290
|
+
const numberOfSpaces = this.text().split(' ').length - 1;
|
|
291
|
+
glyphWidth += (this.pathLength - textWidth) / numberOfSpaces;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const charEndPoint = this._getPointAtLength(offsetToGlyph + glyphWidth);
|
|
295
|
+
if (!charEndPoint) {
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const width = Path.getLineLength(
|
|
300
|
+
charStartPoint.x,
|
|
301
|
+
charStartPoint.y,
|
|
302
|
+
charEndPoint.x,
|
|
303
|
+
charEndPoint.y
|
|
304
|
+
);
|
|
305
|
+
|
|
306
|
+
let kern = 0;
|
|
307
|
+
if (kerningFunc) {
|
|
308
|
+
try {
|
|
309
|
+
// getKerning is a user provided getter. Make sure it never breaks our logic
|
|
310
|
+
kern = kerningFunc(chars[i - 1].char, char) * this.fontSize();
|
|
311
|
+
} catch (e) {
|
|
312
|
+
kern = 0;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
charStartPoint.x += kern;
|
|
317
|
+
charEndPoint.x += kern;
|
|
318
|
+
this.textWidth += kern;
|
|
319
|
+
|
|
320
|
+
const midpoint = Path.getPointOnLine(
|
|
321
|
+
kern + width / 2.0,
|
|
322
|
+
charStartPoint.x,
|
|
323
|
+
charStartPoint.y,
|
|
324
|
+
charEndPoint.x,
|
|
325
|
+
charEndPoint.y
|
|
326
|
+
);
|
|
327
|
+
|
|
328
|
+
const rotation = Math.atan2(
|
|
329
|
+
charEndPoint.y - charStartPoint.y,
|
|
330
|
+
charEndPoint.x - charStartPoint.x
|
|
331
|
+
);
|
|
332
|
+
this.glyphInfo.push({
|
|
333
|
+
transposeX: midpoint.x,
|
|
334
|
+
transposeY: midpoint.y,
|
|
335
|
+
text: charArr[i],
|
|
336
|
+
rotation: rotation,
|
|
337
|
+
p0: charStartPoint,
|
|
338
|
+
p1: charEndPoint,
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
offsetToGlyph += glyphWidth;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
getSelfRect() {
|
|
345
|
+
if (!this.glyphInfo.length) {
|
|
346
|
+
return {
|
|
347
|
+
x: 0,
|
|
348
|
+
y: 0,
|
|
349
|
+
width: 0,
|
|
350
|
+
height: 0,
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
const points: number[] = [];
|
|
354
|
+
|
|
355
|
+
this.glyphInfo.forEach(function (info) {
|
|
356
|
+
points.push(info.p0.x);
|
|
357
|
+
points.push(info.p0.y);
|
|
358
|
+
points.push(info.p1.x);
|
|
359
|
+
points.push(info.p1.y);
|
|
360
|
+
});
|
|
361
|
+
let minX = points[0] || 0;
|
|
362
|
+
let maxX = points[0] || 0;
|
|
363
|
+
let minY = points[1] || 0;
|
|
364
|
+
let maxY = points[1] || 0;
|
|
365
|
+
let x, y;
|
|
366
|
+
for (let i = 0; i < points.length / 2; i++) {
|
|
367
|
+
x = points[i * 2];
|
|
368
|
+
y = points[i * 2 + 1];
|
|
369
|
+
minX = Math.min(minX, x);
|
|
370
|
+
maxX = Math.max(maxX, x);
|
|
371
|
+
minY = Math.min(minY, y);
|
|
372
|
+
maxY = Math.max(maxY, y);
|
|
373
|
+
}
|
|
374
|
+
const fontSize = this.fontSize();
|
|
375
|
+
return {
|
|
376
|
+
x: minX - fontSize / 2,
|
|
377
|
+
y: minY - fontSize / 2,
|
|
378
|
+
width: maxX - minX + fontSize,
|
|
379
|
+
height: maxY - minY + fontSize,
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
destroy(): this {
|
|
383
|
+
Util.releaseCanvas(this.dummyCanvas);
|
|
384
|
+
return super.destroy();
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
fontFamily: GetSet<string, this>;
|
|
388
|
+
fontSize: GetSet<number, this>;
|
|
389
|
+
fontStyle: GetSet<string, this>;
|
|
390
|
+
fontVariant: GetSet<string, this>;
|
|
391
|
+
align: GetSet<string, this>;
|
|
392
|
+
letterSpacing: GetSet<number, this>;
|
|
393
|
+
text: GetSet<string, this>;
|
|
394
|
+
data: GetSet<string, this>;
|
|
395
|
+
|
|
396
|
+
kerningFunc: GetSet<(leftChar: string, rightChar: string) => number, this>;
|
|
397
|
+
textBaseline: GetSet<string, this>;
|
|
398
|
+
textDecoration: GetSet<string, this>;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
TextPath.prototype._fillFunc = _fillFunc;
|
|
402
|
+
TextPath.prototype._strokeFunc = _strokeFunc;
|
|
403
|
+
TextPath.prototype._fillFuncHit = _fillFunc;
|
|
404
|
+
TextPath.prototype._strokeFuncHit = _strokeFunc;
|
|
405
|
+
TextPath.prototype.className = 'TextPath';
|
|
406
|
+
TextPath.prototype._attrsAffectingSize = ['text', 'fontSize', 'data'];
|
|
407
|
+
_registerNode(TextPath);
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* get/set SVG path data string. This method
|
|
411
|
+
* also automatically parses the data string
|
|
412
|
+
* into a data array. Currently supported SVG data:
|
|
413
|
+
* M, m, L, l, H, h, V, v, Q, q, T, t, C, c, S, s, A, a, Z, z
|
|
414
|
+
* @name Konva.TextPath#data
|
|
415
|
+
* @method
|
|
416
|
+
* @param {String} data svg path string
|
|
417
|
+
* @returns {String}
|
|
418
|
+
* @example
|
|
419
|
+
* // get data
|
|
420
|
+
* var data = shape.data();
|
|
421
|
+
*
|
|
422
|
+
* // set data
|
|
423
|
+
* shape.data('M200,100h100v50z');
|
|
424
|
+
*/
|
|
425
|
+
Factory.addGetterSetter(TextPath, 'data');
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* get/set font family
|
|
429
|
+
* @name Konva.TextPath#fontFamily
|
|
430
|
+
* @method
|
|
431
|
+
* @param {String} fontFamily
|
|
432
|
+
* @returns {String}
|
|
433
|
+
* @example
|
|
434
|
+
* // get font family
|
|
435
|
+
* var fontFamily = shape.fontFamily();
|
|
436
|
+
*
|
|
437
|
+
* // set font family
|
|
438
|
+
* shape.fontFamily('Arial');
|
|
439
|
+
*/
|
|
440
|
+
Factory.addGetterSetter(TextPath, 'fontFamily', 'Arial');
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* get/set font size in pixels
|
|
444
|
+
* @name Konva.TextPath#fontSize
|
|
445
|
+
* @method
|
|
446
|
+
* @param {Number} fontSize
|
|
447
|
+
* @returns {Number}
|
|
448
|
+
* @example
|
|
449
|
+
* // get font size
|
|
450
|
+
* var fontSize = shape.fontSize();
|
|
451
|
+
*
|
|
452
|
+
* // set font size to 22px
|
|
453
|
+
* shape.fontSize(22);
|
|
454
|
+
*/
|
|
455
|
+
|
|
456
|
+
Factory.addGetterSetter(TextPath, 'fontSize', 12, getNumberValidator());
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* get/set font style. Can be 'normal', 'italic', or 'bold', '500' or even 'italic bold'. 'normal' is the default.
|
|
460
|
+
* @name Konva.TextPath#fontStyle
|
|
461
|
+
* @method
|
|
462
|
+
* @param {String} fontStyle
|
|
463
|
+
* @returns {String}
|
|
464
|
+
* @example
|
|
465
|
+
* // get font style
|
|
466
|
+
* var fontStyle = shape.fontStyle();
|
|
467
|
+
*
|
|
468
|
+
* // set font style
|
|
469
|
+
* shape.fontStyle('bold');
|
|
470
|
+
*/
|
|
471
|
+
|
|
472
|
+
Factory.addGetterSetter(TextPath, 'fontStyle', NORMAL);
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* get/set horizontal align of text. Can be 'left', 'center', 'right' or 'justify'
|
|
476
|
+
* @name Konva.TextPath#align
|
|
477
|
+
* @method
|
|
478
|
+
* @param {String} align
|
|
479
|
+
* @returns {String}
|
|
480
|
+
* @example
|
|
481
|
+
* // get text align
|
|
482
|
+
* var align = text.align();
|
|
483
|
+
*
|
|
484
|
+
* // center text
|
|
485
|
+
* text.align('center');
|
|
486
|
+
*
|
|
487
|
+
* // align text to right
|
|
488
|
+
* text.align('right');
|
|
489
|
+
*/
|
|
490
|
+
Factory.addGetterSetter(TextPath, 'align', 'left');
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* get/set letter spacing. The default is 0.
|
|
494
|
+
* @name Konva.TextPath#letterSpacing
|
|
495
|
+
* @method
|
|
496
|
+
* @param {Number} letterSpacing
|
|
497
|
+
* @returns {Number}
|
|
498
|
+
* @example
|
|
499
|
+
* // get letter spacing value
|
|
500
|
+
* var letterSpacing = shape.letterSpacing();
|
|
501
|
+
*
|
|
502
|
+
* // set the letter spacing value
|
|
503
|
+
* shape.letterSpacing(2);
|
|
504
|
+
*/
|
|
505
|
+
|
|
506
|
+
Factory.addGetterSetter(TextPath, 'letterSpacing', 0, getNumberValidator());
|
|
507
|
+
|
|
508
|
+
/**
|
|
509
|
+
* get/set text baseline. The default is 'middle'. Can be 'top', 'bottom', 'middle', 'alphabetic', 'hanging'
|
|
510
|
+
* @name Konva.TextPath#textBaseline
|
|
511
|
+
* @method
|
|
512
|
+
* @param {String} textBaseline
|
|
513
|
+
* @returns {String}
|
|
514
|
+
* @example
|
|
515
|
+
* // get current text baseline
|
|
516
|
+
* var textBaseline = shape.textBaseline();
|
|
517
|
+
*
|
|
518
|
+
* // set new text baseline
|
|
519
|
+
* shape.textBaseline('top');
|
|
520
|
+
*/
|
|
521
|
+
Factory.addGetterSetter(TextPath, 'textBaseline', 'middle');
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* get/set font variant. Can be 'normal' or 'small-caps'. 'normal' is the default.
|
|
525
|
+
* @name Konva.TextPath#fontVariant
|
|
526
|
+
* @method
|
|
527
|
+
* @param {String} fontVariant
|
|
528
|
+
* @returns {String}
|
|
529
|
+
* @example
|
|
530
|
+
* // get font variant
|
|
531
|
+
* var fontVariant = shape.fontVariant();
|
|
532
|
+
*
|
|
533
|
+
* // set font variant
|
|
534
|
+
* shape.fontVariant('small-caps');
|
|
535
|
+
*/
|
|
536
|
+
Factory.addGetterSetter(TextPath, 'fontVariant', NORMAL);
|
|
537
|
+
|
|
538
|
+
/**
|
|
539
|
+
* get/set text
|
|
540
|
+
* @name Konva.TextPath#getText
|
|
541
|
+
* @method
|
|
542
|
+
* @param {String} text
|
|
543
|
+
* @returns {String}
|
|
544
|
+
* @example
|
|
545
|
+
* // get text
|
|
546
|
+
* var text = text.text();
|
|
547
|
+
*
|
|
548
|
+
* // set text
|
|
549
|
+
* text.text('Hello world!');
|
|
550
|
+
*/
|
|
551
|
+
Factory.addGetterSetter(TextPath, 'text', EMPTY_STRING);
|
|
552
|
+
|
|
553
|
+
/**
|
|
554
|
+
* get/set text decoration of a text. Can be '' or 'underline'.
|
|
555
|
+
* @name Konva.TextPath#textDecoration
|
|
556
|
+
* @method
|
|
557
|
+
* @param {String} textDecoration
|
|
558
|
+
* @returns {String}
|
|
559
|
+
* @example
|
|
560
|
+
* // get text decoration
|
|
561
|
+
* var textDecoration = shape.textDecoration();
|
|
562
|
+
*
|
|
563
|
+
* // underline text
|
|
564
|
+
* shape.textDecoration('underline');
|
|
565
|
+
*/
|
|
566
|
+
Factory.addGetterSetter(TextPath, 'textDecoration', '');
|
|
567
|
+
|
|
568
|
+
/**
|
|
569
|
+
* get/set kerning function.
|
|
570
|
+
* @name Konva.TextPath#kerningFunc
|
|
571
|
+
* @method
|
|
572
|
+
* @param {String} kerningFunc
|
|
573
|
+
* @returns {String}
|
|
574
|
+
* @example
|
|
575
|
+
* // get text decoration
|
|
576
|
+
* var kerningFunc = text.kerningFunc();
|
|
577
|
+
*
|
|
578
|
+
* // center text
|
|
579
|
+
* text.kerningFunc(function(leftChar, rightChar) {
|
|
580
|
+
* return 1;
|
|
581
|
+
* });
|
|
582
|
+
*/
|
|
583
|
+
Factory.addGetterSetter(TextPath, 'kerningFunc', undefined);
|