Merge remote-tracking branch 'origin/master'

This commit is contained in:
Jürg Lehni 2011-05-21 00:59:19 +01:00
commit 9f1172f24a
8 changed files with 253 additions and 11 deletions

View file

@ -33,8 +33,9 @@ var Item = this.Item = Base.extend({
},
_clone: function(copy) {
// Copy over style
copy.setStyle(this._style);
// If this item has a pathStyle, copy it:
if (this._style)
copy.setStyle(this._style);
// If this item has children, clone and append each of them:
if (this._children) {
for (var i = 0, l = this._children.length; i < l; i++)

View file

@ -28,7 +28,7 @@ var PlacedSymbol = this.PlacedSymbol = Item.extend({
},
clone: function() {
return this._clone(new PlacedSymbol(symbol, this.matrix.clone()));
return this._clone(new PlacedSymbol(this.symbol, this.matrix.clone()));
},
_transform: function(matrix, flags) {

View file

@ -97,6 +97,7 @@ if (window.tests) {
'test/tests/Color.js',
'test/tests/Project.js',
'test/tests/Item.js',
'test/tests/Item_Cloning.js',
'test/tests/Layer.js',
'test/tests/Group.js',
'test/tests/Segment.js',

View file

@ -23,7 +23,7 @@ var ParagraphStyle = this.ParagraphStyle = Base.extend({
statics: {
create: function(item) {
var style = new CharacterStyle(CharacterStyle.dont);
var style = new ParagraphStyle(ParagraphStyle.dont);
style._item = item;
return style;
}

View file

@ -21,18 +21,15 @@ var PointText = this.PointText = TextItem.extend({
this.base();
var point = Point.read(arguments);
this.content = '';
// TODO: Since we're exposing matrix, we actually need to extract _point
// from it each time getPoint is called, as it could be modified other
// than through PointText#transform().
this._point = LinkedPoint.create(this, 'setPoint', point.x, point.y);
this.matrix = new Matrix().translate(point);
this._matrix = new Matrix().translate(point);
},
clone: function() {
var copy = this._clone(new PointText(this._point));
copy.content = this.content;
// Use Matrix#initialize to easily copy over values.
copy.matrix.initialize(this.matrix);
copy._matrix.initialize(this._matrix);
return copy;
},
@ -56,7 +53,7 @@ var PointText = this.PointText = TextItem.extend({
},
_transform: function(matrix, flags) {
this.matrix.preConcatenate(matrix);
this._matrix.preConcatenate(matrix);
// We need to transform the LinkedPoint, passing true for dontNotify so
// chaning it won't trigger calls of setPoint(), leading to an endless
// recursion.
@ -70,7 +67,7 @@ var PointText = this.PointText = TextItem.extend({
ctx.font = this._characterStyle.fontSize + 'pt ' +
this._characterStyle.font;
ctx.textAlign = this._paragraphStyle.justification;
this.matrix.applyToContext(ctx);
this._matrix.applyToContext(ctx);
var fillColor = this.getFillColor();
var strokeColor = this.getStrokeColor();

View file

@ -26,6 +26,12 @@ var TextItem = this.TextItem = Item.extend({
this.setParagraphStyle();
},
_clone: function(copy) {
copy.setCharacterStyle(this._characterStyle);
copy.setParagraphStyle(this._paragraphStyle);
return this.base(copy);
},
getCharacterStyle: function() {
return this._characterStyle;
},

View file

@ -86,4 +86,146 @@ function compareGrayColors(color1, color2, message) {
compareNumbers(color1.gray, color2.gray,
(message || '') + ' gray');
}
function cloneAndCompare(item) {
var copy = item.clone();
compareItems(item, copy);
// Remove the cloned item to restore the document:
copy.remove();
}
function compareItems(item, item2) {
equals(function() {
return item != item2;
}, true);
var itemProperties = ['opacity', 'locked', 'visible', 'blendMode', 'name',
'closed', 'selected'];
Base.each(itemProperties, function(key) {
equals(function() {
return item[key] == item2[key];
}, true, 'item[\'' + key + '\'] == item2[\'' + key + '\']');
});
equals(function() {
return item.id != item2.id;
}, true);
if (item._matrix) {
equals(function() {
return item._matrix != item2._matrix;
}, true);
equals(item._matrix.toString(), item2._matrix.toString(),
'item._matrix.toString() == item2._matrix.toString()');
}
if (item2.segments) {
equals(item.segments.toString(), item2.segments.toString(),
'item.segments.toString() == item2.segments.toString()');
}
// Path specific
if (item instanceof PathItem) {
equals(function() {
return item._clockwise == item2._clockwise;
}, true);
}
// Group specific
if (item instanceof Group) {
equals(function() {
return item._clipped == item2._clipped;
}, true);
}
// Layer specific
if (item instanceof Layer) {
equals(function() {
return item.project == item2.project;
}, true);
}
// PlacedSymbol specific
if (item instanceof PlacedSymbol) {
equals(function() {
return item.symbol == item2.symbol;
}, true);
}
// TODO: Raster specific
// TextItem specific:
if (item instanceof TextItem) {
equals(item.content, item2.content, 'item.content == item2.content');
var characterStyleKeys = ['fontSize', 'font'];
Base.each(characterStyleKeys, function(key) {
equals(function() {
return item2.characterStyle[key];
}, item.characterStyle[key], 'item.characterStyle[\'' + key
+ '\'] == item2.characterStyle[\'' + key + '\']');
});
var paragraphStyleKeys = ['justification'];
Base.each(paragraphStyleKeys, function(key) {
equals(function() {
return item2.paragraphStyle[key];
}, item.paragraphStyle[key], 'item.paragraphStyle[\'' + key
+ '\'] == item2.paragraphStyle[\'' + key + '\']');
});
}
// PointText specific:
if (item instanceof PointText) {
equals(item.point.toString(), item2.point.toString());
}
if (item._style) {
// Path Style
if (item.fillColor) {
// The fillColor should not point to the same color object:
equals(function() {
return item.fillColor != item2.fillColor;
}, true, 'The fillColor should not point to the same color object:');
if (item.fillColor instanceof GradientColor) {
// TODO!
} else {
equals(item.fillColor.toString(), item2.fillColor.toString());
}
}
if (item.strokeColor) {
equals(function() {
return item.strokeColor != item2.strokeColor;
}, true, 'The strokeColor should not point to the same color object:');
if (item.strokeColor instanceof GradientColor) {
// TODO
} else {
equals(item.strokeColor.toString(), item2.strokeColor.toString());
}
}
Base.each(['strokeCap', 'strokeJoin', 'dashOffset', 'miterLimit',
'strokeOverprint', 'fillOverprint'], function(key) {
if (item[key]) {
equals(function() {
return item[key] == item2[key];
}, true, 'item[\'' + key + '\'] == item2[\'' + key + '\']');
}
});
if (item.dashArray) {
equals(item.dashArray.toString(), item2.dashArray.toString(),
'item.dashArray.toString(), item2.dashArray.toString()');
}
}
// Check length of children and recursively compare them:
if (item.children) {
equals(function() {
return item.children.length == item2.children.length;
}, true);
for (var i = 0, l = item.children.length; i < l; i++) {
compareItems(item.children[i], item2.children[i]);
}
}
}

View file

@ -0,0 +1,95 @@
test('Path#clone()', function() {
var proj = paper.project;
var path = new Path([10, 20], [30, 40]);
path.closed = true;
path.name = 'test';
path.style = {
strokeCap: 'round',
strokeJoin: 'round',
dashOffset: 10,
dashArray: [10, 2, 10],
fillColor: new RGBColor(0, 0, 1),
strokeColor: new RGBColor(0, 0, 1),
miterLimit: 5
};
path.clockwise = false;
path.opacity = 0.5;
path.locked = true;
path.visible = false;
path.blendMode = 'blend';
path._clipMask = true;
path.selected = true;
cloneAndCompare(path);
});
test('CompoundPath#clone()', function() {
var path1 = new Path.Rectangle([200, 200], [100, 100]);
var path2 = new Path.Rectangle([50, 50], [200, 200]);
var compound = new CompoundPath(path1, path2);
cloneAndCompare(compound);
});
test('Layer#clone()', function() {
var path = new Path.Rectangle([200, 200], [100, 100]);
cloneAndCompare(paper.project.activeLayer);
});
test('Group#clone()', function() {
var path = new Path.Circle([150, 150], 60);
path.style = {
strokeCap: 'round',
strokeJoin: 'round',
dashOffset: 10,
dashArray: [10, 2, 10],
fillColor: new RGBColor(0, 0, 1),
strokeColor: new RGBColor(0, 0, 1),
miterLimit: 5
};
var secondPath = new Path.Circle([175, 175], 85);
var group = new Group([path, secondPath]);
cloneAndCompare(group);
});
test('PointText#clone()', function() {
var pointText = new PointText(new Point(50, 50));
pointText.content = 'test';
pointText.position += 100;
pointText.characterStyle = {
font: 'serif',
fontSize: 20
};
pointText.paragraphStyle.justification = 'center';
cloneAndCompare(pointText);
});
test('PlacedSymbol#clone()', function() {
var path = new Path.Circle([150, 150], 60);
var symbol = new Symbol(path);
var placedSymbol = new PlacedSymbol(symbol);
placedSymbol.position = [100, 100];
placedSymbol.rotate(90);
cloneAndCompare(placedSymbol);
});
test('Symbol#clone()', function() {
var path = new Path.Circle([150, 150], 60);
path.style = {
strokeCap: 'round',
strokeJoin: 'round',
dashOffset: 10,
dashArray: [10, 2, 10],
fillColor: new RGBColor(0, 0, 1),
strokeColor: new RGBColor(0, 0, 1),
miterLimit: 5
};
path.selected = true;
var symbol = new Symbol(path);
var copy = symbol.clone();
compareItems(symbol.definition, copy.definition);
equals(function() {
return symbol.project == copy.project;
}, true);
equals(function() {
return paper.project.symbols.length == 2;
}, true);
});