2012-09-30 17:51:50 -04:00
|
|
|
/*
|
2012-11-02 20:47:14 -04:00
|
|
|
* Paper.js
|
|
|
|
*
|
|
|
|
* This file is part of Paper.js, a JavaScript Vector Graphics Library,
|
|
|
|
* based on Scriptographer.org and designed to be largely API compatible.
|
|
|
|
* http://paperjs.org/
|
|
|
|
* http://scriptographer.org/
|
|
|
|
*
|
|
|
|
* Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
|
|
|
|
* http://lehni.org/ & http://jonathanpuckey.com/
|
|
|
|
*
|
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
2012-11-02 21:14:20 -04:00
|
|
|
* The base for this code was donated by Stetson-Team-Alpha.
|
2012-11-02 20:47:14 -04:00
|
|
|
*/
|
2012-09-30 17:51:50 -04:00
|
|
|
|
2012-09-13 20:45:27 -04:00
|
|
|
/**
|
2012-11-02 20:47:14 -04:00
|
|
|
* @name SvgImporter
|
2012-11-05 21:58:16 -05:00
|
|
|
*
|
2012-11-02 20:47:14 -04:00
|
|
|
* @class The SvgImporter object represents an object created using the SVG
|
|
|
|
* Canvas that will be converted into a Paper.js object.
|
|
|
|
* The SVG object is imported into Paper.js by converting it into items
|
|
|
|
* within groups.
|
|
|
|
*
|
|
|
|
*/
|
2012-11-02 21:40:41 -04:00
|
|
|
var SvgImporter = this.SvgImporter = new function() {
|
2012-09-30 17:51:50 -04:00
|
|
|
|
2012-11-02 22:16:23 -04:00
|
|
|
// Define a couple of helper functions to easily read values from SVG
|
|
|
|
// objects, dealing with baseVal, and item lists.
|
|
|
|
// index is option, and if passed, causes a lookup in a list.
|
|
|
|
|
|
|
|
function getValue(svg, key, index) {
|
|
|
|
var base = svg[key].baseVal;
|
|
|
|
return index !== undefined
|
|
|
|
? index < base.numberOfItems ? base.getItem(index).value || 0 : 0
|
|
|
|
: base.value || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPoint(svg, x, y, index) {
|
|
|
|
return Point.create(getValue(svg, x, index), getValue(svg, y, index));
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSize(svg, w, h, index) {
|
|
|
|
return Size.create(getValue(svg, w, index), getValue(svg, h, index));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Define importer functions for various SVG node types
|
|
|
|
|
2012-11-06 12:14:17 -05:00
|
|
|
function createGroupImporter(type) {
|
|
|
|
return function(svg) {
|
|
|
|
var items = [],
|
|
|
|
nodes = svg.childNodes;
|
|
|
|
for (var i = 0, l = nodes.length; i < l; i++) {
|
|
|
|
var child = nodes[i];
|
|
|
|
if (child.nodeType == 1) {
|
|
|
|
var item = SvgImporter.importSvg(child);
|
|
|
|
if (item) {
|
|
|
|
var parent = item.getParent();
|
|
|
|
if (parent && !(parent instanceof Layer))
|
|
|
|
item = parent;
|
|
|
|
items.push(item);
|
|
|
|
}
|
|
|
|
}
|
2012-11-02 19:51:42 -04:00
|
|
|
}
|
2012-11-06 12:14:17 -05:00
|
|
|
return new type(items);
|
2012-09-30 17:51:50 -04:00
|
|
|
}
|
2012-11-02 21:40:41 -04:00
|
|
|
}
|
2012-09-30 17:51:50 -04:00
|
|
|
|
2012-11-06 12:14:17 -05:00
|
|
|
var importGroup = createGroupImporter(Group);
|
|
|
|
var importCompoundPath = createGroupImporter(CompoundPath);
|
|
|
|
|
2012-11-05 21:27:13 -05:00
|
|
|
function importPoly(svg, type) {
|
2012-11-03 01:53:33 -04:00
|
|
|
var path = new Path(),
|
2012-11-02 22:04:29 -04:00
|
|
|
points = svg.points,
|
|
|
|
start = points.getItem(0);
|
2012-11-03 01:53:33 -04:00
|
|
|
path.moveTo(start);
|
2012-11-02 22:10:58 -04:00
|
|
|
for (var i = 1, l = points.numberOfItems; i < l; i++)
|
2012-11-03 01:53:33 -04:00
|
|
|
path.lineTo(points.getItem(i));
|
2012-11-05 21:27:13 -05:00
|
|
|
if (type === 'polygon')
|
2012-11-03 01:53:33 -04:00
|
|
|
path.closePath();
|
|
|
|
return path;
|
2012-11-02 21:40:41 -04:00
|
|
|
}
|
2012-09-30 17:51:50 -04:00
|
|
|
|
2012-11-06 12:14:17 -05:00
|
|
|
var definitions = {};
|
|
|
|
function getDefinition(value) {
|
|
|
|
var matches = value.match(/#([^\)']+)/);
|
|
|
|
return definitions[matches ? matches[1] : value];
|
|
|
|
}
|
|
|
|
|
2012-11-02 21:40:41 -04:00
|
|
|
var importers = {
|
2012-11-06 12:11:54 -05:00
|
|
|
// http://www.w3.org/TR/SVG/struct.html#Groups
|
2012-11-02 21:40:41 -04:00
|
|
|
g: importGroup,
|
2012-11-06 12:11:54 -05:00
|
|
|
// http://www.w3.org/TR/SVG/struct.html#NewDocument
|
2012-11-02 21:40:41 -04:00
|
|
|
svg: importGroup,
|
2012-11-06 12:14:17 -05:00
|
|
|
clippath: importCompoundPath,
|
|
|
|
|
2012-11-06 12:11:54 -05:00
|
|
|
// http://www.w3.org/TR/SVG/struct.html#SymbolElement
|
|
|
|
symbol: function(svg) {
|
|
|
|
var item = importGroup(svg);
|
|
|
|
applyAttributesAndStyles(svg, item);
|
|
|
|
// TODO: We're returning a symbol. How to handle this?
|
|
|
|
return new Symbol(item);
|
|
|
|
},
|
2012-11-06 12:14:17 -05:00
|
|
|
|
|
|
|
// http://www.w3.org/TR/SVG/struct.html#DefsElement
|
|
|
|
defs: function(svg) {
|
|
|
|
var group = importGroup(svg);
|
|
|
|
group.remove();
|
|
|
|
return group;
|
|
|
|
},
|
|
|
|
|
2012-11-06 12:11:54 -05:00
|
|
|
// http://www.w3.org/TR/SVG/shapes.html#PolygonElement
|
2012-11-02 21:40:41 -04:00
|
|
|
polygon: importPoly,
|
2012-11-06 12:11:54 -05:00
|
|
|
// http://www.w3.org/TR/SVG/shapes.html#PolylineElement
|
2012-11-02 21:40:41 -04:00
|
|
|
polyline: importPoly,
|
2012-09-30 17:51:50 -04:00
|
|
|
|
2012-11-06 12:11:54 -05:00
|
|
|
// http://www.w3.org/TR/SVG/shapes.html#InterfaceSVGCircleElement
|
2012-11-02 22:04:29 -04:00
|
|
|
circle: function(svg) {
|
|
|
|
return new Path.Circle(getPoint(svg, 'cx', 'cy'),
|
|
|
|
getValue(svg, 'r'));
|
2012-11-02 21:40:41 -04:00
|
|
|
},
|
2012-09-30 17:51:50 -04:00
|
|
|
|
2012-11-06 12:11:54 -05:00
|
|
|
// http://www.w3.org/TR/SVG/shapes.html#InterfaceSVGEllipseElement
|
2012-11-02 22:04:29 -04:00
|
|
|
ellipse: function(svg) {
|
|
|
|
var center = getPoint(svg, 'cx', 'cy'),
|
|
|
|
radius = getSize(svg, 'rx', 'ry');
|
|
|
|
return new Path.Oval(new Rectangle(center.subtract(radius),
|
|
|
|
center.add(radius)));
|
2012-11-02 21:40:41 -04:00
|
|
|
},
|
2012-09-30 17:51:50 -04:00
|
|
|
|
2012-11-06 12:11:54 -05:00
|
|
|
// http://www.w3.org/TR/SVG/shapes.html#RectElement
|
2012-11-02 22:04:29 -04:00
|
|
|
rect: function(svg) {
|
|
|
|
var point = getPoint(svg, 'x', 'y'),
|
|
|
|
size = getSize(svg, 'width', 'height'),
|
|
|
|
radius = getSize(svg, 'rx', 'ry');
|
2012-11-04 12:01:11 -05:00
|
|
|
// If radius is 0, Path.RoundRectangle automatically produces a
|
|
|
|
// normal rectangle for us.
|
2012-11-02 22:04:29 -04:00
|
|
|
return new Path.RoundRectangle(new Rectangle(point, size), radius);
|
2012-11-02 21:40:41 -04:00
|
|
|
},
|
2012-09-30 17:51:50 -04:00
|
|
|
|
2012-11-06 12:11:54 -05:00
|
|
|
// http://www.w3.org/TR/SVG/shapes.html#LineElement
|
2012-11-02 22:04:29 -04:00
|
|
|
line: function(svg) {
|
|
|
|
return new Path.Line(getPoint(svg, 'x1', 'y1'),
|
|
|
|
getPoint(svg, 'x2', 'y2'));
|
2012-11-02 21:40:41 -04:00
|
|
|
},
|
2012-09-30 17:51:50 -04:00
|
|
|
|
2012-11-02 22:04:29 -04:00
|
|
|
text: function(svg) {
|
2012-11-02 21:40:41 -04:00
|
|
|
// Not supported by Paper.js
|
|
|
|
// x: multiple values for x
|
|
|
|
// y: multiple values for y
|
|
|
|
// dx: multiple values for x
|
|
|
|
// dy: multiple values for y
|
|
|
|
// rotate: character rotation
|
|
|
|
// lengthAdjust:
|
2012-11-03 00:11:30 -04:00
|
|
|
var text = new PointText(getPoint(svg, 'x', 'y', 0)
|
|
|
|
.add(getPoint(svg, 'dx', 'dy', 0)));
|
2012-11-02 22:04:29 -04:00
|
|
|
text.content = svg.textContent || '';
|
2012-11-02 21:40:41 -04:00
|
|
|
return text;
|
|
|
|
},
|
|
|
|
|
2012-11-06 12:11:54 -05:00
|
|
|
// http://www.w3.org/TR/SVG/paths.html
|
2012-11-02 22:04:29 -04:00
|
|
|
path: function(svg) {
|
2012-11-03 22:45:19 -04:00
|
|
|
var path = new Path(),
|
2012-11-02 23:09:12 -04:00
|
|
|
list = svg.pathSegList,
|
2012-11-05 10:16:36 -05:00
|
|
|
compoundPath, lastPoint;
|
2012-11-02 23:09:12 -04:00
|
|
|
for (var i = 0, l = list.numberOfItems; i < l; i++) {
|
2012-11-05 10:16:36 -05:00
|
|
|
var segment = list.getItem(i),
|
2012-11-05 11:54:10 -05:00
|
|
|
segType = segment.pathSegType,
|
|
|
|
isRelative = segType % 2 == 1;
|
2012-11-05 21:26:29 -05:00
|
|
|
if (segType === /*#=*/ SVGPathSeg.PATHSEG_UNKNOWN)
|
2012-11-02 21:40:41 -04:00
|
|
|
continue;
|
2012-11-05 10:16:36 -05:00
|
|
|
if (!path.isEmpty())
|
|
|
|
lastPoint = path.getLastSegment().getPoint();
|
2012-11-05 11:54:10 -05:00
|
|
|
var relative = isRelative && !path.isEmpty()
|
2012-11-05 10:16:36 -05:00
|
|
|
? lastPoint
|
2012-11-03 01:57:06 -04:00
|
|
|
: Point.create(0, 0);
|
2012-11-05 11:54:10 -05:00
|
|
|
// Horizontal or vertical lineto commands, so fill in the
|
|
|
|
// missing x or y value:
|
2012-11-05 21:26:29 -05:00
|
|
|
var coord = (segType == /*#=*/ SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_ABS
|
|
|
|
|| segType == /*#=*/ SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_REL) && 'y'
|
|
|
|
|| (segType == /*#=*/ SVGPathSeg.PATHSEG_LINETO_VERTICAL_ABS
|
|
|
|
|| segType == /*#=*/ SVGPathSeg.PATHSEG_LINETO_VERTICAL_REL) && 'x';
|
2012-11-05 11:54:10 -05:00
|
|
|
if (coord)
|
|
|
|
segment[coord] = isRelative ? 0 : lastPoint[coord];
|
2012-11-03 01:53:33 -04:00
|
|
|
var point = Point.create(segment.x, segment.y).add(relative);
|
2012-11-05 11:54:10 -05:00
|
|
|
switch (segType) {
|
2012-11-05 21:26:29 -05:00
|
|
|
case /*#=*/ SVGPathSeg.PATHSEG_CLOSEPATH:
|
2012-11-02 21:40:41 -04:00
|
|
|
path.closePath();
|
|
|
|
break;
|
2012-11-05 21:26:29 -05:00
|
|
|
case /*#=*/ SVGPathSeg.PATHSEG_MOVETO_ABS:
|
|
|
|
case /*#=*/ SVGPathSeg.PATHSEG_MOVETO_REL:
|
2012-11-03 22:45:19 -04:00
|
|
|
if (!path.isEmpty() && !compoundPath) {
|
|
|
|
compoundPath = new CompoundPath([path]);
|
|
|
|
}
|
|
|
|
if (compoundPath) {
|
|
|
|
path = new Path();
|
2012-11-03 17:38:36 -04:00
|
|
|
compoundPath.addChild(path);
|
|
|
|
}
|
2012-11-03 22:45:19 -04:00
|
|
|
path.moveTo(point);
|
2012-11-02 21:40:41 -04:00
|
|
|
break;
|
2012-11-05 21:26:29 -05:00
|
|
|
case /*#=*/ SVGPathSeg.PATHSEG_LINETO_ABS:
|
|
|
|
case /*#=*/ SVGPathSeg.PATHSEG_LINETO_REL:
|
|
|
|
case /*#=*/ SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_ABS:
|
|
|
|
case /*#=*/ SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_REL:
|
|
|
|
case /*#=*/ SVGPathSeg.PATHSEG_LINETO_VERTICAL_ABS:
|
|
|
|
case /*#=*/ SVGPathSeg.PATHSEG_LINETO_VERTICAL_REL:
|
2012-11-03 01:53:33 -04:00
|
|
|
path.lineTo(point);
|
2012-11-02 21:40:41 -04:00
|
|
|
break;
|
2012-11-05 21:26:29 -05:00
|
|
|
case /*#=*/ SVGPathSeg.PATHSEG_CURVETO_CUBIC_ABS:
|
|
|
|
case /*#=*/ SVGPathSeg.PATHSEG_CURVETO_CUBIC_REL:
|
2012-11-02 21:40:41 -04:00
|
|
|
path.cubicCurveTo(
|
2012-11-02 23:09:12 -04:00
|
|
|
relative.add(segment.x1, segment.y1),
|
|
|
|
relative.add(segment.x2, segment.y2),
|
2012-11-03 01:53:33 -04:00
|
|
|
point
|
2012-11-02 21:40:41 -04:00
|
|
|
);
|
|
|
|
break;
|
2012-11-05 21:26:29 -05:00
|
|
|
case /*#=*/ SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_ABS:
|
|
|
|
case /*#=*/ SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_REL:
|
2012-11-02 21:40:41 -04:00
|
|
|
path.quadraticCurveTo(
|
2012-11-02 23:09:12 -04:00
|
|
|
relative.add(segment.x1, segment.y1),
|
2012-11-03 01:53:33 -04:00
|
|
|
point
|
2012-11-02 21:40:41 -04:00
|
|
|
);
|
|
|
|
break;
|
2012-11-02 23:11:40 -04:00
|
|
|
// TODO: Implement Arcs: ttp://www.w3.org/TR/SVG/implnote.html
|
2012-11-05 21:26:29 -05:00
|
|
|
// case /*#=*/ SVGPathSeg.PATHSEG_ARC_ABS:
|
|
|
|
// case /*#=*/ SVGPathSeg.PATHSEG_ARC_REL:
|
2012-11-02 23:11:40 -04:00
|
|
|
// break;
|
2012-11-05 21:26:29 -05:00
|
|
|
case /*#=*/ SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_ABS:
|
|
|
|
case /*#=*/ SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_REL:
|
2012-11-02 23:09:12 -04:00
|
|
|
var prev = list.getItem(i - 1),
|
2012-11-05 10:16:36 -05:00
|
|
|
control = lastPoint.add(lastPoint.subtract(
|
2012-11-02 23:09:12 -04:00
|
|
|
Point.create(prev.x2, prev.y2)
|
|
|
|
.subtract(prev.x, prev.y)
|
2012-11-05 10:16:36 -05:00
|
|
|
.add(lastPoint)));
|
2012-11-02 21:40:41 -04:00
|
|
|
path.cubicCurveTo(
|
2012-11-02 23:09:12 -04:00
|
|
|
control,
|
|
|
|
relative.add(segment.x2, segment.y2),
|
2012-11-03 01:53:33 -04:00
|
|
|
point);
|
2012-11-02 21:40:41 -04:00
|
|
|
break;
|
2012-11-05 21:26:29 -05:00
|
|
|
case /*#=*/ SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS:
|
|
|
|
case /*#=*/ SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL:
|
2012-11-02 23:09:12 -04:00
|
|
|
var control,
|
|
|
|
j = i;
|
|
|
|
for (; j >= 0; j--) {
|
|
|
|
var prev = list.getItem(j);
|
2012-11-05 21:26:29 -05:00
|
|
|
if (prev.pathSegType === /*#=*/ SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_ABS ||
|
|
|
|
prev.pathSegType === /*#=*/ SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_REL) {
|
2012-11-02 23:09:12 -04:00
|
|
|
control = Point.create(prev.x1, prev.y1)
|
|
|
|
.subtract(prev.x, prev.y)
|
2012-11-03 22:45:19 -04:00
|
|
|
.add(path._segments[j].getPoint());
|
2012-11-02 21:40:41 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-11-02 23:09:12 -04:00
|
|
|
for (; j < i; ++j) {
|
2012-11-03 22:45:19 -04:00
|
|
|
var anchor = path._segments[j].getPoint();
|
2012-11-03 01:57:19 -04:00
|
|
|
control = anchor.add(anchor.subtract(control));
|
2012-11-02 21:40:41 -04:00
|
|
|
}
|
2012-11-03 01:53:33 -04:00
|
|
|
path.quadraticCurveTo(control, point);
|
2012-11-02 21:40:41 -04:00
|
|
|
break;
|
2012-10-22 19:31:08 -04:00
|
|
|
}
|
2012-09-30 17:51:50 -04:00
|
|
|
}
|
2012-11-03 17:38:36 -04:00
|
|
|
return compoundPath || path;
|
2012-09-30 17:51:50 -04:00
|
|
|
}
|
2012-11-02 21:40:41 -04:00
|
|
|
};
|
2012-09-30 17:51:50 -04:00
|
|
|
|
2012-09-15 23:58:39 -04:00
|
|
|
/**
|
2012-09-30 17:51:50 -04:00
|
|
|
* Converts various SVG styles and attributes into Paper.js styles and
|
2012-11-02 21:14:20 -04:00
|
|
|
* attributes and applies them to the passed item.
|
2012-09-30 17:51:50 -04:00
|
|
|
*
|
2012-11-02 21:14:20 -04:00
|
|
|
* @param {SVGSVGElement} svg an SVG node to read style and attributes from.
|
|
|
|
* @param {Item} item the item to apply the style and attributes to.
|
2012-09-15 23:58:39 -04:00
|
|
|
*/
|
2012-11-02 22:16:23 -04:00
|
|
|
function applyAttributesAndStyles(svg, item) {
|
2012-11-05 21:26:29 -05:00
|
|
|
// SVG attributes can be set both as styles and direct node attributes,
|
|
|
|
// so we need to parse both
|
2012-11-02 22:11:28 -04:00
|
|
|
for (var i = 0, l = svg.style.length; i < l; i++) {
|
|
|
|
var name = svg.style[i];
|
2012-11-03 01:53:24 -04:00
|
|
|
applyAttributeOrStyle(svg, item, name, svg.style[Base.camelize(name)]);
|
2012-09-30 17:51:50 -04:00
|
|
|
}
|
2012-11-02 22:11:28 -04:00
|
|
|
for (var i = 0, l = svg.attributes.length; i < l; i++) {
|
|
|
|
var attr = svg.attributes[i];
|
2012-11-02 22:16:23 -04:00
|
|
|
applyAttributeOrStyle(svg, item, attr.name, attr.value);
|
2012-09-30 17:51:50 -04:00
|
|
|
}
|
2012-11-02 21:40:41 -04:00
|
|
|
}
|
2012-09-30 17:51:50 -04:00
|
|
|
|
|
|
|
/**
|
2012-11-02 21:14:20 -04:00
|
|
|
* Parses an SVG style attibute and applies it to a Paper.js item.
|
2012-09-30 17:51:50 -04:00
|
|
|
*
|
2012-11-02 22:16:23 -04:00
|
|
|
* @param {SVGSVGElement} svg an SVG node
|
|
|
|
* @param {Item} item the item to apply the style or attribute to.
|
2012-11-02 21:14:20 -04:00
|
|
|
* @param {String} name an SVG style name
|
|
|
|
* @param value the value of the SVG style
|
2012-09-16 01:02:23 -04:00
|
|
|
*/
|
2012-11-02 22:16:23 -04:00
|
|
|
function applyAttributeOrStyle(svg, item, name, value) {
|
2012-11-02 22:26:15 -04:00
|
|
|
if (value == null)
|
2012-09-30 17:51:50 -04:00
|
|
|
return;
|
2012-11-05 22:26:54 -05:00
|
|
|
if (value === 'none')
|
|
|
|
value = null;
|
|
|
|
var entry = SvgStyles.attributes[name];
|
|
|
|
if (entry) {
|
|
|
|
if (entry.type === 'number') {
|
|
|
|
value = parseFloat(value, 10);
|
|
|
|
} else if (entry.type === 'array') {
|
|
|
|
value = value.replace(/px/g, '').replace(/, /g, ',')
|
|
|
|
.replace(/ /g, ',').split(',');
|
|
|
|
for (var i = 0, l = value.length; i < l; i++)
|
|
|
|
value[i] = parseFloat(value[i], 10);
|
|
|
|
}
|
2012-11-05 22:48:00 -05:00
|
|
|
item._style[entry.set](value);
|
2012-11-05 22:26:54 -05:00
|
|
|
} else {
|
|
|
|
switch (name) {
|
|
|
|
case 'id':
|
2012-11-06 12:14:17 -05:00
|
|
|
definitions[value] = item;
|
2012-11-05 22:26:54 -05:00
|
|
|
item.setName(value);
|
|
|
|
break;
|
2012-11-06 12:11:54 -05:00
|
|
|
// http://www.w3.org/TR/SVG/masking.html#ClipPathProperty
|
2012-11-06 12:14:17 -05:00
|
|
|
case 'clip-path':
|
2012-11-06 13:16:03 -05:00
|
|
|
var clipPath = getDefinition(value).clone().flatten(),
|
2012-11-06 12:14:17 -05:00
|
|
|
group = new Group([clipPath, item]);
|
|
|
|
group.clipped = true;
|
|
|
|
break;
|
2012-11-06 12:11:54 -05:00
|
|
|
// http://www.w3.org/TR/SVG/coords.html#TransformAttribute
|
2012-11-05 22:26:54 -05:00
|
|
|
case 'transform':
|
|
|
|
applyTransform(svg, item);
|
|
|
|
break;
|
|
|
|
case 'opacity':
|
|
|
|
item.setOpacity(parseFloat(value, 10));
|
|
|
|
break;
|
|
|
|
case 'visibility':
|
2012-11-06 12:12:55 -05:00
|
|
|
item.setVisible(value === 'visible');
|
2012-11-05 22:26:54 -05:00
|
|
|
break;
|
|
|
|
case 'font':
|
|
|
|
case 'font-family':
|
|
|
|
case 'font-size':
|
2012-11-06 12:11:54 -05:00
|
|
|
// http://www.w3.org/TR/SVG/text.html#TextAnchorProperty
|
2012-11-05 22:26:54 -05:00
|
|
|
case 'text-anchor':
|
|
|
|
applyTextStyle(svg, item, name, value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Not supported yet.
|
|
|
|
break;
|
|
|
|
}
|
2012-10-22 19:31:08 -04:00
|
|
|
}
|
2012-11-02 21:40:41 -04:00
|
|
|
}
|
2012-09-30 17:51:50 -04:00
|
|
|
|
2012-11-03 00:11:30 -04:00
|
|
|
function applyTextStyle(svg, item, name, value) {
|
|
|
|
if (item instanceof TextItem) {
|
|
|
|
switch (name) {
|
|
|
|
case 'font':
|
2012-11-03 01:53:33 -04:00
|
|
|
// TODO: Verify if there is not another way?
|
2012-11-03 00:11:30 -04:00
|
|
|
var text = document.createElement('span');
|
|
|
|
text.style.font = value;
|
|
|
|
for (var i = 0; i < text.style.length; i++) {
|
2012-11-05 22:48:00 -05:00
|
|
|
var name = text.style[i];
|
|
|
|
applyAttributeOrStyle(svg, item, name, text.style[name]);
|
2012-11-03 00:11:30 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'font-family':
|
2012-11-05 22:48:00 -05:00
|
|
|
item.setFont(value.split(',')[0].replace(/^\s+|\s+$/g, ''));
|
2012-11-03 00:11:30 -04:00
|
|
|
break;
|
|
|
|
case 'font-size':
|
|
|
|
item.setFontSize(parseFloat(value, 10));
|
|
|
|
break;
|
|
|
|
case 'text-anchor':
|
|
|
|
item.setJustification({
|
|
|
|
start: 'left',
|
|
|
|
middle: 'center',
|
|
|
|
end: 'right'
|
|
|
|
}[value]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (item instanceof Group) {
|
|
|
|
// Text styles need to be recursively passed down to children that
|
|
|
|
// might be TextItems explicitely.
|
|
|
|
var children = item._children;
|
|
|
|
for (var i = 0, l = children.length; i < l; i++) {
|
|
|
|
applyTextStyle(svg, children[i], name, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-30 17:51:50 -04:00
|
|
|
/**
|
2012-11-02 21:14:20 -04:00
|
|
|
* Applies the transformations specified on the SVG node to a Paper.js item
|
2012-09-30 17:51:50 -04:00
|
|
|
*
|
2012-11-02 21:14:20 -04:00
|
|
|
* @param {SVGSVGElement} svg an SVG node
|
2012-11-02 22:16:23 -04:00
|
|
|
* @param {Item} item a Paper.js item
|
2012-09-30 17:51:50 -04:00
|
|
|
*/
|
2012-11-02 22:16:23 -04:00
|
|
|
function applyTransform(svg, item) {
|
2012-11-02 22:39:59 -04:00
|
|
|
var transforms = svg.transform.baseVal,
|
|
|
|
matrix = new Matrix();
|
2012-11-02 22:11:16 -04:00
|
|
|
for (var i = 0, l = transforms.numberOfItems; i < l; i++) {
|
2012-11-02 22:39:59 -04:00
|
|
|
var transform = transforms.getItem(i);
|
2012-11-05 21:26:29 -05:00
|
|
|
if (transform.type === /*#=*/ SVGTransform.SVG_TRANSFORM_UNKNOWN)
|
2012-09-30 17:51:50 -04:00
|
|
|
continue;
|
2012-11-02 22:39:59 -04:00
|
|
|
// Convert SVG Matrix to Paper Matrix.
|
|
|
|
// TODO: Should this be moved to our Matrix constructor?
|
|
|
|
var mx = transform.matrix,
|
|
|
|
a = mx.a,
|
|
|
|
b = mx.b,
|
|
|
|
c = mx.c,
|
|
|
|
d = mx.d;
|
2012-09-30 17:51:50 -04:00
|
|
|
switch (transform.type) {
|
2012-11-02 22:39:59 -04:00
|
|
|
// Compensate for SVG's theta rotation going the opposite direction
|
2012-11-05 21:27:50 -05:00
|
|
|
case /*#=*/ SVGTransform.SVG_TRANSFORM_MATRIX:
|
2012-11-02 22:39:59 -04:00
|
|
|
var tmp = b;
|
|
|
|
b = c;
|
|
|
|
c = tmp;
|
2012-10-22 19:31:08 -04:00
|
|
|
break;
|
2012-11-05 21:26:29 -05:00
|
|
|
case /*#=*/ SVGTransform.SVG_TRANSFORM_SKEWX:
|
2012-11-02 22:39:59 -04:00
|
|
|
b = c;
|
|
|
|
c = 0;
|
2012-10-22 19:31:08 -04:00
|
|
|
break;
|
2012-11-05 21:26:29 -05:00
|
|
|
case /*#=*/ SVGTransform.SVG_TRANSFORM_SKEWY:
|
2012-11-02 22:39:59 -04:00
|
|
|
c = b;
|
|
|
|
b = 0;
|
2012-10-22 19:31:08 -04:00
|
|
|
break;
|
2012-11-05 21:26:29 -05:00
|
|
|
case /*#=*/ SVGTransform.SVG_TRANSFORM_ROTATE:
|
2012-11-02 22:39:59 -04:00
|
|
|
b = -b;
|
|
|
|
c = -c;
|
2012-10-22 19:31:08 -04:00
|
|
|
break;
|
2012-09-30 17:51:50 -04:00
|
|
|
}
|
2012-11-02 22:39:59 -04:00
|
|
|
matrix.concatenate(new Matrix(a, c, b, d, mx.e, mx.f));
|
2012-09-30 17:51:50 -04:00
|
|
|
}
|
|
|
|
item.transform(matrix);
|
|
|
|
}
|
2012-11-02 21:40:41 -04:00
|
|
|
|
|
|
|
return /** @Lends SvgImporter */{
|
|
|
|
/**
|
|
|
|
* Creates a Paper.js item using data parsed from the selected
|
|
|
|
* SVG DOM node.
|
|
|
|
*
|
|
|
|
* @param {SVGSVGElement} svg the SVG DOM node to convert
|
|
|
|
* @return {Item} the converted Paper.js item
|
|
|
|
*/
|
|
|
|
importSvg: function(svg) {
|
2012-11-05 21:27:13 -05:00
|
|
|
var type = svg.nodeName.toLowerCase(),
|
|
|
|
importer = importers[type];
|
2012-11-02 21:40:41 -04:00
|
|
|
// TODO: importer == null: Not supported yet.
|
2012-11-05 21:27:13 -05:00
|
|
|
var item = importer && importer(svg, type);
|
2012-11-02 21:40:41 -04:00
|
|
|
if (item)
|
2012-11-02 22:16:23 -04:00
|
|
|
applyAttributesAndStyles(svg, item);
|
2012-11-02 21:40:41 -04:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
};
|
2012-11-02 19:19:45 -04:00
|
|
|
};
|