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
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
1 var libxml = require('libxmljs');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
2 var sys=require('sys');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
3 var mbfly = require("mbfly");
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
4 var mb_rt = new mbfly.mb_rt(":0.0", 720,480);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
5
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
6 function MB_loadSVG(mb_rt,root,filename) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
7 var doc = libxml.parseXmlFile(filename);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
8 var nodes = doc.root().childNodes();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
9 var coord = mb_rt.coord_new(root);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
10 var k;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
11
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
12 for(k in nodes) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
13 var n = nodes[k].name();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
14 if (n == "defs") {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
15 _MB_parseDefs(root,nodes[k]);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
16 } else if (n == "g") {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
17 _MB_parseGroup(root,'root_coord',nodes[k]);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
18 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
19 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
20 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
21
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
22 function getInteger(n,name)
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
23 {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
24 if (n == null) return 0;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
25 var a = n.attr(name);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
26 if (a==null) return 0;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
27 return parseInt(a.value());
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
28 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
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
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
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
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
75 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
76
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
77 function _MB_parseTSpan(coord, n)
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
78 {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
79 var x = getInteger(n,'x');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
80 var y = getInteger(n,'y');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
81 var tcoord = mb_rt.coord_new(coord);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
82 var nodes = n.childNodes();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
83 var k;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
84
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
85 sys.puts(n.text());
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
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
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
88 paint.fill(obj);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
89 tcoord.add_shape(obj);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
90 for(k in nodes) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
91 var name = nodes[k].name();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
92 if (name == "tspan") {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
93 _MB_parseTSpan(tcoord,nodes[k]);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
94 } else {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
95 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
96 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
97 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
98
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
99 function _MB_parseText(coord,id, n)
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
100 {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
101 var x = getInteger(n,'x');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
102 var y = getInteger(n,'y');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
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
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
105 var nodes = n.childNodes();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
106 var k;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
107 for(k in nodes) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
108 var n = nodes[k].name();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
109 if (n == "tspan") {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
110 _MB_parseTSpan(tcoord,nodes[k]);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
111 } else {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
112 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
113 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
114
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
115
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
116 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
117
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
118
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
119 function parseTransform(coord, s)
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
120 {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
121 var off = s.indexOf('translate');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
122 if (off != -1) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
123 var ss = s.substring(off+9);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
124 for(i=0;i<ss.length;i++) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
125 if (ss[i] == '(') break;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
126 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
127 ss = ss.substring(i+1);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
128 for(i=0;i<ss.length;i++) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
129 if (ss[i] == ')') {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
130 ss = ss.substring(0,i);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
131 break;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
132 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
133 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
134 var f = ss.split(',');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
135 var x,y;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
136 x = parseInt(f[0]);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
137 y = parseInt(f[1]);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
138 coord[2] = x;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
139 coord[5] = y;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
140 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
141 off = s.indexOf('matrix');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
142 if (off != -1) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
143 sys.puts("matrix");
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
144 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
145 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
146
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
147 function _MB_parseRect(coord, id, n)
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
148 {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
149
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
150 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
151
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
152 function _MB_parseGroup(root, group_id, n)
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
153 {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
154 var k;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
155 var nodes = n.childNodes();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
156 var coord = mb_rt.coord_new(root);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
157 // Parse the transform and style here
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
158 var trans = n.attr('transform');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
159 if (trans!=null) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
160 parseTransform(coord, trans.value());
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
161 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
162
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
163 for(k in nodes) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
164 var n = nodes[k].name();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
165 var attr = nodes[k].attr('id');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
166 var id;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
167 if (attr) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
168 id = attr.value();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
169 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
170 if (n == "g") {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
171 _MB_parseGroup(coord, id, nodes[k]);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
172 } else if (n == "text") {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
173 _MB_parseText(coord, id, nodes[k]);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
174 } else if (n == "rect") {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
175 _MB_parseRect(coord, id, nodes[k]);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
176 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
177 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
178
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
179 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
180
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
181
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
182 function _MB_parseDefs(root,n)
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
183 {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
184 var k;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
185 var nodes = n.childNodes();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
186
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
187 for(k in nodes) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
188 var name = nodes[k].name();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
189 if (name == "linearGradient") {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
190 //_MB_parseLinearGradient(root,nodes[k]);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
191 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
192 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
193 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
194
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
195
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
196 MB_loadSVG(mb_rt,mb_rt.root,"test.svg");
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
197 mb_rt.redraw_all();