paper.js/src/svg/SVGExport.js

416 lines
12 KiB
JavaScript
Raw Normal View History

2012-11-02 20:47:14 -04:00
/*
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
2012-09-30 17:51:50 -04:00
* http://paperjs.org/
*
* Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey
2012-09-30 17:51:50 -04:00
* http://lehni.org/ & http://jonathanpuckey.com/
*
* Distributed under the MIT license. See LICENSE file for details.
*
* All rights reserved.
*/
2012-11-04 02:04:15 -05:00
/**
* A function scope holding all the functionality needed to convert a
* Paper.js DOM to a SVG DOM.
2012-11-04 02:04:15 -05:00
*/
new function() {
var formatter;
2013-02-10 22:02:53 -05:00
function setAttributes(node, attrs) {
for (var key in attrs) {
var val = attrs[key],
namespace = SVGNamespaces[key];
if (typeof val === 'number')
val = formatter.number(val);
if (namespace) {
node.setAttributeNS(namespace, key, val);
} else {
2013-02-10 22:02:53 -05:00
node.setAttribute(key, val);
}
}
2013-02-10 22:02:53 -05:00
return node;
2012-11-05 23:31:45 -05:00
}
function createElement(tag, attrs) {
return setAttributes(
document.createElementNS('http://www.w3.org/2000/svg', tag), attrs);
}
2013-10-16 10:47:00 -04:00
function getTransform(item, coordinates, center) {
var matrix = item._matrix,
trans = matrix.getTranslation(),
attrs = {};
if (coordinates) {
// If the item suppports x- and y- coordinates, we're taking out the
// translation part of the matrix and move it to x, y attributes, to
// produce more readable markup, and not have to use center points
// in rotate(). To do so, SVG requries us to inverse transform the
// translation point by the matrix itself, since they are provided
// in local coordinates.
matrix = matrix.shiftless();
var point = matrix._inverseTransform(trans);
2013-10-16 10:47:00 -04:00
attrs[center ? 'cx' : 'x'] = point.x;
attrs[center ? 'cy' : 'y'] = point.y;
trans = null;
}
if (matrix.isIdentity())
return attrs;
// See if we can decompose the matrix and can formulate it as a simple
// translate/scale/rotate command sequence.
var decomposed = matrix.decompose();
if (decomposed && !decomposed.shearing) {
var parts = [],
angle = decomposed.rotation,
scale = decomposed.scaling;
if (trans && !trans.isZero())
parts.push('translate(' + formatter.point(trans) + ')');
if (!Numerical.isZero(scale.x - 1) || !Numerical.isZero(scale.y - 1))
parts.push('scale(' + formatter.point(scale) +')');
if (angle)
parts.push('rotate(' + formatter.number(angle) + ')');
attrs.transform = parts.join(' ');
} else {
attrs.transform = 'matrix(' + matrix.getValues().join(',') + ')';
}
return attrs;
}
2013-02-10 22:02:53 -05:00
function exportGroup(item) {
var attrs = getTransform(item),
children = item._children;
var node = createElement('g', attrs);
for (var i = 0, l = children.length; i < l; i++) {
var child = children[i];
var childNode = exportSVG(child);
if (childNode) {
if (child.isClipMask()) {
var clip = createElement('clipPath');
clip.appendChild(childNode);
setDefinition(child, clip, 'clip');
setAttributes(node, {
'clip-path': 'url(#' + clip.id + ')'
});
} else {
node.appendChild(childNode);
}
}
}
2013-02-10 22:02:53 -05:00
return node;
}
2013-02-09 12:44:25 -05:00
function exportRaster(item) {
var attrs = getTransform(item, true),
size = item.getSize();
// Take into account that rasters are centered:
2013-02-09 12:44:25 -05:00
attrs.x -= size.width / 2;
attrs.y -= size.height / 2;
2013-02-10 13:23:49 -05:00
attrs.width = size.width;
attrs.height = size.height;
attrs.href = item.toDataURL();
return createElement('image', attrs);
2013-02-09 12:44:25 -05:00
}
function exportPath(item) {
var segments = item._segments,
type,
attrs;
if (segments.length === 0)
2012-12-09 21:04:56 -05:00
return null;
if (item.isPolygon()) {
if (segments.length >= 3) {
type = item._closed ? 'polygon' : 'polyline';
var parts = [];
for(i = 0, l = segments.length; i < l; i++)
parts.push(formatter.point(segments[i]._point));
attrs = {
points: parts.join(' ')
};
} else {
type = 'line';
var first = segments[0]._point,
last = segments[segments.length - 1]._point;
attrs = {
x1: first.x,
y1: first.y,
x2: last.x,
y2: last.y
};
}
} else {
type = 'path';
var data = item.getPathData();
attrs = data && { d: data };
2012-09-30 17:51:50 -04:00
}
2013-02-09 16:38:22 -05:00
return createElement(type, attrs);
}
2013-10-16 10:47:00 -04:00
function exportShape(item) {
var shape = item._shape,
center = item.getPosition(true),
radius = item._radius,
attrs = getTransform(item, true, shape !== 'rectangle');
if (shape === 'rectangle') {
shape = 'rect'; // SVG
var size = item._size,
width = size.width,
height = size.height;
attrs.x -= width / 2;
attrs.y -= height / 2;
attrs.width = width;
attrs.height = height;
if (radius.isZero())
radius = null;
}
if (radius) {
if (shape === 'circle') {
attrs.r = radius;
} else {
attrs.rx = radius.width;
attrs.ry = radius.height;
}
}
return createElement(shape, attrs);
}
2013-02-10 13:23:49 -05:00
function exportCompoundPath(item) {
var attrs = getTransform(item, true);
var data = item.getPathData();
if (data)
attrs.d = data;
2013-02-10 13:23:49 -05:00
return createElement('path', attrs);
}
function exportPlacedSymbol(item) {
var attrs = getTransform(item, true),
symbol = item.getSymbol(),
2013-10-16 10:14:37 -04:00
symbolNode = getDefinition(symbol, 'symbol'),
2013-02-10 13:23:49 -05:00
definition = symbol.getDefinition(),
bounds = definition.getBounds();
if (!symbolNode) {
symbolNode = createElement('symbol', {
viewBox: formatter.rectangle(bounds)
2013-02-10 13:23:49 -05:00
});
2013-04-23 10:19:08 -04:00
symbolNode.appendChild(exportSVG(definition));
setDefinition(symbol, symbolNode, 'symbol');
2013-02-10 13:23:49 -05:00
}
attrs.href = '#' + symbolNode.id;
2013-02-10 13:23:49 -05:00
attrs.x += bounds.x;
attrs.y += bounds.y;
attrs.width = formatter.number(bounds.width);
attrs.height = formatter.number(bounds.height);
2013-02-10 13:23:49 -05:00
return createElement('use', attrs);
}
function exportGradient(color, item) {
// NOTE: As long as the fillTransform attribute is not implemented,
// we need to create a separate gradient object for each gradient,
// even when they share the same gradient defintion.
// http://www.svgopen.org/2011/papers/20-Separating_gradients_from_geometry/
2013-04-23 10:19:08 -04:00
// TODO: Implement gradient merging in SVGImport
var gradientNode = getDefinition(color, 'color');
if (!gradientNode) {
var gradient = color.getGradient(),
radial = gradient._radial,
2013-02-12 20:23:56 -05:00
matrix = item._gradientMatrix,
origin = color.getOrigin().transform(matrix),
destination = color.getDestination().transform(matrix),
attrs;
2013-05-08 23:29:37 -04:00
if (radial) {
attrs = {
cx: origin.x,
cy: origin.y,
r: origin.getDistance(destination)
};
var highlight = color.getHighlight();
if (highlight) {
highlight = highlight.transform(matrix);
attrs.fx = highlight.x;
attrs.fy = highlight.y;
}
2013-05-08 23:29:37 -04:00
} else {
attrs = {
x1: origin.x,
y1: origin.y,
x2: destination.x,
y2: destination.y
};
}
attrs.gradientUnits = 'userSpaceOnUse';
gradientNode = createElement(
(radial ? 'radial' : 'linear') + 'Gradient', attrs);
var stops = gradient._stops;
for (var i = 0, l = stops.length; i < l; i++) {
var stop = stops[i],
stopColor = stop._color,
alpha = stopColor.getAlpha();
2013-02-10 22:40:44 -05:00
attrs = {
offset: stop._rampPoint,
2013-06-12 17:04:59 -04:00
'stop-color': stopColor.toCSS(true)
2013-02-10 22:40:44 -05:00
};
// See applyStyle for an explanation of why there are separated
// opacity / color attributes.
if (alpha < 1)
attrs['stop-opacity'] = alpha;
gradientNode.appendChild(createElement('stop', attrs));
}
setDefinition(color, gradientNode, 'color');
2013-02-10 22:02:53 -05:00
}
return 'url(#' + gradientNode.id + ')';
2013-02-10 22:02:53 -05:00
}
2013-06-18 19:57:09 -04:00
function exportText(item) {
var node = createElement('text', getTransform(item, true));
node.textContent = item._content;
return node;
}
2012-11-06 14:00:58 -05:00
var exporters = {
group: exportGroup,
layer: exportGroup,
raster: exportRaster,
path: exportPath,
2013-10-16 10:47:00 -04:00
shape: exportShape,
2013-06-18 19:57:09 -04:00
'compound-path': exportCompoundPath,
'placed-symbol': exportPlacedSymbol,
2013-06-18 19:57:09 -04:00
'point-text': exportText
2012-11-06 14:00:58 -05:00
};
2013-02-10 22:02:53 -05:00
function applyStyle(item, node) {
var attrs = {},
parent = item.getParent();
if (item._name != null)
2012-11-05 22:26:54 -05:00
attrs.id = item._name;
2013-04-23 10:19:08 -04:00
Base.each(SVGStyles, function(entry) {
// Get a given style only if it differs from the value on the parent
// (A layer or group which can have style values in SVG).
2013-06-18 19:18:13 -04:00
var get = entry.get,
type = entry.type,
value = item[get]();
if (!parent || !Base.equals(parent[get](), value)) {
2013-06-18 19:18:13 -04:00
if (type === 'color' && value != null) {
// Support for css-style rgba() values is not in SVG 1.1, so
// separate the alpha value of colors with alpha into the
// separate fill- / stroke-opacity attribute:
var alpha = value.getAlpha();
if (alpha < 1)
attrs[entry.attribute + '-opacity'] = alpha;
}
attrs[entry.attribute] = value == null
? 'none'
2013-06-18 19:18:13 -04:00
: type === 'number'
? formatter.number(value)
2013-06-18 19:18:13 -04:00
: type === 'color'
? value.gradient
? exportGradient(value, item)
// true for noAlpha, see above
: value.toCSS(true)
2013-06-18 19:18:13 -04:00
: type === 'array'
? value.join(',')
2013-06-18 19:18:13 -04:00
: type === 'lookup'
? entry.toSVG[value]
: value;
}
});
if (attrs.opacity === 1)
delete attrs.opacity;
if (item._visibility != null && !item._visibility)
attrs.visibility = 'hidden';
delete item._gradientMatrix; // see exportPath()
2013-02-10 22:02:53 -05:00
return setAttributes(node, attrs);
}
2013-02-10 13:23:49 -05:00
var definitions;
function getDefinition(item, type) {
2013-02-10 13:23:49 -05:00
if (!definitions)
definitions = { ids: {}, svgs: {} };
return item && definitions.svgs[type + '-' + item._id];
2013-02-10 13:23:49 -05:00
}
function setDefinition(item, node, type) {
// Make sure the definitions lookup is created before we use it.
// This is required by 'clip', where getDefinition() is not called.
if (!definitions)
getDefinition();
// Have different id ranges per type
var id = definitions.ids[type] = (definitions.ids[type] || 0) + 1;
2013-06-18 20:29:00 -04:00
// Give the svg node an id, and link to it from the item id.
2013-02-10 22:02:53 -05:00
node.id = type + '-' + id;
2013-06-18 20:29:00 -04:00
definitions.svgs[type + '-' + item._id] = node;
2013-02-10 13:23:49 -05:00
}
function exportDefinitions(node, options) {
2013-02-10 13:23:49 -05:00
if (!definitions)
2013-02-10 22:02:53 -05:00
return node;
// We can only use svg nodes as defintion containers. Have the loop
2013-02-10 13:23:49 -05:00
// produce one if it's a single item of another type (when calling
2013-04-23 10:19:08 -04:00
// #exportSVG() on an item rather than a whole project)
// jsdom in Node.js uses uppercase values for nodeName...
var svg = node.nodeName.toLowerCase() === 'svg' && node,
defs = null;
2013-02-10 13:23:49 -05:00
for (var i in definitions.svgs) {
// This code is inside the loop so we only create a container if we
// actually have svgs.
if (!defs) {
if (!svg) {
svg = createElement('svg');
svg.appendChild(node);
}
defs = svg.insertBefore(createElement('defs'), svg.firstChild);
2013-02-10 13:23:49 -05:00
}
defs.appendChild(definitions.svgs[i]);
2013-02-10 13:23:49 -05:00
}
// Clear definitions at the end of export
definitions = null;
return options && options.asString
? new XMLSerializer().serializeToString(svg)
: svg;
2013-02-10 13:23:49 -05:00
}
2013-04-23 10:19:08 -04:00
function exportSVG(item) {
var exporter = exporters[item._type],
node = exporter && exporter(item, item._type);
if (node && item._data)
node.setAttribute('data-paper-data', JSON.stringify(item._data));
2013-02-10 22:02:53 -05:00
return node && applyStyle(item, node);
2013-02-10 13:23:49 -05:00
}
function setOptions(options) {
formatter = options && options.precision
? new Formatter(options.precision)
: Formatter.instance;
}
Item.inject({
exportSVG: function(options) {
setOptions(options);
return exportDefinitions(exportSVG(this), options);
}
});
Project.inject({
exportSVG: function(options) {
setOptions(options);
2013-06-11 17:15:54 -04:00
var layers = this.layers,
size = this.view.getSize(),
node = createElement('svg', {
x: 0,
y: 0,
width: size.width,
height: size.height,
version: '1.1',
xmlns: 'http://www.w3.org/2000/svg',
2013-06-19 11:22:20 -04:00
'xmlns:xlink': 'http://www.w3.org/1999/xlink'
2013-06-11 17:15:54 -04:00
});
for (var i = 0, l = layers.length; i < l; i++)
2013-04-23 10:19:08 -04:00
node.appendChild(exportSVG(layers[i]));
return exportDefinitions(node, options);
}
});
};