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