diff nodejs/svg.js @ 880:ac3e8492ad74 abs_n_rel_center

Formalize path data for MadButterfly. Inkscape and other editors would omit 'l' or 'L' after 'm' or 'M'. MadButterfly can not handle it, now. So, we work around it at SVG parser.
author Thinker K.F. Li <thinker@codemud.net>
date Sat, 25 Sep 2010 18:46:37 +0800
parents 44f46d6873be
children a17c4e231e54
line wrap: on
line diff
--- a/nodejs/svg.js	Sat Sep 25 17:43:39 2010 +0800
+++ b/nodejs/svg.js	Sat Sep 25 18:46:37 2010 +0800
@@ -703,12 +703,51 @@
     tgt.stroke_width = stroke_width;
 };
 
+function formalize_path_data(d) {
+    var posM, posm;
+    var pos;
+    var nums = "0123456789+-.";
+    var rel = false;
+    var cmd;
+
+    posM = d.search("M");
+    posm = d.search("m");
+    pos = posm < posM? posm: posM;
+    if(pos == -1)
+	pos = posM == -1? posm: posM;
+    if(pos == -1)
+	return d;
+
+    if(posm == pos)
+	rel = true;
+    
+    pos = pos + 1;
+    while(pos < d.length && " ,".search(d[pos]) >= 0)
+	pos++;
+    while(pos < d.length && nums.search(d[pos]) >= 0)
+	pos++;
+    while(pos < d.length && " ,".search(d[pos]) >= 0)
+	pos++;
+    while(pos < d.length && nums.search(d[pos]) >= 0)
+	pos++;
+    while(pos < d.length && " ,".search(d[pos]) >= 0)
+	pos++;
+    if(nums.search(d[pos]) >= 0) {
+	if(rel)
+	    cmd = "l";
+	else
+	    cmd = "L";
+	d = d.substring(0, pos) + cmd + formalize_path_data(d.substring(pos));
+    }
+    return d;
+}
+
 loadSVG.prototype.parsePath=function(accu, coord,id, n)
 {
-    var d = n.attr('d').value();
+    var d = formalize_path_data(n.attr('d').value());
     var style = n.attr('style');
     var path = this.mb_rt.path_new(d);
-    
+
     guessPathBoundingBox(coord,d);
     coord.add_shape(path);
     this._set_paint(n, path);