2011-02-17 22:58:56 +00:00
|
|
|
CompoundPath = PathItem.extend(new function() {
|
|
|
|
|
2011-02-17 14:33:25 +01:00
|
|
|
function getCurrentPath(compoundPath) {
|
|
|
|
if (compoundPath.children.length) {
|
|
|
|
return compoundPath.children[compoundPath.children.length - 1];
|
|
|
|
} else {
|
|
|
|
throw Error('Use a moveTo() command first');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-17 22:58:56 +00:00
|
|
|
var fields = {
|
2011-02-17 13:36:40 +01:00
|
|
|
initialize: function(items) {
|
|
|
|
this.base();
|
|
|
|
this.children = [];
|
|
|
|
if (items) {
|
|
|
|
for (var i = 0, l = items.length; i < l; i++) {
|
|
|
|
this.appendTop(items[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2011-02-17 14:33:25 +01:00
|
|
|
|
2011-02-24 17:11:02 +01:00
|
|
|
// TODO: have getBounds of Group / Layer / CompoundPath use the same
|
|
|
|
// code (from a utility script?)
|
|
|
|
getBounds: function() {
|
|
|
|
if (this.children.length) {
|
|
|
|
var rect = this.children[0].bounds;
|
|
|
|
var x1 = rect.x;
|
|
|
|
var y1 = rect.y;
|
|
|
|
var x2 = rect.x + rect.width;
|
|
|
|
var y2 = rect.y + rect.height;
|
|
|
|
for (var i = 1, l = this.children.length; i < l; i++) {
|
|
|
|
var rect2 = this.children[i].bounds;
|
|
|
|
x1 = Math.min(rect2.x, x1);
|
|
|
|
y1 = Math.min(rect2.y, y1);
|
|
|
|
x2 = Math.max(rect2.x + rect2.width, x1 + x2 - x1);
|
|
|
|
y2 = Math.max(rect2.y + rect2.height, y1 + y2 - y1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new Rectangle(x1, y1, x2 - x1, y2 - y1);
|
|
|
|
},
|
2011-02-21 01:16:53 +01:00
|
|
|
|
2011-02-17 14:33:25 +01:00
|
|
|
/**
|
|
|
|
* If this is a compound path with only one path inside,
|
|
|
|
* the path is moved outside and the compound path is erased.
|
|
|
|
* Otherwise, the compound path is returned unmodified.
|
|
|
|
*
|
|
|
|
* @return the simplified compound path.
|
|
|
|
*/
|
|
|
|
simplify: function() {
|
2011-02-21 03:32:39 +01:00
|
|
|
if (this.children.length == 1) {
|
2011-02-17 14:33:25 +01:00
|
|
|
var child = this.children[0];
|
|
|
|
child.moveAbove(this);
|
|
|
|
this.remove();
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
smooth: function() {
|
2011-02-17 16:00:03 +01:00
|
|
|
for (var i = 0, l = this.children.length; i < l; i++) {
|
2011-02-17 14:33:25 +01:00
|
|
|
this.children[i].smooth();
|
|
|
|
}
|
|
|
|
},
|
2011-02-17 22:58:56 +00:00
|
|
|
|
2011-02-17 13:36:40 +01:00
|
|
|
moveTo: function() {
|
|
|
|
var path = new Path();
|
|
|
|
this.appendTop(path);
|
|
|
|
path.moveTo.apply(path, arguments);
|
|
|
|
},
|
2011-02-17 22:58:56 +00:00
|
|
|
|
2011-02-17 13:36:40 +01:00
|
|
|
moveBy: function() {
|
2011-02-24 17:13:41 +01:00
|
|
|
var point = arguments.length ? Point.read(arguments) : new Point();
|
|
|
|
var path = getCurrentPath(this);
|
|
|
|
var current = path.segments[path.segments.length - 1].point;
|
|
|
|
this.moveTo(current.add(point));
|
2011-02-17 23:01:18 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
closePath: function() {
|
|
|
|
var path = getCurrentPath();
|
|
|
|
path.setClosed(true);
|
2011-02-17 13:36:40 +01:00
|
|
|
}
|
2011-02-17 22:58:56 +00:00
|
|
|
};
|
2011-02-17 23:01:18 +00:00
|
|
|
|
2011-02-17 23:34:03 +00:00
|
|
|
Base.each(['lineTo', 'cubicCurveTo', 'quadraticCurveTo', 'curveTo',
|
|
|
|
'arcTo', 'lineBy', 'curveBy', 'arcBy'], function(key) {
|
|
|
|
fields[key] = function() {
|
2011-02-17 22:58:56 +00:00
|
|
|
var path = getCurrentPath(this);
|
2011-02-17 23:34:03 +00:00
|
|
|
path[key].apply(path, arguments);
|
2011-02-17 13:36:40 +01:00
|
|
|
};
|
2011-02-17 23:34:03 +00:00
|
|
|
});
|
2011-02-17 13:36:40 +01:00
|
|
|
|
2011-02-17 22:58:56 +00:00
|
|
|
return fields;
|
2011-02-19 14:12:05 +01:00
|
|
|
});
|