123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440 |
- var Path = require("../graphic/Path");
- var PathProxy = require("../core/PathProxy");
- var transformPath = require("./transformPath");
- // command chars
- // var cc = [
- // 'm', 'M', 'l', 'L', 'v', 'V', 'h', 'H', 'z', 'Z',
- // 'c', 'C', 'q', 'Q', 't', 'T', 's', 'S', 'a', 'A'
- // ];
- var mathSqrt = Math.sqrt;
- var mathSin = Math.sin;
- var mathCos = Math.cos;
- var PI = Math.PI;
- var vMag = function (v) {
- return Math.sqrt(v[0] * v[0] + v[1] * v[1]);
- };
- var vRatio = function (u, v) {
- return (u[0] * v[0] + u[1] * v[1]) / (vMag(u) * vMag(v));
- };
- var vAngle = function (u, v) {
- return (u[0] * v[1] < u[1] * v[0] ? -1 : 1) * Math.acos(vRatio(u, v));
- };
- function processArc(x1, y1, x2, y2, fa, fs, rx, ry, psiDeg, cmd, path) {
- var psi = psiDeg * (PI / 180.0);
- var xp = mathCos(psi) * (x1 - x2) / 2.0 + mathSin(psi) * (y1 - y2) / 2.0;
- var yp = -1 * mathSin(psi) * (x1 - x2) / 2.0 + mathCos(psi) * (y1 - y2) / 2.0;
- var lambda = xp * xp / (rx * rx) + yp * yp / (ry * ry);
- if (lambda > 1) {
- rx *= mathSqrt(lambda);
- ry *= mathSqrt(lambda);
- }
- var f = (fa === fs ? -1 : 1) * mathSqrt((rx * rx * (ry * ry) - rx * rx * (yp * yp) - ry * ry * (xp * xp)) / (rx * rx * (yp * yp) + ry * ry * (xp * xp))) || 0;
- var cxp = f * rx * yp / ry;
- var cyp = f * -ry * xp / rx;
- var cx = (x1 + x2) / 2.0 + mathCos(psi) * cxp - mathSin(psi) * cyp;
- var cy = (y1 + y2) / 2.0 + mathSin(psi) * cxp + mathCos(psi) * cyp;
- var theta = vAngle([1, 0], [(xp - cxp) / rx, (yp - cyp) / ry]);
- var u = [(xp - cxp) / rx, (yp - cyp) / ry];
- var v = [(-1 * xp - cxp) / rx, (-1 * yp - cyp) / ry];
- var dTheta = vAngle(u, v);
- if (vRatio(u, v) <= -1) {
- dTheta = PI;
- }
- if (vRatio(u, v) >= 1) {
- dTheta = 0;
- }
- if (fs === 0 && dTheta > 0) {
- dTheta = dTheta - 2 * PI;
- }
- if (fs === 1 && dTheta < 0) {
- dTheta = dTheta + 2 * PI;
- }
- path.addData(cmd, cx, cy, rx, ry, theta, dTheta, psi, fs);
- }
- var commandReg = /([mlvhzcqtsa])([^mlvhzcqtsa]*)/ig; // Consider case:
- // (1) delimiter can be comma or space, where continuous commas
- // or spaces should be seen as one comma.
- // (2) value can be like:
- // '2e-4', 'l.5.9' (ignore 0), 'M-10-10', 'l-2.43e-1,34.9983',
- // 'l-.5E1,54', '121-23-44-11' (no delimiter)
- var numberReg = /-?([0-9]*\.)?[0-9]+([eE]-?[0-9]+)?/g; // var valueSplitReg = /[\s,]+/;
- function createPathProxyFromString(data) {
- if (!data) {
- return new PathProxy();
- } // var data = data.replace(/-/g, ' -')
- // .replace(/ /g, ' ')
- // .replace(/ /g, ',')
- // .replace(/,,/g, ',');
- // var n;
- // create pipes so that we can split the data
- // for (n = 0; n < cc.length; n++) {
- // cs = cs.replace(new RegExp(cc[n], 'g'), '|' + cc[n]);
- // }
- // data = data.replace(/-/g, ',-');
- // create array
- // var arr = cs.split('|');
- // init context point
- var cpx = 0;
- var cpy = 0;
- var subpathX = cpx;
- var subpathY = cpy;
- var prevCmd;
- var path = new PathProxy();
- var CMD = PathProxy.CMD; // commandReg.lastIndex = 0;
- // var cmdResult;
- // while ((cmdResult = commandReg.exec(data)) != null) {
- // var cmdStr = cmdResult[1];
- // var cmdContent = cmdResult[2];
- var cmdList = data.match(commandReg);
- for (var l = 0; l < cmdList.length; l++) {
- var cmdText = cmdList[l];
- var cmdStr = cmdText.charAt(0);
- var cmd; // String#split is faster a little bit than String#replace or RegExp#exec.
- // var p = cmdContent.split(valueSplitReg);
- // var pLen = 0;
- // for (var i = 0; i < p.length; i++) {
- // // '' and other invalid str => NaN
- // var val = parseFloat(p[i]);
- // !isNaN(val) && (p[pLen++] = val);
- // }
- var p = cmdText.match(numberReg) || [];
- var pLen = p.length;
- for (var i = 0; i < pLen; i++) {
- p[i] = parseFloat(p[i]);
- }
- var off = 0;
- while (off < pLen) {
- var ctlPtx;
- var ctlPty;
- var rx;
- var ry;
- var psi;
- var fa;
- var fs;
- var x1 = cpx;
- var y1 = cpy; // convert l, H, h, V, and v to L
- switch (cmdStr) {
- case 'l':
- cpx += p[off++];
- cpy += p[off++];
- cmd = CMD.L;
- path.addData(cmd, cpx, cpy);
- break;
- case 'L':
- cpx = p[off++];
- cpy = p[off++];
- cmd = CMD.L;
- path.addData(cmd, cpx, cpy);
- break;
- case 'm':
- cpx += p[off++];
- cpy += p[off++];
- cmd = CMD.M;
- path.addData(cmd, cpx, cpy);
- subpathX = cpx;
- subpathY = cpy;
- cmdStr = 'l';
- break;
- case 'M':
- cpx = p[off++];
- cpy = p[off++];
- cmd = CMD.M;
- path.addData(cmd, cpx, cpy);
- subpathX = cpx;
- subpathY = cpy;
- cmdStr = 'L';
- break;
- case 'h':
- cpx += p[off++];
- cmd = CMD.L;
- path.addData(cmd, cpx, cpy);
- break;
- case 'H':
- cpx = p[off++];
- cmd = CMD.L;
- path.addData(cmd, cpx, cpy);
- break;
- case 'v':
- cpy += p[off++];
- cmd = CMD.L;
- path.addData(cmd, cpx, cpy);
- break;
- case 'V':
- cpy = p[off++];
- cmd = CMD.L;
- path.addData(cmd, cpx, cpy);
- break;
- case 'C':
- cmd = CMD.C;
- path.addData(cmd, p[off++], p[off++], p[off++], p[off++], p[off++], p[off++]);
- cpx = p[off - 2];
- cpy = p[off - 1];
- break;
- case 'c':
- cmd = CMD.C;
- path.addData(cmd, p[off++] + cpx, p[off++] + cpy, p[off++] + cpx, p[off++] + cpy, p[off++] + cpx, p[off++] + cpy);
- cpx += p[off - 2];
- cpy += p[off - 1];
- break;
- case 'S':
- ctlPtx = cpx;
- ctlPty = cpy;
- var len = path.len();
- var pathData = path.data;
- if (prevCmd === CMD.C) {
- ctlPtx += cpx - pathData[len - 4];
- ctlPty += cpy - pathData[len - 3];
- }
- cmd = CMD.C;
- x1 = p[off++];
- y1 = p[off++];
- cpx = p[off++];
- cpy = p[off++];
- path.addData(cmd, ctlPtx, ctlPty, x1, y1, cpx, cpy);
- break;
- case 's':
- ctlPtx = cpx;
- ctlPty = cpy;
- var len = path.len();
- var pathData = path.data;
- if (prevCmd === CMD.C) {
- ctlPtx += cpx - pathData[len - 4];
- ctlPty += cpy - pathData[len - 3];
- }
- cmd = CMD.C;
- x1 = cpx + p[off++];
- y1 = cpy + p[off++];
- cpx += p[off++];
- cpy += p[off++];
- path.addData(cmd, ctlPtx, ctlPty, x1, y1, cpx, cpy);
- break;
- case 'Q':
- x1 = p[off++];
- y1 = p[off++];
- cpx = p[off++];
- cpy = p[off++];
- cmd = CMD.Q;
- path.addData(cmd, x1, y1, cpx, cpy);
- break;
- case 'q':
- x1 = p[off++] + cpx;
- y1 = p[off++] + cpy;
- cpx += p[off++];
- cpy += p[off++];
- cmd = CMD.Q;
- path.addData(cmd, x1, y1, cpx, cpy);
- break;
- case 'T':
- ctlPtx = cpx;
- ctlPty = cpy;
- var len = path.len();
- var pathData = path.data;
- if (prevCmd === CMD.Q) {
- ctlPtx += cpx - pathData[len - 4];
- ctlPty += cpy - pathData[len - 3];
- }
- cpx = p[off++];
- cpy = p[off++];
- cmd = CMD.Q;
- path.addData(cmd, ctlPtx, ctlPty, cpx, cpy);
- break;
- case 't':
- ctlPtx = cpx;
- ctlPty = cpy;
- var len = path.len();
- var pathData = path.data;
- if (prevCmd === CMD.Q) {
- ctlPtx += cpx - pathData[len - 4];
- ctlPty += cpy - pathData[len - 3];
- }
- cpx += p[off++];
- cpy += p[off++];
- cmd = CMD.Q;
- path.addData(cmd, ctlPtx, ctlPty, cpx, cpy);
- break;
- case 'A':
- rx = p[off++];
- ry = p[off++];
- psi = p[off++];
- fa = p[off++];
- fs = p[off++];
- x1 = cpx, y1 = cpy;
- cpx = p[off++];
- cpy = p[off++];
- cmd = CMD.A;
- processArc(x1, y1, cpx, cpy, fa, fs, rx, ry, psi, cmd, path);
- break;
- case 'a':
- rx = p[off++];
- ry = p[off++];
- psi = p[off++];
- fa = p[off++];
- fs = p[off++];
- x1 = cpx, y1 = cpy;
- cpx += p[off++];
- cpy += p[off++];
- cmd = CMD.A;
- processArc(x1, y1, cpx, cpy, fa, fs, rx, ry, psi, cmd, path);
- break;
- }
- }
- if (cmdStr === 'z' || cmdStr === 'Z') {
- cmd = CMD.Z;
- path.addData(cmd); // z may be in the middle of the path.
- cpx = subpathX;
- cpy = subpathY;
- }
- prevCmd = cmd;
- }
- path.toStatic();
- return path;
- } // TODO Optimize double memory cost problem
- function createPathOptions(str, opts) {
- var pathProxy = createPathProxyFromString(str);
- opts = opts || {};
- opts.buildPath = function (path) {
- if (path.setData) {
- path.setData(pathProxy.data); // Svg and vml renderer don't have context
- var ctx = path.getContext();
- if (ctx) {
- path.rebuildPath(ctx);
- }
- } else {
- var ctx = path;
- pathProxy.rebuildPath(ctx);
- }
- };
- opts.applyTransform = function (m) {
- transformPath(pathProxy, m);
- this.dirty(true);
- };
- return opts;
- }
- /**
- * Create a Path object from path string data
- * http://www.w3.org/TR/SVG/paths.html#PathData
- * @param {Object} opts Other options
- */
- function createFromString(str, opts) {
- return new Path(createPathOptions(str, opts));
- }
- /**
- * Create a Path class from path string data
- * @param {string} str
- * @param {Object} opts Other options
- */
- function extendFromString(str, opts) {
- return Path.extend(createPathOptions(str, opts));
- }
- /**
- * Merge multiple paths
- */
- // TODO Apply transform
- // TODO stroke dash
- // TODO Optimize double memory cost problem
- function mergePath(pathEls, opts) {
- var pathList = [];
- var len = pathEls.length;
- for (var i = 0; i < len; i++) {
- var pathEl = pathEls[i];
- if (!pathEl.path) {
- pathEl.createPathProxy();
- }
- if (pathEl.__dirtyPath) {
- pathEl.buildPath(pathEl.path, pathEl.shape, true);
- }
- pathList.push(pathEl.path);
- }
- var pathBundle = new Path(opts); // Need path proxy.
- pathBundle.createPathProxy();
- pathBundle.buildPath = function (path) {
- path.appendPath(pathList); // Svg and vml renderer don't have context
- var ctx = path.getContext();
- if (ctx) {
- path.rebuildPath(ctx);
- }
- };
- return pathBundle;
- }
- exports.createFromString = createFromString;
- exports.extendFromString = extendFromString;
- exports.mergePath = mergePath;
|