graphic.js
15.5 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
// TODO
// 1. shadow
// 2. Image: sx, sy, sw, sh
import {createElement} from './core';
import PathProxy from '../core/PathProxy';
import BoundingRect from '../core/BoundingRect';
import * as matrix from '../core/matrix';
import * as textContain from '../contain/text';
import * as textHelper from '../graphic/helper/text';
import Text from '../graphic/Text';
var CMD = PathProxy.CMD;
var arrayJoin = Array.prototype.join;
var NONE = 'none';
var mathRound = Math.round;
var mathSin = Math.sin;
var mathCos = Math.cos;
var PI = Math.PI;
var PI2 = Math.PI * 2;
var degree = 180 / PI;
var EPSILON = 1e-4;
function round4(val) {
return mathRound(val * 1e4) / 1e4;
}
function isAroundZero(val) {
return val < EPSILON && val > -EPSILON;
}
function pathHasFill(style, isText) {
var fill = isText ? style.textFill : style.fill;
return fill != null && fill !== NONE;
}
function pathHasStroke(style, isText) {
var stroke = isText ? style.textStroke : style.stroke;
return stroke != null && stroke !== NONE;
}
function setTransform(svgEl, m) {
if (m) {
attr(svgEl, 'transform', 'matrix(' + arrayJoin.call(m, ',') + ')');
}
}
function attr(el, key, val) {
if (!val || val.type !== 'linear' && val.type !== 'radial') {
// Don't set attribute for gradient, since it need new dom nodes
el.setAttribute(key, val);
}
}
function attrXLink(el, key, val) {
el.setAttributeNS('http://www.w3.org/1999/xlink', key, val);
}
function bindStyle(svgEl, style, isText, el) {
if (pathHasFill(style, isText)) {
var fill = isText ? style.textFill : style.fill;
fill = fill === 'transparent' ? NONE : fill;
/**
* FIXME:
* This is a temporary fix for Chrome's clipping bug
* that happens when a clip-path is referring another one.
* This fix should be used before Chrome's bug is fixed.
* For an element that has clip-path, and fill is none,
* set it to be "rgba(0, 0, 0, 0.002)" will hide the element.
* Otherwise, it will show black fill color.
* 0.002 is used because this won't work for alpha values smaller
* than 0.002.
*
* See
* https://bugs.chromium.org/p/chromium/issues/detail?id=659790
* for more information.
*/
if (svgEl.getAttribute('clip-path') !== 'none' && fill === NONE) {
fill = 'rgba(0, 0, 0, 0.002)';
}
attr(svgEl, 'fill', fill);
attr(svgEl, 'fill-opacity', style.fillOpacity != null ? style.fillOpacity * style.opacity : style.opacity);
}
else {
attr(svgEl, 'fill', NONE);
}
if (pathHasStroke(style, isText)) {
var stroke = isText ? style.textStroke : style.stroke;
stroke = stroke === 'transparent' ? NONE : stroke;
attr(svgEl, 'stroke', stroke);
var strokeWidth = isText
? style.textStrokeWidth
: style.lineWidth;
var strokeScale = !isText && style.strokeNoScale
? el.getLineScale()
: 1;
attr(svgEl, 'stroke-width', strokeWidth / strokeScale);
// stroke then fill for text; fill then stroke for others
attr(svgEl, 'paint-order', isText ? 'stroke' : 'fill');
attr(svgEl, 'stroke-opacity', style.strokeOpacity != null ? style.strokeOpacity : style.opacity);
var lineDash = style.lineDash;
if (lineDash) {
attr(svgEl, 'stroke-dasharray', style.lineDash.join(','));
attr(svgEl, 'stroke-dashoffset', mathRound(style.lineDashOffset || 0));
}
else {
attr(svgEl, 'stroke-dasharray', '');
}
// PENDING
style.lineCap && attr(svgEl, 'stroke-linecap', style.lineCap);
style.lineJoin && attr(svgEl, 'stroke-linejoin', style.lineJoin);
style.miterLimit && attr(svgEl, 'stroke-miterlimit', style.miterLimit);
}
else {
attr(svgEl, 'stroke', NONE);
}
}
/***************************************************
* PATH
**************************************************/
function pathDataToString(path) {
var str = [];
var data = path.data;
var dataLength = path.len();
for (var i = 0; i < dataLength;) {
var cmd = data[i++];
var cmdStr = '';
var nData = 0;
switch (cmd) {
case CMD.M:
cmdStr = 'M';
nData = 2;
break;
case CMD.L:
cmdStr = 'L';
nData = 2;
break;
case CMD.Q:
cmdStr = 'Q';
nData = 4;
break;
case CMD.C:
cmdStr = 'C';
nData = 6;
break;
case CMD.A:
var cx = data[i++];
var cy = data[i++];
var rx = data[i++];
var ry = data[i++];
var theta = data[i++];
var dTheta = data[i++];
var psi = data[i++];
var clockwise = data[i++];
var dThetaPositive = Math.abs(dTheta);
var isCircle = isAroundZero(dThetaPositive - PI2)
&& !isAroundZero(dThetaPositive);
var large = false;
if (dThetaPositive >= PI2) {
large = true;
}
else if (isAroundZero(dThetaPositive)) {
large = false;
}
else {
large = (dTheta > -PI && dTheta < 0 || dTheta > PI)
=== !!clockwise;
}
var x0 = round4(cx + rx * mathCos(theta));
var y0 = round4(cy + ry * mathSin(theta));
// It will not draw if start point and end point are exactly the same
// We need to shift the end point with a small value
// FIXME A better way to draw circle ?
if (isCircle) {
if (clockwise) {
dTheta = PI2 - 1e-4;
}
else {
dTheta = -PI2 + 1e-4;
}
large = true;
if (i === 9) {
// Move to (x0, y0) only when CMD.A comes at the
// first position of a shape.
// For instance, when drawing a ring, CMD.A comes
// after CMD.M, so it's unnecessary to move to
// (x0, y0).
str.push('M', x0, y0);
}
}
var x = round4(cx + rx * mathCos(theta + dTheta));
var y = round4(cy + ry * mathSin(theta + dTheta));
// FIXME Ellipse
str.push('A', round4(rx), round4(ry),
mathRound(psi * degree), +large, +clockwise, x, y);
break;
case CMD.Z:
cmdStr = 'Z';
break;
case CMD.R:
var x = round4(data[i++]);
var y = round4(data[i++]);
var w = round4(data[i++]);
var h = round4(data[i++]);
str.push(
'M', x, y,
'L', x + w, y,
'L', x + w, y + h,
'L', x, y + h,
'L', x, y
);
break;
}
cmdStr && str.push(cmdStr);
for (var j = 0; j < nData; j++) {
// PENDING With scale
str.push(round4(data[i++]));
}
}
return str.join(' ');
}
var svgPath = {};
export {svgPath as path};
svgPath.brush = function (el) {
var style = el.style;
var svgEl = el.__svgEl;
if (!svgEl) {
svgEl = createElement('path');
el.__svgEl = svgEl;
}
if (!el.path) {
el.createPathProxy();
}
var path = el.path;
if (el.__dirtyPath) {
path.beginPath();
path.subPixelOptimize = false;
el.buildPath(path, el.shape);
el.__dirtyPath = false;
var pathStr = pathDataToString(path);
if (pathStr.indexOf('NaN') < 0) {
// Ignore illegal path, which may happen such in out-of-range
// data in Calendar series.
attr(svgEl, 'd', pathStr);
}
}
bindStyle(svgEl, style, false, el);
setTransform(svgEl, el.transform);
if (style.text != null) {
svgTextDrawRectText(el, el.getBoundingRect());
}
};
/***************************************************
* IMAGE
**************************************************/
var svgImage = {};
export {svgImage as image};
svgImage.brush = function (el) {
var style = el.style;
var image = style.image;
if (image instanceof HTMLImageElement) {
var src = image.src;
image = src;
}
if (!image) {
return;
}
var x = style.x || 0;
var y = style.y || 0;
var dw = style.width;
var dh = style.height;
var svgEl = el.__svgEl;
if (!svgEl) {
svgEl = createElement('image');
el.__svgEl = svgEl;
}
if (image !== el.__imageSrc) {
attrXLink(svgEl, 'href', image);
// Caching image src
el.__imageSrc = image;
}
attr(svgEl, 'width', dw);
attr(svgEl, 'height', dh);
attr(svgEl, 'x', x);
attr(svgEl, 'y', y);
setTransform(svgEl, el.transform);
if (style.text != null) {
svgTextDrawRectText(el, el.getBoundingRect());
}
};
/***************************************************
* TEXT
**************************************************/
var svgText = {};
export {svgText as text};
var tmpRect = new BoundingRect();
var svgTextDrawRectText = function (el, rect, textRect) {
var style = el.style;
el.__dirty && textHelper.normalizeTextStyle(style, true);
var text = style.text;
// Convert to string
if (text == null) {
// Draw no text only when text is set to null, but not ''
return;
}
else {
text += '';
}
var textSvgEl = el.__textSvgEl;
if (!textSvgEl) {
textSvgEl = createElement('text');
el.__textSvgEl = textSvgEl;
}
var x;
var y;
var textPosition = style.textPosition;
var distance = style.textDistance;
var align = style.textAlign || 'left';
if (typeof style.fontSize === 'number') {
style.fontSize += 'px';
}
var font = style.font
|| [
style.fontStyle || '',
style.fontWeight || '',
style.fontSize || '',
style.fontFamily || ''
].join(' ')
|| textContain.DEFAULT_FONT;
var verticalAlign = getVerticalAlignForSvg(style.textVerticalAlign);
textRect = textContain.getBoundingRect(
text, font, align,
verticalAlign, style.textPadding, style.textLineHeight
);
var lineHeight = textRect.lineHeight;
// Text position represented by coord
if (textPosition instanceof Array) {
x = rect.x + textPosition[0];
y = rect.y + textPosition[1];
}
else {
var newPos = textContain.adjustTextPositionOnRect(
textPosition, rect, distance
);
x = newPos.x;
y = newPos.y;
verticalAlign = getVerticalAlignForSvg(newPos.textVerticalAlign);
align = newPos.textAlign;
}
attr(textSvgEl, 'alignment-baseline', verticalAlign);
if (font) {
textSvgEl.style.font = font;
}
var textPadding = style.textPadding;
// Make baseline top
attr(textSvgEl, 'x', x);
attr(textSvgEl, 'y', y);
bindStyle(textSvgEl, style, true, el);
if (el instanceof Text || el.style.transformText) {
// Transform text with element
setTransform(textSvgEl, el.transform);
}
else {
if (el.transform) {
tmpRect.copy(rect);
tmpRect.applyTransform(el.transform);
rect = tmpRect;
}
else {
var pos = el.transformCoordToGlobal(rect.x, rect.y);
rect.x = pos[0];
rect.y = pos[1];
el.transform = matrix.identity(matrix.create());
}
// Text rotation, but no element transform
var origin = style.textOrigin;
if (origin === 'center') {
x = textRect.width / 2 + x;
y = textRect.height / 2 + y;
}
else if (origin) {
x = origin[0] + x;
y = origin[1] + y;
}
var rotate = -style.textRotation || 0;
var transform = matrix.create();
// Apply textRotate to element matrix
matrix.rotate(transform, transform, rotate);
var pos = [el.transform[4], el.transform[5]];
matrix.translate(transform, transform, pos);
setTransform(textSvgEl, transform);
}
var textLines = text.split('\n');
var nTextLines = textLines.length;
var textAnchor = align;
// PENDING
if (textAnchor === 'left') {
textAnchor = 'start';
textPadding && (x += textPadding[3]);
}
else if (textAnchor === 'right') {
textAnchor = 'end';
textPadding && (x -= textPadding[1]);
}
else if (textAnchor === 'center') {
textAnchor = 'middle';
textPadding && (x += (textPadding[3] - textPadding[1]) / 2);
}
var dy = 0;
if (verticalAlign === 'after-edge') {
dy = -textRect.height + lineHeight;
textPadding && (dy -= textPadding[2]);
}
else if (verticalAlign === 'middle') {
dy = (-textRect.height + lineHeight) / 2;
textPadding && (y += (textPadding[0] - textPadding[2]) / 2);
}
else {
textPadding && (dy += textPadding[0]);
}
// Font may affect position of each tspan elements
if (el.__text !== text || el.__textFont !== font) {
var tspanList = el.__tspanList || [];
el.__tspanList = tspanList;
for (var i = 0; i < nTextLines; i++) {
// Using cached tspan elements
var tspan = tspanList[i];
if (!tspan) {
tspan = tspanList[i] = createElement('tspan');
textSvgEl.appendChild(tspan);
attr(tspan, 'alignment-baseline', verticalAlign);
attr(tspan, 'text-anchor', textAnchor);
}
else {
tspan.innerHTML = '';
}
attr(tspan, 'x', x);
attr(tspan, 'y', y + i * lineHeight + dy);
tspan.appendChild(document.createTextNode(textLines[i]));
}
// Remove unsed tspan elements
for (; i < tspanList.length; i++) {
textSvgEl.removeChild(tspanList[i]);
}
tspanList.length = nTextLines;
el.__text = text;
el.__textFont = font;
}
else if (el.__tspanList.length) {
// Update span x and y
var len = el.__tspanList.length;
for (var i = 0; i < len; ++i) {
var tspan = el.__tspanList[i];
if (tspan) {
attr(tspan, 'x', x);
attr(tspan, 'y', y + i * lineHeight + dy);
}
}
}
};
function getVerticalAlignForSvg(verticalAlign) {
if (verticalAlign === 'middle') {
return 'middle';
}
else if (verticalAlign === 'bottom') {
return 'after-edge';
}
else {
return 'hanging';
}
}
svgText.drawRectText = svgTextDrawRectText;
svgText.brush = function (el) {
var style = el.style;
if (style.text != null) {
// 强制设置 textPosition
style.textPosition = [0, 0];
svgTextDrawRectText(el, {
x: style.x || 0, y: style.y || 0,
width: 0, height: 0
}, el.getBoundingRect());
}
};