comparison nodejs/svg.js @ 787:0899dcac441c

Set bounding box attributes for JS Parse inkscape:bbox-x/y/width/height to - bbox_x - bbox_y - bbox_width - bbox_height
author Thinker K.F. Li <thinker@codemud.net>
date Tue, 31 Aug 2010 01:26:22 +0800
parents b6d9c42019d1
children 7ec13634c97d
comparison
equal deleted inserted replaced
786:b97c328a29b1 787:0899dcac441c
17 }; 17 };
18 18
19 19
20 function loadSVG(mb_rt, root, filename) { 20 function loadSVG(mb_rt, root, filename) {
21 var doc = libxml.parseXmlFile(filename); 21 var doc = libxml.parseXmlFile(filename);
22 var nodes = doc.root().childNodes(); 22 var _root = doc.root();
23 var nodes = _root.childNodes();
23 var coord = mb_rt.coord_new(root); 24 var coord = mb_rt.coord_new(root);
24 var k; 25 var k;
25 var accu=[1,0,0,0,1,0]; 26 var accu=[1,0,0,0,1,0];
26 this.mb_rt = mb_rt; 27 this.mb_rt = mb_rt;
27 this.stop_ref={}; 28 this.stop_ref={};
28 this.gradients={}; 29 this.gradients={};
29 root.center=new Object(); 30 root.center=new Object();
30 root.center.x = 10000; 31 root.center.x = 10000;
31 root.center.y = 10000; 32 root.center.y = 10000;
33
34 if(_root.attr("width")) {
35 k = _root.attr("width").value();
36 this.width = parseFloat(k);
37 }
38 if(_root.attr("height")) {
39 k = _root.attr("height").value();
40 this.height = parseFloat(k);
41 }
32 42
33 for(k in nodes) { 43 for(k in nodes) {
34 var n = nodes[k].name(); 44 var n = nodes[k].name();
35 if (n == "defs") { 45 if (n == "defs") {
36 this.parseDefs(root,nodes[k]); 46 this.parseDefs(root,nodes[k]);
235 if (coord.center.y > miny) 245 if (coord.center.y > miny)
236 coord.center.y = miny; 246 coord.center.y = miny;
237 }; 247 };
238 248
239 249
250 loadSVG.prototype._set_bbox = function(node, tgt) {
251 var a;
252 var vstr;
253
254 a = node.attr("bbox-x");
255 if(!a)
256 return;
257
258 vstr = a.value();
259 tgt.bbox_x = parseFloat(vstr);
260
261 a = node.attr("bbox-y");
262 vstr = a.value();
263 tgt.bbox_y = this.height - parseFloat(vstr);
264
265 a = node.attr("bbox-width");
266 vstr = a.value();
267 tgt.bbox_width = parseFloat(vstr);
268
269 a = node.attr("bbox-height");
270 vstr = a.value();
271 tgt.bbox_height = parseFloat(vstr);
272
273 tgt.center_x = tgt.bbox_width / 2 + tgt.bbox_x;
274 a = node.attr("transform-center-x");
275 if(a) {
276 vstr = a.value();
277 tgt.center_x += parseFloat(vstr);
278 }
279
280 tgt.center_y = tgt.bbox_height / 2 + tgt.bbox_y;
281 a = node.attr("transform-center-y");
282 if(a) {
283 vstr = a.value();
284 tgt.center_y -= parseFloat(vstr);
285 }
286 }
287
240 loadSVG.prototype._set_paint = function(node, tgt) { 288 loadSVG.prototype._set_paint = function(node, tgt) {
241 var style = node.attr('style'); 289 var style = node.attr('style');
242 var paint; 290 var paint;
243 var fill_alpha = 1; 291 var fill_alpha = 1;
244 var stroke_alpha = 1; 292 var stroke_alpha = 1;
299 var d = n.attr('d').value(); 347 var d = n.attr('d').value();
300 var style = n.attr('style'); 348 var style = n.attr('style');
301 var path = this.mb_rt.path_new(d); 349 var path = this.mb_rt.path_new(d);
302 350
303 guessPathBoundingBox(coord,d); 351 guessPathBoundingBox(coord,d);
352 this._set_bbox(n, path);
304 this._set_paint(n, path); 353 this._set_paint(n, path);
305 coord.add_shape(path); 354 coord.add_shape(path);
306 355
307 make_mbnames(this.mb_rt, n, path); 356 make_mbnames(this.mb_rt, n, path);
308 }; 357 };
421 if (coord.center.y > ry) 470 if (coord.center.y > ry)
422 coord.center.y = ry; 471 coord.center.y = ry;
423 472
424 var rect = this.mb_rt.rect_new(x,y,w,h,10, 10); 473 var rect = this.mb_rt.rect_new(x,y,w,h,10, 10);
425 this._set_paint(n, rect); 474 this._set_paint(n, rect);
475 this._set_bbox(n, tcoord);
426 tcoord.add_shape(rect); 476 tcoord.add_shape(rect);
427 }; 477 };
428 478
429 // When we parse a group, we need to calculate the origin of the group 479 // When we parse a group, we need to calculate the origin of the group
430 // so that we can resize the group without changing its origin point. 480 // so that we can resize the group without changing its origin point.
473 } 523 }
474 if (root.center.x > coord.center.x) 524 if (root.center.x > coord.center.x)
475 root.center.x = coord.center.x; 525 root.center.x = coord.center.x;
476 if (root.center.y > coord.center.y) 526 if (root.center.y > coord.center.y)
477 root.center.y = coord.center.y; 527 root.center.y = coord.center.y;
528
529 this._set_bbox(n, coord);
530
478 make_mbnames(this.mb_rt, n, coord); 531 make_mbnames(this.mb_rt, n, coord);
479 }; 532 };
480 533
481 loadSVG.prototype.parseImage=function(accu,coord,id, n) 534 loadSVG.prototype.parseImage=function(accu,coord,id, n)
482 { 535 {
523 var img = this.mb_rt.image_new(x,y,w,h); 576 var img = this.mb_rt.image_new(x,y,w,h);
524 var img_data = ldr.load(ref); 577 var img_data = ldr.load(ref);
525 var paint = this.mb_rt.paint_image_new(img_data); 578 var paint = this.mb_rt.paint_image_new(img_data);
526 paint.fill(img); 579 paint.fill(img);
527 tcoord.add_shape(img); 580 tcoord.add_shape(img);
581
582 this._set_bbox(n, img);
583
528 make_mbnames(this.mb_rt, n, img); 584 make_mbnames(this.mb_rt, n, img);
529 }; 585 };
530 586
531 loadSVG.prototype._MB_parseLinearGradient=function(root,n) 587 loadSVG.prototype._MB_parseLinearGradient=function(root,n)
532 { 588 {