diff --git a/README.md b/README.md index b93f660..624e0ac 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,7 @@ trace.loadImage('path/to/image.png', function(err) { trace.getSVG(); // returns SVG document contents trace.getPathTag(); // will return just tag + trace.getJson(); // returns SVG document contents in JSON format trace.getSymbol('traced-image'); // will return tag with given ID }); @@ -181,4 +182,4 @@ The GNU General Public License version 2 (GPLv2). Please see [License File](LICE [multilevel-thresholding]: http://www.iis.sinica.edu.tw/page/jise/2001/200109_01.pdf [potrace-by-kilobtye]: https://github.com/kilobtye/potrace [potrace-js-demo]: http://kilobtye.github.io/potrace/ -[jimp]: https://github.com/oliver-moran/jimp \ No newline at end of file +[jimp]: https://github.com/oliver-moran/jimp diff --git a/lib/Potrace.js b/lib/Potrace.js index 47cf3d6..920523e 100644 --- a/lib/Potrace.js +++ b/lib/Potrace.js @@ -1143,6 +1143,54 @@ Potrace.prototype = { : '') + '\t' + this.getPathTag(this._params.color, scale) + '\n' + ''; + }, + + /** + * Generates a JSON object with path information + * + * @param {string} [fillColor=auto] - overrides color from parameters + * @param {Object} [scale={ x: 1, y: 1 }] - override svg scale + * @param {number} [scale.x=1] - scale of the x dimension + * @param {number} [scale.y=1] - scale of the y dimension + * @param {String} [stroke=none] - override svg stroke value + * @param {boolean} [json=false] - return result as a json array of paths + * @returns {Array.} + */ + getJson: function(fillColor='auto', scale={ x: 1, y: 1 }, stroke='black') { + fillColor = arguments.length === 0 ? this._params.color : fillColor; + + if (fillColor === Potrace.COLOR_AUTO) { + fillColor = this._params.blackOnWhite ? 'black' : 'white'; + } + + if (!this._imageLoaded) { + throw new Error('Image should be loaded first'); + } + + if (!this._processed) { + this._bmToPathlist(); + this._processPath(); + this._processed = true; + } + + const tags = []; + tags.push({ + width: this._params.width || this._luminanceData.width, + height: this._params.height || this._luminanceData.height + }); + this._pathlist.forEach(path => { + tags.push({ + d: utils.renderCurve(path.curve, scale), + fill: fillColor, + stroke: stroke, + fillRule: 'evenodd' + }); + }); + tags.forEach((path, index) => { + path.id = index; + }); + + return tags; } }; @@ -1167,4 +1215,4 @@ module.exports = Potrace; * Jimp module * @external Jimp * @see https://www.npmjs.com/package/jimp - */ \ No newline at end of file + */