forked from enviPath/enviPy
107 lines
3.4 KiB
JavaScript
Executable File
107 lines
3.4 KiB
JavaScript
Executable File
'use strict';
|
|
|
|
// Encode SVG PathData
|
|
// http://www.w3.org/TR/SVG/paths.html#PathDataBNF
|
|
|
|
// Access to SVGPathData constructor
|
|
var SVGPathData = require('./SVGPathData.js');
|
|
|
|
// TransformStream inherance required modules
|
|
var TransformStream = require('readable-stream').Transform;
|
|
var util = require('util');
|
|
|
|
// Private consts : Char groups
|
|
var WSP = ' ';
|
|
|
|
// Inherit of writeable stream
|
|
util.inherits(SVGPathDataEncoder, TransformStream);
|
|
|
|
// Constructor
|
|
function SVGPathDataEncoder(options) {
|
|
|
|
// Ensure new were used
|
|
if(!(this instanceof SVGPathDataEncoder)) {
|
|
return new SVGPathDataEncoder(options);
|
|
}
|
|
|
|
// Parent constructor
|
|
TransformStream.call(this, {
|
|
objectMode: true,
|
|
});
|
|
|
|
// Setting objectMode separately
|
|
this._writableState.objectMode = true;
|
|
this._readableState.objectMode = false;
|
|
|
|
}
|
|
|
|
|
|
// Read method
|
|
SVGPathDataEncoder.prototype._transform = function(commands, encoding, done) {
|
|
var str = '';
|
|
var i;
|
|
var j;
|
|
|
|
if(!(commands instanceof Array)) {
|
|
commands = [commands];
|
|
}
|
|
for(i = 0, j = commands.length; i < j; i++) {
|
|
// Horizontal move to command
|
|
if(commands[i].type === SVGPathData.CLOSE_PATH) {
|
|
str += 'z';
|
|
continue;
|
|
// Horizontal move to command
|
|
} else if(commands[i].type === SVGPathData.HORIZ_LINE_TO) {
|
|
str += (commands[i].relative ? 'h' : 'H') +
|
|
commands[i].x;
|
|
// Vertical move to command
|
|
} else if(commands[i].type === SVGPathData.VERT_LINE_TO) {
|
|
str += (commands[i].relative ? 'v' : 'V') +
|
|
commands[i].y;
|
|
// Move to command
|
|
} else if(commands[i].type === SVGPathData.MOVE_TO) {
|
|
str += (commands[i].relative ? 'm' : 'M') +
|
|
commands[i].x + WSP + commands[i].y;
|
|
// Line to command
|
|
} else if(commands[i].type === SVGPathData.LINE_TO) {
|
|
str += (commands[i].relative ? 'l' : 'L') +
|
|
commands[i].x + WSP + commands[i].y;
|
|
// Curve to command
|
|
} else if(commands[i].type === SVGPathData.CURVE_TO) {
|
|
str += (commands[i].relative ? 'c' : 'C') +
|
|
commands[i].x2 + WSP + commands[i].y2 +
|
|
WSP + commands[i].x1 + WSP + commands[i].y1 +
|
|
WSP + commands[i].x + WSP + commands[i].y;
|
|
// Smooth curve to command
|
|
} else if(commands[i].type === SVGPathData.SMOOTH_CURVE_TO) {
|
|
str += (commands[i].relative ? 's' : 'S') +
|
|
commands[i].x2 + WSP + commands[i].y2 +
|
|
WSP + commands[i].x + WSP + commands[i].y;
|
|
// Quadratic bezier curve to command
|
|
} else if(commands[i].type === SVGPathData.QUAD_TO) {
|
|
str += (commands[i].relative ? 'q' : 'Q') +
|
|
commands[i].x1 + WSP + commands[i].y1 +
|
|
WSP + commands[i].x + WSP + commands[i].y;
|
|
// Smooth quadratic bezier curve to command
|
|
} else if(commands[i].type === SVGPathData.SMOOTH_QUAD_TO) {
|
|
str += (commands[i].relative ? 't' : 'T') +
|
|
commands[i].x + WSP + commands[i].y;
|
|
// Elliptic arc command
|
|
} else if(commands[i].type === SVGPathData.ARC) {
|
|
str += (commands[i].relative ? 'a' : 'A') +
|
|
commands[i].rX + WSP + commands[i].rY +
|
|
WSP + commands[i].xRot +
|
|
WSP + commands[i].lArcFlag + WSP + commands[i].sweepFlag +
|
|
WSP + commands[i].x + WSP + commands[i].y;
|
|
// Unkown command
|
|
} else {
|
|
this.emit('error', new Error('Unexpected command type "' +
|
|
commands[i].type + '" at index ' + i + '.'));
|
|
}
|
|
}
|
|
this.push(new Buffer(str, 'utf8'));
|
|
done();
|
|
};
|
|
|
|
module.exports = SVGPathDataEncoder;
|