2 * Raphael.Export https://github.com/ElbertF/Raphael.Export
4 * Licensed under the MIT license:
5 * http://www.opensource.org/licenses/mit-license.php
11 * Escapes string for XML interpolation
12 * @param value string or number value to escape
13 * @returns string escaped
15 function escapeXML(s) {
16 if (typeof s === 'number')
27 for (var entity in replace) {
28 s = s.replace(new RegExp(entity, 'g'), '&' + replace[entity] + ';');
35 * Generic map function
36 * @param iterable the array or object to be mapped
37 * @param callback the callback function(element, key)
40 function map(iterable, callback) {
41 var mapped = new Array;
43 for (var i in iterable) {
44 if (iterable.hasOwnProperty(i)) {
45 var value = callback.call(this, iterable[i], i);
56 * Generic reduce function
57 * @param iterable array or object to be reduced
58 * @param callback the callback function(initial, element, i)
59 * @param initial the initial value
60 * @return the reduced value
62 function reduce(iterable, callback, initial) {
63 for (var i in iterable) {
64 if (iterable.hasOwnProperty(i)) {
65 initial = callback.call(this, initial, iterable[i], i);
73 * Utility method for creating a tag
74 * @param name the tag name, e.g., 'text'
75 * @param attrs the attribute string, e.g., name1="val1" name2="val2"
76 * or attribute map, e.g., { name1 : 'val1', name2 : 'val2' }
77 * @param content the content string inside the tag
78 * @returns string of the tag
80 function tag(name, attrs, matrix, content) {
81 if (typeof content === 'undefined' || content === null) {
85 if (typeof attrs === 'object') {
86 attrs = map(attrs, function (element, name) {
87 if (name === 'transform')
90 return name + '="' + escapeXML(element) + '"';
94 return '<' + name + (matrix ? ' transform="matrix(' + matrix.toString().replace(/^matrix\(|\)$/g, '') + ')" ' : ' ') + attrs + '>' +
96 '</' + name + '>' + "\n";
100 * @return object the style object
102 function extractStyle(node) {
105 family: node.attrs.font.replace(/^.*?"(\w+)".*$/, '$1'),
106 size: typeof node.attrs['font-size'] === 'undefined' ? null : node.attrs['font-size'],
107 anchor: typeof node.attrs['text-anchor'] === 'undefined' ? null : node.attrs['text-anchor'],
113 * @param style object from style()
116 function styleToString(style) {
117 // TODO figure out what is 'normal'
120 'font-family:' + style.font.family,
121 'font-weight:normal',
123 'font-stretch:normal',
124 'font-variant:normal'
126 if (style.font.size !== null) {
127 r.push('font-size: ' + style.font.size + 'px')
135 * Computes tspan dy using font size. This formula was empircally determined
136 * using a best-fit line. Works well in both VML and SVG browsers.
137 * @param fontSize number
140 function computeTSpanDy(fontSize, line, lines) {
141 if (fontSize === null)
144 //return fontSize * 4.5 / 13
145 return fontSize * 4.5 / 13 * (line - .2 - lines / 2) * 3.5;
149 'text': function (node) {
151 var style = extractStyle(node);
153 var tags = new Array;
157 var textArray = node.attrs['text'].split('\n');
159 textArray.forEach((v,k) => {
160 content.push(tag('tspan',
163 dy: computeTSpanDy(style.font.size, k + 1, textArray.length)
175 function (initial, value, name) {
176 if (name !== 'text' && name !== 'w' && name !== 'h') {
177 if (name === 'font-size')
178 value = value + 'px';
180 initial[name] = escapeXML(value.toString());
186 style: 'text-anchor: ' + (style.font.anchor ? (style.font.anchor + ';') : 'middle;') +
187 styleToString(style) + ';'
196 'path': function (node) {
197 var initial = (node.matrix.a === 1 && node.matrix.d === 1) ? {} : {'transform': node.matrix.toString()};
205 function (initial, value, name) {
206 if (name === 'path') {
210 initial[name] = (typeof (value) == 'undefined') ? '' : value.toString();
215 style: 'fill:' + Raphael.color(node.attrs.fill).hex + ';'
221 // Other serializers should go here
224 R.fn.toSVG = function () {
227 restore = {svg: R.svg, vml: R.vml},
228 svg = '<svg style="overflow: hidden; position: relative;" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="' + paper.width + '" version="1.1" height="' + paper.height + '">';
233 for (var node = paper.bottom; node != null; node = node.next) {
234 if (node.node.style.display === 'none')
240 if (typeof serializer[node.type] === 'function') {
241 svg += serializer[node.type](node);
248 attrs += ' preserveAspectRatio="none"';
252 for (i in node.attrs) {
254 var val = node.attrs[i].toString();
266 val = Raphael.getRGB(val).hex;
271 attrs += ' ' + name + '="' + escapeXML(val) + '"';
275 svg += '<' + node.type + ' transform="matrix(' + node.matrix.toString().replace(/^matrix\(|\)$/g, '') + ')"' + attrs + '></' + node.type + '>';