0
0
mirror of https://github.com/Doodle3D/doodle3d-client.git synced 2024-11-22 09:17:56 +01:00

Reindent Svg.js.

This commit is contained in:
Wouter R 2014-01-19 17:43:27 +01:00
parent 996a6d5300
commit 304657a104

198
js/Svg.js
View File

@ -9,132 +9,132 @@
//SVG validator: http://validator.w3.org/ //SVG validator: http://validator.w3.org/
//SVG viewer: http://svg-edit.googlecode.com/svn/branches/2.6/editor/svg-editor.html //SVG viewer: http://svg-edit.googlecode.com/svn/branches/2.6/editor/svg-editor.html
function saveToSvg() { function saveToSvg() {
var lastX = 0, lastY = 0, lastIsMove; var lastX = 0, lastY = 0, lastIsMove = false;
var svg = ''; var svg = '';
var boundsWidth = doodleBounds[2] - doodleBounds[0]; var boundsWidth = doodleBounds[2] - doodleBounds[0];
var boundsHeight = doodleBounds[3] - doodleBounds[1]; var boundsHeight = doodleBounds[3] - doodleBounds[1];
svg += '<?xml version="1.0" standalone="no"?>\n'; svg += '<?xml version="1.0" standalone="no"?>\n';
svg += '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n'; svg += '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n';
svg += '<svg width="' + boundsWidth + '" height="' + boundsHeight + '" version="1.1" xmlns="http://www.w3.org/2000/svg">\n'; svg += '<svg width="' + boundsWidth + '" height="' + boundsHeight + '" version="1.1" xmlns="http://www.w3.org/2000/svg">\n';
svg += '\t<desc>Doodle 3D sketch</desc>\n'; svg += '\t<desc>Doodle 3D sketch</desc>\n';
var data = ''; var data = '';
for (var i = 0; i < _points.length; ++i) { for (var i = 0; i < _points.length; ++i) {
var x = _points[i][0], y = _points[i][1], isMove = _points[i][2]; var x = _points[i][0], y = _points[i][1], isMove = _points[i][2];
var dx = x - lastX, dy = y - lastY; var dx = x - lastX, dy = y - lastY;
if (i == 0) if (i == 0)
data += 'M'; //emit absolute move on first pair of coordinates data += 'M'; //emit absolute move on first pair of coordinates
else if (isMove != lastIsMove) else if (isMove != lastIsMove)
data += isMove ? 'm' : 'l'; data += isMove ? 'm' : 'l';
data += dx + ',' + dy + ' '; data += dx + ',' + dy + ' ';
lastX = x; lastX = x;
lastY = y; lastY = y;
lastIsMove = isMove; lastIsMove = isMove;
} }
svg += '\t<path transform="translate(' + -doodleBounds[0] + ',' + -doodleBounds[1] + ')" d="' + data + '" fill="none" stroke="black" stroke-width="2" />\n'; svg += '\t<path transform="translate(' + -doodleBounds[0] + ',' + -doodleBounds[1] + ')" d="' + data + '" fill="none" stroke="black" stroke-width="2" />\n';
var fields = JSON.stringify({'height': numLayers, 'outlineShape': VERTICALSHAPE, 'twist': rStep}); var fields = JSON.stringify({'height': numLayers, 'outlineShape': VERTICALSHAPE, 'twist': rStep});
svg += '\t<!--<![CDATA[d3d-keys ' + fields + ']]>-->\n'; svg += '\t<!--<![CDATA[d3d-keys ' + fields + ']]>-->\n';
svg += '</svg>\n'; svg += '</svg>\n';
return svg; return svg;
} }
//TODO: use local variables instead of _points,numLayers,VERTICALSHAPE and rStep so we can leave a current doodle in tact if an error occurs while parsing //TODO: use local variables instead of _points,numLayers,VERTICALSHAPE and rStep so we can leave a current doodle in tact if an error occurs while parsing
function loadFromSvg(svgData) { function loadFromSvg(svgData) {
var mode = '', x = 0, y = 0; var mode = '', x = 0, y = 0;
console.log("loading " + svgData.length + " bytes of data..."); console.log("loading " + svgData.length + " bytes of data...");
clearDoodle(); clearDoodle();
var p = svgData.indexOf("<path"); var p = svgData.indexOf("<path");
if (p == -1) { console.log("loadFromSvg: could not find parsing start point"); return false; } if (p == -1) { console.log("loadFromSvg: could not find parsing start point"); return false; }
p = svgData.indexOf('d="', p); p = svgData.indexOf('d="', p);
if (p == -1) { console.log("loadFromSvg: could not find parsing start point"); return false; } if (p == -1) { console.log("loadFromSvg: could not find parsing start point"); return false; }
p += 3; //skip 'd="' p += 3; //skip 'd="'
var skipSpace = function() { while (svgData.charAt(p) == ' ') p++; } var skipSpace = function() { while (svgData.charAt(p) == ' ') p++; }
var parseCommand = function() { var parseCommand = function() {
while (true) { while (true) {
skipSpace(); skipSpace();
var c = svgData.charAt(p); var c = svgData.charAt(p);
if (c == 'M' || c == 'm' || c == 'l') { //new command letter if (c == 'M' || c == 'm' || c == 'l') { //new command letter
mode = c; mode = c;
} else if (c == '"') { //end of command chain } else if (c == '"') { //end of command chain
return true; return true;
} else { //something else, must be a pair of coordinates... } else { //something else, must be a pair of coordinates...
var tx = 0, ty = 0, numberEnd = 0, len = 0; var tx = 0, ty = 0, numberEnd = 0, len = 0;
numberEnd = svgData.indexOf(',', p); numberEnd = svgData.indexOf(',', p);
if (numberEnd == -1) { console.log("could not find comma in coordinate pair"); return false; } if (numberEnd == -1) { console.log("could not find comma in coordinate pair"); return false; }
len = numberEnd - p; len = numberEnd - p;
tx = parseInt(svgData.substr(p, len)); tx = parseInt(svgData.substr(p, len));
p += len + 1; p += len + 1;
skipSpace(); skipSpace();
numberEnd = svgData.indexOf(' ', p); numberEnd = svgData.indexOf(' ', p);
if (numberEnd == -1) { console.log("could not find space after coordinate pair"); return false; } if (numberEnd == -1) { console.log("could not find space after coordinate pair"); return false; }
len = numberEnd - p; len = numberEnd - p;
ty = parseInt(svgData.substr(p, len)); ty = parseInt(svgData.substr(p, len));
p += len; p += len;
if (mode == 'M' || mode == 'L') { if (mode == 'M' || mode == 'L') {
x = tx; y = ty; x = tx; y = ty;
} else if (mode == 'm' || mode == 'l') { } else if (mode == 'm' || mode == 'l') {
x += tx; y += ty; x += tx; y += ty;
} else { } else {
console.log("loadFromSvg: found coordinate pair but mode was never set"); console.log("loadFromSvg: found coordinate pair but mode was never set");
return false; return false;
} }
var isMove = mode == 'm' || mode == 'M'; var isMove = mode == 'm' || mode == 'M';
//TODO: create script-wide function for adding points? //TODO: create script-wide function for adding points?
//console.log("inserting "+x+","+y+" ",isMove); //console.log("inserting "+x+","+y+" ",isMove);
updatePrevX = x; updatePrevX = x;
updatePrevY = y; updatePrevY = y;
_points.push([x, y, isMove]); _points.push([x, y, isMove]);
adjustBounds(x, y); adjustBounds(x, y);
adjustPreviewTransformation(); adjustPreviewTransformation();
if (isMove) draw(x, y, .5); if (isMove) draw(x, y, .5);
else draw(x, y); else draw(x, y);
} }
p++; p++;
} }
return true; return true;
}; };
parseCommand(); //depends on value of p, so don't move this without taking that into consideration parseCommand(); //depends on value of p, so don't move this without taking that into consideration
const fieldDefMarker = "<!--<![CDATA[d3d-keys"; const fieldDefMarker = "<!--<![CDATA[d3d-keys";
p = svgData.indexOf(fieldDefMarker); p = svgData.indexOf(fieldDefMarker);
if (p == -1) { console.log("loadFromSvg: could not find metadata marker"); return false; } if (p == -1) { console.log("loadFromSvg: could not find metadata marker"); return false; }
p += fieldDefMarker.length; p += fieldDefMarker.length;
skipSpace(); skipSpace();
var endP = svgData.indexOf("]]>-->", p); var endP = svgData.indexOf("]]>-->", p);
if (endP == -1) { console.log("loadFromSvg: could not find metadata end-marker"); return false; } if (endP == -1) { console.log("loadFromSvg: could not find metadata end-marker"); return false; }
var metaFields = JSON.parse(svgData.substr(p, endP - p)); var metaFields = JSON.parse(svgData.substr(p, endP - p));
//TODO: log error and return false if parsing failed //TODO: log error and return false if parsing failed
for (var k in metaFields) { for (var k in metaFields) {
var v = metaFields[k]; var v = metaFields[k];
switch (k) { switch (k) {
case "height": numLayers = v; break; case "height": numLayers = v; break;
case "outlineShape": VERTICALSHAPE = v; break; case "outlineShape": VERTICALSHAPE = v; break;
case "twist": rStep = v; break; case "twist": rStep = v; break;
} }
} }
renderToImageDataPreview(); renderToImageDataPreview();
return true; return true;
} }