Mercurial > MadButterfly
comparison nodejs/svg.js @ 785:b6d9c42019d1
Refactor color parsing and fix bug of parsing path
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Tue, 31 Aug 2010 00:49:35 +0800 |
parents | 37a1bd3e3ce1 |
children | 0899dcac441c |
comparison
equal
deleted
inserted
replaced
784:37a1bd3e3ce1 | 785:b6d9c42019d1 |
---|---|
1 // -*- indent-tabs-mode: t; tab-width: 4; c-basic-offset: 4; -*- | 1 // -*- indent-tabs-mode: t; tab-width: 4; c-basic-offset: 4; -*- |
2 // vim: sw=4:ts=8:sts=4:ai | 2 // vim: sw=4:ts=8:sts=4 |
3 var libxml = require('libxmljs'); | 3 var libxml = require('libxmljs'); |
4 var sys=require('sys'); | 4 var sys=require('sys'); |
5 var mbfly = require("mbfly"); | 5 var mbfly = require("mbfly"); |
6 var ldr = mbfly.img_ldr_new("."); | 6 var ldr = mbfly.img_ldr_new("."); |
7 | 7 |
165 b = parseInt(color.substring(5,7),16)/255; | 165 b = parseInt(color.substring(5,7),16)/255; |
166 paint = this.mb_rt.paint_color_new(r, g, b, alpha); | 166 paint = this.mb_rt.paint_color_new(r, g, b, alpha); |
167 } else if(_std_colors[color]) { | 167 } else if(_std_colors[color]) { |
168 c = _std_colors[color]; | 168 c = _std_colors[color]; |
169 paint = this.mb_rt.paint_color_new(c[0], c[1], c[2], alpha); | 169 paint = this.mb_rt.paint_color_new(c[0], c[1], c[2], alpha); |
170 } else if (fill.substring(0,3) == 'url') { | |
171 var id = fill.substring(5,fill.length-1); | |
172 var gr = this.gradients[id]; | |
173 paint = this.mb_rt.paint_linear_new(gr[0],gr[1],gr[2],gr[3]); | |
174 paint.set_stops(this.stop_ref[id]); | |
170 } else { | 175 } else { |
171 paint = this.mb_rt.paint_color_new(0,0,0,1); | 176 paint = this.mb_rt.paint_color_new(0,0,0,1); |
172 } | 177 } |
173 return paint; | 178 return paint; |
174 } | 179 }; |
175 | 180 |
176 function guessPathBoundingBox(coord,d) | 181 function guessPathBoundingBox(coord,d) |
177 { | 182 { |
178 return; | 183 return; |
179 var items = d.split(' '); | 184 var items = d.split(' '); |
180 var len = items.length; | 185 var len = items.length; |
181 var pair; | 186 var pair; |
182 var i; | 187 var i; |
183 var minx,miny; | 188 var minx,miny; |
184 | 189 |
185 minx = 10000; | 190 minx = 10000; |
186 miny = 10000; | 191 miny = 10000; |
187 | 192 |
188 for(i=0;i<len;i++) { | 193 for(i=0;i<len;i++) { |
189 var type = items[i].toLowerCase(); | 194 var type = items[i].toLowerCase(); |
190 x = minx;y = miny; | 195 x = minx;y = miny; |
191 switch(type) { | 196 switch(type) { |
192 case 'm': | 197 case 'm': |
193 case 'l': | 198 case 'l': |
194 case 'a': | 199 case 'a': |
195 case 'x': | 200 case 'x': |
196 pair = items[i+1].split(','); | 201 pair = items[i+1].split(','); |
197 if (pair.length==2) { | 202 if (pair.length==2) { |
198 x = parseFloat(pair[0]); | 203 x = parseFloat(pair[0]); |
199 y = parseFloat(pair[1]); | 204 y = parseFloat(pair[1]); |
200 i++; | 205 i++; |
201 } else { | 206 } else { |
202 x = parseFloat(items[i+1]); | |
203 y = parseFloat(items[i+2]); | |
204 i+=2; | |
205 } | |
206 break; | |
207 case 'q': | |
208 // Implement this latter | |
209 break; | |
210 case 'c': | |
211 // Implement this latter | |
212 break; | |
213 case 's': | |
214 // Implement this latter | |
215 break; | |
216 case 'h': | |
217 x = parseFloat(items[i+1]); | 207 x = parseFloat(items[i+1]); |
218 break; | 208 y = parseFloat(items[i+2]); |
219 case 'v': | 209 i+=2; |
220 y = parseFloat(items[i+1]); | |
221 break; | |
222 default: | |
223 continue; | |
224 } | 210 } |
225 if (x < minx) minx = x; | 211 break; |
226 if (y < miny) miny = y; | 212 case 'q': |
227 } | 213 // Implement this latter |
228 if (coord.center.x > minx) | 214 break; |
229 coord.center.x = minx; | 215 case 'c': |
230 if (coord.center.y > miny) | 216 // Implement this latter |
231 coord.center.y = miny; | 217 break; |
232 }; | 218 case 's': |
233 | 219 // Implement this latter |
234 | 220 break; |
235 loadSVG.prototype.parsePath=function(coord,id, n) | 221 case 'h': |
236 { | 222 x = parseFloat(items[i+1]); |
237 var d = n.attr('d').value(); | 223 break; |
238 var style = n.attr('style'); | 224 case 'v': |
239 var path = this.mb_rt.path_new(d); | 225 y = parseFloat(items[i+1]); |
226 break; | |
227 default: | |
228 continue; | |
229 } | |
230 if (x < minx) minx = x; | |
231 if (y < miny) miny = y; | |
232 } | |
233 if (coord.center.x > minx) | |
234 coord.center.x = minx; | |
235 if (coord.center.y > miny) | |
236 coord.center.y = miny; | |
237 }; | |
238 | |
239 | |
240 loadSVG.prototype._set_paint = function(node, tgt) { | |
241 var style = node.attr('style'); | |
240 var paint; | 242 var paint; |
241 var fill_alpha = 1; | 243 var fill_alpha = 1; |
242 var stroke_alpha = 1; | 244 var stroke_alpha = 1; |
243 var fill_color; | 245 var fill_color; |
244 var stroke_color; | 246 var stroke_color; |
245 var black_paint; | 247 var black_paint; |
246 | 248 var i; |
247 guessPathBoundingBox(coord,d); | 249 |
248 if(style != null) { | 250 if(style != null) { |
249 var items = style.value().split(';'); | 251 var items = style.value().split(';'); |
250 var alpha; | 252 var alpha; |
251 | 253 |
252 for(i in items) { | 254 for(i in items) { |
258 } else if (f[0] == 'fill-opacity') { | 260 } else if (f[0] == 'fill-opacity') { |
259 fill_alpha = parseFloat(f[1]); | 261 fill_alpha = parseFloat(f[1]); |
260 } else if (f[0] == 'stroke') { | 262 } else if (f[0] == 'stroke') { |
261 stroke_color = f[1]; | 263 stroke_color = f[1]; |
262 } else if (f[0] == 'stroke-width') { | 264 } else if (f[0] == 'stroke-width') { |
263 path.stroke_width = parseFloat(f[1]); | 265 tgt.stroke_width = parseFloat(f[1]); |
264 } else if (f[0] == 'stroke-opacity') { | 266 } else if (f[0] == 'stroke-opacity') { |
265 stroke_alpha = parseFloat(f[1]); | 267 stroke_alpha = parseFloat(f[1]); |
268 } else if (f[0] == 'display') { | |
269 if(f[1] == 'none') | |
270 return; | |
266 } | 271 } |
267 } | 272 } |
268 | 273 |
269 } | 274 } |
270 | 275 |
272 black_paint = this.mb_rt.paint_color_new(0, 0, 0, 1); | 277 black_paint = this.mb_rt.paint_color_new(0, 0, 0, 1); |
273 | 278 |
274 if(fill_color) { | 279 if(fill_color) { |
275 if(fill_color != "none") { | 280 if(fill_color != "none") { |
276 paint = this._prepare_paint_color(fill_color, fill_alpha); | 281 paint = this._prepare_paint_color(fill_color, fill_alpha); |
277 paint.fill(path); | 282 paint.fill(tgt); |
278 } | 283 } |
279 } else { | 284 } else { |
280 black_paint.fill(path); | 285 black_paint.fill(tgt); |
281 } | 286 } |
282 if(stroke_color) { | 287 if(stroke_color) { |
283 if(stroke_color != "none") { | 288 if(stroke_color != "none") { |
284 paint = this._prepare_paint_color(stroke_color, stroke_alpha); | 289 paint = this._prepare_paint_color(stroke_color, stroke_alpha); |
285 paint.stroke(path); | 290 paint.stroke(tgt); |
286 } | 291 } |
287 } else { | 292 } else { |
288 black_paint.stroke(path); | 293 black_paint.stroke(tgt); |
289 } | 294 } |
295 }; | |
296 | |
297 loadSVG.prototype.parsePath=function(accu, coord,id, n) | |
298 { | |
299 var d = n.attr('d').value(); | |
300 var style = n.attr('style'); | |
301 var path = this.mb_rt.path_new(d); | |
302 | |
303 guessPathBoundingBox(coord,d); | |
304 this._set_paint(n, path); | |
290 coord.add_shape(path); | 305 coord.add_shape(path); |
291 | 306 |
292 make_mbnames(this.mb_rt, n, path); | 307 make_mbnames(this.mb_rt, n, path); |
293 }; | 308 }; |
294 | 309 |
404 if (coord.center.x > rx) | 419 if (coord.center.x > rx) |
405 coord.center.x = rx; | 420 coord.center.x = rx; |
406 if (coord.center.y > ry) | 421 if (coord.center.y > ry) |
407 coord.center.y = ry; | 422 coord.center.y = ry; |
408 | 423 |
409 if (style==null) { | |
410 paint = this.mb_rt.paint_color_new(0,0,0,0.1); | |
411 } else { | |
412 var items = style.value().split(';'); | |
413 var fill = ''; | |
414 var alpha = 1; | |
415 display = 'on'; | |
416 for(i in items) { | |
417 var f = items[i].split(':'); | |
418 if (f[0] == 'opacity') { | |
419 alpha = f[1]; | |
420 } else if (f[0] == 'fill') { | |
421 fill = f[1]; | |
422 } else if (f[0] == 'fill-opacity') { | |
423 } else if (f[0] == 'stroke') { | |
424 } else if (f[0] == 'stroken-width') { | |
425 } else if (f[0] == 'stroke-opacity') { | |
426 } else if (f[0] == 'display') { | |
427 display = f[1]; | |
428 } | |
429 } | |
430 if (display == 'none') { | |
431 return; | |
432 } | |
433 if (fill[0]=='#') { | |
434 var r,g,b; | |
435 r = parseInt(fill.substring(1,3),16)/256; | |
436 g = parseInt(fill.substring(3,5),16)/256; | |
437 b = parseInt(fill.substring(5,7),16)/256; | |
438 | |
439 paint = this.mb_rt.paint_color_new(r,g,b,parseFloat(alpha)); | |
440 } else if (fill.substring(0,3) == 'url') { | |
441 var id = fill.substring(5,fill.length-1); | |
442 var gr = this.gradients[id]; | |
443 paint = this.mb_rt.paint_linear_new(gr[0],gr[1],gr[2],gr[3]); | |
444 paint.set_stops(this.stop_ref[id]); | |
445 } else { | |
446 paint = this.mb_rt.paint_color_new(0,0,0,1); | |
447 } | |
448 } | |
449 var rect = this.mb_rt.rect_new(x,y,w,h,10, 10); | 424 var rect = this.mb_rt.rect_new(x,y,w,h,10, 10); |
450 paint.fill(rect); | 425 this._set_paint(n, rect); |
451 tcoord.add_shape(rect); | 426 tcoord.add_shape(rect); |
452 }; | 427 }; |
453 | 428 |
454 // When we parse a group, we need to calculate the origin of the group | 429 // When we parse a group, we need to calculate the origin of the group |
455 // so that we can resize the group without changing its origin point. | 430 // so that we can resize the group without changing its origin point. |