parseSVG.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. import Group from '../graphic/Group';
  2. import ZRImage from '../graphic/Image';
  3. import Circle from '../graphic/shape/Circle';
  4. import Rect from '../graphic/shape/Rect';
  5. import Ellipse from '../graphic/shape/Ellipse';
  6. import Line from '../graphic/shape/Line';
  7. import Polygon from '../graphic/shape/Polygon';
  8. import Polyline from '../graphic/shape/Polyline';
  9. import * as matrix from '../core/matrix';
  10. import { createFromString } from './path';
  11. import { defaults, trim, each, map, keys, hasOwn } from '../core/util';
  12. import LinearGradient from '../graphic/LinearGradient';
  13. import RadialGradient from '../graphic/RadialGradient';
  14. import TSpan from '../graphic/TSpan';
  15. import { parseXML } from './parseXML';
  16. ;
  17. var nodeParsers;
  18. var INHERITABLE_STYLE_ATTRIBUTES_MAP = {
  19. 'fill': 'fill',
  20. 'stroke': 'stroke',
  21. 'stroke-width': 'lineWidth',
  22. 'opacity': 'opacity',
  23. 'fill-opacity': 'fillOpacity',
  24. 'stroke-opacity': 'strokeOpacity',
  25. 'stroke-dasharray': 'lineDash',
  26. 'stroke-dashoffset': 'lineDashOffset',
  27. 'stroke-linecap': 'lineCap',
  28. 'stroke-linejoin': 'lineJoin',
  29. 'stroke-miterlimit': 'miterLimit',
  30. 'font-family': 'fontFamily',
  31. 'font-size': 'fontSize',
  32. 'font-style': 'fontStyle',
  33. 'font-weight': 'fontWeight',
  34. 'text-anchor': 'textAlign',
  35. 'visibility': 'visibility',
  36. 'display': 'display'
  37. };
  38. var INHERITABLE_STYLE_ATTRIBUTES_MAP_KEYS = keys(INHERITABLE_STYLE_ATTRIBUTES_MAP);
  39. var SELF_STYLE_ATTRIBUTES_MAP = {
  40. 'alignment-baseline': 'textBaseline',
  41. 'stop-color': 'stopColor'
  42. };
  43. var SELF_STYLE_ATTRIBUTES_MAP_KEYS = keys(SELF_STYLE_ATTRIBUTES_MAP);
  44. var SVGParser = (function () {
  45. function SVGParser() {
  46. this._defs = {};
  47. this._root = null;
  48. }
  49. SVGParser.prototype.parse = function (xml, opt) {
  50. opt = opt || {};
  51. var svg = parseXML(xml);
  52. if (!svg) {
  53. throw new Error('Illegal svg');
  54. }
  55. this._defsUsePending = [];
  56. var root = new Group();
  57. this._root = root;
  58. var named = [];
  59. var viewBox = svg.getAttribute('viewBox') || '';
  60. var width = parseFloat((svg.getAttribute('width') || opt.width));
  61. var height = parseFloat((svg.getAttribute('height') || opt.height));
  62. isNaN(width) && (width = null);
  63. isNaN(height) && (height = null);
  64. parseAttributes(svg, root, null, true, false);
  65. var child = svg.firstChild;
  66. while (child) {
  67. this._parseNode(child, root, named, null, false, false);
  68. child = child.nextSibling;
  69. }
  70. applyDefs(this._defs, this._defsUsePending);
  71. this._defsUsePending = [];
  72. var viewBoxRect;
  73. var viewBoxTransform;
  74. if (viewBox) {
  75. var viewBoxArr = splitNumberSequence(viewBox);
  76. if (viewBoxArr.length >= 4) {
  77. viewBoxRect = {
  78. x: parseFloat((viewBoxArr[0] || 0)),
  79. y: parseFloat((viewBoxArr[1] || 0)),
  80. width: parseFloat(viewBoxArr[2]),
  81. height: parseFloat(viewBoxArr[3])
  82. };
  83. }
  84. }
  85. if (viewBoxRect && width != null && height != null) {
  86. viewBoxTransform = makeViewBoxTransform(viewBoxRect, { x: 0, y: 0, width: width, height: height });
  87. if (!opt.ignoreViewBox) {
  88. var elRoot = root;
  89. root = new Group();
  90. root.add(elRoot);
  91. elRoot.scaleX = elRoot.scaleY = viewBoxTransform.scale;
  92. elRoot.x = viewBoxTransform.x;
  93. elRoot.y = viewBoxTransform.y;
  94. }
  95. }
  96. if (!opt.ignoreRootClip && width != null && height != null) {
  97. root.setClipPath(new Rect({
  98. shape: { x: 0, y: 0, width: width, height: height }
  99. }));
  100. }
  101. return {
  102. root: root,
  103. width: width,
  104. height: height,
  105. viewBoxRect: viewBoxRect,
  106. viewBoxTransform: viewBoxTransform,
  107. named: named
  108. };
  109. };
  110. SVGParser.prototype._parseNode = function (xmlNode, parentGroup, named, namedFrom, isInDefs, isInText) {
  111. var nodeName = xmlNode.nodeName.toLowerCase();
  112. var el;
  113. var namedFromForSub = namedFrom;
  114. if (nodeName === 'defs') {
  115. isInDefs = true;
  116. }
  117. if (nodeName === 'text') {
  118. isInText = true;
  119. }
  120. if (nodeName === 'defs' || nodeName === 'switch') {
  121. el = parentGroup;
  122. }
  123. else {
  124. if (!isInDefs) {
  125. var parser_1 = nodeParsers[nodeName];
  126. if (parser_1 && hasOwn(nodeParsers, nodeName)) {
  127. el = parser_1.call(this, xmlNode, parentGroup);
  128. var nameAttr = xmlNode.getAttribute('name');
  129. if (nameAttr) {
  130. var newNamed = {
  131. name: nameAttr,
  132. namedFrom: null,
  133. svgNodeTagLower: nodeName,
  134. el: el
  135. };
  136. named.push(newNamed);
  137. if (nodeName === 'g') {
  138. namedFromForSub = newNamed;
  139. }
  140. }
  141. else if (namedFrom) {
  142. named.push({
  143. name: namedFrom.name,
  144. namedFrom: namedFrom,
  145. svgNodeTagLower: nodeName,
  146. el: el
  147. });
  148. }
  149. parentGroup.add(el);
  150. }
  151. }
  152. var parser = paintServerParsers[nodeName];
  153. if (parser && hasOwn(paintServerParsers, nodeName)) {
  154. var def = parser.call(this, xmlNode);
  155. var id = xmlNode.getAttribute('id');
  156. if (id) {
  157. this._defs[id] = def;
  158. }
  159. }
  160. }
  161. if (el && el.isGroup) {
  162. var child = xmlNode.firstChild;
  163. while (child) {
  164. if (child.nodeType === 1) {
  165. this._parseNode(child, el, named, namedFromForSub, isInDefs, isInText);
  166. }
  167. else if (child.nodeType === 3 && isInText) {
  168. this._parseText(child, el);
  169. }
  170. child = child.nextSibling;
  171. }
  172. }
  173. };
  174. SVGParser.prototype._parseText = function (xmlNode, parentGroup) {
  175. var text = new TSpan({
  176. style: {
  177. text: xmlNode.textContent
  178. },
  179. silent: true,
  180. x: this._textX || 0,
  181. y: this._textY || 0
  182. });
  183. inheritStyle(parentGroup, text);
  184. parseAttributes(xmlNode, text, this._defsUsePending, false, false);
  185. applyTextAlignment(text, parentGroup);
  186. var textStyle = text.style;
  187. var fontSize = textStyle.fontSize;
  188. if (fontSize && fontSize < 9) {
  189. textStyle.fontSize = 9;
  190. text.scaleX *= fontSize / 9;
  191. text.scaleY *= fontSize / 9;
  192. }
  193. var font = (textStyle.fontSize || textStyle.fontFamily) && [
  194. textStyle.fontStyle,
  195. textStyle.fontWeight,
  196. (textStyle.fontSize || 12) + 'px',
  197. textStyle.fontFamily || 'sans-serif'
  198. ].join(' ');
  199. textStyle.font = font;
  200. var rect = text.getBoundingRect();
  201. this._textX += rect.width;
  202. parentGroup.add(text);
  203. return text;
  204. };
  205. SVGParser.internalField = (function () {
  206. nodeParsers = {
  207. 'g': function (xmlNode, parentGroup) {
  208. var g = new Group();
  209. inheritStyle(parentGroup, g);
  210. parseAttributes(xmlNode, g, this._defsUsePending, false, false);
  211. return g;
  212. },
  213. 'rect': function (xmlNode, parentGroup) {
  214. var rect = new Rect();
  215. inheritStyle(parentGroup, rect);
  216. parseAttributes(xmlNode, rect, this._defsUsePending, false, false);
  217. rect.setShape({
  218. x: parseFloat(xmlNode.getAttribute('x') || '0'),
  219. y: parseFloat(xmlNode.getAttribute('y') || '0'),
  220. width: parseFloat(xmlNode.getAttribute('width') || '0'),
  221. height: parseFloat(xmlNode.getAttribute('height') || '0')
  222. });
  223. rect.silent = true;
  224. return rect;
  225. },
  226. 'circle': function (xmlNode, parentGroup) {
  227. var circle = new Circle();
  228. inheritStyle(parentGroup, circle);
  229. parseAttributes(xmlNode, circle, this._defsUsePending, false, false);
  230. circle.setShape({
  231. cx: parseFloat(xmlNode.getAttribute('cx') || '0'),
  232. cy: parseFloat(xmlNode.getAttribute('cy') || '0'),
  233. r: parseFloat(xmlNode.getAttribute('r') || '0')
  234. });
  235. circle.silent = true;
  236. return circle;
  237. },
  238. 'line': function (xmlNode, parentGroup) {
  239. var line = new Line();
  240. inheritStyle(parentGroup, line);
  241. parseAttributes(xmlNode, line, this._defsUsePending, false, false);
  242. line.setShape({
  243. x1: parseFloat(xmlNode.getAttribute('x1') || '0'),
  244. y1: parseFloat(xmlNode.getAttribute('y1') || '0'),
  245. x2: parseFloat(xmlNode.getAttribute('x2') || '0'),
  246. y2: parseFloat(xmlNode.getAttribute('y2') || '0')
  247. });
  248. line.silent = true;
  249. return line;
  250. },
  251. 'ellipse': function (xmlNode, parentGroup) {
  252. var ellipse = new Ellipse();
  253. inheritStyle(parentGroup, ellipse);
  254. parseAttributes(xmlNode, ellipse, this._defsUsePending, false, false);
  255. ellipse.setShape({
  256. cx: parseFloat(xmlNode.getAttribute('cx') || '0'),
  257. cy: parseFloat(xmlNode.getAttribute('cy') || '0'),
  258. rx: parseFloat(xmlNode.getAttribute('rx') || '0'),
  259. ry: parseFloat(xmlNode.getAttribute('ry') || '0')
  260. });
  261. ellipse.silent = true;
  262. return ellipse;
  263. },
  264. 'polygon': function (xmlNode, parentGroup) {
  265. var pointsStr = xmlNode.getAttribute('points');
  266. var pointsArr;
  267. if (pointsStr) {
  268. pointsArr = parsePoints(pointsStr);
  269. }
  270. var polygon = new Polygon({
  271. shape: {
  272. points: pointsArr || []
  273. },
  274. silent: true
  275. });
  276. inheritStyle(parentGroup, polygon);
  277. parseAttributes(xmlNode, polygon, this._defsUsePending, false, false);
  278. return polygon;
  279. },
  280. 'polyline': function (xmlNode, parentGroup) {
  281. var pointsStr = xmlNode.getAttribute('points');
  282. var pointsArr;
  283. if (pointsStr) {
  284. pointsArr = parsePoints(pointsStr);
  285. }
  286. var polyline = new Polyline({
  287. shape: {
  288. points: pointsArr || []
  289. },
  290. silent: true
  291. });
  292. inheritStyle(parentGroup, polyline);
  293. parseAttributes(xmlNode, polyline, this._defsUsePending, false, false);
  294. return polyline;
  295. },
  296. 'image': function (xmlNode, parentGroup) {
  297. var img = new ZRImage();
  298. inheritStyle(parentGroup, img);
  299. parseAttributes(xmlNode, img, this._defsUsePending, false, false);
  300. img.setStyle({
  301. image: xmlNode.getAttribute('xlink:href') || xmlNode.getAttribute('href'),
  302. x: +xmlNode.getAttribute('x'),
  303. y: +xmlNode.getAttribute('y'),
  304. width: +xmlNode.getAttribute('width'),
  305. height: +xmlNode.getAttribute('height')
  306. });
  307. img.silent = true;
  308. return img;
  309. },
  310. 'text': function (xmlNode, parentGroup) {
  311. var x = xmlNode.getAttribute('x') || '0';
  312. var y = xmlNode.getAttribute('y') || '0';
  313. var dx = xmlNode.getAttribute('dx') || '0';
  314. var dy = xmlNode.getAttribute('dy') || '0';
  315. this._textX = parseFloat(x) + parseFloat(dx);
  316. this._textY = parseFloat(y) + parseFloat(dy);
  317. var g = new Group();
  318. inheritStyle(parentGroup, g);
  319. parseAttributes(xmlNode, g, this._defsUsePending, false, true);
  320. return g;
  321. },
  322. 'tspan': function (xmlNode, parentGroup) {
  323. var x = xmlNode.getAttribute('x');
  324. var y = xmlNode.getAttribute('y');
  325. if (x != null) {
  326. this._textX = parseFloat(x);
  327. }
  328. if (y != null) {
  329. this._textY = parseFloat(y);
  330. }
  331. var dx = xmlNode.getAttribute('dx') || '0';
  332. var dy = xmlNode.getAttribute('dy') || '0';
  333. var g = new Group();
  334. inheritStyle(parentGroup, g);
  335. parseAttributes(xmlNode, g, this._defsUsePending, false, true);
  336. this._textX += parseFloat(dx);
  337. this._textY += parseFloat(dy);
  338. return g;
  339. },
  340. 'path': function (xmlNode, parentGroup) {
  341. var d = xmlNode.getAttribute('d') || '';
  342. var path = createFromString(d);
  343. inheritStyle(parentGroup, path);
  344. parseAttributes(xmlNode, path, this._defsUsePending, false, false);
  345. path.silent = true;
  346. return path;
  347. }
  348. };
  349. })();
  350. return SVGParser;
  351. }());
  352. var paintServerParsers = {
  353. 'lineargradient': function (xmlNode) {
  354. var x1 = parseInt(xmlNode.getAttribute('x1') || '0', 10);
  355. var y1 = parseInt(xmlNode.getAttribute('y1') || '0', 10);
  356. var x2 = parseInt(xmlNode.getAttribute('x2') || '10', 10);
  357. var y2 = parseInt(xmlNode.getAttribute('y2') || '0', 10);
  358. var gradient = new LinearGradient(x1, y1, x2, y2);
  359. parsePaintServerUnit(xmlNode, gradient);
  360. parseGradientColorStops(xmlNode, gradient);
  361. return gradient;
  362. },
  363. 'radialgradient': function (xmlNode) {
  364. var cx = parseInt(xmlNode.getAttribute('cx') || '0', 10);
  365. var cy = parseInt(xmlNode.getAttribute('cy') || '0', 10);
  366. var r = parseInt(xmlNode.getAttribute('r') || '0', 10);
  367. var gradient = new RadialGradient(cx, cy, r);
  368. parsePaintServerUnit(xmlNode, gradient);
  369. parseGradientColorStops(xmlNode, gradient);
  370. return gradient;
  371. }
  372. };
  373. function parsePaintServerUnit(xmlNode, gradient) {
  374. var gradientUnits = xmlNode.getAttribute('gradientUnits');
  375. if (gradientUnits === 'userSpaceOnUse') {
  376. gradient.global = true;
  377. }
  378. }
  379. function parseGradientColorStops(xmlNode, gradient) {
  380. var stop = xmlNode.firstChild;
  381. while (stop) {
  382. if (stop.nodeType === 1
  383. && stop.nodeName.toLocaleLowerCase() === 'stop') {
  384. var offsetStr = stop.getAttribute('offset');
  385. var offset = void 0;
  386. if (offsetStr && offsetStr.indexOf('%') > 0) {
  387. offset = parseInt(offsetStr, 10) / 100;
  388. }
  389. else if (offsetStr) {
  390. offset = parseFloat(offsetStr);
  391. }
  392. else {
  393. offset = 0;
  394. }
  395. var styleVals = {};
  396. parseInlineStyle(stop, styleVals, styleVals);
  397. var stopColor = styleVals.stopColor
  398. || stop.getAttribute('stop-color')
  399. || '#000000';
  400. gradient.colorStops.push({
  401. offset: offset,
  402. color: stopColor
  403. });
  404. }
  405. stop = stop.nextSibling;
  406. }
  407. }
  408. function inheritStyle(parent, child) {
  409. if (parent && parent.__inheritedStyle) {
  410. if (!child.__inheritedStyle) {
  411. child.__inheritedStyle = {};
  412. }
  413. defaults(child.__inheritedStyle, parent.__inheritedStyle);
  414. }
  415. }
  416. function parsePoints(pointsString) {
  417. var list = splitNumberSequence(pointsString);
  418. var points = [];
  419. for (var i = 0; i < list.length; i += 2) {
  420. var x = parseFloat(list[i]);
  421. var y = parseFloat(list[i + 1]);
  422. points.push([x, y]);
  423. }
  424. return points;
  425. }
  426. function parseAttributes(xmlNode, el, defsUsePending, onlyInlineStyle, isTextGroup) {
  427. var disp = el;
  428. var inheritedStyle = disp.__inheritedStyle = disp.__inheritedStyle || {};
  429. var selfStyle = {};
  430. if (xmlNode.nodeType === 1) {
  431. parseTransformAttribute(xmlNode, el);
  432. parseInlineStyle(xmlNode, inheritedStyle, selfStyle);
  433. if (!onlyInlineStyle) {
  434. parseAttributeStyle(xmlNode, inheritedStyle, selfStyle);
  435. }
  436. }
  437. disp.style = disp.style || {};
  438. if (inheritedStyle.fill != null) {
  439. disp.style.fill = getFillStrokeStyle(disp, 'fill', inheritedStyle.fill, defsUsePending);
  440. }
  441. if (inheritedStyle.stroke != null) {
  442. disp.style.stroke = getFillStrokeStyle(disp, 'stroke', inheritedStyle.stroke, defsUsePending);
  443. }
  444. each([
  445. 'lineWidth', 'opacity', 'fillOpacity', 'strokeOpacity', 'miterLimit', 'fontSize'
  446. ], function (propName) {
  447. if (inheritedStyle[propName] != null) {
  448. disp.style[propName] = parseFloat(inheritedStyle[propName]);
  449. }
  450. });
  451. each([
  452. 'lineDashOffset', 'lineCap', 'lineJoin', 'fontWeight', 'fontFamily', 'fontStyle', 'textAlign'
  453. ], function (propName) {
  454. if (inheritedStyle[propName] != null) {
  455. disp.style[propName] = inheritedStyle[propName];
  456. }
  457. });
  458. if (isTextGroup) {
  459. disp.__selfStyle = selfStyle;
  460. }
  461. if (inheritedStyle.lineDash) {
  462. disp.style.lineDash = map(splitNumberSequence(inheritedStyle.lineDash), function (str) {
  463. return parseFloat(str);
  464. });
  465. }
  466. if (inheritedStyle.visibility === 'hidden' || inheritedStyle.visibility === 'collapse') {
  467. disp.invisible = true;
  468. }
  469. if (inheritedStyle.display === 'none') {
  470. disp.ignore = true;
  471. }
  472. }
  473. function applyTextAlignment(text, parentGroup) {
  474. var parentSelfStyle = parentGroup.__selfStyle;
  475. if (parentSelfStyle) {
  476. var textBaseline = parentSelfStyle.textBaseline;
  477. var zrTextBaseline = textBaseline;
  478. if (!textBaseline || textBaseline === 'auto') {
  479. zrTextBaseline = 'alphabetic';
  480. }
  481. else if (textBaseline === 'baseline') {
  482. zrTextBaseline = 'alphabetic';
  483. }
  484. else if (textBaseline === 'before-edge' || textBaseline === 'text-before-edge') {
  485. zrTextBaseline = 'top';
  486. }
  487. else if (textBaseline === 'after-edge' || textBaseline === 'text-after-edge') {
  488. zrTextBaseline = 'bottom';
  489. }
  490. else if (textBaseline === 'central' || textBaseline === 'mathematical') {
  491. zrTextBaseline = 'middle';
  492. }
  493. text.style.textBaseline = zrTextBaseline;
  494. }
  495. var parentInheritedStyle = parentGroup.__inheritedStyle;
  496. if (parentInheritedStyle) {
  497. var textAlign = parentInheritedStyle.textAlign;
  498. var zrTextAlign = textAlign;
  499. if (textAlign) {
  500. if (textAlign === 'middle') {
  501. zrTextAlign = 'center';
  502. }
  503. text.style.textAlign = zrTextAlign;
  504. }
  505. }
  506. }
  507. var urlRegex = /^url\(\s*#(.*?)\)/;
  508. function getFillStrokeStyle(el, method, str, defsUsePending) {
  509. var urlMatch = str && str.match(urlRegex);
  510. if (urlMatch) {
  511. var url = trim(urlMatch[1]);
  512. defsUsePending.push([el, method, url]);
  513. return;
  514. }
  515. if (str === 'none') {
  516. str = null;
  517. }
  518. return str;
  519. }
  520. function applyDefs(defs, defsUsePending) {
  521. for (var i = 0; i < defsUsePending.length; i++) {
  522. var item = defsUsePending[i];
  523. item[0].style[item[1]] = defs[item[2]];
  524. }
  525. }
  526. var numberReg = /-?([0-9]*\.)?[0-9]+([eE]-?[0-9]+)?/g;
  527. function splitNumberSequence(rawStr) {
  528. return rawStr.match(numberReg) || [];
  529. }
  530. var transformRegex = /(translate|scale|rotate|skewX|skewY|matrix)\(([\-\s0-9\.eE,]*)\)/g;
  531. var DEGREE_TO_ANGLE = Math.PI / 180;
  532. function parseTransformAttribute(xmlNode, node) {
  533. var transform = xmlNode.getAttribute('transform');
  534. if (transform) {
  535. transform = transform.replace(/,/g, ' ');
  536. var transformOps_1 = [];
  537. var mt = null;
  538. transform.replace(transformRegex, function (str, type, value) {
  539. transformOps_1.push(type, value);
  540. return '';
  541. });
  542. for (var i = transformOps_1.length - 1; i > 0; i -= 2) {
  543. var value = transformOps_1[i];
  544. var type = transformOps_1[i - 1];
  545. var valueArr = splitNumberSequence(value);
  546. mt = mt || matrix.create();
  547. switch (type) {
  548. case 'translate':
  549. matrix.translate(mt, mt, [parseFloat(valueArr[0]), parseFloat(valueArr[1] || '0')]);
  550. break;
  551. case 'scale':
  552. matrix.scale(mt, mt, [parseFloat(valueArr[0]), parseFloat(valueArr[1] || valueArr[0])]);
  553. break;
  554. case 'rotate':
  555. matrix.rotate(mt, mt, -parseFloat(valueArr[0]) * DEGREE_TO_ANGLE);
  556. break;
  557. case 'skewX':
  558. var sx = Math.tan(parseFloat(valueArr[0]) * DEGREE_TO_ANGLE);
  559. matrix.mul(mt, [1, 0, sx, 1, 0, 0], mt);
  560. break;
  561. case 'skewY':
  562. var sy = Math.tan(parseFloat(valueArr[0]) * DEGREE_TO_ANGLE);
  563. matrix.mul(mt, [1, sy, 0, 1, 0, 0], mt);
  564. break;
  565. case 'matrix':
  566. mt[0] = parseFloat(valueArr[0]);
  567. mt[1] = parseFloat(valueArr[1]);
  568. mt[2] = parseFloat(valueArr[2]);
  569. mt[3] = parseFloat(valueArr[3]);
  570. mt[4] = parseFloat(valueArr[4]);
  571. mt[5] = parseFloat(valueArr[5]);
  572. break;
  573. }
  574. }
  575. node.setLocalTransform(mt);
  576. }
  577. }
  578. var styleRegex = /([^\s:;]+)\s*:\s*([^:;]+)/g;
  579. function parseInlineStyle(xmlNode, inheritableStyleResult, selfStyleResult) {
  580. var style = xmlNode.getAttribute('style');
  581. if (!style) {
  582. return;
  583. }
  584. styleRegex.lastIndex = 0;
  585. var styleRegResult;
  586. while ((styleRegResult = styleRegex.exec(style)) != null) {
  587. var svgStlAttr = styleRegResult[1];
  588. var zrInheritableStlAttr = hasOwn(INHERITABLE_STYLE_ATTRIBUTES_MAP, svgStlAttr)
  589. ? INHERITABLE_STYLE_ATTRIBUTES_MAP[svgStlAttr]
  590. : null;
  591. if (zrInheritableStlAttr) {
  592. inheritableStyleResult[zrInheritableStlAttr] = styleRegResult[2];
  593. }
  594. var zrSelfStlAttr = hasOwn(SELF_STYLE_ATTRIBUTES_MAP, svgStlAttr)
  595. ? SELF_STYLE_ATTRIBUTES_MAP[svgStlAttr]
  596. : null;
  597. if (zrSelfStlAttr) {
  598. selfStyleResult[zrSelfStlAttr] = styleRegResult[2];
  599. }
  600. }
  601. }
  602. function parseAttributeStyle(xmlNode, inheritableStyleResult, selfStyleResult) {
  603. for (var i = 0; i < INHERITABLE_STYLE_ATTRIBUTES_MAP_KEYS.length; i++) {
  604. var svgAttrName = INHERITABLE_STYLE_ATTRIBUTES_MAP_KEYS[i];
  605. var attrValue = xmlNode.getAttribute(svgAttrName);
  606. if (attrValue != null) {
  607. inheritableStyleResult[INHERITABLE_STYLE_ATTRIBUTES_MAP[svgAttrName]] = attrValue;
  608. }
  609. }
  610. for (var i = 0; i < SELF_STYLE_ATTRIBUTES_MAP_KEYS.length; i++) {
  611. var svgAttrName = SELF_STYLE_ATTRIBUTES_MAP_KEYS[i];
  612. var attrValue = xmlNode.getAttribute(svgAttrName);
  613. if (attrValue != null) {
  614. selfStyleResult[SELF_STYLE_ATTRIBUTES_MAP[svgAttrName]] = attrValue;
  615. }
  616. }
  617. }
  618. export function makeViewBoxTransform(viewBoxRect, boundingRect) {
  619. var scaleX = boundingRect.width / viewBoxRect.width;
  620. var scaleY = boundingRect.height / viewBoxRect.height;
  621. var scale = Math.min(scaleX, scaleY);
  622. return {
  623. scale: scale,
  624. x: -(viewBoxRect.x + viewBoxRect.width / 2) * scale + (boundingRect.x + boundingRect.width / 2),
  625. y: -(viewBoxRect.y + viewBoxRect.height / 2) * scale + (boundingRect.y + boundingRect.height / 2)
  626. };
  627. }
  628. export function parseSVG(xml, opt) {
  629. var parser = new SVGParser();
  630. return parser.parse(xml, opt);
  631. }
  632. export { parseXML };