comparison nodejs/svg.js @ 706:fdd68e69c59f

Parse path tag for SVG
author Thinker K.F. Li <thinker@branda.to>
date Fri, 13 Aug 2010 17:20:17 +0800
parents d950487bd9f9
children ac9c20db953e
comparison
equal deleted inserted replaced
705:efcbcb750d63 706:fdd68e69c59f
112 } else { 112 } else {
113 } 113 }
114 } 114 }
115 } 115 }
116 116
117 function _prepare_paint_color(color, alpha) {
118 var paint;
119
120 if (color[0]=='#') {
121 var r,g,b;
122 r = parseInt(color.substring(1,3),16)/256;
123 g = parseInt(color.substring(3,5),16)/256;
124 b = parseInt(color.substring(5,7),16)/256;
125 paint = mb_rt.paint_color_new(r, g, b, alpha);
126 } else {
127 paint = mb_rt.paint_color_new(0,0,0,1);
128 }
129 return paint;
130 }
131
132 function _MB_parsePath(coord,id, n)
133 {
134 var d = n.attr('d').value();
135 var style = n.attr('style');
136 var path = mb_rt.path_new(d);
137 var paint;
138
139 if (style==null) {
140 paint = mb_rt.paint_color_new(0,0,0,0.1);
141 paint.stroke(path);
142 } else {
143 var items = style.value().split(';');
144 var fill_alpha = 1;
145 var stroke_alpha = 1;
146 var fill_color;
147 var stroke_color;
148 var alpha;
149
150 for(i in items) {
151 sys.puts(items[i]);
152 var f = items[i].split(':');
153 if (f[0] == 'opacity') {
154 alpha = f[1];
155 } else if (f[0] == 'fill') {
156 if(f[1] != "none")
157 fill_color = f[1];
158 } else if (f[0] == 'fill-opacity') {
159 fill_alpha = parseFloat(f[1]);
160 } else if (f[0] == 'stroke') {
161 if(f[1] != "none")
162 stroke_color = f[1];
163 } else if (f[0] == 'stroke-width') {
164 path.stroke_width = parseFloat(f[1]);
165 } else if (f[0] == 'stroke-opacity') {
166 stroke_alpha = parseFloat(f[1]);
167 }
168 }
169
170 if(fill_color) {
171 paint = _prepare_paint_color(fill_color, fill_alpha);
172 paint.fill(path);
173 }
174 if(stroke_color) {
175 paint = _prepare_paint_color(stroke_color, stroke_alpha);
176 paint.stroke(path);
177 }
178
179 }
180 coord.add_shape(path);
181 }
182
117 function _MB_parseText(coord,id, n) 183 function _MB_parseText(coord,id, n)
118 { 184 {
119 var x = getInteger(n,'x'); 185 var x = getInteger(n,'x');
120 var y = getInteger(n,'y'); 186 var y = getInteger(n,'y');
121 var tcoord = mb_rt.coord_new(coord); 187 var tcoord = mb_rt.coord_new(coord);
134 } 200 }
135 201
136 202
137 } 203 }
138 204
139
140 function parseTransform(coord, s) 205 function parseTransform(coord, s)
141 { 206 {
142 var off = s.indexOf('translate'); 207 var off = s.indexOf('translate');
143 if (off != -1) { 208 if (off != -1) {
144 var ss = s.substring(off+9); 209 var ss = s.substring(off+9);
152 break; 217 break;
153 } 218 }
154 } 219 }
155 var f = ss.split(','); 220 var f = ss.split(',');
156 var x,y; 221 var x,y;
157 x = parseInt(f[0]); 222 x = parseFloat(f[0]);
158 y = parseInt(f[1]); 223 y = parseFloat(f[1]);
159 coord[2] = x; 224 coord[0] = 1;
225 coord[1] = 0;
226 coord[2] = x;
227 coord[3] = 0;
228 coord[4] = 1;
160 coord[5] = y; 229 coord[5] = y;
161 } 230 }
162 off = s.indexOf('matrix'); 231 off = s.indexOf('matrix');
163 if (off != -1) { 232 if (off != -1) {
164 sys.puts("matrix");
165 var end = s.indexOf(')'); 233 var end = s.indexOf(')');
166 var m = s.substring(7,end); 234 var m = s.substring(7,end);
167 var fields = m.split(','); 235 var fields = m.split(',');
168 coord[0] = parseFloat(fields[0]); 236 coord[0] = parseFloat(fields[0]);
169 coord[1] = parseFloat(fields[1]); 237 coord[1] = parseFloat(fields[2]);
170 coord[2] = parseFloat(fields[4]); 238 coord[2] = parseFloat(fields[4]);
171 coord[3] = parseFloat(fields[2]); 239 coord[3] = parseFloat(fields[1]);
172 coord[4] = parseFloat(fields[3]); 240 coord[4] = parseFloat(fields[3]);
173 coord[5] = parseFloat(fields[5]); 241 coord[5] = parseFloat(fields[5]);
174 } 242 }
175 } 243 }
176 244
240 if (attr) { 308 if (attr) {
241 id = attr.value(); 309 id = attr.value();
242 } 310 }
243 if (n == "g") { 311 if (n == "g") {
244 _MB_parseGroup(coord, id, nodes[k]); 312 _MB_parseGroup(coord, id, nodes[k]);
313 } else if (n == "path") {
314 _MB_parsePath(coord, id, nodes[k]);
245 } else if (n == "text") { 315 } else if (n == "text") {
246 _MB_parseText(coord, id, nodes[k]); 316 _MB_parseText(coord, id, nodes[k]);
247 } else if (n == "rect") { 317 } else if (n == "rect") {
248 _MB_parseRect(coord, id, nodes[k]); 318 _MB_parseRect(coord, id, nodes[k]);
249 } else if (n == "image") { 319 } else if (n == "image") {