UNPKG

@visactor/vrender-core

Version:
64 lines (59 loc) 2.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: !0 }), exports.parseSvgPath = exports.enumCommandMap = void 0; const rePathCommand = /([-+]?((\d+\.\d+)|((\d+)|(\.\d+)))(?:[eE][-+]?\d+)?)/gi, commandLengths = { m: 2, l: 2, h: 1, v: 1, c: 6, s: 4, q: 4, t: 2, a: 7, M: 2, L: 2, H: 1, V: 1, C: 6, S: 4, Q: 4, T: 2, A: 7 }; let currPath, coordsStr, commandChar, coordStr, coordNumber, standardCommandLen; function parseSvgPath(str) { if (!str) return []; const paths = str.match(/[mzlhvcsqta][^mzlhvcsqta]*/gi); if (null === paths) return []; let currCommandData, coordsStrArr; const result = []; for (let i = 0, len = paths.length; i < len; i++) if (currPath = paths[i], coordsStr = currPath.slice(1), commandChar = currPath[0], currCommandData = [ commandChar ], coordsStrArr = coordsStr.match(rePathCommand), null !== coordsStrArr) { for (let i = 0, len = coordsStrArr.length; i < len; i++) coordStr = coordsStrArr[i], coordNumber = parseFloat(coordStr), Number.isNaN(coordNumber) || currCommandData.push(coordNumber); if (standardCommandLen = commandLengths[commandChar], currCommandData.length - 1 > standardCommandLen) { let subCommand, bestCommandChar = commandChar; for (let i = 1, len = currCommandData.length; i < len; i += standardCommandLen) { subCommand = [ bestCommandChar ]; for (let j = i, subLen = i + standardCommandLen; j < subLen; j++) subCommand.push(currCommandData[j]); result.push(subCommand), "m" === bestCommandChar ? bestCommandChar = "l" : "M" === bestCommandChar && (bestCommandChar = "L"); } } else result.push(currCommandData); } else result.push(currCommandData); return result; } exports.enumCommandMap = { A: 0, AT: 1, C: 2, Z: 3, E: 4, L: 5, M: 6, Q: 7, R: 8 }, exports.parseSvgPath = parseSvgPath; //# sourceMappingURL=path-svg.js.map