Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function parseSvg (source) {
- //Path Tokens
- const PT = Object.defineProperties({}, {
- MOVETO : {
- value: 'm',
- writable: false,
- enumerable: true,
- configurable: true
- },
- LINETO : {
- value: 'l',
- writable: false,
- enumerable: true,
- configurable: true
- },
- HORZLINE : {
- value: 'h',
- writable: false,
- enumerable: true,
- configurable: true
- },
- VERTLINE : {
- value: 'v',
- writable: false,
- enumerable: true,
- configurable: true
- },
- CURVETO : {
- value: 'c',
- writable: false,
- enumerable: true,
- configurable: true
- },
- SCURVETO : {
- value: 's',
- writable: false,
- enumerable: true,
- configurable: true
- },
- QUADTO : {
- value: 'q',
- writable: false,
- enumerable: true,
- configurable: true
- },
- SQUADTO : {
- value: 't',
- writable: false,
- enumerable: true,
- configurable: true
- },
- ARCTO : {
- value: 'a',
- writable: false,
- enumerable: true,
- configurable: true
- },
- CLOSESHAPE : {
- value: 'z',
- writable: false,
- enumerable: true,
- configurable: true
- }
- }); // !TableState defineProperties
- let output = '';
- let addFirst = false;
- // split path into tokens
- let rxPaths = /[A-Za-z]\s*(?:(?:-?[.0-9]+\s*?,?\s*?)+)?/gm;
- // split tokens into elements
- let rxTokens = /([A-Za-z])(\s*\S+)/m;
- // split parameters into coordinates
- let rxCoords = /(-|(?<=-))?([.0-9]+)/gm;
- let tokens = source.match(rxPaths);
- let [x, y] = [0, 0];
- let [xs, ys] = [0, 0];
- tokens.forEach((token, i) => {
- let tkMatch = token.match(rxTokens);
- let strCurve='';
- if (tkMatch !== null) {
- let coords = tkMatch[2].match(rxCoords).map(Number);
- switch (tkMatch[1].trim().toLowerCase()) {
- case PT.MOVETO:
- addFirst = true;
- [x, y] = updateTrail(coords, x, y, tkMatch[1] > 'Z');
- break;
- case PT.CURVETO:
- strCurve = getCurveTo(coords, x, y, tkMatch[1] > 'Z');
- [xs, ys] = updateTrail([coords[2], coords[3]], x, y, tkMatch[1] > 'Z');
- [x, y] = updateTrail([coords[4], coords[5]], x, y, tkMatch[1] > 'Z');
- break;
- case PT.SCURVETO:
- strCurve = getSmoothCurveTo(coords, x, y, xs, ys, tkMatch[1] > 'Z');
- [xs, ys] = updateTrail([coords[0], coords[1]], x, y, tkMatch[1] > 'Z');
- [x, y] = updateTrail([coords[2], coords[3]], x, y, tkMatch[1] > 'Z');
- break;
- case PT.LINETO:
- strCurve = getLineTo(coords, x, y, tkMatch[1] > 'Z');
- [x, y] = updateTrail(coords, x, y, tkMatch[1] > 'Z');
- break;
- case PT.HORZLINE:
- strCurve = getHVLine(coords[0], x, y, true, tkMatch[1] > 'Z');
- x = coords[0] + ((tkMatch[1] > 'Z')? x : 0);
- break;
- case PT.VERTLINE:
- strCurve = getHVLine(coords[0], x, y, false, tkMatch[1] > 'Z');
- y = coords[0] + ((tkMatch[1] > 'Z')? y : 0);
- break;
- default:
- throw Error('This path cannot be parsed');
- } // !switch
- } else {
- switch (token.trim().toLowerCase()) {
- case PT.CLOSESHAPE:
- strCurve = '\\infty,\\infty\n\\infty,\\infty\n';
- break;
- default:
- throw Error('This path cannot be parsed');
- } // !switch
- } // if-else
- if (addFirst) {
- addFirst = false;
- output += `${x},${y}\n`;
- }
- output += strCurve;
- });
- //copy(output);
- let col1 = output.match(/\S+\s*(?=,)/gm);
- let col2 = output.match(/(?<=,)\s*\S+/gm);
- // console.log(col1);
- // console.log(col2);
- Calc.setExpression({
- type : 'table',
- columns : [{
- latex : 'x',
- values : (col1)
- }, {
- latex : 'y',
- values : (col2)
- }]
- });
- function updateTrail(pack, x, y, isRel = false) {
- if (isRel) {
- pack = pack.map((item, i) => {
- if (i % 2 === 0) {
- return item + x;
- } else {
- return item + y;
- }
- });
- }
- return pack;
- }
- function getCurveTo(pack, x, y, isRel = false) {
- if (isRel) {
- pack = pack.map((item, i) => {
- if (i % 2 === 0) {
- return item + x;
- } else {
- return item + y;
- }
- });
- }
- return `${pack[0]},${pack[1]}\n${pack[2]},${pack[3]}\n${pack[4]},${pack[5]}\n`;
- };
- function getSmoothCurveTo(pack, x, y, xs, ys, isRel = false) {
- if (isRel) {
- pack = pack.map((item, i) => {
- if (i % 2 === 0) {
- return item + x;
- } else {
- return item + y;
- }
- });
- }
- return `${2 * x - xs},${2 * y - ys}\n${pack[0]},${pack[1]}\n${pack[2]},${pack[3]}\n`;
- };
- function getLineTo(pack, x, y, isRel = false) {
- if (isRel) {
- pack = pack.map((item, i) => {
- if (i % 2 === 0) {
- return item + x;
- } else {
- return item + y;
- }
- });
- }
- return `${(pack[0] + 2 * x) / 3},${(pack[1] + 2 * y) / 3}\n${(2 * pack[0] + x) / 3},${(2 * pack[1] + y) / 3}\n${pack[0]},${pack[1]}\n`;
- };
- function getHVLine(value, x, y, horz = true, isRel = false) {
- let xy = horz ? x : y;
- if (isRel) {
- value += xy;
- }
- if (horz) {
- return `${(x + 2 * value) / 3},${y}\n${(2 * x + value) / 3},${y}\n${value},${y}\n`;
- } else {
- return `${x},${(y + 2 * value) / 3}\n${x},${(2 * y + value) / 3}\n${x},${value}\n`;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment