Mercurial > MadButterfly
comparison nodejs/svg.js @ 624:d45c928f6523
Add SVG parser sample code.
author | wycc |
---|---|
date | Thu, 15 Jul 2010 22:34:04 +0800 |
parents | |
children | 9f2080b68f8e |
comparison
equal
deleted
inserted
replaced
577:d561b2415711 | 624:d45c928f6523 |
---|---|
1 var libxml = require('libxmljs'); | |
2 var sys=require('sys'); | |
3 var mbfly = require("mbfly"); | |
4 var mb_rt = new mbfly.mb_rt(":0.0", 720,480); | |
5 | |
6 function MB_loadSVG(mb_rt,root,filename) { | |
7 var doc = libxml.parseXmlFile(filename); | |
8 var nodes = doc.root().childNodes(); | |
9 var coord = mb_rt.coord_new(root); | |
10 var k; | |
11 | |
12 for(k in nodes) { | |
13 var n = nodes[k].name(); | |
14 if (n == "defs") { | |
15 _MB_parseDefs(root,nodes[k]); | |
16 } else if (n == "g") { | |
17 _MB_parseGroup(root,'root_coord',nodes[k]); | |
18 } | |
19 } | |
20 } | |
21 | |
22 function getInteger(n,name) | |
23 { | |
24 if (n == null) return 0; | |
25 var a = n.attr(name); | |
26 if (a==null) return 0; | |
27 return parseInt(a.value()); | |
28 } | |
29 | |
30 function parseStyle(n) | |
31 { | |
32 } | |
33 | |
34 function _MB_parseTSpan(coord, n) | |
35 { | |
36 var x = getInteger(n,'x'); | |
37 var y = getInteger(n,'y'); | |
38 var tcoord = mb_rt.coord_new(coord); | |
39 var paint = parseStyle(n); | |
40 var nodes = n.childNodes(); | |
41 var k; | |
42 | |
43 sys.puts(n.text()); | |
44 var obj = mb_rt.stext_new(n.text(),x,y); | |
45 var paint = mb_rt.paint_color_new(1,1,1,1); | |
46 var face=mb_rt.font_face_query("courier", 2, 100); | |
47 obj.set_style([[5,face,20]]); | |
48 paint.fill(obj); | |
49 tcoord.add_shape(obj); | |
50 for(k in nodes) { | |
51 var name = nodes[k].name(); | |
52 if (name == "tspan") { | |
53 _MB_parseTSpan(tcoord,nodes[k]); | |
54 } else { | |
55 } | |
56 } | |
57 } | |
58 | |
59 function _MB_parseText(coord,id, n) | |
60 { | |
61 var x = getInteger(n,'x'); | |
62 var y = getInteger(n,'y'); | |
63 var tcoord = mb_rt.coord_new(coord); | |
64 var paint = parseStyle(n); | |
65 var nodes = n.childNodes(); | |
66 var k; | |
67 for(k in nodes) { | |
68 var n = nodes[k].name(); | |
69 if (n == "tspan") { | |
70 _MB_parseTSpan(tcoord,nodes[k]); | |
71 } else { | |
72 } | |
73 } | |
74 | |
75 | |
76 } | |
77 | |
78 | |
79 function parseTransform(coord, s) | |
80 { | |
81 var off = s.indexOf('translate'); | |
82 if (off != -1) { | |
83 var ss = s.substring(off+9); | |
84 for(i=0;i<ss.length;i++) { | |
85 if (ss[i] == '(') break; | |
86 } | |
87 ss = ss.substring(i+1); | |
88 for(i=0;i<ss.length;i++) { | |
89 if (ss[i] == ')') { | |
90 ss = ss.substring(0,i); | |
91 break; | |
92 } | |
93 } | |
94 var f = ss.split(','); | |
95 var x,y; | |
96 x = parseInt(f[0]); | |
97 y = parseInt(f[1]); | |
98 coord[2] = x; | |
99 coord[5] = y; | |
100 } | |
101 off = s.indexOf('matrix'); | |
102 if (off != -1) { | |
103 sys.puts("matrix"); | |
104 } | |
105 } | |
106 | |
107 function _MB_parseRect(coord, id, n) | |
108 { | |
109 | |
110 } | |
111 | |
112 function _MB_parseGroup(root, group_id, n) | |
113 { | |
114 var k; | |
115 var nodes = n.childNodes(); | |
116 var coord = mb_rt.coord_new(root); | |
117 // Parse the transform and style here | |
118 var trans = n.attr('transform'); | |
119 if (trans!=null) { | |
120 parseTransform(coord, trans.value()); | |
121 } | |
122 | |
123 for(k in nodes) { | |
124 var n = nodes[k].name(); | |
125 var attr = nodes[k].attr('id'); | |
126 var id; | |
127 if (attr) { | |
128 id = attr.value(); | |
129 } | |
130 if (n == "g") { | |
131 _MB_parseGroup(coord, id, nodes[k]); | |
132 } else if (n == "text") { | |
133 _MB_parseText(coord, id, nodes[k]); | |
134 } else if (n == "rect") { | |
135 _MB_parseRect(coord, id, nodes[k]); | |
136 } | |
137 } | |
138 | |
139 } | |
140 | |
141 | |
142 function _MB_parseDefs(root,n) | |
143 { | |
144 var k; | |
145 var nodes = n.childNodes(); | |
146 | |
147 for(k in nodes) { | |
148 var name = nodes[k].name(); | |
149 if (name == "linearGradient") { | |
150 //_MB_parseLinearGradient(root,nodes[k]); | |
151 } | |
152 } | |
153 } | |
154 | |
155 | |
156 MB_loadSVG(mb_rt,mb_rt.root,"test.svg"); | |
157 mb_rt.redraw_all(); |