annotate nodejs/svg.js @ 750:199bac90b90a

Add linearGradient support.
author wycc
date Thu, 26 Aug 2010 00:16:12 +0800
parents 6879aa403306
children ae1ae29348d1
rev   line source
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
1 var libxml = require('libxmljs');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
2 var sys=require('sys');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
3 var mbfly = require("mbfly");
646
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
4 var ldr = mbfly.img_ldr_new(".");
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
5
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
6
713
e60ae262127b Recognize color name
Thinker K.F. Li <thinker@branda.to>
parents: 712
diff changeset
7 var _std_colors = {
e60ae262127b Recognize color name
Thinker K.F. Li <thinker@branda.to>
parents: 712
diff changeset
8 "white": [1, 1, 1],
e60ae262127b Recognize color name
Thinker K.F. Li <thinker@branda.to>
parents: 712
diff changeset
9 "black": [0, 0, 0],
e60ae262127b Recognize color name
Thinker K.F. Li <thinker@branda.to>
parents: 712
diff changeset
10 "red": [1, 0, 0]
e60ae262127b Recognize color name
Thinker K.F. Li <thinker@branda.to>
parents: 712
diff changeset
11 };
e60ae262127b Recognize color name
Thinker K.F. Li <thinker@branda.to>
parents: 712
diff changeset
12
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
13 exports.loadSVG=function(mb_rt,root,filename) {
718
0cd59ce76e67 Refactor loadSVG as a class
Thinker K.F. Li <thinker@branda.to>
parents: 717
diff changeset
14 return new loadSVG(mb_rt, root, filename);
0cd59ce76e67 Refactor loadSVG as a class
Thinker K.F. Li <thinker@branda.to>
parents: 717
diff changeset
15 };
0cd59ce76e67 Refactor loadSVG as a class
Thinker K.F. Li <thinker@branda.to>
parents: 717
diff changeset
16
0cd59ce76e67 Refactor loadSVG as a class
Thinker K.F. Li <thinker@branda.to>
parents: 717
diff changeset
17 function loadSVG(mb_rt, root, filename) {
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
18 var doc = libxml.parseXmlFile(filename);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
19 var nodes = doc.root().childNodes();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
20 var coord = mb_rt.coord_new(root);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
21 var k;
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
22 this.mb_rt = mb_rt;
750
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
23 this.stop_ref={};
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
24 this.gradients={};
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
25
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
26 for(k in nodes) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
27 var n = nodes[k].name();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
28 if (n == "defs") {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
29 this.parseDefs(root,nodes[k]);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
30 } else if (n == "g") {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
31 this.parseGroup(root,'root_coord',nodes[k]);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
32 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
33 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
34 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
35
719
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
36 function make_mbnames(mb_rt, n, obj) {
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
37 var mbname;
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
38 var name;
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
39
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
40 if(!mb_rt.mbnames)
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
41 mb_rt.mbnames = {};
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
42
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
43 mbname = n.attr("mbname");
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
44 if(mbname) {
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
45 name = mbname.value();
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
46 mb_rt.mbnames[name] = obj;
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
47 }
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
48 }
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
49
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
50 function getInteger(n,name)
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
51 {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
52 if (n == null) return 0;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
53 var a = n.attr(name);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
54 if (a==null) return 0;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
55 return parseInt(a.value());
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
56 }
719
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
57
631
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
58 function parsePointSize(s)
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
59 {
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
60 var fs=0;
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
61 var i;
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
62
631
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
63 for(i=0;i<s.length;i++) {
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
64 if (s[i]<'0' || s[i] > '9') break;
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
65 fs = fs*10 + (s[i]-'0');
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
66 }
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
67 return fs;
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
68
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
69 }
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
70
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
71 function parseColor(c)
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
72 {
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
73 if (c[0] == '#') {
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
74 return parseInt(c.substring(1,3),16)<<16 | parseInt(c.substring(3,5),16)<<8 | parseInt(c.substring(5,7),16);
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
75 }
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
76 }
719
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
77
631
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
78 function parseTextStyle(style,n)
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
79 {
625
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
80 var attr;
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
81 if (n) {
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
82 attr = n.attr('style');
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
83 } else {
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
84 attr = null;
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
85 }
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
86 if (attr == null) {
631
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
87 return;
625
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
88 }
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
89 var f = attr.value().split(';');
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
90
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
91 for(i in f) {
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
92 var kv = f[i].split(':');
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
93 if (kv[0] == 'font-size') {
631
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
94 style.fs = parsePointSize(kv[1]);
625
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
95 } else if (kv[0] == "font-style") {
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
96 } else if (kv[0] == "font-weight") {
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
97 } else if (kv[0] == "fill") {
631
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
98 style.color = parseColor(kv[1]);
625
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
99 } else if (kv[0] == "fill-opacity") {
703
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
100 } else if (kv[0] == "stroke-opacity") {
625
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
101 } else if (kv[0] == "stroke") {
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
102 } else if (kv[0] == "stroke-width") {
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
103 } else if (kv[0] == "stroke-linecap") {
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
104 } else if (kv[0] == "stroke-linejoin") {
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
105 } else if (kv[0] == "stroke-lineopacity") {
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
106 } else if (kv[0] == "font-family") {
631
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
107 style.family = kv[1];
625
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
108 } else if (kv[0] == "font-stretch") {
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
109 } else if (kv[0] == "font-variant") {
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
110 } else if (kv[0] == "text-anchor") {
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
111 } else if (kv[0] == "text-align") {
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
112 } else if (kv[0] == "writing-mode") {
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
113 } else if (kv[0] == "line-height") {
632
e517bbefe0e9 Add the missing 'else'
wycc
parents: 631
diff changeset
114 } else {
625
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
115 sys.puts("Unknown style: "+kv[0]);
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
116 }
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
117 }
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
118 }
732
6879aa403306 Add set_text to the coordinate of the coord_t of the text.
wycc
parents: 720
diff changeset
119 function tspan_set_text(text)
6879aa403306 Add set_text to the coordinate of the coord_t of the text.
wycc
parents: 720
diff changeset
120 {
6879aa403306 Add set_text to the coordinate of the coord_t of the text.
wycc
parents: 720
diff changeset
121 this.text.set_text(text);
6879aa403306 Add set_text to the coordinate of the coord_t of the text.
wycc
parents: 720
diff changeset
122 }
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
123
718
0cd59ce76e67 Refactor loadSVG as a class
Thinker K.F. Li <thinker@branda.to>
parents: 717
diff changeset
124 loadSVG.prototype.parseTSpan=function(coord, n,style)
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
125 {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
126 var x = getInteger(n,'x');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
127 var y = getInteger(n,'y');
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
128 var tcoord = this.mb_rt.coord_new(coord);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
129 var nodes = n.childNodes();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
130 var k;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
131
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
132 var obj = this.mb_rt.stext_new(n.text(),x,y);
631
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
133 parseTextStyle(style,n);
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
134 style.paint = this.mb_rt.paint_color_new(1,1,1,1);
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
135 style.face=this.mb_rt.font_face_query(style.family, 2, 100);
631
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
136 obj.set_style([[20,style.face,style.fs]]);
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
137 style.paint.fill(obj);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
138 tcoord.add_shape(obj);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
139 for(k in nodes) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
140 var name = nodes[k].name();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
141 if (name == "tspan") {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
142 this.parseTSpan(tcoord,nodes[k]);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
143 } else {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
144 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
145 }
732
6879aa403306 Add set_text to the coordinate of the coord_t of the text.
wycc
parents: 720
diff changeset
146 tcoord.set_text=tspan_set_text;
6879aa403306 Add set_text to the coordinate of the coord_t of the text.
wycc
parents: 720
diff changeset
147 tcoord.text = obj;
719
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
148 make_mbnames(this.mb_rt, n, tcoord);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
149 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
150
718
0cd59ce76e67 Refactor loadSVG as a class
Thinker K.F. Li <thinker@branda.to>
parents: 717
diff changeset
151 loadSVG.prototype._prepare_paint_color=function(color, alpha) {
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
152 var paint;
713
e60ae262127b Recognize color name
Thinker K.F. Li <thinker@branda.to>
parents: 712
diff changeset
153 var c;
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
154
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
155 if (color[0]=='#') {
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
156 var r,g,b;
717
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
157 r = parseInt(color.substring(1,3),16)/255;
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
158 g = parseInt(color.substring(3,5),16)/255;
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
159 b = parseInt(color.substring(5,7),16)/255;
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
160 paint = this.mb_rt.paint_color_new(r, g, b, alpha);
713
e60ae262127b Recognize color name
Thinker K.F. Li <thinker@branda.to>
parents: 712
diff changeset
161 } else if(_std_colors[color]) {
e60ae262127b Recognize color name
Thinker K.F. Li <thinker@branda.to>
parents: 712
diff changeset
162 c = _std_colors[color];
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
163 paint = this.mb_rt.paint_color_new(c[0], c[1], c[2], alpha);
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
164 } else {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
165 paint = this.mb_rt.paint_color_new(0,0,0,1);
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
166 }
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
167 return paint;
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
168 }
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
169
718
0cd59ce76e67 Refactor loadSVG as a class
Thinker K.F. Li <thinker@branda.to>
parents: 717
diff changeset
170 loadSVG.prototype.parsePath=function(coord,id, n)
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
171 {
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
172 var d = n.attr('d').value();
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
173 var style = n.attr('style');
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
174 var path = this.mb_rt.path_new(d);
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
175 var paint;
717
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
176 var fill_alpha = 1;
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
177 var stroke_alpha = 1;
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
178 var fill_color;
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
179 var stroke_color;
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
180 var black_paint;
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
181
717
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
182 if(style != null) {
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
183 var items = style.value().split(';');
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
184 var alpha;
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
185
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
186 for(i in items) {
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
187 var f = items[i].split(':');
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
188 if (f[0] == 'opacity') {
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
189 alpha = f[1];
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
190 } else if (f[0] == 'fill') {
717
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
191 fill_color = f[1];
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
192 } else if (f[0] == 'fill-opacity') {
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
193 fill_alpha = parseFloat(f[1]);
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
194 } else if (f[0] == 'stroke') {
717
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
195 stroke_color = f[1];
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
196 } else if (f[0] == 'stroke-width') {
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
197 path.stroke_width = parseFloat(f[1]);
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
198 } else if (f[0] == 'stroke-opacity') {
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
199 stroke_alpha = parseFloat(f[1]);
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
200 }
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
201 }
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
202
717
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
203 }
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
204
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
205 if(!fill_color || !stroke_color)
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
206 black_paint = this.mb_rt.paint_color_new(0, 0, 0, 1);
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
207
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
208 if(fill_color) {
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
209 if(fill_color != "none") {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
210 paint = this._prepare_paint_color(fill_color, fill_alpha);
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
211 paint.fill(path);
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
212 }
717
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
213 } else {
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
214 black_paint.fill(path);
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
215 }
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
216 if(stroke_color) {
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
217 if(stroke_color != "none") {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
218 paint = this._prepare_paint_color(stroke_color, stroke_alpha);
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
219 paint.stroke(path);
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
220 }
717
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
221 } else {
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
222 black_paint.stroke(path);
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
223 }
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
224 coord.add_shape(path);
719
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
225
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
226 make_mbnames(this.mb_rt, n, path);
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
227 }
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
228
718
0cd59ce76e67 Refactor loadSVG as a class
Thinker K.F. Li <thinker@branda.to>
parents: 717
diff changeset
229 loadSVG.prototype.parseText=function(coord,id, n)
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
230 {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
231 var x = getInteger(n,'x');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
232 var y = getInteger(n,'y');
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
233 var tcoord = this.mb_rt.coord_new(coord);
631
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
234 var style = new Object();
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
235 style.fs = 20;
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
236 style.family = 'courier';
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
237 parseTextStyle(style,n);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
238 var nodes = n.childNodes();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
239 var k;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
240 for(k in nodes) {
720
9c5abb4e114b Fix issue of redefine a variable with the same name of a argument
Thinker K.F. Li <thinker@branda.to>
parents: 719
diff changeset
241 var c= nodes[k].name();
9c5abb4e114b Fix issue of redefine a variable with the same name of a argument
Thinker K.F. Li <thinker@branda.to>
parents: 719
diff changeset
242 if (c == "tspan") {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
243 this.parseTSpan(tcoord,nodes[k],style);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
244 } else {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
245 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
246 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
247
719
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
248 make_mbnames(this.mb_rt, n, tcoord);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
249 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
250
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
251 function parseTransform(coord, s)
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
252 {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
253 var off = s.indexOf('translate');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
254 if (off != -1) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
255 var ss = s.substring(off+9);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
256 for(i=0;i<ss.length;i++) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
257 if (ss[i] == '(') break;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
258 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
259 ss = ss.substring(i+1);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
260 for(i=0;i<ss.length;i++) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
261 if (ss[i] == ')') {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
262 ss = ss.substring(0,i);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
263 break;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
264 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
265 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
266 var f = ss.split(',');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
267 var x,y;
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
268 x = parseFloat(f[0]);
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
269 y = parseFloat(f[1]);
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
270 coord[0] = 1;
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
271 coord[1] = 0;
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
272 coord[2] = x;
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
273 coord[3] = 0;
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
274 coord[4] = 1;
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
275 coord[5] = y;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
276 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
277 off = s.indexOf('matrix');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
278 if (off != -1) {
703
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
279 var end = s.indexOf(')');
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
280 var m = s.substring(7,end);
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
281 var fields = m.split(',');
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
282 coord[0] = parseFloat(fields[0]);
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
283 coord[1] = parseFloat(fields[2]);
703
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
284 coord[2] = parseFloat(fields[4]);
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
285 coord[3] = parseFloat(fields[1]);
703
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
286 coord[4] = parseFloat(fields[3]);
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
287 coord[5] = parseFloat(fields[5]);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
288 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
289 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
290
718
0cd59ce76e67 Refactor loadSVG as a class
Thinker K.F. Li <thinker@branda.to>
parents: 717
diff changeset
291 loadSVG.prototype.parseRect=function(coord, id, n)
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
292 {
703
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
293 var x = getInteger(n,'x');
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
294 var y = getInteger(n,'y');
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
295 var w = getInteger(n,'width');
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
296 var h = getInteger(n,'height');
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
297 var paint;
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
298
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
299 var style = n.attr('style');
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
300
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
301 if (style==null) {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
302 paint = this.mb_rt.paint_color_new(0,0,0,0.1);
703
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
303 } else {
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
304 var items = style.value().split(';');
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
305 var fill = '';
708
ac9c20db953e Default alpha of a tag is '1'
Thinker K.F. Li <thinker@branda.to>
parents: 706
diff changeset
306 var alpha = 1;
703
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
307 for(i in items) {
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
308 var f = items[i].split(':');
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
309 if (f[0] == 'opacity') {
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
310 alpha = f[1];
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
311 } else if (f[0] == 'fill') {
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
312 fill = f[1];
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
313 } else if (f[0] == 'fill-opacity') {
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
314 } else if (f[0] == 'stroke') {
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
315 } else if (f[0] == 'stroken-width') {
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
316 } else if (f[0] == 'stroke-opacity') {
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
317 }
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
318 }
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
319 if (fill[0]=='#') {
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
320 var r,g,b;
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
321 r = parseInt(fill.substring(1,3),16)/256;
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
322 g = parseInt(fill.substring(3,5),16)/256;
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
323 b = parseInt(fill.substring(5,7),16)/256;
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
324
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
325 paint = this.mb_rt.paint_color_new(r,g,b,parseFloat(alpha));
750
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
326 } else if (fill.substring(0,3) == 'url') {
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
327 var id = fill.substring(5,fill.length-1);
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
328 var gr = this.gradients[id];
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
329 paint = this.mb_rt.paint_linear_new(gr[0],gr[1],gr[2],gr[3]);
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
330 paint.set_stops(this.stop_ref[id]);
703
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
331 } else {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
332 paint = this.mb_rt.paint_color_new(0,0,0,1);
703
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
333 }
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
334 }
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
335 var rect = this.mb_rt.rect_new(x,y,w,h,10, 10);
703
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
336 paint.fill(rect);
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
337 coord.add_shape(rect);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
338 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
339
718
0cd59ce76e67 Refactor loadSVG as a class
Thinker K.F. Li <thinker@branda.to>
parents: 717
diff changeset
340 loadSVG.prototype.parseGroup=function(root, group_id, n)
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
341 {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
342 var k;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
343 var nodes = n.childNodes();
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
344 var coord = this.mb_rt.coord_new(root);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
345 // Parse the transform and style here
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
346 var trans = n.attr('transform');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
347 if (trans!=null) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
348 parseTransform(coord, trans.value());
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
349 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
350
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
351 for(k in nodes) {
719
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
352 var c = nodes[k].name();
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
353 var attr = nodes[k].attr('id');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
354 var id;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
355 if (attr) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
356 id = attr.value();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
357 }
719
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
358 if (c == "g") {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
359 this.parseGroup(coord, id, nodes[k]);
719
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
360 } else if (c == "path") {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
361 this.parsePath(coord, id, nodes[k]);
719
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
362 } else if (c == "text") {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
363 this.parseText(coord, id, nodes[k]);
719
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
364 } else if (c == "rect") {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
365 this.parseRect(coord, id, nodes[k]);
719
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
366 } else if (c == "image") {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
367 this.parseImage(coord, id, nodes[k]);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
368 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
369 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
370
719
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
371 make_mbnames(this.mb_rt, n, coord);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
372 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
373
718
0cd59ce76e67 Refactor loadSVG as a class
Thinker K.F. Li <thinker@branda.to>
parents: 717
diff changeset
374 loadSVG.prototype.parseImage=function(coord,id, n)
646
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
375 {
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
376 var ref = n.attr('href').value();
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
377
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
378 if (ref == null) return;
647
492da72e6537 Add image tag support.
wycc
parents: 646
diff changeset
379 if (ref.substr(0,7) == "file://") {
492da72e6537 Add image tag support.
wycc
parents: 646
diff changeset
380 ref = ref.substring(7);
492da72e6537 Add image tag support.
wycc
parents: 646
diff changeset
381 } else if (ref.substr(0,5)=="file:") {
492da72e6537 Add image tag support.
wycc
parents: 646
diff changeset
382 ref = ref.substring(5);
492da72e6537 Add image tag support.
wycc
parents: 646
diff changeset
383 } else {
646
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
384 return;
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
385 }
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
386 sys.puts("Load image "+ref);
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
387 var w;
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
388 var h;
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
389 var x,y;
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
390
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
391 w = n.attr("width");
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
392 if (w == null) return;
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
393 w = parseInt(w.value());
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
394 h = n.attr("height");
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
395 if (h == null) return;
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
396 h = parseInt(h.value());
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
397 x = n.attr("x");
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
398 if (x == null) return;
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
399 x = parseInt(x.value());
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
400 y = n.attr("y");
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
401 if (y == null) return;
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
402 y = parseInt(y.value());
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
403 var img = this.mb_rt.image_new(x,y,w,h);
646
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
404 var img_data = ldr.load(ref);
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
405 var paint = this.mb_rt.paint_image_new(img_data);
646
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
406 paint.fill(img);
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
407 coord.add_shape(img);
719
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
408 make_mbnames(this.mb_rt, n, img);
646
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
409 }
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
410
750
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
411 loadSVG.prototype._MB_parseLinearGradient=function(root,n)
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
412 {
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
413 var id = n.attr('id');
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
414 var k;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
415 var nodes = n.childNodes();
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
416
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
417 if (id == null) return;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
418 var x1 = n.attr("x1");
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
419 var y1 = n.attr("y1");
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
420 var x2 = n.attr("x2");
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
421 var y2 = n.attr("y2");
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
422 var gr;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
423 var color, opacity;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
424 var stops;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
425 var r,g,b;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
426 stops=[];
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
427 for(k in nodes) {
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
428 var ss = nodes[k];
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
429 if (ss.name()=="stop") {
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
430 var style = ss.attr("style").value();
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
431 var items = style.split(';');
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
432 var off = parseInt(ss.attr('offset').value());
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
433 color = 'black';
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
434 opacity = 1;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
435 for (i in items) {
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
436 it = items[i];
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
437 var f = it.split(':');
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
438 k = f[0];
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
439 v = f[1];
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
440 if (k == 'stop-color') {
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
441 color = v.substring(1);
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
442 if (v == 'white') {
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
443 r = 1;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
444 g = 1;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
445 b = 1;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
446 } else if (v == 'black') {
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
447 r = 0;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
448 g = 0;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
449 b = 0;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
450 } else {
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
451 r = parseInt(color.substring(0,2),16)/255.0;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
452 g = parseInt(color.substring(2,4),16)/255.0;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
453 b = parseInt(color.substring(4,6),16)/255.0;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
454 }
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
455 } else if (k=='stop-opacity') {
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
456 opacity = parseFloat(v);
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
457 }
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
458 }
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
459 stops.push([off, r,g,b,opacity]);
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
460 }
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
461 }
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
462 var href = n.attr('href');
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
463 if (href != null) {
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
464 href = href.value();
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
465 pstops = this.stop_ref[href.substring(1)];
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
466 stops = pstops.concat(stops);
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
467 }
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
468 id = id.value();
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
469 this.stop_ref[id] = stops;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
470 if (x1)
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
471 x1 = parseFloat(x1.value());
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
472 if (x2)
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
473 x2 = parseFloat(x2.value());
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
474 if (y1)
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
475 y1 = parseFloat(y1.value());
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
476 if (y2)
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
477 y2 = parseFloat(y2.value());
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
478 this.gradients[id] = [x1,y1,x2,y2];
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
479 }
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
480
718
0cd59ce76e67 Refactor loadSVG as a class
Thinker K.F. Li <thinker@branda.to>
parents: 717
diff changeset
481 loadSVG.prototype.parseDefs=function(root,n)
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
482 {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
483 var k;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
484 var nodes = n.childNodes();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
485
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
486 for(k in nodes) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
487 var name = nodes[k].name();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
488 if (name == "linearGradient") {
750
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
489 this._MB_parseLinearGradient(root,nodes[k]);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
490 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
491 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
492 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
493
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
494