all files / src/ SVGPathData.js

100% Statements 81/81
100% Branches 6/6
100% Functions 19/19
100% Lines 81/81
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152    218×     50×     17×       12×     11×                 13×       10×                                     93× 93× 93× 93× 93×   93×   82× 687× 687×   683×     82× 82×       50× 50×   50× 100×   100× 150× 150× 50×       50× 50× 50×     218× 218×   218× 921×   921× 1664× 1664× 743×       218× 208× 178×                      
'use strict';
 
function SVGPathData(content) {
  this.commands = SVGPathData.parse(content);
}
 
SVGPathData.prototype.encode = function() {
  return SVGPathData.encode(this.commands);
};
 
SVGPathData.prototype.round = function() {
  return this.transform.apply(this, [SVGPathData.Transformer.ROUND].concat(
    [].slice.call(arguments, 0)));
};
 
SVGPathData.prototype.toAbs = function() {
  return this.transform(SVGPathData.Transformer.TO_ABS);
};
 
SVGPathData.prototype.toRel = function() {
  return this.transform(SVGPathData.Transformer.TO_REL);
};
 
SVGPathData.prototype.translate = function() {
  return this.transform.apply(this, [SVGPathData.Transformer.TRANSLATE].concat(
    [].slice.call(arguments, 0)));
};
 
SVGPathData.prototype.scale = function() {
  return this.transform.apply(this, [SVGPathData.Transformer.SCALE].concat(
    [].slice.call(arguments, 0)));
};
 
SVGPathData.prototype.rotate = function() {
  return this.transform.apply(this, [SVGPathData.Transformer.ROTATE].concat(
    [].slice.call(arguments, 0)));
};
 
SVGPathData.prototype.matrix = function() {
  return this.transform.apply(this, [SVGPathData.Transformer.MATRIX].concat(
    [].slice.call(arguments, 0)));
};
 
SVGPathData.prototype.skewX = function() {
  return this.transform.apply(this, [SVGPathData.Transformer.SKEW_X].concat(
    [].slice.call(arguments, 0)));
};
 
SVGPathData.prototype.skewY = function() {
  return this.transform.apply(this, [SVGPathData.Transformer.SKEW_Y].concat(
    [].slice.call(arguments, 0)));
};
 
SVGPathData.prototype.xSymetry = function() {
  return this.transform.apply(this, [SVGPathData.Transformer.X_AXIS_SIMETRY].concat(
    [].slice.call(arguments, 0)));
};
 
SVGPathData.prototype.ySymetry = function() {
  return this.transform.apply(this, [SVGPathData.Transformer.Y_AXIS_SIMETRY].concat(
    [].slice.call(arguments, 0)));
};
 
SVGPathData.prototype.aToC = function() {
  return this.transform.apply(this, [SVGPathData.Transformer.A_TO_C].concat(
    [].slice.call(arguments, 0)));
};
 
SVGPathData.prototype.transform = function(transformFunction) {
  var newCommands = [];
  var curCommands = [];
  var commands = this.commands;
  var i;
  var ii;
 
  transformFunction = transformFunction.apply(null, [].slice.call(arguments, 1));
 
  for(i = 0, ii = commands.length; i < ii; i++) {
    curCommands = transformFunction(commands[i]);
    if(curCommands instanceof Array) {
      newCommands = newCommands.concat(curCommands);
    } else {
      newCommands.push(curCommands);
    }
  }
  this.commands = newCommands;
  return this;
};
 
// Static methods
SVGPathData.encode = function(commands) {
  var content = '';
  var encoder = new SVGPathData.Encoder();
 
  encoder.on('readable', function() {
    var str;
 
    do {
      str = encoder.read();
      if(null !== str) {
        content += str;
      }
    } while(null !== str);
  });
  encoder.write(commands);
  encoder.end();
  return content;
};
 
SVGPathData.parse = function(content) {
  var commands = [];
  var parser = new SVGPathData.Parser();
 
  parser.on('readable', function() {
    var command;
 
    do {
      command = parser.read();
      if(null !== command) {
        commands.push(command);
      }
    } while(null !== command);
  });
  parser.write(content);
  parser.end();
  return commands;
};
 
// Commands static vars
SVGPathData.CLOSE_PATH = 1;
SVGPathData.MOVE_TO = 2;
SVGPathData.HORIZ_LINE_TO = 4;
SVGPathData.VERT_LINE_TO = 8;
SVGPathData.LINE_TO = 16;
SVGPathData.CURVE_TO = 32;
SVGPathData.SMOOTH_CURVE_TO = 64;
SVGPathData.QUAD_TO = 128;
SVGPathData.SMOOTH_QUAD_TO = 256;
SVGPathData.ARC = 512;
SVGPathData.DRAWING_COMMANDS =
  SVGPathData.HORIZ_LINE_TO | SVGPathData.VERT_LINE_TO | SVGPathData.LINE_TO |
  SVGPathData.CURVE_TO | SVGPathData.SMOOTH_CURVE_TO | SVGPathData.QUAD_TO |
  SVGPathData.SMOOTH_QUAD_TO | SVGPathData.ARC;
 
// Export the main contructor first (tests are failing otherwise)
module.exports = SVGPathData;
 
// Expose the internal constructors
SVGPathData.Parser = require('./SVGPathDataParser.js');
SVGPathData.Encoder = require('./SVGPathDataEncoder.js');
SVGPathData.Transformer = require('./SVGPathDataTransformer.js');