Mercurial > MadButterfly
annotate nodejs/svg.js @ 631:01e960bfc9ff
Implement text style parser
author | wycc |
---|---|
date | Wed, 21 Jul 2010 07:26:12 +0800 |
parents | 9f2080b68f8e |
children | e517bbefe0e9 |
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") { |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
84 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
|
85 } |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
86 } |
624 | 87 } |
88 | |
631 | 89 function _MB_parseTSpan(coord, n,style) |
624 | 90 { |
91 var x = getInteger(n,'x'); | |
92 var y = getInteger(n,'y'); | |
93 var tcoord = mb_rt.coord_new(coord); | |
94 var nodes = n.childNodes(); | |
95 var k; | |
96 | |
97 sys.puts(n.text()); | |
98 var obj = mb_rt.stext_new(n.text(),x,y); | |
631 | 99 parseTextStyle(style,n); |
100 style.paint = mb_rt.paint_color_new(1,1,1,1); | |
101 style.face=mb_rt.font_face_query(style.family, 2, 100); | |
102 obj.set_style([[20,style.face,style.fs]]); | |
103 style.paint.fill(obj); | |
624 | 104 tcoord.add_shape(obj); |
105 for(k in nodes) { | |
106 var name = nodes[k].name(); | |
107 if (name == "tspan") { | |
108 _MB_parseTSpan(tcoord,nodes[k]); | |
109 } else { | |
110 } | |
111 } | |
112 } | |
113 | |
114 function _MB_parseText(coord,id, n) | |
115 { | |
116 var x = getInteger(n,'x'); | |
117 var y = getInteger(n,'y'); | |
118 var tcoord = mb_rt.coord_new(coord); | |
631 | 119 var style = new Object(); |
120 style.fs = 20; | |
121 style.family = 'courier'; | |
122 parseTextStyle(style,n); | |
624 | 123 var nodes = n.childNodes(); |
124 var k; | |
125 for(k in nodes) { | |
126 var n = nodes[k].name(); | |
127 if (n == "tspan") { | |
631 | 128 _MB_parseTSpan(tcoord,nodes[k],style); |
624 | 129 } else { |
130 } | |
131 } | |
132 | |
133 | |
134 } | |
135 | |
136 | |
137 function parseTransform(coord, s) | |
138 { | |
139 var off = s.indexOf('translate'); | |
140 if (off != -1) { | |
141 var ss = s.substring(off+9); | |
142 for(i=0;i<ss.length;i++) { | |
143 if (ss[i] == '(') break; | |
144 } | |
145 ss = ss.substring(i+1); | |
146 for(i=0;i<ss.length;i++) { | |
147 if (ss[i] == ')') { | |
148 ss = ss.substring(0,i); | |
149 break; | |
150 } | |
151 } | |
152 var f = ss.split(','); | |
153 var x,y; | |
154 x = parseInt(f[0]); | |
155 y = parseInt(f[1]); | |
156 coord[2] = x; | |
157 coord[5] = y; | |
158 } | |
159 off = s.indexOf('matrix'); | |
160 if (off != -1) { | |
161 sys.puts("matrix"); | |
162 } | |
163 } | |
164 | |
165 function _MB_parseRect(coord, id, n) | |
166 { | |
167 | |
168 } | |
169 | |
170 function _MB_parseGroup(root, group_id, n) | |
171 { | |
172 var k; | |
173 var nodes = n.childNodes(); | |
174 var coord = mb_rt.coord_new(root); | |
175 // Parse the transform and style here | |
176 var trans = n.attr('transform'); | |
177 if (trans!=null) { | |
178 parseTransform(coord, trans.value()); | |
179 } | |
180 | |
181 for(k in nodes) { | |
182 var n = nodes[k].name(); | |
183 var attr = nodes[k].attr('id'); | |
184 var id; | |
185 if (attr) { | |
186 id = attr.value(); | |
187 } | |
188 if (n == "g") { | |
189 _MB_parseGroup(coord, id, nodes[k]); | |
190 } else if (n == "text") { | |
191 _MB_parseText(coord, id, nodes[k]); | |
192 } else if (n == "rect") { | |
193 _MB_parseRect(coord, id, nodes[k]); | |
194 } | |
195 } | |
196 | |
197 } | |
198 | |
199 | |
200 function _MB_parseDefs(root,n) | |
201 { | |
202 var k; | |
203 var nodes = n.childNodes(); | |
204 | |
205 for(k in nodes) { | |
206 var name = nodes[k].name(); | |
207 if (name == "linearGradient") { | |
208 //_MB_parseLinearGradient(root,nodes[k]); | |
209 } | |
210 } | |
211 } | |
212 | |
213 | |
214 MB_loadSVG(mb_rt,mb_rt.root,"test.svg"); | |
215 mb_rt.redraw_all(); |