Mercurial > MadButterfly
comparison nodejs/svg.js @ 714:f53e45d1fcd0
Translate the svg.js as a nodejs module.
author | wycc |
---|---|
date | Fri, 13 Aug 2010 23:38:29 +0800 |
parents | e60ae262127b |
children | b822b1912d67 |
comparison
equal
deleted
inserted
replaced
713:e60ae262127b | 714:f53e45d1fcd0 |
---|---|
1 var libxml = require('libxmljs'); | 1 var libxml = require('libxmljs'); |
2 var sys=require('sys'); | 2 var sys=require('sys'); |
3 var mbfly = require("mbfly"); | 3 var mbfly = require("mbfly"); |
4 var mb_rt = new mbfly.mb_rt(":0.0", 720,480); | |
5 var ldr = mbfly.img_ldr_new("."); | 4 var ldr = mbfly.img_ldr_new("."); |
6 var background = mb_rt.rect_new(0, 0, 720, 480, 0, 0); | 5 |
7 var paint = mb_rt.paint_color_new(1, 1, 1, 1); | |
8 paint.fill(background); | |
9 mb_rt.root.add_shape(background); | |
10 | 6 |
11 var _std_colors = { | 7 var _std_colors = { |
12 "white": [1, 1, 1], | 8 "white": [1, 1, 1], |
13 "black": [0, 0, 0], | 9 "black": [0, 0, 0], |
14 "red": [1, 0, 0] | 10 "red": [1, 0, 0] |
15 }; | 11 }; |
16 | 12 |
17 function MB_loadSVG(mb_rt,root,filename) { | 13 exports.loadSVG=function(mb_rt,root,filename) { |
18 var doc = libxml.parseXmlFile(filename); | 14 var doc = libxml.parseXmlFile(filename); |
19 var nodes = doc.root().childNodes(); | 15 var nodes = doc.root().childNodes(); |
20 var coord = mb_rt.coord_new(root); | 16 var coord = mb_rt.coord_new(root); |
21 var k; | 17 var k; |
18 this.mb_rt = mb_rt; | |
22 | 19 |
23 for(k in nodes) { | 20 for(k in nodes) { |
24 var n = nodes[k].name(); | 21 var n = nodes[k].name(); |
25 if (n == "defs") { | 22 if (n == "defs") { |
26 _MB_parseDefs(root,nodes[k]); | 23 this.parseDefs(root,nodes[k]); |
27 } else if (n == "g") { | 24 } else if (n == "g") { |
28 _MB_parseGroup(root,'root_coord',nodes[k]); | 25 this.parseGroup(root,'root_coord',nodes[k]); |
29 } | 26 } |
30 } | 27 } |
31 } | 28 } |
32 | 29 |
33 function getInteger(n,name) | 30 function getInteger(n,name) |
97 sys.puts("Unknown style: "+kv[0]); | 94 sys.puts("Unknown style: "+kv[0]); |
98 } | 95 } |
99 } | 96 } |
100 } | 97 } |
101 | 98 |
102 function _MB_parseTSpan(coord, n,style) | 99 exports.parseTSpan=function(coord, n,style) |
103 { | 100 { |
104 var x = getInteger(n,'x'); | 101 var x = getInteger(n,'x'); |
105 var y = getInteger(n,'y'); | 102 var y = getInteger(n,'y'); |
106 var tcoord = mb_rt.coord_new(coord); | 103 var tcoord = this.mb_rt.coord_new(coord); |
107 var nodes = n.childNodes(); | 104 var nodes = n.childNodes(); |
108 var k; | 105 var k; |
109 | 106 |
110 sys.puts(n.text()); | 107 sys.puts(n.text()); |
111 var obj = mb_rt.stext_new(n.text(),x,y); | 108 var obj = this.mb_rt.stext_new(n.text(),x,y); |
112 parseTextStyle(style,n); | 109 parseTextStyle(style,n); |
113 style.paint = mb_rt.paint_color_new(1,1,1,1); | 110 style.paint = this.mb_rt.paint_color_new(1,1,1,1); |
114 style.face=mb_rt.font_face_query(style.family, 2, 100); | 111 style.face=this.mb_rt.font_face_query(style.family, 2, 100); |
115 obj.set_style([[20,style.face,style.fs]]); | 112 obj.set_style([[20,style.face,style.fs]]); |
116 style.paint.fill(obj); | 113 style.paint.fill(obj); |
117 tcoord.add_shape(obj); | 114 tcoord.add_shape(obj); |
118 for(k in nodes) { | 115 for(k in nodes) { |
119 var name = nodes[k].name(); | 116 var name = nodes[k].name(); |
120 if (name == "tspan") { | 117 if (name == "tspan") { |
121 _MB_parseTSpan(tcoord,nodes[k]); | 118 this.parseTSpan(tcoord,nodes[k]); |
122 } else { | 119 } else { |
123 } | 120 } |
124 } | 121 } |
125 } | 122 } |
126 | 123 |
127 function _prepare_paint_color(color, alpha) { | 124 exports._prepare_paint_color=function(color, alpha) { |
128 var paint; | 125 var paint; |
129 var c; | 126 var c; |
130 | 127 |
131 if (color[0]=='#') { | 128 if (color[0]=='#') { |
132 var r,g,b; | 129 var r,g,b; |
133 r = parseInt(color.substring(1,3),16)/256; | 130 r = parseInt(color.substring(1,3),16)/256; |
134 g = parseInt(color.substring(3,5),16)/256; | 131 g = parseInt(color.substring(3,5),16)/256; |
135 b = parseInt(color.substring(5,7),16)/256; | 132 b = parseInt(color.substring(5,7),16)/256; |
136 paint = mb_rt.paint_color_new(r, g, b, alpha); | 133 paint = this.mb_rt.paint_color_new(r, g, b, alpha); |
137 } else if(_std_colors[color]) { | 134 } else if(_std_colors[color]) { |
138 c = _std_colors[color]; | 135 c = _std_colors[color]; |
139 paint = mb_rt.paint_color_new(c[0], c[1], c[2], alpha); | 136 paint = this.mb_rt.paint_color_new(c[0], c[1], c[2], alpha); |
140 } else { | 137 } else { |
141 paint = mb_rt.paint_color_new(0,0,0,1); | 138 paint = this.mb_rt.paint_color_new(0,0,0,1); |
142 } | 139 } |
143 return paint; | 140 return paint; |
144 } | 141 } |
145 | 142 |
146 function _MB_parsePath(coord,id, n) | 143 exports.parsePath=function(coord,id, n) |
147 { | 144 { |
148 var d = n.attr('d').value(); | 145 var d = n.attr('d').value(); |
149 var style = n.attr('style'); | 146 var style = n.attr('style'); |
150 var path = mb_rt.path_new(d); | 147 var path = this.mb_rt.path_new(d); |
151 var paint; | 148 var paint; |
152 | 149 |
153 if (style==null) { | 150 if (style==null) { |
154 paint = mb_rt.paint_color_new(0,0,0,1); | 151 paint = this.mb_rt.paint_color_new(0,0,0,1); |
155 paint.fill(path); | 152 paint.fill(path); |
156 } else { | 153 } else { |
157 var items = style.value().split(';'); | 154 var items = style.value().split(';'); |
158 var fill_alpha = 1; | 155 var fill_alpha = 1; |
159 var stroke_alpha = 1; | 156 var stroke_alpha = 1; |
180 stroke_alpha = parseFloat(f[1]); | 177 stroke_alpha = parseFloat(f[1]); |
181 } | 178 } |
182 } | 179 } |
183 | 180 |
184 if(fill_color) { | 181 if(fill_color) { |
185 paint = _prepare_paint_color(fill_color, fill_alpha); | 182 paint = this._prepare_paint_color(fill_color, fill_alpha); |
186 paint.fill(path); | 183 paint.fill(path); |
187 } | 184 } |
188 if(stroke_color) { | 185 if(stroke_color) { |
189 paint = _prepare_paint_color(stroke_color, stroke_alpha); | 186 paint = this._prepare_paint_color(stroke_color, stroke_alpha); |
190 paint.stroke(path); | 187 paint.stroke(path); |
191 } | 188 } |
192 if(!stroke_color && !fill_color) { | 189 if(!stroke_color && !fill_color) { |
193 paint = mb_rt.paint_color_new(0, 0, 0, 1); | 190 paint = this.mb_rt.paint_color_new(0, 0, 0, 1); |
194 paint.fill(path); | 191 paint.fill(path); |
195 } | 192 } |
196 | 193 |
197 } | 194 } |
198 coord.add_shape(path); | 195 coord.add_shape(path); |
199 } | 196 } |
200 | 197 |
201 function _MB_parseText(coord,id, n) | 198 exports.parseText=function(coord,id, n) |
202 { | 199 { |
203 var x = getInteger(n,'x'); | 200 var x = getInteger(n,'x'); |
204 var y = getInteger(n,'y'); | 201 var y = getInteger(n,'y'); |
205 var tcoord = mb_rt.coord_new(coord); | 202 var tcoord = this.mb_rt.coord_new(coord); |
206 var style = new Object(); | 203 var style = new Object(); |
207 style.fs = 20; | 204 style.fs = 20; |
208 style.family = 'courier'; | 205 style.family = 'courier'; |
209 parseTextStyle(style,n); | 206 parseTextStyle(style,n); |
210 var nodes = n.childNodes(); | 207 var nodes = n.childNodes(); |
211 var k; | 208 var k; |
212 for(k in nodes) { | 209 for(k in nodes) { |
213 var n = nodes[k].name(); | 210 var n = nodes[k].name(); |
214 if (n == "tspan") { | 211 if (n == "tspan") { |
215 _MB_parseTSpan(tcoord,nodes[k],style); | 212 this.parseTSpan(tcoord,nodes[k],style); |
216 } else { | 213 } else { |
217 } | 214 } |
218 } | 215 } |
219 | 216 |
220 | 217 |
258 coord[4] = parseFloat(fields[3]); | 255 coord[4] = parseFloat(fields[3]); |
259 coord[5] = parseFloat(fields[5]); | 256 coord[5] = parseFloat(fields[5]); |
260 } | 257 } |
261 } | 258 } |
262 | 259 |
263 function _MB_parseRect(coord, id, n) | 260 exports.parseRect=function(coord, id, n) |
264 { | 261 { |
265 var x = getInteger(n,'x'); | 262 var x = getInteger(n,'x'); |
266 var y = getInteger(n,'y'); | 263 var y = getInteger(n,'y'); |
267 var w = getInteger(n,'width'); | 264 var w = getInteger(n,'width'); |
268 var h = getInteger(n,'height'); | 265 var h = getInteger(n,'height'); |
269 var paint; | 266 var paint; |
270 | 267 |
271 var style = n.attr('style'); | 268 var style = n.attr('style'); |
272 | 269 |
273 if (style==null) { | 270 if (style==null) { |
274 paint = mb_rt.paint_color_new(0,0,0,0.1); | 271 paint = this.mb_rt.paint_color_new(0,0,0,0.1); |
275 } else { | 272 } else { |
276 var items = style.value().split(';'); | 273 var items = style.value().split(';'); |
277 var fill = ''; | 274 var fill = ''; |
278 var alpha = 1; | 275 var alpha = 1; |
279 for(i in items) { | 276 for(i in items) { |
295 r = parseInt(fill.substring(1,3),16)/256; | 292 r = parseInt(fill.substring(1,3),16)/256; |
296 g = parseInt(fill.substring(3,5),16)/256; | 293 g = parseInt(fill.substring(3,5),16)/256; |
297 b = parseInt(fill.substring(5,7),16)/256; | 294 b = parseInt(fill.substring(5,7),16)/256; |
298 sys.puts("r="+r+" g="+g+" b="+b+" a="+alpha); | 295 sys.puts("r="+r+" g="+g+" b="+b+" a="+alpha); |
299 | 296 |
300 paint = mb_rt.paint_color_new(r,g,b,parseFloat(alpha)); | 297 paint = this.mb_rt.paint_color_new(r,g,b,parseFloat(alpha)); |
301 } else { | 298 } else { |
302 paint = mb_rt.paint_color_new(0,0,0,1); | 299 paint = this.mb_rt.paint_color_new(0,0,0,1); |
303 } | 300 } |
304 } | 301 } |
305 var rect = mb_rt.rect_new(x,y,w,h,10, 10); | 302 var rect = this.mb_rt.rect_new(x,y,w,h,10, 10); |
306 sys.puts("rect x="+x+" y="+y+" w="+w+" h="+h); | 303 sys.puts("rect x="+x+" y="+y+" w="+w+" h="+h); |
307 paint.fill(rect); | 304 paint.fill(rect); |
308 coord.add_shape(rect); | 305 coord.add_shape(rect); |
309 } | 306 } |
310 | 307 |
311 function _MB_parseGroup(root, group_id, n) | 308 exports.parseGroup=function(root, group_id, n) |
312 { | 309 { |
313 var k; | 310 var k; |
314 var nodes = n.childNodes(); | 311 var nodes = n.childNodes(); |
315 var coord = mb_rt.coord_new(root); | 312 var coord = this.mb_rt.coord_new(root); |
316 // Parse the transform and style here | 313 // Parse the transform and style here |
317 var trans = n.attr('transform'); | 314 var trans = n.attr('transform'); |
318 if (trans!=null) { | 315 if (trans!=null) { |
319 parseTransform(coord, trans.value()); | 316 parseTransform(coord, trans.value()); |
320 } | 317 } |
325 var id; | 322 var id; |
326 if (attr) { | 323 if (attr) { |
327 id = attr.value(); | 324 id = attr.value(); |
328 } | 325 } |
329 if (n == "g") { | 326 if (n == "g") { |
330 _MB_parseGroup(coord, id, nodes[k]); | 327 this.parseGroup(coord, id, nodes[k]); |
331 } else if (n == "path") { | 328 } else if (n == "path") { |
332 _MB_parsePath(coord, id, nodes[k]); | 329 this.parsePath(coord, id, nodes[k]); |
333 } else if (n == "text") { | 330 } else if (n == "text") { |
334 _MB_parseText(coord, id, nodes[k]); | 331 this.parseText(coord, id, nodes[k]); |
335 } else if (n == "rect") { | 332 } else if (n == "rect") { |
336 _MB_parseRect(coord, id, nodes[k]); | 333 this.parseRect(coord, id, nodes[k]); |
337 } else if (n == "image") { | 334 } else if (n == "image") { |
338 _MB_parseImage(coord, id, nodes[k]); | 335 this.parseImage(coord, id, nodes[k]); |
339 } | 336 } |
340 } | 337 } |
341 | 338 |
342 } | 339 } |
343 | 340 |
344 function _MB_parseImage(coord,id, n) | 341 exports.parseImage=function(coord,id, n) |
345 { | 342 { |
346 sys.puts("---> image"); | 343 sys.puts("---> image"); |
347 var ref = n.attr('href').value(); | 344 var ref = n.attr('href').value(); |
348 | 345 |
349 if (ref == null) return; | 346 if (ref == null) return; |
371 x = parseInt(x.value()); | 368 x = parseInt(x.value()); |
372 y = n.attr("y"); | 369 y = n.attr("y"); |
373 if (y == null) return; | 370 if (y == null) return; |
374 y = parseInt(y.value()); | 371 y = parseInt(y.value()); |
375 sys.puts("x="+x+",y="+y+",w="+w+",h="+h); | 372 sys.puts("x="+x+",y="+y+",w="+w+",h="+h); |
376 var img = mb_rt.image_new(x,y,w,h); | 373 var img = this.mb_rt.image_new(x,y,w,h); |
377 var img_data = ldr.load(ref); | 374 var img_data = ldr.load(ref); |
378 sys.puts(img_data); | 375 sys.puts(img_data); |
379 var paint = mb_rt.paint_image_new(img_data); | 376 var paint = this.mb_rt.paint_image_new(img_data); |
380 paint.fill(img); | 377 paint.fill(img); |
381 coord.add_shape(img); | 378 coord.add_shape(img); |
382 } | 379 } |
383 | 380 |
384 function _MB_parseDefs(root,n) | 381 exports.parseDefs=function(root,n) |
385 { | 382 { |
386 var k; | 383 var k; |
387 var nodes = n.childNodes(); | 384 var nodes = n.childNodes(); |
388 | 385 |
389 for(k in nodes) { | 386 for(k in nodes) { |
393 } | 390 } |
394 } | 391 } |
395 } | 392 } |
396 | 393 |
397 | 394 |
398 MB_loadSVG(mb_rt,mb_rt.root,"test.svg"); | |
399 mb_rt.redraw_all(); | |
400 mb_rt.flush(); |