Mercurial > MadButterfly
annotate nodejs/svg.js @ 625:9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
author | wycc |
---|---|
date | Fri, 16 Jul 2010 04:04:53 +0800 |
parents | d45c928f6523 |
children | 01e960bfc9ff |
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 } | |
29 | |
625
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
30 function parseTextStyle(obj,n) |
624 | 31 { |
625
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
32 var attr; |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
33 if (n) { |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
34 attr = n.attr('style'); |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
35 } else { |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
36 attr = null; |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
37 } |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
38 var fs = 20; |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
39 var family="ciurier"; |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
40 if (attr == null) { |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
41 var paint = mb_rt.paint_color_new(1,1,1,1); |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
42 var face=mb_rt.font_face_query(family, 2, 100); |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
43 obj.set_style([[20,face,fs]]); |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
44 return paint; |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
45 } |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
46 var f = attr.value().split(';'); |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
47 |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
48 for(i in f) { |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
49 var kv = f[i].split(':'); |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
50 if (kv[0] == 'font-size') { |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
51 fs = parsePointSize(kv[1]); |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
52 } 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
|
53 } 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
|
54 } else if (kv[0] == "fill") { |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
55 } 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
|
56 } else if (kv[0] == "stroke") { |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
57 } 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
|
58 } 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
|
59 } 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
|
60 } 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
|
61 } else if (kv[0] == "font-family") { |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
62 } 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
|
63 } 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
|
64 } 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
|
65 } 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
|
66 } 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
|
67 } 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
|
68 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
|
69 } |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
70 } |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
71 var paint = mb_rt.paint_color_new(1,1,1,1); |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
72 var face=mb_rt.font_face_query(family, 2, 100); |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
73 obj.set_style([[20,face,fs]]); |
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
74 return paint; |
624 | 75 } |
76 | |
77 function _MB_parseTSpan(coord, n) | |
78 { | |
79 var x = getInteger(n,'x'); | |
80 var y = getInteger(n,'y'); | |
81 var tcoord = mb_rt.coord_new(coord); | |
82 var nodes = n.childNodes(); | |
83 var k; | |
84 | |
85 sys.puts(n.text()); | |
86 var obj = mb_rt.stext_new(n.text(),x,y); | |
625
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
87 var paint = parseTextStyle(obj,n); |
624 | 88 paint.fill(obj); |
89 tcoord.add_shape(obj); | |
90 for(k in nodes) { | |
91 var name = nodes[k].name(); | |
92 if (name == "tspan") { | |
93 _MB_parseTSpan(tcoord,nodes[k]); | |
94 } else { | |
95 } | |
96 } | |
97 } | |
98 | |
99 function _MB_parseText(coord,id, n) | |
100 { | |
101 var x = getInteger(n,'x'); | |
102 var y = getInteger(n,'y'); | |
103 var tcoord = mb_rt.coord_new(coord); | |
625
9f2080b68f8e
Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents:
624
diff
changeset
|
104 //var paint = parseTextStyle(n); |
624 | 105 var nodes = n.childNodes(); |
106 var k; | |
107 for(k in nodes) { | |
108 var n = nodes[k].name(); | |
109 if (n == "tspan") { | |
110 _MB_parseTSpan(tcoord,nodes[k]); | |
111 } else { | |
112 } | |
113 } | |
114 | |
115 | |
116 } | |
117 | |
118 | |
119 function parseTransform(coord, s) | |
120 { | |
121 var off = s.indexOf('translate'); | |
122 if (off != -1) { | |
123 var ss = s.substring(off+9); | |
124 for(i=0;i<ss.length;i++) { | |
125 if (ss[i] == '(') break; | |
126 } | |
127 ss = ss.substring(i+1); | |
128 for(i=0;i<ss.length;i++) { | |
129 if (ss[i] == ')') { | |
130 ss = ss.substring(0,i); | |
131 break; | |
132 } | |
133 } | |
134 var f = ss.split(','); | |
135 var x,y; | |
136 x = parseInt(f[0]); | |
137 y = parseInt(f[1]); | |
138 coord[2] = x; | |
139 coord[5] = y; | |
140 } | |
141 off = s.indexOf('matrix'); | |
142 if (off != -1) { | |
143 sys.puts("matrix"); | |
144 } | |
145 } | |
146 | |
147 function _MB_parseRect(coord, id, n) | |
148 { | |
149 | |
150 } | |
151 | |
152 function _MB_parseGroup(root, group_id, n) | |
153 { | |
154 var k; | |
155 var nodes = n.childNodes(); | |
156 var coord = mb_rt.coord_new(root); | |
157 // Parse the transform and style here | |
158 var trans = n.attr('transform'); | |
159 if (trans!=null) { | |
160 parseTransform(coord, trans.value()); | |
161 } | |
162 | |
163 for(k in nodes) { | |
164 var n = nodes[k].name(); | |
165 var attr = nodes[k].attr('id'); | |
166 var id; | |
167 if (attr) { | |
168 id = attr.value(); | |
169 } | |
170 if (n == "g") { | |
171 _MB_parseGroup(coord, id, nodes[k]); | |
172 } else if (n == "text") { | |
173 _MB_parseText(coord, id, nodes[k]); | |
174 } else if (n == "rect") { | |
175 _MB_parseRect(coord, id, nodes[k]); | |
176 } | |
177 } | |
178 | |
179 } | |
180 | |
181 | |
182 function _MB_parseDefs(root,n) | |
183 { | |
184 var k; | |
185 var nodes = n.childNodes(); | |
186 | |
187 for(k in nodes) { | |
188 var name = nodes[k].name(); | |
189 if (name == "linearGradient") { | |
190 //_MB_parseLinearGradient(root,nodes[k]); | |
191 } | |
192 } | |
193 } | |
194 | |
195 | |
196 MB_loadSVG(mb_rt,mb_rt.root,"test.svg"); | |
197 mb_rt.redraw_all(); |