plugins/raphael.export.js
[raphael] / plugins / raphael.export.js
1 /**
2  * Raphael.Export https://github.com/ElbertF/Raphael.Export
3  *
4  * Licensed under the MIT license:
5  * http://www.opensource.org/licenses/mit-license.php
6  *
7  */
8
9 (function(R) {
10         /**
11         * Escapes string for XML interpolation
12         * @param value string or number value to escape
13         * @returns string escaped
14         */
15         function escapeXML(s) {
16                 if ( typeof s === 'number' ) return s.toString();
17
18                 var replace = {
19             '&': 'amp',
20             '<': 'lt',
21             '>': 'gt',
22             '"': 'quot',
23             '\'': 'apos'
24         };
25
26                 for ( var entity in replace ) {
27                         s = s.replace(new RegExp(entity, 'g'), '&' + replace[entity] + ';');
28                 }
29
30                 return s;
31         }
32
33         /**
34         * Generic map function
35         * @param iterable the array or object to be mapped
36         * @param callback the callback function(element, key)
37         * @returns array
38         */
39         function map(iterable, callback) {
40                 var mapped = new Array;
41
42                 for ( var i in iterable ) {
43                         if ( iterable.hasOwnProperty(i) ) {
44                                 var value = callback.call(this, iterable[i], i);
45
46                                 if ( value !== null ) mapped.push(value);
47                         }
48                 }
49
50                 return mapped;
51         }
52
53         /**
54         * Generic reduce function
55         * @param iterable array or object to be reduced
56         * @param callback the callback function(initial, element, i)
57         * @param initial the initial value
58         * @return the reduced value
59         */
60         function reduce(iterable, callback, initial) {
61                 for ( var i in iterable ) {
62                         if ( iterable.hasOwnProperty(i) ) {
63                                 initial = callback.call(this, initial, iterable[i], i);
64                         }
65                 }
66
67                 return initial;
68         }
69
70         /**
71         * Utility method for creating a tag
72         * @param name the tag name, e.g., 'text'
73         * @param attrs the attribute string, e.g., name1="val1" name2="val2"
74         * or attribute map, e.g., { name1 : 'val1', name2 : 'val2' }
75         * @param content the content string inside the tag
76         * @returns string of the tag
77         */
78         function tag(name, attrs, matrix, content) {
79                 if ( typeof content === 'undefined' || content === null ) {
80                         content = '';
81                 }
82
83                 if ( typeof attrs === 'object' ) {
84                         attrs = map(attrs, function(element, name) {
85                                 if ( name === 'transform') return;
86
87                                 return name + '="' + escapeXML(element) + '"';
88                         }).join(' ');
89                 }
90
91                 return '<' + name + ( matrix ? ' transform="matrix(' + matrix.toString().replace(/^matrix\(|\)$/g, '') + ')" ' : ' ' ) + attrs + '>' +
92              content +
93             '</' + name + '>' + "\n";
94         }
95
96         /**
97         * @return object the style object
98         */
99         function extractStyle(node) {
100                 return {
101                         font: {
102                                 family: node.attrs.font.replace(/^.*?"(\w+)".*$/, '$1'),
103                                 size:   typeof node.attrs['font-size'] === 'undefined' ? null : node.attrs['font-size']
104                                 }
105                         };
106         }
107
108         /**
109         * @param style object from style()
110         * @return string
111         */
112         function styleToString(style) {
113                 // TODO figure out what is 'normal'
114                 return 'font: normal normal normal 10px/normal ' + style.font.family +
115             ( style.font.size === null ? '' : '; font-size: ' + style.font.size + 'px' );
116         }
117
118         /**
119         * Computes tspan dy using font size. This formula was empircally determined
120         * using a best-fit line. Works well in both VML and SVG browsers.
121         * @param fontSize number
122         * @return number
123         */
124         function computeTSpanDy(fontSize, line, lines) {
125                 if ( fontSize === null ) fontSize = 10;
126
127                 //return fontSize * 4.5 / 13
128                 return fontSize * 4.5 / 13 * ( line - .2 - lines / 2 ) * 3.5;
129         }
130
131         var serializer = {
132                 'text': function(node) {
133                         style = extractStyle(node);
134             Roo.log(style);
135                         var tags = new Array;
136
137                         map(node.attrs['text'].split('\n'), function(text, iterable, line) {
138                 line = line || 0;
139                                 tags.push(tag(
140                                         'text',
141                                         reduce(
142                                                 node.attrs,
143                                                 function(initial, value, name) {
144                                                         if ( name !== 'text' && name !== 'w' && name !== 'h' ) {
145                                                                 if ( name === 'font-size') value = value + 'px';
146
147                                                                 initial[name] = escapeXML(value.toString());
148                                                         }
149
150                                                         return initial;
151                                                 },
152                                                 {
153                                 style: 'text-anchor: middle; ' + styleToString(style) + ';' }
154                                                 ),
155                                         node.matrix,
156                                         tag('tspan',
157                             {
158                                 dy: computeTSpanDy(style.font.size, line + 1, node.attrs['text'].split('\n').length)
159                             },
160                             null,
161                             escapeXML(text)
162                     )
163                                 ));
164                         });
165
166                         return tags;
167                 },
168                 'path' : function(node) {
169                         var initial = ( node.matrix.a === 1 && node.matrix.d === 1 ) ? {} : { 'transform' : node.matrix.toString() };
170
171                         return tag(
172                                 'path',
173                                 reduce(
174                                         node.attrs,
175                                         function(initial, value, name) {
176                                                 if ( name === 'path' ) name = 'd';
177
178                                                 initial[name] = value.toString();
179
180                                                 return initial;
181                                         },
182                                         {}
183                                 ),
184                                 node.matrix
185                                 );
186                 }
187                 // Other serializers should go here
188         };
189
190         R.fn.toSVG = function() {
191                 var
192                         paper   = this,
193                         restore = { svg: R.svg, vml: R.vml },
194                         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 + '">'
195                         ;
196
197                 R.svg = true;
198                 R.vml = false;
199
200                 for ( var node = paper.bottom; node != null; node = node.next ) {
201                         if ( node.node.style.display === 'none' ) continue;
202
203                         var attrs = '';
204
205                         // Use serializer
206                         if ( typeof serializer[node.type] === 'function' ) {
207                                 svg += serializer[node.type](node);
208
209                                 continue;
210                         }
211
212                         switch ( node.type ) {
213                                 case 'image':
214                                         attrs += ' preserveAspectRatio="none"';
215                                         break;
216                         }
217
218                         for ( i in node.attrs ) {
219                                 var name = i;
220
221                                 switch ( i ) {
222                                         case 'src':
223                                                 name = 'xlink:href';
224
225                                                 break;
226                                         case 'transform':
227                                                 name = '';
228
229                                                 break;
230                                 }
231
232                                 if ( name ) {
233                                         attrs += ' ' + name + '="' + escapeXML(node.attrs[i].toString()) + '"';
234                                 }
235                         }
236
237                         svg += '<' + node.type + ' transform="matrix(' + node.matrix.toString().replace(/^matrix\(|\)$/g, '') + ')"' + attrs + '></' + node.type + '>';
238                 }
239
240                 svg += '</svg>';
241
242                 R.svg = restore.svg;
243                 R.vml = restore.vml;
244
245                 return svg;
246         };
247 })(window.Raphael);