Commit 92ca11ac authored by Julian Frosch's avatar Julian Frosch
Browse files

Minor changes

parent 3cb36c17
......@@ -116,7 +116,6 @@ method.approximatePath = function(pathNo) {
sPoint[1] = curves[1][3][1];
// build commands
var commands = this.buildCurveCommands(curves);
// TODO: Test this
// add commands to "new" path
commands.forEach(function(command) {
nPath.push(command);
......@@ -134,7 +133,6 @@ method.approximatePath = function(pathNo) {
sPoint[1] = curves[1][3][1];
// build commands
var commands = this.buildCurveCommands(curves);
// TODO: Test this
// add commands to "new" path
commands.forEach(function(command) {
nPath.push(command);
......
......@@ -2,39 +2,30 @@ var method = SVGParser.prototype;
// empty constructor
function SVGParser() {
this._fs = require('fs');
this._xml2js = require('xml2js');
this._parsedData = null;
}
/**
Takes the SVG-file that was approximated, substitutes it's path's d-attributes and writes it to a new file.
*/
method.writeSVG = function(filename, nPaths, callback) {
var builder = new this._xml2js.Builder();
var parser = new this._xml2js.Parser();
var parse = require('parse-svg-path');
var fs = this._fs;
fs.readFile(this._path, function(err, data) {
if (err) {
callback(err);
} else {
parser.parseString(data, function (err, result) {
if (err) {
callback(err);
} else {
var obj = result;
var oPaths = obj.svg.g[0].path;
for (var i = 0; i < oPaths.length; i++) {
var nPath = nPaths[i];
var data = "";
for (var j = 0; j < nPath.length; j++) {
data += nPath[j][0]+" "+nPath[j][1]+","+nPath[j][2]+" ";
}
oPaths[i].$.d = data;
}
var xml = builder.buildObject(obj);
fs.writeFile(filename, xml, function() {
callback();
})
}
});
}
});
var obj = this._parsedData;
var oPaths = obj.svg.g[0].path;
for (var i = 0; i < oPaths.length; i++) {
var nPath = nPaths[i];
var data = "";
for (var j = 0; j < nPath.length; j++) {
data += nPath[j][0]+" "+nPath[j][1]+","+nPath[j][2]+" ";
}
oPaths[i].$.d = data;
}
var xml = builder.buildObject(obj);
this._fs.writeFile(filename, xml, function() {
callback();
})
}
/**
......@@ -52,10 +43,10 @@ method.writeSVG = function(filename, nPaths, callback) {
[[["m", 96, 363], ["C", 407, 189, 587, 328, 587, 328]], [...], ...]
*/
method.getSVGPaths = function(path, callback) {
this._fs = require('fs');
this._xml2js = require('xml2js');
this._path = path;
var parse = require('parse-svg-path');
// for getting the correct context in anonymous function
var self = this;
var parser = new this._xml2js.Parser();
this._fs.readFile(path, function(err, data) {
......@@ -66,17 +57,17 @@ method.getSVGPaths = function(path, callback) {
if (err) {
callback(err, null);
} else {
// save for later use
self._parsedData = result;
var data = [];
var paths = result.svg.g[0].path;
for (var i = 0; i < paths.length; i++) {
var pathData = paths[i].$.d;
//console.dir(pathData);
var parsedPath = parse(pathData);
data.push(parsedPath);
//console.dir(parsedPath);
//console.log('Done');
}
callback(null, data);
}
......
......@@ -18,12 +18,17 @@ $(function(){
output.insertAdjacentHTML('beforeend', "<p class='error'>" + dateformat(d, "HH:MM:ss") + " - " + text + "</p>");
}
/**
Writes the approximated data (currently) to TXT and SVG.
TODO: maybe put TXT-writing functionality to the parser, too?
*/
var writePathsToFile = function(paths) {
var fs = require('fs');
var path = require('path');
var parsed = path.parse(svg);
var filename = parsed.dir + path.sep + parsed.name + "_approximation.txt";
var stream = fs.createWriteStream(filename);
var filenameBase = parsed.dir + path.sep + parsed.name + "_approximation";
var stream = fs.createWriteStream(filenameBase + ".txt");
stream.once('open', function(fd) {
for (var i = 0; i < paths.length; i++) {
var path = paths[i];
......@@ -41,11 +46,12 @@ $(function(){
stream.write("\n");
}
stream.end();
printOut("Wrote to file " + filename);
printOut("Wrote to file " + filenameBase + ".txt");
});
svgParser.writeSVG("test.svg", paths, function(err) {
printOut("XML done!");
// let the parser write to SVG-file
svgParser.writeSVG(filenameBase + ".svg", paths, function(err) {
printOut("Wrote to file " + filenameBase + ".svg");
})
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment