# HG changeset patch # User Thinker K.F. Li # Date 1280024344 -28800 # Node ID 2f60a2dbe0b3c46b3f8c2590e822438557e2e2f9 # Parent 6f71f1b8e4e70a1b62f6c366153dfc26ce709dc2# Parent e517bbefe0e97f3029c7437bc15a3c2da602925d Merge heads from fourdollars and wycc. diff -r 6f71f1b8e4e7 -r 2f60a2dbe0b3 nodejs/README --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nodejs/README Sun Jul 25 10:19: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 + diff -r 6f71f1b8e4e7 -r 2f60a2dbe0b3 nodejs/svg.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nodejs/svg.js Sun Jul 25 10:19:04 2010 +0800 @@ -0,0 +1,216 @@ +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 parsePointSize(s) +{ + var fs=0; + var i; + + for(i=0;i '9') break; + fs = fs*10 + (s[i]-'0'); + } + return fs; + +} + + +function parseColor(c) +{ + if (c[0] == '#') { + return parseInt(c.substring(1,3),16)<<16 | parseInt(c.substring(3,5),16)<<8 | parseInt(c.substring(5,7),16); + } +} +function parseTextStyle(style,n) +{ + var attr; + if (n) { + attr = n.attr('style'); + } else { + attr = null; + } + if (attr == null) { + return; + } + var f = attr.value().split(';'); + + for(i in f) { + var kv = f[i].split(':'); + if (kv[0] == 'font-size') { + style.fs = parsePointSize(kv[1]); + } else if (kv[0] == "font-style") { + } else if (kv[0] == "font-weight") { + } else if (kv[0] == "fill") { + style.color = parseColor(kv[1]); + } else if (kv[0] == "fill-opacity") { + } else if (kv[0] == "stroke") { + } else if (kv[0] == "stroke-width") { + } else if (kv[0] == "stroke-linecap") { + } else if (kv[0] == "stroke-linejoin") { + } else if (kv[0] == "stroke-lineopacity") { + } else if (kv[0] == "font-family") { + style.family = kv[1]; + } else if (kv[0] == "font-stretch") { + } else if (kv[0] == "font-variant") { + } else if (kv[0] == "text-anchor") { + } else if (kv[0] == "text-align") { + } else if (kv[0] == "writing-mode") { + } else if (kv[0] == "line-height") { + } else { + sys.puts("Unknown style: "+kv[0]); + } + } +} + +function _MB_parseTSpan(coord, n,style) +{ + var x = getInteger(n,'x'); + var y = getInteger(n,'y'); + var tcoord = mb_rt.coord_new(coord); + var nodes = n.childNodes(); + var k; + + sys.puts(n.text()); + var obj = mb_rt.stext_new(n.text(),x,y); + parseTextStyle(style,n); + style.paint = mb_rt.paint_color_new(1,1,1,1); + style.face=mb_rt.font_face_query(style.family, 2, 100); + obj.set_style([[20,style.face,style.fs]]); + style.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 style = new Object(); + style.fs = 20; + style.family = 'courier'; + parseTextStyle(style,n); + var nodes = n.childNodes(); + var k; + for(k in nodes) { + var n = nodes[k].name(); + if (n == "tspan") { + _MB_parseTSpan(tcoord,nodes[k],style); + } else { + } + } + + +} + + +function parseTransform(coord, s) +{ + var off = s.indexOf('translate'); + if (off != -1) { + var ss = s.substring(off+9); + for(i=0;i + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + item1 + + + item1 + + + item1 + + + item1 + + + item1 + + + item1 + + + item1 + + + item1 + + + item1 + + Menu test + + + + + + + + diff -r 6f71f1b8e4e7 -r 2f60a2dbe0b3 nodejs/testcase.js --- a/nodejs/testcase.js Sat Jul 24 16:13:06 2010 +0800 +++ b/nodejs/testcase.js Sun Jul 25 10:19: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);