2 * Raphael.Export https://github.com/ElbertF/Raphael.Export
4 * Licensed under the MIT license:
5 * http://www.opensource.org/licenses/mit-license.php
9 importz = imports['seed/importz.js'].importz;
10 Raphael = importz('Raphael');
14 * Escapes string for XML interpolation
15 * @param value string or number value to escape
16 * @returns string escaped
18 function escapeXML(s) {
19 if (typeof s === 'number')
30 for (var entity in replace) {
31 s = s.replace(new RegExp(entity, 'g'), '&' + replace[entity] + ';');
38 * Generic map function
39 * @param iterable the array or object to be mapped
40 * @param callback the callback function(element, key)
43 function map(iterable, callback) {
44 var mapped = new Array;
46 for (var i in iterable) {
47 if (iterable.hasOwnProperty(i)) {
48 var value = callback.call(this, iterable[i], i);
59 * Generic reduce function
60 * @param iterable array or object to be reduced
61 * @param callback the callback function(initial, element, i)
62 * @param initial the initial value
63 * @return the reduced value
65 function reduce(iterable, callback, initial) {
66 for (var i in iterable) {
67 if (iterable.hasOwnProperty(i)) {
68 initial = callback.call(this, initial, iterable[i], i);
76 * Utility method for creating a tag
77 * @param name the tag name, e.g., 'text'
78 * @param attrs the attribute string, e.g., name1="val1" name2="val2"
79 * or attribute map, e.g., { name1 : 'val1', name2 : 'val2' }
80 * @param content the content string inside the tag
81 * @returns string of the tag
83 function tag(name, attrs, matrix, content) {
84 if (typeof content === 'undefined' || content === null) {
88 if (typeof attrs === 'object') {
89 attrs = map(attrs, function (element, name) {
90 if (name === 'transform')
93 return name + '="' + escapeXML(element) + '"';
97 return '<' + name + (matrix ? ' transform="matrix(' + matrix.toString().replace(/^matrix\(|\)$/g, '') + ')" ' : ' ') + attrs + '>' +
99 '</' + name + '>' + "\n";
103 * @return object the style object
105 function extractStyle(node) {
106 //Roo.log(JSON.stringify(style));
109 family: typeof node.attrs['font-family'] === 'undefined' ? null : node.attrs['font-family'],
110 size: typeof node.attrs['font-size'] === 'undefined' ? null : (node.attrs['font-size']),
111 anchor: typeof node.attrs['text-anchor'] === 'undefined' ? null : node.attrs['text-anchor'],
117 * @param style object from style()
120 function styleToString(style) {
121 // TODO figure out what is 'normal'
122 //Roo.log(JSON.stringify(style));
124 'font-family:' + style.font.family,
125 'font-weight:normal',
127 'font-stretch:normal',
128 'font-variant:normal'
130 if (style.font.size !== null) {
131 r.push('font-size: ' + style.font.size + 'px')
139 * Computes tspan dy using font size. This formula was empircally determined
140 * using a best-fit line. Works well in both VML and SVG browsers.
141 * @param fontSize number
144 function computeTSpanDy(fontSize, line, lines) {
145 if (fontSize === null)
148 //return fontSize * 4.5 / 13
149 return fontSize * 4.5 / 13 * (line - .2 - lines / 2) * 4.5;
153 'text': function (node) {
154 var style = extractStyle(node);
156 var tags = new Array;
160 var textArray = node.attrs['text'].split('\n');
162 textArray.forEach(function(v,k) {
163 content.push(tag('tspan',
166 dy: computeTSpanDy(style.font.size, k + 1, textArray.length)
178 function (initial, value, name) {
179 if (name !== 'text' && name !== 'w' && name !== 'h') {
180 if (name === 'font-size')
181 value = value + 'px';
183 initial[name] = escapeXML(value.toString());
189 style: 'text-anchor: ' + (style.font.anchor ? (style.font.anchor + ';') : 'middle;') +
190 styleToString(style) + ';'
199 'path': function (node) {
200 var initial = (node.matrix.a === 1 && node.matrix.d === 1) ? {} : {'transform': node.matrix.toString()};
208 function (initial, value, name) {
209 if (name === 'path') {
213 initial[name] = (typeof (value) == 'undefined') ? '' : value.toString();
218 style: 'fill:' + Raphael.color(node.attrs.fill).hex + ';'
224 // Other serializers should go here
229 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 + '">';
232 for (var node = paper.bottom; node != null; node = node.next) {
233 //if ( node.node.style.display === 'none' ) continue;
238 if (typeof serializer[node.type] === 'function') {
239 svg += serializer[node.type](node);
246 attrs += ' preserveAspectRatio="none"';
249 //Roo.log(JSON.stringify(node, null,4));
251 for (i in node.attrs) {
254 var val = node.attrs[i].toString();
266 //s(JSON.stringify(node, null,4));
267 val = typeof (node.node.attributes[i]) == 'undefined' ? val : node.node.attributes[i];
269 val = Raphael.color.getRGB(val).hex;
270 //Roo.log("fill: " + val);
275 attrs += ' ' + name + '="' + escapeXML(val) + '"';
279 svg += '<' + node.type + ' transform="matrix(' + node.matrix.toString().replace(/^matrix\(|\)$/g, '') + ')"' + attrs + '></' + node.type + '>' + "\n";