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         
115         var r = [
116                 'font-family:' + style.font.family,
117                 'font-weight:normal',
118                 'font-style:normal',
119                 'font-stretch:normal',
120                 'font-variant:normal'
121         ];
122         if (style.font.size !== null ) {
123                 r.push('font-size: ' + style.font.size + 'px') 
124         }
125         
126         return r.join(';')
127         
128         }
129
130         /**
131         * Computes tspan dy using font size. This formula was empircally determined
132         * using a best-fit line. Works well in both VML and SVG browsers.
133         * @param fontSize number
134         * @return number
135         */
136         function computeTSpanDy(fontSize, line, lines) {
137                 if ( fontSize === null ) fontSize = 10;
138
139                 //return fontSize * 4.5 / 13
140                 return fontSize * 4.5 / 13 * ( line - .2 - lines / 2 ) * 3.5;
141         }
142
143         var serializer = {
144                 'text': function(node) {
145                         style = extractStyle(node);
146             Roo.log(style);
147                         var tags = new Array;
148
149                         map(node.attrs['text'].split('\n'), function(text, iterable, line) {
150                 line = line || 0;
151                                 tags.push(tag(
152                                         'text',
153                                         reduce(
154                                                 node.attrs,
155                                                 function(initial, value, name) {
156                                                         if ( name !== 'text' && name !== 'w' && name !== 'h' ) {
157                                                                 if ( name === 'font-size') value = value + 'px';
158
159                                                                 initial[name] = escapeXML(value.toString());
160                                                         }
161
162                                                         return initial;
163                                                 },
164                                                 {
165                                 style: 'text-anchor: middle; ' + styleToString(style) + ';' }
166                                                 ),
167                                         node.matrix,
168                                         tag('tspan',
169                             {
170                                 dy: computeTSpanDy(style.font.size, line + 1, node.attrs['text'].split('\n').length)
171                             },
172                             null,
173                             escapeXML(text)
174                     )
175                                 ));
176                         });
177
178                         return tags;
179                 },
180                 'path' : function(node) {
181                         var initial = ( node.matrix.a === 1 && node.matrix.d === 1 ) ? {} : { 'transform' : node.matrix.toString() };
182
183                         return tag(
184                                 'path',
185                                 reduce(
186                                         node.attrs,
187                                         function(initial, value, name) {
188                                                 if ( name === 'path' ) name = 'd';
189
190                                                 initial[name] = value.toString();
191
192                                                 return initial;
193                                         },
194                                         {}
195                                 ),
196                                 node.matrix
197                                 );
198                 }
199                 // Other serializers should go here
200         };
201
202         R.fn.toSVG = function() {
203                 var
204                         paper   = this,
205                         restore = { svg: R.svg, vml: R.vml },
206                         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 + '">'
207                         ;
208
209                 R.svg = true;
210                 R.vml = false;
211
212                 for ( var node = paper.bottom; node != null; node = node.next ) {
213                         if ( node.node.style.display === 'none' ) continue;
214
215                         var attrs = '';
216
217                         // Use serializer
218                         if ( typeof serializer[node.type] === 'function' ) {
219                                 svg += serializer[node.type](node);
220
221                                 continue;
222                         }
223
224                         switch ( node.type ) {
225                                 case 'image':
226                                         attrs += ' preserveAspectRatio="none"';
227                                         break;
228                         }
229
230                         for ( i in node.attrs ) {
231                                 var name = i;
232
233                                 switch ( i ) {
234                                         case 'src':
235                                                 name = 'xlink:href';
236
237                                                 break;
238                                         case 'transform':
239                                                 name = '';
240
241                                                 break;
242                                 }
243
244                                 if ( name ) {
245                                         attrs += ' ' + name + '="' + escapeXML(node.attrs[i].toString()) + '"';
246                                 }
247                         }
248
249                         svg += '<' + node.type + ' transform="matrix(' + node.matrix.toString().replace(/^matrix\(|\)$/g, '') + ')"' + attrs + '></' + node.type + '>';
250                 }
251
252                 svg += '</svg>';
253
254                 R.svg = restore.svg;
255                 R.vml = restore.vml;
256
257                 return svg;
258         };
259 })(window.Raphael);