You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

path-data-polyfill.js 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. // @info
  2. // Polyfill for SVG getPathData() and setPathData() methods. Based on:
  3. // - SVGPathSeg polyfill by Philip Rogers (MIT License)
  4. // https://github.com/progers/pathseg
  5. // - SVGPathNormalizer by Tadahisa Motooka (MIT License)
  6. // https://github.com/motooka/SVGPathNormalizer/tree/master/src
  7. // - arcToCubicCurves() by Dmitry Baranovskiy (MIT License)
  8. // https://github.com/DmitryBaranovskiy/raphael/blob/v2.1.1/raphael.core.js#L1837
  9. // @author
  10. // Jarosław Foksa
  11. // @license
  12. // MIT License
  13. if (!SVGPathElement.prototype.getPathData || !SVGPathElement.prototype.setPathData) {
  14. (function () {
  15. var commandsMap = {
  16. "Z": "Z", "M": "M", "L": "L", "C": "C", "Q": "Q", "A": "A", "H": "H", "V": "V", "S": "S", "T": "T",
  17. "z": "Z", "m": "m", "l": "l", "c": "c", "q": "q", "a": "a", "h": "h", "v": "v", "s": "s", "t": "t"
  18. };
  19. var Source = function (string) {
  20. this._string = string;
  21. this._currentIndex = 0;
  22. this._endIndex = this._string.length;
  23. this._prevCommand = null;
  24. this._skipOptionalSpaces();
  25. };
  26. var isIE = window.navigator.userAgent.indexOf("MSIE ") !== -1;
  27. Source.prototype = {
  28. parseSegment: function () {
  29. var char = this._string[this._currentIndex];
  30. var command = commandsMap[char] ? commandsMap[char] : null;
  31. if (command === null) {
  32. // Possibly an implicit command. Not allowed if this is the first command.
  33. if (this._prevCommand === null) {
  34. return null;
  35. }
  36. // Check for remaining coordinates in the current command.
  37. if (
  38. (char === "+" || char === "-" || char === "." || (char >= "0" && char <= "9")) && this._prevCommand !== "Z"
  39. ) {
  40. if (this._prevCommand === "M") {
  41. command = "L";
  42. }
  43. else if (this._prevCommand === "m") {
  44. command = "l";
  45. }
  46. else {
  47. command = this._prevCommand;
  48. }
  49. }
  50. else {
  51. command = null;
  52. }
  53. if (command === null) {
  54. return null;
  55. }
  56. }
  57. else {
  58. this._currentIndex += 1;
  59. }
  60. this._prevCommand = command;
  61. var values = null;
  62. var cmd = command.toUpperCase();
  63. if (cmd === "H" || cmd === "V") {
  64. values = [this._parseNumber()];
  65. }
  66. else if (cmd === "M" || cmd === "L" || cmd === "T") {
  67. values = [this._parseNumber(), this._parseNumber()];
  68. }
  69. else if (cmd === "S" || cmd === "Q") {
  70. values = [this._parseNumber(), this._parseNumber(), this._parseNumber(), this._parseNumber()];
  71. }
  72. else if (cmd === "C") {
  73. values = [
  74. this._parseNumber(),
  75. this._parseNumber(),
  76. this._parseNumber(),
  77. this._parseNumber(),
  78. this._parseNumber(),
  79. this._parseNumber()
  80. ];
  81. }
  82. else if (cmd === "A") {
  83. values = [
  84. this._parseNumber(),
  85. this._parseNumber(),
  86. this._parseNumber(),
  87. this._parseArcFlag(),
  88. this._parseArcFlag(),
  89. this._parseNumber(),
  90. this._parseNumber()
  91. ];
  92. }
  93. else if (cmd === "Z") {
  94. this._skipOptionalSpaces();
  95. values = [];
  96. }
  97. if (values === null || values.indexOf(null) >= 0) {
  98. // Unknown command or known command with invalid values
  99. return null;
  100. }
  101. else {
  102. return { type: command, values: values };
  103. }
  104. },
  105. hasMoreData: function () {
  106. return this._currentIndex < this._endIndex;
  107. },
  108. peekSegmentType: function () {
  109. var char = this._string[this._currentIndex];
  110. return commandsMap[char] ? commandsMap[char] : null;
  111. },
  112. initialCommandIsMoveTo: function () {
  113. // If the path is empty it is still valid, so return true.
  114. if (!this.hasMoreData()) {
  115. return true;
  116. }
  117. var command = this.peekSegmentType();
  118. // Path must start with moveTo.
  119. return command === "M" || command === "m";
  120. },
  121. _isCurrentSpace: function () {
  122. var char = this._string[this._currentIndex];
  123. return char <= " " && (char === " " || char === "\n" || char === "\t" || char === "\r" || char === "\f");
  124. },
  125. _skipOptionalSpaces: function () {
  126. while (this._currentIndex < this._endIndex && this._isCurrentSpace()) {
  127. this._currentIndex += 1;
  128. }
  129. return this._currentIndex < this._endIndex;
  130. },
  131. _skipOptionalSpacesOrDelimiter: function () {
  132. if (
  133. this._currentIndex < this._endIndex &&
  134. !this._isCurrentSpace() &&
  135. this._string[this._currentIndex] !== ","
  136. ) {
  137. return false;
  138. }
  139. if (this._skipOptionalSpaces()) {
  140. if (this._currentIndex < this._endIndex && this._string[this._currentIndex] === ",") {
  141. this._currentIndex += 1;
  142. this._skipOptionalSpaces();
  143. }
  144. }
  145. return this._currentIndex < this._endIndex;
  146. },
  147. // Parse a number from an SVG path. This very closely follows genericParseNumber(...) from
  148. // Source/core/svg/SVGParserUtilities.cpp.
  149. // Spec: http://www.w3.org/TR/SVG11/single-page.html#paths-PathDataBNF
  150. _parseNumber: function () {
  151. var exponent = 0;
  152. var integer = 0;
  153. var frac = 1;
  154. var decimal = 0;
  155. var sign = 1;
  156. var expsign = 1;
  157. var startIndex = this._currentIndex;
  158. this._skipOptionalSpaces();
  159. // Read the sign.
  160. if (this._currentIndex < this._endIndex && this._string[this._currentIndex] === "+") {
  161. this._currentIndex += 1;
  162. }
  163. else if (this._currentIndex < this._endIndex && this._string[this._currentIndex] === "-") {
  164. this._currentIndex += 1;
  165. sign = -1;
  166. }
  167. if (
  168. this._currentIndex === this._endIndex ||
  169. (
  170. (this._string[this._currentIndex] < "0" || this._string[this._currentIndex] > "9") &&
  171. this._string[this._currentIndex] !== "."
  172. )
  173. ) {
  174. // The first character of a number must be one of [0-9+-.].
  175. return null;
  176. }
  177. // Read the integer part, build right-to-left.
  178. var startIntPartIndex = this._currentIndex;
  179. while (
  180. this._currentIndex < this._endIndex &&
  181. this._string[this._currentIndex] >= "0" &&
  182. this._string[this._currentIndex] <= "9"
  183. ) {
  184. this._currentIndex += 1; // Advance to first non-digit.
  185. }
  186. if (this._currentIndex !== startIntPartIndex) {
  187. var scanIntPartIndex = this._currentIndex - 1;
  188. var multiplier = 1;
  189. while (scanIntPartIndex >= startIntPartIndex) {
  190. integer += multiplier * (this._string[scanIntPartIndex] - "0");
  191. scanIntPartIndex -= 1;
  192. multiplier *= 10;
  193. }
  194. }
  195. // Read the decimals.
  196. if (this._currentIndex < this._endIndex && this._string[this._currentIndex] === ".") {
  197. this._currentIndex += 1;
  198. // There must be a least one digit following the .
  199. if (
  200. this._currentIndex >= this._endIndex ||
  201. this._string[this._currentIndex] < "0" ||
  202. this._string[this._currentIndex] > "9"
  203. ) {
  204. return null;
  205. }
  206. while (
  207. this._currentIndex < this._endIndex &&
  208. this._string[this._currentIndex] >= "0" &&
  209. this._string[this._currentIndex] <= "9"
  210. ) {
  211. frac *= 10;
  212. decimal += (this._string.charAt(this._currentIndex) - "0") / frac;
  213. this._currentIndex += 1;
  214. }
  215. }
  216. // Read the exponent part.
  217. if (
  218. this._currentIndex !== startIndex &&
  219. this._currentIndex + 1 < this._endIndex &&
  220. (this._string[this._currentIndex] === "e" || this._string[this._currentIndex] === "E") &&
  221. (this._string[this._currentIndex + 1] !== "x" && this._string[this._currentIndex + 1] !== "m")
  222. ) {
  223. this._currentIndex += 1;
  224. // Read the sign of the exponent.
  225. if (this._string[this._currentIndex] === "+") {
  226. this._currentIndex += 1;
  227. }
  228. else if (this._string[this._currentIndex] === "-") {
  229. this._currentIndex += 1;
  230. expsign = -1;
  231. }
  232. // There must be an exponent.
  233. if (
  234. this._currentIndex >= this._endIndex ||
  235. this._string[this._currentIndex] < "0" ||
  236. this._string[this._currentIndex] > "9"
  237. ) {
  238. return null;
  239. }
  240. while (
  241. this._currentIndex < this._endIndex &&
  242. this._string[this._currentIndex] >= "0" &&
  243. this._string[this._currentIndex] <= "9"
  244. ) {
  245. exponent *= 10;
  246. exponent += (this._string[this._currentIndex] - "0");
  247. this._currentIndex += 1;
  248. }
  249. }
  250. var number = integer + decimal;
  251. number *= sign;
  252. if (exponent) {
  253. number *= Math.pow(10, expsign * exponent);
  254. }
  255. if (startIndex === this._currentIndex) {
  256. return null;
  257. }
  258. this._skipOptionalSpacesOrDelimiter();
  259. return number;
  260. },
  261. _parseArcFlag: function () {
  262. if (this._currentIndex >= this._endIndex) {
  263. return null;
  264. }
  265. var flag = null;
  266. var flagChar = this._string[this._currentIndex];
  267. this._currentIndex += 1;
  268. if (flagChar === "0") {
  269. flag = 0;
  270. }
  271. else if (flagChar === "1") {
  272. flag = 1;
  273. }
  274. else {
  275. return null;
  276. }
  277. this._skipOptionalSpacesOrDelimiter();
  278. return flag;
  279. }
  280. };
  281. var parsePathDataString = function (string) {
  282. if (!string || string.length === 0) return [];
  283. var source = new Source(string);
  284. var pathData = [];
  285. if (source.initialCommandIsMoveTo()) {
  286. while (source.hasMoreData()) {
  287. var pathSeg = source.parseSegment();
  288. if (pathSeg === null) {
  289. break;
  290. }
  291. else {
  292. pathData.push(pathSeg);
  293. }
  294. }
  295. }
  296. return pathData;
  297. }
  298. var setAttribute = SVGPathElement.prototype.setAttribute;
  299. var removeAttribute = SVGPathElement.prototype.removeAttribute;
  300. var $cachedPathData = window.Symbol ? Symbol() : "__cachedPathData";
  301. var $cachedNormalizedPathData = window.Symbol ? Symbol() : "__cachedNormalizedPathData";
  302. // @info
  303. // Get an array of corresponding cubic bezier curve parameters for given arc curve paramters.
  304. var arcToCubicCurves = function (x1, y1, x2, y2, r1, r2, angle, largeArcFlag, sweepFlag, _recursive) {
  305. var degToRad = function (degrees) {
  306. return (Math.PI * degrees) / 180;
  307. };
  308. var rotate = function (x, y, angleRad) {
  309. var X = x * Math.cos(angleRad) - y * Math.sin(angleRad);
  310. var Y = x * Math.sin(angleRad) + y * Math.cos(angleRad);
  311. return { x: X, y: Y };
  312. };
  313. var angleRad = degToRad(angle);
  314. var params = [];
  315. var f1, f2, cx, cy;
  316. if (_recursive) {
  317. f1 = _recursive[0];
  318. f2 = _recursive[1];
  319. cx = _recursive[2];
  320. cy = _recursive[3];
  321. }
  322. else {
  323. var p1 = rotate(x1, y1, -angleRad);
  324. x1 = p1.x;
  325. y1 = p1.y;
  326. var p2 = rotate(x2, y2, -angleRad);
  327. x2 = p2.x;
  328. y2 = p2.y;
  329. var x = (x1 - x2) / 2;
  330. var y = (y1 - y2) / 2;
  331. var h = (x * x) / (r1 * r1) + (y * y) / (r2 * r2);
  332. if (h > 1) {
  333. h = Math.sqrt(h);
  334. r1 = h * r1;
  335. r2 = h * r2;
  336. }
  337. var sign;
  338. if (largeArcFlag === sweepFlag) {
  339. sign = -1;
  340. }
  341. else {
  342. sign = 1;
  343. }
  344. var r1Pow = r1 * r1;
  345. var r2Pow = r2 * r2;
  346. var left = r1Pow * r2Pow - r1Pow * y * y - r2Pow * x * x;
  347. var right = r1Pow * y * y + r2Pow * x * x;
  348. var k = sign * Math.sqrt(Math.abs(left / right));
  349. cx = k * r1 * y / r2 + (x1 + x2) / 2;
  350. cy = k * -r2 * x / r1 + (y1 + y2) / 2;
  351. f1 = Math.asin(parseFloat(((y1 - cy) / r2).toFixed(9)));
  352. f2 = Math.asin(parseFloat(((y2 - cy) / r2).toFixed(9)));
  353. if (x1 < cx) {
  354. f1 = Math.PI - f1;
  355. }
  356. if (x2 < cx) {
  357. f2 = Math.PI - f2;
  358. }
  359. if (f1 < 0) {
  360. f1 = Math.PI * 2 + f1;
  361. }
  362. if (f2 < 0) {
  363. f2 = Math.PI * 2 + f2;
  364. }
  365. if (sweepFlag && f1 > f2) {
  366. f1 = f1 - Math.PI * 2;
  367. }
  368. if (!sweepFlag && f2 > f1) {
  369. f2 = f2 - Math.PI * 2;
  370. }
  371. }
  372. var df = f2 - f1;
  373. if (Math.abs(df) > (Math.PI * 120 / 180)) {
  374. var f2old = f2;
  375. var x2old = x2;
  376. var y2old = y2;
  377. if (sweepFlag && f2 > f1) {
  378. f2 = f1 + (Math.PI * 120 / 180) * (1);
  379. }
  380. else {
  381. f2 = f1 + (Math.PI * 120 / 180) * (-1);
  382. }
  383. x2 = cx + r1 * Math.cos(f2);
  384. y2 = cy + r2 * Math.sin(f2);
  385. params = arcToCubicCurves(x2, y2, x2old, y2old, r1, r2, angle, 0, sweepFlag, [f2, f2old, cx, cy]);
  386. }
  387. df = f2 - f1;
  388. var c1 = Math.cos(f1);
  389. var s1 = Math.sin(f1);
  390. var c2 = Math.cos(f2);
  391. var s2 = Math.sin(f2);
  392. var t = Math.tan(df / 4);
  393. var hx = 4 / 3 * r1 * t;
  394. var hy = 4 / 3 * r2 * t;
  395. var m1 = [x1, y1];
  396. var m2 = [x1 + hx * s1, y1 - hy * c1];
  397. var m3 = [x2 + hx * s2, y2 - hy * c2];
  398. var m4 = [x2, y2];
  399. m2[0] = 2 * m1[0] - m2[0];
  400. m2[1] = 2 * m1[1] - m2[1];
  401. if (_recursive) {
  402. return [m2, m3, m4].concat(params);
  403. }
  404. else {
  405. params = [m2, m3, m4].concat(params);
  406. var curves = [];
  407. for (var i = 0; i < params.length; i += 3) {
  408. var r1 = rotate(params[i][0], params[i][1], angleRad);
  409. var r2 = rotate(params[i + 1][0], params[i + 1][1], angleRad);
  410. var r3 = rotate(params[i + 2][0], params[i + 2][1], angleRad);
  411. curves.push([r1.x, r1.y, r2.x, r2.y, r3.x, r3.y]);
  412. }
  413. return curves;
  414. }
  415. };
  416. var clonePathData = function (pathData) {
  417. return pathData.map(function (seg) {
  418. return { type: seg.type, values: Array.prototype.slice.call(seg.values) }
  419. });
  420. };
  421. // @info
  422. // Takes any path data, returns path data that consists only from absolute commands.
  423. var absolutizePathData = function (pathData) {
  424. var absolutizedPathData = [];
  425. var currentX = null;
  426. var currentY = null;
  427. var subpathX = null;
  428. var subpathY = null;
  429. pathData.forEach(function (seg) {
  430. var type = seg.type;
  431. if (type === "M") {
  432. var x = seg.values[0];
  433. var y = seg.values[1];
  434. absolutizedPathData.push({ type: "M", values: [x, y] });
  435. subpathX = x;
  436. subpathY = y;
  437. currentX = x;
  438. currentY = y;
  439. }
  440. else if (type === "m") {
  441. var x = currentX + seg.values[0];
  442. var y = currentY + seg.values[1];
  443. absolutizedPathData.push({ type: "M", values: [x, y] });
  444. subpathX = x;
  445. subpathY = y;
  446. currentX = x;
  447. currentY = y;
  448. }
  449. else if (type === "L") {
  450. var x = seg.values[0];
  451. var y = seg.values[1];
  452. absolutizedPathData.push({ type: "L", values: [x, y] });
  453. currentX = x;
  454. currentY = y;
  455. }
  456. else if (type === "l") {
  457. var x = currentX + seg.values[0];
  458. var y = currentY + seg.values[1];
  459. absolutizedPathData.push({ type: "L", values: [x, y] });
  460. currentX = x;
  461. currentY = y;
  462. }
  463. else if (type === "C") {
  464. var x1 = seg.values[0];
  465. var y1 = seg.values[1];
  466. var x2 = seg.values[2];
  467. var y2 = seg.values[3];
  468. var x = seg.values[4];
  469. var y = seg.values[5];
  470. absolutizedPathData.push({ type: "C", values: [x1, y1, x2, y2, x, y] });
  471. currentX = x;
  472. currentY = y;
  473. }
  474. else if (type === "c") {
  475. var x1 = currentX + seg.values[0];
  476. var y1 = currentY + seg.values[1];
  477. var x2 = currentX + seg.values[2];
  478. var y2 = currentY + seg.values[3];
  479. var x = currentX + seg.values[4];
  480. var y = currentY + seg.values[5];
  481. absolutizedPathData.push({ type: "C", values: [x1, y1, x2, y2, x, y] });
  482. currentX = x;
  483. currentY = y;
  484. }
  485. else if (type === "Q") {
  486. var x1 = seg.values[0];
  487. var y1 = seg.values[1];
  488. var x = seg.values[2];
  489. var y = seg.values[3];
  490. absolutizedPathData.push({ type: "Q", values: [x1, y1, x, y] });
  491. currentX = x;
  492. currentY = y;
  493. }
  494. else if (type === "q") {
  495. var x1 = currentX + seg.values[0];
  496. var y1 = currentY + seg.values[1];
  497. var x = currentX + seg.values[2];
  498. var y = currentY + seg.values[3];
  499. absolutizedPathData.push({ type: "Q", values: [x1, y1, x, y] });
  500. currentX = x;
  501. currentY = y;
  502. }
  503. else if (type === "A") {
  504. var x = seg.values[5];
  505. var y = seg.values[6];
  506. absolutizedPathData.push({
  507. type: "A",
  508. values: [seg.values[0], seg.values[1], seg.values[2], seg.values[3], seg.values[4], x, y]
  509. });
  510. currentX = x;
  511. currentY = y;
  512. }
  513. else if (type === "a") {
  514. var x = currentX + seg.values[5];
  515. var y = currentY + seg.values[6];
  516. absolutizedPathData.push({
  517. type: "A",
  518. values: [seg.values[0], seg.values[1], seg.values[2], seg.values[3], seg.values[4], x, y]
  519. });
  520. currentX = x;
  521. currentY = y;
  522. }
  523. else if (type === "H") {
  524. var x = seg.values[0];
  525. absolutizedPathData.push({ type: "H", values: [x] });
  526. currentX = x;
  527. }
  528. else if (type === "h") {
  529. var x = currentX + seg.values[0];
  530. absolutizedPathData.push({ type: "H", values: [x] });
  531. currentX = x;
  532. }
  533. else if (type === "V") {
  534. var y = seg.values[0];
  535. absolutizedPathData.push({ type: "V", values: [y] });
  536. currentY = y;
  537. }
  538. else if (type === "v") {
  539. var y = currentY + seg.values[0];
  540. absolutizedPathData.push({ type: "V", values: [y] });
  541. currentY = y;
  542. }
  543. else if (type === "S") {
  544. var x2 = seg.values[0];
  545. var y2 = seg.values[1];
  546. var x = seg.values[2];
  547. var y = seg.values[3];
  548. absolutizedPathData.push({ type: "S", values: [x2, y2, x, y] });
  549. currentX = x;
  550. currentY = y;
  551. }
  552. else if (type === "s") {
  553. var x2 = currentX + seg.values[0];
  554. var y2 = currentY + seg.values[1];
  555. var x = currentX + seg.values[2];
  556. var y = currentY + seg.values[3];
  557. absolutizedPathData.push({ type: "S", values: [x2, y2, x, y] });
  558. currentX = x;
  559. currentY = y;
  560. }
  561. else if (type === "T") {
  562. var x = seg.values[0];
  563. var y = seg.values[1]
  564. absolutizedPathData.push({ type: "T", values: [x, y] });
  565. currentX = x;
  566. currentY = y;
  567. }
  568. else if (type === "t") {
  569. var x = currentX + seg.values[0];
  570. var y = currentY + seg.values[1]
  571. absolutizedPathData.push({ type: "T", values: [x, y] });
  572. currentX = x;
  573. currentY = y;
  574. }
  575. else if (type === "Z" || type === "z") {
  576. absolutizedPathData.push({ type: "Z", values: [] });
  577. currentX = subpathX;
  578. currentY = subpathY;
  579. }
  580. });
  581. return absolutizedPathData;
  582. };
  583. // @info
  584. // Takes path data that consists only from absolute commands, returns path data that consists only from
  585. // "M", "L", "C" and "Z" commands.
  586. var reducePathData = function (pathData) {
  587. var reducedPathData = [];
  588. var lastType = null;
  589. var lastControlX = null;
  590. var lastControlY = null;
  591. var currentX = null;
  592. var currentY = null;
  593. var subpathX = null;
  594. var subpathY = null;
  595. pathData.forEach(function (seg) {
  596. if (seg.type === "M") {
  597. var x = seg.values[0];
  598. var y = seg.values[1];
  599. reducedPathData.push({ type: "M", values: [x, y] });
  600. subpathX = x;
  601. subpathY = y;
  602. currentX = x;
  603. currentY = y;
  604. }
  605. else if (seg.type === "C") {
  606. var x1 = seg.values[0];
  607. var y1 = seg.values[1];
  608. var x2 = seg.values[2];
  609. var y2 = seg.values[3];
  610. var x = seg.values[4];
  611. var y = seg.values[5];
  612. reducedPathData.push({ type: "C", values: [x1, y1, x2, y2, x, y] });
  613. lastControlX = x2;
  614. lastControlY = y2;
  615. currentX = x;
  616. currentY = y;
  617. }
  618. else if (seg.type === "L") {
  619. var x = seg.values[0];
  620. var y = seg.values[1];
  621. reducedPathData.push({ type: "L", values: [x, y] });
  622. currentX = x;
  623. currentY = y;
  624. }
  625. else if (seg.type === "H") {
  626. var x = seg.values[0];
  627. reducedPathData.push({ type: "L", values: [x, currentY] });
  628. currentX = x;
  629. }
  630. else if (seg.type === "V") {
  631. var y = seg.values[0];
  632. reducedPathData.push({ type: "L", values: [currentX, y] });
  633. currentY = y;
  634. }
  635. else if (seg.type === "S") {
  636. var x2 = seg.values[0];
  637. var y2 = seg.values[1];
  638. var x = seg.values[2];
  639. var y = seg.values[3];
  640. var cx1, cy1;
  641. if (lastType === "C" || lastType === "S") {
  642. cx1 = currentX + (currentX - lastControlX);
  643. cy1 = currentY + (currentY - lastControlY);
  644. }
  645. else {
  646. cx1 = currentX;
  647. cy1 = currentY;
  648. }
  649. reducedPathData.push({ type: "C", values: [cx1, cy1, x2, y2, x, y] });
  650. lastControlX = x2;
  651. lastControlY = y2;
  652. currentX = x;
  653. currentY = y;
  654. }
  655. else if (seg.type === "T") {
  656. var x = seg.values[0];
  657. var y = seg.values[1];
  658. var x1, y1;
  659. if (lastType === "Q" || lastType === "T") {
  660. x1 = currentX + (currentX - lastControlX);
  661. y1 = currentY + (currentY - lastControlY);
  662. }
  663. else {
  664. x1 = currentX;
  665. y1 = currentY;
  666. }
  667. var cx1 = currentX + 2 * (x1 - currentX) / 3;
  668. var cy1 = currentY + 2 * (y1 - currentY) / 3;
  669. var cx2 = x + 2 * (x1 - x) / 3;
  670. var cy2 = y + 2 * (y1 - y) / 3;
  671. reducedPathData.push({ type: "C", values: [cx1, cy1, cx2, cy2, x, y] });
  672. lastControlX = x1;
  673. lastControlY = y1;
  674. currentX = x;
  675. currentY = y;
  676. }
  677. else if (seg.type === "Q") {
  678. var x1 = seg.values[0];
  679. var y1 = seg.values[1];
  680. var x = seg.values[2];
  681. var y = seg.values[3];
  682. var cx1 = currentX + 2 * (x1 - currentX) / 3;
  683. var cy1 = currentY + 2 * (y1 - currentY) / 3;
  684. var cx2 = x + 2 * (x1 - x) / 3;
  685. var cy2 = y + 2 * (y1 - y) / 3;
  686. reducedPathData.push({ type: "C", values: [cx1, cy1, cx2, cy2, x, y] });
  687. lastControlX = x1;
  688. lastControlY = y1;
  689. currentX = x;
  690. currentY = y;
  691. }
  692. else if (seg.type === "A") {
  693. var r1 = Math.abs(seg.values[0]);
  694. var r2 = Math.abs(seg.values[1]);
  695. var angle = seg.values[2];
  696. var largeArcFlag = seg.values[3];
  697. var sweepFlag = seg.values[4];
  698. var x = seg.values[5];
  699. var y = seg.values[6];
  700. if (r1 === 0 || r2 === 0) {
  701. reducedPathData.push({ type: "C", values: [currentX, currentY, x, y, x, y] });
  702. currentX = x;
  703. currentY = y;
  704. }
  705. else {
  706. if (currentX !== x || currentY !== y) {
  707. var curves = arcToCubicCurves(currentX, currentY, x, y, r1, r2, angle, largeArcFlag, sweepFlag);
  708. curves.forEach(function (curve) {
  709. reducedPathData.push({ type: "C", values: curve });
  710. });
  711. currentX = x;
  712. currentY = y;
  713. }
  714. }
  715. }
  716. else if (seg.type === "Z") {
  717. reducedPathData.push(seg);
  718. currentX = subpathX;
  719. currentY = subpathY;
  720. }
  721. lastType = seg.type;
  722. });
  723. return reducedPathData;
  724. };
  725. SVGPathElement.prototype.setAttribute = function (name, value) {
  726. if (name === "d") {
  727. this[$cachedPathData] = null;
  728. this[$cachedNormalizedPathData] = null;
  729. }
  730. setAttribute.call(this, name, value);
  731. };
  732. SVGPathElement.prototype.removeAttribute = function (name, value) {
  733. if (name === "d") {
  734. this[$cachedPathData] = null;
  735. this[$cachedNormalizedPathData] = null;
  736. }
  737. removeAttribute.call(this, name);
  738. };
  739. SVGPathElement.prototype.getPathData = function (options) {
  740. if (options && options.normalize) {
  741. if (this[$cachedNormalizedPathData]) {
  742. return clonePathData(this[$cachedNormalizedPathData]);
  743. }
  744. else {
  745. var pathData;
  746. if (this[$cachedPathData]) {
  747. pathData = clonePathData(this[$cachedPathData]);
  748. }
  749. else {
  750. pathData = parsePathDataString(this.getAttribute("d") || "");
  751. this[$cachedPathData] = clonePathData(pathData);
  752. }
  753. var normalizedPathData = reducePathData(absolutizePathData(pathData));
  754. this[$cachedNormalizedPathData] = clonePathData(normalizedPathData);
  755. return normalizedPathData;
  756. }
  757. }
  758. else {
  759. if (this[$cachedPathData]) {
  760. return clonePathData(this[$cachedPathData]);
  761. }
  762. else {
  763. var pathData = parsePathDataString(this.getAttribute("d") || "");
  764. this[$cachedPathData] = clonePathData(pathData);
  765. return pathData;
  766. }
  767. }
  768. };
  769. SVGPathElement.prototype.setPathData = function (pathData) {
  770. if (pathData.length === 0) {
  771. if (isIE) {
  772. // @bugfix https://github.com/mbostock/d3/issues/1737
  773. this.setAttribute("d", "");
  774. }
  775. else {
  776. this.removeAttribute("d");
  777. }
  778. }
  779. else {
  780. var d = "";
  781. for (var i = 0, l = pathData.length; i < l; i += 1) {
  782. var seg = pathData[i];
  783. if (i > 0) {
  784. d += " ";
  785. }
  786. d += seg.type;
  787. if (seg.values && seg.values.length > 0) {
  788. d += " " + seg.values.join(" ");
  789. }
  790. }
  791. this.setAttribute("d", d);
  792. }
  793. };
  794. SVGRectElement.prototype.getPathData = function (options) {
  795. var x = this.x.baseVal.value;
  796. var y = this.y.baseVal.value;
  797. var width = this.width.baseVal.value;
  798. var height = this.height.baseVal.value;
  799. var rx = this.hasAttribute("rx") ? this.rx.baseVal.value : this.ry.baseVal.value;
  800. var ry = this.hasAttribute("ry") ? this.ry.baseVal.value : this.rx.baseVal.value;
  801. if (rx > width / 2) {
  802. rx = width / 2;
  803. }
  804. if (ry > height / 2) {
  805. ry = height / 2;
  806. }
  807. var pathData = [
  808. { type: "M", values: [x + rx, y] },
  809. { type: "H", values: [x + width - rx] },
  810. { type: "A", values: [rx, ry, 0, 0, 1, x + width, y + ry] },
  811. { type: "V", values: [y + height - ry] },
  812. { type: "A", values: [rx, ry, 0, 0, 1, x + width - rx, y + height] },
  813. { type: "H", values: [x + rx] },
  814. { type: "A", values: [rx, ry, 0, 0, 1, x, y + height - ry] },
  815. { type: "V", values: [y + ry] },
  816. { type: "A", values: [rx, ry, 0, 0, 1, x + rx, y] },
  817. { type: "Z", values: [] }
  818. ];
  819. // Get rid of redundant "A" segs when either rx or ry is 0
  820. pathData = pathData.filter(function (s) {
  821. return s.type === "A" && (s.values[0] === 0 || s.values[1] === 0) ? false : true;
  822. });
  823. if (options && options.normalize === true) {
  824. pathData = reducePathData(pathData);
  825. }
  826. return pathData;
  827. };
  828. SVGCircleElement.prototype.getPathData = function (options) {
  829. var cx = this.cx.baseVal.value;
  830. var cy = this.cy.baseVal.value;
  831. var r = this.r.baseVal.value;
  832. var pathData = [
  833. { type: "M", values: [cx + r, cy] },
  834. { type: "A", values: [r, r, 0, 0, 1, cx, cy + r] },
  835. { type: "A", values: [r, r, 0, 0, 1, cx - r, cy] },
  836. { type: "A", values: [r, r, 0, 0, 1, cx, cy - r] },
  837. { type: "A", values: [r, r, 0, 0, 1, cx + r, cy] },
  838. { type: "Z", values: [] }
  839. ];
  840. if (options && options.normalize === true) {
  841. pathData = reducePathData(pathData);
  842. }
  843. return pathData;
  844. };
  845. SVGEllipseElement.prototype.getPathData = function (options) {
  846. var cx = this.cx.baseVal.value;
  847. var cy = this.cy.baseVal.value;
  848. var rx = this.rx.baseVal.value;
  849. var ry = this.ry.baseVal.value;
  850. var pathData = [
  851. { type: "M", values: [cx + rx, cy] },
  852. { type: "A", values: [rx, ry, 0, 0, 1, cx, cy + ry] },
  853. { type: "A", values: [rx, ry, 0, 0, 1, cx - rx, cy] },
  854. { type: "A", values: [rx, ry, 0, 0, 1, cx, cy - ry] },
  855. { type: "A", values: [rx, ry, 0, 0, 1, cx + rx, cy] },
  856. { type: "Z", values: [] }
  857. ];
  858. if (options && options.normalize === true) {
  859. pathData = reducePathData(pathData);
  860. }
  861. return pathData;
  862. };
  863. SVGLineElement.prototype.getPathData = function () {
  864. return [
  865. { type: "M", values: [this.x1.baseVal.value, this.y1.baseVal.value] },
  866. { type: "L", values: [this.x2.baseVal.value, this.y2.baseVal.value] }
  867. ];
  868. };
  869. SVGPolylineElement.prototype.getPathData = function () {
  870. var pathData = [];
  871. for (var i = 0; i < this.points.numberOfItems; i += 1) {
  872. var point = this.points.getItem(i);
  873. pathData.push({
  874. type: (i === 0 ? "M" : "L"),
  875. values: [point.x, point.y]
  876. });
  877. }
  878. return pathData;
  879. };
  880. SVGPolygonElement.prototype.getPathData = function () {
  881. var pathData = [];
  882. for (var i = 0; i < this.points.numberOfItems; i += 1) {
  883. var point = this.points.getItem(i);
  884. pathData.push({
  885. type: (i === 0 ? "M" : "L"),
  886. values: [point.x, point.y]
  887. });
  888. }
  889. pathData.push({
  890. type: "Z",
  891. values: []
  892. });
  893. return pathData;
  894. };
  895. })();
  896. }