-
Notifications
You must be signed in to change notification settings - Fork 643
/
text.js
214 lines (192 loc) 路 5.4 KB
/
text.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const Util = require('../../util/common');
const Shape = require('../shape');
let textWidthCacheCounter = 0;
let textWidthCache = {};
const TEXT_CACHE_MAX = 5000;
class Text extends Shape {
_initProperties() {
super._initProperties();
this._attrs.canFill = true;
this._attrs.canStroke = true;
this._attrs.type = 'text';
}
getDefaultAttrs() {
return {
lineWidth: 0,
lineCount: 1,
fontSize: 12,
fontFamily: 'sans-serif',
fontStyle: 'normal',
fontWeight: 'normal',
fontVariant: 'normal',
textAlign: 'start',
textBaseline: 'bottom',
lineHeight: null,
textArr: null
};
}
_getFontStyle() {
const attrs = this._attrs.attrs;
const { fontSize, fontFamily, fontWeight, fontStyle, fontVariant } = attrs;
return `${fontStyle} ${fontVariant} ${fontWeight} ${fontSize}px ${fontFamily}`;
}
_afterAttrsSet() {
const attrs = this._attrs.attrs;
attrs.font = this._getFontStyle();
if (attrs.text) {
const text = attrs.text;
let textArr;
if (Util.isString(text) && (text.indexOf('\n') !== -1)) {
textArr = text.split('\n');
const lineCount = textArr.length;
attrs.lineCount = lineCount;
attrs.textArr = textArr;
}
}
this.set('attrs', attrs);
}
_getTextHeight() {
const attrs = this._attrs.attrs;
const lineCount = attrs.lineCount;
const fontSize = attrs.fontSize * 1;
if (lineCount > 1) {
const spaceingY = this._getSpaceingY();
return fontSize * lineCount + spaceingY * (lineCount - 1);
}
return fontSize;
}
_getSpaceingY() {
const attrs = this._attrs.attrs;
const lineHeight = attrs.lineHeight;
const fontSize = attrs.fontSize * 1;
return lineHeight ? (lineHeight - fontSize) : fontSize * 0.14;
}
drawInner(context) {
const self = this;
const attrs = self._attrs.attrs;
const text = attrs.text;
if (!text) {
return;
}
const textArr = attrs.textArr;
const fontSize = attrs.fontSize * 1;
const spaceingY = self._getSpaceingY();
const x = attrs.x;
const y = attrs.y;
const textBaseline = attrs.textBaseline;
let height;
if (textArr) {
height = self._getTextHeight();
}
let subY;
// context.beginPath();
if (self.hasFill()) {
const fillOpacity = attrs.fillOpacity;
if (!Util.isNil(fillOpacity) && fillOpacity !== 1) {
context.globalAlpha = fillOpacity;
}
if (textArr) {
for (let i = 0, len = textArr.length; i < len; i++) {
const subText = textArr[i];
subY = y + i * (spaceingY + fontSize) - height + fontSize; // bottom;
if (textBaseline === 'middle') {
subY += height - fontSize - (height - fontSize) / 2;
}
if (textBaseline === 'top') {
subY += height - fontSize;
}
context.fillText(subText, x, subY);
}
} else {
context.fillText(text, x, y);
}
}
if (self.hasStroke()) {
if (textArr) {
for (let i = 0, len = textArr.length; i < len; i++) {
const subText = textArr[i];
subY = y + i * (spaceingY + fontSize) - height + fontSize; // bottom;
if (textBaseline === 'middle') {
subY += height - fontSize - (height - fontSize) / 2;
}
if (textBaseline === 'top') {
subY += height - fontSize;
}
context.strokeText(subText, x, subY);
}
} else {
context.strokeText(text, x, y);
}
}
}
calculateBox() {
const self = this;
const attrs = self._attrs.attrs;
const { x, y, textAlign, textBaseline } = attrs;
const width = self._getTextWidth(); // attrs.width
if (!width) {
// 濡傛灉width涓嶅瓨鍦紝鍥涚偣鍏卞叾瀹炵偣
return {
minX: x,
minY: y,
maxX: x,
maxY: y
};
}
const height = self._getTextHeight(); // attrs.height
const point = {
x,
y: y - height
}; // default textAlign: start, textBaseline: bottom
if (textAlign) {
if (textAlign === 'end' || textAlign === 'right') {
point.x -= width;
} else if (textAlign === 'center') {
point.x -= width / 2;
}
}
if (textBaseline) {
if (textBaseline === 'top') {
point.y += height;
} else if (textBaseline === 'middle') {
point.y += height / 2;
}
}
return {
minX: point.x,
minY: point.y,
maxX: point.x + width,
maxY: point.y + height
};
}
_getTextWidth() {
const attrs = this._attrs.attrs;
const text = attrs.text;
const context = this.get('context');
if (Util.isNil(text)) return undefined;
const font = attrs.font;
const textArr = attrs.textArr;
const key = text + '' + font;
if (textWidthCache[key]) {
return textWidthCache[key];
}
let width = 0;
if (textArr) {
for (let i = 0, length = textArr.length; i < length; i++) {
const subText = textArr[i];
width = Math.max(width, Util.measureText(subText, font, context).width);
}
} else {
width = Util.measureText(text, font, context).width;
}
if (textWidthCacheCounter > TEXT_CACHE_MAX) {
textWidthCacheCounter = 0;
textWidthCache = {};
}
textWidthCacheCounter++;
textWidthCache[key] = width;
return width;
}
}
Shape.Text = Text;
module.exports = Text;