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