Made some optimizations and fixed issue 716: Rotate, move, break link messes up image library image shape

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1811 eee81c28-f429-11dd-99c0-75d572ba1ddd
This commit is contained in:
Alexis Deveria
2010-10-20 13:53:43 +00:00
parent 331659ead0
commit 7e6b6706cf

View File

@@ -1287,6 +1287,7 @@ var SelectorManager;
var bShow = show ? "inline" : "none"; var bShow = show ? "inline" : "none";
selectorManager.selectorGripsGroup.setAttribute("display", bShow); selectorManager.selectorGripsGroup.setAttribute("display", bShow);
var elem = this.selectedElement; var elem = this.selectedElement;
this.hasGrips = show;
if(elem && show) { if(elem && show) {
this.selectorGroup.appendChild(selectorManager.selectorGripsGroup); this.selectorGroup.appendChild(selectorManager.selectorGripsGroup);
this.updateGripCursors(getRotationAngle(elem)); this.updateGripCursors(getRotationAngle(elem));
@@ -1323,17 +1324,21 @@ var SelectorManager;
mgr = selectorManager, mgr = selectorManager,
selectedGrips = mgr.selectorGrips, selectedGrips = mgr.selectorGrips,
selected = this.selectedElement, selected = this.selectedElement,
sw = selected.getAttribute("stroke-width"); sw = selected.getAttribute("stroke-width");
var offset = 1/current_zoom; var offset = 1/current_zoom;
if (selected.getAttribute("stroke") !== "none" && !isNaN(sw)) { if (selected.getAttribute("stroke") !== "none" && !isNaN(sw)) {
offset += (sw/2); offset += (sw/2);
} }
if (selected.tagName == "text") { var tagName = selected.tagName;
if (tagName === "text") {
offset += 2/current_zoom; offset += 2/current_zoom;
} }
var bbox = getBBox(selected); var bbox = getBBox(selected);
if(selected.tagName == 'g' && !$(selected).data('gsvg')) {
if(tagName === 'g' && !elData(selected, 'gsvg')) {
// The bbox for a group does not include stroke vals, so we // The bbox for a group does not include stroke vals, so we
// get the bbox based on its children. // get the bbox based on its children.
var stroked_bbox = getStrokedBBox(selected.childNodes); var stroked_bbox = getStrokedBBox(selected.childNodes);
@@ -1341,7 +1346,6 @@ var SelectorManager;
bbox = stroked_bbox; bbox = stroked_bbox;
} }
} }
// loop and transform our bounding box until we reach our first rotation // loop and transform our bounding box until we reach our first rotation
var m = getMatrix(selected); var m = getMatrix(selected);
@@ -1360,10 +1364,11 @@ var SelectorManager;
//* //*
var nbox = transformBox(l*current_zoom, t*current_zoom, w*current_zoom, h*current_zoom, m), var nbox = transformBox(l*current_zoom, t*current_zoom, w*current_zoom, h*current_zoom, m),
nbax = nbox.aabox.x - offset, aabox = nbox.aabox,
nbay = nbox.aabox.y - offset, nbax = aabox.x - offset,
nbaw = nbox.aabox.width + (offset * 2), nbay = aabox.y - offset,
nbah = nbox.aabox.height + (offset * 2); nbaw = aabox.width + (offset * 2),
nbah = aabox.height + (offset * 2);
// now if the shape is rotated, un-rotate it // now if the shape is rotated, un-rotate it
var cx = nbax + nbaw/2, var cx = nbax + nbaw/2,
@@ -1381,15 +1386,18 @@ var SelectorManager;
nbox.br = transformPoint(nbox.br.x,nbox.br.y,rotm); nbox.br = transformPoint(nbox.br.x,nbox.br.y,rotm);
// calculate the axis-aligned bbox // calculate the axis-aligned bbox
var minx = nbox.tl.x, var tl = nbox.tl;
miny = nbox.tl.y, var minx = tl.x,
maxx = nbox.tl.x, miny = tl.y,
maxy = nbox.tl.y; maxx = tl.x,
maxy = tl.y;
minx = Math.min(minx, Math.min(nbox.tr.x, Math.min(nbox.bl.x, nbox.br.x) ) ) - offset; var Min = Math.min, Max = Math.max;
miny = Math.min(miny, Math.min(nbox.tr.y, Math.min(nbox.bl.y, nbox.br.y) ) ) - offset;
maxx = Math.max(maxx, Math.max(nbox.tr.x, Math.max(nbox.bl.x, nbox.br.x) ) ) + offset; minx = Min(minx, Min(nbox.tr.x, Min(nbox.bl.x, nbox.br.x) ) ) - offset;
maxy = Math.max(maxy, Math.max(nbox.tr.y, Math.max(nbox.bl.y, nbox.br.y) ) ) + offset; miny = Min(miny, Min(nbox.tr.y, Min(nbox.bl.y, nbox.br.y) ) ) - offset;
maxx = Max(maxx, Max(nbox.tr.x, Max(nbox.bl.x, nbox.br.x) ) ) + offset;
maxy = Max(maxy, Max(nbox.tr.y, Max(nbox.bl.y, nbox.br.y) ) ) + offset;
nbax = minx; nbax = minx;
nbay = miny; nbay = miny;
@@ -1403,42 +1411,38 @@ var SelectorManager;
+ " L" + (nbax+nbaw) + "," + nbay + " L" + (nbax+nbaw) + "," + nbay
+ " " + (nbax+nbaw) + "," + (nbay+nbah) + " " + (nbax+nbaw) + "," + (nbay+nbah)
+ " " + nbax + "," + (nbay+nbah) + "z"; + " " + nbax + "," + (nbay+nbah) + "z";
assignAttributes(selectedBox, {'d': dstr}); selectedBox.setAttribute('d', dstr);
this.gripCoords = { var xform = angle ? "rotate(" + [angle,cx,cy].join(",") + ")" : "";
nw: [nbax, nbay], this.selectorGroup.setAttribute("transform", xform);
ne: [nbax+nbaw, nbay],
sw: [nbax, nbay+nbah], if(selected === selectedElements[0]) {
se: [nbax+nbaw, nbay+nbah], this.gripCoords = {
n: [nbax + (nbaw)/2, nbay], nw: [nbax, nbay],
w: [nbax, nbay + (nbah)/2], ne: [nbax+nbaw, nbay],
e: [nbax + nbaw, nbay + (nbah)/2], sw: [nbax, nbay+nbah],
s: [nbax + (nbaw)/2, nbay + nbah] se: [nbax+nbaw, nbay+nbah],
}; n: [nbax + (nbaw)/2, nbay],
w: [nbax, nbay + (nbah)/2],
e: [nbax + nbaw, nbay + (nbah)/2],
s: [nbax + (nbaw)/2, nbay + nbah]
};
if(selected == selectedElements[0]) {
for(var dir in this.gripCoords) { for(var dir in this.gripCoords) {
var coords = this.gripCoords[dir]; var coords = this.gripCoords[dir];
assignAttributes(selectedGrips[dir], { assignAttributes(selectedGrips[dir], {
cx: coords[0], cy: coords[1] cx: coords[0], cy: coords[1]
}); });
}; };
}
if (angle) { // we want to go 20 pixels in the negative transformed y direction, ignoring scale
this.selectorGroup.setAttribute("transform", "rotate(" + [angle,cx,cy].join(",") + ")"); assignAttributes(mgr.rotateGripConnector, { x1: nbax + (nbaw)/2,
y1: nbay,
x2: nbax + (nbaw)/2,
y2: nbay- 20});
assignAttributes(mgr.rotateGrip, { cx: nbax + (nbaw)/2,
cy: nbay - 20 });
} }
else {
this.selectorGroup.setAttribute("transform", "");
}
// we want to go 20 pixels in the negative transformed y direction, ignoring scale
assignAttributes(mgr.rotateGripConnector, { x1: nbax + (nbaw)/2,
y1: nbay,
x2: nbax + (nbaw)/2,
y2: nbay- 20});
assignAttributes(mgr.rotateGrip, { cx: nbax + (nbaw)/2,
cy: nbay - 20 });
svgroot.unsuspendRedraw(sr_handle); svgroot.unsuspendRedraw(sr_handle);
}; };
@@ -1894,8 +1898,10 @@ var assignAttributes = this.assignAttributes = function(node, attrs, suspendLeng
var ns = (i.substr(0,4) === "xml:" ? xmlns : var ns = (i.substr(0,4) === "xml:" ? xmlns :
i.substr(0,6) === "xlink:" ? xlinkns : null); i.substr(0,6) === "xlink:" ? xlinkns : null);
if(ns || !unitCheck) { if(ns) {
node.setAttributeNS(ns, i, attrs[i]); node.setAttributeNS(ns, i, attrs[i]);
} else if(!unitCheck) {
node.setAttribute(i, attrs[i]);
} else { } else {
setUnitAttr(node, i, attrs[i]); setUnitAttr(node, i, attrs[i]);
} }
@@ -2153,7 +2159,8 @@ this.addExtension = function(name, ext_func) {
var shortFloat = function(val) { var shortFloat = function(val) {
var digits = save_options.round_digits; var digits = save_options.round_digits;
if(!isNaN(val)) { if(!isNaN(val)) {
return Number(Number(val).toFixed(digits)); // Note that + converts to Number
return +((+val).toFixed(digits));
} else if($.isArray(val)) { } else if($.isArray(val)) {
return shortFloat(val[0]) + ',' + shortFloat(val[1]); return shortFloat(val[0]) + ',' + shortFloat(val[1]);
} else { } else {
@@ -3304,8 +3311,8 @@ var remapElement = this.remapElement = function(selected,changes,m) {
break; break;
case 11: // relative elliptical arc (a) case 11: // relative elliptical arc (a)
case 10: // absolute elliptical arc (A) case 10: // absolute elliptical arc (A)
dstr += seg.r1 + "," + seg.r2 + " " + seg.angle + " " + Number(seg.largeArcFlag) + dstr += seg.r1 + "," + seg.r2 + " " + seg.angle + " " + (+seg.largeArcFlag) +
" " + Number(seg.sweepFlag) + " " + seg.x + "," + seg.y + " "; " " + (+seg.sweepFlag) + " " + seg.x + "," + seg.y + " ";
break; break;
case 17: // relative smooth cubic (s) case 17: // relative smooth cubic (s)
case 16: // absolute smooth cubic (S) case 16: // absolute smooth cubic (S)
@@ -3405,28 +3412,19 @@ var recalculateDimensions = this.recalculateDimensions = function(selected) {
// combine matrix + translate // combine matrix + translate
k = tlist.numberOfItems; k = tlist.numberOfItems;
if(k >= 2 && tlist.getItem(k-2).type === 1 && tlist.getItem(k-1).type === 2) { if(k >= 2 && tlist.getItem(k-2).type === 1 && tlist.getItem(k-1).type === 2) {
console.log('combine')
var mt = svgroot.createSVGTransform(); var mt = svgroot.createSVGTransform();
// logMatrix(tlist.getItem(0).matrix);
// logMatrix(transformListToTransform(tlist).matrix); var m = matrixMultiply(
mt.setMatrix(transformListToTransform(tlist).matrix); tlist.getItem(k-2).matrix,
tlist.clear(); tlist.getItem(k-1).matrix
);
mt.setMatrix(m);
tlist.removeItem(k-2);
tlist.removeItem(k-2);
tlist.appendItem(mt); tlist.appendItem(mt);
console.log(selected.getAttribute('transform'));
// mt.setMatrix(transformListToTransform(tlist).matrix, 1, 2);
// tlist.removeItem(k-2);
// tlist.removeItem(k-2);
// tlist.appendItem(mt);
// console.log(selected.getAttribute('transform'));
} }
} }
// Grouped SVG element // Grouped SVG element
var gsvg = $(selected).data('gsvg'); var gsvg = $(selected).data('gsvg');
@@ -4094,7 +4092,7 @@ var transformPoint = function(x, y, m) {
// Returns: // Returns:
// Boolean indicating whether or not the matrix is 1,0,0,1,0,0 // Boolean indicating whether or not the matrix is 1,0,0,1,0,0
var isIdentity = function(m) { var isIdentity = function(m) {
return (m.a == 1 && m.b == 0 && m.c == 0 && m.d == 1 && m.e == 0 && m.f == 0); return (m.a === 1 && m.b === 0 && m.c === 0 && m.d === 1 && m.e === 0 && m.f === 0);
} }
// Function: matrixMultiply // Function: matrixMultiply
@@ -4898,7 +4896,7 @@ var getMouseTarget = this.getMouseTarget = function(evt) {
// we temporarily use a translate on the element(s) being dragged // we temporarily use a translate on the element(s) being dragged
// this transform is removed upon mousing up and the element is // this transform is removed upon mousing up and the element is
// relocated to the new location // relocated to the new location
if (selectedElements[0] != null) { if (selectedElements[0] !== null) {
var dx = x - start_x; var dx = x - start_x;
var dy = y - start_y; var dy = y - start_y;
@@ -8464,7 +8462,7 @@ var convertToGroup = this.convertToGroup = function(elem) {
// } // }
// Not ideal, but works // Not ideal, but works
ts += "translate(" + (pos.x || 0) + "," + (pos.x || 0) + ")"; ts += "translate(" + (pos.x || 0) + "," + (pos.y || 0) + ")";
var prev = $elem.prev(); var prev = $elem.prev();
@@ -8742,11 +8740,11 @@ this.importSvgString = function(xmlString) {
// if no explicit viewbox, create one out of the width and height // if no explicit viewbox, create one out of the width and height
vb = innervb ? innervb.split(" ") : [0,0,innerw,innerh]; vb = innervb ? innervb.split(" ") : [0,0,innerw,innerh];
for (var j = 0; j < 4; ++j) for (var j = 0; j < 4; ++j)
vb[j] = Number(vb[j]); vb[j] = +(vb[j]);
// TODO: properly handle preserveAspectRatio // TODO: properly handle preserveAspectRatio
var canvasw = Number(svgcontent.getAttribute("width")), var canvasw = +svgcontent.getAttribute("width"),
canvash = Number(svgcontent.getAttribute("height")); canvash = +svgcontent.getAttribute("height");
// imported content should be 1/3 of the canvas on its largest dimension // imported content should be 1/3 of the canvas on its largest dimension
if (innerh > innerw) { if (innerh > innerw) {