Mercurial > MadButterfly
annotate nodejs/svg.js @ 713:e60ae262127b
Recognize color name
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Fri, 13 Aug 2010 20:41:00 +0800 |
parents | 54618e0cd36b |
children | f53e45d1fcd0 |
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("."); |
709 | 6 var background = mb_rt.rect_new(0, 0, 720, 480, 0, 0); |
7 var paint = mb_rt.paint_color_new(1, 1, 1, 1); | |
8 paint.fill(background); | |
9 mb_rt.root.add_shape(background); | |
624 | 10 |
713 | 11 var _std_colors = { |
12 "white": [1, 1, 1], | |
13 "black": [0, 0, 0], | |
14 "red": [1, 0, 0] | |
15 }; | |
16 | |
624 | 17 function MB_loadSVG(mb_rt,root,filename) { |
18 var doc = libxml.parseXmlFile(filename); | |
19 var nodes = doc.root().childNodes(); | |
20 var coord = mb_rt.coord_new(root); | |
21 var k; | |
22 | |
23 for(k in nodes) { | |
24 var n = nodes[k].name(); | |
25 if (n == "defs") { | |
26 _MB_parseDefs(root,nodes[k]); | |
27 } else if (n == "g") { | |
28 _MB_parseGroup(root,'root_coord',nodes[k]); | |
29 } | |
30 } | |
31 } | |
32 | |
33 function getInteger(n,name) | |
34 { | |
35 if (n == null) return 0; | |
36 var a = n.attr(name); | |
37 if (a==null) return 0; | |
38 return parseInt(a.value()); | |
39 } | |
631 | 40 function parsePointSize(s) |
41 { | |
42 var fs=0; | |
43 var i; | |
624 | 44 |
631 | 45 for(i=0;i<s.length;i++) { |
46 if (s[i]<'0' || s[i] > '9') break; | |
47 fs = fs*10 + (s[i]-'0'); | |
48 } | |
49 return fs; | |
50 | |
51 } | |
52 | |
53 | |
54 function parseColor(c) | |
55 { | |
56 if (c[0] == '#') { | |
57 return parseInt(c.substring(1,3),16)<<16 | parseInt(c.substring(3,5),16)<<8 | parseInt(c.substring(5,7),16); | |
58 } | |
59 } | |
60 function parseTextStyle(style,n) | |
624 | 61 { |
625
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
62 var attr; |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
63 if (n) { |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
64 attr = n.attr('style'); |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
65 } else { |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
66 attr = null; |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
67 } |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
68 if (attr == null) { |
631 | 69 return; |
625
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
70 } |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
71 var f = attr.value().split(';'); |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
72 |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
73 for(i in f) { |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
74 var kv = f[i].split(':'); |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
75 if (kv[0] == 'font-size') { |
631 | 76 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
|
77 } 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
|
78 } 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
|
79 } else if (kv[0] == "fill") { |
631 | 80 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
|
81 } else if (kv[0] == "fill-opacity") { |
703
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
82 } else if (kv[0] == "stroke-opacity") { |
625
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
83 } else if (kv[0] == "stroke") { |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
84 } 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
|
85 } 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
|
86 } 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
|
87 } 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
|
88 } else if (kv[0] == "font-family") { |
631 | 89 style.family = kv[1]; |
625
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
90 } 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
|
91 } 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
|
92 } 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
|
93 } 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
|
94 } 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
|
95 } else if (kv[0] == "line-height") { |
632 | 96 } else { |
625
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
97 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
|
98 } |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
99 } |
624 | 100 } |
101 | |
631 | 102 function _MB_parseTSpan(coord, n,style) |
624 | 103 { |
104 var x = getInteger(n,'x'); | |
105 var y = getInteger(n,'y'); | |
106 var tcoord = mb_rt.coord_new(coord); | |
107 var nodes = n.childNodes(); | |
108 var k; | |
109 | |
110 sys.puts(n.text()); | |
111 var obj = mb_rt.stext_new(n.text(),x,y); | |
631 | 112 parseTextStyle(style,n); |
113 style.paint = mb_rt.paint_color_new(1,1,1,1); | |
114 style.face=mb_rt.font_face_query(style.family, 2, 100); | |
115 obj.set_style([[20,style.face,style.fs]]); | |
116 style.paint.fill(obj); | |
624 | 117 tcoord.add_shape(obj); |
118 for(k in nodes) { | |
119 var name = nodes[k].name(); | |
120 if (name == "tspan") { | |
121 _MB_parseTSpan(tcoord,nodes[k]); | |
122 } else { | |
123 } | |
124 } | |
125 } | |
126 | |
706 | 127 function _prepare_paint_color(color, alpha) { |
128 var paint; | |
713 | 129 var c; |
706 | 130 |
131 if (color[0]=='#') { | |
132 var r,g,b; | |
133 r = parseInt(color.substring(1,3),16)/256; | |
134 g = parseInt(color.substring(3,5),16)/256; | |
135 b = parseInt(color.substring(5,7),16)/256; | |
136 paint = mb_rt.paint_color_new(r, g, b, alpha); | |
713 | 137 } else if(_std_colors[color]) { |
138 c = _std_colors[color]; | |
139 paint = mb_rt.paint_color_new(c[0], c[1], c[2], alpha); | |
706 | 140 } else { |
141 paint = mb_rt.paint_color_new(0,0,0,1); | |
142 } | |
143 return paint; | |
144 } | |
145 | |
146 function _MB_parsePath(coord,id, n) | |
147 { | |
148 var d = n.attr('d').value(); | |
149 var style = n.attr('style'); | |
150 var path = mb_rt.path_new(d); | |
151 var paint; | |
152 | |
153 if (style==null) { | |
710
c93381320d6e
Fill shapes with black if with out any stroke and fill in the style
Thinker K.F. Li <thinker@branda.to>
parents:
709
diff
changeset
|
154 paint = mb_rt.paint_color_new(0,0,0,1); |
712
54618e0cd36b
fill with black by default instead of stroke black
Thinker K.F. Li <thinker@branda.to>
parents:
710
diff
changeset
|
155 paint.fill(path); |
706 | 156 } else { |
157 var items = style.value().split(';'); | |
158 var fill_alpha = 1; | |
159 var stroke_alpha = 1; | |
160 var fill_color; | |
161 var stroke_color; | |
162 var alpha; | |
163 | |
164 for(i in items) { | |
165 sys.puts(items[i]); | |
166 var f = items[i].split(':'); | |
167 if (f[0] == 'opacity') { | |
168 alpha = f[1]; | |
169 } else if (f[0] == 'fill') { | |
170 if(f[1] != "none") | |
171 fill_color = f[1]; | |
172 } else if (f[0] == 'fill-opacity') { | |
173 fill_alpha = parseFloat(f[1]); | |
174 } else if (f[0] == 'stroke') { | |
175 if(f[1] != "none") | |
176 stroke_color = f[1]; | |
177 } else if (f[0] == 'stroke-width') { | |
178 path.stroke_width = parseFloat(f[1]); | |
179 } else if (f[0] == 'stroke-opacity') { | |
180 stroke_alpha = parseFloat(f[1]); | |
181 } | |
182 } | |
183 | |
184 if(fill_color) { | |
185 paint = _prepare_paint_color(fill_color, fill_alpha); | |
186 paint.fill(path); | |
187 } | |
188 if(stroke_color) { | |
189 paint = _prepare_paint_color(stroke_color, stroke_alpha); | |
190 paint.stroke(path); | |
191 } | |
710
c93381320d6e
Fill shapes with black if with out any stroke and fill in the style
Thinker K.F. Li <thinker@branda.to>
parents:
709
diff
changeset
|
192 if(!stroke_color && !fill_color) { |
c93381320d6e
Fill shapes with black if with out any stroke and fill in the style
Thinker K.F. Li <thinker@branda.to>
parents:
709
diff
changeset
|
193 paint = mb_rt.paint_color_new(0, 0, 0, 1); |
c93381320d6e
Fill shapes with black if with out any stroke and fill in the style
Thinker K.F. Li <thinker@branda.to>
parents:
709
diff
changeset
|
194 paint.fill(path); |
c93381320d6e
Fill shapes with black if with out any stroke and fill in the style
Thinker K.F. Li <thinker@branda.to>
parents:
709
diff
changeset
|
195 } |
706 | 196 |
197 } | |
198 coord.add_shape(path); | |
199 } | |
200 | |
624 | 201 function _MB_parseText(coord,id, n) |
202 { | |
203 var x = getInteger(n,'x'); | |
204 var y = getInteger(n,'y'); | |
205 var tcoord = mb_rt.coord_new(coord); | |
631 | 206 var style = new Object(); |
207 style.fs = 20; | |
208 style.family = 'courier'; | |
209 parseTextStyle(style,n); | |
624 | 210 var nodes = n.childNodes(); |
211 var k; | |
212 for(k in nodes) { | |
213 var n = nodes[k].name(); | |
214 if (n == "tspan") { | |
631 | 215 _MB_parseTSpan(tcoord,nodes[k],style); |
624 | 216 } else { |
217 } | |
218 } | |
219 | |
220 | |
221 } | |
222 | |
223 function parseTransform(coord, s) | |
224 { | |
225 var off = s.indexOf('translate'); | |
226 if (off != -1) { | |
227 var ss = s.substring(off+9); | |
228 for(i=0;i<ss.length;i++) { | |
229 if (ss[i] == '(') break; | |
230 } | |
231 ss = ss.substring(i+1); | |
232 for(i=0;i<ss.length;i++) { | |
233 if (ss[i] == ')') { | |
234 ss = ss.substring(0,i); | |
235 break; | |
236 } | |
237 } | |
238 var f = ss.split(','); | |
239 var x,y; | |
706 | 240 x = parseFloat(f[0]); |
241 y = parseFloat(f[1]); | |
242 coord[0] = 1; | |
243 coord[1] = 0; | |
244 coord[2] = x; | |
245 coord[3] = 0; | |
246 coord[4] = 1; | |
624 | 247 coord[5] = y; |
248 } | |
249 off = s.indexOf('matrix'); | |
250 if (off != -1) { | |
703
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
251 var end = s.indexOf(')'); |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
252 var m = s.substring(7,end); |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
253 var fields = m.split(','); |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
254 coord[0] = parseFloat(fields[0]); |
706 | 255 coord[1] = parseFloat(fields[2]); |
703
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
256 coord[2] = parseFloat(fields[4]); |
706 | 257 coord[3] = parseFloat(fields[1]); |
703
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
258 coord[4] = parseFloat(fields[3]); |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
259 coord[5] = parseFloat(fields[5]); |
624 | 260 } |
261 } | |
262 | |
263 function _MB_parseRect(coord, id, n) | |
264 { | |
703
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
265 var x = getInteger(n,'x'); |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
266 var y = getInteger(n,'y'); |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
267 var w = getInteger(n,'width'); |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
268 var h = getInteger(n,'height'); |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
269 var paint; |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
270 |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
271 var style = n.attr('style'); |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
272 |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
273 if (style==null) { |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
274 paint = mb_rt.paint_color_new(0,0,0,0.1); |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
275 } else { |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
276 var items = style.value().split(';'); |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
277 var fill = ''; |
708
ac9c20db953e
Default alpha of a tag is '1'
Thinker K.F. Li <thinker@branda.to>
parents:
706
diff
changeset
|
278 var alpha = 1; |
703
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
279 for(i in items) { |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
280 sys.puts(items[i]); |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
281 var f = items[i].split(':'); |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
282 if (f[0] == 'opacity') { |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
283 alpha = f[1]; |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
284 } else if (f[0] == 'fill') { |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
285 fill = f[1]; |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
286 } else if (f[0] == 'fill-opacity') { |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
287 } else if (f[0] == 'stroke') { |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
288 } else if (f[0] == 'stroken-width') { |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
289 } else if (f[0] == 'stroke-opacity') { |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
290 } |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
291 } |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
292 sys.puts("fill="+fill); |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
293 if (fill[0]=='#') { |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
294 var r,g,b; |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
295 r = parseInt(fill.substring(1,3),16)/256; |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
296 g = parseInt(fill.substring(3,5),16)/256; |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
297 b = parseInt(fill.substring(5,7),16)/256; |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
298 sys.puts("r="+r+" g="+g+" b="+b+" a="+alpha); |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
299 |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
300 paint = mb_rt.paint_color_new(r,g,b,parseFloat(alpha)); |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
301 } else { |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
302 paint = mb_rt.paint_color_new(0,0,0,1); |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
303 } |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
304 } |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
305 var rect = mb_rt.rect_new(x,y,w,h,10, 10); |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
306 sys.puts("rect x="+x+" y="+y+" w="+w+" h="+h); |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
307 paint.fill(rect); |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
308 coord.add_shape(rect); |
624 | 309 } |
310 | |
311 function _MB_parseGroup(root, group_id, n) | |
312 { | |
313 var k; | |
314 var nodes = n.childNodes(); | |
315 var coord = mb_rt.coord_new(root); | |
316 // Parse the transform and style here | |
317 var trans = n.attr('transform'); | |
318 if (trans!=null) { | |
319 parseTransform(coord, trans.value()); | |
320 } | |
321 | |
322 for(k in nodes) { | |
323 var n = nodes[k].name(); | |
324 var attr = nodes[k].attr('id'); | |
325 var id; | |
326 if (attr) { | |
327 id = attr.value(); | |
328 } | |
329 if (n == "g") { | |
330 _MB_parseGroup(coord, id, nodes[k]); | |
706 | 331 } else if (n == "path") { |
332 _MB_parsePath(coord, id, nodes[k]); | |
624 | 333 } else if (n == "text") { |
334 _MB_parseText(coord, id, nodes[k]); | |
335 } else if (n == "rect") { | |
336 _MB_parseRect(coord, id, nodes[k]); | |
646 | 337 } else if (n == "image") { |
338 _MB_parseImage(coord, id, nodes[k]); | |
624 | 339 } |
340 } | |
341 | |
342 } | |
343 | |
646 | 344 function _MB_parseImage(coord,id, n) |
345 { | |
346 sys.puts("---> image"); | |
347 var ref = n.attr('href').value(); | |
348 | |
349 if (ref == null) return; | |
350 sys.puts(ref); | |
647 | 351 if (ref.substr(0,7) == "file://") { |
352 ref = ref.substring(7); | |
353 } else if (ref.substr(0,5)=="file:") { | |
354 ref = ref.substring(5); | |
355 } else { | |
646 | 356 return; |
357 } | |
358 sys.puts("Load image "+ref); | |
359 var w; | |
360 var h; | |
361 var x,y; | |
362 | |
363 w = n.attr("width"); | |
364 if (w == null) return; | |
365 w = parseInt(w.value()); | |
366 h = n.attr("height"); | |
367 if (h == null) return; | |
368 h = parseInt(h.value()); | |
369 x = n.attr("x"); | |
370 if (x == null) return; | |
371 x = parseInt(x.value()); | |
372 y = n.attr("y"); | |
373 if (y == null) return; | |
374 y = parseInt(y.value()); | |
375 sys.puts("x="+x+",y="+y+",w="+w+",h="+h); | |
376 var img = mb_rt.image_new(x,y,w,h); | |
377 var img_data = ldr.load(ref); | |
378 sys.puts(img_data); | |
379 var paint = mb_rt.paint_image_new(img_data); | |
380 paint.fill(img); | |
381 coord.add_shape(img); | |
382 } | |
624 | 383 |
384 function _MB_parseDefs(root,n) | |
385 { | |
386 var k; | |
387 var nodes = n.childNodes(); | |
388 | |
389 for(k in nodes) { | |
390 var name = nodes[k].name(); | |
391 if (name == "linearGradient") { | |
392 //_MB_parseLinearGradient(root,nodes[k]); | |
393 } | |
394 } | |
395 } | |
396 | |
397 | |
398 MB_loadSVG(mb_rt,mb_rt.root,"test.svg"); | |
399 mb_rt.redraw_all(); | |
704
d950487bd9f9
Flush mb_rt, immediately, after redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
703
diff
changeset
|
400 mb_rt.flush(); |