Mercurial > MadButterfly
annotate nodejs/svg.js @ 646:3a1e80de44ff
Add image support
author | wycc |
---|---|
date | Thu, 29 Jul 2010 00:12:18 +0800 |
parents | e517bbefe0e9 |
children | 492da72e6537 |
rev | line source |
---|---|
624 | 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); | |
646 | 5 var ldr = mbfly.img_ldr_new("."); |
624 | 6 |
7 function MB_loadSVG(mb_rt,root,filename) { | |
8 var doc = libxml.parseXmlFile(filename); | |
9 var nodes = doc.root().childNodes(); | |
10 var coord = mb_rt.coord_new(root); | |
11 var k; | |
12 | |
13 for(k in nodes) { | |
14 var n = nodes[k].name(); | |
15 if (n == "defs") { | |
16 _MB_parseDefs(root,nodes[k]); | |
17 } else if (n == "g") { | |
18 _MB_parseGroup(root,'root_coord',nodes[k]); | |
19 } | |
20 } | |
21 } | |
22 | |
23 function getInteger(n,name) | |
24 { | |
25 if (n == null) return 0; | |
26 var a = n.attr(name); | |
27 if (a==null) return 0; | |
28 return parseInt(a.value()); | |
29 } | |
631 | 30 function parsePointSize(s) |
31 { | |
32 var fs=0; | |
33 var i; | |
624 | 34 |
631 | 35 for(i=0;i<s.length;i++) { |
36 if (s[i]<'0' || s[i] > '9') break; | |
37 fs = fs*10 + (s[i]-'0'); | |
38 } | |
39 return fs; | |
40 | |
41 } | |
42 | |
43 | |
44 function parseColor(c) | |
45 { | |
46 if (c[0] == '#') { | |
47 return parseInt(c.substring(1,3),16)<<16 | parseInt(c.substring(3,5),16)<<8 | parseInt(c.substring(5,7),16); | |
48 } | |
49 } | |
50 function parseTextStyle(style,n) | |
624 | 51 { |
625
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
52 var attr; |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
53 if (n) { |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
54 attr = n.attr('style'); |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
55 } else { |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
56 attr = null; |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
57 } |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
58 if (attr == null) { |
631 | 59 return; |
625
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
60 } |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
61 var f = attr.value().split(';'); |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
62 |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
63 for(i in f) { |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
64 var kv = f[i].split(':'); |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
65 if (kv[0] == 'font-size') { |
631 | 66 style.fs = parsePointSize(kv[1]); |
625
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
67 } else if (kv[0] == "font-style") { |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
68 } else if (kv[0] == "font-weight") { |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
69 } else if (kv[0] == "fill") { |
631 | 70 style.color = parseColor(kv[1]); |
625
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
71 } else if (kv[0] == "fill-opacity") { |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
72 } else if (kv[0] == "stroke") { |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
73 } else if (kv[0] == "stroke-width") { |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
74 } else if (kv[0] == "stroke-linecap") { |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
75 } else if (kv[0] == "stroke-linejoin") { |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
76 } else if (kv[0] == "stroke-lineopacity") { |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
77 } else if (kv[0] == "font-family") { |
631 | 78 style.family = kv[1]; |
625
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
79 } else if (kv[0] == "font-stretch") { |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
80 } else if (kv[0] == "font-variant") { |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
81 } else if (kv[0] == "text-anchor") { |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
82 } else if (kv[0] == "text-align") { |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
83 } else if (kv[0] == "writing-mode") { |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
84 } else if (kv[0] == "line-height") { |
632 | 85 } else { |
625
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
86 sys.puts("Unknown style: "+kv[0]); |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
87 } |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
88 } |
624 | 89 } |
90 | |
631 | 91 function _MB_parseTSpan(coord, n,style) |
624 | 92 { |
93 var x = getInteger(n,'x'); | |
94 var y = getInteger(n,'y'); | |
95 var tcoord = mb_rt.coord_new(coord); | |
96 var nodes = n.childNodes(); | |
97 var k; | |
98 | |
99 sys.puts(n.text()); | |
100 var obj = mb_rt.stext_new(n.text(),x,y); | |
631 | 101 parseTextStyle(style,n); |
102 style.paint = mb_rt.paint_color_new(1,1,1,1); | |
103 style.face=mb_rt.font_face_query(style.family, 2, 100); | |
104 obj.set_style([[20,style.face,style.fs]]); | |
105 style.paint.fill(obj); | |
624 | 106 tcoord.add_shape(obj); |
107 for(k in nodes) { | |
108 var name = nodes[k].name(); | |
109 if (name == "tspan") { | |
110 _MB_parseTSpan(tcoord,nodes[k]); | |
111 } else { | |
112 } | |
113 } | |
114 } | |
115 | |
116 function _MB_parseText(coord,id, n) | |
117 { | |
118 var x = getInteger(n,'x'); | |
119 var y = getInteger(n,'y'); | |
120 var tcoord = mb_rt.coord_new(coord); | |
631 | 121 var style = new Object(); |
122 style.fs = 20; | |
123 style.family = 'courier'; | |
124 parseTextStyle(style,n); | |
624 | 125 var nodes = n.childNodes(); |
126 var k; | |
127 for(k in nodes) { | |
128 var n = nodes[k].name(); | |
129 if (n == "tspan") { | |
631 | 130 _MB_parseTSpan(tcoord,nodes[k],style); |
624 | 131 } else { |
132 } | |
133 } | |
134 | |
135 | |
136 } | |
137 | |
138 | |
139 function parseTransform(coord, s) | |
140 { | |
141 var off = s.indexOf('translate'); | |
142 if (off != -1) { | |
143 var ss = s.substring(off+9); | |
144 for(i=0;i<ss.length;i++) { | |
145 if (ss[i] == '(') break; | |
146 } | |
147 ss = ss.substring(i+1); | |
148 for(i=0;i<ss.length;i++) { | |
149 if (ss[i] == ')') { | |
150 ss = ss.substring(0,i); | |
151 break; | |
152 } | |
153 } | |
154 var f = ss.split(','); | |
155 var x,y; | |
156 x = parseInt(f[0]); | |
157 y = parseInt(f[1]); | |
158 coord[2] = x; | |
159 coord[5] = y; | |
160 } | |
161 off = s.indexOf('matrix'); | |
162 if (off != -1) { | |
163 sys.puts("matrix"); | |
164 } | |
165 } | |
166 | |
167 function _MB_parseRect(coord, id, n) | |
168 { | |
169 | |
170 } | |
171 | |
172 function _MB_parseGroup(root, group_id, n) | |
173 { | |
174 var k; | |
175 var nodes = n.childNodes(); | |
176 var coord = mb_rt.coord_new(root); | |
177 // Parse the transform and style here | |
178 var trans = n.attr('transform'); | |
179 if (trans!=null) { | |
180 parseTransform(coord, trans.value()); | |
181 } | |
182 | |
183 for(k in nodes) { | |
184 var n = nodes[k].name(); | |
185 var attr = nodes[k].attr('id'); | |
186 var id; | |
187 if (attr) { | |
188 id = attr.value(); | |
189 } | |
190 if (n == "g") { | |
191 _MB_parseGroup(coord, id, nodes[k]); | |
192 } else if (n == "text") { | |
193 _MB_parseText(coord, id, nodes[k]); | |
194 } else if (n == "rect") { | |
195 _MB_parseRect(coord, id, nodes[k]); | |
646 | 196 } else if (n == "image") { |
197 _MB_parseImage(coord, id, nodes[k]); | |
624 | 198 } |
199 } | |
200 | |
201 } | |
202 | |
646 | 203 function _MB_parseImage(coord,id, n) |
204 { | |
205 sys.puts("---> image"); | |
206 var ref = n.attr('href').value(); | |
207 | |
208 if (ref == null) return; | |
209 sys.puts(ref); | |
210 if (ref.substr(0,7) != "file://") { | |
211 return; | |
212 } | |
213 ref = ref.substring(7); | |
214 sys.puts("Load image "+ref); | |
215 var w; | |
216 var h; | |
217 var x,y; | |
218 | |
219 w = n.attr("width"); | |
220 if (w == null) return; | |
221 w = parseInt(w.value()); | |
222 h = n.attr("height"); | |
223 if (h == null) return; | |
224 h = parseInt(h.value()); | |
225 x = n.attr("x"); | |
226 if (x == null) return; | |
227 x = parseInt(x.value()); | |
228 y = n.attr("y"); | |
229 if (y == null) return; | |
230 y = parseInt(y.value()); | |
231 sys.puts("x="+x+",y="+y+",w="+w+",h="+h); | |
232 var img = mb_rt.image_new(x,y,w,h); | |
233 var img_data = ldr.load(ref); | |
234 sys.puts(img_data); | |
235 var paint = mb_rt.paint_image_new(img_data); | |
236 paint.fill(img); | |
237 coord.add_shape(img); | |
238 } | |
624 | 239 |
240 function _MB_parseDefs(root,n) | |
241 { | |
242 var k; | |
243 var nodes = n.childNodes(); | |
244 | |
245 for(k in nodes) { | |
246 var name = nodes[k].name(); | |
247 if (name == "linearGradient") { | |
248 //_MB_parseLinearGradient(root,nodes[k]); | |
249 } | |
250 } | |
251 } | |
252 | |
253 | |
254 MB_loadSVG(mb_rt,mb_rt.root,"test.svg"); | |
255 mb_rt.redraw_all(); |