changeset 624:d45c928f6523

Add SVG parser sample code.
author wycc
date Thu, 15 Jul 2010 22:34:04 +0800
parents d561b2415711
children 9f2080b68f8e
files nodejs/README nodejs/svg.js nodejs/test.svg nodejs/testcase.js
diffstat 4 files changed, 485 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nodejs/README	Thu Jul 15 22:34:04 2010 +0800
@@ -0,0 +1,22 @@
+svg.js: 
+===============
+
+The sample code to load an SVG file. It requires the libxml, which is available at http://github.com/polotek/libxmljs/tree/refactor.
+Please copy the libxmljs.node to the objs/default if you want to test the libxml+mbfly without installing them into the system. The
+path can be defined by the NODE_PATH environment variable.
+
+(1) Compile MadButterfly with nodejs support
+ ~user/MadButterfly$ ./configure --enable-nodejs; make
+
+(2) Compile libxml
+ ~user/MadButterfly/nodejs$ git clone http://github.com/polotek/libxmljs.git
+ ~user/MadButterfly/nodejs$ cd libxml; make
+
+(3) Copy the libxml.node to the nodejs build directory
+ ~user/MadButterfly/nodejs$ cp libxml/libxml.node objs/default
+(4) Set the path
+ ~user/MadButterfly/nodejs$ export NODE_PATH=objs/default
+
+(5) Execute svg.js
+ ~user/MadButterfly/nodejs$ node svg.js
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nodejs/svg.js	Thu Jul 15 22:34:04 2010 +0800
@@ -0,0 +1,157 @@
+var libxml = require('libxmljs');
+var sys=require('sys');
+var mbfly = require("mbfly");
+var mb_rt = new mbfly.mb_rt(":0.0", 720,480);
+
+function MB_loadSVG(mb_rt,root,filename) {
+    var doc = libxml.parseXmlFile(filename);
+    var nodes = doc.root().childNodes();
+    var coord = mb_rt.coord_new(root);
+	var k;
+
+    for(k in nodes) {
+	    var n = nodes[k].name();
+		if (n == "defs") {
+		    _MB_parseDefs(root,nodes[k]);
+		} else if (n == "g") {
+		    _MB_parseGroup(root,'root_coord',nodes[k]);
+		} 
+    }
+}
+
+function getInteger(n,name)
+{
+    if (n == null) return 0;
+    var a = n.attr(name);
+	if (a==null) return 0;
+	return parseInt(a.value());
+}
+
+function parseStyle(n)
+{
+}
+
+function _MB_parseTSpan(coord, n)
+{
+    var x = getInteger(n,'x');
+    var y = getInteger(n,'y');
+	var tcoord = mb_rt.coord_new(coord);
+    var paint = parseStyle(n);
+	var nodes = n.childNodes();
+	var k;
+
+    sys.puts(n.text());
+    var obj = mb_rt.stext_new(n.text(),x,y);
+	var paint = mb_rt.paint_color_new(1,1,1,1);
+	var face=mb_rt.font_face_query("courier", 2, 100);
+	obj.set_style([[5,face,20]]);
+	paint.fill(obj);
+	tcoord.add_shape(obj);
+	for(k in nodes) {
+	    var name = nodes[k].name();
+		if (name == "tspan") {
+		    _MB_parseTSpan(tcoord,nodes[k]);
+		} else {
+		}
+	}
+}
+
+function _MB_parseText(coord,id, n)
+{
+    var x = getInteger(n,'x');
+    var y = getInteger(n,'y');
+	var tcoord = mb_rt.coord_new(coord);
+    var paint = parseStyle(n);
+	var nodes = n.childNodes();
+	var k;
+	for(k in nodes) {
+	    var n = nodes[k].name();
+		if (n == "tspan") {
+	        _MB_parseTSpan(tcoord,nodes[k]);
+		} else {
+		}
+	}
+	
+    
+}
+
+
+function parseTransform(coord, s)
+{
+    var off = s.indexOf('translate');
+	if (off != -1) {
+	    var ss = s.substring(off+9);
+		for(i=0;i<ss.length;i++) {
+		    if (ss[i] == '(') break;
+		}
+		ss = ss.substring(i+1);
+		for(i=0;i<ss.length;i++) {
+		    if (ss[i] == ')') {
+				ss = ss.substring(0,i);
+				break;
+			}
+		}
+		var f = ss.split(',');
+		var x,y;
+		x = parseInt(f[0]);
+		y = parseInt(f[1]);
+        coord[2] = x;
+		coord[5] = y;
+	}
+	off = s.indexOf('matrix');
+	if (off != -1) {
+	    sys.puts("matrix");
+	}
+}
+
+function _MB_parseRect(coord, id, n) 
+{
+    
+}
+
+function _MB_parseGroup(root, group_id, n)
+{
+    var k;
+    var nodes = n.childNodes();
+    var coord = mb_rt.coord_new(root);
+	// Parse the transform and style here
+	var trans = n.attr('transform');
+	if (trans!=null) {
+	    parseTransform(coord, trans.value());
+	}
+
+	for(k in nodes) {
+	    var n = nodes[k].name();
+		var attr = nodes[k].attr('id');
+		var id;
+		if (attr) {
+		    id = attr.value();
+		}
+		if (n == "g") {
+		    _MB_parseGroup(coord, id, nodes[k]);
+		} else if (n == "text") {
+		    _MB_parseText(coord, id, nodes[k]);
+		} else if (n == "rect") {
+		    _MB_parseRect(coord, id, nodes[k]);
+		}
+	}
+    
+}
+
+
+function _MB_parseDefs(root,n) 
+{
+    var k;
+	var nodes = n.childNodes();
+    
+	for(k in nodes) {
+	    var name = nodes[k].name();
+	    if (name == "linearGradient") {
+		    //_MB_parseLinearGradient(root,nodes[k]);
+		}
+	}
+}
+
+
+MB_loadSVG(mb_rt,mb_rt.root,"test.svg");
+mb_rt.redraw_all();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nodejs/test.svg	Thu Jul 15 22:34:04 2010 +0800
@@ -0,0 +1,305 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!-- Created with Inkscape (http://www.inkscape.org/) -->
+<svg
+   xmlns:ns0="http://madbutterfly.sourceforge.net/DTD/madbutterfly.dtd"
+   xmlns:dc="http://purl.org/dc/elements/1.1/"
+   xmlns:cc="http://creativecommons.org/ns#"
+   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+   xmlns:svg="http://www.w3.org/2000/svg"
+   xmlns="http://www.w3.org/2000/svg"
+   xmlns:xlink="http://www.w3.org/1999/xlink"
+   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
+   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
+   width="720"
+   height="480"
+   id="svg2"
+   sodipodi:version="0.32"
+   inkscape:version="0.46"
+   sodipodi:docname="list.svg"
+   version="1.0"
+   inkscape:output_extension="org.inkscape.output.svg.inkscape">
+  <sodipodi:namedview
+     id="base"
+     pagecolor="#ffffff"
+     bordercolor="#666666"
+     borderopacity="1.0"
+     gridtolerance="10000"
+     guidetolerance="10"
+     objecttolerance="10"
+     inkscape:pageopacity="0.0"
+     inkscape:pageshadow="2"
+     inkscape:zoom="1.0347223"
+     inkscape:cx="267.0313"
+     inkscape:cy="228.90269"
+     inkscape:document-units="px"
+     inkscape:current-layer="layer4"
+     showgrid="false"
+     inkscape:window-width="1024"
+     inkscape:window-height="768"
+     inkscape:window-x="0"
+     inkscape:window-y="0" />
+  <defs
+     id="defs4">
+    <linearGradient
+       id="linearGradient3183">
+      <stop
+         style="stop-color:#000000;stop-opacity:1;"
+         offset="0"
+         id="stop3185" />
+      <stop
+         style="stop-color:#505050;stop-opacity:1;"
+         offset="1"
+         id="stop3187" />
+    </linearGradient>
+    <inkscape:perspective
+       id="perspective10"
+       inkscape:persp3d-origin="372.04724 : 350.78739 : 1"
+       inkscape:vp_z="744.09448 : 526.18109 : 1"
+       inkscape:vp_y="0 : 1000 : 0"
+       inkscape:vp_x="0 : 526.18109 : 1"
+       sodipodi:type="inkscape:persp3d" />
+    <linearGradient
+       inkscape:collect="always"
+       xlink:href="#linearGradient3183"
+       id="linearGradient3189"
+       x1="0"
+       y1="239.5"
+       x2="719.99998"
+       y2="239.5"
+       gradientUnits="userSpaceOnUse" />
+  </defs>
+  <metadata
+     id="metadata7">
+    <rdf:RDF>
+      <cc:Work
+         rdf:about="">
+        <dc:format>image/svg+xml</dc:format>
+        <dc:type
+           rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
+      </cc:Work>
+    </rdf:RDF>
+    <ns0:scenes
+       current="1" />
+  </metadata>
+  <g
+     inkscape:groupmode="layer"
+     id="layer2"
+     inkscape:label="Background"
+     sodipodi:insensitive="true">
+    <rect
+       style="opacity:1;fill:url(#linearGradient3189);fill-opacity:1;stroke:#000000;stroke-width:0.9993065;stroke-opacity:1"
+       id="rect3181"
+       width="719.00067"
+       height="480.0007"
+       x="0.49965325"
+       y="-0.50034672" />
+  </g>
+  <g
+     id="layer1"
+     inkscape:groupmode="layer"
+     inkscape:label="Layer 1">
+    <g
+       transform="translate(147.14286,-2.85715)"
+       id="item1"
+       mbname="item1"
+       style="fill-opacity:1">
+      <text
+         id="text2395"
+         y="60"
+         x="157.14285"
+         style="font-size:24px;font-style:normal;font-weight:bold;fill:#ffffff;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Times New Roman;-inkscape-font-specification:Times New Roman Bold;font-stretch:normal;font-variant:normal;text-anchor:start;text-align:start;writing-mode:lr;line-height:125%"
+         xml:space="preserve"
+         sodipodi:linespacing="125%"><tspan
+           mbname="item1text"
+           y="60"
+           x="157.14285"
+           id="tspan2397"
+           sodipodi:role="line">item1</tspan></text>
+    </g>
+    <g
+       transform="translate(146.65319,39.689836)"
+       id="item2"
+       mbname="item2"
+       style="fill-opacity:1">
+      <text
+         id="text2421"
+         y="60"
+         x="157.14285"
+         style="font-size:24px;font-style:normal;font-weight:bold;fill:#ffffff;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Bitstream Vera Sans;-inkscape-font-specification:Bitstream Vera Sans Bold;font-stretch:normal;font-variant:normal;text-anchor:start;text-align:start;writing-mode:lr;line-height:125%"
+         xml:space="preserve"
+         sodipodi:linespacing="125%"><tspan
+           mbname="item2text"
+           y="60"
+           x="157.14285"
+           id="tspan2423"
+           sodipodi:role="line">item1</tspan></text>
+    </g>
+    <g
+       transform="translate(148.3266,82.236798)"
+       id="item3"
+       mbname="item3"
+       style="fill-opacity:1">
+      <text
+         id="item3text"
+         y="60"
+         x="157.14285"
+         style="font-size:24px;font-style:normal;font-weight:bold;fill:#ffffff;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Bitstream Vera Sans;-inkscape-font-specification:Bitstream Vera Sans Bold;font-stretch:normal;font-variant:normal;text-anchor:start;text-align:start;writing-mode:lr;line-height:125%"
+         xml:space="preserve"
+         sodipodi:linespacing="125%"><tspan
+           mbname="item3text"
+           y="60"
+           x="157.14285"
+           id="tspan2429"
+           sodipodi:role="line">item1</tspan></text>
+    </g>
+    <g
+       transform="translate(147.83692,124.7838)"
+       id="item4"
+       mbname="item4"
+       style="fill-opacity:1">
+      <text
+         id="item4text"
+         y="60"
+         x="157.14285"
+         style="font-size:24px;font-style:normal;font-weight:bold;fill:#ffffff;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Bitstream Vera Sans;-inkscape-font-specification:Bitstream Vera Sans Bold;font-stretch:normal;font-variant:normal;text-anchor:start;text-align:start;writing-mode:lr;line-height:125%"
+         xml:space="preserve"
+         sodipodi:linespacing="125%"><tspan
+           mbname="item4text"
+           y="60"
+           x="157.14285"
+           id="tspan2435"
+           sodipodi:role="line">item1</tspan></text>
+    </g>
+    <g
+       transform="translate(146.30615,167.33077)"
+       id="item5"
+       mbname="item5"
+       style="fill-opacity:1">
+      <text
+         id="item5text"
+         y="60"
+         x="157.14285"
+         style="font-size:24px;font-style:normal;font-weight:bold;fill:#ffffff;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Bitstream Vera Sans;-inkscape-font-specification:Bitstream Vera Sans Bold;font-stretch:normal;font-variant:normal;text-anchor:start;text-align:start;writing-mode:lr;line-height:125%"
+         xml:space="preserve"
+         sodipodi:linespacing="125%"><tspan
+           mbname="item5text"
+           y="60"
+           x="157.14285"
+           id="tspan2441"
+           sodipodi:role="line">item1</tspan></text>
+    </g>
+    <g
+       transform="translate(145.81648,209.87776)"
+       id="item6"
+       mbname="item6"
+       style="fill-opacity:1">
+      <text
+         id="item6text"
+         y="60"
+         x="157.14285"
+         style="font-size:24px;font-style:normal;font-weight:bold;fill:#ffffff;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Bitstream Vera Sans;-inkscape-font-specification:Bitstream Vera Sans Bold;font-stretch:normal;font-variant:normal;text-anchor:start;text-align:start;writing-mode:lr;line-height:125%"
+         xml:space="preserve"
+         sodipodi:linespacing="125%"><tspan
+           mbname="item6text"
+           y="60"
+           x="157.14285"
+           id="tspan2447"
+           sodipodi:role="line">item1</tspan></text>
+    </g>
+    <g
+       transform="translate(147.48989,252.42474)"
+       id="item7"
+       mbname="item7"
+       style="fill-opacity:1">
+      <text
+         id="item7text"
+         y="60"
+         x="157.14285"
+         style="font-size:24px;font-style:oblique;font-weight:normal;fill:#ffffff;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Bitstream Vera Sans;-inkscape-font-specification:Bitstream Vera Sans Oblique;font-stretch:normal;font-variant:normal;text-anchor:start;text-align:start;writing-mode:lr;line-height:125%"
+         xml:space="preserve"
+         sodipodi:linespacing="125%"><tspan
+           mbname="item7text"
+           y="60"
+           x="157.14285"
+           id="tspan2453"
+           sodipodi:role="line">item1</tspan></text>
+    </g>
+    <g
+       transform="translate(147.00021,294.97172)"
+       id="item8"
+       mbname="item8"
+       style="fill-opacity:1">
+      <text
+         id="item8text"
+         y="60"
+         x="157.14285"
+         style="font-size:24px;font-style:normal;font-weight:normal;fill:#ffffff;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Bitstream Vera Sans"
+         xml:space="preserve"><tspan
+           mbname="item8text"
+           y="60"
+           x="157.14285"
+           id="tspan2459"
+           sodipodi:role="line">item1</tspan></text>
+    </g>
+    <g
+       transform="translate(143.79604,337.5187)"
+       id="item9"
+       mbname="item9"
+       style="fill-opacity:1">
+      <text
+         mbname=""
+         id="text2407"
+         y="60"
+         x="157.14285"
+         style="font-size:24px;font-style:normal;font-weight:normal;fill:#ffffff;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Bitstream Vera Sans"
+         xml:space="preserve"><tspan
+           mbname="item9text"
+           y="60"
+           x="157.14285"
+           id="tspan2409"
+           sodipodi:role="line">item1</tspan></text>
+    </g>
+    <text
+       id="text2416"
+       y="44.383541"
+       x="24.177626"
+       style="font-size:24px;font-style:normal;font-weight:normal;fill:#ffffff;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Bitstream Vera Sans"
+       xml:space="preserve"><tspan
+         mbname="item1text"
+         y="44.383541"
+         x="24.177626"
+         id="tspan2418"
+         sodipodi:role="line">Menu test</tspan></text>
+  </g>
+  <g
+     inkscape:groupmode="layer"
+     id="layer4"
+     inkscape:label="lightbar">
+    <g
+       mbname="item_lightbar"
+       transform="matrix(0.9148913,0,0,1,168.41407,93.684101)"
+       id="item_lightbar"
+       style="stroke:none">
+      <rect
+         style="opacity:0.3669725;fill:#001f41;fill-opacity:1;stroke:none;stroke-width:0.99680871;stroke-opacity:1"
+         id="rect3191"
+         width="442.8623"
+         height="29.190758"
+         x="0.19951171"
+         y="31.140766"
+         transform="matrix(0.9999958,-2.895334e-3,0,1,0,0)"
+         rx="10.000001"
+         ry="10" />
+      <rect
+         style="opacity:0.3669725;fill:#eafbf3;fill-opacity:1;stroke:none;stroke-width:0.99680871;stroke-opacity:1"
+         id="rect2405"
+         width="442.86224"
+         height="29.190758"
+         x="-4.099226"
+         y="27.920256"
+         transform="matrix(0.9999958,-2.8953343e-3,0,1,0,0)"
+         rx="10"
+         ry="10" />
+    </g>
+  </g>
+</svg>
--- a/nodejs/testcase.js	Wed Jun 16 20:25:06 2010 +0800
+++ b/nodejs/testcase.js	Thu Jul 15 22:34:04 2010 +0800
@@ -40,5 +40,5 @@
 	var deg = (i++) * 0.1;
 	coord[2] = (i % 20) * 10;
 	mb_rt.redraw_changed();
-    }, 50);
+    }, 20);
 setTimeout(function() { sys.puts("timeout"); }, 1000);