Fix rest of Issue 471: upon loading/setting the SVG, do a depth-first recalculateDimensions() on all nodes

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1380 eee81c28-f429-11dd-99c0-75d572ba1ddd
master
Jeff Schiller 2010-02-12 03:54:49 +00:00
parent 9915407382
commit 8d4861d546
1 changed files with 53 additions and 36 deletions

View File

@ -1369,7 +1369,7 @@ function BatchCommand(text) {
} }
} }
}); });
// recurse to children // recurse to children
i = node.childNodes.length; i = node.childNodes.length;
while (i--) { sanitizeSvg(node.childNodes.item(i)); } while (i--) { sanitizeSvg(node.childNodes.item(i)); }
@ -1923,7 +1923,7 @@ function BatchCommand(text) {
} }
// if this element had no transforms, we are done // if this element had no transforms, we are done
if (tlist.numberOfItems == 0) { if (!tlist || tlist.numberOfItems == 0) {
selected.removeAttribute("transform"); selected.removeAttribute("transform");
return null; return null;
} }
@ -2001,25 +2001,27 @@ function BatchCommand(text) {
m = svgroot.createSVGMatrix(); m = svgroot.createSVGMatrix();
// temporarily strip off the rotate and save the old center // temporarily strip off the rotate and save the old center
var a = gangle * Math.PI / 180; var gangle = canvas.getRotationAngle(selected);
if ( Math.abs(a) > (1.0e-10) ) { if (gangle) {
var s = Math.sin(a)/(1 - Math.cos(a)); var a = gangle * Math.PI / 180;
} else { if ( Math.abs(a) > (1.0e-10) ) {
// FIXME: This blows up if the angle is exactly 0! var s = Math.sin(a)/(1 - Math.cos(a));
var s = 2/a; } else {
} // FIXME: This blows up if the angle is exactly 0!
for (var i = 0; i < tlist.numberOfItems; ++i) { var s = 2/a;
var xform = tlist.getItem(i); }
if (xform.type == 4) { for (var i = 0; i < tlist.numberOfItems; ++i) {
// extract old center through mystical arts var xform = tlist.getItem(i);
var rm = xform.matrix; if (xform.type == 4) {
oldcenter.y = (s*rm.e + rm.f)/2; // extract old center through mystical arts
oldcenter.x = (rm.e - s*rm.f)/2; var rm = xform.matrix;
tlist.removeItem(i); oldcenter.y = (s*rm.e + rm.f)/2;
break; oldcenter.x = (rm.e - s*rm.f)/2;
tlist.removeItem(i);
break;
}
} }
} }
var tx = 0, ty = 0, var tx = 0, ty = 0,
operation = 0, operation = 0,
N = tlist.numberOfItems; N = tlist.numberOfItems;
@ -2234,23 +2236,24 @@ function BatchCommand(text) {
m = svgroot.createSVGMatrix(), m = svgroot.createSVGMatrix(),
// temporarily strip off the rotate and save the old center // temporarily strip off the rotate and save the old center
angle = canvas.getRotationAngle(selected); angle = canvas.getRotationAngle(selected);
if (angle) {
var a = angle * Math.PI / 180; var a = angle * Math.PI / 180;
if ( Math.abs(a) > (1.0e-10) ) { if ( Math.abs(a) > (1.0e-10) ) {
var s = Math.sin(a)/(1 - Math.cos(a)); var s = Math.sin(a)/(1 - Math.cos(a));
} else { } else {
// FIXME: This blows up if the angle is exactly 0! // FIXME: This blows up if the angle is exactly 0!
var s = 2/a; var s = 2/a;
} }
for (var i = 0; i < tlist.numberOfItems; ++i) { for (var i = 0; i < tlist.numberOfItems; ++i) {
var xform = tlist.getItem(i); var xform = tlist.getItem(i);
if (xform.type == 4) { if (xform.type == 4) {
// extract old center through mystical arts // extract old center through mystical arts
var rm = xform.matrix; var rm = xform.matrix;
oldcenter.y = (s*rm.e + rm.f)/2; oldcenter.y = (s*rm.e + rm.f)/2;
oldcenter.x = (rm.e - s*rm.f)/2; oldcenter.x = (rm.e - s*rm.f)/2;
tlist.removeItem(i); tlist.removeItem(i);
break; break;
}
} }
} }
@ -5555,6 +5558,20 @@ function BatchCommand(text) {
canvas.fixOperaXML(svgcontent, newDoc.documentElement); canvas.fixOperaXML(svgcontent, newDoc.documentElement);
} }
// recalculate dimensions on the top-level children so that unnecessary transforms
// are removed
var deepdive = function(node) {
if (node.nodeType == 1) {
var children = node.children;
var i = children.length;
while (i--) { deepdive(children.item(i)); }
try {
recalculateDimensions(node);
} catch(e) { console.log(e); }
}
}
deepdive(svgcontent);
var content = $(svgcontent); var content = $(svgcontent);
// determine proper size // determine proper size