Blame view

node_modules/zrender/lib/svg-legacy/graphic.js 4.15 KB
bd028579   易尊强   2/28
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
  import { createElement, XLINKNS } from '../svg/core.js';
  import { getMatrixStr, TEXT_ALIGN_TO_ANCHOR, adjustTextY } from '../svg/helper.js';
  import { getLineHeight } from '../contain/text.js';
  import SVGPathRebuilder from '../svg/SVGPathRebuilder.js';
  import mapStyleToAttrs from '../svg/mapStyleToAttrs.js';
  import { DEFAULT_FONT } from '../core/platform.js';
  function setTransform(svgEl, m) {
      if (m) {
          attr(svgEl, 'transform', getMatrixStr(m));
      }
  }
  function attr(el, key, val) {
      if (!val || val.type !== 'linear' && val.type !== 'radial') {
          el.setAttribute(key, val);
      }
  }
  function attrXLink(el, key, val) {
      el.setAttributeNS(XLINKNS, key, val);
  }
  function attrXML(el, key, val) {
      el.setAttributeNS('http://www.w3.org/XML/1998/namespace', key, val);
  }
  function bindStyle(svgEl, style, el) {
      mapStyleToAttrs(function (key, val) { return attr(svgEl, key, val); }, style, el, true);
  }
  var 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.shapeChanged()) {
              path.beginPath();
              el.buildPath(path, el.shape);
              el.pathUpdated();
          }
          var pathVersion = path.getVersion();
          var elExt = el;
          var svgPathBuilder = elExt.__svgPathBuilder;
          if (elExt.__svgPathVersion !== pathVersion || !svgPathBuilder || el.style.strokePercent < 1) {
              if (!svgPathBuilder) {
                  svgPathBuilder = elExt.__svgPathBuilder = new SVGPathRebuilder();
              }
              svgPathBuilder.reset();
              path.rebuildPath(svgPathBuilder, el.style.strokePercent);
              svgPathBuilder.generateStr();
              elExt.__svgPathVersion = pathVersion;
          }
          attr(svgEl, 'd', svgPathBuilder.getStr());
          bindStyle(svgEl, style, el);
          setTransform(svgEl, el.transform);
      }
  };
  export { svgPath as path };
  var svgImage = {
      brush: function (el) {
          var style = el.style;
          var image = style.image;
          if (image instanceof HTMLImageElement) {
              image = image.src;
          }
          else if (image instanceof HTMLCanvasElement) {
              image = image.toDataURL();
          }
          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);
              el.__imageSrc = image;
          }
          attr(svgEl, 'width', dw + '');
          attr(svgEl, 'height', dh + '');
          attr(svgEl, 'x', x + '');
          attr(svgEl, 'y', y + '');
          bindStyle(svgEl, style, el);
          setTransform(svgEl, el.transform);
      }
  };
  export { svgImage as image };
  var svgText = {
      brush: function (el) {
          var style = el.style;
          var text = style.text;
          text != null && (text += '');
          if (!text || isNaN(style.x) || isNaN(style.y)) {
              return;
          }
          var textSvgEl = el.__svgEl;
          if (!textSvgEl) {
              textSvgEl = createElement('text');
              attrXML(textSvgEl, 'xml:space', 'preserve');
              el.__svgEl = textSvgEl;
          }
          var font = style.font || DEFAULT_FONT;
          var textSvgElStyle = textSvgEl.style;
          textSvgElStyle.font = font;
          textSvgEl.textContent = text;
          bindStyle(textSvgEl, style, el);
          setTransform(textSvgEl, el.transform);
          var x = style.x || 0;
          var y = adjustTextY(style.y || 0, getLineHeight(font), style.textBaseline);
          var textAlign = TEXT_ALIGN_TO_ANCHOR[style.textAlign]
              || style.textAlign;
          attr(textSvgEl, 'dominant-baseline', 'central');
          attr(textSvgEl, 'text-anchor', textAlign);
          attr(textSvgEl, 'x', x + '');
          attr(textSvgEl, 'y', y + '');
      }
  };
  export { svgText as text };