annotate nodejs/svg.js @ 624:d45c928f6523

Add SVG parser sample code.
author wycc
date Thu, 15 Jul 2010 22:34:04 +0800
parents
children 9f2080b68f8e
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
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
30 function parseStyle(n)
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
31 {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
32 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
33
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
34 function _MB_parseTSpan(coord, n)
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
35 {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
36 var x = getInteger(n,'x');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
37 var y = getInteger(n,'y');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
38 var tcoord = mb_rt.coord_new(coord);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
39 var paint = parseStyle(n);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
40 var nodes = n.childNodes();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
41 var k;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
42
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
43 sys.puts(n.text());
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
44 var obj = mb_rt.stext_new(n.text(),x,y);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
45 var paint = mb_rt.paint_color_new(1,1,1,1);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
46 var face=mb_rt.font_face_query("courier", 2, 100);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
47 obj.set_style([[5,face,20]]);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
48 paint.fill(obj);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
49 tcoord.add_shape(obj);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
50 for(k in nodes) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
51 var name = nodes[k].name();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
52 if (name == "tspan") {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
53 _MB_parseTSpan(tcoord,nodes[k]);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
54 } else {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
55 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
56 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
57 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
58
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
59 function _MB_parseText(coord,id, n)
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
60 {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
61 var x = getInteger(n,'x');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
62 var y = getInteger(n,'y');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
63 var tcoord = mb_rt.coord_new(coord);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
64 var paint = parseStyle(n);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
65 var nodes = n.childNodes();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
66 var k;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
67 for(k in nodes) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
68 var n = nodes[k].name();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
69 if (n == "tspan") {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
70 _MB_parseTSpan(tcoord,nodes[k]);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
71 } else {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
72 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
73 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
74
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
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
78
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
79 function parseTransform(coord, s)
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
80 {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
81 var off = s.indexOf('translate');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
82 if (off != -1) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
83 var ss = s.substring(off+9);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
84 for(i=0;i<ss.length;i++) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
85 if (ss[i] == '(') break;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
86 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
87 ss = ss.substring(i+1);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
88 for(i=0;i<ss.length;i++) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
89 if (ss[i] == ')') {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
90 ss = ss.substring(0,i);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
91 break;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
92 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
93 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
94 var f = ss.split(',');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
95 var x,y;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
96 x = parseInt(f[0]);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
97 y = parseInt(f[1]);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
98 coord[2] = x;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
99 coord[5] = y;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
100 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
101 off = s.indexOf('matrix');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
102 if (off != -1) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
103 sys.puts("matrix");
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
104 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
105 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
106
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
107 function _MB_parseRect(coord, id, n)
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
108 {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
109
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
110 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
111
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
112 function _MB_parseGroup(root, group_id, n)
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
113 {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
114 var k;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
115 var nodes = n.childNodes();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
116 var coord = mb_rt.coord_new(root);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
117 // Parse the transform and style here
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
118 var trans = n.attr('transform');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
119 if (trans!=null) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
120 parseTransform(coord, trans.value());
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
121 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
122
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
123 for(k in nodes) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
124 var n = nodes[k].name();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
125 var attr = nodes[k].attr('id');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
126 var id;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
127 if (attr) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
128 id = attr.value();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
129 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
130 if (n == "g") {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
131 _MB_parseGroup(coord, id, nodes[k]);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
132 } else if (n == "text") {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
133 _MB_parseText(coord, id, nodes[k]);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
134 } else if (n == "rect") {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
135 _MB_parseRect(coord, id, nodes[k]);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
136 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
137 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
138
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
139 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
140
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
141
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
142 function _MB_parseDefs(root,n)
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
143 {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
144 var k;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
145 var nodes = n.childNodes();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
146
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
147 for(k in nodes) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
148 var name = nodes[k].name();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
149 if (name == "linearGradient") {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
150 //_MB_parseLinearGradient(root,nodes[k]);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
151 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
152 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
153 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
154
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
155
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
156 MB_loadSVG(mb_rt,mb_rt.root,"test.svg");
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
157 mb_rt.redraw_all();