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' ) return s.toString();
18 var replace = { '&': 'amp', '<': 'lt', '>': 'gt', '"': 'quot', '\'': 'apos' };
20 for ( var entity in replace ) {
21 s = s.replace(new RegExp(entity, 'g'), '&' + replace[entity] + ';');
28 * Generic map function
29 * @param iterable the array or object to be mapped
30 * @param callback the callback function(element, key)
33 function map(iterable, callback) {
34 var mapped = new Array;
36 for ( var i in iterable ) {
37 if ( iterable.hasOwnProperty(i) ) {
38 var value = callback.call(this, iterable[i], i);
40 if ( value !== null ) mapped.push(value);
48 * Generic reduce function
49 * @param iterable array or object to be reduced
50 * @param callback the callback function(initial, element, i)
51 * @param initial the initial value
52 * @return the reduced value
54 function reduce(iterable, callback, initial) {
55 for ( var i in iterable ) {
56 if ( iterable.hasOwnProperty(i) ) {
57 initial = callback.call(this, initial, iterable[i], i);
65 * Utility method for creating a tag
66 * @param name the tag name, e.g., 'text'
67 * @param attrs the attribute string, e.g., name1="val1" name2="val2"
68 * or attribute map, e.g., { name1 : 'val1', name2 : 'val2' }
69 * @param content the content string inside the tag
70 * @returns string of the tag
72 function tag(name, attrs, matrix, content) {
73 if ( typeof content === 'undefined' || content === null ) {
77 if ( typeof attrs === 'object' ) {
78 attrs = map(attrs, function(element, name) {
79 if ( name === 'transform') return;
81 return name + '="' + escapeXML(element) + '"';
85 return '<' + name + ( matrix ? ' transform="matrix(' + matrix.toString().replace(/^matrix\(|\)$/g, '') + ')" ' : ' ' ) + attrs + '>' + content + '</' + name + '>';
89 * @return object the style object
91 function extractStyle(node) {
94 family: node.attrs.font.replace(/^.*?"(\w+)".*$/, '$1'),
95 size: typeof node.attrs['font-size'] === 'undefined' ? null : node.attrs['font-size']
101 * @param style object from style()
104 function styleToString(style) {
105 // TODO figure out what is 'normal'
106 return 'font: normal normal normal 10px/normal ' + style.font.family + ( style.font.size === null ? '' : '; font-size: ' + style.font.size + 'px' );
110 * Computes tspan dy using font size. This formula was empircally determined
111 * using a best-fit line. Works well in both VML and SVG browsers.
112 * @param fontSize number
115 function computeTSpanDy(fontSize, line, lines) {
116 if ( fontSize === null ) fontSize = 10;
118 //return fontSize * 4.5 / 13
119 return fontSize * 4.5 / 13 * ( line - .2 - lines / 2 ) * 3.5;
123 'text': function(node) {
124 style = extractStyle(node);
126 var tags = new Array;
128 map(node.attrs['text'].split('\n'), function(text, iterable, line) {
134 function(initial, value, name) {
135 if ( name !== 'text' && name !== 'w' && name !== 'h' ) {
136 if ( name === 'font-size') value = value + 'px';
138 initial[name] = escapeXML(value.toString());
143 { style: 'text-anchor: middle; ' + styleToString(style) + ';' }
146 tag('tspan', { dy: computeTSpanDy(style.font.size, line + 1, node.attrs['text'].split('\n').length) }, null, text)
152 'path' : function(node) {
153 var initial = ( node.matrix.a === 1 && node.matrix.d === 1 ) ? {} : { 'transform' : node.matrix.toString() };
159 function(initial, value, name) {
160 if ( name === 'path' ) name = 'd';
162 initial[name] = value.toString();
171 // Other serializers should go here
174 R.fn.toSVG = function() {
177 restore = { svg: R.svg, vml: R.vml },
178 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 + '">'
184 for ( var node = paper.bottom; node != null; node = node.next ) {
185 if ( node.node.style.display === 'none' ) continue;
190 if ( typeof serializer[node.type] === 'function' ) {
191 svg += serializer[node.type](node);
196 switch ( node.type ) {
198 attrs += ' preserveAspectRatio="none"';
202 for ( i in node.attrs ) {
217 attrs += ' ' + name + '="' + escapeXML(node.attrs[i].toString()) + '"';
221 svg += '<' + node.type + ' transform="matrix(' + node.matrix.toString().replace(/^matrix\(|\)$/g, '') + ')"' + attrs + '></' + node.type + '>';