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