Fixed bug on deleting first node of open path and made imported closed paths have correct end segment

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1431 eee81c28-f429-11dd-99c0-75d572ba1ddd
master
Alexis Deveria 2010-02-26 19:44:41 +00:00
parent 21ba2823ce
commit 89ed9b3985
1 changed files with 68 additions and 22 deletions

View File

@ -1352,6 +1352,7 @@ function BatchCommand(text) {
if (node.nodeName == 'path' && attrName == 'd') { if (node.nodeName == 'path' && attrName == 'd') {
// Convert to absolute // Convert to absolute
node.setAttribute('d',pathActions.convertPath(node)); node.setAttribute('d',pathActions.convertPath(node));
pathActions.fixEnd(node);
} }
// for the style attribute, rewrite it in terms of XML presentational attributes // for the style attribute, rewrite it in terms of XML presentational attributes
if (attrName == "style") { if (attrName == "style") {
@ -3732,6 +3733,28 @@ function BatchCommand(text) {
function retPath() { function retPath() {
return path; return path;
} }
function resetD(p) {
p.setAttribute("d", pathActions.convertPath(p));
}
function insertItemBefore(elem, newseg, index) {
// Support insertItemBefore on paths for FF2
var list = elem.pathSegList;
var len = list.numberOfItems;
var arr = [];
for(var i=0; i<len; i++) {
var cur_seg = list.getItem(i);
arr.push(cur_seg)
}
list.clear();
for(var i=0; i<len; i++) {
if(i == index) { //index+1
list.appendItem(newseg);
}
list.appendItem(arr[i]);
}
}
// TODO: See if this should just live in replacePathSeg // TODO: See if this should just live in replacePathSeg
function ptObjToArr(type, seg_item) { function ptObjToArr(type, seg_item) {
@ -4216,20 +4239,7 @@ function BatchCommand(text) {
if(support.pathInsertItemBefore) { if(support.pathInsertItemBefore) {
list.insertItemBefore(newseg, index); list.insertItemBefore(newseg, index);
} else { } else {
//TODO: Fix this insertItemBefore(elem, newseg, index);
var len = list.numberOfItems;
var arr = [];
for(var i=0; i<len; i++) {
var cur_seg = list.getItem(i);
arr.push(cur_seg)
}
list.clear();
for(var i=0; i<len; i++) {
if(i == index) { //index+1
list.appendItem(seg);
}
list.appendItem(arr[i]);
}
} }
} }
@ -4239,9 +4249,9 @@ function BatchCommand(text) {
var list = elem.pathSegList; var list = elem.pathSegList;
seg.show(false); seg.show(false);
var next = seg.next;
if(seg.mate) { if(seg.mate) {
// Make the next point be the "M" point // Make the next point be the "M" point
var next = seg.next;
var pt = [next.item.x, next.item.y]; var pt = [next.item.x, next.item.y];
replacePathSeg(2, next.index, pt); replacePathSeg(2, next.index, pt);
@ -4249,13 +4259,20 @@ function BatchCommand(text) {
replacePathSeg(4, seg.index, pt); replacePathSeg(4, seg.index, pt);
list.removeItem(seg.mate.index); list.removeItem(seg.mate.index);
} else if(!seg.prev) {
// First node of open path, make next point the M
var item = seg.item;
var pt = [next.item.x, next.item.y];
replacePathSeg(2, seg.next.index, pt);
list.removeItem(index);
} else { } else {
list.removeItem(index); list.removeItem(index);
} }
} }
this.endChanges = function(text) { this.endChanges = function(text) {
if(isWebkit) path.resetD(); if(isWebkit) resetD(p.elem);
var cmd = new ChangeElementCommand(elem, {d: p.last_d}, text); var cmd = new ChangeElementCommand(elem, {d: p.last_d}, text);
addCommandToHistory(cmd); addCommandToHistory(cmd);
call("changed", [elem]); call("changed", [elem]);
@ -4320,10 +4337,6 @@ function BatchCommand(text) {
this.last_d = elem.getAttribute('d'); this.last_d = elem.getAttribute('d');
} }
this.resetD = function() {
p.elem.setAttribute("d", convertToD(elem.pathSegList));
}
this.show = function(y) { this.show = function(y) {
// Shows this path's segment grips // Shows this path's segment grips
p.eachSeg(function() { p.eachSeg(function() {
@ -5053,8 +5066,9 @@ function BatchCommand(text) {
var sel_pt = sel_pts[0]-1 > 0 ? sel_pts[0]-1 : 1; var sel_pt = sel_pts[0]-1 > 0 ? sel_pts[0]-1 : 1;
path.clearSelection(); path.clearSelection();
path.addPtsToSelection(sel_pt);
// TODO: Find right way to select point now
// path.selectPt(sel_pt);
if(window.opera) { // Opera repaints incorrectly if(window.opera) { // Opera repaints incorrectly
var cp = $(path.elem); cp.attr('d',cp.attr('d')); var cp = $(path.elem); cp.attr('d',cp.attr('d'));
} }
@ -5078,6 +5092,38 @@ function BatchCommand(text) {
seg.move(diff.x, diff.y); seg.move(diff.x, diff.y);
path.endChanges("Move path point"); path.endChanges("Move path point");
}, },
fixEnd: function(elem) {
// Adds an extra segment if the last seg before a Z doesn't end
// at its M point
// M0,0 L0,100 L100,100 z
var segList = elem.pathSegList;
var len = segList.numberOfItems;
var last_m;
for (var i = 0; i < len; ++i) {
var item = segList.getItem(i);
if(item.pathSegType === 2) {
last_m = item;
}
if(item.pathSegType === 1) {
var prev = segList.getItem(i-1);
if(prev.x != last_m.x && prev.y != last_m.y) {
// Add an L segment here
var newseg = elem.createSVGPathSegLinetoAbs(last_m.x, last_m.y);
if(support.pathInsertItemBefore) {
segList.insertItemBefore(newseg, i);
} else {
insertItemBefore(elem, newseg, i);
}
// Can this be done better?
pathActions.fixEnd(elem);
break;
}
}
}
if(isWebkit) resetD(elem);
},
// Convert a path to one with only absolute or relative values // Convert a path to one with only absolute or relative values
convertPath: function(path, toRel) { convertPath: function(path, toRel) {
var segList = path.pathSegList; var segList = path.pathSegList;