svg-path-d
Version:
SVG path data (path[d] attribute content) manipulation library.
134 lines • 3.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTokens = void 0;
var RE_COMMAND = /([MLHVZCSQTA])/gi;
var RE_FLAG = /[01]/;
var RE_SIGNED = /[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?/;
var RE_UNSIGNED = /[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?/;
/**
* [Path Doc](https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Paths)
*/
var REXPS = {
// Move To:
// M x y
// m dx dy
M: [RE_SIGNED, RE_SIGNED],
// Line To:
// L x y
// l dx dy
L: [RE_SIGNED, RE_SIGNED],
// Horizontal Line To:
// H x
// h dx
H: [RE_SIGNED],
// Vertical Line To:
// V y
// v dy
V: [RE_SIGNED],
// Close Path.
Z: [],
// Bézier curves:
// Curve To:
// C x1 y1, x2 y2, x y
// c dx1 dy1, dx2 dy2, dx dy
C: [RE_SIGNED, RE_SIGNED, RE_SIGNED, RE_SIGNED, RE_SIGNED, RE_SIGNED],
// Shortcut Curve To:
// S x2 y2, x y
// s dx2 dy2, dx dy
S: [RE_SIGNED, RE_SIGNED, RE_SIGNED, RE_SIGNED],
// Quadratic Curve To:
// Q x1 y1, x y
// q dx1 dy1, dx dy
Q: [RE_SIGNED, RE_SIGNED, RE_SIGNED, RE_SIGNED],
// Shortcut Quadratic Curve To:
// T x y
// t dx dy
T: [RE_SIGNED, RE_SIGNED],
// Arc To:
// A rx ry x-axis-rotation large-arc-flag sweep-flag x y
// a rx ry x-axis-rotation large-arc-flag sweep-flag dx dy
A: [RE_UNSIGNED, RE_UNSIGNED, RE_SIGNED, RE_FLAG, RE_FLAG, RE_SIGNED, RE_SIGNED],
};
/**
* Helper class.
*/
var Reader = /** @class */ (function () {
function Reader() {
// tslint:disable-next-line: variable-name
this._data = '';
}
Object.defineProperty(Reader.prototype, "data", {
get: function () {
return this._data;
},
set: function (value) {
this._data = value.trim();
},
enumerable: false,
configurable: true
});
Reader.prototype.moveTo = function (start) {
this.data = this.data.slice(start);
};
Reader.prototype.read = function (exp) {
exp.lastIndex = 0;
var m = exp.exec(this.data);
if (m !== null) {
var value = m[0];
this.moveTo(m.index + value.length);
return value;
}
return null;
};
Reader.prototype.readAll = function (exps) {
var buf = [];
for (var _i = 0, exps_1 = exps; _i < exps_1.length; _i++) {
var exp = exps_1[_i];
var value = this.read(exp);
if (value !== null) {
buf.push(value);
}
else {
return null;
}
}
return buf;
};
return Reader;
}());
function getTokens(pathData) {
var tokens = [];
// Split by command.
var split = pathData.split(RE_COMMAND);
var reader = new Reader();
for (var i = 2; i < split.length; i += 2) {
var command = split[i - 1];
reader.data = split[i];
while (true) {
var name_1 = command.toUpperCase();
var relative = name_1 !== command;
var args = reader.readAll(REXPS[name_1]);
if (args !== null) {
tokens.push({ name: name_1, relative: relative, args: args });
}
else {
// console.warn('Couldn\'t properly parse this expression:', reader.data);
break;
}
if (!reader.data || args.length === 0) {
break;
}
else {
if (command === 'M') {
command = 'L';
}
else if (command === 'm') {
command = 'l';
}
}
}
}
return tokens;
}
exports.getTokens = getTokens;
//# sourceMappingURL=parser.js.map