2009-11-02 20:09:02 +00:00
|
|
|
/*
|
|
|
|
TODOs for TransformList:
|
2009-11-10 15:16:49 +00:00
|
|
|
|
2009-11-22 00:50:09 +00:00
|
|
|
* Ensure rotation works properly (extract the correct rotational center now?)
|
2009-11-23 16:24:01 +00:00
|
|
|
* when groups are resized, center is not found properly (dx/dy are not calculated correctly)
|
|
|
|
* clean up setRotationAngle()
|
2009-11-22 00:50:09 +00:00
|
|
|
* Ensure resized/rotated groups show the proper located and sized selector box
|
2009-11-23 16:24:01 +00:00
|
|
|
* Ensure ungrouping works (Issue 204)
|
2009-11-02 20:09:02 +00:00
|
|
|
*/
|
2009-10-18 20:46:32 +00:00
|
|
|
/*
|
|
|
|
TODOs for Localizing:
|
|
|
|
|
|
|
|
- rename tool_path to tool_fhpath in all localization files (already updated in UI and script)
|
|
|
|
- rename tool_poly to tool_path in all localization files (already updated in UI and script)
|
|
|
|
- rename poly_node_x to path_node_x globally
|
|
|
|
- rename poly_node_y to path_node_y globally
|
|
|
|
- rename svninfo_dim to svginfo_dim globally (typo)
|
|
|
|
- provide translations in all other non-EN lang.XX.js files for:
|
|
|
|
- path_node_x
|
|
|
|
- path_node_y
|
|
|
|
- seg_type
|
|
|
|
- straight_segments
|
|
|
|
- curve_segments
|
|
|
|
- tool_node_clone
|
|
|
|
- tool_node_delete
|
|
|
|
- selLayerLabel
|
|
|
|
- selLayerNames
|
|
|
|
- sidepanel_handle
|
|
|
|
*/
|
2009-10-27 20:54:29 +00:00
|
|
|
var isWebkit = navigator.userAgent.indexOf("AppleWebKit") != -1;
|
2009-06-23 05:17:53 +00:00
|
|
|
if(!window.console) {
|
2009-09-22 21:50:55 +00:00
|
|
|
window.console = {};
|
|
|
|
window.console.log = function(str) {};
|
|
|
|
window.console.dir = function(str) {};
|
|
|
|
}
|
|
|
|
if( window.opera ) {
|
|
|
|
window.console.log = function(str) {opera.postError(str);}
|
2009-06-23 05:17:53 +00:00
|
|
|
}
|
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
// this defines which elements and attributes that we support
|
2009-08-14 22:50:34 +00:00
|
|
|
// TODO: add <a> elements to this
|
2009-09-05 22:20:36 +00:00
|
|
|
// TODO: add <tspan> to this
|
2009-07-10 05:04:06 +00:00
|
|
|
var svgWhiteList = {
|
2009-10-19 20:13:52 +00:00
|
|
|
"circle": ["cx", "cy", "fill", "fill-opacity", "fill-rule", "id", "opacity", "r", "requiredFeatures", "stroke", "stroke-dasharray", "stroke-dashoffset", "stroke-linecap", "stroke-linejoin", "stroke-miterlimit", "stroke-opacity", "stroke-width", "systemLanguage", "transform"],
|
2009-07-14 12:52:39 +00:00
|
|
|
"defs": [],
|
2009-10-17 20:29:56 +00:00
|
|
|
"desc": [],
|
2009-10-19 20:13:52 +00:00
|
|
|
"ellipse": ["cx", "cy", "fill", "fill-opacity", "fill-rule", "id", "opacity", "requiredFeatures", "rx", "ry", "stroke", "stroke-dasharray", "stroke-dashoffset", "stroke-linecap", "stroke-linejoin", "stroke-miterlimit", "stroke-opacity", "stroke-width", "systemLanguage", "transform"],
|
2009-10-17 20:29:56 +00:00
|
|
|
"g": ["id", "display", "requiredFeatures", "systemLanguage", "transform"],
|
|
|
|
"image": ["height", "id", "opacity", "requiredFeatures", "systemLanguage", "transform", "width", "x", "xlink:href", "xlink:title", "y"],
|
2009-10-19 20:13:52 +00:00
|
|
|
"line": ["fill", "fill-opacity", "fill-rule", "id", "opacity", "requiredFeatures", "stroke", "stroke-dasharray", "stroke-dashoffset", "stroke-linecap", "stroke-linejoin", "stroke-miterlimit", "stroke-opacity", "stroke-width", "systemLanguage", "transform", "x1", "x2", "y1", "y2"],
|
2009-10-17 20:29:56 +00:00
|
|
|
"linearGradient": ["id", "gradientTransform", "gradientUnits", "requiredFeatures", "spreadMethod", "systemLanguage", "x1", "x2", "y1", "y2"],
|
2009-10-19 20:13:52 +00:00
|
|
|
"path": ["d", "fill", "fill-opacity", "fill-rule", "id", "opacity", "requiredFeatures", "stroke", "stroke-dasharray", "stroke-dashoffset", "stroke-linecap", "stroke-linejoin", "stroke-miterlimit", "stroke-opacity", "stroke-width", "systemLanguage", "transform"],
|
|
|
|
"polygon": ["id", "fill", "fill-opacity", "fill-rule", "id", "opacity", "points", "requiredFeatures", "stroke", "stroke-dasharray", "stroke-dashoffset", "stroke-linecap", "stroke-linejoin", "stroke-miterlimit", "stroke-opacity", "stroke-width", "systemLanguage", "transform"],
|
|
|
|
"polyline": ["id", "fill", "fill-opacity", "fill-rule", "opacity", "points", "requiredFeatures", "stroke", "stroke-dasharray", "stroke-dashoffset", "stroke-linecap", "stroke-linejoin", "stroke-miterlimit", "stroke-opacity", "stroke-width", "systemLanguage", "transform"],
|
2009-10-17 20:29:56 +00:00
|
|
|
"radialGradient": ["id", "cx", "cy", "fx", "fy", "gradientTransform", "gradientUnits", "r", "requiredFeatures", "spreadMethod", "systemLanguage"],
|
2009-10-19 20:13:52 +00:00
|
|
|
"rect": ["fill", "fill-opacity", "fill-rule", "height", "id", "opacity", "requiredFeatures", "rx", "ry", "stroke", "stroke-dasharray", "stroke-dashoffset", "stroke-linecap", "stroke-linejoin", "stroke-miterlimit", "stroke-opacity", "stroke-width", "systemLanguage", "transform", "width", "x", "y"],
|
2009-10-17 20:29:56 +00:00
|
|
|
"stop": ["id", "offset", "requiredFeatures", "stop-color", "stop-opacity", "systemLanguage"],
|
|
|
|
"switch": ["id", "requiredFeatures", "systemLanguage"],
|
|
|
|
"svg": ["id", "height", "requiredFeatures", "systemLanguage", "transform", "viewBox", "width", "xmlns", "xmlns:xlink"],
|
2009-10-19 20:13:52 +00:00
|
|
|
"text": ["fill", "fill-opacity", "fill-rule", "font-family", "font-size", "font-style", "font-weight", "id", "opacity", "requiredFeatures", "stroke", "stroke-dasharray", "stroke-dashoffset", "stroke-linecap", "stroke-linejoin", "stroke-miterlimit", "stroke-opacity", "stroke-width", "systemLanguage", "transform", "text-anchor", "x", "xml:space", "y"],
|
2009-09-20 23:04:57 +00:00
|
|
|
"title": [],
|
2009-08-17 06:56:55 +00:00
|
|
|
};
|
2009-07-10 05:04:06 +00:00
|
|
|
|
2009-10-20 13:49:53 +00:00
|
|
|
function SvgCanvas(c)
|
|
|
|
{
|
|
|
|
|
2009-10-05 08:16:41 +00:00
|
|
|
var toXml = function(str) {
|
|
|
|
return str.replace("&", "&").replace("<", "<").replace(">",">");
|
|
|
|
};
|
|
|
|
var fromXml = function(str) {
|
|
|
|
return str.replace(">", ">").replace("<", "<").replace("&", "&");
|
|
|
|
};
|
|
|
|
|
2009-10-02 15:05:31 +00:00
|
|
|
var pathFuncsStrs = ['Moveto','Lineto','CurvetoCubic','CurvetoQuadratic','Arc','LinetoHorizontal','LinetoVertical','CurvetoCubicSmooth','CurvetoQuadraticSmooth']
|
|
|
|
var pathFuncs = [0,'ClosePath'];
|
|
|
|
$.each(pathFuncsStrs,function(i,s){pathFuncs.push(s+'Abs');pathFuncs.push(s+'Rel');});
|
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
// These command objects are used for the Undo/Redo stack
|
|
|
|
// attrs contains the values that the attributes had before the change
|
|
|
|
function ChangeElementCommand(elem, attrs, text) {
|
|
|
|
this.elem = elem;
|
|
|
|
this.text = text ? ("Change " + elem.tagName + " " + text) : ("Change " + elem.tagName);
|
|
|
|
this.newValues = {};
|
|
|
|
this.oldValues = attrs;
|
|
|
|
for (attr in attrs) {
|
|
|
|
if (attr == "#text") this.newValues[attr] = elem.textContent;
|
|
|
|
else this.newValues[attr] = elem.getAttribute(attr);
|
|
|
|
}
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
this.apply = function() {
|
2009-09-06 03:51:16 +00:00
|
|
|
var bChangedTransform = false;
|
2009-06-25 02:54:07 +00:00
|
|
|
for( attr in this.newValues ) {
|
|
|
|
if (this.newValues[attr]) {
|
|
|
|
if (attr == "#text") this.elem.textContent = this.newValues[attr];
|
|
|
|
else this.elem.setAttribute(attr, this.newValues[attr]);
|
|
|
|
}
|
|
|
|
else {
|
2009-09-30 20:11:08 +00:00
|
|
|
if (attr == "#text") this.elem.textContent = "";
|
|
|
|
else {
|
|
|
|
this.elem.setAttribute(attr, "");
|
|
|
|
this.elem.removeAttribute(attr);
|
|
|
|
}
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
2009-09-06 03:51:16 +00:00
|
|
|
if (attr == "transform") { bChangedTransform = true; }
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
2009-09-04 05:10:48 +00:00
|
|
|
// relocate rotational transform, if necessary
|
2009-09-06 03:51:16 +00:00
|
|
|
if(!bChangedTransform) {
|
2009-09-04 05:10:48 +00:00
|
|
|
var angle = canvas.getRotationAngle(elem);
|
|
|
|
if (angle) {
|
|
|
|
var bbox = elem.getBBox();
|
2009-09-17 14:47:54 +00:00
|
|
|
var cx = round(bbox.x + bbox.width/2),
|
|
|
|
cy = round(bbox.y + bbox.height/2);
|
2009-09-04 05:10:48 +00:00
|
|
|
var rotate = ["rotate(", angle, " ", cx, ",", cy, ")"].join('');
|
|
|
|
if (rotate != elem.getAttribute("transform")) {
|
|
|
|
elem.setAttribute("transform", rotate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-22 02:42:31 +00:00
|
|
|
// if we are changing layer names, re-identify all layers
|
2009-10-21 15:12:32 +00:00
|
|
|
if (this.elem.tagName == "title" && this.elem.parentNode.parentNode == svgcontent) {
|
2009-10-17 20:29:56 +00:00
|
|
|
identifyLayers();
|
2009-09-22 02:42:31 +00:00
|
|
|
}
|
2009-06-25 02:54:07 +00:00
|
|
|
return true;
|
|
|
|
};
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
this.unapply = function() {
|
2009-09-06 03:51:16 +00:00
|
|
|
var bChangedTransform = false;
|
2009-06-25 02:54:07 +00:00
|
|
|
for( attr in this.oldValues ) {
|
|
|
|
if (this.oldValues[attr]) {
|
|
|
|
if (attr == "#text") this.elem.textContent = this.oldValues[attr];
|
|
|
|
else this.elem.setAttribute(attr, this.oldValues[attr]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (attr == "#text") this.elem.textContent = "";
|
|
|
|
else this.elem.removeAttribute(attr);
|
|
|
|
}
|
2009-09-06 03:51:16 +00:00
|
|
|
if (attr == "transform") { bChangedTransform = true; }
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
2009-09-04 05:10:48 +00:00
|
|
|
// relocate rotational transform, if necessary
|
2009-09-06 03:51:16 +00:00
|
|
|
if(!bChangedTransform) {
|
2009-09-04 05:10:48 +00:00
|
|
|
var angle = canvas.getRotationAngle(elem);
|
|
|
|
if (angle) {
|
|
|
|
var bbox = elem.getBBox();
|
2009-09-17 14:47:54 +00:00
|
|
|
var cx = round(bbox.x + bbox.width/2),
|
|
|
|
cy = round(bbox.y + bbox.height/2);
|
2009-09-04 05:10:48 +00:00
|
|
|
var rotate = ["rotate(", angle, " ", cx, ",", cy, ")"].join('');
|
|
|
|
if (rotate != elem.getAttribute("transform")) {
|
|
|
|
elem.setAttribute("transform", rotate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-22 02:42:31 +00:00
|
|
|
// if we are changing layer names, re-identify all layers
|
2009-10-21 15:12:32 +00:00
|
|
|
if (this.elem.tagName == "title" && this.elem.parentNode.parentNode == svgcontent) {
|
2009-10-17 20:29:56 +00:00
|
|
|
identifyLayers();
|
2009-09-22 02:42:31 +00:00
|
|
|
}
|
2009-06-25 02:54:07 +00:00
|
|
|
return true;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-01 21:28:22 +00:00
|
|
|
this.elements = function() { return [this.elem]; }
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function InsertElementCommand(elem, text) {
|
|
|
|
this.elem = elem;
|
|
|
|
this.text = text || ("Create " + elem.tagName);
|
|
|
|
this.parent = elem.parentNode;
|
|
|
|
|
2009-09-22 01:00:24 +00:00
|
|
|
this.apply = function() {
|
|
|
|
this.elem = this.parent.insertBefore(this.elem, this.elem.nextSibling);
|
2009-10-21 15:12:32 +00:00
|
|
|
if (this.parent == svgcontent) {
|
2009-10-17 20:29:56 +00:00
|
|
|
identifyLayers();
|
2009-09-22 01:00:24 +00:00
|
|
|
}
|
|
|
|
};
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
this.unapply = function() {
|
|
|
|
this.parent = this.elem.parentNode;
|
|
|
|
this.elem = this.elem.parentNode.removeChild(this.elem);
|
2009-10-21 15:12:32 +00:00
|
|
|
if (this.parent == svgcontent) {
|
2009-10-17 20:29:56 +00:00
|
|
|
identifyLayers();
|
2009-09-22 01:00:24 +00:00
|
|
|
}
|
2009-06-25 02:54:07 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-01 21:28:22 +00:00
|
|
|
this.elements = function() { return [this.elem]; };
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function RemoveElementCommand(elem, parent, text) {
|
|
|
|
this.elem = elem;
|
|
|
|
this.text = text || ("Delete " + elem.tagName);
|
|
|
|
this.parent = parent;
|
|
|
|
|
|
|
|
this.apply = function() {
|
|
|
|
this.parent = this.elem.parentNode;
|
2009-06-29 14:38:30 +00:00
|
|
|
this.elem = this.parent.removeChild(this.elem);
|
2009-10-21 15:12:32 +00:00
|
|
|
if (this.parent == svgcontent) {
|
2009-10-17 20:29:56 +00:00
|
|
|
identifyLayers();
|
2009-09-22 01:00:24 +00:00
|
|
|
}
|
2009-06-25 02:54:07 +00:00
|
|
|
};
|
|
|
|
|
2009-09-22 01:00:24 +00:00
|
|
|
this.unapply = function() {
|
|
|
|
this.elem = this.parent.insertBefore(this.elem, this.elem.nextSibling);
|
2009-10-21 15:12:32 +00:00
|
|
|
if (this.parent == svgcontent) {
|
2009-10-17 20:29:56 +00:00
|
|
|
identifyLayers();
|
2009-09-22 01:00:24 +00:00
|
|
|
}
|
|
|
|
};
|
2009-07-01 21:28:22 +00:00
|
|
|
|
|
|
|
this.elements = function() { return [this.elem]; };
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function MoveElementCommand(elem, oldNextSibling, oldParent, text) {
|
|
|
|
this.elem = elem;
|
2009-09-06 03:51:16 +00:00
|
|
|
this.text = text ? ("Move " + elem.tagName + " to " + text) : ("Move " + elem.tagName);
|
2009-06-25 02:54:07 +00:00
|
|
|
this.oldNextSibling = oldNextSibling;
|
|
|
|
this.oldParent = oldParent;
|
|
|
|
this.newNextSibling = elem.nextSibling;
|
|
|
|
this.newParent = elem.parentNode;
|
2009-06-29 14:38:30 +00:00
|
|
|
|
|
|
|
this.apply = function() {
|
2009-06-25 02:54:07 +00:00
|
|
|
this.elem = this.newParent.insertBefore(this.elem, this.newNextSibling);
|
2009-10-21 15:12:32 +00:00
|
|
|
if (this.newParent == svgcontent) {
|
2009-10-17 20:29:56 +00:00
|
|
|
identifyLayers();
|
2009-09-22 01:00:24 +00:00
|
|
|
}
|
2009-06-25 02:54:07 +00:00
|
|
|
};
|
2009-06-29 14:38:30 +00:00
|
|
|
|
|
|
|
this.unapply = function() {
|
2009-06-25 02:54:07 +00:00
|
|
|
this.elem = this.oldParent.insertBefore(this.elem, this.oldNextSibling);
|
2009-10-21 15:12:32 +00:00
|
|
|
if (this.oldParent == svgcontent) {
|
2009-10-17 20:29:56 +00:00
|
|
|
identifyLayers();
|
2009-09-22 01:00:24 +00:00
|
|
|
}
|
2009-06-25 02:54:07 +00:00
|
|
|
};
|
2009-07-01 21:28:22 +00:00
|
|
|
|
|
|
|
this.elements = function() { return [this.elem]; };
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
|
|
|
|
2009-06-29 03:58:34 +00:00
|
|
|
// TODO: create a 'typing' command object that tracks changes in text
|
|
|
|
// if a new Typing command is created and the top command on the stack is also a Typing
|
|
|
|
// and they both affect the same element, then collapse the two commands into one
|
|
|
|
|
2009-07-01 21:28:22 +00:00
|
|
|
// this command object acts an arbitrary number of subcommands
|
|
|
|
function BatchCommand(text) {
|
|
|
|
this.text = text || "Batch Command";
|
|
|
|
this.stack = [];
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-01 21:28:22 +00:00
|
|
|
this.apply = function() {
|
2009-07-08 00:03:08 +00:00
|
|
|
var len = this.stack.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
2009-07-01 21:28:22 +00:00
|
|
|
this.stack[i].apply();
|
|
|
|
}
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-01 21:28:22 +00:00
|
|
|
this.unapply = function() {
|
|
|
|
for (var i = this.stack.length-1; i >= 0; i--) {
|
|
|
|
this.stack[i].unapply();
|
|
|
|
}
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
this.elements = function() {
|
|
|
|
// iterate through all our subcommands and find all the elements we are changing
|
|
|
|
var elems = [];
|
|
|
|
var cmd = this.stack.length;
|
|
|
|
while (cmd--) {
|
|
|
|
var thisElems = this.stack[cmd].elements();
|
|
|
|
var elem = thisElems.length;
|
|
|
|
while (elem--) {
|
|
|
|
if (elems.indexOf(thisElems[elem]) == -1) elems.push(thisElems[elem]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return elems;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-02 04:50:24 +00:00
|
|
|
this.addSubCommand = function(cmd) { this.stack.push(cmd); };
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-01 21:28:22 +00:00
|
|
|
this.isEmpty = function() { return this.stack.length == 0; };
|
|
|
|
}
|
2009-06-29 03:58:34 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// private members
|
|
|
|
|
|
|
|
// **************************************************************************************
|
|
|
|
// FIXME: what's the right way to make this 'class' private to the SelectorManager?
|
|
|
|
function Selector(id, elem) {
|
|
|
|
// this is the selector's unique number
|
|
|
|
this.id = id;
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this holds a reference to the element for which this selector is being used
|
|
|
|
this.selectedElement = elem;
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this is a flag used internally to track whether the selector is being used or not
|
2009-06-29 03:58:34 +00:00
|
|
|
this.locked = true;
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this function is used to reset the id and element that the selector is attached to
|
|
|
|
this.reset = function(e) {
|
|
|
|
this.locked = true;
|
|
|
|
this.selectedElement = e;
|
2009-07-01 15:34:15 +00:00
|
|
|
this.resize();
|
2009-08-14 18:35:48 +00:00
|
|
|
selectorManager.update();
|
2009-06-29 13:20:39 +00:00
|
|
|
this.selectorGroup.setAttribute("display", "inline");
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this holds a reference to the <g> element that holds all visual elements of the selector
|
|
|
|
this.selectorGroup = addSvgElementFromJson({ "element": "g",
|
|
|
|
"attr": {"id": ("selectorGroup"+this.id)}
|
|
|
|
});
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this holds a reference to <rect> element
|
|
|
|
this.selectorRect = this.selectorGroup.appendChild( addSvgElementFromJson({
|
|
|
|
"element": "rect",
|
|
|
|
"attr": {
|
|
|
|
"id": ("selectedBox"+this.id),
|
|
|
|
"fill": "none",
|
|
|
|
"stroke": "blue",
|
|
|
|
"stroke-width": "1",
|
|
|
|
"stroke-dasharray": "5,5",
|
|
|
|
"width": 1,
|
|
|
|
"height": 1,
|
|
|
|
// need to specify this so that the rect is not selectable
|
2009-08-04 03:14:23 +00:00
|
|
|
"style": "pointer-events:none"
|
2009-06-29 13:20:39 +00:00
|
|
|
}
|
|
|
|
}) );
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this holds a reference to the grip elements for this selector
|
|
|
|
this.selectorGrips = { "nw":null,
|
|
|
|
"n":null,
|
|
|
|
"ne":null,
|
|
|
|
"e":null,
|
2009-08-26 13:59:25 +00:00
|
|
|
"se":null,
|
2009-06-29 13:20:39 +00:00
|
|
|
"s":null,
|
2009-08-26 13:59:25 +00:00
|
|
|
"sw":null,
|
|
|
|
"w":null
|
2009-06-29 13:20:39 +00:00
|
|
|
};
|
2009-08-26 19:09:13 +00:00
|
|
|
this.rotateGripConnector = this.selectorGroup.appendChild( addSvgElementFromJson({
|
|
|
|
"element": "line",
|
|
|
|
"attr": {
|
2009-11-02 20:09:02 +00:00
|
|
|
"id": ("selectorGrip_rotateconnector_" + this.id),
|
2009-08-26 19:09:13 +00:00
|
|
|
"stroke": "blue",
|
2009-09-28 18:19:22 +00:00
|
|
|
"stroke-width": "1"
|
2009-08-26 19:09:13 +00:00
|
|
|
}
|
|
|
|
}) );
|
|
|
|
this.rotateGrip = this.selectorGroup.appendChild( addSvgElementFromJson({
|
|
|
|
"element": "circle",
|
|
|
|
"attr": {
|
|
|
|
"id": ("selectorGrip_rotate_" + this.id),
|
|
|
|
"fill": "lime",
|
|
|
|
"r": 4,
|
|
|
|
"stroke": "blue",
|
2009-09-28 18:19:22 +00:00
|
|
|
"stroke-width": 2,
|
|
|
|
"style": "cursor:url(images/rotate.png) 12 12, auto;"
|
2009-08-26 19:09:13 +00:00
|
|
|
}
|
|
|
|
}) );
|
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// add the corner grips
|
|
|
|
for (dir in this.selectorGrips) {
|
2009-07-01 21:59:11 +00:00
|
|
|
this.selectorGrips[dir] = this.selectorGroup.appendChild(
|
|
|
|
addSvgElementFromJson({
|
|
|
|
"element": "rect",
|
|
|
|
"attr": {
|
2009-11-02 20:09:02 +00:00
|
|
|
"id": ("selectorGrip_resize_" + dir + "_" + this.id),
|
2009-07-01 21:59:11 +00:00
|
|
|
"fill": "blue",
|
|
|
|
"width": 6,
|
|
|
|
"height": 6,
|
|
|
|
"style": ("cursor:" + dir + "-resize"),
|
|
|
|
// This expands the mouse-able area of the grips making them
|
|
|
|
// easier to grab with the mouse.
|
|
|
|
// This works in Opera and WebKit, but does not work in Firefox
|
|
|
|
// see https://bugzilla.mozilla.org/show_bug.cgi?id=500174
|
|
|
|
"stroke-width": 2,
|
|
|
|
"pointer-events":"all",
|
|
|
|
"display":"none"
|
|
|
|
}
|
|
|
|
}) );
|
2009-06-29 13:20:39 +00:00
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
this.showGrips = function(show) {
|
2009-07-30 22:45:59 +00:00
|
|
|
// TODO: use suspendRedraw() here
|
2009-08-26 19:09:13 +00:00
|
|
|
var bShow = show ? "inline" : "none";
|
2009-08-27 14:44:23 +00:00
|
|
|
this.rotateGrip.setAttribute("display", bShow);
|
|
|
|
this.rotateGripConnector.setAttribute("display", bShow);
|
|
|
|
var elem = this.selectedElement;
|
2009-11-05 15:44:03 +00:00
|
|
|
if(elem && (elem.tagName == "text")) bShow = "none";// || elem.tagName == "g")) bShow = "none";
|
2009-06-29 13:20:39 +00:00
|
|
|
for (dir in this.selectorGrips) {
|
2009-08-26 19:09:13 +00:00
|
|
|
this.selectorGrips[dir].setAttribute("display", bShow);
|
2009-06-29 13:20:39 +00:00
|
|
|
}
|
2009-08-31 20:05:52 +00:00
|
|
|
if(elem) this.updateGripCursors(canvas.getRotationAngle(elem));
|
2009-06-29 13:20:39 +00:00
|
|
|
};
|
2009-08-26 13:59:25 +00:00
|
|
|
|
|
|
|
// Updates cursors for corner grips on rotation so arrows point the right way
|
|
|
|
this.updateGripCursors = function(angle) {
|
|
|
|
var dir_arr = [];
|
|
|
|
var steps = Math.round(angle / 45);
|
2009-08-27 12:37:02 +00:00
|
|
|
if(steps < 0) steps += 8;
|
2009-08-26 13:59:25 +00:00
|
|
|
for (dir in this.selectorGrips) {
|
|
|
|
dir_arr.push(dir);
|
|
|
|
}
|
|
|
|
while(steps > 0) {
|
|
|
|
dir_arr.push(dir_arr.shift());
|
|
|
|
steps--;
|
|
|
|
}
|
|
|
|
var i = 0;
|
|
|
|
for (dir in this.selectorGrips) {
|
|
|
|
this.selectorGrips[dir].setAttribute('style', ("cursor:" + dir_arr[i] + "-resize"));
|
|
|
|
i++;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2009-11-04 15:30:10 +00:00
|
|
|
// TODO: update this function to not use the cur_bbox anymore
|
2009-08-21 15:53:36 +00:00
|
|
|
this.resize = function(cur_bbox) {
|
2009-06-29 13:20:39 +00:00
|
|
|
var selectedBox = this.selectorRect;
|
|
|
|
var selectedGrips = this.selectorGrips;
|
|
|
|
var selected = this.selectedElement;
|
2009-09-17 14:47:54 +00:00
|
|
|
var sw = round(selected.getAttribute("stroke-width"));
|
|
|
|
var offset = 1/canvas.getZoom();
|
|
|
|
if (selected.getAttribute("stroke") != "none" && !isNaN(sw)) {
|
2009-06-29 13:20:39 +00:00
|
|
|
offset += sw/2;
|
|
|
|
}
|
|
|
|
if (selected.tagName == "text") {
|
2009-09-17 14:47:54 +00:00
|
|
|
offset += 2/canvas.getZoom();
|
2009-06-29 13:20:39 +00:00
|
|
|
}
|
2009-08-27 04:01:19 +00:00
|
|
|
var oldbox = canvas.getBBox(this.selectedElement);
|
|
|
|
var bbox = cur_bbox || oldbox;
|
2009-11-02 17:11:37 +00:00
|
|
|
if(selected.tagName == 'g') {
|
|
|
|
// The bbox for a group does not include stroke vals, so we
|
|
|
|
// get the bbox based on its children.
|
|
|
|
var stroked_bbox = canvas.getStrokedBBox(selected.childNodes);
|
|
|
|
|
|
|
|
$.each(bbox, function(key, val) {
|
|
|
|
bbox[key] = bbox[key] + stroked_bbox[key] - oldbox[key];
|
|
|
|
});
|
|
|
|
}
|
2009-06-29 13:20:39 +00:00
|
|
|
var l=bbox.x-offset, t=bbox.y-offset, w=bbox.width+(offset<<1), h=bbox.height+(offset<<1);
|
2009-11-04 15:30:10 +00:00
|
|
|
|
|
|
|
// loop and transform our bounding box until we reach our first rotation
|
|
|
|
var tlist = canvas.getTransformList(this.selectedElement);
|
|
|
|
var m = svgroot.createSVGMatrix();
|
|
|
|
var bFoundRotate = false;
|
|
|
|
var topleft = {x:l*current_zoom,y:t*current_zoom},
|
|
|
|
botright = {x:(l+w)*current_zoom,y:(t+h)*current_zoom};
|
|
|
|
var tstr = "";
|
|
|
|
var i = tlist.numberOfItems;
|
|
|
|
// loop backwards through the list of transforms and update the selector box coords
|
|
|
|
while (i--) {
|
|
|
|
var xform = tlist.getItem(i);
|
|
|
|
// once we hit a rotate, we stop doing this and just save up the transform
|
|
|
|
// string fragment and apply it to the selector group
|
|
|
|
if (xform.type == 4) {
|
|
|
|
bFoundRotate = true;
|
|
|
|
}
|
|
|
|
if (bFoundRotate) {
|
2009-11-20 15:40:05 +00:00
|
|
|
tstr = transformToObj(xform, true).text + " " + tstr;
|
2009-11-04 15:30:10 +00:00
|
|
|
}
|
|
|
|
else if(!bFoundRotate) {
|
|
|
|
m = matrixMultiply(xform.matrix,m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-12 20:08:04 +00:00
|
|
|
// This should probably be handled somewhere else, but for now
|
|
|
|
// it keeps the selection box correctly positioned when zoomed
|
|
|
|
m.e *= current_zoom;
|
|
|
|
m.f *= current_zoom;
|
|
|
|
|
2009-11-04 15:30:10 +00:00
|
|
|
// apply the transforms
|
|
|
|
topleft = transformPoint( topleft.x, topleft.y, m );
|
|
|
|
botright = transformPoint( botright.x, botright.y, m );
|
|
|
|
|
|
|
|
this.selectorGroup.setAttribute("transform", "");
|
|
|
|
this.selectorGroup.removeAttribute("transform");
|
|
|
|
if (tstr != "") {
|
|
|
|
this.selectorGroup.setAttribute("transform", tstr);
|
|
|
|
}
|
|
|
|
|
|
|
|
l = topleft.x;
|
|
|
|
t = topleft.y;
|
|
|
|
w = botright.x - topleft.x;
|
|
|
|
h = botright.y - topleft.y;
|
|
|
|
|
|
|
|
// TODO: handle negative?
|
|
|
|
|
2009-08-24 19:19:27 +00:00
|
|
|
var sr_handle = svgroot.suspendRedraw(100);
|
2009-11-04 15:30:10 +00:00
|
|
|
|
|
|
|
// TODO: move to a path instead of a rect and then plot the
|
|
|
|
// grip coordinates more carefully
|
2009-08-24 18:39:22 +00:00
|
|
|
assignAttributes(selectedBox, {
|
|
|
|
'x': l,
|
|
|
|
'y': t,
|
|
|
|
'width': w,
|
|
|
|
'height': h
|
|
|
|
});
|
2009-08-24 19:19:27 +00:00
|
|
|
|
2009-08-24 18:39:22 +00:00
|
|
|
var gripCoords = {
|
|
|
|
nw: [l-3, t-3],
|
|
|
|
ne: [l+w-3, t-3],
|
|
|
|
sw: [l-3, t+h-3],
|
|
|
|
se: [l+w-3, t+h-3],
|
|
|
|
n: [l+w/2-3, t-3],
|
|
|
|
w: [l-3, t+h/2-3],
|
|
|
|
e: [l+w-3, t+h/2-3],
|
|
|
|
s: [l+w/2-3, t+h-3]
|
|
|
|
};
|
|
|
|
$.each(gripCoords, function(dir, coords) {
|
|
|
|
assignAttributes(selectedGrips[dir], {
|
|
|
|
x: coords[0], y: coords[1]
|
|
|
|
});
|
|
|
|
});
|
2009-08-21 15:53:36 +00:00
|
|
|
|
2009-08-26 19:09:13 +00:00
|
|
|
assignAttributes(this.rotateGripConnector, { x1: l+w/2, y1: t-20, x2: l+w/2, y2: t });
|
|
|
|
assignAttributes(this.rotateGrip, { cx: l+w/2, cy: t-20 });
|
|
|
|
|
2009-08-24 19:19:27 +00:00
|
|
|
svgroot.unsuspendRedraw(sr_handle);
|
2009-06-29 13:20:39 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-01 15:34:15 +00:00
|
|
|
// now initialize the selector
|
|
|
|
this.reset(elem);
|
2009-06-29 13:20:39 +00:00
|
|
|
};
|
2009-06-01 17:35:22 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
function SelectorManager() {
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this will hold the <g> element that contains all selector rects/grips
|
|
|
|
this.selectorParentGroup = null;
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-30 06:24:41 +00:00
|
|
|
// this is a special rect that is used for multi-select
|
|
|
|
this.rubberBandBox = null;
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this will hold objects of type Selector (see above)
|
|
|
|
this.selectors = [];
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-30 06:37:39 +00:00
|
|
|
// this holds a map of SVG elements to their Selector object
|
|
|
|
this.selectorMap = {};
|
2009-06-30 06:24:41 +00:00
|
|
|
|
|
|
|
// local reference to this object
|
|
|
|
var mgr = this;
|
2009-09-04 06:36:37 +00:00
|
|
|
|
|
|
|
this.initGroup = function() {
|
2009-09-29 17:01:28 +00:00
|
|
|
// remove old selector parent group if it existed
|
|
|
|
if (mgr.selectorParentGroup && mgr.selectorParentGroup.parentNode) {
|
|
|
|
mgr.selectorParentGroup.parentNode.removeChild(mgr.selectorParentGroup);
|
|
|
|
}
|
|
|
|
// create parent selector group and add it to svgroot
|
|
|
|
mgr.selectorParentGroup = svgdoc.createElementNS(svgns, "g");
|
|
|
|
mgr.selectorParentGroup.setAttribute("id", "selectorParentGroup");
|
|
|
|
svgroot.appendChild(mgr.selectorParentGroup);
|
2009-09-04 17:44:08 +00:00
|
|
|
mgr.selectorMap = {};
|
|
|
|
mgr.selectors = [];
|
|
|
|
mgr.rubberBandBox = null;
|
2009-09-10 20:34:20 +00:00
|
|
|
mgr.update();
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
this.requestSelector = function(elem) {
|
2009-07-01 15:34:15 +00:00
|
|
|
if (elem == null) return null;
|
2009-06-29 13:20:39 +00:00
|
|
|
var N = this.selectors.length;
|
2009-06-30 14:29:28 +00:00
|
|
|
// if we've already acquired one for this element, return it
|
2009-07-01 21:59:11 +00:00
|
|
|
if (typeof(this.selectorMap[elem.id]) == "object") {
|
|
|
|
this.selectorMap[elem.id].locked = true;
|
|
|
|
return this.selectorMap[elem.id];
|
2009-06-30 14:29:28 +00:00
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
for (var i = 0; i < N; ++i) {
|
|
|
|
if (this.selectors[i] && !this.selectors[i].locked) {
|
|
|
|
this.selectors[i].locked = true;
|
2009-07-01 15:34:15 +00:00
|
|
|
this.selectors[i].reset(elem);
|
2009-07-01 21:59:11 +00:00
|
|
|
this.selectorMap[elem.id] = this.selectors[i];
|
2009-06-29 13:20:39 +00:00
|
|
|
return this.selectors[i];
|
2009-06-29 03:58:34 +00:00
|
|
|
}
|
|
|
|
}
|
2009-06-29 13:20:39 +00:00
|
|
|
// if we reached here, no available selectors were found, we create one
|
|
|
|
this.selectors[N] = new Selector(N, elem);
|
|
|
|
this.selectorParentGroup.appendChild(this.selectors[N].selectorGroup);
|
2009-07-01 21:59:11 +00:00
|
|
|
this.selectorMap[elem.id] = this.selectors[N];
|
2009-06-29 13:20:39 +00:00
|
|
|
return this.selectors[N];
|
|
|
|
};
|
2009-07-01 15:34:15 +00:00
|
|
|
this.releaseSelector = function(elem) {
|
|
|
|
if (elem == null) return;
|
2009-06-29 13:20:39 +00:00
|
|
|
var N = this.selectors.length;
|
2009-07-01 21:59:11 +00:00
|
|
|
var sel = this.selectorMap[elem.id];
|
2009-06-29 13:20:39 +00:00
|
|
|
for (var i = 0; i < N; ++i) {
|
|
|
|
if (this.selectors[i] && this.selectors[i] == sel) {
|
|
|
|
if (sel.locked == false) {
|
|
|
|
console.log("WARNING! selector was released but was already unlocked");
|
|
|
|
}
|
2009-07-01 21:59:11 +00:00
|
|
|
delete this.selectorMap[elem.id];
|
2009-06-29 13:20:39 +00:00
|
|
|
sel.locked = false;
|
|
|
|
sel.selectedElement = null;
|
2009-07-01 19:28:08 +00:00
|
|
|
sel.showGrips(false);
|
2009-06-29 13:20:39 +00:00
|
|
|
|
|
|
|
// remove from DOM and store reference in JS but only if it exists in the DOM
|
|
|
|
try {
|
|
|
|
sel.selectorGroup.setAttribute("display", "none");
|
|
|
|
} catch(e) { }
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this keeps the selector groups as the last child in the document
|
|
|
|
this.update = function() {
|
|
|
|
this.selectorParentGroup = svgroot.appendChild(this.selectorParentGroup);
|
2009-08-07 05:44:55 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-30 06:24:41 +00:00
|
|
|
this.getRubberBandBox = function() {
|
|
|
|
if (this.rubberBandBox == null) {
|
2009-06-30 17:50:08 +00:00
|
|
|
this.rubberBandBox = this.selectorParentGroup.appendChild(
|
2009-07-01 19:28:08 +00:00
|
|
|
addSvgElementFromJson({ "element": "rect",
|
|
|
|
"attr": {
|
|
|
|
"id": "selectorRubberBand",
|
|
|
|
"fill": "blue",
|
|
|
|
"fill-opacity": 0.15,
|
|
|
|
"stroke": "blue",
|
|
|
|
"stroke-width": 0.5,
|
|
|
|
"display": "none",
|
2009-08-04 03:14:23 +00:00
|
|
|
"style": "pointer-events:none"
|
2009-07-01 19:28:08 +00:00
|
|
|
}
|
|
|
|
}));
|
2009-06-30 06:24:41 +00:00
|
|
|
}
|
|
|
|
return this.rubberBandBox;
|
2009-08-07 05:44:55 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-09-04 06:36:37 +00:00
|
|
|
this.initGroup();
|
2009-06-29 13:20:39 +00:00
|
|
|
}
|
|
|
|
// **************************************************************************************
|
2009-06-25 08:22:30 +00:00
|
|
|
|
2009-10-27 20:54:29 +00:00
|
|
|
// **************************************************************************************
|
|
|
|
// SVGTransformList implementation for Webkit
|
|
|
|
// These methods do not currently raise any exceptions.
|
|
|
|
// These methods also do not check that transforms are being inserted or handle if
|
|
|
|
// a transform is already in the list, etc. This is basically implementing as much
|
|
|
|
// of SVGTransformList that we need to get the job done.
|
|
|
|
//
|
|
|
|
// interface SVGEditTransformList {
|
2009-10-28 14:09:46 +00:00
|
|
|
// attribute unsigned long numberOfItems;
|
|
|
|
// void clear ( )
|
|
|
|
// SVGTransform initialize ( in SVGTransform newItem )
|
|
|
|
// SVGTransform getItem ( in unsigned long index )
|
|
|
|
// SVGTransform insertItemBefore ( in SVGTransform newItem, in unsigned long index )
|
|
|
|
// SVGTransform replaceItem ( in SVGTransform newItem, in unsigned long index )
|
|
|
|
// SVGTransform removeItem ( in unsigned long index )
|
|
|
|
// SVGTransform appendItem ( in SVGTransform newItem )
|
2009-11-04 15:30:10 +00:00
|
|
|
// NOT IMPLEMENTED: SVGTransform createSVGTransformFromMatrix ( in SVGMatrix matrix );
|
|
|
|
// NOT IMPLEMENTED: SVGTransform consolidate ( );
|
2009-10-28 14:09:46 +00:00
|
|
|
// }
|
2009-10-27 20:54:29 +00:00
|
|
|
// **************************************************************************************
|
|
|
|
var svgTransformLists = {};
|
|
|
|
var SVGEditTransformList = function(elem) {
|
|
|
|
this._elem = elem || null;
|
|
|
|
this._xforms = [];
|
2009-11-01 20:55:01 +00:00
|
|
|
// TODO: how do we capture the undo-ability in the changed transform list?
|
2009-10-28 14:09:46 +00:00
|
|
|
this._update = function() {
|
|
|
|
var tstr = "";
|
2009-11-01 20:55:01 +00:00
|
|
|
var concatMatrix = svgroot.createSVGMatrix();
|
|
|
|
for (var i = 0; i < this.numberOfItems; ++i) {
|
|
|
|
var xform = this._list.getItem(i);
|
2009-11-05 15:44:03 +00:00
|
|
|
tstr += transformToObj(xform).text + " ";
|
2009-10-28 14:09:46 +00:00
|
|
|
}
|
2009-11-01 20:55:01 +00:00
|
|
|
this._elem.setAttribute("transform", tstr);
|
2009-10-28 14:09:46 +00:00
|
|
|
};
|
2009-11-01 20:55:01 +00:00
|
|
|
this._list = this;
|
2009-10-27 20:54:29 +00:00
|
|
|
|
|
|
|
this.numberOfItems = 0;
|
|
|
|
this.clear = function() {
|
2009-11-01 20:55:01 +00:00
|
|
|
this.numberOfItems = 0;
|
|
|
|
this._xforms = [];
|
2009-10-27 20:54:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.initialize = function(newItem) {
|
2009-11-01 20:55:01 +00:00
|
|
|
this.numberOfItems = 1;
|
|
|
|
this._xforms = [newItem];
|
2009-10-27 20:54:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.getItem = function(index) {
|
2009-11-01 20:55:01 +00:00
|
|
|
if (index < this.numberOfItems && index >= 0) {
|
|
|
|
return this._xforms[index];
|
2009-10-27 20:54:29 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.insertItemBefore = function(newItem, index) {
|
|
|
|
var retValue = null;
|
|
|
|
if (index >= 0) {
|
2009-11-01 20:55:01 +00:00
|
|
|
if (index < this.numberOfItems) {
|
|
|
|
var newxforms = new Array(this.numberOfItems + 1);
|
2009-10-27 20:54:29 +00:00
|
|
|
// TODO: use array copying and slicing
|
|
|
|
for ( var i = 0; i < index; ++i) {
|
2009-11-01 20:55:01 +00:00
|
|
|
newxforms[i] = this._xforms[i];
|
2009-10-27 20:54:29 +00:00
|
|
|
}
|
|
|
|
newxforms[i] = newItem;
|
2009-11-01 20:55:01 +00:00
|
|
|
for ( var j = i+1; i < this.numberOfItems; ++j, ++i) {
|
|
|
|
newxforms[j] = this._xforms[i];
|
2009-10-27 20:54:29 +00:00
|
|
|
}
|
2009-11-01 20:55:01 +00:00
|
|
|
this.numberOfItems++;
|
|
|
|
this._xforms = newxforms;
|
2009-10-27 20:54:29 +00:00
|
|
|
retValue = newItem;
|
2009-11-01 20:55:01 +00:00
|
|
|
this._list._update();
|
2009-10-27 20:54:29 +00:00
|
|
|
}
|
|
|
|
else {
|
2009-11-01 20:55:01 +00:00
|
|
|
retValue = this._list.appendItem(newItem);
|
2009-10-27 20:54:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return retValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.replaceItem = function(newItem, index) {
|
|
|
|
var retValue = null;
|
2009-11-01 20:55:01 +00:00
|
|
|
if (index < this.numberOfItems && index >= 0) {
|
|
|
|
this._xforms[index] = newItem;
|
2009-10-27 20:54:29 +00:00
|
|
|
retValue = newItem;
|
2009-11-01 20:55:01 +00:00
|
|
|
this._list._update();
|
2009-10-27 20:54:29 +00:00
|
|
|
}
|
|
|
|
return retValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.removeItem = function(index) {
|
|
|
|
var retValue = null;
|
2009-11-01 20:55:01 +00:00
|
|
|
if (index < this.numberOfItems && index >= 0) {
|
|
|
|
var retValue = this._xforms[index];
|
|
|
|
var newxforms = new Array(this.numberOfItems - 1);
|
2009-10-27 20:54:29 +00:00
|
|
|
for (var i = 0; i < index; ++i) {
|
2009-11-01 20:55:01 +00:00
|
|
|
newxforms[i] = this._xforms[i];
|
2009-10-27 20:54:29 +00:00
|
|
|
}
|
2009-11-01 20:55:01 +00:00
|
|
|
for (var j = i; j < this.numberOfItems-1; ++j, ++i) {
|
|
|
|
newxforms[j] = this._xforms[i+1];
|
2009-10-27 20:54:29 +00:00
|
|
|
}
|
2009-11-01 20:55:01 +00:00
|
|
|
this.numberOfItems--;
|
|
|
|
this._xforms = newxforms;
|
|
|
|
this._list._update();
|
2009-10-27 20:54:29 +00:00
|
|
|
}
|
|
|
|
return retValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.appendItem = function(newItem) {
|
2009-11-01 20:55:01 +00:00
|
|
|
this._xforms.push(newItem);
|
|
|
|
this.numberOfItems++;
|
|
|
|
this._list._update();
|
2009-10-27 20:54:29 +00:00
|
|
|
return newItem;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
// **************************************************************************************
|
|
|
|
|
2009-08-07 05:44:55 +00:00
|
|
|
var addSvgElementFromJson = function(data) {
|
|
|
|
return canvas.updateElementFromJson(data)
|
|
|
|
};
|
|
|
|
|
2009-08-24 19:19:27 +00:00
|
|
|
var assignAttributes = function(node, attrs, suspendLength) {
|
|
|
|
if(!suspendLength) suspendLength = 0;
|
2009-10-14 03:03:13 +00:00
|
|
|
// Opera has a problem with suspendRedraw() apparently
|
|
|
|
var handle = null;
|
|
|
|
if (!window.opera) svgroot.suspendRedraw(suspendLength);
|
2009-08-24 19:19:27 +00:00
|
|
|
|
2009-08-07 05:44:55 +00:00
|
|
|
for (i in attrs) {
|
2009-10-16 13:17:23 +00:00
|
|
|
var ns = (i.substr(0,4) == "xml:" ? xmlns :
|
|
|
|
i.substr(0,6) == "xlink:" ? xlinkns : null);
|
2009-10-13 00:51:15 +00:00
|
|
|
node.setAttributeNS(ns, i, attrs[i]);
|
2009-08-07 05:44:55 +00:00
|
|
|
}
|
2009-08-24 19:19:27 +00:00
|
|
|
|
2009-10-14 03:03:13 +00:00
|
|
|
if (!window.opera) svgroot.unsuspendRedraw(handle);
|
2009-08-07 05:44:55 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-07 05:44:55 +00:00
|
|
|
// remove unneeded attributes
|
|
|
|
// makes resulting SVG smaller
|
|
|
|
var cleanupElement = function(element) {
|
|
|
|
var handle = svgroot.suspendRedraw(60);
|
|
|
|
if (element.getAttribute('fill-opacity') == '1')
|
|
|
|
element.removeAttribute('fill-opacity');
|
|
|
|
if (element.getAttribute('opacity') == '1')
|
|
|
|
element.removeAttribute('opacity');
|
|
|
|
if (element.getAttribute('stroke') == 'none')
|
|
|
|
element.removeAttribute('stroke');
|
|
|
|
if (element.getAttribute('stroke-dasharray') == 'none')
|
|
|
|
element.removeAttribute('stroke-dasharray');
|
|
|
|
if (element.getAttribute('stroke-opacity') == '1')
|
|
|
|
element.removeAttribute('stroke-opacity');
|
|
|
|
if (element.getAttribute('stroke-width') == '1')
|
|
|
|
element.removeAttribute('stroke-width');
|
|
|
|
if (element.getAttribute('rx') == '0')
|
|
|
|
element.removeAttribute('rx')
|
|
|
|
if (element.getAttribute('ry') == '0')
|
|
|
|
element.removeAttribute('ry')
|
2009-10-08 17:02:23 +00:00
|
|
|
if (element.getAttribute('display') == 'inline')
|
|
|
|
element.removeAttribute('display');
|
2009-08-07 05:44:55 +00:00
|
|
|
svgroot.unsuspendRedraw(handle);
|
2009-08-17 06:56:55 +00:00
|
|
|
};
|
2009-08-07 05:44:55 +00:00
|
|
|
|
|
|
|
this.updateElementFromJson = function(data) {
|
|
|
|
var shape = svgdoc.getElementById(data.attr.id);
|
|
|
|
// if shape is a path but we need to create a rect/ellipse, then remove the path
|
|
|
|
if (shape && data.element != shape.tagName) {
|
2009-09-21 02:05:58 +00:00
|
|
|
current_layer.removeChild(shape);
|
2009-08-07 05:44:55 +00:00
|
|
|
shape = null;
|
|
|
|
}
|
|
|
|
if (!shape) {
|
|
|
|
shape = svgdoc.createElementNS(svgns, data.element);
|
2009-09-21 02:05:58 +00:00
|
|
|
if (current_layer) {
|
|
|
|
current_layer.appendChild(shape);
|
|
|
|
}
|
2009-08-07 05:44:55 +00:00
|
|
|
}
|
2009-08-24 19:19:27 +00:00
|
|
|
assignAttributes(shape, data.attr, 100);
|
2009-08-07 05:44:55 +00:00
|
|
|
cleanupElement(shape);
|
|
|
|
return shape;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-09-13 12:55:15 +00:00
|
|
|
// TODO: declare the variables and set them as null, then move this setup stuff to
|
|
|
|
// an initialization function - probably just use clear()
|
2009-06-09 22:13:13 +00:00
|
|
|
var canvas = this;
|
2009-06-07 00:17:21 +00:00
|
|
|
var container = c;
|
2009-06-01 21:24:30 +00:00
|
|
|
var svgns = "http://www.w3.org/2000/svg";
|
2009-09-04 23:08:27 +00:00
|
|
|
var xlinkns = "http://www.w3.org/1999/xlink";
|
2009-10-13 05:41:32 +00:00
|
|
|
var xmlns = "http://www.w3.org/XML/1998/namespace";
|
2009-06-12 06:45:37 +00:00
|
|
|
var idprefix = "svg_";
|
2009-06-07 00:17:21 +00:00
|
|
|
var svgdoc = c.ownerDocument;
|
|
|
|
var svgroot = svgdoc.createElementNS(svgns, "svg");
|
2009-06-07 10:27:55 +00:00
|
|
|
svgroot.setAttribute("width", 640);
|
|
|
|
svgroot.setAttribute("height", 480);
|
2009-06-08 15:05:12 +00:00
|
|
|
svgroot.setAttribute("id", "svgroot");
|
2009-06-08 08:09:15 +00:00
|
|
|
svgroot.setAttribute("xmlns", svgns);
|
2009-09-04 23:08:27 +00:00
|
|
|
svgroot.setAttribute("xmlns:xlink", xlinkns);
|
2009-06-07 00:17:21 +00:00
|
|
|
container.appendChild(svgroot);
|
2009-10-21 15:12:32 +00:00
|
|
|
var svgcontent = svgdoc.createElementNS(svgns, "svg");
|
|
|
|
svgcontent.setAttribute('id', 'svgcontent');
|
|
|
|
svgcontent.setAttribute('viewBox', '0 0 640 480');
|
|
|
|
svgcontent.setAttribute("xmlns", svgns);
|
|
|
|
svgcontent.setAttribute("xmlns:xlink", xlinkns);
|
|
|
|
svgroot.appendChild(svgcontent);
|
2009-10-13 01:40:38 +00:00
|
|
|
// TODO: make this string optional and set by the client
|
|
|
|
// TODO: make sure this is always at the top of the SVG file right underneath the <svg> element
|
|
|
|
var comment = svgdoc.createComment(" Created with SVG-edit - http://svg-edit.googlecode.com/ ");
|
2009-10-21 15:12:32 +00:00
|
|
|
svgcontent.appendChild(comment);
|
2009-09-23 14:28:03 +00:00
|
|
|
// TODO For Issue 208: this is a start on a thumbnail
|
|
|
|
// var svgthumb = svgdoc.createElementNS(svgns, "use");
|
|
|
|
// svgthumb.setAttribute('width', '100');
|
|
|
|
// svgthumb.setAttribute('height', '100');
|
2009-10-21 15:12:32 +00:00
|
|
|
// svgthumb.setAttributeNS(xlinkns, 'href', '#svgcontent');
|
2009-09-23 14:28:03 +00:00
|
|
|
// svgroot.appendChild(svgthumb);
|
2009-09-21 22:16:44 +00:00
|
|
|
// z-ordered array of tuples containing layer names and <g> elements
|
2009-09-23 03:05:27 +00:00
|
|
|
// the first layer is the one at the bottom of the rendering
|
2009-09-21 22:16:44 +00:00
|
|
|
var all_layers = [];
|
2009-11-18 16:50:25 +00:00
|
|
|
var encodableImages = {};
|
|
|
|
var last_good_img_url = 'images/logo.png';
|
2009-09-21 02:05:58 +00:00
|
|
|
// pointer to the current layer <g>
|
|
|
|
var current_layer = null;
|
2009-11-17 16:42:04 +00:00
|
|
|
var save_options = {round_digits: 5};
|
2009-06-02 16:40:33 +00:00
|
|
|
var d_attr = null;
|
|
|
|
var started = false;
|
2009-06-01 21:24:30 +00:00
|
|
|
var obj_num = 1;
|
2009-06-02 16:40:33 +00:00
|
|
|
var start_x = null;
|
|
|
|
var start_y = null;
|
2009-11-10 15:16:49 +00:00
|
|
|
var start_transform = null;
|
2009-06-27 03:18:50 +00:00
|
|
|
var current_mode = "select";
|
2009-06-23 05:17:53 +00:00
|
|
|
var current_resize_mode = "none";
|
2009-08-31 14:54:59 +00:00
|
|
|
|
|
|
|
var all_properties = {
|
|
|
|
shape: {
|
|
|
|
fill: "#FF0000",
|
|
|
|
fill_paint: null,
|
|
|
|
fill_opacity: 1,
|
|
|
|
stroke: "#000000",
|
|
|
|
stroke_paint: null,
|
|
|
|
stroke_opacity: 1,
|
|
|
|
stroke_width: 5,
|
|
|
|
stroke_style: 'none',
|
|
|
|
opacity: 1
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
all_properties.text = $.extend(true, {}, all_properties.shape);
|
|
|
|
$.extend(all_properties.text, {
|
|
|
|
fill: "#000000",
|
|
|
|
stroke_width: 0,
|
2009-10-20 15:50:58 +00:00
|
|
|
font_size: 24,
|
2009-08-31 14:54:59 +00:00
|
|
|
font_family: 'serif'
|
|
|
|
});
|
|
|
|
|
|
|
|
var cur_shape = all_properties.shape;
|
|
|
|
var cur_text = all_properties.text;
|
2009-08-31 15:59:42 +00:00
|
|
|
var cur_properties = cur_shape;
|
2009-08-31 14:54:59 +00:00
|
|
|
|
2009-06-01 21:24:30 +00:00
|
|
|
var freehand_min_x = null;
|
|
|
|
var freehand_max_x = null;
|
|
|
|
var freehand_min_y = null;
|
|
|
|
var freehand_max_y = null;
|
2009-10-18 20:46:32 +00:00
|
|
|
var current_path = null;
|
|
|
|
var current_path_pts = [];
|
|
|
|
var current_path_pt = -1;
|
|
|
|
var current_path_pt_drag = -1;
|
|
|
|
var current_path_oldd = null;
|
2009-09-25 17:59:32 +00:00
|
|
|
var current_ctrl_pt_drag = -1;
|
2009-09-09 14:18:24 +00:00
|
|
|
var current_zoom = 1;
|
2009-06-29 22:22:13 +00:00
|
|
|
// this will hold all the currently selected elements
|
|
|
|
// default size of 1 until it needs to grow bigger
|
|
|
|
var selectedElements = new Array(1);
|
2009-06-30 06:24:41 +00:00
|
|
|
// this holds the selected's bbox
|
2009-07-01 19:28:08 +00:00
|
|
|
var selectedBBoxes = new Array(1);
|
2009-09-06 23:25:12 +00:00
|
|
|
var justSelected = null;
|
2009-06-30 06:24:41 +00:00
|
|
|
// this object manages selectors for us
|
2009-06-29 13:20:39 +00:00
|
|
|
var selectorManager = new SelectorManager();
|
2009-06-30 06:24:41 +00:00
|
|
|
var rubberBox = null;
|
2009-06-06 18:34:42 +00:00
|
|
|
var events = {};
|
2009-06-25 02:54:07 +00:00
|
|
|
var undoStackPointer = 0;
|
|
|
|
var undoStack = [];
|
2009-08-26 19:59:52 +00:00
|
|
|
var curBBoxes = [];
|
|
|
|
|
2009-09-17 14:47:54 +00:00
|
|
|
// This method rounds the incoming value to the nearest value based on the current_zoom
|
|
|
|
var round = function(val){
|
|
|
|
return parseInt(val*current_zoom)/current_zoom;
|
|
|
|
};
|
|
|
|
|
2009-07-02 04:50:24 +00:00
|
|
|
// This method sends back an array or a NodeList full of elements that
|
2009-09-24 03:13:25 +00:00
|
|
|
// intersect the multi-select rubber-band-box on the current_layer only.
|
2009-07-02 04:50:24 +00:00
|
|
|
//
|
|
|
|
// Since the only browser that supports the SVG DOM getIntersectionList is Opera,
|
|
|
|
// we need to provide an implementation here. We brute-force it for now.
|
|
|
|
//
|
|
|
|
// Reference:
|
2009-07-01 19:28:08 +00:00
|
|
|
// Firefox does not implement getIntersectionList(), see https://bugzilla.mozilla.org/show_bug.cgi?id=501421
|
|
|
|
// Webkit does not implement getIntersectionList(), see https://bugs.webkit.org/show_bug.cgi?id=11274
|
2009-07-01 15:34:15 +00:00
|
|
|
var getIntersectionList = function(rect) {
|
2009-07-02 04:50:24 +00:00
|
|
|
if (rubberBox == null) { return null; }
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-26 19:59:52 +00:00
|
|
|
if(!curBBoxes.length) {
|
|
|
|
// Cache all bboxes
|
2009-09-24 03:13:25 +00:00
|
|
|
curBBoxes = canvas.getVisibleElements(current_layer, true);
|
2009-07-01 15:34:15 +00:00
|
|
|
}
|
2009-08-26 19:59:52 +00:00
|
|
|
|
|
|
|
var resultList = null;
|
|
|
|
try {
|
2009-09-21 02:05:58 +00:00
|
|
|
resultList = current_layer.getIntersectionList(rect, null);
|
2009-08-26 19:59:52 +00:00
|
|
|
} catch(e) { }
|
|
|
|
|
|
|
|
if (resultList == null || typeof(resultList.item) != "function") {
|
|
|
|
resultList = [];
|
|
|
|
|
|
|
|
var rubberBBox = rubberBox.getBBox();
|
2009-09-10 19:20:26 +00:00
|
|
|
$.each(rubberBBox, function(key, val) {
|
|
|
|
rubberBBox[key] = val / current_zoom;
|
|
|
|
});
|
2009-08-26 19:59:52 +00:00
|
|
|
var i = curBBoxes.length;
|
|
|
|
while (i--) {
|
|
|
|
if (Utils.rectsIntersect(rubberBBox, curBBoxes[i].bbox)) {
|
|
|
|
resultList.push(curBBoxes[i].elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-02 04:50:24 +00:00
|
|
|
// addToSelection expects an array, but it's ok to pass a NodeList
|
|
|
|
// because using square-bracket notation is allowed:
|
|
|
|
// http://www.w3.org/TR/DOM-Level-2-Core/ecma-script-binding.html
|
2009-07-01 15:34:15 +00:00
|
|
|
return resultList;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 14:38:30 +00:00
|
|
|
// FIXME: we MUST compress consecutive text changes to the same element
|
2009-06-25 02:54:07 +00:00
|
|
|
// (right now each keystroke is saved as a separate command that includes the
|
|
|
|
// entire text contents of the text element)
|
|
|
|
// TODO: consider limiting the history that we store here (need to do some slicing)
|
2009-06-30 06:24:41 +00:00
|
|
|
var addCommandToHistory = function(cmd) {
|
2009-06-25 02:54:07 +00:00
|
|
|
// if our stack pointer is not at the end, then we have to remove
|
|
|
|
// all commands after the pointer and insert the new command
|
|
|
|
if (undoStackPointer < undoStack.length && undoStack.length > 0) {
|
|
|
|
undoStack = undoStack.splice(0, undoStackPointer);
|
|
|
|
}
|
2009-07-02 04:50:24 +00:00
|
|
|
undoStack.push(cmd);
|
2009-06-25 02:54:07 +00:00
|
|
|
undoStackPointer = undoStack.length;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-25 02:54:07 +00:00
|
|
|
|
2009-06-01 21:24:30 +00:00
|
|
|
// private functions
|
2009-06-05 12:33:02 +00:00
|
|
|
var getId = function() {
|
2009-06-25 08:22:30 +00:00
|
|
|
if (events["getid"]) return call("getid", obj_num);
|
|
|
|
return idprefix + obj_num;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-11 13:15:16 +00:00
|
|
|
var getNextId = function() {
|
|
|
|
// ensure the ID does not exist
|
|
|
|
var id = getId();
|
|
|
|
while (svgdoc.getElementById(id)) {
|
|
|
|
obj_num++;
|
|
|
|
id = getId();
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
return id;
|
2009-07-11 13:15:16 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-06-06 18:34:42 +00:00
|
|
|
var call = function(event, arg) {
|
|
|
|
if (events[event]) {
|
|
|
|
return events[event](this,arg);
|
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 18:34:42 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
// this function sanitizes the input node and its children
|
|
|
|
// this function only keeps what is allowed from our whitelist defined above
|
|
|
|
var sanitizeSvg = function(node) {
|
|
|
|
// we only care about element nodes
|
2009-07-13 02:57:11 +00:00
|
|
|
// automatically return for all comment, etc nodes
|
|
|
|
// for text, we do a whitespace trim
|
|
|
|
if (node.nodeType == 3) {
|
|
|
|
node.nodeValue = node.nodeValue.replace(/^\s+|\s+$/g, "");
|
2009-11-16 18:06:30 +00:00
|
|
|
// Remove empty text nodes
|
|
|
|
if(!node.nodeValue.length) node.parentNode.removeChild(node);
|
2009-07-13 02:57:11 +00:00
|
|
|
}
|
2009-07-10 05:04:06 +00:00
|
|
|
if (node.nodeType != 1) return;
|
|
|
|
var doc = node.ownerDocument;
|
|
|
|
var parent = node.parentNode;
|
|
|
|
// can parent ever be null here? I think the root node's parent is the document...
|
|
|
|
if (!doc || !parent) return;
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
var allowedAttrs = svgWhiteList[node.nodeName];
|
|
|
|
// if this element is allowed
|
|
|
|
if (allowedAttrs != undefined) {
|
|
|
|
var i = node.attributes.length;
|
|
|
|
while (i--) {
|
|
|
|
// if the attribute is not in our whitelist, then remove it
|
|
|
|
// could use jQuery's inArray(), but I don't know if that's any better
|
|
|
|
var attrName = node.attributes.item(i).nodeName;
|
|
|
|
if (allowedAttrs.indexOf(attrName) == -1) {
|
|
|
|
// TODO: do I need to call setAttribute(..., "") here for Fx2?
|
|
|
|
node.removeAttribute(attrName);
|
|
|
|
}
|
2009-10-23 15:21:27 +00:00
|
|
|
if(attrName == 'd') {
|
|
|
|
// Convert to absolute
|
|
|
|
node.setAttribute('d',convertPath(node));
|
|
|
|
}
|
2009-07-10 05:04:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// recurse to children
|
|
|
|
i = node.childNodes.length;
|
|
|
|
while (i--) { sanitizeSvg(node.childNodes.item(i)); }
|
|
|
|
}
|
|
|
|
// else, remove this element
|
|
|
|
else {
|
|
|
|
// remove all children from this node and insert them before this node
|
|
|
|
var children = [];
|
|
|
|
while (node.hasChildNodes()) {
|
|
|
|
children.push(parent.insertBefore(node.firstChild, node));
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
// remove this node from the document altogether
|
|
|
|
parent.removeChild(node);
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
// call sanitizeSvg on each of those children
|
|
|
|
var i = children.length;
|
|
|
|
while (i--) { sanitizeSvg(children[i]); }
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
}
|
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-08-19 17:08:05 +00:00
|
|
|
var removeUnusedGrads = function() {
|
2009-10-21 15:12:32 +00:00
|
|
|
var defs = svgcontent.getElementsByTagNameNS(svgns, "defs");
|
2009-08-25 12:40:58 +00:00
|
|
|
if(!defs || !defs.length) return;
|
2009-08-19 17:08:05 +00:00
|
|
|
|
2009-10-21 15:12:32 +00:00
|
|
|
var all_els = svgcontent.getElementsByTagNameNS(svgns, '*');
|
2009-08-19 17:08:05 +00:00
|
|
|
var grad_uses = [];
|
|
|
|
|
|
|
|
$.each(all_els, function(i, el) {
|
|
|
|
var fill = el.getAttribute('fill');
|
|
|
|
if(fill && fill.indexOf('url(#') == 0) {
|
|
|
|
//found gradient
|
|
|
|
grad_uses.push(fill);
|
|
|
|
}
|
|
|
|
|
|
|
|
var stroke = el.getAttribute('stroke');
|
|
|
|
if(stroke && stroke.indexOf('url(#') == 0) {
|
|
|
|
//found gradient
|
|
|
|
grad_uses.push(stroke);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2009-10-21 15:12:32 +00:00
|
|
|
var lgrads = svgcontent.getElementsByTagNameNS(svgns, "linearGradient");
|
2009-08-19 17:08:05 +00:00
|
|
|
var grad_ids = [];
|
|
|
|
var i = lgrads.length;
|
|
|
|
while (i--) {
|
|
|
|
var grad = lgrads[i];
|
2009-11-13 20:45:18 +00:00
|
|
|
var id = grad.id;
|
2009-08-19 17:08:05 +00:00
|
|
|
var url_id = 'url(#' + id + ')';
|
|
|
|
if($.inArray(url_id, grad_uses) == -1) {
|
|
|
|
// Not found, so remove
|
|
|
|
grad.parentNode.removeChild(grad);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove defs if empty
|
|
|
|
var i = defs.length;
|
|
|
|
while (i--) {
|
|
|
|
var def = defs[i];
|
|
|
|
if(!def.getElementsByTagNameNS(svgns,'*').length) {
|
|
|
|
def.parentNode.removeChild(def);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 14:18:24 +00:00
|
|
|
|
|
|
|
var svgCanvasToString = function() {
|
|
|
|
removeUnusedGrads();
|
2009-10-18 20:46:32 +00:00
|
|
|
canvas.clearPath(true);
|
2009-10-29 19:43:34 +00:00
|
|
|
$.each(svgcontent.childNodes, function(i, node) {
|
|
|
|
if(i && node.nodeType == 8 && node.data.indexOf('Created with') != -1) {
|
|
|
|
svgcontent.insertBefore(node, svgcontent.firstChild);
|
|
|
|
}
|
|
|
|
});
|
2009-10-21 15:12:32 +00:00
|
|
|
svgcontent.removeAttribute('id');
|
2009-11-20 18:10:59 +00:00
|
|
|
var res = canvas.getResolution();
|
|
|
|
assignAttributes(svgcontent, {width: res.w, height: res.h});
|
|
|
|
svgcontent.removeAttribute('viewBox');
|
|
|
|
|
2009-10-21 15:12:32 +00:00
|
|
|
var output = svgToString(svgcontent, 0);
|
2009-11-20 18:10:59 +00:00
|
|
|
|
|
|
|
assignAttributes(svgcontent, {id: 'svgcontent', 'viewBox':[0,0,res.w,res.h].join(' ')});
|
|
|
|
svgcontent.removeAttribute('width');
|
|
|
|
svgcontent.removeAttribute('height');
|
2009-09-09 14:18:24 +00:00
|
|
|
return output;
|
|
|
|
}
|
2009-08-19 17:08:05 +00:00
|
|
|
|
2009-06-01 21:24:30 +00:00
|
|
|
var svgToString = function(elem, indent) {
|
2009-07-10 05:04:06 +00:00
|
|
|
var out = new Array();
|
2009-06-01 21:24:30 +00:00
|
|
|
if (elem) {
|
2009-10-08 17:02:23 +00:00
|
|
|
cleanupElement(elem);
|
2009-06-01 21:24:30 +00:00
|
|
|
var attrs = elem.attributes;
|
|
|
|
var attr;
|
|
|
|
var i;
|
|
|
|
var childs = elem.childNodes;
|
2009-07-10 05:04:06 +00:00
|
|
|
for (i=0; i<indent; i++) out.push(" ");
|
|
|
|
out.push("<"); out.push(elem.nodeName);
|
2009-06-01 21:24:30 +00:00
|
|
|
for (i=attrs.length-1; i>=0; i--) {
|
|
|
|
attr = attrs.item(i);
|
2009-10-23 15:21:27 +00:00
|
|
|
var attrVal = attr.nodeValue;
|
|
|
|
if (attrVal != "") {
|
|
|
|
if(attrVal.indexOf('pointer-events') == 0) continue;
|
2009-09-05 11:59:25 +00:00
|
|
|
out.push(" ");
|
2009-10-23 15:21:27 +00:00
|
|
|
if(attr.localName == 'd') attrVal = convertPath(elem, true);
|
|
|
|
if(!isNaN(attrVal)) {
|
|
|
|
attrVal = shortFloat(attrVal);
|
|
|
|
}
|
2009-11-17 16:42:04 +00:00
|
|
|
|
|
|
|
// Embed images when saving
|
|
|
|
if(save_options.apply
|
|
|
|
&& elem.nodeName == 'image'
|
|
|
|
&& attr.localName == 'href'
|
|
|
|
&& save_options.images
|
|
|
|
&& save_options.images == 'embed') {
|
2009-11-18 16:50:25 +00:00
|
|
|
var img = encodableImages[attrVal];
|
|
|
|
if(img) attrVal = img;
|
2009-11-17 16:42:04 +00:00
|
|
|
}
|
2009-09-05 18:13:20 +00:00
|
|
|
// map various namespaces to our fixed namespace prefixes
|
|
|
|
// TODO: put this into a map and do a look-up instead of if-else
|
2009-10-13 05:41:32 +00:00
|
|
|
if (attr.namespaceURI == xlinkns) {
|
2009-09-05 11:59:25 +00:00
|
|
|
out.push('xlink:');
|
|
|
|
}
|
2009-09-05 18:13:20 +00:00
|
|
|
else if(attr.namespaceURI == 'http://www.w3.org/2000/xmlns/' && attr.localName != 'xmlns') {
|
|
|
|
out.push('xmlns:');
|
|
|
|
}
|
2009-10-13 05:41:32 +00:00
|
|
|
else if(attr.namespaceURI == xmlns) {
|
|
|
|
out.push('xml:');
|
|
|
|
}
|
2009-09-05 18:13:20 +00:00
|
|
|
out.push(attr.localName); out.push("=\"");
|
2009-10-23 15:21:27 +00:00
|
|
|
out.push(attrVal); out.push("\"");
|
2009-06-16 02:52:36 +00:00
|
|
|
}
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
|
|
|
if (elem.hasChildNodes()) {
|
2009-07-10 05:04:06 +00:00
|
|
|
out.push(">");
|
2009-06-01 21:24:30 +00:00
|
|
|
indent++;
|
2009-06-27 03:42:12 +00:00
|
|
|
var bOneLine = false;
|
2009-06-01 21:24:30 +00:00
|
|
|
for (i=0; i<childs.length; i++)
|
|
|
|
{
|
2009-06-30 17:50:08 +00:00
|
|
|
var child = childs.item(i);
|
|
|
|
if (child.id == "selectorParentGroup") continue;
|
2009-07-13 03:23:58 +00:00
|
|
|
switch(child.nodeType) {
|
|
|
|
case 1: // element node
|
2009-07-10 05:04:06 +00:00
|
|
|
out.push("\n");
|
|
|
|
out.push(svgToString(childs.item(i), indent));
|
2009-07-13 03:23:58 +00:00
|
|
|
break;
|
|
|
|
case 3: // text node
|
2009-07-13 02:57:11 +00:00
|
|
|
var str = child.nodeValue.replace(/^\s+|\s+$/g, "");
|
|
|
|
if (str != "") {
|
|
|
|
bOneLine = true;
|
2009-10-05 08:16:41 +00:00
|
|
|
out.push(toXml(str) + "");
|
2009-07-13 00:17:15 +00:00
|
|
|
}
|
2009-07-13 03:23:58 +00:00
|
|
|
break;
|
|
|
|
case 8: // comment
|
|
|
|
out.push("\n");
|
|
|
|
out.push(new Array(indent+1).join(" "));
|
|
|
|
out.push("<!--");
|
|
|
|
out.push(child.data);
|
|
|
|
out.push("-->");
|
|
|
|
break;
|
|
|
|
} // switch on node type
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
|
|
|
indent--;
|
2009-06-29 14:38:30 +00:00
|
|
|
if (!bOneLine) {
|
2009-07-10 05:04:06 +00:00
|
|
|
out.push("\n");
|
2009-07-30 22:45:59 +00:00
|
|
|
for (i=0; i<indent; i++) out.push(" ");
|
2009-06-27 03:42:12 +00:00
|
|
|
}
|
2009-07-10 05:04:06 +00:00
|
|
|
out.push("</"); out.push(elem.nodeName); out.push(">");
|
2009-06-01 21:24:30 +00:00
|
|
|
} else {
|
2009-07-10 05:04:06 +00:00
|
|
|
out.push("/>");
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-10 05:04:06 +00:00
|
|
|
return out.join('');
|
2009-06-30 06:24:41 +00:00
|
|
|
}; // end svgToString()
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-11-18 16:50:25 +00:00
|
|
|
this.embedImage = function(val, callback) {
|
2009-11-17 16:42:04 +00:00
|
|
|
|
|
|
|
// Below is some code to fetch the data: URL representation
|
|
|
|
// of local image files. It is commented out until we figure out
|
|
|
|
// a way of introducing this as an option into the UI. Also, it
|
|
|
|
// does not work in Firefox at all :(
|
|
|
|
|
|
|
|
// load in the image and once it's loaded, get the dimensions
|
2009-11-18 16:50:25 +00:00
|
|
|
$(new Image()).load(function() {
|
2009-11-17 16:42:04 +00:00
|
|
|
// create a canvas the same size as the raster image
|
|
|
|
var canvas = document.createElement("canvas");
|
2009-11-18 16:50:25 +00:00
|
|
|
canvas.width = this.width;
|
|
|
|
canvas.height = this.height;
|
2009-11-17 16:42:04 +00:00
|
|
|
// load the raster image into the canvas
|
2009-11-18 16:50:25 +00:00
|
|
|
canvas.getContext("2d").drawImage(this,0,0);
|
2009-11-17 16:42:04 +00:00
|
|
|
// retrieve the data: URL
|
|
|
|
try {
|
2009-11-17 21:36:59 +00:00
|
|
|
var urldata = ';svgedit_url=' + encodeURIComponent(val);
|
2009-11-18 16:50:25 +00:00
|
|
|
urldata = canvas.toDataURL().replace(';base64',urldata+';base64');
|
|
|
|
encodableImages[val] = urldata;
|
2009-11-17 16:42:04 +00:00
|
|
|
} catch(e) {
|
2009-11-18 16:50:25 +00:00
|
|
|
encodableImages[val] = false;
|
2009-11-17 16:42:04 +00:00
|
|
|
}
|
2009-11-18 16:50:25 +00:00
|
|
|
last_good_img_url = val;
|
|
|
|
if(callback) callback(encodableImages[val]);
|
|
|
|
}).attr('src',val);
|
2009-11-17 16:42:04 +00:00
|
|
|
}
|
|
|
|
|
2009-11-03 21:16:36 +00:00
|
|
|
// importNode, like cloneNode, causes the comma-to-period
|
|
|
|
// issue in Opera/Win/non-en. Thankfully we can compare to the original XML
|
|
|
|
// and simply use the original value when necessary
|
2009-11-13 20:45:18 +00:00
|
|
|
this.fixOperaXML = function(elem, orig_el) {
|
2009-11-03 21:16:36 +00:00
|
|
|
var x_attrs = elem.attributes;
|
|
|
|
$.each(x_attrs, function(i, attr) {
|
|
|
|
if(attr.nodeValue.indexOf(',') == -1) return;
|
|
|
|
// attr val has comma, so let's get the good value
|
2009-11-18 16:50:25 +00:00
|
|
|
var ns = attr.prefix == 'xlink' ? xlinkns :
|
2009-11-03 21:16:36 +00:00
|
|
|
attr.prefix == "xml" ? xmlns : null;
|
2009-11-18 16:50:25 +00:00
|
|
|
var good_attrval = orig_el.getAttribute(attr.localName);
|
2009-11-13 20:45:18 +00:00
|
|
|
if(ns) {
|
|
|
|
elem.setAttributeNS(ns, attr.nodeName, good_attrval);
|
|
|
|
} else {
|
|
|
|
elem.setAttribute(attr.nodeName, good_attrval);
|
|
|
|
}
|
2009-11-03 21:16:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var childs = elem.childNodes;
|
|
|
|
var o_childs = orig_el.childNodes;
|
|
|
|
$.each(childs, function(i, child) {
|
|
|
|
if(child.nodeType == 1) {
|
2009-11-13 20:45:18 +00:00
|
|
|
canvas.fixOperaXML(child, o_childs[i]);
|
2009-11-03 21:16:36 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2009-07-08 00:03:08 +00:00
|
|
|
var recalculateAllSelectedDimensions = function() {
|
2009-07-01 21:28:22 +00:00
|
|
|
var text = (current_resize_mode == "none" ? "position" : "size");
|
|
|
|
var batchCmd = new BatchCommand(text);
|
2009-07-01 19:54:51 +00:00
|
|
|
|
2009-07-08 00:03:08 +00:00
|
|
|
var i = selectedElements.length;
|
|
|
|
while(i--) {
|
2009-11-05 15:44:03 +00:00
|
|
|
var cmd = recalculateDimensions(selectedElements[i]);//,selectedBBoxes[i]);
|
2009-07-08 00:03:08 +00:00
|
|
|
if (cmd) {
|
|
|
|
batchCmd.addSubCommand(cmd);
|
2009-07-01 19:54:51 +00:00
|
|
|
}
|
2009-07-08 00:03:08 +00:00
|
|
|
}
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2009-07-08 00:03:08 +00:00
|
|
|
if (!batchCmd.isEmpty()) {
|
|
|
|
addCommandToHistory(batchCmd);
|
|
|
|
call("changed", selectedElements);
|
|
|
|
}
|
|
|
|
};
|
2009-11-09 15:17:59 +00:00
|
|
|
|
2009-10-19 20:13:52 +00:00
|
|
|
/*
|
2009-11-09 15:17:59 +00:00
|
|
|
|
|
|
|
The user changes shape geometry in one of several ways:
|
|
|
|
- drag/moving it
|
|
|
|
- rotating it
|
|
|
|
- FUTURE: skewing it
|
|
|
|
- resizing it
|
|
|
|
(we ignore path node editing here)
|
|
|
|
|
|
|
|
From a transformation point of view:
|
|
|
|
- translations are always in the editor's frame of reference (NOT the element's)
|
|
|
|
- rotations rotate the element's frame of reference
|
2009-11-09 16:39:29 +00:00
|
|
|
- FUTURE: skewing skews the element's rotated frame of reference
|
2009-11-09 15:17:59 +00:00
|
|
|
- resizing modifies the dimensions of the element in its rotated+skewed
|
|
|
|
frame of reference.
|
|
|
|
|
|
|
|
Thus, from a coding point of view, what we do when the user is changing geometry is:
|
|
|
|
|
|
|
|
- when the user drags an element, we PREPEND the tlist with a:
|
|
|
|
translate(tx,ty)
|
|
|
|
- when the user rotates an element, we INSERT into the tlist a:
|
|
|
|
rotate(angle,cx,cy) after any translates
|
|
|
|
- FUTURE: when the user skews an element, we INSERT into the tlist a:
|
|
|
|
skewX(angle) or skewY(angle) after the rotate
|
|
|
|
- when the user is resizing an element, we APPEND the tlist with a:
|
|
|
|
translate(tx,ty) scale(sx,sy) translate(-tx,-ty)
|
|
|
|
|
|
|
|
Thus, a simple element's transform list looks like the following:
|
|
|
|
[ Translate ] [ Rotate ] [ SkewX/Y] [ Scale ]
|
|
|
|
|
|
|
|
When the user is done changing the shape's geometry (i.e. upon lifting the mouse button)
|
|
|
|
we then attempt to reduce the transform list of the element by the following:
|
|
|
|
|
|
|
|
- a translate can be removed by actually moving the element (modifying its x,y values)
|
|
|
|
- a rotate cannot be removed
|
|
|
|
- FUTURE: a skewX/skewY cannot be removed
|
|
|
|
- a scale can be removed by resizing the element (modifying its width/height values)
|
2009-10-19 20:13:52 +00:00
|
|
|
|
2009-11-09 15:17:59 +00:00
|
|
|
Thus, a simple element's transform list can always be reduced to:
|
|
|
|
[ Rotate ] [ SkewX/Y ]
|
2009-10-19 20:13:52 +00:00
|
|
|
|
2009-11-09 15:17:59 +00:00
|
|
|
However, a group is an element that contains one or more other elements, let's call
|
|
|
|
this a Complex Element.
|
|
|
|
|
|
|
|
From the user point of view, a complex element is handled no differently
|
|
|
|
than a simple element. Thus its transform list looks like the following:
|
|
|
|
[ Translate] [ Rotate ] [ SkewX/SkewY ] [ Scale ]
|
|
|
|
|
|
|
|
This means a complex element has a reduced transform list as:
|
|
|
|
[ Rotate ] [ SkewX/SkewY ] [ Scale ]
|
|
|
|
|
|
|
|
Next, we have to consider the case when a group is dissolved (ungrouped). When
|
|
|
|
the group is dissolving, the transform list must make its way down to the children.
|
|
|
|
|
|
|
|
Thus, every child of the now-dissolved group inherits the transformlist. Child N's
|
|
|
|
transform list looks like:
|
|
|
|
[ Parent Rotate ] [ Parent SkewX/SkewY ] [ Parent Scale ] [ Rotate ] [ SkewX/SkewY ] [ Scale ]
|
|
|
|
|
2009-10-19 20:13:52 +00:00
|
|
|
*/
|
|
|
|
|
2009-08-15 04:37:08 +00:00
|
|
|
// this is how we map paths to our preferred relative segment types
|
2009-10-09 20:31:24 +00:00
|
|
|
var pathMap = [ 0, 'z', 'M', 'M', 'L', 'L', 'C', 'C', 'Q', 'Q', 'A', 'A',
|
|
|
|
'L', 'L', 'L', 'L', // TODO: be less lazy below and map them to h and v
|
|
|
|
'S', 'S', 'T', 'T' ];
|
2009-11-02 20:09:02 +00:00
|
|
|
var truePathMap = [0, 'z', 'M', 'm', 'L', 'l', 'C', 'c', 'Q', 'q', 'A', 'a',
|
|
|
|
'H', 'h', 'V', 'v', 'S', 's', 'T', 't'];
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-11-12 05:02:11 +00:00
|
|
|
var logMatrix = function(m) {
|
|
|
|
console.log([m.a,m.b,m.c,m.d,m.e,m.f]);
|
|
|
|
};
|
|
|
|
|
2009-08-16 14:56:22 +00:00
|
|
|
// this function returns the command which resulted from the selected change
|
2009-11-02 20:09:02 +00:00
|
|
|
// TODO: use suspendRedraw() and unsuspendRedraw() around this function
|
2009-11-05 15:44:03 +00:00
|
|
|
var recalculateDimensions = function(selected) {
|
|
|
|
if (selected == null) return null;
|
2009-11-22 00:50:09 +00:00
|
|
|
|
2009-11-02 20:09:02 +00:00
|
|
|
var tlist = canvas.getTransformList(selected);
|
|
|
|
|
2009-11-10 15:16:49 +00:00
|
|
|
// remove any stray identity transforms
|
|
|
|
if (tlist && tlist.numberOfItems > 0) {
|
|
|
|
var k = tlist.numberOfItems;
|
|
|
|
while (k--) {
|
|
|
|
var xform = tlist.getItem(k);
|
|
|
|
if (xform.type == 0 || xform.type == 1) {
|
|
|
|
tlist.removeItem(k);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-22 00:50:09 +00:00
|
|
|
// if this element had no transforms, we are done
|
|
|
|
if (tlist.numberOfItems == 0) return null;
|
|
|
|
|
2009-11-02 20:09:02 +00:00
|
|
|
// we know we have some transforms, so set up return variable
|
|
|
|
var batchCmd = new BatchCommand("Transform");
|
|
|
|
|
|
|
|
// store initial values that will be affected by reducing the transform list
|
|
|
|
var changes = {}, initial = null;
|
|
|
|
switch (selected.tagName)
|
|
|
|
{
|
|
|
|
case "line":
|
|
|
|
changes["x1"] = selected.getAttribute("x1");
|
|
|
|
changes["y1"] = selected.getAttribute("y1");
|
|
|
|
changes["x2"] = selected.getAttribute("x2");
|
|
|
|
changes["y2"] = selected.getAttribute("y2");
|
|
|
|
break;
|
|
|
|
case "circle":
|
|
|
|
changes["cx"] = selected.getAttribute("cx");
|
|
|
|
changes["cy"] = selected.getAttribute("cy");
|
|
|
|
changes["r"] = selected.getAttribute("r");
|
|
|
|
break;
|
|
|
|
case "ellipse":
|
|
|
|
changes["cx"] = selected.getAttribute("cx");
|
|
|
|
changes["cy"] = selected.getAttribute("cy");
|
|
|
|
changes["rx"] = selected.getAttribute("rx");
|
|
|
|
changes["ry"] = selected.getAttribute("ry");
|
|
|
|
break;
|
|
|
|
case "rect":
|
|
|
|
case "image":
|
|
|
|
changes["x"] = selected.getAttribute("x");
|
|
|
|
changes["y"] = selected.getAttribute("y");
|
|
|
|
changes["width"] = selected.getAttribute("width");
|
|
|
|
changes["height"] = selected.getAttribute("height");
|
|
|
|
break;
|
|
|
|
case "text":
|
|
|
|
changes["x"] = selected.getAttribute("x");
|
|
|
|
changes["y"] = selected.getAttribute("y");
|
|
|
|
break;
|
|
|
|
case "polygon":
|
|
|
|
case "polyline":
|
|
|
|
initial = {};
|
|
|
|
initial["points"] = selected.getAttribute("points");
|
|
|
|
var list = selected.points;
|
|
|
|
var len = list.numberOfItems;
|
|
|
|
changes["points"] = new Array(len);
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
var pt = list.getItem(i);
|
|
|
|
changes["points"][i] = {x:pt.x,y:pt.y};
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "path":
|
|
|
|
initial = {};
|
|
|
|
initial["d"] = selected.getAttribute("d");
|
|
|
|
var segList = selected.pathSegList;
|
|
|
|
var len = segList.numberOfItems;
|
|
|
|
changes["d"] = new Array(len);
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
var seg = segList.getItem(i);
|
|
|
|
changes["d"][i] = {
|
|
|
|
type: seg.pathSegType,
|
|
|
|
x: seg.x,
|
|
|
|
y: seg.y,
|
|
|
|
x1: seg.x1,
|
|
|
|
y1: seg.y1,
|
|
|
|
x2: seg.x2,
|
|
|
|
y2: seg.y2,
|
|
|
|
r1: seg.r1,
|
|
|
|
r2: seg.r2,
|
|
|
|
angle: seg.angle,
|
|
|
|
largeArcFlag: seg.largeArcFlag,
|
|
|
|
sweepFlag: seg.sweepFlag
|
|
|
|
};
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} // switch on element type to get initial values
|
|
|
|
|
|
|
|
// if we haven't created an initial array in polygon/polyline/path, then
|
|
|
|
// make a copy of initial values and include the transform
|
|
|
|
if (initial == null) {
|
2009-11-19 20:16:29 +00:00
|
|
|
initial = $.extend(true, {}, changes);
|
2009-11-02 20:09:02 +00:00
|
|
|
}
|
2009-11-10 15:16:49 +00:00
|
|
|
// save the start transform value too
|
|
|
|
initial["transform"] = start_transform ? start_transform : "";
|
2009-11-02 20:09:02 +00:00
|
|
|
|
|
|
|
// reduce the transform list here...
|
2009-11-12 05:02:11 +00:00
|
|
|
|
2009-11-22 00:50:09 +00:00
|
|
|
// if it's a group, we have special processing to flatten transforms
|
2009-11-09 15:17:59 +00:00
|
|
|
if (selected.tagName == "g") {
|
2009-11-22 00:50:09 +00:00
|
|
|
|
|
|
|
var angle = 0;
|
|
|
|
var sx = 1, sy = 1;
|
|
|
|
var tx = 0, ty = 0;
|
|
|
|
var oldcx = 0, oldcy = 0, newcx = 0, newcy = 0;
|
|
|
|
var opType = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
The current concatenated matrix will be of the form:
|
|
|
|
|
|
|
|
[ T ] [ R,oldc ] [ S ] [ T,s ] [ S,new ] [ - T,s ]
|
|
|
|
|
|
|
|
which can be simply represented as:
|
2009-11-11 07:42:23 +00:00
|
|
|
|
2009-11-22 00:50:09 +00:00
|
|
|
| x' | | a c e | | x |
|
|
|
|
| y' | = | b d f | * | y |
|
|
|
|
| 1 | | 0 0 1 | | 1 |
|
|
|
|
|
|
|
|
where: a = A*cos(r), c = -C*sin(r)
|
|
|
|
b = A*sin(r), d = C*cos(r)
|
|
|
|
e,f are the translation required to recenter and properly scale it.
|
|
|
|
A is the total x scale factor
|
|
|
|
C is the total y scale factor
|
|
|
|
|
|
|
|
We always want to reduce the new transformation matrix to:
|
|
|
|
|
|
|
|
[ R,newc ] [ S ]
|
|
|
|
|
|
|
|
which will be of the form:
|
|
|
|
|
|
|
|
| x' | | a c g | | x + tx |
|
|
|
|
| y' | = | b d h | * | y + ty |
|
|
|
|
| 1 | | 0 0 1 | | 1 |
|
|
|
|
|
|
|
|
where: tx,ty are appropriate translations on the children so that the effect
|
|
|
|
is identical to the original concatenated matrix above
|
|
|
|
g,h are translations to recenter the rotation.
|
|
|
|
|
|
|
|
We can get a, b, c, d, e, f from the actual tlist matrix.
|
|
|
|
We can calculate g,h from the new bounding box.
|
2009-11-11 07:42:23 +00:00
|
|
|
|
2009-11-22 00:50:09 +00:00
|
|
|
Thus, we solve for tx,ty and we get:
|
|
|
|
|
|
|
|
tx = ( (e-g)*cos(r) + (f-h)*sin(r) ) / A
|
|
|
|
ty = ( -(e-g)*sin(r) + (e-g)*cos(r) ) / C
|
|
|
|
*/
|
|
|
|
|
|
|
|
// First, we quickly extract the factors:
|
|
|
|
var N = tlist.numberOfItems;
|
|
|
|
var i = N;
|
|
|
|
while (i--) {
|
|
|
|
var xform = tlist.getItem(i);
|
|
|
|
if (xform.type == 3) {
|
|
|
|
// extract scale factors
|
2009-11-10 07:15:29 +00:00
|
|
|
var sobj = transformToObj(xform);
|
|
|
|
sx *= sobj.sx;
|
|
|
|
sy *= sobj.sy;
|
2009-11-12 05:02:11 +00:00
|
|
|
}
|
|
|
|
else if (xform.type == 4) {
|
2009-11-22 00:50:09 +00:00
|
|
|
var robj = transformToObj(xform);
|
|
|
|
// extract angle and old center
|
|
|
|
angle = robj.angle;
|
|
|
|
oldcx = robj.cx;
|
|
|
|
oldcy = robj.cy;
|
|
|
|
newcx = oldcx;
|
|
|
|
newcy = oldcy;
|
2009-11-09 20:06:07 +00:00
|
|
|
}
|
|
|
|
}
|
2009-11-22 00:50:09 +00:00
|
|
|
|
|
|
|
// now find the new transformed bbox so we can determine what the
|
|
|
|
// new center of rotation should be
|
|
|
|
var origm = transformListToTransform(tlist).matrix;
|
|
|
|
if (angle != 0) {
|
2009-11-23 16:24:01 +00:00
|
|
|
var box = canvas.getBBox(selected);
|
|
|
|
var topleft = transformPoint(box.x,box.y,origm);
|
|
|
|
var botright = transformPoint(box.x+box.width,box.y+box.height,origm);
|
|
|
|
newcx = topleft.x + (botright.x-topleft.x)/2;
|
|
|
|
newcy = topleft.y + (botright.y-topleft.y)/2;
|
2009-11-22 00:50:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// now get e,f and calculate g,h
|
|
|
|
var e = origm.e,
|
|
|
|
f = origm.f;
|
|
|
|
var rad = angle * Math.PI / 180;
|
|
|
|
var g = newcx * (1 - Math.cos(rad)) + newcy * Math.sin(rad);
|
|
|
|
h = newcy * (1 - Math.cos(rad)) - newcx * Math.sin(rad);
|
|
|
|
|
|
|
|
// now actually calculate the new scale factors
|
|
|
|
var tx = ( (e-g)*Math.cos(rad) + (f-h)*Math.sin(rad) ) / sx,
|
|
|
|
ty = ( -(e-g)*Math.sin(rad) + (f-h)*Math.cos(rad) ) / sy;
|
|
|
|
|
|
|
|
// now we can remove all transforms from the list and create our new transforms
|
|
|
|
tlist.clear();
|
2009-11-12 05:02:11 +00:00
|
|
|
|
2009-11-22 00:50:09 +00:00
|
|
|
if (angle) {
|
|
|
|
var rot = svgroot.createSVGTransform();
|
|
|
|
rot.setRotate(angle,newcx,newcy);
|
|
|
|
tlist.appendItem(rot);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sx != 1 || sy != 1) {
|
|
|
|
var scale = svgroot.createSVGTransform();
|
|
|
|
scale.setScale(sx,sy);
|
|
|
|
tlist.appendItem(scale);
|
|
|
|
}
|
|
|
|
|
2009-11-11 07:42:23 +00:00
|
|
|
// force the accumulated translation down to the children
|
|
|
|
if (tx != 0 || ty != 0) {
|
2009-11-23 16:24:01 +00:00
|
|
|
// FIXME: unfortunately recalculateDimensions depends on this global variable
|
2009-11-22 03:45:18 +00:00
|
|
|
var old_start_transform = start_transform;
|
2009-11-23 16:24:01 +00:00
|
|
|
start_transform = null;
|
2009-11-11 07:42:23 +00:00
|
|
|
// we pass the translates down to the individual children
|
|
|
|
var children = selected.childNodes;
|
|
|
|
var c = children.length;
|
|
|
|
while (c--) {
|
|
|
|
var child = children.item(c);
|
|
|
|
if (child.nodeType == 1) {
|
|
|
|
var childTlist = canvas.getTransformList(child);
|
|
|
|
var newxlate = svgroot.createSVGTransform();
|
|
|
|
newxlate.setTranslate(tx,ty);
|
|
|
|
childTlist.insertItemBefore(newxlate, 0);
|
|
|
|
batchCmd.addSubCommand( recalculateDimensions(child) );
|
|
|
|
}
|
|
|
|
}
|
2009-11-22 03:45:18 +00:00
|
|
|
start_transform = old_start_transform;
|
2009-11-11 07:42:23 +00:00
|
|
|
}
|
2009-11-09 15:17:59 +00:00
|
|
|
}
|
|
|
|
// else, it's a non-group
|
2009-11-09 20:06:07 +00:00
|
|
|
else {
|
2009-11-22 00:50:09 +00:00
|
|
|
var box = canvas.getBBox(selected);
|
|
|
|
var origcenter = {x: (box.x+box.width/2), y: (box.y+box.height/2)};
|
|
|
|
var newcenter = {x: origcenter.x, y: origcenter.y};
|
|
|
|
|
2009-11-09 20:06:07 +00:00
|
|
|
// This pass loop in reverse order and removes any translates or scales.
|
|
|
|
// Once we hit our first rotate(), we will only remove translates.
|
|
|
|
var bRemoveTransform = true;
|
|
|
|
n = tlist.numberOfItems;
|
|
|
|
while (n--) {
|
|
|
|
// once we reach an unmoveable transform, we can stop
|
|
|
|
var xform = tlist.getItem(n);
|
|
|
|
var m = xform.matrix;
|
|
|
|
// if translate...
|
|
|
|
var remap = null, scalew = null, scaleh = null;
|
|
|
|
switch (xform.type) {
|
|
|
|
case 2: // TRANSLATE - always remove
|
|
|
|
remap = function(x,y) { return transformPoint(x,y,m); };
|
2009-11-03 15:59:27 +00:00
|
|
|
scalew = function(w) { return w; }
|
|
|
|
scaleh = function(h) { return h; }
|
2009-11-09 20:06:07 +00:00
|
|
|
break;
|
|
|
|
case 3: // SCALE - only remove if we haven't hit a rotate
|
|
|
|
if (!bRemoveTransform) continue;
|
|
|
|
remap = function(x,y) { return transformPoint(x,y,m); };
|
|
|
|
scalew = function(w) { return m.a * w; }
|
|
|
|
scaleh = function(h) { return m.d * h; }
|
|
|
|
break;
|
|
|
|
case 4: // ROTATE - only re-center if we haven't previously hit a rotate
|
|
|
|
if (!bRemoveTransform) continue;
|
|
|
|
// if the new center of the shape has moved, then
|
|
|
|
// re-center the rotation, and determine the movement
|
|
|
|
// offset required to keep the shape in the same place
|
|
|
|
if (origcenter.x != newcenter.x || origcenter.y != newcenter.y) {
|
|
|
|
var alpha = xform.angle * Math.PI / 180.0;
|
2009-11-02 20:09:02 +00:00
|
|
|
|
2009-11-09 20:06:07 +00:00
|
|
|
// determine where the new rotated center should be
|
|
|
|
var dx = newcenter.x - origcenter.x,
|
|
|
|
dy = newcenter.y - origcenter.y,
|
|
|
|
r = Math.sqrt(dx*dx + dy*dy),
|
|
|
|
theta = Math.atan2(dy,dx) + alpha;
|
|
|
|
var cx = r * Math.cos(theta) + origcenter.x,
|
|
|
|
cy = r * Math.sin(theta) + origcenter.y;
|
|
|
|
|
|
|
|
dx = cx - newcenter.x;
|
|
|
|
dy = cy - newcenter.y;
|
|
|
|
|
|
|
|
remap = function(x,y) {
|
|
|
|
return { x: x + dx, y: y + dy };
|
|
|
|
};
|
|
|
|
scalew = function(w) { return w; }
|
|
|
|
scaleh = function(h) { return h; }
|
|
|
|
// this latches to false once we hit our first rotate transform
|
|
|
|
bRemoveTransform = false;
|
|
|
|
var newrot = svgroot.createSVGTransform();
|
|
|
|
newrot.setRotate(xform.angle, cx, cy);
|
|
|
|
tlist.replaceItem(newrot, n);
|
2009-11-02 20:09:02 +00:00
|
|
|
}
|
2009-11-09 20:06:07 +00:00
|
|
|
break;
|
|
|
|
// fall through to the default: continue below
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!remap) continue;
|
2009-11-02 20:09:02 +00:00
|
|
|
|
2009-11-09 20:06:07 +00:00
|
|
|
newcenter = remap(box.x+box.width/2, box.y+box.height/2);
|
|
|
|
var bpt = remap(box.x,box.y);
|
|
|
|
box.x = bpt.x;
|
|
|
|
box.y = bpt.y;
|
|
|
|
box.width = scalew(box.width);
|
|
|
|
box.height = scaleh(box.height);
|
2009-11-02 20:09:02 +00:00
|
|
|
|
2009-11-09 20:06:07 +00:00
|
|
|
switch (selected.tagName)
|
|
|
|
{
|
2009-11-12 05:02:11 +00:00
|
|
|
case "g":
|
|
|
|
break;
|
2009-11-09 20:06:07 +00:00
|
|
|
case "line":
|
|
|
|
var pt1 = remap(changes["x1"],changes["y1"]),
|
|
|
|
pt2 = remap(changes["x2"],changes["y2"]);
|
|
|
|
changes["x1"] = pt1.x;
|
|
|
|
changes["y1"] = pt1.y;
|
|
|
|
changes["x2"] = pt2.x;
|
|
|
|
changes["y2"] = pt2.y;
|
|
|
|
break;
|
|
|
|
case "circle":
|
|
|
|
var c = remap(changes["cx"],changes["cy"]);
|
|
|
|
changes["cx"] = c.x;
|
|
|
|
changes["cy"] = c.y;
|
|
|
|
// take the minimum of the new selected box's dimensions for the new circle radius
|
|
|
|
changes["r"] = Math.min(box.width/2,box.height/2);
|
|
|
|
break;
|
|
|
|
case "ellipse":
|
|
|
|
var c = remap(changes["cx"],changes["cy"]);
|
|
|
|
changes["cx"] = c.x;
|
|
|
|
changes["cy"] = c.y;
|
|
|
|
changes["rx"] = scalew(changes["rx"]);
|
|
|
|
changes["ry"] = scaleh(changes["ry"]);
|
2009-11-02 20:09:02 +00:00
|
|
|
break;
|
2009-11-09 20:06:07 +00:00
|
|
|
case "rect":
|
|
|
|
case "image":
|
|
|
|
var pt1 = remap(changes["x"],changes["y"]);
|
|
|
|
changes["x"] = pt1.x;
|
|
|
|
changes["y"] = pt1.y;
|
|
|
|
changes["width"] = scalew(changes["width"]);
|
|
|
|
changes["height"] = scaleh(changes["height"]);
|
|
|
|
break;
|
|
|
|
case "text":
|
|
|
|
var pt1 = remap(changes["x"],changes["y"]);
|
|
|
|
changes["x"] = pt1.x;
|
|
|
|
changes["y"] = pt1.y;
|
|
|
|
break;
|
|
|
|
case "polygon":
|
|
|
|
case "polyline":
|
|
|
|
var len = changes["points"].length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
var pt = changes["points"][i];
|
|
|
|
pt = remap(pt.x,pt.y);
|
|
|
|
changes["points"][i].x = pt.x;
|
|
|
|
changes["points"][i].y = pt.y;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "path":
|
|
|
|
var len = changes["d"].length;
|
|
|
|
var firstseg = changes["d"][0];
|
|
|
|
var firstpt = remap(firstseg.x,firstseg.y);
|
|
|
|
changes["d"][0].x = firstpt.x;
|
|
|
|
changes["d"][0].y = firstpt.y;
|
|
|
|
for (var i = 1; i < len; ++i) {
|
|
|
|
var seg = changes["d"][i];
|
|
|
|
var type = seg.type;
|
|
|
|
// if absolute or first segment, we want to remap x, y, x1, y1, x2, y2
|
|
|
|
// if relative, we want to scalew, scaleh
|
|
|
|
if (type % 2 == 0) { // absolute
|
|
|
|
var pt = remap(seg.x,seg.y),
|
|
|
|
pt1 = remap(seg.x1,seg.y1),
|
|
|
|
pt2 = remap(seg.x2,seg.y2);
|
|
|
|
seg.x = pt.x;
|
|
|
|
seg.y = pt.y;
|
|
|
|
seg.x1 = pt1.x;
|
|
|
|
seg.y1 = pt1.y;
|
|
|
|
seg.x2 = pt2.x;
|
|
|
|
seg.y2 = pt2.y;
|
|
|
|
seg.r1 = scalew(seg.r1),
|
|
|
|
seg.r2 = scaleh(seg.r2);
|
|
|
|
}
|
|
|
|
else { // relative
|
|
|
|
seg.x = scalew(seg.x);
|
|
|
|
seg.y = scaleh(seg.y);
|
|
|
|
seg.x1 = scalew(seg.x1);
|
|
|
|
seg.y1 = scaleh(seg.y1);
|
|
|
|
seg.x2 = scalew(seg.x2);
|
|
|
|
seg.y2 = scaleh(seg.y2);
|
|
|
|
seg.r1 = scalew(seg.r1),
|
|
|
|
seg.r2 = scaleh(seg.r2);
|
|
|
|
}
|
|
|
|
} // for each segment
|
|
|
|
break;
|
|
|
|
} // switch on element type to get initial values
|
|
|
|
|
|
|
|
// we have eliminated the transform, so remove it from the list
|
|
|
|
if (bRemoveTransform) {
|
|
|
|
tlist.removeItem(n);
|
2009-11-02 20:09:02 +00:00
|
|
|
}
|
2009-11-09 20:06:07 +00:00
|
|
|
|
|
|
|
// now loop through the other transforms and adjust accordingly
|
|
|
|
for ( var j = n; j < tlist.numberOfItems; ++j) {
|
|
|
|
var changed_xform = tlist.getItem(j);
|
|
|
|
switch (changed_xform.type) {
|
|
|
|
// TODO: TRANSLATE, SCALE?
|
|
|
|
case 4: // rotate
|
|
|
|
var newrot = svgroot.createSVGTransform();
|
|
|
|
newrot.setRotate(changed_xform.angle, newcenter.x, newcenter.y);
|
|
|
|
tlist.replaceItem(newrot, j);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // looping for each transform
|
|
|
|
} // a non-group
|
2009-11-02 20:09:02 +00:00
|
|
|
|
|
|
|
// now we have a set of changes and an applied reduced transform list
|
|
|
|
// we apply the changes directly to the DOM
|
|
|
|
switch (selected.tagName)
|
|
|
|
{
|
|
|
|
case "rect":
|
|
|
|
case "image":
|
2009-11-20 15:40:05 +00:00
|
|
|
changes.x = changes.x-0 + Math.min(0,changes.width);
|
|
|
|
changes.y = changes.y-0 + Math.min(0,changes.height);
|
2009-11-19 20:16:29 +00:00
|
|
|
changes.width = Math.abs(changes.width);
|
|
|
|
changes.height = Math.abs(changes.height);
|
|
|
|
assignAttributes(selected, changes, 1000);
|
|
|
|
break;
|
|
|
|
case "ellipse":
|
|
|
|
changes.rx = Math.abs(changes.rx);
|
|
|
|
changes.ry = Math.abs(changes.ry);
|
|
|
|
case "circle":
|
|
|
|
if(changes.r) changes.r = Math.abs(changes.r);
|
|
|
|
case "line":
|
2009-11-02 20:09:02 +00:00
|
|
|
case "text":
|
|
|
|
assignAttributes(selected, changes, 1000);
|
|
|
|
break;
|
|
|
|
case "polyline":
|
|
|
|
case "polygon":
|
|
|
|
var len = changes["points"].length;
|
|
|
|
var pstr = "";
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
var pt = changes["points"][i];
|
|
|
|
pstr += pt.x + "," + pt.y + " ";
|
|
|
|
}
|
|
|
|
selected.setAttribute("points", pstr);
|
|
|
|
break;
|
|
|
|
case "path":
|
|
|
|
var dstr = "";
|
|
|
|
var len = changes["d"].length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
var seg = changes["d"][i];
|
|
|
|
var type = seg.type;
|
|
|
|
dstr += truePathMap[type];
|
|
|
|
switch(type) {
|
|
|
|
case 13: // relative horizontal line (h)
|
|
|
|
case 12: // absolute horizontal line (H)
|
|
|
|
dstr += seg.x + " ";
|
|
|
|
break;
|
|
|
|
case 15: // relative vertical line (v)
|
|
|
|
case 14: // absolute vertical line (V)
|
|
|
|
dstr += seg.y + " ";
|
|
|
|
break;
|
|
|
|
case 3: // relative move (m)
|
|
|
|
case 5: // relative line (l)
|
|
|
|
case 19: // relative smooth quad (t)
|
|
|
|
case 2: // absolute move (M)
|
|
|
|
case 4: // absolute line (L)
|
|
|
|
case 18: // absolute smooth quad (T)
|
|
|
|
dstr += seg.x + "," + seg.y + " ";
|
|
|
|
break;
|
|
|
|
case 7: // relative cubic (c)
|
|
|
|
case 6: // absolute cubic (C)
|
|
|
|
dstr += seg.x1 + "," + seg.y1 + " " + seg.x2 + "," + seg.y2 + " " +
|
|
|
|
seg.x + "," + seg.y + " ";
|
|
|
|
break;
|
|
|
|
case 9: // relative quad (q)
|
|
|
|
case 8: // absolute quad (Q)
|
|
|
|
dstr += seg.x + "," + seg.y + " " + seg.x1 + "," + seg.y1 + " ";
|
|
|
|
break;
|
|
|
|
case 11: // relative elliptical arc (a)
|
|
|
|
case 10: // absolute elliptical arc (A)
|
|
|
|
dstr += seg.r1 + "," + seg.r2 + " " + seg.angle + " " + seg.largeArcFlag +
|
|
|
|
" " + seg.sweepFlag + " " + seg.x + "," + seg.y + " ";
|
|
|
|
break;
|
|
|
|
case 17: // relative smooth cubic (s)
|
|
|
|
case 16: // absolute smooth cubic (S)
|
|
|
|
dstr += seg.x + "," + seg.y + " " + seg.x2 + "," + seg.y2 + " ";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
selected.setAttribute("d", dstr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the transform list has been emptied, remove it
|
|
|
|
if (tlist.numberOfItems == 0) {
|
|
|
|
selected.removeAttribute("transform");
|
|
|
|
}
|
|
|
|
|
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(selected, initial));
|
|
|
|
|
|
|
|
return batchCmd;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-23 05:17:53 +00:00
|
|
|
|
2009-06-02 12:52:44 +00:00
|
|
|
// public events
|
2009-07-01 15:34:15 +00:00
|
|
|
|
2009-10-13 02:48:27 +00:00
|
|
|
// Group: Selection
|
|
|
|
|
2009-10-13 02:35:36 +00:00
|
|
|
// Function: clearSelection
|
|
|
|
// Clears the selection. The 'selected' handler is then called.
|
2009-07-01 15:34:15 +00:00
|
|
|
this.clearSelection = function() {
|
2009-09-22 18:29:15 +00:00
|
|
|
if (selectedElements[0] != null) {
|
|
|
|
var len = selectedElements.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
var elem = selectedElements[i];
|
|
|
|
if (elem == null) break;
|
|
|
|
selectorManager.releaseSelector(elem);
|
|
|
|
selectedElements[i] = null;
|
|
|
|
selectedBBoxes[i] = null;
|
|
|
|
}
|
2009-07-01 15:34:15 +00:00
|
|
|
}
|
2009-07-01 19:28:08 +00:00
|
|
|
call("selected", selectedElements);
|
2009-07-01 15:34:15 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-09-10 14:27:34 +00:00
|
|
|
// TODO: do we need to worry about selectedBBoxes here?
|
2009-10-13 02:35:36 +00:00
|
|
|
|
|
|
|
// Function: addToSelection
|
|
|
|
// Adds a list of elements to the selection. The 'selected' handler is then called.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// elemsToAdd - an array of DOM elements to add to the selection
|
|
|
|
// showGrips - a boolean flag indicating whether the resize grips should be shown
|
2009-08-21 13:39:23 +00:00
|
|
|
this.addToSelection = function(elemsToAdd, showGrips) {
|
2009-07-01 15:34:15 +00:00
|
|
|
if (elemsToAdd.length == 0) { return; }
|
|
|
|
// find the first null in our selectedElements array
|
|
|
|
var j = 0;
|
|
|
|
while (j < selectedElements.length) {
|
|
|
|
if (selectedElements[j] == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++j;
|
|
|
|
}
|
2009-08-28 19:20:54 +00:00
|
|
|
|
2009-07-01 15:34:15 +00:00
|
|
|
// now add each element consecutively
|
2009-07-03 02:49:42 +00:00
|
|
|
var i = elemsToAdd.length;
|
|
|
|
while (i--) {
|
2009-07-01 15:34:15 +00:00
|
|
|
var elem = elemsToAdd[i];
|
2009-07-01 19:28:08 +00:00
|
|
|
// we ignore any selectors
|
|
|
|
if (elem.id.substr(0,13) == "selectorGrip_") continue;
|
2009-07-01 15:34:15 +00:00
|
|
|
// if it's not already there, add it
|
|
|
|
if (selectedElements.indexOf(elem) == -1) {
|
2009-07-01 19:28:08 +00:00
|
|
|
selectedElements[j] = elem;
|
2009-08-16 15:07:00 +00:00
|
|
|
selectedBBoxes[j++] = this.getBBox(elem);
|
2009-09-06 23:25:12 +00:00
|
|
|
var sel = selectorManager.requestSelector(elem);
|
|
|
|
if (selectedElements.length > 1) {
|
|
|
|
sel.showGrips(false);
|
|
|
|
}
|
2009-07-01 19:28:08 +00:00
|
|
|
call("selected", selectedElements);
|
2009-07-01 15:34:15 +00:00
|
|
|
}
|
|
|
|
}
|
2009-11-16 15:19:51 +00:00
|
|
|
|
2009-11-16 19:17:18 +00:00
|
|
|
if(showGrips) {
|
|
|
|
selectorManager.requestSelector(selectedElements[0]).showGrips(true);
|
|
|
|
}
|
|
|
|
else if (selectedElements.length > 1) {
|
|
|
|
selectorManager.requestSelector(selectedElements[0]).showGrips(false);
|
|
|
|
}
|
|
|
|
|
2009-11-16 15:19:51 +00:00
|
|
|
// make sure the elements are in the correct order
|
2009-11-16 17:04:19 +00:00
|
|
|
// See: http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-compareDocumentPosition
|
|
|
|
|
2009-11-16 15:19:51 +00:00
|
|
|
selectedElements.sort(function(a,b) {
|
|
|
|
if(a && b && a.compareDocumentPosition) {
|
|
|
|
return 3 - (b.compareDocumentPosition(a) & 6);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2009-11-16 19:17:18 +00:00
|
|
|
// Make sure null value is at the end
|
|
|
|
if(!selectedElements[0]) selectedElements.push(selectedElements.shift());
|
|
|
|
|
2009-07-01 15:34:15 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-09-10 14:27:34 +00:00
|
|
|
// TODO: could use slice here to make this faster?
|
2009-10-13 02:35:36 +00:00
|
|
|
// TODO: should the 'selected' handler
|
|
|
|
|
|
|
|
// Function: removeFromSelection
|
|
|
|
// Removes elements from the selection.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// elemsToRemove - an array of elements to remove from selection
|
2009-07-01 15:34:15 +00:00
|
|
|
this.removeFromSelection = function(elemsToRemove) {
|
|
|
|
if (selectedElements[0] == null) { return; }
|
|
|
|
if (elemsToRemove.length == 0) { return; }
|
|
|
|
|
2009-08-17 06:56:55 +00:00
|
|
|
// find every element and remove it from our array copy
|
2009-07-01 15:34:15 +00:00
|
|
|
var newSelectedItems = new Array(selectedElements.length);
|
2009-09-10 14:27:34 +00:00
|
|
|
var newSelectedBBoxes = new Array(selectedBBoxes.length);
|
2009-07-01 15:34:15 +00:00
|
|
|
var j = 0;
|
2009-07-03 02:49:42 +00:00
|
|
|
var len = selectedElements.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
2009-07-01 15:34:15 +00:00
|
|
|
var elem = selectedElements[i];
|
|
|
|
if (elem) {
|
|
|
|
// keep the item
|
|
|
|
if (elemsToRemove.indexOf(elem) == -1) {
|
2009-09-10 14:27:34 +00:00
|
|
|
newSelectedBBoxes[j] = selectedBBoxes[i];
|
2009-07-01 15:34:15 +00:00
|
|
|
newSelectedItems[j++] = elem;
|
|
|
|
}
|
|
|
|
else { // remove the item and its selector
|
|
|
|
selectorManager.releaseSelector(elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// the copy becomes the master now
|
|
|
|
selectedElements = newSelectedItems;
|
2009-09-10 14:27:34 +00:00
|
|
|
selectedBBoxes = newSelectedBBoxes;
|
2009-07-01 15:34:15 +00:00
|
|
|
};
|
2009-10-05 19:09:39 +00:00
|
|
|
|
|
|
|
this.addNodeToSelection = function(point) {
|
|
|
|
// Currently only one node can be selected at a time, should allow more later
|
|
|
|
// Should point be the index or the grip element?
|
2009-10-13 14:52:51 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var is_closed = current_path.getAttribute('d').toLowerCase().indexOf('z') != -1;
|
2009-10-12 20:26:03 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
if(is_closed && point == current_path_pts.length/2 - 1) {
|
|
|
|
current_path_pt = 0;
|
2009-10-07 16:04:32 +00:00
|
|
|
} else {
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path_pt = point;
|
2009-10-07 16:04:32 +00:00
|
|
|
}
|
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
$('#pathpointgrip_container circle').attr('stroke','#00F');
|
|
|
|
var grip = $('#pathpointgrip_' + point).attr('stroke','#0FF');
|
2009-10-23 17:27:37 +00:00
|
|
|
$('#ctrlpointgrip_container circle').attr('fill', '#EEE');
|
|
|
|
$('#ctrlpointgrip_' + current_path_pt + 'c1, #ctrlpointgrip_' + current_path_pt + 'c2').attr('fill','#0FF');
|
|
|
|
|
2009-10-13 14:52:51 +00:00
|
|
|
updateSegLine();
|
|
|
|
updateSegLine(true);
|
|
|
|
|
2009-10-05 19:09:39 +00:00
|
|
|
call("selected", [grip[0]]);
|
|
|
|
}
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-10-22 03:18:54 +00:00
|
|
|
// Some global variables that we may need to refactor
|
|
|
|
var root_sctm = null;
|
2009-10-22 03:29:49 +00:00
|
|
|
var mouse_target = null;
|
2009-10-22 03:18:54 +00:00
|
|
|
|
|
|
|
// A (hopefully) quicker function to transform a point by a matrix
|
|
|
|
// (this function avoids any DOM calls and just does the math)
|
2009-10-22 13:20:41 +00:00
|
|
|
// Returns a x,y object representing the transformed point
|
2009-10-22 03:18:54 +00:00
|
|
|
var transformPoint = function(x, y, m) {
|
|
|
|
return { x: m.a * x + m.c * y + m.e, y: m.b * x + m.d * y + m.f};
|
|
|
|
};
|
2009-11-04 15:30:10 +00:00
|
|
|
|
2009-11-11 07:42:23 +00:00
|
|
|
// matrixMultiply() is provided because WebKit didn't implement multiply() correctly
|
2009-11-04 15:30:10 +00:00
|
|
|
// on the SVGMatrix interface. See https://bugs.webkit.org/show_bug.cgi?id=16062
|
2009-11-11 07:42:23 +00:00
|
|
|
// This function tries to return a SVGMatrix that is the multiplication m1*m2.
|
|
|
|
// As far as I can tell, there is no way for us to handle matrix multiplication
|
|
|
|
// of arbitrary matrices because we cannot directly set the a,b,c,d,e,f values
|
|
|
|
// of the resulting matrix, we have to do it with translate/rotate/scale
|
|
|
|
// TODO: investigate if webkit allows direct setting of matrix.a, etc
|
2009-11-04 15:30:10 +00:00
|
|
|
var matrixMultiply = function(m1, m2) {
|
|
|
|
var a = m1.a*m2.a + m1.c*m2.b,
|
|
|
|
b = m1.b*m2.a + m1.d*m2.b,
|
|
|
|
c = m1.a*m2.c + m1.c*m2.d,
|
|
|
|
d = m1.b*m2.c + m1.d*m2.d,
|
|
|
|
e = m1.a*m2.e + m1.c*m2.f + m1.e,
|
|
|
|
f = m1.b*m2.e + m1.d*m2.f + m1.f;
|
2009-11-11 07:42:23 +00:00
|
|
|
|
|
|
|
// now construct a matrix by analyzing a,b,c,d,e,f and trying to
|
|
|
|
// translate, rotate, and scale the thing into place
|
2009-11-04 15:30:10 +00:00
|
|
|
var m = svgroot.createSVGMatrix();
|
2009-11-11 07:42:23 +00:00
|
|
|
var sx = 1, sy = 1, angle = 0;
|
|
|
|
|
|
|
|
// translate
|
2009-11-04 15:30:10 +00:00
|
|
|
m = m.translate(e,f);
|
2009-11-11 07:42:23 +00:00
|
|
|
|
|
|
|
// see if there was a rotation
|
|
|
|
var rad = Math.atan2(b,a);
|
|
|
|
if (rad != 0 && rad != Math.PI && rad != -Math.PI) {
|
|
|
|
m = m.rotate(180.0 * rad / Math.PI);
|
|
|
|
sx = b / Math.sin(rad);
|
|
|
|
sy = -c / Math.sin(rad);
|
|
|
|
}
|
2009-11-12 05:02:11 +00:00
|
|
|
else {
|
|
|
|
sx = a / Math.cos(rad);
|
|
|
|
sy = d / Math.cos(rad);
|
2009-11-11 07:42:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// scale
|
|
|
|
if (sx != 1 || sy != 1) {
|
|
|
|
m = m.scaleNonUniform(sx,sy);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: handle skews?
|
2009-11-04 15:30:10 +00:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This returns a single matrix Transform for a given Transform List
|
|
|
|
// (this is the equivalent of SVGTransformList.consolidate() but unlike
|
|
|
|
// that method, this one does not modify the actual SVGTransformList)
|
|
|
|
var transformListToTransform = function(tlist) {
|
|
|
|
var m = svgroot.createSVGMatrix();
|
|
|
|
for (var i = 0; i < tlist.numberOfItems; ++i) {
|
|
|
|
m = matrixMultiply(m, tlist.getItem(i).matrix);
|
|
|
|
}
|
|
|
|
return svgroot.createSVGTransformFromMatrix(m);
|
|
|
|
};
|
|
|
|
|
2009-11-23 16:24:01 +00:00
|
|
|
// FIXME: this should not have anything to do with zoom here - update the one place it is used this way
|
|
|
|
// converts a tiny object equivalent of a SVGTransform
|
2009-11-05 15:44:03 +00:00
|
|
|
// has the following properties:
|
|
|
|
// - tx, ty, sx, sy, angle, cx, cy, string
|
2009-11-20 15:40:05 +00:00
|
|
|
var transformToObj = function(xform, mZoom) {
|
2009-11-04 15:30:10 +00:00
|
|
|
var m = xform.matrix;
|
2009-11-05 15:44:03 +00:00
|
|
|
var tobj = {tx:0,ty:0,sx:1,sy:1,angle:0,cx:0,cy:0,text:""};
|
2009-11-20 15:40:05 +00:00
|
|
|
var z = mZoom?current_zoom:1;
|
2009-11-04 15:30:10 +00:00
|
|
|
switch(xform.type) {
|
2009-11-20 15:40:05 +00:00
|
|
|
case 2: // TRANSLATE
|
2009-11-05 15:44:03 +00:00
|
|
|
tobj.tx = m.e;
|
|
|
|
tobj.ty = m.f;
|
2009-11-20 15:40:05 +00:00
|
|
|
tobj.text = "translate(" + m.e*z + "," + m.f*z + ")";
|
2009-11-05 15:44:03 +00:00
|
|
|
break;
|
2009-11-04 15:30:10 +00:00
|
|
|
case 3: // SCALE
|
2009-11-05 15:44:03 +00:00
|
|
|
tobj.sx = m.a;
|
|
|
|
tobj.sy = m.d;
|
|
|
|
if (m.a == m.d) tobj.text = "scale(" + m.a + ")";
|
|
|
|
else tobj.text = "scale(" + m.a + "," + m.d + ")";
|
|
|
|
break;
|
2009-11-04 15:30:10 +00:00
|
|
|
case 4: // ROTATE
|
2009-11-05 15:44:03 +00:00
|
|
|
tobj.angle = xform.angle;
|
|
|
|
// this prevents divide by zero
|
|
|
|
if (xform.angle != 0) {
|
|
|
|
var K = 1 - m.a;
|
|
|
|
tobj.cy = ( K * m.f + m.b*m.e ) / ( K*K + m.b*m.b );
|
|
|
|
tobj.cx = ( m.e - m.b * tobj.cy ) / K;
|
|
|
|
}
|
2009-11-20 15:40:05 +00:00
|
|
|
tobj.text = "rotate(" + xform.angle + " " + tobj.cx*z + "," + tobj.cy*z + ")";
|
2009-11-05 15:44:03 +00:00
|
|
|
break;
|
2009-11-04 15:30:10 +00:00
|
|
|
// TODO: matrix, skewX, skewY
|
|
|
|
}
|
2009-11-05 15:44:03 +00:00
|
|
|
return tobj;
|
2009-11-04 15:30:10 +00:00
|
|
|
};
|
2009-10-22 03:18:54 +00:00
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
// - when we are in a create mode, the element is added to the canvas
|
2009-07-01 19:28:08 +00:00
|
|
|
// but the action is not recorded until mousing up
|
2009-06-29 14:38:30 +00:00
|
|
|
// - when we are in select mode, select the element, remember the position
|
2009-06-25 02:54:07 +00:00
|
|
|
// and do nothing else
|
2009-06-07 00:17:21 +00:00
|
|
|
var mouseDown = function(evt)
|
2009-06-01 21:24:30 +00:00
|
|
|
{
|
2009-10-22 03:18:54 +00:00
|
|
|
root_sctm = svgroot.getScreenCTM().inverse();
|
|
|
|
var pt = transformPoint( evt.pageX, evt.pageY, root_sctm );
|
2009-10-22 03:07:15 +00:00
|
|
|
var mouse_x = pt.x;
|
|
|
|
var mouse_y = pt.y;
|
2009-09-05 23:09:46 +00:00
|
|
|
evt.preventDefault();
|
2009-09-04 22:22:40 +00:00
|
|
|
|
2009-08-19 17:08:05 +00:00
|
|
|
if($.inArray(current_mode, ['select', 'resize']) == -1) {
|
|
|
|
addGradient();
|
|
|
|
}
|
2009-09-09 14:18:24 +00:00
|
|
|
|
2009-09-11 13:32:56 +00:00
|
|
|
x = mouse_x / current_zoom;
|
|
|
|
y = mouse_y / current_zoom;
|
2009-09-09 14:18:24 +00:00
|
|
|
|
2009-08-24 17:40:41 +00:00
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
2009-10-16 18:01:05 +00:00
|
|
|
|
2009-10-22 03:29:49 +00:00
|
|
|
// find mouse target
|
|
|
|
mouse_target = evt.target;
|
|
|
|
// go up until we hit a child of a layer
|
|
|
|
while (mouse_target.parentNode.parentNode.tagName == "g") {
|
|
|
|
mouse_target = mouse_target.parentNode;
|
|
|
|
}
|
|
|
|
// Webkit bubbles the mouse event all the way up to the div, so we
|
|
|
|
// set the mouse_target to the svgroot like the other browsers
|
|
|
|
if (mouse_target.nodeName.toLowerCase() == "div") {
|
2009-10-22 13:20:41 +00:00
|
|
|
mouse_target = svgroot;
|
2009-10-22 03:29:49 +00:00
|
|
|
}
|
|
|
|
// if it is a selector grip, then it must be a single element selected,
|
2009-11-02 20:09:02 +00:00
|
|
|
// set the mouse_target to that and update the mode to rotate/resize
|
2009-10-22 13:20:41 +00:00
|
|
|
if (mouse_target.parentNode == selectorManager.selectorParentGroup && selectedElements[0] != null) {
|
2009-11-02 20:09:02 +00:00
|
|
|
var gripid = evt.target.id;
|
|
|
|
var griptype = gripid.substr(0,20);
|
|
|
|
// rotating
|
|
|
|
if (griptype == "selectorGrip_rotate_") {
|
|
|
|
current_mode = "rotate";
|
|
|
|
}
|
|
|
|
// resizing
|
|
|
|
else if(griptype == "selectorGrip_resize_") {
|
|
|
|
current_mode = "resize";
|
|
|
|
current_resize_mode = gripid.substr(20,gripid.indexOf("_",20)-20);
|
|
|
|
}
|
2009-10-22 03:29:49 +00:00
|
|
|
mouse_target = selectedElements[0];
|
|
|
|
}
|
2009-11-02 20:09:02 +00:00
|
|
|
|
2009-11-10 15:16:49 +00:00
|
|
|
start_transform = mouse_target.getAttribute("transform");
|
2009-11-02 20:09:02 +00:00
|
|
|
var tlist = canvas.getTransformList(mouse_target);
|
2009-10-22 03:29:49 +00:00
|
|
|
|
2009-06-03 08:41:25 +00:00
|
|
|
switch (current_mode) {
|
|
|
|
case "select":
|
2009-06-08 02:17:07 +00:00
|
|
|
started = true;
|
2009-06-25 02:54:07 +00:00
|
|
|
current_resize_mode = "none";
|
2009-10-22 13:20:41 +00:00
|
|
|
if (mouse_target != svgroot) {
|
2009-07-01 19:54:51 +00:00
|
|
|
// if this element is not yet selected, clear selection and select it
|
2009-10-22 03:29:49 +00:00
|
|
|
if (selectedElements.indexOf(mouse_target) == -1) {
|
2009-09-06 23:25:12 +00:00
|
|
|
// only clear selection if shift is not pressed (otherwise, add
|
|
|
|
// element to selection)
|
|
|
|
if (!evt.shiftKey) {
|
|
|
|
canvas.clearSelection();
|
|
|
|
}
|
2009-10-22 03:29:49 +00:00
|
|
|
canvas.addToSelection([mouse_target]);
|
|
|
|
justSelected = mouse_target;
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path = null;
|
2009-07-01 19:54:51 +00:00
|
|
|
}
|
2009-10-17 20:29:56 +00:00
|
|
|
// else if it's a path, go into pathedit mode in mouseup
|
2009-11-02 20:09:02 +00:00
|
|
|
|
|
|
|
// insert a dummy transform so if the element is moved it will have
|
|
|
|
// a transform to use for its translate
|
|
|
|
tlist.insertItemBefore(svgroot.createSVGTransform(), 0);
|
2009-06-30 06:24:41 +00:00
|
|
|
}
|
|
|
|
else {
|
2009-07-01 19:28:08 +00:00
|
|
|
canvas.clearSelection();
|
2009-06-30 06:24:41 +00:00
|
|
|
current_mode = "multiselect";
|
2009-07-01 19:28:08 +00:00
|
|
|
if (rubberBox == null) {
|
|
|
|
rubberBox = selectorManager.getRubberBandBox();
|
|
|
|
}
|
2009-09-10 19:20:26 +00:00
|
|
|
start_x *= current_zoom;
|
|
|
|
start_y *= current_zoom;
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(rubberBox, {
|
|
|
|
'x': start_x,
|
|
|
|
'y': start_y,
|
|
|
|
'width': 0,
|
|
|
|
'height': 0,
|
|
|
|
'display': 'inline'
|
|
|
|
}, 100);
|
2009-06-07 05:07:23 +00:00
|
|
|
}
|
2009-06-03 08:41:25 +00:00
|
|
|
break;
|
2009-09-16 15:45:50 +00:00
|
|
|
case "zoom":
|
|
|
|
started = true;
|
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
2009-09-16 17:18:11 +00:00
|
|
|
if (rubberBox == null) {
|
|
|
|
rubberBox = selectorManager.getRubberBandBox();
|
|
|
|
}
|
|
|
|
assignAttributes(rubberBox, {
|
|
|
|
'x': start_x * current_zoom,
|
|
|
|
'y': start_y * current_zoom,
|
|
|
|
'width': 0,
|
|
|
|
'height': 0,
|
|
|
|
'display': 'inline'
|
|
|
|
}, 100);
|
2009-09-16 15:45:50 +00:00
|
|
|
break;
|
2009-06-23 05:17:53 +00:00
|
|
|
case "resize":
|
|
|
|
started = true;
|
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
2009-11-02 20:09:02 +00:00
|
|
|
// append three dummy transforms to the tlist so that
|
|
|
|
// we can translate,scale,translate in mousemove
|
|
|
|
tlist.appendItem(svgroot.createSVGTransform());
|
|
|
|
tlist.appendItem(svgroot.createSVGTransform());
|
|
|
|
tlist.appendItem(svgroot.createSVGTransform());
|
2009-06-23 05:17:53 +00:00
|
|
|
break;
|
2009-06-03 08:41:25 +00:00
|
|
|
case "fhellipse":
|
|
|
|
case "fhrect":
|
2009-10-18 20:04:26 +00:00
|
|
|
case "fhpath":
|
2009-06-03 08:41:25 +00:00
|
|
|
started = true;
|
2009-06-08 02:17:07 +00:00
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
2009-08-07 17:19:32 +00:00
|
|
|
d_attr = x + "," + y + " ";
|
2009-09-01 19:52:34 +00:00
|
|
|
var stroke_w = cur_shape.stroke_width == 0?1:cur_shape.stroke_width;
|
2009-06-03 08:41:25 +00:00
|
|
|
addSvgElementFromJson({
|
2009-08-07 17:19:32 +00:00
|
|
|
"element": "polyline",
|
2009-06-03 08:41:25 +00:00
|
|
|
"attr": {
|
2009-08-07 17:19:32 +00:00
|
|
|
"points": d_attr,
|
2009-07-11 13:15:16 +00:00
|
|
|
"id": getNextId(),
|
2009-06-03 08:41:25 +00:00
|
|
|
"fill": "none",
|
2009-08-31 14:54:59 +00:00
|
|
|
"stroke": cur_shape.stroke,
|
2009-09-01 19:52:34 +00:00
|
|
|
"stroke-width": stroke_w,
|
2009-08-31 14:54:59 +00:00
|
|
|
"stroke-dasharray": cur_shape.stroke_style,
|
|
|
|
"stroke-opacity": cur_shape.stroke_opacity,
|
2009-08-16 06:02:26 +00:00
|
|
|
"stroke-linecap": "round",
|
|
|
|
"stroke-linejoin": "round",
|
2009-09-21 22:16:44 +00:00
|
|
|
"opacity": cur_shape.opacity / 2,
|
2009-10-11 04:32:21 +00:00
|
|
|
"style": "pointer-events:inherit"
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
freehand_min_x = x;
|
|
|
|
freehand_max_x = x;
|
|
|
|
freehand_min_y = y;
|
|
|
|
freehand_max_y = y;
|
|
|
|
break;
|
2009-09-04 22:22:40 +00:00
|
|
|
case "image":
|
|
|
|
started = true;
|
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
|
|
|
var newImage = addSvgElementFromJson({
|
|
|
|
"element": "image",
|
|
|
|
"attr": {
|
|
|
|
"x": x,
|
|
|
|
"y": y,
|
|
|
|
"width": 0,
|
|
|
|
"height": 0,
|
|
|
|
"id": getNextId(),
|
2009-09-21 22:16:44 +00:00
|
|
|
"opacity": cur_shape.opacity / 2,
|
2009-10-11 04:32:21 +00:00
|
|
|
"style": "pointer-events:inherit"
|
2009-09-04 22:22:40 +00:00
|
|
|
}
|
|
|
|
});
|
2009-11-18 16:50:25 +00:00
|
|
|
newImage.setAttributeNS(xlinkns, "href", last_good_img_url);
|
2009-09-04 22:22:40 +00:00
|
|
|
break;
|
2009-06-03 08:41:25 +00:00
|
|
|
case "square":
|
2009-06-25 02:54:07 +00:00
|
|
|
// FIXME: once we create the rect, we lose information that this was a square
|
2009-08-21 12:21:43 +00:00
|
|
|
// (for resizing purposes this could be important)
|
2009-06-03 08:41:25 +00:00
|
|
|
case "rect":
|
|
|
|
started = true;
|
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
|
|
|
addSvgElementFromJson({
|
|
|
|
"element": "rect",
|
|
|
|
"attr": {
|
|
|
|
"x": x,
|
|
|
|
"y": y,
|
|
|
|
"width": 0,
|
|
|
|
"height": 0,
|
2009-07-11 13:15:16 +00:00
|
|
|
"id": getNextId(),
|
2009-08-31 14:54:59 +00:00
|
|
|
"fill": cur_shape.fill,
|
|
|
|
"stroke": cur_shape.stroke,
|
|
|
|
"stroke-width": cur_shape.stroke_width,
|
|
|
|
"stroke-dasharray": cur_shape.stroke_style,
|
|
|
|
"stroke-opacity": cur_shape.stroke_opacity,
|
|
|
|
"fill-opacity": cur_shape.fill_opacity,
|
2009-09-21 22:16:44 +00:00
|
|
|
"opacity": cur_shape.opacity / 2,
|
2009-10-11 04:32:21 +00:00
|
|
|
"style": "pointer-events:inherit"
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case "line":
|
|
|
|
started = true;
|
2009-09-01 19:52:34 +00:00
|
|
|
var stroke_w = cur_shape.stroke_width == 0?1:cur_shape.stroke_width;
|
2009-06-03 08:41:25 +00:00
|
|
|
addSvgElementFromJson({
|
|
|
|
"element": "line",
|
|
|
|
"attr": {
|
|
|
|
"x1": x,
|
|
|
|
"y1": y,
|
|
|
|
"x2": x,
|
|
|
|
"y2": y,
|
2009-07-11 13:15:16 +00:00
|
|
|
"id": getNextId(),
|
2009-08-31 14:54:59 +00:00
|
|
|
"stroke": cur_shape.stroke,
|
2009-09-01 19:52:34 +00:00
|
|
|
"stroke-width": stroke_w,
|
2009-08-31 14:54:59 +00:00
|
|
|
"stroke-dasharray": cur_shape.stroke_style,
|
|
|
|
"stroke-opacity": cur_shape.stroke_opacity,
|
2009-06-17 17:36:00 +00:00
|
|
|
"fill": "none",
|
2009-09-21 22:16:44 +00:00
|
|
|
"opacity": cur_shape.opacity / 2,
|
2009-10-11 04:32:21 +00:00
|
|
|
"style": "pointer-events:inherit"
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case "circle":
|
2009-06-03 15:00:48 +00:00
|
|
|
started = true;
|
|
|
|
addSvgElementFromJson({
|
|
|
|
"element": "circle",
|
|
|
|
"attr": {
|
|
|
|
"cx": x,
|
|
|
|
"cy": y,
|
|
|
|
"r": 0,
|
2009-07-11 13:15:16 +00:00
|
|
|
"id": getNextId(),
|
2009-08-31 14:54:59 +00:00
|
|
|
"fill": cur_shape.fill,
|
|
|
|
"stroke": cur_shape.stroke,
|
|
|
|
"stroke-width": cur_shape.stroke_width,
|
|
|
|
"stroke-dasharray": cur_shape.stroke_style,
|
|
|
|
"stroke-opacity": cur_shape.stroke_opacity,
|
|
|
|
"fill-opacity": cur_shape.fill_opacity,
|
2009-09-21 22:16:44 +00:00
|
|
|
"opacity": cur_shape.opacity / 2,
|
2009-10-11 04:32:21 +00:00
|
|
|
"style": "pointer-events:inherit"
|
2009-06-03 15:00:48 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
2009-06-03 08:41:25 +00:00
|
|
|
case "ellipse":
|
|
|
|
started = true;
|
2009-10-16 20:08:36 +00:00
|
|
|
addSvgElementFromJson({
|
2009-06-03 08:41:25 +00:00
|
|
|
"element": "ellipse",
|
|
|
|
"attr": {
|
|
|
|
"cx": x,
|
|
|
|
"cy": y,
|
|
|
|
"rx": 0,
|
|
|
|
"ry": 0,
|
2009-07-11 13:15:16 +00:00
|
|
|
"id": getNextId(),
|
2009-08-31 14:54:59 +00:00
|
|
|
"fill": cur_shape.fill,
|
|
|
|
"stroke": cur_shape.stroke,
|
|
|
|
"stroke-width": cur_shape.stroke_width,
|
|
|
|
"stroke-dasharray": cur_shape.stroke_style,
|
|
|
|
"stroke-opacity": cur_shape.stroke_opacity,
|
|
|
|
"fill-opacity": cur_shape.fill_opacity,
|
2009-09-21 22:16:44 +00:00
|
|
|
"opacity": cur_shape.opacity / 2,
|
2009-10-11 04:32:21 +00:00
|
|
|
"style": "pointer-events:inherit"
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
|
|
|
});
|
2009-06-03 15:00:48 +00:00
|
|
|
break;
|
2009-06-10 03:12:19 +00:00
|
|
|
case "text":
|
|
|
|
started = true;
|
|
|
|
var newText = addSvgElementFromJson({
|
|
|
|
"element": "text",
|
|
|
|
"attr": {
|
|
|
|
"x": x,
|
|
|
|
"y": y,
|
2009-07-11 13:15:16 +00:00
|
|
|
"id": getNextId(),
|
2009-08-31 14:54:59 +00:00
|
|
|
"fill": cur_text.fill,
|
|
|
|
"stroke": cur_shape.stroke,
|
|
|
|
"stroke-width": cur_text.stroke_width,
|
|
|
|
"stroke-dasharray": cur_shape.stroke_style,
|
|
|
|
"stroke-opacity": cur_shape.stroke_opacity,
|
|
|
|
"fill-opacity": cur_shape.fill_opacity,
|
2009-06-11 15:18:46 +00:00
|
|
|
// fix for bug where text elements were always 50% opacity
|
2009-08-31 14:54:59 +00:00
|
|
|
"opacity": cur_shape.opacity,
|
|
|
|
"font-size": cur_text.font_size,
|
|
|
|
"font-family": cur_text.font_family,
|
2009-09-21 22:16:44 +00:00
|
|
|
"text-anchor": "middle",
|
2009-10-13 00:51:15 +00:00
|
|
|
"style": "pointer-events:inherit",
|
|
|
|
"xml:space": "preserve"
|
2009-06-10 03:12:19 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
newText.textContent = "text";
|
|
|
|
break;
|
2009-10-18 20:46:32 +00:00
|
|
|
case "path":
|
2009-10-12 14:29:21 +00:00
|
|
|
setPointContainerTransform("");
|
2009-08-07 05:44:55 +00:00
|
|
|
started = true;
|
|
|
|
break;
|
2009-10-17 20:29:56 +00:00
|
|
|
case "pathedit":
|
2009-08-08 14:03:14 +00:00
|
|
|
started = true;
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path_oldd = current_path.getAttribute("d");
|
2009-08-08 22:17:59 +00:00
|
|
|
var id = evt.target.id;
|
2009-10-18 20:46:32 +00:00
|
|
|
if (id.substr(0,14) == "pathpointgrip_") {
|
2009-10-05 13:50:44 +00:00
|
|
|
// Select this point
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path_pt_drag = parseInt(id.substr(14));
|
|
|
|
canvas.addNodeToSelection(current_path_pt_drag);
|
2009-10-13 14:52:51 +00:00
|
|
|
updateSegLine();
|
2009-09-25 17:59:32 +00:00
|
|
|
} else if(id.indexOf("ctrlpointgrip_") == 0) {
|
|
|
|
current_ctrl_pt_drag = id.split('_')[1];
|
2009-10-23 17:27:37 +00:00
|
|
|
var node_num = current_ctrl_pt_drag.split('c')[0]-0;
|
|
|
|
canvas.addNodeToSelection(node_num);
|
2009-08-08 22:17:59 +00:00
|
|
|
}
|
2009-08-24 17:40:41 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
if(current_path_pt_drag == -1 && current_ctrl_pt_drag == -1) {
|
2009-10-17 20:29:56 +00:00
|
|
|
// if we haven't moused down on a shape, then go into multiselect mode
|
|
|
|
// otherwise, select it
|
2009-08-24 17:40:41 +00:00
|
|
|
canvas.clearSelection();
|
2009-10-22 03:29:49 +00:00
|
|
|
if (mouse_target.id != "svgroot") {
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path = null;
|
2009-10-22 03:29:49 +00:00
|
|
|
canvas.addToSelection([mouse_target], true);
|
2009-10-17 20:29:56 +00:00
|
|
|
canvas.setMode("select");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
canvas.setMode("multiselect");
|
|
|
|
if (rubberBox == null) {
|
|
|
|
rubberBox = selectorManager.getRubberBandBox();
|
|
|
|
}
|
|
|
|
assignAttributes(rubberBox, {
|
|
|
|
'x': start_x,
|
|
|
|
'y': start_y,
|
|
|
|
'width': 0,
|
|
|
|
'height': 0,
|
|
|
|
'display': 'inline'
|
|
|
|
}, 100);
|
2009-08-24 17:40:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-26 19:45:28 +00:00
|
|
|
break;
|
|
|
|
case "rotate":
|
|
|
|
started = true;
|
2009-09-04 05:10:48 +00:00
|
|
|
// we are starting an undoable change (a drag-rotation)
|
|
|
|
canvas.beginUndoableChange("transform", selectedElements);
|
2009-08-08 14:03:14 +00:00
|
|
|
break;
|
2009-08-07 05:44:55 +00:00
|
|
|
default:
|
|
|
|
console.log("Unknown mode in mousedown: " + current_mode);
|
|
|
|
break;
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-08-17 03:11:30 +00:00
|
|
|
// in this function we do not record any state changes yet (but we do update
|
2009-06-25 02:54:07 +00:00
|
|
|
// any elements that are still being created, moved or resized on the canvas)
|
2009-07-10 05:04:06 +00:00
|
|
|
// TODO: svgcanvas should just retain a reference to the image being dragged instead
|
|
|
|
// of the getId() and getElementById() funkiness - this will help us customize the ids
|
2009-10-18 20:46:32 +00:00
|
|
|
// a little bit for squares and paths
|
2009-06-07 00:17:21 +00:00
|
|
|
var mouseMove = function(evt)
|
2009-06-01 21:24:30 +00:00
|
|
|
{
|
2009-06-02 16:40:33 +00:00
|
|
|
if (!started) return;
|
2009-06-29 22:22:13 +00:00
|
|
|
var selected = selectedElements[0];
|
2009-10-22 03:18:54 +00:00
|
|
|
var pt = transformPoint( evt.pageX, evt.pageY, root_sctm );
|
2009-10-22 03:07:15 +00:00
|
|
|
var mouse_x = pt.x;
|
|
|
|
var mouse_y = pt.y;
|
2009-06-05 12:33:02 +00:00
|
|
|
var shape = svgdoc.getElementById(getId());
|
2009-09-04 22:22:40 +00:00
|
|
|
|
2009-09-11 13:32:56 +00:00
|
|
|
x = mouse_x / current_zoom;
|
|
|
|
y = mouse_y / current_zoom;
|
2009-09-09 14:18:24 +00:00
|
|
|
|
2009-09-06 23:25:12 +00:00
|
|
|
evt.preventDefault();
|
2009-11-13 16:55:58 +00:00
|
|
|
|
|
|
|
var setRect = function(square) {
|
|
|
|
var w = Math.abs(x - start_x),
|
|
|
|
h = Math.abs(y - start_y),
|
|
|
|
new_x, new_y;
|
|
|
|
if(square) {
|
|
|
|
w = h = Math.max(w, h);
|
|
|
|
new_x = start_x < x ? start_x : start_x - w;
|
|
|
|
new_y = start_y < y ? start_y : start_y - h;
|
|
|
|
} else {
|
|
|
|
new_x = Math.min(start_x,x);
|
|
|
|
new_y = Math.min(start_y,y);
|
|
|
|
}
|
|
|
|
|
|
|
|
assignAttributes(shape,{
|
|
|
|
'width': w,
|
|
|
|
'height': h,
|
|
|
|
'x': new_x,
|
|
|
|
'y': new_y
|
|
|
|
},1000);
|
|
|
|
}
|
|
|
|
|
2009-06-02 16:40:33 +00:00
|
|
|
switch (current_mode)
|
2009-06-01 21:24:30 +00:00
|
|
|
{
|
2009-06-08 02:17:07 +00:00
|
|
|
case "select":
|
2009-08-30 02:37:16 +00:00
|
|
|
// we temporarily use a translate on the element(s) being dragged
|
2009-07-01 19:28:08 +00:00
|
|
|
// this transform is removed upon mousing up and the element is
|
|
|
|
// relocated to the new location
|
2009-07-01 19:54:51 +00:00
|
|
|
if (selectedElements[0] != null) {
|
2009-06-08 02:17:07 +00:00
|
|
|
var dx = x - start_x;
|
|
|
|
var dy = y - start_y;
|
2009-08-08 14:03:14 +00:00
|
|
|
if (dx != 0 || dy != 0) {
|
|
|
|
var len = selectedElements.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
var selected = selectedElements[i];
|
|
|
|
if (selected == null) break;
|
2009-08-16 15:07:00 +00:00
|
|
|
var box = canvas.getBBox(selected);
|
2009-08-21 15:53:36 +00:00
|
|
|
selectedBBoxes[i].x = box.x + dx;
|
|
|
|
selectedBBoxes[i].y = box.y + dy;
|
2009-11-02 20:09:02 +00:00
|
|
|
|
|
|
|
// update the dummy transform in our transform list
|
|
|
|
// to be a translate
|
|
|
|
var xform = svgroot.createSVGTransform();
|
2009-11-03 14:27:33 +00:00
|
|
|
var tlist = canvas.getTransformList(selected);
|
2009-11-02 20:09:02 +00:00
|
|
|
xform.setTranslate(dx,dy);
|
2009-11-03 14:27:33 +00:00
|
|
|
if(tlist.numberOfItems) {
|
|
|
|
tlist.replaceItem(xform, 0);
|
|
|
|
} else {
|
|
|
|
tlist.appendItem(xform);
|
|
|
|
}
|
2009-11-02 20:09:02 +00:00
|
|
|
|
|
|
|
// update our internal bbox that we're tracking while dragging
|
2009-11-04 15:30:10 +00:00
|
|
|
selectorManager.requestSelector(selected).resize();//box); // TODO: remove box arg
|
2009-08-08 14:03:14 +00:00
|
|
|
}
|
2009-07-01 19:54:51 +00:00
|
|
|
}
|
2009-06-08 02:17:07 +00:00
|
|
|
}
|
2009-06-10 03:12:19 +00:00
|
|
|
break;
|
2009-06-30 06:24:41 +00:00
|
|
|
case "multiselect":
|
2009-09-10 19:20:26 +00:00
|
|
|
x *= current_zoom;
|
|
|
|
y *= current_zoom;
|
2009-08-24 19:19:27 +00:00
|
|
|
assignAttributes(rubberBox, {
|
|
|
|
'x': Math.min(start_x,x),
|
2009-08-24 19:20:52 +00:00
|
|
|
'y': Math.min(start_y,y),
|
2009-08-24 19:19:27 +00:00
|
|
|
'width': Math.abs(x-start_x),
|
|
|
|
'height': Math.abs(y-start_y)
|
|
|
|
},100);
|
2009-07-02 04:50:24 +00:00
|
|
|
|
2009-07-03 02:49:42 +00:00
|
|
|
// clear out selection and set it to the new list
|
2009-07-02 04:50:24 +00:00
|
|
|
canvas.clearSelection();
|
2009-11-22 00:50:09 +00:00
|
|
|
// FIXME: fix this, need to supply rect to getIntersectionList()
|
2009-07-02 04:50:24 +00:00
|
|
|
canvas.addToSelection(getIntersectionList());
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-03 02:49:42 +00:00
|
|
|
/*
|
|
|
|
// for each selected:
|
|
|
|
// - if newList contains selected, do nothing
|
|
|
|
// - if newList doesn't contain selected, remove it from selected
|
|
|
|
// - for any newList that was not in selectedElements, add it to selected
|
|
|
|
var elemsToRemove = [];
|
|
|
|
var newList = getIntersectionList();
|
|
|
|
var len = selectedElements.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
var ind = newList.indexOf(selectedElements[i]);
|
|
|
|
if (ind == -1) {
|
|
|
|
elemsToRemove.push(selectedElements[i]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
newList[ind] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (elemsToRemove.length > 0)
|
|
|
|
canvas.removeFromSelection(elemsToRemove);
|
|
|
|
*/
|
2009-06-30 06:24:41 +00:00
|
|
|
break;
|
2009-06-23 05:17:53 +00:00
|
|
|
case "resize":
|
|
|
|
// we track the resize bounding box and translate/scale the selected element
|
|
|
|
// while the mouse is down, when mouse goes up, we use this to recalculate
|
|
|
|
// the shape's coordinates
|
2009-08-16 15:07:00 +00:00
|
|
|
var box=canvas.getBBox(selected), left=box.x, top=box.y, width=box.width,
|
2009-06-23 05:17:53 +00:00
|
|
|
height=box.height, dx=(x-start_x), dy=(y-start_y);
|
2009-08-22 19:08:01 +00:00
|
|
|
|
2009-08-21 15:53:36 +00:00
|
|
|
// if rotated, adjust the dx,dy values
|
2009-08-22 19:08:01 +00:00
|
|
|
var angle = canvas.getRotationAngle(selected);
|
2009-08-21 15:53:36 +00:00
|
|
|
if (angle) {
|
|
|
|
var r = Math.sqrt( dx*dx + dy*dy );
|
2009-08-22 19:08:01 +00:00
|
|
|
var theta = Math.atan2(dy,dx) - angle * Math.PI / 180.0;
|
2009-08-21 15:53:36 +00:00
|
|
|
dx = r * Math.cos(theta);
|
|
|
|
dy = r * Math.sin(theta);
|
|
|
|
}
|
|
|
|
|
2009-08-22 19:08:01 +00:00
|
|
|
// if not stretching in y direction, set dy to 0
|
|
|
|
// if not stretching in x direction, set dx to 0
|
|
|
|
if(current_resize_mode.indexOf("n")==-1 && current_resize_mode.indexOf("s")==-1) {
|
|
|
|
dy = 0;
|
|
|
|
}
|
|
|
|
if(current_resize_mode.indexOf("e")==-1 && current_resize_mode.indexOf("w")==-1) {
|
|
|
|
dx = 0;
|
|
|
|
}
|
|
|
|
|
2009-06-23 05:17:53 +00:00
|
|
|
var ts = null;
|
2009-08-22 19:08:01 +00:00
|
|
|
var tx = 0, ty = 0;
|
2009-09-29 03:10:29 +00:00
|
|
|
var sy = height ? (height+dy)/height : 1,
|
|
|
|
sx = width ? (width+dx)/width : 1;
|
2009-08-27 22:26:47 +00:00
|
|
|
// if we are dragging on the north side, then adjust the scale factor and ty
|
2009-06-23 05:17:53 +00:00
|
|
|
if(current_resize_mode.indexOf("n") != -1) {
|
2009-09-29 03:10:29 +00:00
|
|
|
sy = height ? (height-dy)/height : 1;
|
2009-08-22 19:08:01 +00:00
|
|
|
ty = height;
|
2009-06-23 05:17:53 +00:00
|
|
|
}
|
2009-08-27 22:26:47 +00:00
|
|
|
|
|
|
|
// if we dragging on the east side, then adjust the scale factor and tx
|
2009-08-22 19:08:01 +00:00
|
|
|
if(current_resize_mode.indexOf("w") != -1) {
|
2009-09-29 03:10:29 +00:00
|
|
|
sx = width ? (width-dx)/width : 1;
|
2009-08-22 19:08:01 +00:00
|
|
|
tx = width;
|
2009-06-23 05:17:53 +00:00
|
|
|
}
|
2009-08-22 19:08:01 +00:00
|
|
|
|
2009-11-02 20:09:02 +00:00
|
|
|
// update the transform list with translate,scale,translate
|
|
|
|
var tlist = canvas.getTransformList(selected);
|
|
|
|
var translateOrigin = svgroot.createSVGTransform(),
|
|
|
|
scale = svgroot.createSVGTransform(),
|
|
|
|
translateBack = svgroot.createSVGTransform();
|
|
|
|
translateOrigin.setTranslate(-(left+tx),-(top+ty));
|
2009-11-13 15:11:48 +00:00
|
|
|
if(evt.shiftKey) {
|
|
|
|
if(sx == 1) sx = sy
|
|
|
|
else sy = sx;
|
|
|
|
}
|
2009-11-02 20:09:02 +00:00
|
|
|
scale.setScale(sx,sy);
|
2009-11-13 15:11:48 +00:00
|
|
|
|
|
|
|
|
2009-11-02 20:09:02 +00:00
|
|
|
translateBack.setTranslate(left+tx,top+ty);
|
|
|
|
var N = tlist.numberOfItems;
|
|
|
|
tlist.replaceItem(translateBack, N-3);
|
|
|
|
tlist.replaceItem(scale, N-2);
|
|
|
|
tlist.replaceItem(translateOrigin, N-1);
|
|
|
|
|
2009-08-27 22:26:47 +00:00
|
|
|
var selectedBBox = selectedBBoxes[0];
|
|
|
|
|
|
|
|
// reset selected bbox top-left position
|
|
|
|
selectedBBox.x = left;
|
|
|
|
selectedBBox.y = top;
|
2009-08-22 19:08:01 +00:00
|
|
|
|
2009-08-27 22:26:47 +00:00
|
|
|
// if this is a translate, adjust the box position
|
2009-08-22 19:08:01 +00:00
|
|
|
if (tx) {
|
2009-08-27 22:26:47 +00:00
|
|
|
selectedBBox.x += dx;
|
2009-08-22 19:08:01 +00:00
|
|
|
}
|
|
|
|
if (ty) {
|
2009-08-27 22:26:47 +00:00
|
|
|
selectedBBox.y += dy;
|
2009-08-22 19:08:01 +00:00
|
|
|
}
|
2009-08-27 22:26:47 +00:00
|
|
|
|
|
|
|
// update box width/height
|
2009-09-17 14:47:54 +00:00
|
|
|
selectedBBox.width = round(width*sx);
|
|
|
|
selectedBBox.height = round(height*sy);
|
2009-08-27 22:26:47 +00:00
|
|
|
|
2009-06-23 05:46:47 +00:00
|
|
|
// normalize selectedBBox
|
|
|
|
if (selectedBBox.width < 0) {
|
2009-08-27 22:26:47 +00:00
|
|
|
selectedBBox.width *= -1;
|
|
|
|
// if we are dragging on the east side and scaled negatively
|
|
|
|
if(current_resize_mode.indexOf("e") != -1 && sx < 0) {
|
|
|
|
selectedBBox.x = box.x - selectedBBox.width;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
selectedBBox.x -= selectedBBox.width;
|
|
|
|
}
|
2009-06-23 05:46:47 +00:00
|
|
|
}
|
|
|
|
if (selectedBBox.height < 0) {
|
2009-08-27 22:26:47 +00:00
|
|
|
selectedBBox.height *= -1;
|
|
|
|
// if we are dragging on the south side and scaled negatively
|
|
|
|
if(current_resize_mode.indexOf("s") != -1 && sy < 0) {
|
|
|
|
selectedBBox.y = box.y - selectedBBox.height;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
selectedBBox.y -= selectedBBox.height;
|
|
|
|
}
|
2009-08-16 14:56:22 +00:00
|
|
|
}
|
2009-08-27 22:26:47 +00:00
|
|
|
|
2009-11-04 15:30:10 +00:00
|
|
|
selectorManager.requestSelector(selected).resize();//selectedBBox); // TODO: remove box arg
|
2009-06-23 07:48:15 +00:00
|
|
|
break;
|
2009-09-16 17:18:11 +00:00
|
|
|
case "zoom":
|
|
|
|
x *= current_zoom;
|
|
|
|
y *= current_zoom;
|
|
|
|
assignAttributes(rubberBox, {
|
|
|
|
'x': Math.min(start_x*current_zoom,x),
|
|
|
|
'y': Math.min(start_y*current_zoom,y),
|
|
|
|
'width': Math.abs(x-start_x*current_zoom),
|
|
|
|
'height': Math.abs(y-start_y*current_zoom)
|
|
|
|
},100);
|
|
|
|
break;
|
2009-06-10 03:12:19 +00:00
|
|
|
case "text":
|
2009-08-24 19:19:27 +00:00
|
|
|
assignAttributes(shape,{
|
|
|
|
'x': x,
|
|
|
|
'y': y
|
|
|
|
},1000);
|
2009-06-10 03:12:19 +00:00
|
|
|
break;
|
2009-06-02 16:40:33 +00:00
|
|
|
case "line":
|
2009-10-14 03:03:13 +00:00
|
|
|
// Opera has a problem with suspendRedraw() apparently
|
|
|
|
var handle = null;
|
|
|
|
if (!window.opera) svgroot.suspendRedraw(1000);
|
2009-06-02 16:40:33 +00:00
|
|
|
shape.setAttributeNS(null, "x2", x);
|
|
|
|
shape.setAttributeNS(null, "y2", y);
|
2009-10-14 03:03:13 +00:00
|
|
|
if (!window.opera) svgroot.unsuspendRedraw(handle);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "square":
|
2009-11-13 16:55:58 +00:00
|
|
|
setRect(true);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "rect":
|
2009-09-04 22:22:40 +00:00
|
|
|
case "image":
|
2009-11-13 16:55:58 +00:00
|
|
|
setRect(evt.shiftKey);
|
2009-09-04 22:22:40 +00:00
|
|
|
break;
|
2009-06-02 16:40:33 +00:00
|
|
|
case "circle":
|
|
|
|
var cx = shape.getAttributeNS(null, "cx");
|
|
|
|
var cy = shape.getAttributeNS(null, "cy");
|
|
|
|
var rad = Math.sqrt( (x-cx)*(x-cx) + (y-cy)*(y-cy) );
|
2009-06-03 15:00:48 +00:00
|
|
|
shape.setAttributeNS(null, "r", rad);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "ellipse":
|
|
|
|
var cx = shape.getAttributeNS(null, "cx");
|
|
|
|
var cy = shape.getAttributeNS(null, "cy");
|
2009-10-14 03:03:13 +00:00
|
|
|
// Opera has a problem with suspendRedraw() apparently
|
|
|
|
var handle = null;
|
|
|
|
if (!window.opera) svgroot.suspendRedraw(1000);
|
2009-06-02 16:40:33 +00:00
|
|
|
shape.setAttributeNS(null, "rx", Math.abs(x - cx) );
|
2009-11-13 16:55:58 +00:00
|
|
|
var ry = Math.abs(evt.shiftKey?(x - cx):(y - cy));
|
|
|
|
shape.setAttributeNS(null, "ry", ry );
|
2009-10-14 03:03:13 +00:00
|
|
|
if (!window.opera) svgroot.unsuspendRedraw(handle);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "fhellipse":
|
|
|
|
case "fhrect":
|
|
|
|
freehand_min_x = Math.min(x, freehand_min_x);
|
|
|
|
freehand_max_x = Math.max(x, freehand_max_x);
|
|
|
|
freehand_min_y = Math.min(y, freehand_min_y);
|
|
|
|
freehand_max_y = Math.max(y, freehand_max_y);
|
2009-06-03 08:41:25 +00:00
|
|
|
// break; missing on purpose
|
2009-10-18 20:04:26 +00:00
|
|
|
case "fhpath":
|
2009-06-08 02:17:07 +00:00
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
2009-08-07 17:19:32 +00:00
|
|
|
d_attr += + x + "," + y + " ";
|
|
|
|
shape.setAttributeNS(null, "points", d_attr);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
2009-10-18 20:46:32 +00:00
|
|
|
// update path stretch line coordinates
|
|
|
|
case "path":
|
|
|
|
var line = document.getElementById("path_stretch_line");
|
2009-08-07 05:44:55 +00:00
|
|
|
if (line) {
|
2009-09-10 20:34:20 +00:00
|
|
|
line.setAttribute("x2", x *= current_zoom);
|
|
|
|
line.setAttribute("y2", y *= current_zoom);
|
2009-08-07 05:44:55 +00:00
|
|
|
}
|
|
|
|
break;
|
2009-10-17 20:29:56 +00:00
|
|
|
case "pathedit":
|
2009-08-08 22:17:59 +00:00
|
|
|
// if we are dragging a point, let's move it
|
2009-10-18 20:46:32 +00:00
|
|
|
if (current_path_pt_drag != -1 && current_path) {
|
|
|
|
var old_path_pts = $.map(current_path_pts, function(n){return n/current_zoom;});
|
|
|
|
updatePath(mouse_x, mouse_y, old_path_pts);
|
|
|
|
} else if (current_ctrl_pt_drag != -1 && current_path) {
|
2009-09-25 17:59:32 +00:00
|
|
|
// Moving the control point. Since only one segment is altered,
|
|
|
|
// we only need to do a pathSegList replace.
|
|
|
|
var data = current_ctrl_pt_drag.split('c');
|
|
|
|
var index = data[0]-0;
|
|
|
|
var ctrl_num = data[1]-0;
|
2009-10-18 20:46:32 +00:00
|
|
|
var c_item = current_path.pathSegList.getItem(index+1);
|
2009-09-29 15:58:46 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var angle = canvas.getRotationAngle(current_path) * Math.PI / 180.0;
|
2009-10-07 20:16:35 +00:00
|
|
|
if (angle) {
|
|
|
|
// calculate the shape's old center that was used for rotation
|
|
|
|
var box = selectedBBoxes[0];
|
|
|
|
var cx = round(box.x + box.width/2) * current_zoom,
|
|
|
|
cy = round(box.y + box.height/2) * current_zoom;
|
|
|
|
var dx = mouse_x - cx, dy = mouse_y - cy;
|
|
|
|
var r = Math.sqrt( dx*dx + dy*dy );
|
|
|
|
var theta = Math.atan2(dy,dx) - angle;
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path_pts[i] = mouse_x = cx + r * Math.cos(theta);
|
|
|
|
current_path_pts[i+1] = mouse_y = cy + r * Math.sin(theta);
|
2009-10-07 20:16:35 +00:00
|
|
|
x = mouse_x / current_zoom;
|
|
|
|
y = mouse_y / current_zoom;
|
|
|
|
}
|
|
|
|
|
2009-10-09 19:47:32 +00:00
|
|
|
c_item['x' + ctrl_num] = x;
|
|
|
|
c_item['y' + ctrl_num] = y;
|
|
|
|
replacePathSeg(6, index+1, [c_item.x,c_item.y, c_item.x1,c_item.y1, c_item.x2,c_item.y2]);
|
2009-10-13 14:52:51 +00:00
|
|
|
|
|
|
|
updateSegLine(true);
|
|
|
|
|
2009-09-25 17:59:32 +00:00
|
|
|
var grip = document.getElementById("ctrlpointgrip_" + current_ctrl_pt_drag);
|
|
|
|
if(grip) {
|
|
|
|
grip.setAttribute("cx", mouse_x);
|
|
|
|
grip.setAttribute("cy", mouse_y);
|
|
|
|
|
|
|
|
var line = document.getElementById("ctrlLine_"+current_ctrl_pt_drag);
|
|
|
|
line.setAttribute("x2", mouse_x);
|
|
|
|
line.setAttribute("y2", mouse_y);
|
|
|
|
}
|
2009-08-08 22:17:59 +00:00
|
|
|
}
|
|
|
|
break;
|
2009-08-26 19:45:28 +00:00
|
|
|
case "rotate":
|
2009-09-04 16:48:20 +00:00
|
|
|
var box = canvas.getBBox(selected),
|
2009-09-17 14:47:54 +00:00
|
|
|
cx = round(box.x + box.width/2),
|
|
|
|
cy = round(box.y + box.height/2);
|
|
|
|
var angle = round(((Math.atan2(cy-y,cx-x) * (180/Math.PI))-90) % 360);
|
2009-09-04 05:10:48 +00:00
|
|
|
canvas.setRotationAngle(angle<-180?(360+angle):angle, true);
|
2009-11-02 20:09:02 +00:00
|
|
|
call("changed", selectedElements);
|
2009-08-26 19:45:28 +00:00
|
|
|
break;
|
2009-08-07 05:44:55 +00:00
|
|
|
default:
|
|
|
|
break;
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
2009-11-02 20:09:02 +00:00
|
|
|
}; // mouseMove()
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-10-23 15:21:27 +00:00
|
|
|
var shortFloat = function(val) {
|
2009-11-17 16:42:04 +00:00
|
|
|
var digits = save_options.round_digits;
|
2009-10-23 15:21:27 +00:00
|
|
|
if(!isNaN(val)) {
|
2009-10-23 17:27:37 +00:00
|
|
|
return Number(Number(val).toFixed(digits));
|
2009-10-23 15:21:27 +00:00
|
|
|
} else if($.isArray(val)) {
|
|
|
|
return shortFloat(val[0]) + ',' + shortFloat(val[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var convertPath = function(path, toRel) {
|
|
|
|
var segList = path.pathSegList;
|
|
|
|
var len = segList.numberOfItems;
|
|
|
|
var curx = 0, cury = 0;
|
|
|
|
var d = "";
|
|
|
|
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
var seg = segList.getItem(i);
|
|
|
|
// if these properties are not in the segment, set them to zero
|
|
|
|
var x = seg.x || 0,
|
|
|
|
y = seg.y || 0,
|
|
|
|
x1 = seg.x1 || 0,
|
|
|
|
y1 = seg.y1 || 0,
|
|
|
|
x2 = seg.x2 || 0,
|
|
|
|
y2 = seg.y2 || 0;
|
|
|
|
|
|
|
|
var type = seg.pathSegType;
|
|
|
|
var letter = pathMap[type]['to'+(toRel?'Lower':'Upper')+'Case']();
|
|
|
|
var addToD = function(pnts, more, last) {
|
|
|
|
var str = '';
|
|
|
|
var more = more?' '+more.join(' '):'';
|
|
|
|
var last = last?shortFloat(last):'';
|
|
|
|
$.each(pnts, function(i, pnt) {
|
|
|
|
pnts[i] = shortFloat(pnt);
|
|
|
|
});
|
|
|
|
d += letter + pnts.join(' ') + more + last;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case 1: // z,Z closepath (Z/z)
|
|
|
|
d += "z";
|
|
|
|
break;
|
|
|
|
case 2: // absolute move (M)
|
|
|
|
case 4: // absolute line (L)
|
|
|
|
case 12: // absolute horizontal line (H)
|
|
|
|
case 14: // absolute vertical line (V)
|
|
|
|
case 18: // absolute smooth quad (T)
|
|
|
|
x -= curx;
|
|
|
|
y -= cury;
|
|
|
|
case 3: // relative move (m)
|
|
|
|
case 5: // relative line (l)
|
|
|
|
case 13: // relative horizontal line (h)
|
|
|
|
case 15: // relative vertical line (v)
|
|
|
|
case 19: // relative smooth quad (t)
|
|
|
|
if(toRel) {
|
|
|
|
curx += x;
|
|
|
|
cury += y;
|
|
|
|
} else {
|
|
|
|
x += curx;
|
|
|
|
y += cury;
|
|
|
|
curx = x;
|
|
|
|
cury = y;
|
|
|
|
}
|
|
|
|
addToD([[x,y]]);
|
|
|
|
break;
|
|
|
|
case 6: // absolute cubic (C)
|
|
|
|
x -= curx; x1 -= curx; x2 -= curx;
|
|
|
|
y -= cury; y1 -= cury; y2 -= cury;
|
|
|
|
case 7: // relative cubic (c)
|
|
|
|
if(toRel) {
|
|
|
|
curx += x;
|
|
|
|
cury += y;
|
|
|
|
} else {
|
|
|
|
x += curx; x1 += curx; x2 += curx;
|
|
|
|
y += cury; y1 += cury; y2 += cury;
|
|
|
|
curx = x;
|
|
|
|
cury = y;
|
|
|
|
}
|
|
|
|
addToD([[x1,y1],[x2,y2],[x,y]]);
|
|
|
|
break;
|
|
|
|
case 8: // absolute quad (Q)
|
|
|
|
x -= curx; x1 -= curx;
|
|
|
|
y -= cury; y1 -= cury;
|
|
|
|
case 9: // relative quad (q)
|
|
|
|
if(toRel) {
|
|
|
|
curx += x;
|
|
|
|
cury += y;
|
|
|
|
} else {
|
|
|
|
x += curx; x1 += curx;
|
|
|
|
y += cury; y1 += cury;
|
|
|
|
curx = x;
|
|
|
|
cury = y;
|
|
|
|
}
|
|
|
|
addToD([[x1,y1],[x,y]]);
|
|
|
|
break;
|
|
|
|
case 10: // absolute elliptical arc (A)
|
|
|
|
x -= curx;
|
|
|
|
y -= cury;
|
|
|
|
case 11: // relative elliptical arc (a)
|
|
|
|
if(toRel) {
|
|
|
|
curx += x;
|
|
|
|
cury += y;
|
|
|
|
} else {
|
|
|
|
x += curx;
|
|
|
|
y += cury;
|
|
|
|
curx = x;
|
|
|
|
cury = y;
|
|
|
|
}
|
|
|
|
addToD([[seg.r1,seg.r2]], [
|
|
|
|
seg.angle,
|
|
|
|
(seg.largeArcFlag ? 1 : 0),
|
|
|
|
(seg.sweepFlag ? 1 : 0)
|
|
|
|
],[x,y]
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 16: // absolute smooth cubic (S)
|
|
|
|
x -= curx; x2 -= curx;
|
|
|
|
y -= cury; y2 -= cury;
|
|
|
|
case 17: // relative smooth cubic (s)
|
|
|
|
if(toRel) {
|
|
|
|
curx += x;
|
|
|
|
cury += y;
|
|
|
|
} else {
|
|
|
|
x += curx; x2 += curx;
|
|
|
|
y += cury; y2 += cury;
|
|
|
|
curx = x;
|
|
|
|
cury = y;
|
|
|
|
}
|
|
|
|
addToD([[x2,y2],[x,y]]);
|
|
|
|
break;
|
|
|
|
} // switch on path segment type
|
|
|
|
} // for each segment
|
|
|
|
return d;
|
|
|
|
};
|
|
|
|
|
2009-10-07 16:04:32 +00:00
|
|
|
var resetPointGrips = function() {
|
|
|
|
var sr = svgroot.suspendRedraw(100);
|
2009-10-18 20:46:32 +00:00
|
|
|
removeAllPointGripsFromPath();
|
|
|
|
recalcPathPoints();
|
|
|
|
addAllPointGripsToPath();
|
2009-10-07 16:04:32 +00:00
|
|
|
svgroot.unsuspendRedraw(sr);
|
|
|
|
};
|
2009-10-07 18:56:03 +00:00
|
|
|
|
2009-10-09 15:29:40 +00:00
|
|
|
var setPointContainerTransform = function(value) {
|
2009-10-18 20:46:32 +00:00
|
|
|
var conts = $('#pathpointgrip_container,#ctrlpointgrip_container');
|
2009-10-09 15:29:40 +00:00
|
|
|
$.each(conts,function() {
|
|
|
|
this.setAttribute("transform", value);
|
|
|
|
if(!value) {
|
|
|
|
this.removeAttribute("transform");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var recalcPathPoints = function() {
|
|
|
|
current_path_pts = [];
|
|
|
|
var segList = current_path.pathSegList;
|
2009-10-07 18:56:03 +00:00
|
|
|
var curx = segList.getItem(0).x, cury = segList.getItem(0).y;
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path_pts.push(curx * current_zoom);
|
|
|
|
current_path_pts.push(cury * current_zoom);
|
2009-10-07 18:56:03 +00:00
|
|
|
var len = segList.numberOfItems;
|
|
|
|
for (var i = 1; i < len; ++i) {
|
|
|
|
var l = segList.getItem(i);
|
|
|
|
var x = l.x, y = l.y;
|
2009-10-18 20:46:32 +00:00
|
|
|
// paths can now be closed, skip Z segments
|
2009-10-07 18:56:03 +00:00
|
|
|
if (l.pathSegType == 1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
var type = l.pathSegType;
|
2009-10-18 20:46:32 +00:00
|
|
|
// current_path_pts just holds the absolute coords
|
2009-10-07 18:56:03 +00:00
|
|
|
if (type == 4) {
|
|
|
|
curx = x;
|
|
|
|
cury = y;
|
|
|
|
} // type 4 (abs line)
|
|
|
|
else if (type == 5) {
|
|
|
|
curx += x;
|
|
|
|
cury += y;
|
|
|
|
} // type 5 (rel line)
|
|
|
|
else if (type == 6) {
|
|
|
|
curx = x;
|
|
|
|
cury = y;
|
|
|
|
} // type 6 (abs curve)
|
|
|
|
else if (type == 7) {
|
|
|
|
curx += x;
|
|
|
|
cury += y;
|
|
|
|
} // type 7 (rel curve)
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path_pts.push(curx * current_zoom);
|
|
|
|
current_path_pts.push(cury * current_zoom);
|
2009-10-07 18:56:03 +00:00
|
|
|
} // for each segment
|
|
|
|
}
|
2009-10-07 16:04:32 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var removeAllPointGripsFromPath = function() {
|
2009-08-08 14:03:14 +00:00
|
|
|
// loop through and hide all pointgrips
|
2009-10-18 20:46:32 +00:00
|
|
|
$('#pathpointgrip_container > *').attr("display", "none");
|
2009-09-25 17:59:32 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var line = document.getElementById("path_stretch_line");
|
2009-08-30 02:24:20 +00:00
|
|
|
if (line) line.setAttribute("display", "none");
|
2009-09-25 17:59:32 +00:00
|
|
|
|
2009-10-05 19:09:39 +00:00
|
|
|
$('#ctrlpointgrip_container *').attr('display','none');
|
2009-08-08 14:03:14 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var addAllPointGripsToPath = function(pointToSelect) {
|
2009-09-12 04:49:50 +00:00
|
|
|
// loop through and show all pointgrips
|
2009-10-18 20:46:32 +00:00
|
|
|
var len = current_path_pts.length;
|
2009-08-31 18:02:15 +00:00
|
|
|
for (var i = 0; i < len; i += 2) {
|
2009-10-18 20:46:32 +00:00
|
|
|
var grip = document.getElementById("pathpointgrip_"+i/2);
|
2009-08-31 18:02:15 +00:00
|
|
|
if (grip) {
|
|
|
|
assignAttributes(grip, {
|
2009-10-18 20:46:32 +00:00
|
|
|
'cx': current_path_pts[i],
|
|
|
|
'cy': current_path_pts[i+1],
|
2009-08-31 18:02:15 +00:00
|
|
|
'display': 'inline'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
2009-10-18 20:46:32 +00:00
|
|
|
addPointGripToPath(current_path_pts[i], current_path_pts[i+1],i/2);
|
2009-08-31 18:02:15 +00:00
|
|
|
}
|
2009-09-25 20:03:20 +00:00
|
|
|
|
|
|
|
var index = i/2;
|
2009-10-18 20:46:32 +00:00
|
|
|
var item = current_path.pathSegList.getItem(index);
|
2009-10-09 19:47:32 +00:00
|
|
|
if(item.pathSegType == 6) {
|
2009-09-25 20:03:20 +00:00
|
|
|
index -= 1;
|
|
|
|
// Same code as when making a curve, needs to be in own function
|
2009-10-18 20:46:32 +00:00
|
|
|
var cur_x = getPathPoint(index)[0];
|
|
|
|
var cur_y = getPathPoint(index)[1];
|
|
|
|
var next_x = getPathPoint(index+1)[0];
|
|
|
|
var next_y = getPathPoint(index+1)[1];
|
2009-10-09 19:47:32 +00:00
|
|
|
addControlPointGrip(item.x1,item.y1, cur_x,cur_y, index+'c1');
|
|
|
|
addControlPointGrip(item.x2,item.y2, next_x,next_y, index+'c2');
|
2009-09-29 15:58:46 +00:00
|
|
|
}
|
2009-08-08 14:03:14 +00:00
|
|
|
}
|
2009-10-18 20:46:32 +00:00
|
|
|
// FIXME: we cannot just use the same transform as the path because we might be
|
2009-09-12 04:49:50 +00:00
|
|
|
// at a different zoom level
|
2009-10-18 20:46:32 +00:00
|
|
|
var angle = canvas.getRotationAngle(current_path);
|
2009-09-12 04:49:50 +00:00
|
|
|
if (angle) {
|
2009-10-18 20:46:32 +00:00
|
|
|
var bbox = canvas.getBBox(current_path);
|
2009-09-12 04:49:50 +00:00
|
|
|
var cx = (bbox.x + bbox.width/2) * current_zoom,
|
|
|
|
cy = (bbox.y + bbox.height/2) * current_zoom;
|
|
|
|
var xform = ["rotate(", angle, " ", cx, ",", cy, ")"].join("");
|
2009-10-09 15:29:40 +00:00
|
|
|
setPointContainerTransform(xform);
|
|
|
|
}
|
|
|
|
if(pointToSelect != null) {
|
|
|
|
canvas.addNodeToSelection(pointToSelect);
|
2009-09-12 04:49:50 +00:00
|
|
|
}
|
2009-08-08 03:36:01 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var addPointGripToPath = function(x,y,index) {
|
2009-08-08 03:36:01 +00:00
|
|
|
// create the container of all the point grips
|
2009-10-18 20:46:32 +00:00
|
|
|
var pointGripContainer = document.getElementById("pathpointgrip_container");
|
2009-08-08 03:36:01 +00:00
|
|
|
if (!pointGripContainer) {
|
|
|
|
var parent = document.getElementById("selectorParentGroup");
|
|
|
|
pointGripContainer = parent.appendChild(document.createElementNS(svgns, "g"));
|
2009-10-18 20:46:32 +00:00
|
|
|
pointGripContainer.id = "pathpointgrip_container";
|
2009-08-08 03:36:01 +00:00
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var pointGrip = document.getElementById("pathpointgrip_"+index);
|
2009-08-08 03:36:01 +00:00
|
|
|
// create it
|
|
|
|
if (!pointGrip) {
|
|
|
|
pointGrip = document.createElementNS(svgns, "circle");
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(pointGrip, {
|
2009-10-18 20:46:32 +00:00
|
|
|
'id': "pathpointgrip_" + index,
|
2009-08-24 20:00:48 +00:00
|
|
|
'display': "none",
|
|
|
|
'r': 4,
|
2009-10-13 14:52:51 +00:00
|
|
|
'fill': "#0FF",
|
2009-08-24 20:00:48 +00:00
|
|
|
'stroke': "#00F",
|
|
|
|
'stroke-width': 2,
|
|
|
|
'cursor': 'move',
|
2009-10-16 13:17:23 +00:00
|
|
|
'style': 'pointer-events:all',
|
|
|
|
'xlink:title': 'Drag point to move it. Double-click point to change segment type.'
|
2009-08-24 20:00:48 +00:00
|
|
|
});
|
2009-08-08 03:36:01 +00:00
|
|
|
pointGrip = pointGripContainer.appendChild(pointGrip);
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var grip = $('#pathpointgrip_'+index);
|
2009-10-13 14:52:51 +00:00
|
|
|
grip.dblclick(function() {
|
2009-10-09 15:29:40 +00:00
|
|
|
canvas.setSegType();
|
2009-09-25 17:59:32 +00:00
|
|
|
});
|
2009-08-08 03:36:01 +00:00
|
|
|
}
|
2009-10-13 14:52:51 +00:00
|
|
|
|
2009-08-08 03:36:01 +00:00
|
|
|
// set up the point grip element and display it
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(pointGrip, {
|
|
|
|
'cx': x,
|
|
|
|
'cy': y,
|
|
|
|
'display': "inline",
|
|
|
|
});
|
2009-08-08 03:36:01 +00:00
|
|
|
};
|
2009-09-25 17:59:32 +00:00
|
|
|
|
2009-10-13 14:52:51 +00:00
|
|
|
var updateSegLine = function(next_node) {
|
|
|
|
// create segment line
|
|
|
|
var segLine = document.getElementById("segline");
|
|
|
|
if(!segLine) {
|
2009-10-18 20:46:32 +00:00
|
|
|
var pointGripContainer = $('#pathpointgrip_container')[0];
|
2009-10-13 14:52:51 +00:00
|
|
|
segLine = document.createElementNS(svgns, "path");
|
|
|
|
assignAttributes(segLine, {
|
|
|
|
'id': "segline",
|
|
|
|
'fill': "none",
|
|
|
|
'stroke': "#0FF",
|
|
|
|
'stroke-width': 2,
|
|
|
|
'style':'pointer-events:none'
|
|
|
|
});
|
|
|
|
segLine = pointGripContainer.appendChild(segLine);
|
|
|
|
}
|
|
|
|
if(!segLine.getAttribute('d')) {
|
2009-10-18 20:46:32 +00:00
|
|
|
var pt = getPathPoint(current_path_pt);
|
2009-10-13 14:52:51 +00:00
|
|
|
segLine.setAttribute('d', 'M' + pt.join(',') + ' 0,0');
|
|
|
|
}
|
|
|
|
segLine.setAttribute('display','inline');
|
|
|
|
|
2009-11-10 16:43:02 +00:00
|
|
|
if(current_path_pt+1 >= current_path.pathSegList.numberOfItems) {
|
|
|
|
segLine.setAttribute('display','none');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-10-13 14:52:51 +00:00
|
|
|
if(!next_node) {
|
|
|
|
// Replace "M" val
|
2009-10-18 20:46:32 +00:00
|
|
|
replacePathSeg(2, 0, getPathPoint(current_path_pt, true), segLine);
|
2009-10-13 14:52:51 +00:00
|
|
|
} else {
|
2009-10-18 20:46:32 +00:00
|
|
|
var seg = current_path.pathSegList.getItem(current_path_pt+1);
|
2009-10-13 14:52:51 +00:00
|
|
|
var points = [seg.x, seg.y];
|
|
|
|
if(seg.x1 != null && seg.x2 != null) {
|
|
|
|
points.splice(2, 0, seg.x1, seg.y1, seg.x2, seg.y2);
|
|
|
|
}
|
|
|
|
points = $.map(points, function(n){return n*current_zoom;});
|
|
|
|
replacePathSeg(seg.pathSegType, 1, points, segLine);
|
|
|
|
}
|
|
|
|
}
|
2009-10-09 15:29:40 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var updatePath = function(mouse_x, mouse_y, old_path_pts) {
|
2009-10-05 13:50:44 +00:00
|
|
|
var x = mouse_x / current_zoom;
|
|
|
|
var y = mouse_y / current_zoom;
|
2009-10-07 18:56:03 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var is_closed = current_path.getAttribute('d').toLowerCase().indexOf('z') != -1;
|
2009-10-05 13:50:44 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var i = current_path_pt_drag * 2;
|
|
|
|
var last_index = current_path_pts.length/2 - 1;
|
|
|
|
var is_first = current_path_pt_drag == 0 || (is_closed && current_path_pt_drag == last_index);
|
|
|
|
var is_last = !is_closed && current_path_pt_drag == last_index;
|
2009-10-05 13:50:44 +00:00
|
|
|
|
|
|
|
// if the image is rotated, then we must modify the x,y mouse coordinates
|
|
|
|
// and rotate them into the shape's rotated coordinate system
|
|
|
|
// we also re-map mouse_x/y and x/y into the rotated coordinate system
|
2009-10-18 20:46:32 +00:00
|
|
|
var angle = canvas.getRotationAngle(current_path) * Math.PI / 180.0;
|
2009-10-05 13:50:44 +00:00
|
|
|
if (angle) {
|
|
|
|
// calculate the shape's old center that was used for rotation
|
|
|
|
var box = selectedBBoxes[0];
|
|
|
|
var cx = round(box.x + box.width/2) * current_zoom,
|
|
|
|
cy = round(box.y + box.height/2) * current_zoom;
|
|
|
|
var dx = mouse_x - cx, dy = mouse_y - cy;
|
|
|
|
var r = Math.sqrt( dx*dx + dy*dy );
|
|
|
|
var theta = Math.atan2(dy,dx) - angle;
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path_pts[i] = mouse_x = cx + r * Math.cos(theta);
|
|
|
|
current_path_pts[i+1] = mouse_y = cy + r * Math.sin(theta);
|
2009-10-05 13:50:44 +00:00
|
|
|
x = mouse_x / current_zoom;
|
|
|
|
y = mouse_y / current_zoom;
|
|
|
|
}
|
|
|
|
else {
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path_pts[i] = x * current_zoom;
|
|
|
|
current_path_pts[i+1] = y * current_zoom;
|
2009-10-05 13:50:44 +00:00
|
|
|
}
|
|
|
|
|
2009-10-07 18:56:03 +00:00
|
|
|
if(is_first && is_closed) {
|
2009-10-05 13:50:44 +00:00
|
|
|
// Update the first point
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path_pts[0] = current_path_pts[i];
|
|
|
|
current_path_pts[1] = current_path_pts[i+1];
|
|
|
|
current_path_pt_drag = 0;
|
2009-10-05 13:50:44 +00:00
|
|
|
}
|
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var index = current_path_pt_drag;
|
|
|
|
var abs_x = getPathPoint(index)[0];
|
|
|
|
var abs_y = getPathPoint(index)[1];
|
2009-10-05 13:50:44 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var item = current_path.pathSegList.getItem(index);
|
|
|
|
var x_diff = x - old_path_pts[index*2];
|
|
|
|
var y_diff = y - old_path_pts[index*2 + 1];
|
2009-10-05 13:50:44 +00:00
|
|
|
|
|
|
|
var cur_type = item.pathSegType;
|
|
|
|
var points = [];
|
|
|
|
|
2009-10-09 19:47:32 +00:00
|
|
|
if(cur_type == 6) {
|
|
|
|
points = [abs_x,abs_y, item.x1,item.y1, item.x2 + x_diff,item.y2 + y_diff];
|
2009-10-05 13:50:44 +00:00
|
|
|
} else {
|
|
|
|
if(is_first) {
|
|
|
|
// Need absolute position for first point
|
2009-10-18 20:46:32 +00:00
|
|
|
points = getPathPoint(0);
|
2009-10-05 13:50:44 +00:00
|
|
|
} else {
|
2009-10-09 19:47:32 +00:00
|
|
|
points = [abs_x, abs_y];
|
2009-10-05 13:50:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
replacePathSeg(cur_type, index, points);
|
|
|
|
|
|
|
|
var setSeg = function(index,first) {
|
2009-10-18 20:46:32 +00:00
|
|
|
var points, item = current_path.pathSegList.getItem(index);
|
2009-10-05 13:50:44 +00:00
|
|
|
var type = item.pathSegType;
|
|
|
|
if(first) {
|
2009-10-09 19:47:32 +00:00
|
|
|
item.x += x_diff;
|
|
|
|
item.y += y_diff;
|
2009-10-05 13:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case 1:
|
|
|
|
points = [];
|
|
|
|
break;
|
2009-10-09 19:47:32 +00:00
|
|
|
case 4:
|
|
|
|
points = [item.x, item.y];
|
2009-10-05 13:50:44 +00:00
|
|
|
break;
|
2009-10-09 19:47:32 +00:00
|
|
|
case 6:
|
2009-10-12 18:33:27 +00:00
|
|
|
if(first) {
|
|
|
|
item.x1 -= x_diff;
|
|
|
|
item.y1 -= y_diff;
|
|
|
|
item.x2 += x_diff;
|
|
|
|
item.y2 += y_diff;
|
|
|
|
}
|
|
|
|
|
2009-10-09 19:47:32 +00:00
|
|
|
points = [item.x, item.y, item.x1 + x_diff,item.y1 + y_diff, item.x2,item.y2];
|
2009-10-05 13:50:44 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
replacePathSeg(type, index, points);
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2009-10-07 18:56:03 +00:00
|
|
|
if(is_closed || !is_last) {
|
|
|
|
var next_type = setSeg(index+1);
|
|
|
|
} else {
|
|
|
|
var next_type = 0;
|
|
|
|
}
|
2009-10-05 13:50:44 +00:00
|
|
|
|
2009-10-07 18:56:03 +00:00
|
|
|
if(is_first && is_closed) {
|
2009-10-05 13:50:44 +00:00
|
|
|
var last_type = setSeg(last_index,1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// move the point grip
|
2009-10-18 20:46:32 +00:00
|
|
|
var grip = document.getElementById("pathpointgrip_" + current_path_pt_drag);
|
2009-10-05 13:50:44 +00:00
|
|
|
if (grip) {
|
|
|
|
grip.setAttribute("cx", mouse_x);
|
|
|
|
grip.setAttribute("cy", mouse_y);
|
2009-10-07 18:56:03 +00:00
|
|
|
if(is_closed && is_first) {
|
2009-10-18 20:46:32 +00:00
|
|
|
var grip = document.getElementById("pathpointgrip_" + last_index);
|
2009-10-05 13:50:44 +00:00
|
|
|
grip.setAttribute("cx", mouse_x);
|
|
|
|
grip.setAttribute("cy", mouse_y);
|
|
|
|
}
|
2009-10-22 19:36:04 +00:00
|
|
|
call("changed", [grip]);
|
2009-10-05 13:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(is_first) cur_type = last_type;
|
|
|
|
|
2009-10-09 19:47:32 +00:00
|
|
|
if(cur_type != 4) {
|
2009-10-05 13:50:44 +00:00
|
|
|
var num = is_first?last_index:index;
|
|
|
|
var id2 = (num-1)+'c2';
|
|
|
|
var line = document.getElementById("ctrlLine_"+id2);
|
|
|
|
if(line) {
|
2009-11-10 16:43:02 +00:00
|
|
|
// Don't do if first point on open path
|
|
|
|
if(!(!is_closed && current_path_pt_drag == 0)) {
|
|
|
|
var x2 = line.getAttribute('x2') - 0 + x_diff*current_zoom;
|
|
|
|
var y2 = line.getAttribute('y2') - 0 + y_diff*current_zoom;
|
|
|
|
addControlPointGrip(x2,y2, mouse_x,mouse_y, id2, true);
|
|
|
|
}
|
2009-10-05 13:50:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-09 19:47:32 +00:00
|
|
|
if(next_type != 4) {
|
2009-10-18 20:46:32 +00:00
|
|
|
var id1 = (current_path_pt_drag)+'c1';
|
2009-10-05 13:50:44 +00:00
|
|
|
var line = document.getElementById("ctrlLine_"+id1);
|
|
|
|
if(line) {
|
|
|
|
var x2 = line.getAttribute('x2') - 0 + x_diff*current_zoom;
|
|
|
|
var y2 = line.getAttribute('y2') - 0 + y_diff*current_zoom;
|
|
|
|
addControlPointGrip(x2,y2, mouse_x,mouse_y, id1, true);
|
|
|
|
}
|
|
|
|
}
|
2009-10-13 14:52:51 +00:00
|
|
|
updateSegLine();
|
|
|
|
if(next_type != 4) {
|
|
|
|
updateSegLine(true);
|
|
|
|
}
|
2009-10-05 13:50:44 +00:00
|
|
|
}
|
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var getPathPoint = function(index, raw_val) {
|
|
|
|
var len = current_path_pts.length;
|
2009-09-29 15:58:46 +00:00
|
|
|
var pt_num = len/2;
|
|
|
|
if(index < 0) {
|
|
|
|
index += pt_num;
|
|
|
|
} else if(index >= pt_num) {
|
|
|
|
index -= pt_num;
|
|
|
|
}
|
2009-09-29 18:11:52 +00:00
|
|
|
var z = raw_val?1:current_zoom;
|
2009-10-18 20:46:32 +00:00
|
|
|
return [current_path_pts[index*2] / z, current_path_pts[index*2 + 1] / z];
|
2009-09-29 15:58:46 +00:00
|
|
|
}
|
|
|
|
|
2009-10-02 15:05:31 +00:00
|
|
|
// This replaces the segment at the given index. Type is given as number.
|
2009-10-18 20:46:32 +00:00
|
|
|
var replacePathSeg = function(type, index, pts, path) {
|
|
|
|
if(!path) path = current_path;
|
2009-10-13 14:52:51 +00:00
|
|
|
var func = 'createSVGPathSeg' + pathFuncs[type];
|
2009-10-18 20:46:32 +00:00
|
|
|
var seg = path[func].apply(path, pts);
|
|
|
|
path.pathSegList.replaceItem(seg, index);
|
2009-10-02 15:05:31 +00:00
|
|
|
}
|
|
|
|
|
2009-09-29 18:11:52 +00:00
|
|
|
var addControlPointGrip = function(x, y, source_x, source_y, id, raw_val) {
|
|
|
|
if(!raw_val) {
|
|
|
|
x *= current_zoom; y *= current_zoom;
|
|
|
|
source_x *= current_zoom; source_y *= current_zoom;
|
|
|
|
}
|
|
|
|
|
2009-09-25 17:59:32 +00:00
|
|
|
// create the container of all the control point grips
|
|
|
|
var ctrlPointGripContainer = document.getElementById("ctrlpointgrip_container");
|
|
|
|
if (!ctrlPointGripContainer) {
|
|
|
|
var parent = document.getElementById("selectorParentGroup");
|
|
|
|
ctrlPointGripContainer = parent.appendChild(document.createElementNS(svgns, "g"));
|
|
|
|
ctrlPointGripContainer.id = "ctrlpointgrip_container";
|
|
|
|
}
|
2009-09-25 20:03:20 +00:00
|
|
|
ctrlPointGripContainer.setAttribute("display", "inline");
|
2009-09-25 17:59:32 +00:00
|
|
|
|
|
|
|
var ctrlLine = document.getElementById("ctrlLine_"+id);
|
|
|
|
if (!ctrlLine) {
|
|
|
|
ctrlLine = document.createElementNS(svgns, "line");
|
|
|
|
assignAttributes(ctrlLine, {
|
|
|
|
'id': "ctrlLine_"+id,
|
|
|
|
'stroke': "#555",
|
|
|
|
'stroke-width': 1,
|
|
|
|
"style": "pointer-events:none"
|
|
|
|
});
|
|
|
|
ctrlLine = ctrlPointGripContainer.appendChild(ctrlLine);
|
|
|
|
}
|
|
|
|
|
|
|
|
assignAttributes(ctrlLine, {
|
|
|
|
'x1': source_x,
|
|
|
|
'y1': source_y,
|
|
|
|
'x2': x,
|
|
|
|
'y2': y,
|
|
|
|
'display': "inline"
|
|
|
|
});
|
|
|
|
|
|
|
|
var pointGrip = document.getElementById("ctrlpointgrip_"+id);
|
|
|
|
// create it
|
|
|
|
if (!pointGrip) {
|
|
|
|
pointGrip = document.createElementNS(svgns, "circle");
|
|
|
|
assignAttributes(pointGrip, {
|
|
|
|
'id': "ctrlpointgrip_" + id,
|
|
|
|
'display': "none",
|
2009-09-29 18:11:52 +00:00
|
|
|
'r': 4,
|
2009-10-23 17:27:37 +00:00
|
|
|
'fill': "#0FF",
|
2009-09-25 17:59:32 +00:00
|
|
|
'stroke': "#55F",
|
|
|
|
'stroke-width': 1,
|
|
|
|
'cursor': 'move',
|
2009-10-16 13:17:23 +00:00
|
|
|
'style': 'pointer-events:all',
|
|
|
|
'xlink:title': 'Drag control point to adjust curve properties'
|
2009-09-25 17:59:32 +00:00
|
|
|
});
|
|
|
|
pointGrip = ctrlPointGripContainer.appendChild(pointGrip);
|
|
|
|
}
|
|
|
|
|
|
|
|
assignAttributes(pointGrip, {
|
|
|
|
'cx': x,
|
|
|
|
'cy': y,
|
|
|
|
'display': "inline"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var removeControlPointGrips = function(index) {
|
|
|
|
for(var i=1; i <= 2; i++) {
|
2009-10-05 19:09:39 +00:00
|
|
|
$("#ctrlpointgrip_" + index + "c" + i + ",#ctrlLine_" + index + "c" + i).attr("display", "none");
|
2009-09-25 17:59:32 +00:00
|
|
|
}
|
|
|
|
}
|
2009-08-08 03:36:01 +00:00
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
// - in create mode, the element's opacity is set properly, we create an InsertElementCommand
|
|
|
|
// and store it on the Undo stack
|
|
|
|
// - in move/resize mode, the element's attributes which were affected by the move/resize are
|
|
|
|
// identified, a ChangeElementCommand is created and stored on the stack for those attrs
|
2009-07-08 00:03:08 +00:00
|
|
|
// this is done in when we recalculate the selected dimensions()
|
2009-06-08 02:17:07 +00:00
|
|
|
var mouseUp = function(evt)
|
2009-06-01 21:24:30 +00:00
|
|
|
{
|
2009-09-06 23:25:12 +00:00
|
|
|
var tempJustSelected = justSelected;
|
|
|
|
justSelected = null;
|
2009-06-02 16:40:33 +00:00
|
|
|
if (!started) return;
|
|
|
|
|
2009-10-22 03:18:54 +00:00
|
|
|
var pt = transformPoint( evt.pageX, evt.pageY, root_sctm );
|
2009-10-22 03:07:15 +00:00
|
|
|
var mouse_x = pt.x;
|
|
|
|
var mouse_y = pt.y;
|
2009-09-11 13:32:56 +00:00
|
|
|
var x = mouse_x / current_zoom;
|
|
|
|
var y = mouse_y / current_zoom;
|
2009-11-02 20:09:02 +00:00
|
|
|
|
2009-06-02 16:40:33 +00:00
|
|
|
started = false;
|
2009-06-05 12:33:02 +00:00
|
|
|
var element = svgdoc.getElementById(getId());
|
2009-06-06 17:53:38 +00:00
|
|
|
var keep = false;
|
2009-06-02 16:40:33 +00:00
|
|
|
switch (current_mode)
|
2009-06-01 21:24:30 +00:00
|
|
|
{
|
2009-07-01 19:28:08 +00:00
|
|
|
// intentionally fall-through to select here
|
2009-06-23 05:17:53 +00:00
|
|
|
case "resize":
|
2009-07-01 19:28:08 +00:00
|
|
|
case "multiselect":
|
|
|
|
if (rubberBox != null) {
|
|
|
|
rubberBox.setAttribute("display", "none");
|
2009-08-26 19:59:52 +00:00
|
|
|
curBBoxes = [];
|
2009-07-01 19:28:08 +00:00
|
|
|
}
|
2009-06-23 05:17:53 +00:00
|
|
|
current_mode = "select";
|
2009-06-02 16:40:33 +00:00
|
|
|
case "select":
|
2009-07-01 19:28:08 +00:00
|
|
|
if (selectedElements[0] != null) {
|
|
|
|
// if we only have one selected element
|
|
|
|
if (selectedElements[1] == null) {
|
|
|
|
// set our current stroke/fill properties to the element's
|
|
|
|
var selected = selectedElements[0];
|
2009-11-19 15:09:21 +00:00
|
|
|
if (selected.tagName != "g" && selected.tagName != "image") {
|
2009-09-13 15:50:43 +00:00
|
|
|
cur_shape.fill = selected.getAttribute("fill");
|
|
|
|
cur_shape.fill_opacity = selected.getAttribute("fill-opacity");
|
|
|
|
cur_shape.stroke = selected.getAttribute("stroke");
|
|
|
|
cur_shape.stroke_opacity = selected.getAttribute("stroke-opacity");
|
|
|
|
cur_shape.stroke_width = selected.getAttribute("stroke-width");
|
|
|
|
cur_shape.stroke_style = selected.getAttribute("stroke-dasharray");
|
|
|
|
}
|
2009-07-01 19:28:08 +00:00
|
|
|
if (selected.tagName == "text") {
|
2009-08-31 14:54:59 +00:00
|
|
|
cur_text.font_size = selected.getAttribute("font-size");
|
|
|
|
cur_text.font_family = selected.getAttribute("font-family");
|
2009-07-01 19:28:08 +00:00
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-27 14:44:23 +00:00
|
|
|
selectorManager.requestSelector(selected).showGrips(true);
|
2009-07-01 19:28:08 +00:00
|
|
|
}
|
2009-11-09 20:06:07 +00:00
|
|
|
// always recalculate dimensions to strip off stray identity transforms
|
|
|
|
recalculateAllSelectedDimensions();
|
2009-08-08 14:03:14 +00:00
|
|
|
// if it was being dragged/resized
|
|
|
|
if (x != start_x || y != start_y) {
|
|
|
|
var len = selectedElements.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
if (selectedElements[i] == null) break;
|
2009-11-02 17:11:37 +00:00
|
|
|
if(selectedElements[i].tagName != 'g') {
|
|
|
|
// Not needed for groups (incorrectly resizes elems), possibly not needed at all?
|
2009-11-04 15:30:10 +00:00
|
|
|
selectorManager.requestSelector(selectedElements[i]).resize();//selectedBBoxes[i]); // TODO: remove box arg
|
2009-11-02 17:11:37 +00:00
|
|
|
}
|
2009-08-08 14:03:14 +00:00
|
|
|
}
|
|
|
|
}
|
2009-10-17 20:29:56 +00:00
|
|
|
// no change in position/size, so maybe we should move to pathedit
|
2009-08-08 14:03:14 +00:00
|
|
|
else {
|
2009-09-06 23:25:12 +00:00
|
|
|
var t = evt.target;
|
2009-08-08 14:03:14 +00:00
|
|
|
if (selectedElements[0].nodeName == "path" && selectedElements[1] == null) {
|
2009-10-18 20:46:32 +00:00
|
|
|
if (current_path == t) {
|
2009-10-17 20:29:56 +00:00
|
|
|
current_mode = "pathedit";
|
2009-08-08 14:03:14 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
// recalculate current_path_pts
|
|
|
|
recalcPathPoints();
|
2009-08-08 14:03:14 +00:00
|
|
|
canvas.clearSelection();
|
2009-10-18 20:46:32 +00:00
|
|
|
// save the path's bbox
|
|
|
|
selectedBBoxes[0] = canvas.getBBox(current_path);
|
|
|
|
addAllPointGripsToPath();
|
2009-10-13 14:52:51 +00:00
|
|
|
canvas.addNodeToSelection(0);
|
2009-10-17 20:29:56 +00:00
|
|
|
} // going into pathedit mode
|
2009-08-08 14:03:14 +00:00
|
|
|
else {
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path = t;
|
2009-08-08 14:03:14 +00:00
|
|
|
}
|
2009-09-06 23:25:12 +00:00
|
|
|
} // if it was a path
|
|
|
|
// else, if it was selected and this is a shift-click, remove it from selection
|
|
|
|
else if (evt.shiftKey && tempJustSelected != t) {
|
|
|
|
canvas.removeFromSelection([t]);
|
|
|
|
}
|
|
|
|
} // no change in mouse position
|
2009-08-17 06:56:55 +00:00
|
|
|
}
|
2009-07-13 04:12:25 +00:00
|
|
|
// we return immediately from select so that the obj_num is not incremented
|
|
|
|
return;
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
2009-09-16 15:45:50 +00:00
|
|
|
case "zoom":
|
2009-09-16 17:18:11 +00:00
|
|
|
if (rubberBox != null) {
|
|
|
|
rubberBox.setAttribute("display", "none");
|
|
|
|
}
|
2009-10-28 18:04:38 +00:00
|
|
|
var factor = evt.shiftKey?.5:2;
|
2009-09-16 15:45:50 +00:00
|
|
|
call("zoomed", {
|
|
|
|
'x': Math.min(start_x,x),
|
|
|
|
'y': Math.min(start_y,y),
|
|
|
|
'width': Math.abs(x-start_x),
|
2009-10-28 18:04:38 +00:00
|
|
|
'height': Math.abs(y-start_y),
|
|
|
|
'factor': factor
|
2009-09-16 15:45:50 +00:00
|
|
|
});
|
|
|
|
return;
|
2009-10-18 20:04:26 +00:00
|
|
|
case "fhpath":
|
2009-09-29 13:50:47 +00:00
|
|
|
// Check that the path contains at least 2 points; a degenerate one-point path
|
|
|
|
// causes problems.
|
|
|
|
// Webkit ignores how we set the points attribute with commas and uses space
|
|
|
|
// to separate all coordinates, see https://bugs.webkit.org/show_bug.cgi?id=29870
|
|
|
|
var coords = element.getAttribute('points');
|
|
|
|
var commaIndex = coords.indexOf(',');
|
|
|
|
if (commaIndex >= 0) {
|
|
|
|
keep = coords.indexOf(',', commaIndex+1) >= 0;
|
|
|
|
} else {
|
|
|
|
keep = coords.indexOf(' ', coords.indexOf(' ')+1) >= 0;
|
|
|
|
}
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "line":
|
2009-10-16 20:07:15 +00:00
|
|
|
keep = (element.x1.baseVal.value != element.x2.baseVal.value ||
|
|
|
|
element.y1.baseVal.value != element.y2.baseVal.value);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "square":
|
|
|
|
case "rect":
|
2009-10-16 20:07:15 +00:00
|
|
|
keep = (element.width.baseVal.value && element.height.baseVal.value);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
2009-09-04 22:22:40 +00:00
|
|
|
case "image":
|
2009-10-16 20:07:15 +00:00
|
|
|
keep = (element.width.baseVal.value && element.height.baseVal.value);
|
2009-09-04 22:22:40 +00:00
|
|
|
break;
|
2009-06-02 16:40:33 +00:00
|
|
|
case "circle":
|
2009-10-16 20:07:15 +00:00
|
|
|
keep = (element.r.baseVal.value);
|
2009-06-03 15:00:48 +00:00
|
|
|
break;
|
2009-06-02 16:40:33 +00:00
|
|
|
case "ellipse":
|
2009-10-16 20:07:15 +00:00
|
|
|
keep = (element.rx.baseVal.value && element.ry.baseVal.value);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "fhellipse":
|
2009-06-03 08:41:25 +00:00
|
|
|
if ((freehand_max_x - freehand_min_x) > 0 &&
|
|
|
|
(freehand_max_y - freehand_min_y) > 0) {
|
2009-06-28 04:10:03 +00:00
|
|
|
element = addSvgElementFromJson({
|
2009-06-03 08:41:25 +00:00
|
|
|
"element": "ellipse",
|
|
|
|
"attr": {
|
|
|
|
"cx": (freehand_min_x + freehand_max_x) / 2,
|
|
|
|
"cy": (freehand_min_y + freehand_max_y) / 2,
|
|
|
|
"rx": (freehand_max_x - freehand_min_x) / 2,
|
|
|
|
"ry": (freehand_max_y - freehand_min_y) / 2,
|
2009-06-05 12:33:02 +00:00
|
|
|
"id": getId(),
|
2009-08-31 14:54:59 +00:00
|
|
|
"fill": cur_shape.fill,
|
|
|
|
"stroke": cur_shape.stroke,
|
|
|
|
"stroke-width": cur_shape.stroke_width,
|
|
|
|
"stroke-dasharray": cur_shape.stroke_style,
|
|
|
|
"opacity": cur_shape.opacity,
|
|
|
|
"stroke-opacity": cur_shape.stroke_opacity,
|
2009-09-21 22:16:44 +00:00
|
|
|
"fill-opacity": cur_shape.fill_opacity,
|
2009-10-11 04:32:21 +00:00
|
|
|
"style": "pointer-events:inherit"
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
2009-06-28 04:10:03 +00:00
|
|
|
});
|
2009-07-01 19:54:51 +00:00
|
|
|
call("changed",[element]);
|
2009-06-16 02:39:12 +00:00
|
|
|
keep = true;
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "fhrect":
|
2009-06-03 08:41:25 +00:00
|
|
|
if ((freehand_max_x - freehand_min_x) > 0 &&
|
|
|
|
(freehand_max_y - freehand_min_y) > 0) {
|
2009-06-28 04:10:03 +00:00
|
|
|
element = addSvgElementFromJson({
|
2009-06-03 08:41:25 +00:00
|
|
|
"element": "rect",
|
|
|
|
"attr": {
|
|
|
|
"x": freehand_min_x,
|
|
|
|
"y": freehand_min_y,
|
|
|
|
"width": (freehand_max_x - freehand_min_x),
|
|
|
|
"height": (freehand_max_y - freehand_min_y),
|
2009-06-05 12:33:02 +00:00
|
|
|
"id": getId(),
|
2009-08-31 14:54:59 +00:00
|
|
|
"fill": cur_shape.fill,
|
|
|
|
"stroke": cur_shape.stroke,
|
|
|
|
"stroke-width": cur_shape.stroke_width,
|
|
|
|
"stroke-dasharray": cur_shape.stroke_style,
|
|
|
|
"opacity": cur_shape.opacity,
|
|
|
|
"stroke-opacity": cur_shape.stroke_opacity,
|
2009-09-21 22:16:44 +00:00
|
|
|
"fill-opacity": cur_shape.fill_opacity,
|
2009-10-11 04:32:21 +00:00
|
|
|
"style": "pointer-events:inherit"
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
2009-06-28 04:10:03 +00:00
|
|
|
});
|
2009-07-01 19:54:51 +00:00
|
|
|
call("changed",[element]);
|
2009-06-16 02:39:12 +00:00
|
|
|
keep = true;
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
2009-06-10 03:12:19 +00:00
|
|
|
case "text":
|
|
|
|
keep = true;
|
2009-07-01 19:28:08 +00:00
|
|
|
canvas.clearSelection();
|
2009-06-10 03:12:19 +00:00
|
|
|
break;
|
2009-10-18 20:46:32 +00:00
|
|
|
case "path":
|
2009-08-07 05:44:55 +00:00
|
|
|
// set element to null here so that it is not removed nor finalized
|
|
|
|
element = null;
|
|
|
|
// continue to be set to true so that mouseMove happens
|
|
|
|
started = true;
|
2009-10-18 20:46:32 +00:00
|
|
|
var stretchy = document.getElementById("path_stretch_line");
|
2009-08-07 05:44:55 +00:00
|
|
|
if (!stretchy) {
|
|
|
|
stretchy = document.createElementNS(svgns, "line");
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(stretchy, {
|
2009-10-18 20:46:32 +00:00
|
|
|
'id': "path_stretch_line",
|
2009-08-24 20:00:48 +00:00
|
|
|
'stroke': "blue",
|
|
|
|
'stroke-width': "0.5"
|
|
|
|
});
|
2009-08-07 05:44:55 +00:00
|
|
|
stretchy = document.getElementById("selectorParentGroup").appendChild(stretchy);
|
|
|
|
}
|
|
|
|
stretchy.setAttribute("display", "inline");
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-07 05:44:55 +00:00
|
|
|
// if pts array is empty, create path element with M at current point
|
2009-10-18 20:46:32 +00:00
|
|
|
if (current_path_pts.length == 0) {
|
|
|
|
current_path_pts.push(x);
|
|
|
|
current_path_pts.push(y);
|
2009-08-07 05:44:55 +00:00
|
|
|
d_attr = "M" + x + "," + y + " ";
|
|
|
|
addSvgElementFromJson({
|
|
|
|
"element": "path",
|
|
|
|
"attr": {
|
|
|
|
"d": d_attr,
|
|
|
|
"id": getNextId(),
|
2009-08-31 14:54:59 +00:00
|
|
|
"fill": cur_shape.fill,
|
|
|
|
"fill-opacity": cur_shape.fill_opacity,
|
|
|
|
"stroke": cur_shape.stroke,
|
|
|
|
"stroke-width": cur_shape.stroke_width,
|
|
|
|
"stroke-dasharray": cur_shape.stroke_style,
|
|
|
|
"stroke-opacity": cur_shape.stroke_opacity,
|
2009-09-21 22:16:44 +00:00
|
|
|
"opacity": cur_shape.opacity / 2,
|
2009-10-11 04:32:21 +00:00
|
|
|
"style": "pointer-events:inherit"
|
2009-08-07 05:44:55 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
// set stretchy line to first point
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(stretchy, {
|
2009-09-11 13:32:56 +00:00
|
|
|
'x1': mouse_x,
|
|
|
|
'y1': mouse_y,
|
|
|
|
'x2': mouse_x,
|
|
|
|
'y2': mouse_y
|
2009-08-24 20:00:48 +00:00
|
|
|
});
|
2009-10-18 20:46:32 +00:00
|
|
|
addPointGripToPath(mouse_x,mouse_y,0);
|
2009-08-07 05:44:55 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// determine if we clicked on an existing point
|
2009-10-18 20:46:32 +00:00
|
|
|
var i = current_path_pts.length;
|
2009-09-15 20:50:38 +00:00
|
|
|
var FUZZ = 6/current_zoom;
|
2009-08-07 05:44:55 +00:00
|
|
|
var clickOnPoint = false;
|
|
|
|
while(i) {
|
|
|
|
i -= 2;
|
2009-10-18 20:46:32 +00:00
|
|
|
var px = current_path_pts[i], py = current_path_pts[i+1];
|
2009-08-07 05:44:55 +00:00
|
|
|
// found a matching point
|
|
|
|
if ( x >= (px-FUZZ) && x <= (px+FUZZ) && y >= (py-FUZZ) && y <= (py+FUZZ) ) {
|
|
|
|
clickOnPoint = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-09-10 20:34:20 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
// get path element that we are in the process of creating
|
|
|
|
var path = svgdoc.getElementById(getId());
|
|
|
|
var len = current_path_pts.length;
|
|
|
|
// if we clicked on an existing point, then we are done this path, commit it
|
2009-08-07 05:44:55 +00:00
|
|
|
// (i,i+1) are the x,y that were clicked on
|
|
|
|
if (clickOnPoint) {
|
|
|
|
// if clicked on any other point but the first OR
|
|
|
|
// the first point was clicked on and there are less than 3 points
|
2009-10-18 20:46:32 +00:00
|
|
|
// then leave the path open
|
|
|
|
// otherwise, close the path
|
2009-10-02 15:05:31 +00:00
|
|
|
if (i == 0 && len >= 6) {
|
|
|
|
// Create end segment
|
2009-10-18 20:46:32 +00:00
|
|
|
var abs_x = current_path_pts[0];
|
|
|
|
var abs_y = current_path_pts[1];
|
2009-10-09 19:47:32 +00:00
|
|
|
d_attr += ['L',abs_x,',',abs_y,'z'].join('');
|
2009-10-18 20:46:32 +00:00
|
|
|
path.setAttribute("d", d_attr);
|
2009-10-02 15:05:31 +00:00
|
|
|
} else if(len < 3) {
|
2009-09-24 16:05:23 +00:00
|
|
|
keep = false;
|
|
|
|
break;
|
2009-08-07 05:44:55 +00:00
|
|
|
}
|
2009-10-18 20:46:32 +00:00
|
|
|
removeAllPointGripsFromPath();
|
|
|
|
// this will signal to commit the path
|
|
|
|
element = path;
|
|
|
|
current_path_pts = [];
|
2009-08-07 05:44:55 +00:00
|
|
|
started = false;
|
|
|
|
}
|
|
|
|
// else, create a new point, append to pts array, update path element
|
|
|
|
else {
|
2009-10-18 20:46:32 +00:00
|
|
|
var lastx = current_path_pts[len-2], lasty = current_path_pts[len-1];
|
|
|
|
// we store absolute values in our path points array for easy checking above
|
|
|
|
current_path_pts.push(x);
|
|
|
|
current_path_pts.push(y);
|
2009-10-09 19:47:32 +00:00
|
|
|
d_attr += "L" + round(x) + "," + round(y) + " ";
|
2009-10-18 20:46:32 +00:00
|
|
|
path.setAttribute("d", d_attr);
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-07 05:44:55 +00:00
|
|
|
// set stretchy line to latest point
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(stretchy, {
|
2009-09-11 13:32:56 +00:00
|
|
|
'x1': mouse_x,
|
|
|
|
'y1': mouse_y,
|
|
|
|
'x2': mouse_x,
|
|
|
|
'y2': mouse_y
|
2009-08-24 20:00:48 +00:00
|
|
|
});
|
2009-10-18 20:46:32 +00:00
|
|
|
addPointGripToPath(mouse_x,mouse_y,(current_path_pts.length/2 - 1));
|
2009-08-07 05:44:55 +00:00
|
|
|
}
|
|
|
|
keep = true;
|
|
|
|
}
|
|
|
|
break;
|
2009-10-17 20:29:56 +00:00
|
|
|
case "pathedit":
|
2009-08-08 14:03:14 +00:00
|
|
|
keep = true;
|
|
|
|
element = null;
|
2009-10-18 20:46:32 +00:00
|
|
|
// if we were dragging a path point, stop it now
|
|
|
|
if (current_path_pt_drag != -1) {
|
|
|
|
current_path_pt_drag = -1;
|
2009-08-28 23:12:46 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var batchCmd = new BatchCommand("Edit Path");
|
2009-08-29 22:10:52 +00:00
|
|
|
// the attribute changes we want to undo
|
|
|
|
var oldvalues = {};
|
2009-10-18 20:46:32 +00:00
|
|
|
oldvalues["d"] = current_path_oldd;
|
2009-08-29 22:10:52 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
// If the path was rotated, we must now pay the piper:
|
|
|
|
// Every path point must be rotated into the rotated coordinate system of
|
2009-08-28 23:12:46 +00:00
|
|
|
// its old center, then determine the new center, then rotate it back
|
2009-10-18 20:46:32 +00:00
|
|
|
// This is because we want the path to remember its rotation
|
|
|
|
var angle = canvas.getRotationAngle(current_path) * Math.PI / 180.0;
|
2009-10-12 14:29:21 +00:00
|
|
|
|
2009-08-28 23:12:46 +00:00
|
|
|
if (angle) {
|
2009-10-18 20:46:32 +00:00
|
|
|
var box = canvas.getBBox(current_path);
|
2009-08-28 23:12:46 +00:00
|
|
|
var oldbox = selectedBBoxes[0];
|
2009-10-12 14:29:21 +00:00
|
|
|
var oldcx = oldbox.x + oldbox.width/2,
|
|
|
|
oldcy = oldbox.y + oldbox.height/2,
|
|
|
|
newcx = box.x + box.width/2,
|
|
|
|
newcy = box.y + box.height/2;
|
2009-08-28 23:12:46 +00:00
|
|
|
|
2009-08-29 18:38:34 +00:00
|
|
|
// un-rotate the new center to the proper position
|
|
|
|
var dx = newcx - oldcx,
|
|
|
|
dy = newcy - oldcy;
|
|
|
|
var r = Math.sqrt(dx*dx + dy*dy);
|
|
|
|
var theta = Math.atan2(dy,dx) + angle;
|
2009-10-12 14:29:21 +00:00
|
|
|
newcx = r * Math.cos(theta) + oldcx;
|
|
|
|
newcy = r * Math.sin(theta) + oldcy;
|
2009-08-29 18:38:34 +00:00
|
|
|
|
2009-10-12 14:29:21 +00:00
|
|
|
var getRotVals = function(x, y) {
|
|
|
|
dx = x - oldcx;
|
|
|
|
dy = y - oldcy;
|
2009-08-28 23:12:46 +00:00
|
|
|
|
|
|
|
// rotate the point around the old center
|
2009-08-29 18:38:34 +00:00
|
|
|
r = Math.sqrt(dx*dx + dy*dy);
|
|
|
|
theta = Math.atan2(dy,dx) + angle;
|
2009-10-12 14:29:21 +00:00
|
|
|
dx = r * Math.cos(theta) + oldcx;
|
|
|
|
dy = r * Math.sin(theta) + oldcy;
|
2009-08-28 23:12:46 +00:00
|
|
|
|
|
|
|
// dx,dy should now hold the actual coordinates of each
|
|
|
|
// point after being rotated
|
2009-08-29 18:38:34 +00:00
|
|
|
|
2009-08-28 23:12:46 +00:00
|
|
|
// now we want to rotate them around the new center in the reverse direction
|
|
|
|
dx -= newcx;
|
|
|
|
dy -= newcy;
|
|
|
|
|
|
|
|
r = Math.sqrt(dx*dx + dy*dy);
|
|
|
|
theta = Math.atan2(dy,dx) - angle;
|
|
|
|
|
2009-10-12 14:29:21 +00:00
|
|
|
return {'x':(r * Math.cos(theta) + newcx)/1,
|
|
|
|
'y':(r * Math.sin(theta) + newcy)/1};
|
2009-08-28 23:12:46 +00:00
|
|
|
}
|
2009-10-12 14:29:21 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var list = current_path.pathSegList;
|
2009-10-12 14:29:21 +00:00
|
|
|
var i = list.numberOfItems;
|
|
|
|
while (i) {
|
|
|
|
i -= 1;
|
|
|
|
var seg = list.getItem(i);
|
|
|
|
var type = seg.pathSegType;
|
|
|
|
if(type == 1) continue;
|
|
|
|
|
|
|
|
var rvals = getRotVals(seg.x,seg.y);
|
|
|
|
var points = [rvals.x, rvals.y];
|
|
|
|
if(seg.x1 != null && seg.x2 != null) {
|
|
|
|
c_vals1 = getRotVals(seg.x1, seg.y1);
|
|
|
|
c_vals2 = getRotVals(seg.x2, seg.y2);
|
|
|
|
points.splice(points.length, 0, c_vals1.x , c_vals1.y, c_vals2.x, c_vals2.y);
|
|
|
|
}
|
|
|
|
replacePathSeg(type, i, points);
|
|
|
|
} // loop for each point
|
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
box = canvas.getBBox(current_path);
|
2009-08-29 18:38:34 +00:00
|
|
|
selectedBBoxes[0].x = box.x; selectedBBoxes[0].y = box.y;
|
|
|
|
selectedBBoxes[0].width = box.width; selectedBBoxes[0].height = box.height;
|
2009-08-28 23:12:46 +00:00
|
|
|
|
|
|
|
// now we must set the new transform to be rotated around the new center
|
2009-08-29 18:38:34 +00:00
|
|
|
var rotate = "rotate(" + (angle * 180.0 / Math.PI) + " " + newcx + "," + newcy + ")";
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path.setAttribute("transform", rotate);
|
2009-10-12 14:29:21 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
if(document.getElementById("pathpointgrip_container")) {
|
2009-09-12 04:49:50 +00:00
|
|
|
var pcx = newcx * current_zoom,
|
|
|
|
pcy = newcy * current_zoom;
|
|
|
|
var xform = ["rotate(", (angle*180.0/Math.PI), " ", pcx, ",", pcy, ")"].join("");
|
2009-10-09 15:29:40 +00:00
|
|
|
setPointContainerTransform(xform);
|
2009-08-29 18:38:34 +00:00
|
|
|
}
|
2009-10-12 14:29:21 +00:00
|
|
|
resetPointGrips();
|
2009-10-13 14:52:51 +00:00
|
|
|
updateSegLine(true);
|
2009-10-12 14:29:21 +00:00
|
|
|
|
2009-08-29 18:38:34 +00:00
|
|
|
} // if rotated
|
2009-08-29 22:10:52 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(current_path, oldvalues, "path points"));
|
2009-08-29 22:10:52 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
2009-10-18 20:46:32 +00:00
|
|
|
call("changed", [current_path]);
|
2009-08-29 22:10:52 +00:00
|
|
|
|
2009-10-02 15:05:31 +00:00
|
|
|
// If connected, last point should equal first
|
2009-10-18 20:46:32 +00:00
|
|
|
if(current_path.getAttribute('d').toLowerCase().indexOf('z') != -1) {
|
|
|
|
current_path_pts[current_path_pts.length-2] = getPathPoint(0,true)[0];
|
|
|
|
current_path_pts[current_path_pts.length-1] = getPathPoint(0,true)[1];
|
2009-10-07 18:56:03 +00:00
|
|
|
}
|
2009-10-13 14:52:51 +00:00
|
|
|
updateSegLine();
|
2009-10-02 15:05:31 +00:00
|
|
|
|
2009-08-29 22:10:52 +00:00
|
|
|
// make these changes undo-able
|
2009-10-18 20:46:32 +00:00
|
|
|
} // if (current_path_pt_drag != -1)
|
2009-09-25 17:59:32 +00:00
|
|
|
else if(current_ctrl_pt_drag != -1) {
|
|
|
|
current_ctrl_pt_drag = -1;
|
2009-10-18 20:46:32 +00:00
|
|
|
var batchCmd = new BatchCommand("Edit Path control points");
|
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(current_path, {d:current_path_oldd}));
|
2009-09-29 19:21:10 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
2009-10-18 20:46:32 +00:00
|
|
|
call("changed", [current_path]);
|
2009-10-02 15:05:31 +00:00
|
|
|
} // else, move back to select mode
|
2009-08-08 22:17:59 +00:00
|
|
|
else {
|
|
|
|
current_mode = "select";
|
2009-10-18 20:46:32 +00:00
|
|
|
removeAllPointGripsFromPath();
|
2009-08-21 12:21:43 +00:00
|
|
|
canvas.clearSelection();
|
2009-08-08 22:17:59 +00:00
|
|
|
canvas.addToSelection([evt.target]);
|
|
|
|
}
|
2009-08-08 14:03:14 +00:00
|
|
|
break;
|
2009-08-26 19:45:28 +00:00
|
|
|
case "rotate":
|
|
|
|
keep = true;
|
|
|
|
element = null;
|
|
|
|
current_mode = "select";
|
2009-09-04 05:10:48 +00:00
|
|
|
var batchCmd = canvas.finishUndoableChange();
|
|
|
|
if (!batchCmd.isEmpty()) {
|
|
|
|
addCommandToHistory(batchCmd);
|
|
|
|
}
|
2009-11-02 20:09:02 +00:00
|
|
|
// perform recalculation to weed out any stray identity transforms that might get stuck
|
|
|
|
recalculateAllSelectedDimensions();
|
2009-08-26 19:45:28 +00:00
|
|
|
break;
|
2009-08-07 05:44:55 +00:00
|
|
|
default:
|
|
|
|
console.log("Unknown mode in mouseup: " + current_mode);
|
|
|
|
break;
|
2009-06-02 16:40:33 +00:00
|
|
|
}
|
2009-06-18 05:08:47 +00:00
|
|
|
if (!keep && element != null) {
|
2009-06-10 03:12:19 +00:00
|
|
|
element.parentNode.removeChild(element);
|
|
|
|
element = null;
|
2009-10-16 20:07:15 +00:00
|
|
|
|
|
|
|
var t = evt.target;
|
2009-11-02 20:09:02 +00:00
|
|
|
|
2009-10-16 20:07:15 +00:00
|
|
|
// if this element is in a group, go up until we reach the top-level group
|
|
|
|
// just below the layer groups
|
|
|
|
// TODO: once we implement links, we also would have to check for <a> elements
|
|
|
|
while (t.parentNode.parentNode.tagName == "g") {
|
|
|
|
t = t.parentNode;
|
|
|
|
}
|
|
|
|
// if we are not in the middle of creating a path, and we've clicked on some shape,
|
|
|
|
// then go to Select mode.
|
|
|
|
// WebKit returns <div> when the canvas is clicked, Firefox/Opera return <svg>
|
2009-10-18 20:46:32 +00:00
|
|
|
if ( (current_mode != "path" || current_path_pts.length == 0) &&
|
2009-10-16 20:07:15 +00:00
|
|
|
t.parentNode.id != "selectorParentGroup" &&
|
|
|
|
t.id != "svgcanvas" && t.id != "svgroot")
|
|
|
|
{
|
|
|
|
// switch into "select" mode if we've clicked on an element
|
|
|
|
canvas.addToSelection([t], true);
|
|
|
|
canvas.setMode("select");
|
|
|
|
}
|
|
|
|
|
2009-06-10 03:12:19 +00:00
|
|
|
} else if (element != null) {
|
2009-08-21 13:39:23 +00:00
|
|
|
canvas.addedNew = true;
|
2009-08-31 14:54:59 +00:00
|
|
|
element.setAttribute("opacity", cur_shape.opacity);
|
2009-06-10 03:12:19 +00:00
|
|
|
cleanupElement(element);
|
2009-06-30 17:50:08 +00:00
|
|
|
selectorManager.update();
|
2009-10-18 20:46:32 +00:00
|
|
|
if(current_mode == "path") {
|
|
|
|
current_path = element;
|
2009-10-17 20:29:56 +00:00
|
|
|
current_mode = "pathedit";
|
2009-10-18 20:46:32 +00:00
|
|
|
recalcPathPoints();
|
|
|
|
addAllPointGripsToPath(current_path_pts.length/2 - 1);
|
2009-11-18 16:50:25 +00:00
|
|
|
} else if (current_mode == "text" || current_mode == "image") {
|
|
|
|
// keep us in the tool we were in unless it was a text or image element
|
2009-10-09 15:29:40 +00:00
|
|
|
canvas.addToSelection([element], true);
|
|
|
|
}
|
2009-06-25 02:54:07 +00:00
|
|
|
// we create the insert command that is stored on the stack
|
|
|
|
// undo means to call cmd.unapply(), redo means to call cmd.apply()
|
|
|
|
addCommandToHistory(new InsertElementCommand(element));
|
2009-07-01 19:54:51 +00:00
|
|
|
call("changed",[element]);
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
2009-11-10 15:16:49 +00:00
|
|
|
|
|
|
|
start_transform = null;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
|
|
|
// public functions
|
|
|
|
|
2009-10-13 02:48:27 +00:00
|
|
|
// Group: Serialization
|
|
|
|
|
|
|
|
// Function: open
|
|
|
|
// Calls the 'opened' handler and sends the SVG XML text. Clients of the SvgCanvas bind
|
|
|
|
// their load function (typically calls to setSvgString() to the 'opened' event.
|
2009-08-25 16:35:57 +00:00
|
|
|
this.open = function(str) {
|
2009-10-13 02:48:27 +00:00
|
|
|
// Nothing by default, handled by optional widget/extension
|
2009-08-25 16:35:57 +00:00
|
|
|
call("opened", str);
|
2009-08-19 03:42:27 +00:00
|
|
|
};
|
|
|
|
|
2009-10-09 19:07:52 +00:00
|
|
|
// Function: save
|
|
|
|
// Serializes the current drawing into SVG XML text and returns it to the 'saved' handler.
|
2009-10-13 02:48:27 +00:00
|
|
|
// This function also includes the XML prolog. Clients of the SvgCanvas bind their save
|
|
|
|
// function to the 'saved' event.
|
2009-10-09 19:07:52 +00:00
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// Nothing
|
2009-11-17 16:42:04 +00:00
|
|
|
this.save = function(opts) {
|
2009-06-07 05:07:23 +00:00
|
|
|
// remove the selected outline before serializing
|
2009-07-01 15:34:15 +00:00
|
|
|
this.clearSelection();
|
2009-11-17 16:42:04 +00:00
|
|
|
// Update save options if provided
|
|
|
|
if(opts) $.extend(save_options, opts);
|
|
|
|
save_options.apply = true;
|
2009-08-19 17:08:05 +00:00
|
|
|
|
2009-06-16 02:52:36 +00:00
|
|
|
var str = "<?xml version=\"1.0\" standalone=\"no\"?>\n";
|
2009-07-08 00:03:08 +00:00
|
|
|
// no need for doctype, see http://jwatt.org/svg/authoring/#doctype-declaration
|
2009-09-09 14:18:24 +00:00
|
|
|
str += svgCanvasToString();
|
2009-08-17 21:55:47 +00:00
|
|
|
call("saved", str);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-09-21 22:16:44 +00:00
|
|
|
var walkTree = function(elem, cbFn){
|
|
|
|
if (elem && elem.nodeType == 1) {
|
|
|
|
cbFn(elem);
|
|
|
|
var i = elem.childNodes.length;
|
|
|
|
while (i--) {
|
|
|
|
walkTree(elem.childNodes.item(i), cbFn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-10-09 19:07:52 +00:00
|
|
|
// Function: getSvgString
|
|
|
|
// Returns the current drawing as raw SVG XML text.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// The current drawing as raw SVG XML text.
|
2009-07-10 05:04:06 +00:00
|
|
|
this.getSvgString = function() {
|
2009-11-17 16:42:04 +00:00
|
|
|
save_options.apply = false;
|
2009-09-09 14:18:24 +00:00
|
|
|
return svgCanvasToString();
|
2009-07-10 05:04:06 +00:00
|
|
|
};
|
|
|
|
|
2009-10-09 19:07:52 +00:00
|
|
|
// Function: setSvgString
|
|
|
|
// This function sets the current drawing as the input SVG XML.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// xmlString - The SVG as XML text.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// This function returns false if the set was unsuccessful, true otherwise.
|
2009-07-10 05:04:06 +00:00
|
|
|
this.setSvgString = function(xmlString) {
|
|
|
|
try {
|
|
|
|
// convert string into XML document
|
|
|
|
var newDoc = Utils.text2xml(xmlString);
|
|
|
|
|
|
|
|
// run it through our sanitizer to remove anything we do not support
|
|
|
|
sanitizeSvg(newDoc.documentElement);
|
|
|
|
|
|
|
|
var batchCmd = new BatchCommand("Change Source");
|
|
|
|
|
2009-09-09 16:43:09 +00:00
|
|
|
// remove old svg document
|
2009-10-21 15:12:32 +00:00
|
|
|
var oldzoom = svgroot.removeChild(svgcontent);
|
2009-09-09 16:43:09 +00:00
|
|
|
batchCmd.addSubCommand(new RemoveElementCommand(oldzoom, svgroot));
|
2009-07-10 05:04:06 +00:00
|
|
|
|
2009-09-09 16:43:09 +00:00
|
|
|
// set new svg document
|
2009-10-21 15:12:32 +00:00
|
|
|
svgcontent = svgroot.appendChild(svgdoc.importNode(newDoc.documentElement, true));
|
2009-11-03 21:16:36 +00:00
|
|
|
|
2009-11-17 21:36:59 +00:00
|
|
|
// change image href vals if possible
|
|
|
|
$(svgcontent).find('image').each(function() {
|
|
|
|
var image = this;
|
|
|
|
var val = this.getAttributeNS(xlinkns, "href");
|
|
|
|
if(val.indexOf('data:') === 0) {
|
|
|
|
// Check if an SVG-edit data URI
|
|
|
|
var m = val.match(/svgedit_url=(.*?);/);
|
|
|
|
if(m) {
|
|
|
|
var url = decodeURIComponent(m[1]);
|
|
|
|
$(new Image()).load(function() {
|
|
|
|
image.setAttributeNS(xlinkns,'href',url);
|
|
|
|
}).attr('src',url);
|
|
|
|
}
|
|
|
|
}
|
2009-11-18 16:50:25 +00:00
|
|
|
// Add to encodableImages if it loads
|
|
|
|
canvas.embedImage(val);
|
2009-11-17 21:36:59 +00:00
|
|
|
});
|
|
|
|
|
2009-11-03 21:16:36 +00:00
|
|
|
// Fix XML for Opera/Win/Non-EN
|
|
|
|
if(window.opera) {
|
2009-11-13 20:45:18 +00:00
|
|
|
canvas.fixOperaXML(svgcontent, newDoc.documentElement);
|
2009-11-03 21:16:36 +00:00
|
|
|
}
|
|
|
|
|
2009-10-21 15:12:32 +00:00
|
|
|
svgcontent.setAttribute('id', 'svgcontent');
|
2009-09-09 17:41:28 +00:00
|
|
|
// determine proper size
|
2009-09-09 18:15:28 +00:00
|
|
|
var w, h;
|
2009-10-21 15:12:32 +00:00
|
|
|
if (svgcontent.getAttribute("viewBox")) {
|
|
|
|
var vb = svgcontent.getAttribute("viewBox").split(' ');
|
2009-09-09 18:15:28 +00:00
|
|
|
w = vb[2];
|
|
|
|
h = vb[3];
|
|
|
|
}
|
|
|
|
// handle old content that doesn't have a viewBox
|
|
|
|
else {
|
2009-10-21 15:12:32 +00:00
|
|
|
w = svgcontent.getAttribute("width");
|
|
|
|
h = svgcontent.getAttribute("height");
|
|
|
|
svgcontent.setAttribute("viewBox", ["0", "0", w, h].join(" "));
|
2009-09-09 18:15:28 +00:00
|
|
|
}
|
2009-09-09 17:41:28 +00:00
|
|
|
// just to be safe, remove any width/height from text so that they are 100%/100%
|
2009-10-21 15:12:32 +00:00
|
|
|
svgcontent.removeAttribute('width');
|
|
|
|
svgcontent.removeAttribute('height');
|
|
|
|
batchCmd.addSubCommand(new InsertElementCommand(svgcontent));
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-09-09 17:41:28 +00:00
|
|
|
// update root to the correct size
|
|
|
|
var changes = {};
|
|
|
|
changes['width'] = svgroot.getAttribute('width');
|
|
|
|
changes['height'] = svgroot.getAttribute('height');
|
|
|
|
svgroot.setAttribute('width', w);
|
|
|
|
svgroot.setAttribute('height', h);
|
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(svgroot, changes));
|
|
|
|
|
|
|
|
// reset zoom
|
|
|
|
current_zoom = 1;
|
2009-09-21 02:05:58 +00:00
|
|
|
|
|
|
|
// identify layers
|
2009-10-17 20:29:56 +00:00
|
|
|
identifyLayers();
|
2009-09-21 22:16:44 +00:00
|
|
|
|
2009-09-09 17:41:28 +00:00
|
|
|
selectorManager.update();
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
2009-10-21 15:12:32 +00:00
|
|
|
call("changed", [svgcontent]);
|
2009-07-10 05:04:06 +00:00
|
|
|
} catch(e) {
|
|
|
|
console.log(e);
|
|
|
|
return false;
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
return true;
|
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-09-22 02:42:31 +00:00
|
|
|
// Layer API Functions
|
2009-10-13 02:48:27 +00:00
|
|
|
|
|
|
|
// Group: Layers
|
|
|
|
|
2009-10-17 20:29:56 +00:00
|
|
|
var identifyLayers = function() {
|
2009-09-22 01:00:24 +00:00
|
|
|
all_layers = [];
|
2009-10-21 15:12:32 +00:00
|
|
|
var numchildren = svgcontent.childNodes.length;
|
|
|
|
// loop through all children of svgcontent
|
2009-09-24 03:13:25 +00:00
|
|
|
var orphans = [], layernames = [];
|
2009-09-22 01:00:24 +00:00
|
|
|
for (var i = 0; i < numchildren; ++i) {
|
2009-10-21 15:12:32 +00:00
|
|
|
var child = svgcontent.childNodes.item(i);
|
2009-09-22 01:00:24 +00:00
|
|
|
// for each g, find its layer name
|
2009-09-24 03:14:43 +00:00
|
|
|
if (child && child.nodeType == 1) {
|
2009-09-24 03:13:25 +00:00
|
|
|
if (child.tagName == "g") {
|
2009-10-17 20:29:56 +00:00
|
|
|
var name = $("title",child).text();
|
2009-09-24 03:13:25 +00:00
|
|
|
// store layer and name in global variable
|
|
|
|
if (name) {
|
|
|
|
layernames.push(name);
|
|
|
|
all_layers.push( [name,child] );
|
|
|
|
current_layer = child;
|
2009-10-13 00:48:05 +00:00
|
|
|
walkTree(child, function(e){e.setAttribute("style", "pointer-events:inherit");});
|
|
|
|
current_layer.setAttribute("style", "pointer-events:none");
|
2009-09-24 03:13:25 +00:00
|
|
|
}
|
2009-09-29 18:00:14 +00:00
|
|
|
// if group did not have a name, it is an orphan
|
|
|
|
else {
|
|
|
|
orphans.push(child);
|
|
|
|
}
|
2009-09-24 03:13:25 +00:00
|
|
|
}
|
2009-10-09 02:14:47 +00:00
|
|
|
// if child has a bbox (i.e. not a <title> or <defs> element), then it is an orphan
|
2009-11-02 14:29:12 +00:00
|
|
|
else if(canvas.getBBox(child) && child.nodeName != 'defs') { // Opera returns a BBox for defs
|
|
|
|
var bb = canvas.getBBox(child);
|
2009-09-24 03:13:25 +00:00
|
|
|
orphans.push(child);
|
2009-09-22 01:00:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-24 03:13:25 +00:00
|
|
|
// create a new layer and add all the orphans to it
|
|
|
|
if (orphans.length > 0) {
|
|
|
|
var i = 1;
|
|
|
|
while ($.inArray(("Layer " + i), layernames) != -1) { i++; }
|
|
|
|
var newname = "Layer " + i;
|
|
|
|
current_layer = svgdoc.createElementNS(svgns, "g");
|
|
|
|
var layer_title = svgdoc.createElementNS(svgns, "title");
|
|
|
|
layer_title.textContent = newname;
|
|
|
|
current_layer.appendChild(layer_title);
|
|
|
|
for (var j = 0; j < orphans.length; ++j) {
|
|
|
|
current_layer.appendChild(orphans[j]);
|
|
|
|
}
|
2009-10-21 15:12:32 +00:00
|
|
|
current_layer = svgcontent.appendChild(current_layer);
|
2009-09-24 03:13:25 +00:00
|
|
|
all_layers.push( [newname, current_layer] );
|
|
|
|
}
|
2009-10-11 04:32:21 +00:00
|
|
|
walkTree(current_layer, function(e){e.setAttribute("style","pointer-events:inherit");});
|
|
|
|
current_layer.setAttribute("style","pointer-events:all");
|
2009-09-22 01:00:24 +00:00
|
|
|
};
|
|
|
|
|
2009-10-09 19:07:52 +00:00
|
|
|
// Function: createLayer
|
2009-10-13 02:35:36 +00:00
|
|
|
// Creates a new top-level layer in the drawing with the given name, sets the current layer
|
|
|
|
// to it, and then clears the selection This function then calls the 'changed' handler.
|
|
|
|
// This is an undoable action.
|
2009-10-09 19:07:52 +00:00
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// name - The given name
|
2009-09-22 01:00:24 +00:00
|
|
|
this.createLayer = function(name) {
|
|
|
|
var batchCmd = new BatchCommand("Create Layer");
|
2009-09-22 18:10:54 +00:00
|
|
|
var new_layer = svgdoc.createElementNS(svgns, "g");
|
2009-09-22 01:00:24 +00:00
|
|
|
var layer_title = svgdoc.createElementNS(svgns, "title");
|
2009-09-22 18:10:54 +00:00
|
|
|
layer_title.textContent = name;
|
|
|
|
new_layer.appendChild(layer_title);
|
2009-10-21 15:12:32 +00:00
|
|
|
new_layer = svgcontent.appendChild(new_layer);
|
2009-09-22 18:10:54 +00:00
|
|
|
batchCmd.addSubCommand(new InsertElementCommand(new_layer));
|
2009-09-22 01:00:24 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
2009-09-22 02:42:31 +00:00
|
|
|
canvas.clearSelection();
|
2009-10-17 20:29:56 +00:00
|
|
|
identifyLayers();
|
2009-09-22 18:10:54 +00:00
|
|
|
canvas.setCurrentLayer(name);
|
|
|
|
call("changed", [new_layer]);
|
2009-09-22 01:00:24 +00:00
|
|
|
};
|
|
|
|
|
2009-10-09 19:07:52 +00:00
|
|
|
// Function: deleteCurrentLayer
|
2009-10-13 02:35:36 +00:00
|
|
|
// Deletes the current layer from the drawing and then clears the selection. This function
|
|
|
|
// then calls the 'changed' handler. This is an undoable action.
|
2009-09-22 01:00:24 +00:00
|
|
|
this.deleteCurrentLayer = function() {
|
|
|
|
if (current_layer && all_layers.length > 1) {
|
|
|
|
var batchCmd = new BatchCommand("Delete Layer");
|
2009-09-22 18:10:54 +00:00
|
|
|
// actually delete from the DOM and store in our Undo History
|
|
|
|
var parent = current_layer.parentNode;
|
|
|
|
batchCmd.addSubCommand(new RemoveElementCommand(current_layer, parent));
|
|
|
|
parent.removeChild(current_layer);
|
2009-09-22 01:00:24 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
2009-09-22 02:42:31 +00:00
|
|
|
canvas.clearSelection();
|
2009-10-17 20:29:56 +00:00
|
|
|
identifyLayers();
|
2009-09-22 18:10:54 +00:00
|
|
|
canvas.setCurrentLayer(all_layers[all_layers.length-1][0]);
|
2009-10-21 15:12:32 +00:00
|
|
|
call("changed", [svgcontent]);
|
2009-09-22 01:00:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2009-10-09 19:07:52 +00:00
|
|
|
// Function: getNumLayers
|
2009-10-13 02:35:36 +00:00
|
|
|
// Returns the number of layers in the current drawing.
|
2009-10-09 19:07:52 +00:00
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// The number of layers in the current drawing.
|
2009-09-21 22:16:44 +00:00
|
|
|
this.getNumLayers = function() {
|
|
|
|
return all_layers.length;
|
|
|
|
};
|
2009-10-09 19:07:52 +00:00
|
|
|
|
2009-10-10 21:46:06 +00:00
|
|
|
// Function: getLayer
|
2009-10-13 02:35:36 +00:00
|
|
|
// Returns the name of the ith layer. If the index is out of range, an empty string is returned.
|
2009-10-10 21:46:06 +00:00
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// i - the zero-based index of the layer you are querying.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// The name of the ith layer
|
2009-09-21 22:16:44 +00:00
|
|
|
this.getLayer = function(i) {
|
|
|
|
if (i >= 0 && i < canvas.getNumLayers()) {
|
|
|
|
return all_layers[i][0];
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
};
|
2009-09-22 04:44:56 +00:00
|
|
|
|
2009-10-10 21:46:06 +00:00
|
|
|
// Function: getCurrentLayer
|
2009-10-13 02:35:36 +00:00
|
|
|
// Returns the name of the currently selected layer. If an error occurs, an empty string
|
|
|
|
// is returned.
|
2009-10-10 21:46:06 +00:00
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// The name of the currently active layer.
|
2009-09-21 22:16:44 +00:00
|
|
|
this.getCurrentLayer = function() {
|
|
|
|
for (var i = 0; i < all_layers.length; ++i) {
|
|
|
|
if (all_layers[i][1] == current_layer) {
|
|
|
|
return all_layers[i][0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
};
|
2009-09-22 04:44:56 +00:00
|
|
|
|
2009-10-10 21:46:06 +00:00
|
|
|
// Function: setCurrentLayer
|
2009-10-13 02:35:36 +00:00
|
|
|
// Sets the current layer. If the name is not a valid layer name, then this function returns
|
|
|
|
// false. Otherwise it returns true. This is not an undo-able action.
|
2009-10-10 21:46:06 +00:00
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// name - the name of the layer you want to switch to.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// true if the current layer was switched, otherwise false
|
2009-09-21 22:16:44 +00:00
|
|
|
this.setCurrentLayer = function(name) {
|
2009-10-05 08:16:41 +00:00
|
|
|
name = toXml(name);
|
2009-09-21 22:16:44 +00:00
|
|
|
for (var i = 0; i < all_layers.length; ++i) {
|
|
|
|
if (name == all_layers[i][0]) {
|
|
|
|
if (current_layer != all_layers[i][1]) {
|
2009-09-22 01:00:24 +00:00
|
|
|
canvas.clearSelection();
|
2009-10-11 04:32:21 +00:00
|
|
|
current_layer.setAttribute("style", "pointer-events:none");
|
2009-09-21 22:16:44 +00:00
|
|
|
current_layer = all_layers[i][1];
|
2009-10-11 04:32:21 +00:00
|
|
|
current_layer.setAttribute("style", "pointer-events:all");
|
2009-09-21 22:16:44 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2009-09-22 04:44:56 +00:00
|
|
|
|
2009-10-10 21:46:06 +00:00
|
|
|
// Function: renameCurrentLayer
|
2009-10-13 02:35:36 +00:00
|
|
|
// Renames the current layer. If the layer name is not valid (i.e. unique), then this function
|
|
|
|
// does nothing and returns false, otherwise it returns true. This is an undo-able action.
|
2009-10-10 21:46:06 +00:00
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// newname - the new name you want to give the current layer. This name must be unique
|
|
|
|
// among all layer names.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// true if the rename succeeded, false otherwise.
|
2009-09-23 03:05:27 +00:00
|
|
|
this.renameCurrentLayer = function(newname) {
|
|
|
|
if (current_layer) {
|
2009-09-21 22:16:44 +00:00
|
|
|
var oldLayer = current_layer;
|
|
|
|
// setCurrentLayer will return false if the name doesn't already exists
|
|
|
|
if (!canvas.setCurrentLayer(newname)) {
|
2009-09-22 02:42:31 +00:00
|
|
|
var batchCmd = new BatchCommand("Rename Layer");
|
2009-09-21 22:16:44 +00:00
|
|
|
// find the index of the layer
|
|
|
|
for (var i = 0; i < all_layers.length; ++i) {
|
|
|
|
if (all_layers[i][1] == oldLayer) break;
|
|
|
|
}
|
2009-09-23 03:05:27 +00:00
|
|
|
var oldname = all_layers[i][0];
|
2009-10-05 08:16:41 +00:00
|
|
|
all_layers[i][0] = toXml(newname);
|
2009-09-21 22:16:44 +00:00
|
|
|
|
|
|
|
// now change the underlying title element contents
|
|
|
|
var len = oldLayer.childNodes.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
var child = oldLayer.childNodes.item(i);
|
|
|
|
// found the <title> element, now append all the
|
|
|
|
if (child && child.tagName == "title") {
|
|
|
|
// wipe out old name
|
|
|
|
while (child.firstChild) { child.removeChild(child.firstChild); }
|
2009-09-22 02:42:31 +00:00
|
|
|
child.textContent = newname;
|
|
|
|
|
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(child, {"#text":oldname}));
|
|
|
|
addCommandToHistory(batchCmd);
|
|
|
|
call("changed", [oldLayer]);
|
2009-09-21 22:16:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-23 03:05:27 +00:00
|
|
|
current_layer = oldLayer;
|
2009-09-21 22:16:44 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2009-09-22 04:44:56 +00:00
|
|
|
|
2009-10-10 21:46:06 +00:00
|
|
|
// Function: setCurrentLayerPosition
|
2009-10-13 02:35:36 +00:00
|
|
|
// Changes the position of the current layer to the new value. If the new index is not valid,
|
|
|
|
// this function does nothing and returns false, otherwise it returns true. This is an
|
|
|
|
// undo-able action.
|
2009-10-10 21:46:06 +00:00
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// newpos - The zero-based index of the new position of the layer. This should be between
|
|
|
|
// 0 and (number of layers - 1)
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// true if the current layer position was changed, false otherwise.
|
2009-09-22 04:44:56 +00:00
|
|
|
this.setCurrentLayerPosition = function(newpos) {
|
|
|
|
if (current_layer && newpos >= 0 && newpos < all_layers.length) {
|
|
|
|
for (var oldpos = 0; oldpos < all_layers.length; ++oldpos) {
|
|
|
|
if (all_layers[oldpos][1] == current_layer) break;
|
|
|
|
}
|
|
|
|
// some unknown error condition (current_layer not in all_layers)
|
|
|
|
if (oldpos == all_layers.length) { return false; }
|
|
|
|
|
|
|
|
if (oldpos != newpos) {
|
|
|
|
// if our new position is below us, we need to insert before the node after newpos
|
|
|
|
var refLayer = null;
|
|
|
|
var oldNextSibling = current_layer.nextSibling;
|
|
|
|
if (newpos > oldpos ) {
|
|
|
|
if (newpos < all_layers.length-1) {
|
|
|
|
refLayer = all_layers[newpos+1][1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if our new position is above us, we need to insert before the node at newpos
|
|
|
|
else {
|
|
|
|
refLayer = all_layers[newpos][1];
|
|
|
|
}
|
2009-10-21 15:12:32 +00:00
|
|
|
svgcontent.insertBefore(current_layer, refLayer);
|
|
|
|
addCommandToHistory(new MoveElementCommand(current_layer, oldNextSibling, svgcontent));
|
2009-09-22 04:44:56 +00:00
|
|
|
|
2009-10-17 20:29:56 +00:00
|
|
|
identifyLayers();
|
2009-09-22 18:10:54 +00:00
|
|
|
canvas.setCurrentLayer(all_layers[newpos][0]);
|
2009-09-22 04:44:56 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2009-10-10 21:46:06 +00:00
|
|
|
// Function: getLayerVisibility
|
2009-10-13 02:35:36 +00:00
|
|
|
// Returns whether the layer is visible. If the layer name is not valid, then this function
|
|
|
|
// returns false.
|
2009-10-10 21:46:06 +00:00
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// layername - the name of the layer which you want to query.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// The visibility state of the layer, or false if the layer name was invalid.
|
2009-09-24 04:48:40 +00:00
|
|
|
this.getLayerVisibility = function(layername) {
|
|
|
|
// find the layer
|
|
|
|
var layer = null;
|
|
|
|
for (var i = 0; i < all_layers.length; ++i) {
|
|
|
|
if (all_layers[i][0] == layername) {
|
|
|
|
layer = all_layers[i][1];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!layer) return false;
|
|
|
|
return (layer.getAttribute("display") != "none");
|
|
|
|
};
|
|
|
|
|
2009-10-10 21:46:06 +00:00
|
|
|
// Function: setLayerVisibility
|
2009-10-13 02:35:36 +00:00
|
|
|
// Sets the visibility of the layer. If the layer name is not valid, this function return
|
|
|
|
// false, otherwise it returns true. This is an undo-able action.
|
2009-10-10 21:46:06 +00:00
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// layername - the name of the layer to change the visibility
|
|
|
|
// bVisible - true/false, whether the layer should be visible
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// true if the layer's visibility was set, false otherwise
|
2009-09-24 04:48:40 +00:00
|
|
|
this.setLayerVisibility = function(layername, bVisible) {
|
|
|
|
// find the layer
|
|
|
|
var layer = null;
|
|
|
|
for (var i = 0; i < all_layers.length; ++i) {
|
|
|
|
if (all_layers[i][0] == layername) {
|
|
|
|
layer = all_layers[i][1];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!layer) return false;
|
|
|
|
|
|
|
|
var oldDisplay = layer.getAttribute("display");
|
|
|
|
if (!oldDisplay) oldDisplay = "inline";
|
|
|
|
layer.setAttribute("display", bVisible ? "inline" : "none");
|
|
|
|
addCommandToHistory(new ChangeElementCommand(layer, {"display":oldDisplay}, "Layer Visibility"));
|
|
|
|
|
|
|
|
if (layer == current_layer) {
|
|
|
|
canvas.clearSelection();
|
|
|
|
}
|
|
|
|
// call("changed", [selected]);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2009-10-10 21:46:06 +00:00
|
|
|
// Function: moveSelectedToLayer
|
2009-10-13 02:35:36 +00:00
|
|
|
// Moves the selected elements to layername. If the name is not a valid layer name, then false
|
|
|
|
// is returned. Otherwise it returns true. This is an undo-able action.
|
2009-10-10 21:46:06 +00:00
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// layername - the name of the layer you want to which you want to move the selected elements
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// true if the selected elements were moved to the layer, false otherwise.
|
|
|
|
this.moveSelectedToLayer = function(layername) {
|
|
|
|
// find the layer
|
|
|
|
var layer = null;
|
|
|
|
for (var i = 0; i < all_layers.length; ++i) {
|
|
|
|
if (all_layers[i][0] == layername) {
|
|
|
|
layer = all_layers[i][1];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!layer) return false;
|
|
|
|
|
|
|
|
var batchCmd = new BatchCommand("Move Elements to Layer");
|
|
|
|
|
|
|
|
// loop for each selected element and move it
|
|
|
|
var selElems = selectedElements;
|
|
|
|
var i = selElems.length;
|
|
|
|
while (i--) {
|
|
|
|
var elem = selElems[i];
|
|
|
|
if (!elem) continue;
|
|
|
|
var oldNextSibling = elem.nextSibling;
|
|
|
|
// TODO: this is pretty brittle!
|
|
|
|
var oldLayer = elem.parentNode;
|
|
|
|
layer.appendChild(elem);
|
|
|
|
batchCmd.addSubCommand(new MoveElementCommand(elem, oldNextSibling, oldLayer));
|
|
|
|
}
|
|
|
|
|
|
|
|
addCommandToHistory(batchCmd);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2009-10-14 05:11:51 +00:00
|
|
|
// Function: getLayerOpacity
|
|
|
|
// Returns the opacity of the given layer. If the input name is not a layer, null is returned.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// layername - name of the layer on which to get the opacity
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// The opacity value of the given layer. This will be a value between 0.0 and 1.0, or null
|
|
|
|
// if layername is not a valid layer
|
|
|
|
this.getLayerOpacity = function(layername) {
|
|
|
|
for (var i = 0; i < all_layers.length; ++i) {
|
|
|
|
if (all_layers[i][0] == layername) {
|
|
|
|
var g = all_layers[i][1];
|
|
|
|
var opacity = g.getAttribute("opacity");
|
|
|
|
if (!opacity) {
|
|
|
|
opacity = "1.0";
|
|
|
|
}
|
|
|
|
return parseFloat(opacity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Function: setLayerOpacity
|
|
|
|
// Sets the opacity of the given layer. If the input name is not a layer, nothing happens.
|
|
|
|
// This is not an undo-able action. NOTE: this function exists solely to apply
|
|
|
|
// a highlighting/de-emphasis effect to a layer, when it is possible for a user to affect
|
|
|
|
// the opacity of a layer, we will need to allow this function to produce an undo-able action.
|
|
|
|
// If opacity is not a value between 0.0 and 1.0, then nothing happens.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// layername - name of the layer on which to set the opacity
|
|
|
|
// opacity - a float value in the range 0.0-1.0
|
|
|
|
this.setLayerOpacity = function(layername, opacity) {
|
|
|
|
if (opacity < 0.0 || opacity > 1.0) return;
|
|
|
|
for (var i = 0; i < all_layers.length; ++i) {
|
|
|
|
if (all_layers[i][0] == layername) {
|
|
|
|
var g = all_layers[i][1];
|
|
|
|
g.setAttribute("opacity", opacity);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2009-09-21 22:16:44 +00:00
|
|
|
|
2009-10-13 02:35:36 +00:00
|
|
|
// Function: clear
|
|
|
|
// Clears the current document. This is not an undoable action.
|
2009-06-01 21:24:30 +00:00
|
|
|
this.clear = function() {
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path_pts = [];
|
2009-09-21 02:05:58 +00:00
|
|
|
|
2009-10-21 15:12:32 +00:00
|
|
|
// clear the svgcontent node
|
|
|
|
var nodes = svgcontent.childNodes;
|
|
|
|
var len = svgcontent.childNodes.length;
|
2009-06-01 21:24:30 +00:00
|
|
|
var i = 0;
|
2009-07-01 15:34:15 +00:00
|
|
|
this.clearSelection();
|
2009-06-01 21:24:30 +00:00
|
|
|
for(var rep = 0; rep < len; rep++){
|
2009-06-02 16:40:33 +00:00
|
|
|
if (nodes[i].nodeType == 1) { // element node
|
2009-10-21 15:12:32 +00:00
|
|
|
svgcontent.removeChild(nodes[i]);
|
2009-06-01 21:24:30 +00:00
|
|
|
} else {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
2009-09-21 02:05:58 +00:00
|
|
|
// create empty first layer
|
2009-09-21 22:16:44 +00:00
|
|
|
all_layers = [];
|
2009-09-22 01:00:24 +00:00
|
|
|
canvas.createLayer("Layer 1");
|
2009-09-21 02:05:58 +00:00
|
|
|
|
2009-07-02 03:54:11 +00:00
|
|
|
// clear the undo stack
|
|
|
|
resetUndoStack();
|
2009-09-04 06:36:37 +00:00
|
|
|
// reset the selector manager
|
|
|
|
selectorManager.initGroup();
|
2009-09-04 17:23:56 +00:00
|
|
|
// reset the rubber band box
|
|
|
|
rubberBox = selectorManager.getRubberBandBox();
|
|
|
|
|
2009-06-12 12:20:48 +00:00
|
|
|
call("cleared");
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-09-04 16:26:48 +00:00
|
|
|
|
2009-10-13 02:35:36 +00:00
|
|
|
// TODO: should this be an 'internal' function?
|
2009-10-18 20:46:32 +00:00
|
|
|
this.clearPath = function(remove) {
|
|
|
|
if(remove && current_mode == "path") {
|
2009-09-24 16:05:23 +00:00
|
|
|
var elem = svgdoc.getElementById(getId());
|
|
|
|
if(elem) elem.parentNode.removeChild(elem);
|
|
|
|
}
|
2009-10-18 20:46:32 +00:00
|
|
|
removeAllPointGripsFromPath();
|
|
|
|
current_path_pts = [];
|
|
|
|
current_path_pt = -1;
|
2009-09-04 16:26:48 +00:00
|
|
|
};
|
2009-10-05 13:50:44 +00:00
|
|
|
|
2009-10-13 02:35:36 +00:00
|
|
|
// TODO: should this be an 'internal' function?
|
2009-10-05 13:50:44 +00:00
|
|
|
this.getNodePoint = function() {
|
2009-10-18 20:46:32 +00:00
|
|
|
if(current_path_pt != -1) {
|
|
|
|
var pt = getPathPoint(current_path_pt, true);
|
|
|
|
var list = current_path.pathSegList;
|
2009-10-12 20:26:03 +00:00
|
|
|
var segtype;
|
2009-10-18 20:46:32 +00:00
|
|
|
if(list.numberOfItems > current_path_pt+1) {
|
|
|
|
segtype = list.getItem(current_path_pt+1).pathSegType;
|
2009-10-12 20:26:03 +00:00
|
|
|
} else {
|
|
|
|
segtype = false;
|
|
|
|
}
|
2009-10-09 15:29:40 +00:00
|
|
|
return {
|
|
|
|
x: pt[0],
|
|
|
|
y: pt[1],
|
|
|
|
type: segtype
|
|
|
|
}
|
2009-10-05 13:50:44 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2009-10-07 16:04:32 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
this.clonePathNode = function() {
|
2009-10-09 15:29:40 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var pt = current_path_pt, list = current_path.pathSegList;
|
2009-10-07 16:04:32 +00:00
|
|
|
|
|
|
|
var next_item = list.getItem(pt+1);
|
|
|
|
|
|
|
|
// Get point in between nodes
|
|
|
|
if(next_item.pathSegType % 2 == 0) { // even num, so abs
|
|
|
|
var cur_item = list.getItem(pt);
|
2009-10-09 19:47:32 +00:00
|
|
|
var new_x = (next_item.x + cur_item.x) / 2;
|
|
|
|
var new_y = (next_item.y + cur_item.y) / 2;
|
2009-10-07 16:04:32 +00:00
|
|
|
} else {
|
|
|
|
var new_x = next_item.x/2;
|
|
|
|
var new_y = next_item.y/2;
|
2009-10-05 19:09:39 +00:00
|
|
|
}
|
2009-10-07 16:04:32 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var seg = current_path.createSVGPathSegLinetoAbs(new_x, new_y);
|
2009-10-07 16:04:32 +00:00
|
|
|
list.insertItemBefore(seg, pt+1); // Webkit doesn't do this right.
|
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var abs_x = (getPathPoint(pt)[0] + new_x) * current_zoom;
|
|
|
|
var abs_y = (getPathPoint(pt)[1] + new_y) * current_zoom;
|
2009-10-07 16:04:32 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var last_num = current_path_pts.length/2;
|
2009-10-07 16:04:32 +00:00
|
|
|
|
|
|
|
// Add new grip
|
2009-10-18 20:46:32 +00:00
|
|
|
addPointGripToPath(abs_x, abs_y, last_num);
|
2009-10-09 15:29:40 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
// Update path_pts
|
|
|
|
current_path_pts.splice(pt*2 + 2, 0, abs_x, abs_y);
|
2009-10-07 16:04:32 +00:00
|
|
|
|
|
|
|
resetPointGrips();
|
|
|
|
this.addNodeToSelection(pt+1);
|
2009-10-09 19:47:32 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
// current_path.setAttribute("d", convertToD(current_path.pathSegList));
|
2009-10-07 16:04:32 +00:00
|
|
|
}
|
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
this.deletePathNode = function() {
|
|
|
|
var last_pt = current_path_pts.length/2 - 1;
|
|
|
|
var pt = current_path_pt, list = current_path.pathSegList;
|
2009-10-05 19:09:39 +00:00
|
|
|
var cur_item = list.getItem(pt);
|
|
|
|
var next_item = list.getItem(pt+1);
|
|
|
|
|
|
|
|
if(pt == 0) {
|
2009-10-18 20:46:32 +00:00
|
|
|
var next_x = getPathPoint(1)[0];
|
|
|
|
var next_y = getPathPoint(1)[1];
|
2009-10-05 19:09:39 +00:00
|
|
|
// Make the next point be the "M" point
|
|
|
|
replacePathSeg(2, 1, [next_x, next_y]);
|
|
|
|
|
|
|
|
// Reposition last node
|
|
|
|
var last_item = list.getItem(last_pt);
|
2009-10-09 19:47:32 +00:00
|
|
|
replacePathSeg(4, last_pt, [next_x, next_y]);
|
2009-10-05 19:09:39 +00:00
|
|
|
removeControlPointGrips(last_pt - 1);
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path_pts.splice(last_pt*2, 2, next_x, next_y);
|
|
|
|
current_path_pts.splice(0, 2);
|
2009-10-05 19:09:39 +00:00
|
|
|
} else {
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path_pts.splice(pt*2, 2);
|
2009-10-05 19:09:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
list.removeItem(pt);
|
|
|
|
|
2009-10-07 16:04:32 +00:00
|
|
|
resetPointGrips();
|
|
|
|
|
|
|
|
if(window.opera) { // Opera repaints incorrectly
|
2009-10-18 20:46:32 +00:00
|
|
|
var cp = $(current_path); cp.attr('d',cp.attr('d'));
|
2009-10-07 16:04:32 +00:00
|
|
|
}
|
2009-10-05 19:09:39 +00:00
|
|
|
|
|
|
|
this.addNodeToSelection(pt);
|
|
|
|
}
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
this.getResolution = function() {
|
2009-09-09 14:18:24 +00:00
|
|
|
// return [svgroot.getAttribute("width"), svgroot.getAttribute("height")];
|
2009-10-21 15:12:32 +00:00
|
|
|
var vb = svgcontent.getAttribute("viewBox").split(' ');
|
2009-09-09 14:18:24 +00:00
|
|
|
return {'w':vb[2], 'h':vb[3], 'zoom': current_zoom};
|
2009-07-10 05:04:06 +00:00
|
|
|
};
|
2009-09-22 17:22:28 +00:00
|
|
|
|
2009-10-19 19:06:07 +00:00
|
|
|
this.getImageTitle = function() {
|
2009-10-21 15:12:32 +00:00
|
|
|
var childs = svgcontent.childNodes;
|
2009-10-19 19:06:07 +00:00
|
|
|
for (var i=0; i<childs.length; i++) {
|
|
|
|
if(childs[i].nodeName == 'title') {
|
|
|
|
return childs[i].textContent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setImageTitle = function(newtitle) {
|
2009-10-21 15:12:32 +00:00
|
|
|
var childs = svgcontent.childNodes, doc_title = false, old_title = '';
|
2009-10-20 13:49:53 +00:00
|
|
|
|
|
|
|
var batchCmd = new BatchCommand("Change Image Title");
|
|
|
|
|
2009-10-19 19:06:07 +00:00
|
|
|
for (var i=0; i<childs.length; i++) {
|
|
|
|
if(childs[i].nodeName == 'title') {
|
2009-10-20 13:49:53 +00:00
|
|
|
doc_title = childs[i];
|
|
|
|
old_title = doc_title.textContent;
|
2009-10-19 19:06:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!doc_title) {
|
|
|
|
doc_title = svgdoc.createElementNS(svgns, "title");
|
2009-10-21 15:12:32 +00:00
|
|
|
svgcontent.insertBefore(doc_title, svgcontent.firstChild);
|
2009-10-19 19:06:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(newtitle.length) {
|
|
|
|
doc_title.textContent = newtitle;
|
|
|
|
} else {
|
|
|
|
// No title given, so element is not necessary
|
|
|
|
doc_title.parentNode.removeChild(doc_title);
|
|
|
|
}
|
2009-10-20 13:49:53 +00:00
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(doc_title, {'#text': old_title}));
|
|
|
|
addCommandToHistory(batchCmd);
|
2009-10-19 19:06:07 +00:00
|
|
|
}
|
|
|
|
|
2009-06-25 08:22:30 +00:00
|
|
|
this.setResolution = function(x, y) {
|
2009-09-11 15:49:50 +00:00
|
|
|
var res = canvas.getResolution();
|
|
|
|
var w = res.w, h = res.h;
|
2009-10-23 18:03:48 +00:00
|
|
|
var batchCmd;
|
2009-07-30 22:45:59 +00:00
|
|
|
|
2009-09-22 17:22:28 +00:00
|
|
|
if(x == 'fit') {
|
2009-08-20 15:59:29 +00:00
|
|
|
canvas.clearSelection();
|
|
|
|
|
|
|
|
// Get bounding box
|
2009-09-22 14:35:05 +00:00
|
|
|
var bbox = canvas.getStrokedBBox();
|
2009-08-20 15:59:29 +00:00
|
|
|
|
|
|
|
if(bbox) {
|
2009-10-23 18:03:48 +00:00
|
|
|
batchCmd = new BatchCommand("Fit Canvas to Content");
|
2009-11-02 19:18:37 +00:00
|
|
|
var visEls = canvas.getVisibleElements();
|
|
|
|
$.each(visEls, function(i, item) {
|
2009-09-22 14:35:05 +00:00
|
|
|
var sel_bb = item.getBBox();
|
2009-11-05 15:44:03 +00:00
|
|
|
// TODO: we are not using the second argument here anymore, what to do?
|
2009-10-23 18:03:48 +00:00
|
|
|
var cmd = recalculateDimensions(item, {
|
2009-09-22 14:35:05 +00:00
|
|
|
x: sel_bb.x - bbox.x,
|
|
|
|
y: sel_bb.y - bbox.y,
|
|
|
|
width: sel_bb.width,
|
|
|
|
height: sel_bb.height
|
|
|
|
});
|
2009-10-23 18:03:48 +00:00
|
|
|
batchCmd.addSubCommand(cmd);
|
2009-09-22 14:35:05 +00:00
|
|
|
});
|
2009-09-22 17:22:28 +00:00
|
|
|
x = Math.round(bbox.width);
|
|
|
|
y = Math.round(bbox.height);
|
2009-08-20 15:59:29 +00:00
|
|
|
} else {
|
2009-09-22 17:22:28 +00:00
|
|
|
return false;
|
2009-08-20 15:59:29 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-14 00:04:53 +00:00
|
|
|
x *= current_zoom;
|
|
|
|
y *= current_zoom;
|
|
|
|
if (x != w || y != h) {
|
|
|
|
var handle = svgroot.suspendRedraw(1000);
|
2009-10-23 18:03:48 +00:00
|
|
|
if(!batchCmd) {
|
|
|
|
batchCmd = new BatchCommand("Change Image Dimensions");
|
|
|
|
}
|
2009-09-14 00:04:53 +00:00
|
|
|
svgroot.setAttribute('width', x);
|
|
|
|
svgroot.setAttribute('height', y);
|
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(svgroot, {"width":w, "height":h}));
|
|
|
|
|
2009-10-21 15:12:32 +00:00
|
|
|
svgcontent.setAttribute("viewBox", ["0 0", x/current_zoom, y/current_zoom].join(' '));
|
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(svgcontent, {"viewBox": ["0 0", w, h].join(' ')}));
|
2009-09-11 15:49:50 +00:00
|
|
|
|
2009-09-14 00:04:53 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
|
|
|
svgroot.unsuspendRedraw(handle);
|
2009-10-21 15:12:32 +00:00
|
|
|
call("changed", [svgcontent]);
|
2009-09-14 00:04:53 +00:00
|
|
|
}
|
2009-09-22 17:22:28 +00:00
|
|
|
return true;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-25 08:22:30 +00:00
|
|
|
|
2009-09-16 15:45:50 +00:00
|
|
|
this.setBBoxZoom = function(val, editor_w, editor_h) {
|
2009-09-16 14:12:51 +00:00
|
|
|
var spacer = .85;
|
2009-09-23 15:29:55 +00:00
|
|
|
var bb;
|
|
|
|
var calcZoom = function(bb) {
|
|
|
|
var w_zoom = Math.round((editor_w / bb.width)*100 * spacer)/100;
|
|
|
|
var h_zoom = Math.round((editor_h / bb.height)*100 * spacer)/100;
|
|
|
|
var zoomlevel = Math.min(w_zoom,h_zoom);
|
|
|
|
canvas.setZoom(zoomlevel);
|
|
|
|
return {'zoom': zoomlevel, 'bbox': bb};
|
|
|
|
}
|
2009-09-16 14:12:51 +00:00
|
|
|
|
2009-09-16 15:45:50 +00:00
|
|
|
if(typeof val == 'object') {
|
2009-09-23 15:29:55 +00:00
|
|
|
bb = val;
|
2009-09-16 15:45:50 +00:00
|
|
|
if(bb.width == 0 || bb.height == 0) {
|
2009-10-29 17:19:07 +00:00
|
|
|
var newzoom = bb.zoom?bb.zoom:current_zoom * bb.factor;
|
|
|
|
canvas.setZoom(newzoom);
|
2009-09-16 15:45:50 +00:00
|
|
|
return {'zoom': current_zoom, 'bbox': bb};
|
|
|
|
}
|
2009-09-23 15:29:55 +00:00
|
|
|
return calcZoom(bb);
|
2009-09-16 15:45:50 +00:00
|
|
|
}
|
2009-09-23 15:29:55 +00:00
|
|
|
|
2009-09-16 15:45:50 +00:00
|
|
|
switch (val) {
|
2009-09-16 14:12:51 +00:00
|
|
|
case 'selection':
|
|
|
|
if(!selectedElements[0]) return;
|
2009-09-22 14:35:05 +00:00
|
|
|
var sel_elems = $.map(selectedElements, function(n){ if(n) return n; });
|
2009-09-23 15:29:55 +00:00
|
|
|
bb = canvas.getStrokedBBox(sel_elems);
|
|
|
|
break;
|
2009-09-16 14:12:51 +00:00
|
|
|
case 'canvas':
|
|
|
|
var res = canvas.getResolution();
|
2009-09-23 15:29:55 +00:00
|
|
|
spacer = .95;
|
|
|
|
bb = {width:res.w, height:res.h ,x:0, y:0};
|
|
|
|
break;
|
2009-09-16 14:12:51 +00:00
|
|
|
case 'content':
|
2009-09-23 15:29:55 +00:00
|
|
|
bb = canvas.getStrokedBBox();
|
|
|
|
break;
|
|
|
|
case 'layer':
|
|
|
|
bb = canvas.getStrokedBBox(canvas.getVisibleElements(current_layer));
|
|
|
|
break;
|
2009-09-16 14:12:51 +00:00
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2009-09-23 15:29:55 +00:00
|
|
|
return calcZoom(bb);
|
2009-09-16 14:12:51 +00:00
|
|
|
}
|
|
|
|
|
2009-09-09 14:18:24 +00:00
|
|
|
this.setZoom = function(zoomlevel) {
|
|
|
|
var res = canvas.getResolution();
|
|
|
|
svgroot.setAttribute("width", res.w * zoomlevel);
|
|
|
|
svgroot.setAttribute("height", res.h * zoomlevel);
|
|
|
|
current_zoom = zoomlevel;
|
2009-09-16 14:12:51 +00:00
|
|
|
$.each(selectedElements, function(i, elem) {
|
|
|
|
if(!elem) return;
|
|
|
|
selectorManager.requestSelector(elem).resize();
|
|
|
|
});
|
2009-10-17 20:29:56 +00:00
|
|
|
if(current_mode == "pathedit") {
|
2009-10-07 18:56:03 +00:00
|
|
|
resetPointGrips();
|
|
|
|
}
|
2009-09-09 14:18:24 +00:00
|
|
|
}
|
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getMode = function() {
|
|
|
|
return current_mode;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2009-06-01 21:24:30 +00:00
|
|
|
this.setMode = function(name) {
|
2009-10-18 20:46:32 +00:00
|
|
|
// toss out half-drawn path
|
|
|
|
if (current_mode == "path" && current_path_pts.length > 0) {
|
2009-08-14 15:50:11 +00:00
|
|
|
var elem = svgdoc.getElementById(getId());
|
|
|
|
elem.parentNode.removeChild(elem);
|
2009-10-18 20:46:32 +00:00
|
|
|
canvas.clearPath();
|
2009-08-14 15:50:11 +00:00
|
|
|
canvas.clearSelection();
|
|
|
|
started = false;
|
2009-08-07 05:44:55 +00:00
|
|
|
}
|
2009-10-17 20:29:56 +00:00
|
|
|
else if (current_mode == "pathedit") {
|
2009-10-18 20:46:32 +00:00
|
|
|
canvas.clearPath();
|
2009-08-17 03:57:43 +00:00
|
|
|
}
|
2009-08-31 14:54:59 +00:00
|
|
|
|
|
|
|
cur_properties = (selectedElements[0] && selectedElements[0].nodeName == 'text') ? cur_text : cur_shape;
|
2009-08-14 15:50:11 +00:00
|
|
|
current_mode = name;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getStrokeColor = function() {
|
2009-08-31 14:54:59 +00:00
|
|
|
return cur_properties.stroke;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2009-09-04 06:11:04 +00:00
|
|
|
this.setStrokeColor = function(val,preventUndo) {
|
2009-08-31 14:54:59 +00:00
|
|
|
cur_shape.stroke = val;
|
2009-08-31 15:59:42 +00:00
|
|
|
cur_properties.stroke_paint = {type:"solidColor"};
|
2009-09-04 06:11:04 +00:00
|
|
|
if (!preventUndo)
|
|
|
|
this.changeSelectedAttribute("stroke", val);
|
2009-09-04 06:30:18 +00:00
|
|
|
else
|
|
|
|
this.changeSelectedAttributeNoUndo("stroke", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getFillColor = function() {
|
2009-08-31 14:54:59 +00:00
|
|
|
return cur_properties.fill;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2009-09-04 06:11:04 +00:00
|
|
|
this.setFillColor = function(val,preventUndo) {
|
2009-08-31 14:54:59 +00:00
|
|
|
cur_properties.fill = val;
|
2009-08-31 15:59:42 +00:00
|
|
|
cur_properties.fill_paint = {type:"solidColor"};
|
2009-07-08 04:40:45 +00:00
|
|
|
// take out any path/line elements when setting fill
|
|
|
|
var elems = [];
|
|
|
|
var i = selectedElements.length;
|
|
|
|
while(i--) {
|
|
|
|
var elem = selectedElements[i];
|
2009-08-07 17:19:32 +00:00
|
|
|
if (elem && elem.tagName != "polyline" && elem.tagName != "line") {
|
2009-07-08 04:40:45 +00:00
|
|
|
elems.push(elem);
|
|
|
|
}
|
|
|
|
}
|
2009-09-04 06:30:18 +00:00
|
|
|
if (elems.length > 0) {
|
|
|
|
if (!preventUndo)
|
|
|
|
this.changeSelectedAttribute("fill", val, elems);
|
|
|
|
else
|
|
|
|
this.changeSelectedAttributeNoUndo("fill", val, elems);
|
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
var findDefs = function() {
|
2009-10-21 15:12:32 +00:00
|
|
|
var defs = svgcontent.getElementsByTagNameNS(svgns, "defs");
|
2009-07-30 22:45:59 +00:00
|
|
|
if (defs.length > 0) {
|
|
|
|
defs = defs[0];
|
|
|
|
}
|
|
|
|
else {
|
2009-08-28 14:39:53 +00:00
|
|
|
// first child is a comment, so call nextSibling
|
2009-10-21 15:12:32 +00:00
|
|
|
defs = svgcontent.insertBefore( svgdoc.createElementNS(svgns, "defs" ), svgcontent.firstChild.nextSibling);
|
2009-07-30 22:45:59 +00:00
|
|
|
}
|
|
|
|
return defs;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-19 17:08:05 +00:00
|
|
|
var addGradient = function() {
|
|
|
|
$.each(['stroke','fill'],function(i,type) {
|
|
|
|
|
2009-08-31 14:54:59 +00:00
|
|
|
if(!cur_properties[type + '_paint'] || cur_properties[type + '_paint'].type == "solidColor") return;
|
2009-08-19 17:08:05 +00:00
|
|
|
var grad = canvas[type + 'Grad'];
|
|
|
|
// find out if there is a duplicate gradient already in the defs
|
|
|
|
var duplicate_grad = findDuplicateGradient(grad);
|
|
|
|
var defs = findDefs();
|
|
|
|
// no duplicate found, so import gradient into defs
|
|
|
|
if (!duplicate_grad) {
|
2009-11-13 20:45:18 +00:00
|
|
|
var orig_grad = grad;
|
2009-08-19 17:08:05 +00:00
|
|
|
grad = defs.appendChild( svgdoc.importNode(grad, true) );
|
2009-11-13 20:45:18 +00:00
|
|
|
canvas.fixOperaXML(grad, orig_grad);
|
2009-08-19 17:08:05 +00:00
|
|
|
// get next id and set it on the grad
|
|
|
|
grad.id = getNextId();
|
|
|
|
}
|
|
|
|
else { // use existing gradient
|
|
|
|
grad = duplicate_grad;
|
|
|
|
}
|
|
|
|
var functype = type=='fill'?'Fill':'Stroke';
|
|
|
|
canvas['set'+ functype +'Color']("url(#" + grad.id + ")");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
var findDuplicateGradient = function(grad) {
|
|
|
|
var defs = findDefs();
|
|
|
|
var existing_grads = defs.getElementsByTagNameNS(svgns, "linearGradient");
|
|
|
|
var i = existing_grads.length;
|
|
|
|
while (i--) {
|
|
|
|
var og = existing_grads.item(i);
|
|
|
|
if (grad.getAttribute('x1') != og.getAttribute('x1') ||
|
|
|
|
grad.getAttribute('y1') != og.getAttribute('y1') ||
|
|
|
|
grad.getAttribute('x2') != og.getAttribute('x2') ||
|
|
|
|
grad.getAttribute('y2') != og.getAttribute('y2'))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
// else could be a duplicate, iterate through stops
|
|
|
|
var stops = grad.getElementsByTagNameNS(svgns, "stop");
|
|
|
|
var ostops = og.getElementsByTagNameNS(svgns, "stop");
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
if (stops.length != ostops.length) {
|
|
|
|
continue;
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
var j = stops.length;
|
|
|
|
while(j--) {
|
|
|
|
var stop = stops.item(j);
|
|
|
|
var ostop = ostops.item(j);
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
if (stop.getAttribute('offset') != ostop.getAttribute('offset') ||
|
|
|
|
stop.getAttribute('stop-opacity') != ostop.getAttribute('stop-opacity') ||
|
|
|
|
stop.getAttribute('stop-color') != ostop.getAttribute('stop-color'))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
if (j == -1) {
|
|
|
|
return og;
|
|
|
|
}
|
|
|
|
} // for each gradient in defs
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
return null;
|
|
|
|
};
|
2009-10-13 03:16:02 +00:00
|
|
|
|
|
|
|
// Group: Fill and Stroke
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-19 17:08:05 +00:00
|
|
|
this.setStrokePaint = function(p, addGrad) {
|
2009-09-04 06:11:04 +00:00
|
|
|
// make a copy
|
|
|
|
var p = new $.jGraduate.Paint(p);
|
2009-09-04 06:30:18 +00:00
|
|
|
this.setStrokeOpacity(p.alpha/100);
|
|
|
|
|
|
|
|
// now set the current paint object
|
|
|
|
cur_properties.stroke_paint = p;
|
2009-09-04 06:11:04 +00:00
|
|
|
if (p.type == "solidColor") {
|
|
|
|
this.setStrokeColor("#"+p.solidColor);
|
2009-07-30 22:45:59 +00:00
|
|
|
}
|
2009-09-04 06:11:04 +00:00
|
|
|
else if(p.type == "linearGradient") {
|
|
|
|
canvas.strokeGrad = p.linearGradient;
|
2009-08-19 17:08:05 +00:00
|
|
|
if(addGrad) addGradient();
|
2009-07-30 22:45:59 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// console.log("none!");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-08-19 17:08:05 +00:00
|
|
|
this.setFillPaint = function(p, addGrad) {
|
2009-09-04 06:11:04 +00:00
|
|
|
// make a copy
|
|
|
|
var p = new $.jGraduate.Paint(p);
|
2009-09-04 06:30:18 +00:00
|
|
|
this.setFillOpacity(p.alpha/100, true);
|
|
|
|
|
|
|
|
// now set the current paint object
|
|
|
|
cur_properties.fill_paint = p;
|
2009-09-04 06:11:04 +00:00
|
|
|
if (p.type == "solidColor") {
|
|
|
|
this.setFillColor("#"+p.solidColor);
|
2009-07-30 22:45:59 +00:00
|
|
|
}
|
2009-09-04 06:11:04 +00:00
|
|
|
else if(p.type == "linearGradient") {
|
|
|
|
canvas.fillGrad = p.linearGradient;
|
2009-08-19 17:08:05 +00:00
|
|
|
if(addGrad) addGradient();
|
2009-07-30 22:45:59 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// console.log("none!");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getStrokeWidth = function() {
|
2009-08-31 14:54:59 +00:00
|
|
|
return cur_properties.stroke_width;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2009-10-22 19:36:04 +00:00
|
|
|
// When attempting to set a line's width to 0, change it to 1 instead
|
2009-06-01 22:01:07 +00:00
|
|
|
this.setStrokeWidth = function(val) {
|
2009-10-22 19:36:04 +00:00
|
|
|
if(val == 0 && $.inArray(current_mode, ['line', 'path']) != -1) {
|
2009-09-01 19:52:34 +00:00
|
|
|
canvas.setStrokeWidth(1);
|
2009-10-22 19:36:04 +00:00
|
|
|
return;
|
2009-09-01 19:52:34 +00:00
|
|
|
}
|
2009-08-31 14:54:59 +00:00
|
|
|
cur_properties.stroke_width = val;
|
2009-06-25 02:54:07 +00:00
|
|
|
this.changeSelectedAttribute("stroke-width", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getStrokeStyle = function() {
|
2009-08-31 14:54:59 +00:00
|
|
|
return cur_shape.stroke_style;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2009-06-01 22:01:07 +00:00
|
|
|
this.setStrokeStyle = function(val) {
|
2009-08-31 14:54:59 +00:00
|
|
|
cur_shape.stroke_style = val;
|
2009-06-25 02:54:07 +00:00
|
|
|
this.changeSelectedAttribute("stroke-dasharray", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-01 22:01:07 +00:00
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getOpacity = function() {
|
2009-08-31 14:54:59 +00:00
|
|
|
return cur_shape.opacity;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2009-06-04 13:43:58 +00:00
|
|
|
this.setOpacity = function(val) {
|
2009-08-31 14:54:59 +00:00
|
|
|
cur_shape.opacity = val;
|
2009-06-25 02:54:07 +00:00
|
|
|
this.changeSelectedAttribute("opacity", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-04 13:43:58 +00:00
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getFillOpacity = function() {
|
2009-08-31 14:54:59 +00:00
|
|
|
return cur_shape.fill_opacity;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2009-09-04 06:11:04 +00:00
|
|
|
this.setFillOpacity = function(val, preventUndo) {
|
2009-08-31 14:54:59 +00:00
|
|
|
cur_shape.fill_opacity = val;
|
2009-09-04 06:11:04 +00:00
|
|
|
if (!preventUndo)
|
|
|
|
this.changeSelectedAttribute("fill-opacity", val);
|
2009-09-04 06:30:18 +00:00
|
|
|
else
|
|
|
|
this.changeSelectedAttributeNoUndo("fill-opacity", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-04 13:43:58 +00:00
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getStrokeOpacity = function() {
|
2009-08-31 14:54:59 +00:00
|
|
|
return cur_shape.stroke_opacity;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2009-09-04 06:11:04 +00:00
|
|
|
this.setStrokeOpacity = function(val, preventUndo) {
|
2009-08-31 14:54:59 +00:00
|
|
|
cur_shape.stroke_opacity = val;
|
2009-09-04 06:11:04 +00:00
|
|
|
if (!preventUndo)
|
|
|
|
this.changeSelectedAttribute("stroke-opacity", val);
|
2009-09-04 06:30:18 +00:00
|
|
|
else
|
|
|
|
this.changeSelectedAttributeNoUndo("stroke-opacity", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-10-27 20:54:29 +00:00
|
|
|
// returns an object that behaves like a SVGTransformList
|
|
|
|
this.getTransformList = function(elem) {
|
|
|
|
if (isWebkit) {
|
2009-11-02 20:09:02 +00:00
|
|
|
var t = svgTransformLists[elem.id];
|
2009-10-27 20:54:29 +00:00
|
|
|
if (!t) {
|
2009-11-02 20:09:02 +00:00
|
|
|
svgTransformLists[elem.id] = new SVGEditTransformList(elem);
|
|
|
|
t = svgTransformLists[elem.id];
|
2009-10-27 20:54:29 +00:00
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
2009-11-02 20:09:02 +00:00
|
|
|
else if (elem.transform) {
|
|
|
|
return elem.transform.baseVal;
|
|
|
|
}
|
|
|
|
return null;
|
2009-10-27 20:54:29 +00:00
|
|
|
};
|
|
|
|
|
2009-08-16 15:07:00 +00:00
|
|
|
this.getBBox = function(elem) {
|
|
|
|
var selected = elem || selectedElements[0];
|
2009-10-09 02:14:47 +00:00
|
|
|
if (elem.nodeType != 1) return null;
|
|
|
|
var ret = null;
|
2009-08-24 12:55:22 +00:00
|
|
|
if(elem.nodeName == 'text' && selected.textContent == '') {
|
2009-08-21 20:12:01 +00:00
|
|
|
selected.textContent = 'a'; // Some character needed for the selector to use.
|
2009-10-09 02:14:47 +00:00
|
|
|
ret = selected.getBBox();
|
2009-08-21 20:12:01 +00:00
|
|
|
selected.textContent = '';
|
|
|
|
} else {
|
2009-10-09 02:14:47 +00:00
|
|
|
try { ret = selected.getBBox(); }
|
|
|
|
catch(e) { ret = null; }
|
2009-08-21 20:12:01 +00:00
|
|
|
}
|
|
|
|
|
2009-08-19 04:49:50 +00:00
|
|
|
// get the bounding box from the DOM (which is in that element's coordinate system)
|
2009-08-21 20:12:01 +00:00
|
|
|
return ret;
|
2009-08-16 15:07:00 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-11-10 15:16:49 +00:00
|
|
|
// TODO: do we need to sum up all rotation angles?
|
2009-08-16 14:56:22 +00:00
|
|
|
this.getRotationAngle = function(elem) {
|
|
|
|
var selected = elem || selectedElements[0];
|
|
|
|
// find the rotation transform (if any) and set it
|
2009-11-02 20:09:02 +00:00
|
|
|
var tlist = canvas.getTransformList(selected);
|
2009-08-16 14:56:22 +00:00
|
|
|
var t = tlist.numberOfItems;
|
|
|
|
while (t--) {
|
|
|
|
var xform = tlist.getItem(t);
|
|
|
|
if (xform.type == 4) {
|
|
|
|
return xform.angle;
|
|
|
|
}
|
|
|
|
}
|
2009-08-16 19:18:22 +00:00
|
|
|
return 0;
|
2009-08-16 14:56:22 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-09-04 05:10:48 +00:00
|
|
|
this.setRotationAngle = function(val,preventUndo) {
|
2009-11-23 17:39:01 +00:00
|
|
|
// ensure val is the proper type
|
|
|
|
val = parseFloat(val);
|
2009-08-16 06:02:26 +00:00
|
|
|
var elem = selectedElements[0];
|
2009-11-23 17:39:01 +00:00
|
|
|
var oldTransform = elem.getAttribute("transform");
|
2009-08-20 05:33:34 +00:00
|
|
|
// we use the actual element's bbox (not the calculated one) since the
|
|
|
|
// calculated bbox's center can change depending on the angle
|
2009-09-04 05:10:48 +00:00
|
|
|
var bbox = elem.getBBox();
|
2009-09-17 14:47:54 +00:00
|
|
|
var cx = round(bbox.x+bbox.width/2), cy = round(bbox.y+bbox.height/2);
|
2009-11-02 20:09:02 +00:00
|
|
|
var tlist = canvas.getTransformList(elem);
|
2009-11-12 05:02:11 +00:00
|
|
|
var rotIndex = 0;
|
|
|
|
// find the index of the rotation transform
|
2009-11-10 15:16:49 +00:00
|
|
|
var n = tlist.numberOfItems;
|
|
|
|
while (n--) {
|
|
|
|
var xform = tlist.getItem(n);
|
|
|
|
if (xform.type == 4) {
|
|
|
|
rotIndex = n;
|
2009-11-12 05:02:11 +00:00
|
|
|
tlist.removeItem(n);
|
2009-11-10 15:16:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-11-02 20:09:02 +00:00
|
|
|
// if we are not rotated yet, insert a dummy xform
|
2009-11-23 16:24:01 +00:00
|
|
|
var m = transformListToTransform(tlist).matrix;
|
|
|
|
var ctm = elem.getCTM();
|
2009-11-12 05:02:11 +00:00
|
|
|
var center = transformPoint(cx,cy,m);
|
|
|
|
var newrot = svgroot.createSVGTransform();
|
2009-11-23 17:39:01 +00:00
|
|
|
newrot.setRotate(val, center.x, center.y);
|
2009-11-12 05:02:11 +00:00
|
|
|
tlist.insertItemBefore(newrot, rotIndex);
|
2009-11-23 17:39:01 +00:00
|
|
|
if (!preventUndo) {
|
|
|
|
// FIXME: we need to undo it, then redo it so it can be undo-able! :)
|
|
|
|
// TODO: figure out how to make changes to transform list undo-able cross-browser?
|
2009-11-02 20:09:02 +00:00
|
|
|
var newTransform = elem.getAttribute("transform");
|
|
|
|
elem.setAttribute("transform", oldTransform);
|
|
|
|
this.changeSelectedAttribute("transform",newTransform,selectedElements);
|
2009-09-04 05:10:48 +00:00
|
|
|
}
|
2009-10-18 20:46:32 +00:00
|
|
|
var pointGripContainer = document.getElementById("pathpointgrip_container");
|
2009-08-31 19:19:07 +00:00
|
|
|
if(elem.nodeName == "path" && pointGripContainer) {
|
2009-11-02 20:09:02 +00:00
|
|
|
setPointContainerTransform(elem.getAttribute("transform"));
|
2009-08-18 19:05:42 +00:00
|
|
|
}
|
2009-11-02 20:09:02 +00:00
|
|
|
var selector = selectorManager.requestSelector(selectedElements[0]);
|
2009-11-10 15:16:49 +00:00
|
|
|
selector.resize();
|
2009-11-02 20:09:02 +00:00
|
|
|
selector.updateGripCursors(val);
|
2009-08-16 02:48:00 +00:00
|
|
|
};
|
2009-06-04 13:43:58 +00:00
|
|
|
|
2009-06-09 22:13:13 +00:00
|
|
|
this.each = function(cb) {
|
|
|
|
$(svgroot).children().each(cb);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-09 22:13:13 +00:00
|
|
|
|
2009-06-06 18:34:42 +00:00
|
|
|
this.bind = function(event, f) {
|
2009-09-02 16:51:11 +00:00
|
|
|
var old = events[event];
|
2009-06-06 18:34:42 +00:00
|
|
|
events[event] = f;
|
2009-09-02 16:51:11 +00:00
|
|
|
return old;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 18:34:42 +00:00
|
|
|
|
2009-06-12 06:45:37 +00:00
|
|
|
this.setIdPrefix = function(p) {
|
|
|
|
idprefix = p;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-06 14:57:13 +00:00
|
|
|
this.getBold = function() {
|
|
|
|
// should only have one element selected
|
|
|
|
var selected = selectedElements[0];
|
|
|
|
if (selected != null && selected.tagName == "text" &&
|
|
|
|
selectedElements[1] == null)
|
|
|
|
{
|
|
|
|
return (selected.getAttribute("font-weight") == "bold");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-06 14:57:13 +00:00
|
|
|
this.setBold = function(b) {
|
|
|
|
var selected = selectedElements[0];
|
|
|
|
if (selected != null && selected.tagName == "text" &&
|
|
|
|
selectedElements[1] == null)
|
|
|
|
{
|
2009-07-06 16:21:12 +00:00
|
|
|
this.changeSelectedAttribute("font-weight", b ? "bold" : "normal");
|
2009-07-06 14:57:13 +00:00
|
|
|
}
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-06 14:57:13 +00:00
|
|
|
this.getItalic = function() {
|
|
|
|
var selected = selectedElements[0];
|
|
|
|
if (selected != null && selected.tagName == "text" &&
|
|
|
|
selectedElements[1] == null)
|
|
|
|
{
|
|
|
|
return (selected.getAttribute("font-style") == "italic");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-06 14:57:13 +00:00
|
|
|
this.setItalic = function(i) {
|
|
|
|
var selected = selectedElements[0];
|
|
|
|
if (selected != null && selected.tagName == "text" &&
|
|
|
|
selectedElements[1] == null)
|
|
|
|
{
|
2009-07-06 15:03:56 +00:00
|
|
|
this.changeSelectedAttribute("font-style", i ? "italic" : "normal");
|
2009-07-06 14:57:13 +00:00
|
|
|
}
|
|
|
|
};
|
2009-06-12 06:45:37 +00:00
|
|
|
|
2009-06-10 03:12:19 +00:00
|
|
|
this.getFontFamily = function() {
|
2009-08-31 14:54:59 +00:00
|
|
|
return cur_text.font_family;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-06-10 03:12:19 +00:00
|
|
|
this.setFontFamily = function(val) {
|
2009-08-31 14:54:59 +00:00
|
|
|
cur_text.font_family = val;
|
2009-06-29 14:38:30 +00:00
|
|
|
this.changeSelectedAttribute("font-family", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-10 03:12:19 +00:00
|
|
|
|
|
|
|
this.getFontSize = function() {
|
2009-08-31 14:54:59 +00:00
|
|
|
return cur_text.font_size;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-06-10 03:12:19 +00:00
|
|
|
this.setFontSize = function(val) {
|
2009-08-31 14:54:59 +00:00
|
|
|
cur_text.font_size = val;
|
2009-06-25 02:54:07 +00:00
|
|
|
this.changeSelectedAttribute("font-size", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-06-10 03:12:19 +00:00
|
|
|
this.getText = function() {
|
2009-06-29 22:22:13 +00:00
|
|
|
var selected = selectedElements[0];
|
2009-06-10 03:12:19 +00:00
|
|
|
if (selected == null) { return ""; }
|
|
|
|
return selected.textContent;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-06-10 03:12:19 +00:00
|
|
|
this.setTextContent = function(val) {
|
2009-06-25 02:54:07 +00:00
|
|
|
this.changeSelectedAttribute("#text", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-09-04 22:22:40 +00:00
|
|
|
this.setImageURL = function(val) {
|
2009-10-16 13:17:23 +00:00
|
|
|
svgCanvas.changeSelectedAttribute("#href", val);
|
2009-09-04 22:22:40 +00:00
|
|
|
};
|
|
|
|
|
2009-06-11 15:18:46 +00:00
|
|
|
this.setRectRadius = function(val) {
|
2009-06-29 22:22:13 +00:00
|
|
|
var selected = selectedElements[0];
|
2009-06-11 15:18:46 +00:00
|
|
|
if (selected != null && selected.tagName == "rect") {
|
2009-06-25 02:54:07 +00:00
|
|
|
var r = selected.getAttribute("rx");
|
|
|
|
if (r != val) {
|
|
|
|
selected.setAttribute("rx", val);
|
|
|
|
selected.setAttribute("ry", val);
|
|
|
|
addCommandToHistory(new ChangeElementCommand(selected, {"rx":r, "ry":r}, "Radius"));
|
2009-07-01 19:54:51 +00:00
|
|
|
call("changed", [selected]);
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
2009-06-11 15:18:46 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-08-28 12:37:52 +00:00
|
|
|
|
2009-10-09 15:29:40 +00:00
|
|
|
this.setSegType = function(new_type) {
|
2009-10-18 20:46:32 +00:00
|
|
|
var grip = $('#pathpointgrip_' + current_path_pt);
|
|
|
|
var old_d = current_path.getAttribute('d');
|
2009-10-09 15:29:40 +00:00
|
|
|
|
|
|
|
var index = grip[0].id.split('_')[1] - 0;
|
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var last_index = current_path_pts.length/2 - 1;
|
|
|
|
var is_closed = current_path.getAttribute('d').toLowerCase().indexOf('z') != -1;
|
2009-10-12 20:26:03 +00:00
|
|
|
|
2009-10-09 15:29:40 +00:00
|
|
|
if(!is_closed && index == last_index) {
|
2009-10-18 20:46:32 +00:00
|
|
|
return; // Last point of unclosed path should do nothing
|
2009-10-09 15:29:40 +00:00
|
|
|
} else if(index >= last_index && is_closed) {
|
|
|
|
index = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
var next_index = index+1;
|
2009-10-18 20:46:32 +00:00
|
|
|
var cur_x = getPathPoint(index)[0];
|
|
|
|
var cur_y = getPathPoint(index)[1];
|
|
|
|
var next_x = getPathPoint(next_index)[0];
|
|
|
|
var next_y = getPathPoint(next_index)[1];
|
2009-10-09 15:29:40 +00:00
|
|
|
|
|
|
|
if(!new_type) { // double-click, so just toggle
|
2009-10-18 20:46:32 +00:00
|
|
|
var batchCmd = new BatchCommand("Toggle Path Segment Type");
|
2009-10-09 15:29:40 +00:00
|
|
|
|
|
|
|
// Toggle segment to curve/straight line
|
2009-10-18 20:46:32 +00:00
|
|
|
var old_type = current_path.pathSegList.getItem(index+1).pathSegType;
|
2009-10-09 15:29:40 +00:00
|
|
|
|
2009-10-09 19:47:32 +00:00
|
|
|
new_type = (old_type == 6) ? 4 : 6;
|
2009-10-09 15:29:40 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
new_type -= 0;
|
2009-10-18 20:46:32 +00:00
|
|
|
var batchCmd = new BatchCommand("Change Path Segment Type");
|
2009-10-09 15:29:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var points;
|
2009-10-09 19:47:32 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
var bb = current_path.getBBox();
|
2009-10-09 15:29:40 +00:00
|
|
|
|
|
|
|
switch ( new_type ) {
|
2009-10-09 19:47:32 +00:00
|
|
|
case 6:
|
|
|
|
var diff_x = next_x - cur_x;
|
|
|
|
var diff_y = next_y - cur_y;
|
|
|
|
|
|
|
|
var ct1_x = cur_x + (diff_y/2);
|
|
|
|
var ct1_y = cur_y - (diff_x/2);
|
|
|
|
var ct2_x = next_x + (diff_y/2);
|
|
|
|
var ct2_y = next_y - (diff_x/2);
|
2009-10-09 15:29:40 +00:00
|
|
|
|
2009-10-09 19:47:32 +00:00
|
|
|
points = [next_x,next_y, ct1_x,ct1_y, ct2_x,ct2_y];
|
2009-10-09 15:29:40 +00:00
|
|
|
break;
|
2009-10-09 19:47:32 +00:00
|
|
|
case 4:
|
|
|
|
points = [next_x,next_y];
|
2009-10-09 15:29:40 +00:00
|
|
|
removeControlPointGrips(index);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
replacePathSeg(new_type, next_index, points);
|
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
addAllPointGripsToPath();
|
2009-11-05 15:44:03 +00:00
|
|
|
recalculateDimensions(current_path);//, current_path.getBBox());
|
2009-10-13 14:52:51 +00:00
|
|
|
updateSegLine(true);
|
2009-10-12 18:33:27 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(current_path, {d: old_d}));
|
2009-10-09 15:29:40 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
2009-10-18 20:46:32 +00:00
|
|
|
call("changed", [current_path]);
|
2009-10-09 15:29:40 +00:00
|
|
|
}
|
|
|
|
|
2009-08-28 12:37:52 +00:00
|
|
|
this.quickClone = function(elem) {
|
|
|
|
// Hack for Firefox bugs where text element features aren't updated
|
2009-09-03 17:03:07 +00:00
|
|
|
if(navigator.userAgent.indexOf('Gecko/') == -1) return elem;
|
2009-08-28 12:37:52 +00:00
|
|
|
var clone = elem.cloneNode(true)
|
|
|
|
elem.parentNode.insertBefore(clone, elem);
|
|
|
|
elem.parentNode.removeChild(elem);
|
2009-10-21 18:47:23 +00:00
|
|
|
selectorManager.releaseSelector(elem);
|
|
|
|
selectedElements[0] = clone;
|
|
|
|
selectorManager.requestSelector(clone).showGrips(true);
|
2009-08-28 12:37:52 +00:00
|
|
|
return clone;
|
|
|
|
}
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2009-09-04 05:10:48 +00:00
|
|
|
// New functions for refactoring of Undo/Redo
|
|
|
|
|
|
|
|
// this is the stack that stores the original values, the elements and
|
|
|
|
// the attribute name for begin/finish
|
|
|
|
var undoChangeStackPointer = -1;
|
|
|
|
var undoableChangeStack = [];
|
|
|
|
|
|
|
|
// This function tells the canvas to remember the old values of the
|
|
|
|
// attrName attribute for each element sent in. The elements and values
|
|
|
|
// are stored on a stack, so the next call to finishUndoableChange() will
|
|
|
|
// pop the elements and old values off the stack, gets the current values
|
|
|
|
// from the DOM and uses all of these to construct the undo-able command.
|
|
|
|
this.beginUndoableChange = function(attrName, elems) {
|
|
|
|
var p = ++undoChangeStackPointer;
|
2009-07-08 04:40:45 +00:00
|
|
|
var i = elems.length;
|
2009-09-04 05:10:48 +00:00
|
|
|
var oldValues = new Array(i), elements = new Array(i);
|
|
|
|
while (i--) {
|
|
|
|
var elem = elems[i];
|
|
|
|
if (elem == null) continue;
|
|
|
|
elements[i] = elem;
|
|
|
|
oldValues[i] = elem.getAttribute(attrName);
|
|
|
|
}
|
|
|
|
undoableChangeStack[p] = {'attrName': attrName,
|
|
|
|
'oldValues': oldValues,
|
|
|
|
'elements': elements};
|
|
|
|
};
|
|
|
|
|
2009-11-02 20:09:02 +00:00
|
|
|
// This function makes the changes to the elements
|
2009-09-04 05:10:48 +00:00
|
|
|
this.changeSelectedAttributeNoUndo = function(attr, newValue, elems) {
|
2009-07-30 22:45:59 +00:00
|
|
|
var handle = svgroot.suspendRedraw(1000);
|
2009-10-17 20:29:56 +00:00
|
|
|
if(current_mode == 'pathedit') {
|
2009-10-05 13:50:44 +00:00
|
|
|
// Editing node
|
|
|
|
var num = (attr == 'x')?0:1;
|
2009-10-18 20:46:32 +00:00
|
|
|
var old_path_pts = $.map(current_path_pts, function(n){return n/current_zoom;});
|
2009-10-05 13:50:44 +00:00
|
|
|
|
2009-10-18 20:46:32 +00:00
|
|
|
current_path_pts[current_path_pt*2 + num] = newValue-0;
|
|
|
|
current_path_pt_drag = current_path_pt;
|
|
|
|
updatePath(current_path_pts[current_path_pt*2], current_path_pts[current_path_pt*2 + 1], old_path_pts);
|
2009-10-05 13:50:44 +00:00
|
|
|
}
|
2009-09-04 06:30:18 +00:00
|
|
|
var elems = elems || selectedElements;
|
2009-09-04 05:10:48 +00:00
|
|
|
var i = elems.length;
|
|
|
|
while (i--) {
|
2009-07-08 04:40:45 +00:00
|
|
|
var elem = elems[i];
|
|
|
|
if (elem == null) continue;
|
2009-11-20 17:17:55 +00:00
|
|
|
// Set x,y vals on elements that don't have them
|
|
|
|
if((attr == 'x' || attr == 'y') && $.inArray(elem.tagName, ['g', 'polyline', 'path']) != -1) {
|
|
|
|
var bbox = canvas.getStrokedBBox([elem]);
|
|
|
|
var diff_x = attr == 'x' ? newValue - bbox.x : 0;
|
|
|
|
var diff_y = attr == 'y' ? newValue - bbox.y : 0;
|
|
|
|
canvas.moveSelectedElements(diff_x*current_zoom, diff_y*current_zoom, true);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-10-08 02:16:36 +00:00
|
|
|
// only allow the transform/opacity attribute to change on <g> elements, slightly hacky
|
|
|
|
if (elem.tagName == "g" && (attr != "transform" && attr != "opacity")) continue;
|
2009-09-04 05:10:48 +00:00
|
|
|
var oldval = attr == "#text" ? elem.textContent : elem.getAttribute(attr);
|
|
|
|
if (oldval == null) oldval = "";
|
2009-10-26 14:28:48 +00:00
|
|
|
if (oldval !== newValue) {
|
2009-08-28 12:37:52 +00:00
|
|
|
if (attr == "#text") {
|
2009-10-22 19:36:04 +00:00
|
|
|
var old_w = elem.getBBox().width;
|
2009-09-04 05:10:48 +00:00
|
|
|
elem.textContent = newValue;
|
2009-08-28 12:37:52 +00:00
|
|
|
elem = canvas.quickClone(elem);
|
2009-10-22 19:36:04 +00:00
|
|
|
|
|
|
|
// Hoped to solve the issue of moving text with text-anchor="start",
|
|
|
|
// but this doesn't actually fix it. Hopefully on the right track, though. -Fyrd
|
|
|
|
|
|
|
|
// var box=canvas.getBBox(elem), left=box.x, top=box.y, width=box.width,
|
|
|
|
// height=box.height, dx = width - old_w, dy=0;
|
|
|
|
// var angle = canvas.getRotationAngle(elem);
|
|
|
|
// if (angle) {
|
|
|
|
// var r = Math.sqrt( dx*dx + dy*dy );
|
|
|
|
// var theta = Math.atan2(dy,dx) - angle * Math.PI / 180.0;
|
|
|
|
// dx = r * Math.cos(theta);
|
|
|
|
// dy = r * Math.sin(theta);
|
|
|
|
//
|
|
|
|
// elem.setAttribute('x', elem.getAttribute('x')-dx);
|
|
|
|
// elem.setAttribute('y', elem.getAttribute('y')-dy);
|
|
|
|
// }
|
|
|
|
|
2009-09-04 22:22:40 +00:00
|
|
|
} else if (attr == "#href") {
|
2009-09-05 11:19:42 +00:00
|
|
|
elem.setAttributeNS(xlinkns, "href", newValue);
|
|
|
|
}
|
2009-09-04 05:10:48 +00:00
|
|
|
else elem.setAttribute(attr, newValue);
|
2009-08-16 15:07:00 +00:00
|
|
|
selectedBBoxes[i] = this.getBBox(elem);
|
2009-09-03 17:03:07 +00:00
|
|
|
// Use the Firefox quickClone hack for text elements with gradients or
|
|
|
|
// where other text attributes are changed.
|
2009-08-28 13:19:47 +00:00
|
|
|
if(elem.nodeName == 'text') {
|
2009-09-04 13:42:44 +00:00
|
|
|
if((newValue+'').indexOf('url') == 0 || $.inArray(attr, ['font-size','font-family','x','y']) != -1) {
|
2009-08-28 13:19:47 +00:00
|
|
|
elem = canvas.quickClone(elem);
|
|
|
|
}
|
2009-08-27 18:42:46 +00:00
|
|
|
}
|
2009-08-13 15:42:41 +00:00
|
|
|
// Timeout needed for Opera & Firefox
|
|
|
|
setTimeout(function() {
|
2009-11-04 15:30:10 +00:00
|
|
|
selectorManager.requestSelector(elem).resize();//elem.getBBox()); // TODO: remove box arg
|
2009-08-13 15:42:41 +00:00
|
|
|
},0);
|
2009-08-23 03:18:59 +00:00
|
|
|
// if this element was rotated, and we changed the position of this element
|
2009-09-04 05:10:48 +00:00
|
|
|
// we need to update the rotational transform attribute
|
2009-08-23 03:18:59 +00:00
|
|
|
var angle = canvas.getRotationAngle(elem);
|
2009-09-02 22:17:04 +00:00
|
|
|
if (angle && attr != "transform") {
|
2009-09-17 14:47:54 +00:00
|
|
|
var cx = round(selectedBBoxes[i].x + selectedBBoxes[i].width/2),
|
|
|
|
cy = round(selectedBBoxes[i].y + selectedBBoxes[i].height/2);
|
2009-08-23 03:18:59 +00:00
|
|
|
var rotate = ["rotate(", angle, " ", cx, ",", cy, ")"].join('');
|
|
|
|
if (rotate != elem.getAttribute("transform")) {
|
|
|
|
elem.setAttribute("transform", rotate);
|
|
|
|
}
|
|
|
|
}
|
2009-09-04 05:10:48 +00:00
|
|
|
} // if oldValue != newValue
|
|
|
|
} // for each elem
|
2009-10-21 15:12:32 +00:00
|
|
|
svgroot.unsuspendRedraw(handle);
|
2009-09-04 05:10:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// This function returns a BatchCommand object which summarizes the
|
|
|
|
// change since beginUndoableChange was called. The command can then
|
|
|
|
// be added to the command history
|
|
|
|
this.finishUndoableChange = function() {
|
|
|
|
var p = undoChangeStackPointer--;
|
|
|
|
var changeset = undoableChangeStack[p];
|
|
|
|
var i = changeset['elements'].length;
|
|
|
|
var attrName = changeset['attrName'];
|
|
|
|
var batchCmd = new BatchCommand("Change " + attrName);
|
|
|
|
while (i--) {
|
|
|
|
var elem = changeset['elements'][i];
|
|
|
|
if (elem == null) continue;
|
|
|
|
var changes = {};
|
|
|
|
changes[attrName] = changeset['oldValues'][i];
|
|
|
|
if (changes[attrName] != elem.getAttribute(attrName)) {
|
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(elem, changes, attrName));
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
2009-06-24 04:33:09 +00:00
|
|
|
}
|
2009-09-04 05:10:48 +00:00
|
|
|
undoableChangeStack[p] = null;
|
|
|
|
return batchCmd;
|
|
|
|
};
|
|
|
|
|
|
|
|
// If you want to change all selectedElements, ignore the elems argument.
|
|
|
|
// If you want to change only a subset of selectedElements, then send the
|
|
|
|
// subset to this function in the elems argument.
|
|
|
|
this.changeSelectedAttribute = function(attr, val, elems) {
|
|
|
|
var elems = elems || selectedElements;
|
|
|
|
canvas.beginUndoableChange(attr, elems);
|
|
|
|
var i = elems.length;
|
|
|
|
|
|
|
|
canvas.changeSelectedAttributeNoUndo(attr, val, elems);
|
|
|
|
|
|
|
|
var batchCmd = canvas.finishUndoableChange();
|
2009-07-08 04:40:45 +00:00
|
|
|
if (!batchCmd.isEmpty()) {
|
2009-09-04 05:10:48 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
2009-07-08 04:40:45 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-06-07 00:17:21 +00:00
|
|
|
$(container).mouseup(mouseUp);
|
|
|
|
$(container).mousedown(mouseDown);
|
|
|
|
$(container).mousemove(mouseMove);
|
2009-06-01 17:35:22 +00:00
|
|
|
|
2009-07-01 20:22:29 +00:00
|
|
|
this.deleteSelectedElements = function() {
|
2009-07-01 21:28:22 +00:00
|
|
|
var batchCmd = new BatchCommand("Delete Elements");
|
2009-07-03 02:49:42 +00:00
|
|
|
var len = selectedElements.length;
|
2009-09-05 22:09:42 +00:00
|
|
|
var selectedCopy = []; //selectedElements is being deleted
|
2009-07-03 02:49:42 +00:00
|
|
|
for (var i = 0; i < len; ++i) {
|
2009-07-01 20:22:29 +00:00
|
|
|
var selected = selectedElements[i];
|
|
|
|
if (selected == null) break;
|
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
var parent = selected.parentNode;
|
2009-06-10 04:44:59 +00:00
|
|
|
var t = selected;
|
2009-07-01 20:22:29 +00:00
|
|
|
// this will unselect the element and remove the selectedOutline
|
|
|
|
selectorManager.releaseSelector(t);
|
2009-06-25 02:54:07 +00:00
|
|
|
var elem = parent.removeChild(t);
|
2009-09-02 18:22:35 +00:00
|
|
|
selectedCopy.push(selected) //for the copy
|
2009-07-01 20:22:29 +00:00
|
|
|
selectedElements[i] = null;
|
2009-07-01 21:28:22 +00:00
|
|
|
batchCmd.addSubCommand(new RemoveElementCommand(elem, parent));
|
2009-06-10 04:44:59 +00:00
|
|
|
}
|
2009-07-01 21:28:22 +00:00
|
|
|
if (!batchCmd.isEmpty()) addCommandToHistory(batchCmd);
|
2009-09-22 18:29:15 +00:00
|
|
|
call("changed", selectedCopy);
|
|
|
|
canvas.clearSelection();
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-09-05 22:09:42 +00:00
|
|
|
|
|
|
|
this.groupSelectedElements = function() {
|
|
|
|
var batchCmd = new BatchCommand("Group Elements");
|
|
|
|
|
|
|
|
// create and insert the group element
|
|
|
|
var g = addSvgElementFromJson({
|
|
|
|
"element": "g",
|
|
|
|
"attr": {
|
|
|
|
"id": getNextId()
|
|
|
|
}
|
|
|
|
});
|
|
|
|
batchCmd.addSubCommand(new InsertElementCommand(g));
|
|
|
|
|
|
|
|
// now move all children into the group
|
|
|
|
var i = selectedElements.length;
|
|
|
|
while (i--) {
|
|
|
|
var elem = selectedElements[i];
|
2009-09-06 23:25:12 +00:00
|
|
|
if (elem == null) continue;
|
2009-09-05 22:09:42 +00:00
|
|
|
var oldNextSibling = elem.nextSibling;
|
|
|
|
var oldParent = elem.parentNode;
|
|
|
|
g.appendChild(elem);
|
|
|
|
batchCmd.addSubCommand(new MoveElementCommand(elem, oldNextSibling, oldParent));
|
|
|
|
}
|
|
|
|
if (!batchCmd.isEmpty()) addCommandToHistory(batchCmd);
|
|
|
|
|
|
|
|
// ensure selectors are at bottom and update selection
|
|
|
|
selectorManager.update();
|
|
|
|
canvas.clearSelection();
|
2009-11-12 20:15:42 +00:00
|
|
|
canvas.addToSelection([g], true);
|
2009-09-05 22:09:42 +00:00
|
|
|
};
|
|
|
|
|
2009-09-06 13:00:05 +00:00
|
|
|
// TODO: when transferring group's rotational transform to the children, must deal
|
2009-09-23 16:18:04 +00:00
|
|
|
// with children who are already rotated within the group (Issue 204)
|
2009-09-05 22:09:42 +00:00
|
|
|
this.ungroupSelectedElement = function() {
|
|
|
|
var g = selectedElements[0];
|
|
|
|
if (g.tagName == "g") {
|
|
|
|
var batchCmd = new BatchCommand("Ungroup Elements");
|
|
|
|
var parent = g.parentNode;
|
|
|
|
var anchor = g.previousSibling;
|
|
|
|
var children = new Array(g.childNodes.length);
|
2009-09-06 03:51:16 +00:00
|
|
|
var xform = g.getAttribute("transform");
|
2009-09-05 22:09:42 +00:00
|
|
|
var i = 0;
|
2009-09-06 13:00:05 +00:00
|
|
|
var gbox = g.getBBox(),
|
|
|
|
gx = gbox.x + gbox.width/2,
|
|
|
|
gy = gbox.y + gbox.height/2;
|
2009-09-30 20:11:08 +00:00
|
|
|
var gangle = canvas.getRotationAngle(g) * Math.PI / 180.0;
|
2009-09-05 22:09:42 +00:00
|
|
|
while (g.firstChild) {
|
|
|
|
var elem = g.firstChild;
|
|
|
|
var oldNextSibling = elem.nextSibling;
|
|
|
|
var oldParent = elem.parentNode;
|
|
|
|
children[i++] = elem = parent.insertBefore(elem, anchor);
|
2009-09-06 03:51:16 +00:00
|
|
|
batchCmd.addSubCommand(new MoveElementCommand(elem, oldNextSibling, oldParent));
|
|
|
|
if (xform) {
|
2009-09-06 13:00:05 +00:00
|
|
|
var childBox = elem.getBBox();
|
|
|
|
var cx = childBox.x + childBox.width/2,
|
|
|
|
cy = childBox.y + childBox.height/2,
|
|
|
|
dx = cx - gx,
|
|
|
|
dy = cy - gy,
|
|
|
|
r = Math.sqrt(dx*dx + dy*dy);
|
2009-09-30 20:11:08 +00:00
|
|
|
var tangle = gangle + Math.atan2(dy,dx);
|
|
|
|
var newcx = r * Math.cos(tangle) + gx,
|
|
|
|
newcy = r * Math.sin(tangle) + gy;
|
2009-09-06 13:00:05 +00:00
|
|
|
childBox.x += (newcx - cx);
|
|
|
|
childBox.y += (newcy - cy);
|
2009-09-30 20:11:08 +00:00
|
|
|
// now we add the angle that the element was rotated by
|
|
|
|
// if it's non-zero, we need to set the new transform
|
|
|
|
// otherwise, we clear it
|
|
|
|
var angle = gangle + canvas.getRotationAngle(elem) * Math.PI / 180.0;
|
|
|
|
var changes = {};
|
|
|
|
changes["transform"] = elem.getAttribute("transform");
|
|
|
|
if (angle != 0) {
|
|
|
|
elem.setAttribute("transform", "rotate(" + (angle*180.0)/Math.PI + " " + cx + "," + cy + ")");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
elem.setAttribute("transform", "");
|
|
|
|
}
|
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(elem, changes));
|
2009-11-05 15:44:03 +00:00
|
|
|
batchCmd.addSubCommand(recalculateDimensions(elem));//, childBox));
|
2009-09-06 03:51:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove transform and make it undo-able
|
|
|
|
if (xform) {
|
|
|
|
var changes = {};
|
2009-09-06 13:00:05 +00:00
|
|
|
changes["transform"] = xform;
|
|
|
|
g.setAttribute("transform", "");
|
|
|
|
g.removeAttribute("transform");
|
2009-09-06 03:51:16 +00:00
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(g, changes));
|
2009-09-05 22:09:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove the group from the selection
|
|
|
|
canvas.clearSelection();
|
|
|
|
|
|
|
|
// delete the group element (but make undo-able)
|
|
|
|
g = parent.removeChild(g);
|
|
|
|
batchCmd.addSubCommand(new RemoveElementCommand(g, parent));
|
|
|
|
|
|
|
|
if (!batchCmd.isEmpty()) addCommandToHistory(batchCmd);
|
|
|
|
|
|
|
|
// ensure selectors are at bottom and update selection
|
|
|
|
selectorManager.update();
|
|
|
|
canvas.addToSelection(children);
|
|
|
|
}
|
|
|
|
};
|
2009-06-10 04:44:59 +00:00
|
|
|
|
2009-06-17 20:08:53 +00:00
|
|
|
this.moveToTopSelectedElement = function() {
|
2009-06-29 22:22:13 +00:00
|
|
|
var selected = selectedElements[0];
|
2009-06-17 20:08:53 +00:00
|
|
|
if (selected != null) {
|
|
|
|
var t = selected;
|
2009-06-25 02:54:07 +00:00
|
|
|
var oldParent = t.parentNode;
|
|
|
|
var oldNextSibling = t.nextSibling;
|
2009-06-29 13:20:39 +00:00
|
|
|
if (oldNextSibling == selectorManager.selectorParentGroup) oldNextSibling = null;
|
2009-06-25 02:54:07 +00:00
|
|
|
t = t.parentNode.appendChild(t);
|
|
|
|
addCommandToHistory(new MoveElementCommand(t, oldNextSibling, oldParent, "top"));
|
2009-06-17 20:08:53 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-17 20:08:53 +00:00
|
|
|
|
|
|
|
this.moveToBottomSelectedElement = function() {
|
2009-06-29 22:22:13 +00:00
|
|
|
var selected = selectedElements[0];
|
2009-06-17 20:08:53 +00:00
|
|
|
if (selected != null) {
|
|
|
|
var t = selected;
|
2009-06-25 02:54:07 +00:00
|
|
|
var oldParent = t.parentNode;
|
|
|
|
var oldNextSibling = t.nextSibling;
|
2009-06-29 13:20:39 +00:00
|
|
|
if (oldNextSibling == selectorManager.selectorParentGroup) oldNextSibling = null;
|
2009-08-28 14:39:53 +00:00
|
|
|
// first child is a comment, so call nextSibling
|
|
|
|
var firstChild = t.parentNode.firstChild.nextSibling;
|
2009-07-30 22:45:59 +00:00
|
|
|
if (firstChild.tagName == 'defs') {
|
|
|
|
firstChild = firstChild.nextSibling;
|
|
|
|
}
|
|
|
|
t = t.parentNode.insertBefore(t, firstChild);
|
2009-06-25 02:54:07 +00:00
|
|
|
addCommandToHistory(new MoveElementCommand(t, oldNextSibling, oldParent, "bottom"));
|
2009-06-17 20:08:53 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2009-07-13 13:51:00 +00:00
|
|
|
this.moveSelectedElements = function(dx,dy,undoable) {
|
|
|
|
// if undoable is not sent, default to true
|
2009-09-13 04:09:33 +00:00
|
|
|
// if single values, scale them to the zoom
|
|
|
|
if (dx.constructor != Array) {
|
2009-10-30 19:39:33 +00:00
|
|
|
dx /= current_zoom;
|
|
|
|
dy /= current_zoom;
|
2009-09-13 04:09:33 +00:00
|
|
|
}
|
2009-08-08 03:36:01 +00:00
|
|
|
var undoable = undoable || true;
|
2009-07-08 00:03:08 +00:00
|
|
|
var batchCmd = new BatchCommand("position");
|
|
|
|
var i = selectedElements.length;
|
|
|
|
while (i--) {
|
|
|
|
var selected = selectedElements[i];
|
|
|
|
if (selected != null) {
|
2009-08-16 15:07:00 +00:00
|
|
|
selectedBBoxes[i] = this.getBBox(selected);
|
2009-11-12 18:38:13 +00:00
|
|
|
|
|
|
|
var xform = svgroot.createSVGTransform();
|
|
|
|
var tlist = canvas.getTransformList(selected);
|
|
|
|
|
2009-08-23 17:16:27 +00:00
|
|
|
// dx and dy could be arrays
|
|
|
|
if (dx.constructor == Array) {
|
2009-10-28 18:50:12 +00:00
|
|
|
selectedBBoxes[i].x += dx[i];
|
|
|
|
selectedBBoxes[i].y += dy[i];
|
2009-11-12 18:38:13 +00:00
|
|
|
xform.setTranslate(dx[i],dy[i]);
|
2009-08-23 17:16:27 +00:00
|
|
|
} else {
|
2009-11-12 18:38:13 +00:00
|
|
|
selectedBBoxes[i].x += dx;
|
2009-08-23 17:16:27 +00:00
|
|
|
selectedBBoxes[i].y += dy;
|
2009-11-12 18:38:13 +00:00
|
|
|
xform.setTranslate(dx,dy);
|
2009-08-23 17:16:27 +00:00
|
|
|
}
|
2009-11-12 18:38:13 +00:00
|
|
|
|
|
|
|
tlist.appendItem(xform);
|
|
|
|
|
2009-11-05 15:44:03 +00:00
|
|
|
var cmd = recalculateDimensions(selected);//,selectedBBoxes[i]);
|
2009-07-08 00:03:08 +00:00
|
|
|
if (cmd) {
|
|
|
|
batchCmd.addSubCommand(cmd);
|
|
|
|
}
|
2009-11-04 15:30:10 +00:00
|
|
|
selectorManager.requestSelector(selected).resize();//selectedBBoxes[i]);
|
2009-07-08 00:03:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!batchCmd.isEmpty()) {
|
2009-07-13 13:51:00 +00:00
|
|
|
if (undoable)
|
|
|
|
addCommandToHistory(batchCmd);
|
2009-07-08 00:03:08 +00:00
|
|
|
call("changed", selectedElements);
|
2009-06-23 21:18:23 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-17 20:08:53 +00:00
|
|
|
|
2009-09-22 14:35:05 +00:00
|
|
|
this.getStrokedBBox = function(elems) {
|
|
|
|
if(!elems) elems = canvas.getVisibleElements();
|
2009-09-22 17:22:28 +00:00
|
|
|
if(!elems.length) return false;
|
2009-11-02 17:11:37 +00:00
|
|
|
|
|
|
|
// Make sure the expected BBox is returned if the element is a group
|
|
|
|
var getCheckedBBox = function(elem) {
|
|
|
|
if(elem.tagName == 'g') {
|
|
|
|
return canvas.getStrokedBBox(elem.childNodes);
|
|
|
|
} else {
|
2009-11-02 19:18:37 +00:00
|
|
|
try {
|
2009-11-09 20:54:42 +00:00
|
|
|
var bb = elem.getBBox();
|
|
|
|
var angle = canvas.getRotationAngle(elem) * Math.PI / 180.0;
|
|
|
|
if (angle) {
|
|
|
|
var rminx = Number.MAX_VALUE, rminy = Number.MAX_VALUE,
|
|
|
|
rmaxx = Number.MIN_VALUE, rmaxy = Number.MIN_VALUE;
|
|
|
|
var cx = round(bb.x + bb.width/2),
|
|
|
|
cy = round(bb.y + bb.height/2);
|
|
|
|
var pts = [ [bb.x - cx, bb.y - cy],
|
|
|
|
[bb.x + bb.width - cx, bb.y - cy],
|
|
|
|
[bb.x + bb.width - cx, bb.y + bb.height - cy],
|
|
|
|
[bb.x - cx, bb.y + bb.height - cy] ];
|
|
|
|
var j = 4;
|
|
|
|
while (j--) {
|
|
|
|
var x = pts[j][0],
|
|
|
|
y = pts[j][1],
|
|
|
|
r = Math.sqrt( x*x + y*y );
|
|
|
|
var theta = Math.atan2(y,x) + angle;
|
|
|
|
x = round(r * Math.cos(theta) + cx);
|
|
|
|
y = round(r * Math.sin(theta) + cy);
|
|
|
|
|
|
|
|
// now set the bbox for the shape after it's been rotated
|
|
|
|
if (x < rminx) rminx = x;
|
|
|
|
if (y < rminy) rminy = y;
|
|
|
|
if (x > rmaxx) rmaxx = x;
|
|
|
|
if (y > rmaxy) rmaxy = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
bb.x = rminx;
|
|
|
|
bb.y = rminy;
|
|
|
|
bb.width = rmaxx - rminx;
|
|
|
|
bb.height = rmaxy - rminy;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bb;
|
2009-11-02 19:18:37 +00:00
|
|
|
} catch(e) { return null; }
|
2009-11-02 17:11:37 +00:00
|
|
|
}
|
|
|
|
}
|
2009-11-02 19:18:37 +00:00
|
|
|
var full_bb;
|
|
|
|
$.each(elems, function() {
|
|
|
|
if(full_bb) return;
|
|
|
|
full_bb = getCheckedBBox(this);
|
|
|
|
});
|
|
|
|
|
|
|
|
if(elems.length == 1) return full_bb;
|
|
|
|
|
2009-09-22 14:35:05 +00:00
|
|
|
var max_x = full_bb.x + full_bb.width;
|
|
|
|
var max_y = full_bb.y + full_bb.height;
|
|
|
|
var min_x = full_bb.x;
|
|
|
|
var min_y = full_bb.y;
|
2009-11-02 17:11:37 +00:00
|
|
|
|
2009-09-22 14:35:05 +00:00
|
|
|
var getOffset = function(elem) {
|
2009-10-28 19:20:56 +00:00
|
|
|
var sw = elem.getAttribute("stroke-width");
|
2009-09-22 14:35:05 +00:00
|
|
|
var offset = 0;
|
|
|
|
if (elem.getAttribute("stroke") != "none" && !isNaN(sw)) {
|
|
|
|
offset += sw/2;
|
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
$.each(elems, function(i, elem) {
|
2009-11-02 17:11:37 +00:00
|
|
|
var cur_bb = getCheckedBBox(elem);
|
2009-11-02 19:18:37 +00:00
|
|
|
if(!cur_bb) return;
|
2009-09-22 14:35:05 +00:00
|
|
|
var offset = getOffset(elem);
|
|
|
|
min_x = Math.min(min_x, cur_bb.x - offset);
|
|
|
|
min_y = Math.min(min_y, cur_bb.y - offset);
|
|
|
|
});
|
|
|
|
|
|
|
|
full_bb.x = min_x;
|
|
|
|
full_bb.y = min_y;
|
|
|
|
|
|
|
|
$.each(elems, function(i, elem) {
|
2009-11-02 17:11:37 +00:00
|
|
|
var cur_bb = getCheckedBBox(elem);
|
2009-11-02 19:18:37 +00:00
|
|
|
if(!cur_bb) return;
|
2009-09-22 14:35:05 +00:00
|
|
|
var offset = getOffset(elem);
|
|
|
|
max_x = Math.max(max_x, cur_bb.x + cur_bb.width + offset);
|
|
|
|
max_y = Math.max(max_y, cur_bb.y + cur_bb.height + offset);
|
|
|
|
});
|
|
|
|
|
|
|
|
full_bb.width = max_x - min_x;
|
|
|
|
full_bb.height = max_y - min_y;
|
|
|
|
return full_bb;
|
|
|
|
}
|
|
|
|
|
2009-09-23 15:29:55 +00:00
|
|
|
this.getVisibleElements = function(parent, includeBBox) {
|
2009-11-02 19:18:37 +00:00
|
|
|
if(!parent) parent = $(svgcontent).children(); // Prevent layers from being included
|
2009-08-28 19:20:54 +00:00
|
|
|
|
2009-10-28 19:20:56 +00:00
|
|
|
var contentElems = [];
|
2009-11-02 19:18:37 +00:00
|
|
|
$(parent).children().each(function(i, elem) {
|
2009-08-28 19:20:54 +00:00
|
|
|
try {
|
2009-11-02 19:18:37 +00:00
|
|
|
var box = elem.getBBox();
|
2009-09-09 14:18:24 +00:00
|
|
|
if (box) {
|
2009-11-02 19:18:37 +00:00
|
|
|
var item = includeBBox?{'elem':elem, 'bbox':canvas.getStrokedBBox([elem])}:elem;
|
2009-08-28 19:20:54 +00:00
|
|
|
contentElems.push(item);
|
|
|
|
}
|
|
|
|
} catch(e) {}
|
2009-10-28 19:20:56 +00:00
|
|
|
});
|
2009-11-02 13:14:59 +00:00
|
|
|
return contentElems.reverse();
|
2009-08-28 19:20:54 +00:00
|
|
|
}
|
|
|
|
|
2009-08-21 13:39:23 +00:00
|
|
|
this.cycleElement = function(next) {
|
|
|
|
var cur_elem = selectedElements[0];
|
|
|
|
var elem = false;
|
2009-09-29 03:23:52 +00:00
|
|
|
var all_elems = this.getVisibleElements(current_layer);
|
2009-08-21 13:39:23 +00:00
|
|
|
if (cur_elem == null) {
|
2009-08-28 19:20:54 +00:00
|
|
|
var num = next?all_elems.length-1:0;
|
|
|
|
elem = all_elems[num];
|
2009-08-21 13:39:23 +00:00
|
|
|
} else {
|
2009-08-28 19:20:54 +00:00
|
|
|
var i = all_elems.length;
|
|
|
|
while(i--) {
|
|
|
|
if(all_elems[i] == cur_elem) {
|
|
|
|
var num = next?i-1:i+1;
|
2009-08-31 17:03:33 +00:00
|
|
|
if(num >= all_elems.length) {
|
|
|
|
num = 0;
|
|
|
|
} else if(num < 0) {
|
|
|
|
num = all_elems.length-1;
|
|
|
|
}
|
|
|
|
elem = all_elems[num];
|
|
|
|
break;
|
2009-08-28 19:20:54 +00:00
|
|
|
}
|
2009-08-21 13:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
canvas.clearSelection();
|
|
|
|
canvas.addToSelection([elem], true);
|
|
|
|
call("selected", selectedElements);
|
|
|
|
}
|
|
|
|
|
2009-07-02 03:54:11 +00:00
|
|
|
var resetUndoStack = function() {
|
|
|
|
undoStack = [];
|
|
|
|
undoStackPointer = 0;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
|
|
|
this.getUndoStackSize = function() { return undoStackPointer; };
|
2009-07-06 18:54:38 +00:00
|
|
|
this.getRedoStackSize = function() { return undoStack.length - undoStackPointer; };
|
|
|
|
|
|
|
|
this.getNextUndoCommandText = function() {
|
|
|
|
if (undoStackPointer > 0)
|
|
|
|
return undoStack[undoStackPointer-1].text;
|
|
|
|
return "";
|
|
|
|
};
|
|
|
|
this.getNextRedoCommandText = function() {
|
|
|
|
if (undoStackPointer < undoStack.length)
|
|
|
|
return undoStack[undoStackPointer].text;
|
|
|
|
return "";
|
|
|
|
};
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
this.undo = function() {
|
2009-06-29 14:38:30 +00:00
|
|
|
if (undoStackPointer > 0) {
|
2009-07-01 15:34:15 +00:00
|
|
|
this.clearSelection();
|
2009-10-18 20:46:32 +00:00
|
|
|
removeAllPointGripsFromPath();
|
2009-06-25 15:35:28 +00:00
|
|
|
var cmd = undoStack[--undoStackPointer];
|
|
|
|
cmd.unapply();
|
2009-07-01 21:28:22 +00:00
|
|
|
call("changed", cmd.elements());
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
2009-07-06 18:54:38 +00:00
|
|
|
};
|
2009-06-25 02:54:07 +00:00
|
|
|
this.redo = function() {
|
|
|
|
if (undoStackPointer < undoStack.length && undoStack.length > 0) {
|
2009-07-01 15:34:15 +00:00
|
|
|
this.clearSelection();
|
2009-06-25 15:35:28 +00:00
|
|
|
var cmd = undoStack[undoStackPointer++];
|
|
|
|
cmd.apply();
|
2009-07-01 21:28:22 +00:00
|
|
|
call("changed", cmd.elements());
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-10-09 05:34:01 +00:00
|
|
|
// this function no longer uses cloneNode because we need to update the id
|
|
|
|
// of every copied element (even the descendants)
|
|
|
|
// we also do it manually because Opera/Win/non-EN puts , instead of .
|
|
|
|
var copyElem = function(el) {
|
|
|
|
// manually create a copy of the element
|
|
|
|
var new_el = document.createElementNS(svgns, el.nodeName);
|
|
|
|
$.each(el.attributes, function(i, attr) {
|
2009-10-13 05:41:32 +00:00
|
|
|
var ns = attr.nodeName == 'href' ? xlinkns :
|
|
|
|
attr.prefix == "xml" ? xmlns : null;
|
2009-10-09 05:34:01 +00:00
|
|
|
new_el.setAttributeNS(ns, attr.nodeName, attr.nodeValue);
|
|
|
|
});
|
|
|
|
// set the copied element's new id
|
|
|
|
new_el.removeAttribute("id");
|
|
|
|
new_el.id = getNextId();
|
|
|
|
// manually increment obj_num because our cloned elements are not in the DOM yet
|
|
|
|
obj_num++;
|
|
|
|
|
|
|
|
// now create copies of all children
|
|
|
|
$.each(el.childNodes, function(i, child) {
|
|
|
|
switch(child.nodeType) {
|
2009-09-23 13:43:13 +00:00
|
|
|
case 1: // element node
|
|
|
|
new_el.appendChild(copyElem(child));
|
|
|
|
break;
|
|
|
|
case 3: // text node
|
|
|
|
new_el.textContent = child.nodeValue;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2009-10-09 05:34:01 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return new_el;
|
|
|
|
};
|
2009-09-23 13:43:13 +00:00
|
|
|
|
2009-10-09 05:34:01 +00:00
|
|
|
// this creates deep DOM copies (clones) of all selected elements
|
|
|
|
this.cloneSelectedElements = function() {
|
2009-08-06 11:43:19 +00:00
|
|
|
var batchCmd = new BatchCommand("Clone Elements");
|
2009-09-01 19:47:25 +00:00
|
|
|
// find all the elements selected (stop at first null)
|
|
|
|
var len = selectedElements.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
2009-08-29 18:38:34 +00:00
|
|
|
var elem = selectedElements[i];
|
|
|
|
if (elem == null) break;
|
2009-07-13 13:51:00 +00:00
|
|
|
}
|
2009-09-01 19:47:25 +00:00
|
|
|
// use slice to quickly get the subset of elements we need
|
|
|
|
var copiedElements = selectedElements.slice(0,i);
|
2009-07-13 13:51:00 +00:00
|
|
|
this.clearSelection();
|
2009-09-01 19:47:25 +00:00
|
|
|
// note that we loop in the reverse way because of the way elements are added
|
|
|
|
// to the selectedElements array (top-first)
|
|
|
|
var i = copiedElements.length;
|
|
|
|
while (i--) {
|
|
|
|
// clone each element and replace it within copiedElements
|
2009-09-23 13:43:13 +00:00
|
|
|
var elem = copiedElements[i] = copyElem(copiedElements[i]);
|
2009-09-22 18:58:52 +00:00
|
|
|
current_layer.appendChild(elem);
|
2009-07-13 13:51:00 +00:00
|
|
|
batchCmd.addSubCommand(new InsertElementCommand(elem));
|
|
|
|
}
|
2009-11-02 14:29:12 +00:00
|
|
|
|
2009-07-13 13:51:00 +00:00
|
|
|
if (!batchCmd.isEmpty()) {
|
2009-11-02 14:29:12 +00:00
|
|
|
this.addToSelection(copiedElements.reverse()); // Need to reverse for correct selection-adding
|
2009-07-13 13:51:00 +00:00
|
|
|
this.moveSelectedElements(20,20,false);
|
|
|
|
addCommandToHistory(batchCmd);
|
|
|
|
call("selected", selectedElements);
|
2009-08-17 06:56:55 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// aligns selected elements (type is a char - see switch below for explanation)
|
2009-08-25 07:04:19 +00:00
|
|
|
// relative_to can be "selected", "largest", "smallest", "page"
|
|
|
|
this.alignSelectedElements = function(type, relative_to) {
|
2009-08-24 15:30:11 +00:00
|
|
|
var bboxes = [], angles = [];
|
2009-08-23 17:16:27 +00:00
|
|
|
var minx = Number.MAX_VALUE, maxx = Number.MIN_VALUE, miny = Number.MAX_VALUE, maxy = Number.MIN_VALUE;
|
2009-08-25 07:04:19 +00:00
|
|
|
var curwidth = Number.MIN_VALUE, curheight = Number.MIN_VALUE;
|
2009-08-17 06:56:55 +00:00
|
|
|
var len = selectedElements.length;
|
|
|
|
if (!len) return;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
if (selectedElements[i] == null) break;
|
|
|
|
var elem = selectedElements[i];
|
2009-11-12 18:38:13 +00:00
|
|
|
bboxes[i] = canvas.getStrokedBBox([elem]);
|
2009-08-24 15:30:11 +00:00
|
|
|
|
|
|
|
// now bbox is axis-aligned and handles rotation
|
2009-08-25 07:04:19 +00:00
|
|
|
switch (relative_to) {
|
|
|
|
case 'smallest':
|
|
|
|
if ( (type == 'l' || type == 'c' || type == 'r') && (curwidth == Number.MIN_VALUE || curwidth > bboxes[i].width) ||
|
|
|
|
(type == 't' || type == 'm' || type == 'b') && (curheight == Number.MIN_VALUE || curheight > bboxes[i].height) ) {
|
|
|
|
minx = bboxes[i].x;
|
|
|
|
miny = bboxes[i].y;
|
|
|
|
maxx = bboxes[i].x + bboxes[i].width;
|
|
|
|
maxy = bboxes[i].y + bboxes[i].height;
|
|
|
|
curwidth = bboxes[i].width;
|
|
|
|
curheight = bboxes[i].height;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'largest':
|
|
|
|
if ( (type == 'l' || type == 'c' || type == 'r') && (curwidth == Number.MIN_VALUE || curwidth < bboxes[i].width) ||
|
|
|
|
(type == 't' || type == 'm' || type == 'b') && (curheight == Number.MIN_VALUE || curheight < bboxes[i].height) ) {
|
|
|
|
minx = bboxes[i].x;
|
|
|
|
miny = bboxes[i].y;
|
|
|
|
maxx = bboxes[i].x + bboxes[i].width;
|
|
|
|
maxy = bboxes[i].y + bboxes[i].height;
|
|
|
|
curwidth = bboxes[i].width;
|
|
|
|
curheight = bboxes[i].height;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: // 'selected'
|
|
|
|
if (bboxes[i].x < minx) minx = bboxes[i].x;
|
|
|
|
if (bboxes[i].y < miny) miny = bboxes[i].y;
|
|
|
|
if (bboxes[i].x + bboxes[i].width > maxx) maxx = bboxes[i].x + bboxes[i].width;
|
|
|
|
if (bboxes[i].y + bboxes[i].height > maxy) maxy = bboxes[i].y + bboxes[i].height;
|
|
|
|
break;
|
|
|
|
}
|
2009-08-24 15:30:11 +00:00
|
|
|
} // loop for each element to find the bbox and adjust min/max
|
2009-08-25 07:04:19 +00:00
|
|
|
|
|
|
|
if (relative_to == 'page') {
|
|
|
|
minx = 0;
|
|
|
|
miny = 0;
|
|
|
|
maxx = svgroot.getAttribute('width');
|
|
|
|
maxy = svgroot.getAttribute('height');
|
|
|
|
}
|
|
|
|
|
2009-08-23 17:16:27 +00:00
|
|
|
var dx = new Array(len);
|
|
|
|
var dy = new Array(len);
|
2009-08-17 06:56:55 +00:00
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
if (selectedElements[i] == null) break;
|
|
|
|
var elem = selectedElements[i];
|
2009-08-24 15:30:11 +00:00
|
|
|
var bbox = bboxes[i];
|
2009-08-23 17:16:27 +00:00
|
|
|
dx[i] = 0;
|
|
|
|
dy[i] = 0;
|
|
|
|
switch (type) {
|
|
|
|
case 'l': // left (horizontal)
|
|
|
|
dx[i] = minx - bbox.x;
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
2009-08-23 17:16:27 +00:00
|
|
|
case 'c': // center (horizontal)
|
|
|
|
dx[i] = (minx+maxx)/2 - (bbox.x + bbox.width/2);
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
2009-08-23 17:16:27 +00:00
|
|
|
case 'r': // right (horizontal)
|
|
|
|
dx[i] = maxx - (bbox.x + bbox.width);
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
2009-08-23 17:16:27 +00:00
|
|
|
case 't': // top (vertical)
|
|
|
|
dy[i] = miny - bbox.y;
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
2009-08-23 17:16:27 +00:00
|
|
|
case 'm': // middle (vertical)
|
|
|
|
dy[i] = (miny+maxy)/2 - (bbox.y + bbox.height/2);
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
2009-08-23 17:16:27 +00:00
|
|
|
case 'b': // bottom (vertical)
|
|
|
|
dy[i] = maxy - (bbox.y + bbox.height);
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-08-23 17:16:27 +00:00
|
|
|
this.moveSelectedElements(dx,dy);
|
2009-07-13 13:51:00 +00:00
|
|
|
};
|
2009-09-17 14:47:54 +00:00
|
|
|
this.getZoom = function(){return current_zoom;};
|
2009-09-21 02:05:58 +00:00
|
|
|
|
2009-10-14 16:57:31 +00:00
|
|
|
// Function: getVersion
|
|
|
|
// Returns a string which describes the revision number of SvgCanvas.
|
|
|
|
this.getVersion = function() {
|
|
|
|
return "svgcanvas.js ($Rev$)";
|
|
|
|
};
|
|
|
|
|
2009-09-21 02:05:58 +00:00
|
|
|
this.clear();
|
|
|
|
};
|
2009-06-08 08:09:15 +00:00
|
|
|
|
|
|
|
// Static class for various utility functions
|
|
|
|
|
2009-06-08 09:08:45 +00:00
|
|
|
var Utils = {
|
2009-06-08 08:09:15 +00:00
|
|
|
|
|
|
|
// This code was written by Tyler Akins and has been placed in the
|
|
|
|
// public domain. It would be nice if you left this header intact.
|
|
|
|
// Base64 code from Tyler Akins -- http://rumkin.com
|
|
|
|
|
2009-06-23 07:48:15 +00:00
|
|
|
// schiller: Removed string concatenation in favour of Array.join() optimization,
|
2009-06-17 03:20:42 +00:00
|
|
|
// also precalculate the size of the array needed.
|
2009-06-16 18:51:35 +00:00
|
|
|
|
2009-06-12 13:27:42 +00:00
|
|
|
"_keyStr" : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
|
2009-06-08 08:09:15 +00:00
|
|
|
|
2009-06-12 13:27:42 +00:00
|
|
|
"encode64" : function(input) {
|
2009-06-17 03:20:42 +00:00
|
|
|
// base64 strings are 4/3 larger than the original string
|
2009-10-14 18:49:35 +00:00
|
|
|
// input = Utils.encodeUTF8(input); // convert non-ASCII characters
|
|
|
|
input = Utils.convertToXMLReferences(input);
|
2009-10-19 19:06:07 +00:00
|
|
|
if(window.btoa) return window.btoa(input); // Use native if available
|
2009-06-17 19:00:23 +00:00
|
|
|
var output = new Array( Math.floor( (input.length + 2) / 3 ) * 4 );
|
2009-06-08 09:08:45 +00:00
|
|
|
var chr1, chr2, chr3;
|
|
|
|
var enc1, enc2, enc3, enc4;
|
2009-06-17 03:20:42 +00:00
|
|
|
var i = 0, p = 0;
|
2009-06-08 08:09:15 +00:00
|
|
|
|
2009-06-08 09:08:45 +00:00
|
|
|
do {
|
|
|
|
chr1 = input.charCodeAt(i++);
|
|
|
|
chr2 = input.charCodeAt(i++);
|
|
|
|
chr3 = input.charCodeAt(i++);
|
2009-06-08 08:09:15 +00:00
|
|
|
|
2009-06-08 09:08:45 +00:00
|
|
|
enc1 = chr1 >> 2;
|
|
|
|
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
|
|
|
|
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
|
|
|
|
enc4 = chr3 & 63;
|
2009-06-08 08:09:15 +00:00
|
|
|
|
2009-06-08 09:08:45 +00:00
|
|
|
if (isNaN(chr2)) {
|
|
|
|
enc3 = enc4 = 64;
|
|
|
|
} else if (isNaN(chr3)) {
|
|
|
|
enc4 = 64;
|
|
|
|
}
|
2009-06-08 08:09:15 +00:00
|
|
|
|
2009-06-17 03:20:42 +00:00
|
|
|
output[p++] = this._keyStr.charAt(enc1);
|
|
|
|
output[p++] = this._keyStr.charAt(enc2);
|
|
|
|
output[p++] = this._keyStr.charAt(enc3);
|
|
|
|
output[p++] = this._keyStr.charAt(enc4);
|
2009-06-08 09:08:45 +00:00
|
|
|
} while (i < input.length);
|
2009-06-08 08:09:15 +00:00
|
|
|
|
2009-06-16 18:51:35 +00:00
|
|
|
return output.join('');
|
2009-07-02 04:50:24 +00:00
|
|
|
},
|
2009-10-14 18:49:35 +00:00
|
|
|
|
|
|
|
// based on http://phpjs.org/functions/utf8_encode:577
|
|
|
|
// codedread:does not seem to work with webkit-based browsers on OSX
|
|
|
|
"encodeUTF8": function(input) {
|
|
|
|
//return unescape(encodeURIComponent(input)); //may or may not work
|
|
|
|
var output = '';
|
|
|
|
for (var n = 0; n < input.length; n++){
|
|
|
|
var c = input.charCodeAt(n);
|
|
|
|
if (c < 128) {
|
|
|
|
output += input[n];
|
|
|
|
}
|
|
|
|
else if (c > 127) {
|
|
|
|
if (c < 2048){
|
|
|
|
output += String.fromCharCode((c >> 6) | 192);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
output += String.fromCharCode((c >> 12) | 224) + String.fromCharCode((c >> 6) & 63 | 128);
|
|
|
|
}
|
|
|
|
output += String.fromCharCode((c & 63) | 128);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
},
|
|
|
|
|
|
|
|
"convertToXMLReferences": function(input) {
|
|
|
|
var output = '';
|
|
|
|
for (var n = 0; n < input.length; n++){
|
|
|
|
var c = input.charCodeAt(n);
|
|
|
|
if (c < 128) {
|
|
|
|
output += input[n];
|
|
|
|
}
|
|
|
|
else if(c > 127) {
|
|
|
|
output += ("&#" + c + ";");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
},
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-02 04:50:24 +00:00
|
|
|
"rectsIntersect": function(r1, r2) {
|
|
|
|
return r2.x < (r1.x+r1.width) &&
|
|
|
|
(r2.x+r2.width) > r1.x &&
|
|
|
|
r2.y < (r1.y+r1.height) &&
|
|
|
|
(r2.y+r2.height) > r1.y;
|
2009-07-10 05:04:06 +00:00
|
|
|
},
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
// found this function http://groups.google.com/group/jquery-dev/browse_thread/thread/c6d11387c580a77f
|
|
|
|
"text2xml": function(sXML) {
|
2009-08-17 06:56:55 +00:00
|
|
|
// NOTE: I'd like to use jQuery for this, but jQuery makes all tags uppercase
|
|
|
|
//return $(xml)[0];
|
|
|
|
var out;
|
|
|
|
try{
|
|
|
|
var dXML = ($.browser.msie)?new ActiveXObject("Microsoft.XMLDOM"):new DOMParser();
|
|
|
|
dXML.async = false;
|
|
|
|
} catch(e){
|
|
|
|
throw new Error("XML Parser could not be instantiated");
|
|
|
|
};
|
|
|
|
try{
|
|
|
|
if($.browser.msie) out = (dXML.loadXML(sXML))?dXML:false;
|
|
|
|
else out = dXML.parseFromString(sXML, "text/xml");
|
|
|
|
}
|
|
|
|
catch(e){ throw new Error("Error parsing XML string"); };
|
|
|
|
return out;
|
|
|
|
}
|
2009-07-02 04:50:24 +00:00
|
|
|
};
|