Mercurial > MadButterfly
annotate nodejs/svg.js @ 717:b822b1912d67
Paint with black for unspecified, not "none", fill and stroke.
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Sat, 14 Aug 2010 23:52:43 +0800 |
parents | f53e45d1fcd0 |
children | 0cd59ce76e67 |
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) { |
624 | 14 var doc = libxml.parseXmlFile(filename); |
15 var nodes = doc.root().childNodes(); | |
16 var coord = mb_rt.coord_new(root); | |
17 var k; | |
714 | 18 this.mb_rt = mb_rt; |
624 | 19 |
20 for(k in nodes) { | |
21 var n = nodes[k].name(); | |
22 if (n == "defs") { | |
714 | 23 this.parseDefs(root,nodes[k]); |
624 | 24 } else if (n == "g") { |
714 | 25 this.parseGroup(root,'root_coord',nodes[k]); |
624 | 26 } |
27 } | |
28 } | |
29 | |
30 function getInteger(n,name) | |
31 { | |
32 if (n == null) return 0; | |
33 var a = n.attr(name); | |
34 if (a==null) return 0; | |
35 return parseInt(a.value()); | |
36 } | |
631 | 37 function parsePointSize(s) |
38 { | |
39 var fs=0; | |
40 var i; | |
624 | 41 |
631 | 42 for(i=0;i<s.length;i++) { |
43 if (s[i]<'0' || s[i] > '9') break; | |
44 fs = fs*10 + (s[i]-'0'); | |
45 } | |
46 return fs; | |
47 | |
48 } | |
49 | |
50 | |
51 function parseColor(c) | |
52 { | |
53 if (c[0] == '#') { | |
54 return parseInt(c.substring(1,3),16)<<16 | parseInt(c.substring(3,5),16)<<8 | parseInt(c.substring(5,7),16); | |
55 } | |
56 } | |
57 function parseTextStyle(style,n) | |
624 | 58 { |
625
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
59 var attr; |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
60 if (n) { |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
61 attr = n.attr('style'); |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
62 } else { |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
63 attr = null; |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
64 } |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
65 if (attr == null) { |
631 | 66 return; |
625
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 var f = attr.value().split(';'); |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
69 |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
70 for(i in f) { |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
71 var kv = f[i].split(':'); |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
72 if (kv[0] == 'font-size') { |
631 | 73 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
|
74 } 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
|
75 } 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
|
76 } else if (kv[0] == "fill") { |
631 | 77 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
|
78 } 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
|
79 } 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
|
80 } else if (kv[0] == "stroke") { |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
81 } 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
|
82 } 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
|
83 } 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
|
84 } 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
|
85 } else if (kv[0] == "font-family") { |
631 | 86 style.family = kv[1]; |
625
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
87 } 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
|
88 } 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
|
89 } 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
|
90 } 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
|
91 } 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
|
92 } else if (kv[0] == "line-height") { |
632 | 93 } else { |
625
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
94 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
|
95 } |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
96 } |
624 | 97 } |
98 | |
714 | 99 exports.parseTSpan=function(coord, n,style) |
624 | 100 { |
101 var x = getInteger(n,'x'); | |
102 var y = getInteger(n,'y'); | |
714 | 103 var tcoord = this.mb_rt.coord_new(coord); |
624 | 104 var nodes = n.childNodes(); |
105 var k; | |
106 | |
107 sys.puts(n.text()); | |
714 | 108 var obj = this.mb_rt.stext_new(n.text(),x,y); |
631 | 109 parseTextStyle(style,n); |
714 | 110 style.paint = this.mb_rt.paint_color_new(1,1,1,1); |
111 style.face=this.mb_rt.font_face_query(style.family, 2, 100); | |
631 | 112 obj.set_style([[20,style.face,style.fs]]); |
113 style.paint.fill(obj); | |
624 | 114 tcoord.add_shape(obj); |
115 for(k in nodes) { | |
116 var name = nodes[k].name(); | |
117 if (name == "tspan") { | |
714 | 118 this.parseTSpan(tcoord,nodes[k]); |
624 | 119 } else { |
120 } | |
121 } | |
122 } | |
123 | |
714 | 124 exports._prepare_paint_color=function(color, alpha) { |
706 | 125 var paint; |
713 | 126 var c; |
706 | 127 |
128 if (color[0]=='#') { | |
129 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
|
130 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
|
131 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
|
132 b = parseInt(color.substring(5,7),16)/255; |
714 | 133 paint = this.mb_rt.paint_color_new(r, g, b, alpha); |
713 | 134 } else if(_std_colors[color]) { |
135 c = _std_colors[color]; | |
714 | 136 paint = this.mb_rt.paint_color_new(c[0], c[1], c[2], alpha); |
706 | 137 } else { |
714 | 138 paint = this.mb_rt.paint_color_new(0,0,0,1); |
706 | 139 } |
140 return paint; | |
141 } | |
142 | |
714 | 143 exports.parsePath=function(coord,id, n) |
706 | 144 { |
145 var d = n.attr('d').value(); | |
146 var style = n.attr('style'); | |
714 | 147 var path = this.mb_rt.path_new(d); |
706 | 148 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
|
149 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
|
150 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
|
151 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
|
152 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
|
153 var black_paint; |
706 | 154 |
717
b822b1912d67
Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents:
714
diff
changeset
|
155 if(style != null) { |
706 | 156 var items = style.value().split(';'); |
157 var alpha; | |
158 | |
159 for(i in items) { | |
160 sys.puts(items[i]); | |
161 var f = items[i].split(':'); | |
162 if (f[0] == 'opacity') { | |
163 alpha = f[1]; | |
164 } 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
|
165 fill_color = f[1]; |
706 | 166 } else if (f[0] == 'fill-opacity') { |
167 fill_alpha = parseFloat(f[1]); | |
168 } 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
|
169 stroke_color = f[1]; |
706 | 170 } else if (f[0] == 'stroke-width') { |
171 path.stroke_width = parseFloat(f[1]); | |
172 } else if (f[0] == 'stroke-opacity') { | |
173 stroke_alpha = parseFloat(f[1]); | |
174 } | |
175 } | |
176 | |
717
b822b1912d67
Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents:
714
diff
changeset
|
177 } |
b822b1912d67
Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents:
714
diff
changeset
|
178 |
b822b1912d67
Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents:
714
diff
changeset
|
179 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
|
180 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
|
181 |
b822b1912d67
Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents:
714
diff
changeset
|
182 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
|
183 if(fill_color != "none") { |
714 | 184 paint = this._prepare_paint_color(fill_color, fill_alpha); |
706 | 185 paint.fill(path); |
186 } | |
717
b822b1912d67
Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents:
714
diff
changeset
|
187 } else { |
b822b1912d67
Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents:
714
diff
changeset
|
188 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
|
189 } |
b822b1912d67
Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents:
714
diff
changeset
|
190 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
|
191 if(stroke_color != "none") { |
714 | 192 paint = this._prepare_paint_color(stroke_color, stroke_alpha); |
706 | 193 paint.stroke(path); |
194 } | |
717
b822b1912d67
Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents:
714
diff
changeset
|
195 } else { |
b822b1912d67
Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents:
714
diff
changeset
|
196 black_paint.stroke(path); |
706 | 197 } |
198 coord.add_shape(path); | |
199 } | |
200 | |
714 | 201 exports.parseText=function(coord,id, n) |
624 | 202 { |
203 var x = getInteger(n,'x'); | |
204 var y = getInteger(n,'y'); | |
714 | 205 var tcoord = this.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") { | |
714 | 215 this.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 | |
714 | 263 exports.parseRect=function(coord, id, n) |
624 | 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) { |
714 | 274 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
|
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 |
714 | 300 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
|
301 } else { |
714 | 302 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
|
303 } |
3457519e3b9c
Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents:
647
diff
changeset
|
304 } |
714 | 305 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
|
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 | |
714 | 311 exports.parseGroup=function(root, group_id, n) |
624 | 312 { |
313 var k; | |
314 var nodes = n.childNodes(); | |
714 | 315 var coord = this.mb_rt.coord_new(root); |
624 | 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") { | |
714 | 330 this.parseGroup(coord, id, nodes[k]); |
706 | 331 } else if (n == "path") { |
714 | 332 this.parsePath(coord, id, nodes[k]); |
624 | 333 } else if (n == "text") { |
714 | 334 this.parseText(coord, id, nodes[k]); |
624 | 335 } else if (n == "rect") { |
714 | 336 this.parseRect(coord, id, nodes[k]); |
646 | 337 } else if (n == "image") { |
714 | 338 this.parseImage(coord, id, nodes[k]); |
624 | 339 } |
340 } | |
341 | |
342 } | |
343 | |
714 | 344 exports.parseImage=function(coord,id, n) |
646 | 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); | |
714 | 376 var img = this.mb_rt.image_new(x,y,w,h); |
646 | 377 var img_data = ldr.load(ref); |
378 sys.puts(img_data); | |
714 | 379 var paint = this.mb_rt.paint_image_new(img_data); |
646 | 380 paint.fill(img); |
381 coord.add_shape(img); | |
382 } | |
624 | 383 |
714 | 384 exports.parseDefs=function(root,n) |
624 | 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 |