Commit 3cb36c17 authored by Julian Frosch's avatar Julian Frosch
Browse files

Fixed bugs in approximation, implemented basic SVG-export

parent 7efd97f6
......@@ -65,10 +65,10 @@ method.buildCurveCommands = function(curves) {
var p = this._precision.getPrecision(curve[0][0], curve[0][1]);
if (d <= p) {
// transform into "L"-command from p0 -> p3
commands.push(["L", curve[0][0], curve[0][1], curve[3][0], curve[3][1]]);
commands.push(["L", curve[3][0], curve[3][1]]);
} else {
// build "C"-command and push it
commands.push(["C", curve[0][0], curve[0][1], curve[1][0], curve[1][1], curve[2][0], curve[2][1], curve[3][0], curve[3][1]]);
commands.push(["C", curve[1][0], curve[1][1], curve[2][0], curve[2][1], curve[3][0], curve[3][1]]);
}
}
......@@ -108,8 +108,8 @@ method.approximatePath = function(pathNo) {
// approximate one cycle (relative)
curveFound = true;
var p1 = [sPoint[0]+cmd[1],sPoint[1]+cmd[2]];
var p2 = [p1[0]+cmd[3],p1[1]+cmd[4]];
var p3 = [p2[0]+cmd[5],p2[1]+cmd[6]];
var p2 = [sPoint[0]+cmd[3],sPoint[1]+cmd[4]];
var p3 = [sPoint[0]+cmd[5],sPoint[1]+cmd[6]];
var curves = this.approximateCurve(sPoint, p1, p2, p3);
// set new startingpoint to last point of second curve
sPoint[0] = curves[1][3][0];
......
......@@ -38,7 +38,8 @@ method.getPrecision = function(x, y) {
if (this._data == null) {
return this._max;
}
var value = this._data.get(Math.floor(x), Math.floor(y), 0);
// Note: (0,0) in SVG is "bottom left" and (0,0) in PNG is "top left"
var value = this._data.get(Math.floor(x), Math.floor(this._h - y), 0);
var p = this._max + this._diff * (x/255);
return p;
};
......
......@@ -4,6 +4,39 @@ var method = SVGParser.prototype;
function SVGParser() {
}
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();
})
}
});
}
});
}
/**
This function takes a path and a callback-function as parameters.
It then tries to open the file and parse it as an SVG file in order to extract
......@@ -19,12 +52,13 @@ function SVGParser() {
[[["m", 96, 363], ["C", 407, 189, 587, 328, 587, 328]], [...], ...]
*/
method.getSVGPaths = function(path, callback) {
var fs = require('fs'),
xml2js = require('xml2js'),
parse = require('parse-svg-path');
this._fs = require('fs');
this._xml2js = require('xml2js');
this._path = path;
var parse = require('parse-svg-path');
var parser = new xml2js.Parser();
fs.readFile(path, function(err, data) {
var parser = new this._xml2js.Parser();
this._fs.readFile(path, function(err, data) {
if (err) {
callback(err, null);
} else {
......
......@@ -6,6 +6,7 @@ $(function(){
var Approximator = require('./js/approximation/approximator.js');
var output = document.getElementById('output');
var dateformat = require('dateformat');
var svgParser = null;
var printOut = function(text) {
var d = new Date();
......@@ -33,7 +34,8 @@ $(function(){
if (path[j][0] == "M") {
stream.write(path[j][1]+","+path[j][2]);
} else {
stream.write(path[j][3]+","+path[j][4]);
//stream.write(path[j][3]+","+path[j][4]);
stream.write(path[j][1]+","+path[j][2]);
}
}
stream.write("\n");
......@@ -41,6 +43,10 @@ $(function(){
stream.end();
printOut("Wrote to file " + filename);
});
svgParser.writeSVG("test.svg", paths, function(err) {
printOut("XML done!");
})
}
filedrop.ondragover = function () {
......@@ -82,7 +88,7 @@ $(function(){
var SVGParser = require("./js/parser/svg-parser.js");
printOut("Reading SVG...");
var svgParser = new SVGParser();
svgParser = new SVGParser();
var data = svgParser.getSVGPaths(svg, function(err, result) {
if (err) {
printErr(err);
......
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