all files / src/ SVGPathDataEncoder.js

100% Statements 43/43
100% Branches 42/42
100% Functions 2/2
100% Lines 43/43
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107                                53×       52×         52× 52×           51× 51× 51×   51×   51×   409× 43× 43×   366× 24×     342×     336× 46×     290× 136×     154× 121×         33×       24×       18×     11× 10×                   50× 50×      
'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;