2012-09-30 17:51:50 -04:00
|
|
|
/*
|
2013-01-28 21:03:27 -05:00
|
|
|
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
|
2012-11-06 16:07:18 -05:00
|
|
|
* http://paperjs.org/
|
|
|
|
*
|
2015-12-27 12:09:25 -05:00
|
|
|
* Copyright (c) 2011 - 2016, Juerg Lehni & Jonathan Puckey
|
2014-01-03 19:47:16 -05:00
|
|
|
* http://scratchdisk.com/ & http://jonathanpuckey.com/
|
2012-11-06 16:07:18 -05:00
|
|
|
*
|
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2016-02-01 06:50:22 -05:00
|
|
|
QUnit.module('SvgImport');
|
2012-09-30 17:51:50 -04:00
|
|
|
|
2015-07-27 04:29:50 -04:00
|
|
|
test('Import SVG line', function() {
|
|
|
|
var attrs = {
|
|
|
|
x1: 5,
|
|
|
|
x2: 45,
|
|
|
|
y1: 5,
|
|
|
|
y2: 45
|
|
|
|
};
|
|
|
|
var imported = paper.project.importSVG(createSVG('line', attrs));
|
|
|
|
var path = new Path.Line([attrs.x1, attrs.y1], [attrs.x2, attrs.y2]);
|
|
|
|
equals(imported, path);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Import SVG rect', function() {
|
|
|
|
var attrs = {
|
|
|
|
x: 25,
|
|
|
|
y: 25,
|
|
|
|
width: 100,
|
|
|
|
height: 100
|
|
|
|
};
|
|
|
|
var imported = paper.project.importSVG(createSVG('rect', attrs),
|
|
|
|
{ expandShapes: true });
|
|
|
|
var path = new Path.Rectangle(attrs);
|
|
|
|
equals(imported, path);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Import SVG round rect', function() {
|
|
|
|
var attrs = {
|
|
|
|
x: 25,
|
|
|
|
y: 25,
|
|
|
|
rx: 50,
|
|
|
|
ry: 50,
|
|
|
|
width: 100,
|
|
|
|
height: 100
|
|
|
|
};
|
|
|
|
var imported = paper.project.importSVG(createSVG('rect', attrs),
|
|
|
|
{ expandShapes: true });
|
|
|
|
var path = new Path.Rectangle(new Rectangle(attrs),
|
|
|
|
new Size(attrs.rx, attrs.ry));
|
|
|
|
equals(imported, path);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Import SVG ellipse', function() {
|
|
|
|
var attrs = {
|
|
|
|
cx: 300,
|
|
|
|
cy: 80,
|
|
|
|
rx: 100,
|
|
|
|
ry: 50
|
2016-01-27 13:57:07 -05:00
|
|
|
};
|
2015-07-27 04:29:50 -04:00
|
|
|
var imported = paper.project.importSVG(createSVG('ellipse', attrs),
|
|
|
|
{ expandShapes: true });
|
|
|
|
var path = new Path.Ellipse({
|
|
|
|
center: new Point(attrs.cx, attrs.cy),
|
|
|
|
radius: new Point(attrs.rx, attrs.ry)
|
2014-08-16 13:24:54 -04:00
|
|
|
});
|
2015-07-27 04:29:50 -04:00
|
|
|
equals(imported, path);
|
2012-09-30 17:51:50 -04:00
|
|
|
});
|
|
|
|
|
2015-07-27 04:29:50 -04:00
|
|
|
test('Import SVG circle', function() {
|
|
|
|
var attrs = {
|
|
|
|
cx: 100,
|
|
|
|
cy: 80,
|
|
|
|
r: 50
|
2016-01-27 13:57:07 -05:00
|
|
|
};
|
2015-07-27 04:29:50 -04:00
|
|
|
var imported = paper.project.importSVG(createSVG('circle', attrs),
|
|
|
|
{ expandShapes: true });
|
|
|
|
var path = new Path.Circle({
|
|
|
|
center: new Point(attrs.cx, attrs.cy),
|
|
|
|
radius: attrs.r
|
2014-08-16 13:24:54 -04:00
|
|
|
});
|
2015-07-27 04:29:50 -04:00
|
|
|
equals(imported, path);
|
2012-09-30 17:51:50 -04:00
|
|
|
});
|
|
|
|
|
2015-07-27 04:29:50 -04:00
|
|
|
function createPolyPath(str) {
|
|
|
|
var points = str.split(' ').map(function(point) {
|
|
|
|
return point.split(',').map(parseFloat);
|
2014-08-16 13:24:54 -04:00
|
|
|
});
|
2015-07-27 04:29:50 -04:00
|
|
|
var path = new Path();
|
|
|
|
path.moveTo(points[0]);
|
|
|
|
for (var i = 1; i < points.length; i++)
|
|
|
|
path.lineTo(points[i]);
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
test('Import SVG polygon', function() {
|
|
|
|
var points = '100,10 40,180 190,60 10,60 160,180';
|
|
|
|
var imported = paper.project.importSVG(createSVG('polygon', {
|
|
|
|
points: points
|
|
|
|
}));
|
|
|
|
var path = createPolyPath(points);
|
|
|
|
path.closePath();
|
|
|
|
equals(imported, path);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Import SVG polyline', function() {
|
2015-07-27 05:42:41 -04:00
|
|
|
var points = '5,5 45,45 5,45 45,5';
|
2015-07-27 04:29:50 -04:00
|
|
|
var imported = paper.project.importSVG(createSVG('polyline', {
|
|
|
|
points: points
|
|
|
|
}));
|
|
|
|
var path = createPolyPath(points);
|
|
|
|
equals(imported, path);
|
2012-09-30 17:51:50 -04:00
|
|
|
});
|
2015-07-27 05:42:41 -04:00
|
|
|
|
|
|
|
test('Import complex CompoundPath and clone', function() {
|
2016-02-14 16:51:50 -05:00
|
|
|
var svg = '<svg xmlns="http://www.w3.org/2000/svg"><path fill="red" d="M4,14h20v-2H4V14z M15,26h7v-2h-7V26z M15,22h9v-2h-9V22z M15,18h9v-2h-9V18z M4,26h9V16H4V26z M28,10V6H0v22c0,0,0,4,4,4 h25c0,0,3-0.062,3-4V10H28z M4,30c-2,0-2-2-2-2V8h24v20c0,0.921,0.284,1.558,0.676,2H4z"/></svg>';
|
2016-01-27 20:21:34 -05:00
|
|
|
var item = paper.project.importSVG(svg);
|
2015-12-26 15:46:36 -05:00
|
|
|
equals(item.clone(), item, null, { cloned: true });
|
2015-07-27 05:42:41 -04:00
|
|
|
});
|
2016-02-01 14:15:37 -05:00
|
|
|
|
2016-02-14 16:51:50 -05:00
|
|
|
test('Import SVG without insertion', function() {
|
|
|
|
var svg = createSVG('path', { d: '' });
|
|
|
|
var imported = paper.project.importSVG(svg, { insert: true });
|
|
|
|
equals(function() {
|
|
|
|
return imported.parent === project.activeLayer;
|
|
|
|
}, true);
|
|
|
|
var imported = paper.project.importSVG(svg, { insert: false });
|
|
|
|
equals(function() {
|
|
|
|
return imported.parent === null;
|
|
|
|
}, true);
|
|
|
|
});
|
|
|
|
|
2016-02-11 06:51:48 -05:00
|
|
|
function importSVG(assert, url, message, options) {
|
|
|
|
var done = assert.async();
|
|
|
|
project.importSVG(url, {
|
|
|
|
onLoad: function(item, svg) {
|
2016-02-14 08:52:37 -05:00
|
|
|
if (!message) {
|
|
|
|
message = 'The imported SVG "' + url + '" should visually be '
|
|
|
|
+ 'the same as the rasterized original SVG data.';
|
2016-02-11 06:51:48 -05:00
|
|
|
}
|
|
|
|
compareSVG(done, item, svg, message, options);
|
|
|
|
},
|
|
|
|
onError: function(error) {
|
2016-02-14 08:52:37 -05:00
|
|
|
var ok = !!(options && options.expectError);
|
|
|
|
QUnit.push(ok, false, !ok, ok && message
|
2016-02-19 18:19:36 -05:00
|
|
|
|| 'Loading SVG from a valid URL should not give an error: ' +
|
|
|
|
error);
|
2016-02-11 06:51:48 -05:00
|
|
|
done();
|
|
|
|
}
|
2016-02-01 14:15:37 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-02-11 06:51:48 -05:00
|
|
|
if (!isNode) {
|
|
|
|
// JSDom does not have SVG rendering, so we can't test there.
|
|
|
|
var svgFiles = ['viewbox', 'clipping', 'gradients-1'];
|
|
|
|
// TODO: Investigate why Phantom struggles with this file:
|
|
|
|
if (!isPhantom)
|
|
|
|
svgFiles.push('gradients-2');
|
|
|
|
svgFiles.forEach(function(name) {
|
|
|
|
name += '.svg';
|
|
|
|
test('Import ' + name, function(assert) {
|
|
|
|
importSVG(assert, 'assets/' + name);
|
2016-02-01 14:15:37 -05:00
|
|
|
});
|
2016-02-11 06:51:48 -05:00
|
|
|
});
|
2016-02-14 08:52:37 -05:00
|
|
|
|
|
|
|
test('Import inexistent file', function(assert) {
|
|
|
|
importSVG(assert, 'assets/inexistent.svg',
|
|
|
|
'Load an inexistent SVG file should trigger an error',
|
|
|
|
{ expectError: true });
|
|
|
|
});
|
2016-02-01 14:15:37 -05:00
|
|
|
}
|