annotate nodejs/svg.js @ 717:b822b1912d67

Paint with black for unspecified, not "none", fill and stroke.
author Thinker K.F. Li <thinker@branda.to>
date Sat, 14 Aug 2010 23:52:43 +0800
parents f53e45d1fcd0
children 0cd59ce76e67
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) {
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
14 var doc = libxml.parseXmlFile(filename);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
15 var nodes = doc.root().childNodes();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
16 var coord = mb_rt.coord_new(root);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
17 var k;
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
18 this.mb_rt = mb_rt;
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
19
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
20 for(k in nodes) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
21 var n = nodes[k].name();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
22 if (n == "defs") {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
23 this.parseDefs(root,nodes[k]);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
24 } else if (n == "g") {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
25 this.parseGroup(root,'root_coord',nodes[k]);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
26 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
27 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
28 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
29
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
30 function getInteger(n,name)
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
31 {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
32 if (n == null) return 0;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
33 var a = n.attr(name);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
34 if (a==null) return 0;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
35 return parseInt(a.value());
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
36 }
631
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
37 function parsePointSize(s)
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
38 {
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
39 var fs=0;
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
40 var i;
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
41
631
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
42 for(i=0;i<s.length;i++) {
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
43 if (s[i]<'0' || s[i] > '9') break;
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
44 fs = fs*10 + (s[i]-'0');
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
45 }
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
46 return fs;
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
47
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
48 }
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
49
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
50
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
51 function parseColor(c)
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
52 {
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
53 if (c[0] == '#') {
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
54 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
55 }
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
56 }
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
57 function parseTextStyle(style,n)
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
58 {
625
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
59 var attr;
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
60 if (n) {
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
61 attr = n.attr('style');
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
62 } else {
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
63 attr = null;
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
64 }
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
65 if (attr == null) {
631
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
66 return;
625
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
67 }
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
68 var f = attr.value().split(';');
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
69
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
70 for(i in f) {
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
71 var kv = f[i].split(':');
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
72 if (kv[0] == 'font-size') {
631
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
73 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
74 } 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
75 } 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
76 } else if (kv[0] == "fill") {
631
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
77 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
78 } 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
79 } 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
80 } else if (kv[0] == "stroke") {
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
81 } 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
82 } 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
83 } 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
84 } 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
85 } else if (kv[0] == "font-family") {
631
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
86 style.family = kv[1];
625
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
87 } 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
88 } 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
89 } 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
90 } 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
91 } 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
92 } else if (kv[0] == "line-height") {
632
e517bbefe0e9 Add the missing 'else'
wycc
parents: 631
diff changeset
93 } else {
625
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
94 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
95 }
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
96 }
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
97 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
98
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
99 exports.parseTSpan=function(coord, n,style)
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
100 {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
101 var x = getInteger(n,'x');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
102 var y = getInteger(n,'y');
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
103 var tcoord = this.mb_rt.coord_new(coord);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
104 var nodes = n.childNodes();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
105 var k;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
106
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
107 sys.puts(n.text());
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
108 var obj = this.mb_rt.stext_new(n.text(),x,y);
631
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
109 parseTextStyle(style,n);
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
110 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
111 style.face=this.mb_rt.font_face_query(style.family, 2, 100);
631
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
112 obj.set_style([[20,style.face,style.fs]]);
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
113 style.paint.fill(obj);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
114 tcoord.add_shape(obj);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
115 for(k in nodes) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
116 var name = nodes[k].name();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
117 if (name == "tspan") {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
118 this.parseTSpan(tcoord,nodes[k]);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
119 } else {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
120 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
121 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
122 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
123
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
124 exports._prepare_paint_color=function(color, alpha) {
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
125 var paint;
713
e60ae262127b Recognize color name
Thinker K.F. Li <thinker@branda.to>
parents: 712
diff changeset
126 var c;
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
127
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
128 if (color[0]=='#') {
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
129 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
130 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
131 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
132 b = parseInt(color.substring(5,7),16)/255;
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
133 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
134 } else if(_std_colors[color]) {
e60ae262127b Recognize color name
Thinker K.F. Li <thinker@branda.to>
parents: 712
diff changeset
135 c = _std_colors[color];
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
136 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
137 } else {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
138 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
139 }
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
140 return paint;
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
141 }
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
142
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
143 exports.parsePath=function(coord,id, n)
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
144 {
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
145 var d = n.attr('d').value();
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
146 var style = n.attr('style');
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
147 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
148 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
149 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
150 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
151 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
152 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
153 var black_paint;
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
154
717
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
155 if(style != null) {
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
156 var items = style.value().split(';');
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
157 var alpha;
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
158
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
159 for(i in items) {
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
160 sys.puts(items[i]);
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
161 var f = items[i].split(':');
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
162 if (f[0] == 'opacity') {
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
163 alpha = f[1];
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
164 } 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
165 fill_color = f[1];
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
166 } else if (f[0] == 'fill-opacity') {
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
167 fill_alpha = parseFloat(f[1]);
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
168 } 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
169 stroke_color = f[1];
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
170 } else if (f[0] == 'stroke-width') {
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
171 path.stroke_width = parseFloat(f[1]);
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
172 } else if (f[0] == 'stroke-opacity') {
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
173 stroke_alpha = parseFloat(f[1]);
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
174 }
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
175 }
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
176
717
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
177 }
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
178
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
179 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
180 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
181
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
182 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
183 if(fill_color != "none") {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
184 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
185 paint.fill(path);
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
186 }
717
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
187 } else {
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
188 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
189 }
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
190 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
191 if(stroke_color != "none") {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
192 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
193 paint.stroke(path);
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
194 }
717
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
195 } else {
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
196 black_paint.stroke(path);
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
197 }
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
198 coord.add_shape(path);
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
199 }
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
200
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
201 exports.parseText=function(coord,id, n)
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
202 {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
203 var x = getInteger(n,'x');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
204 var y = getInteger(n,'y');
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
205 var tcoord = this.mb_rt.coord_new(coord);
631
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
206 var style = new Object();
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
207 style.fs = 20;
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
208 style.family = 'courier';
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
209 parseTextStyle(style,n);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
210 var nodes = n.childNodes();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
211 var k;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
212 for(k in nodes) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
213 var n = nodes[k].name();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
214 if (n == "tspan") {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
215 this.parseTSpan(tcoord,nodes[k],style);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
216 } else {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
217 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
218 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
219
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
220
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
221 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
222
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
223 function parseTransform(coord, s)
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
224 {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
225 var off = s.indexOf('translate');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
226 if (off != -1) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
227 var ss = s.substring(off+9);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
228 for(i=0;i<ss.length;i++) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
229 if (ss[i] == '(') break;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
230 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
231 ss = ss.substring(i+1);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
232 for(i=0;i<ss.length;i++) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
233 if (ss[i] == ')') {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
234 ss = ss.substring(0,i);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
235 break;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
236 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
237 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
238 var f = ss.split(',');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
239 var x,y;
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
240 x = parseFloat(f[0]);
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
241 y = parseFloat(f[1]);
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
242 coord[0] = 1;
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
243 coord[1] = 0;
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
244 coord[2] = x;
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
245 coord[3] = 0;
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
246 coord[4] = 1;
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
247 coord[5] = y;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
248 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
249 off = s.indexOf('matrix');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
250 if (off != -1) {
703
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
251 var end = s.indexOf(')');
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
252 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
253 var fields = m.split(',');
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
254 coord[0] = parseFloat(fields[0]);
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
255 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
256 coord[2] = parseFloat(fields[4]);
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
257 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
258 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
259 coord[5] = parseFloat(fields[5]);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
260 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
261 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
262
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
263 exports.parseRect=function(coord, id, n)
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
264 {
703
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
265 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
266 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
267 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
268 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
269 var paint;
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
270
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
271 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
272
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
273 if (style==null) {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
274 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
275 } else {
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
276 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
277 var fill = '';
708
ac9c20db953e Default alpha of a tag is '1'
Thinker K.F. Li <thinker@branda.to>
parents: 706
diff changeset
278 var alpha = 1;
703
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
279 for(i in items) {
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
280 sys.puts(items[i]);
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
281 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
282 if (f[0] == 'opacity') {
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
283 alpha = f[1];
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
284 } 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
285 fill = f[1];
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
286 } 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
287 } 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
288 } 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
289 } 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
290 }
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
291 }
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
292 sys.puts("fill="+fill);
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
293 if (fill[0]=='#') {
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
294 var r,g,b;
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
295 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
296 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
297 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
298 sys.puts("r="+r+" g="+g+" b="+b+" a="+alpha);
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
299
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
300 paint = this.mb_rt.paint_color_new(r,g,b,parseFloat(alpha));
703
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
301 } else {
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,1);
703
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
303 }
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
304 }
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
305 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
306 sys.puts("rect x="+x+" y="+y+" w="+w+" h="+h);
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
307 paint.fill(rect);
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
308 coord.add_shape(rect);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
309 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
310
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
311 exports.parseGroup=function(root, group_id, n)
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
312 {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
313 var k;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
314 var nodes = n.childNodes();
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
315 var coord = this.mb_rt.coord_new(root);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
316 // Parse the transform and style here
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
317 var trans = n.attr('transform');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
318 if (trans!=null) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
319 parseTransform(coord, trans.value());
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
320 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
321
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
322 for(k in nodes) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
323 var n = nodes[k].name();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
324 var attr = nodes[k].attr('id');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
325 var id;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
326 if (attr) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
327 id = attr.value();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
328 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
329 if (n == "g") {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
330 this.parseGroup(coord, id, nodes[k]);
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
331 } else if (n == "path") {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
332 this.parsePath(coord, id, nodes[k]);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
333 } else if (n == "text") {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
334 this.parseText(coord, id, nodes[k]);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
335 } else if (n == "rect") {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
336 this.parseRect(coord, id, nodes[k]);
646
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
337 } else if (n == "image") {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
338 this.parseImage(coord, id, nodes[k]);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
339 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
340 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
341
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
342 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
343
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
344 exports.parseImage=function(coord,id, n)
646
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
345 {
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
346 sys.puts("---> image");
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
347 var ref = n.attr('href').value();
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
348
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
349 if (ref == null) return;
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
350 sys.puts(ref);
647
492da72e6537 Add image tag support.
wycc
parents: 646
diff changeset
351 if (ref.substr(0,7) == "file://") {
492da72e6537 Add image tag support.
wycc
parents: 646
diff changeset
352 ref = ref.substring(7);
492da72e6537 Add image tag support.
wycc
parents: 646
diff changeset
353 } else if (ref.substr(0,5)=="file:") {
492da72e6537 Add image tag support.
wycc
parents: 646
diff changeset
354 ref = ref.substring(5);
492da72e6537 Add image tag support.
wycc
parents: 646
diff changeset
355 } else {
646
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
356 return;
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
357 }
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
358 sys.puts("Load image "+ref);
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
359 var w;
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
360 var h;
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
361 var x,y;
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
362
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
363 w = n.attr("width");
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
364 if (w == null) return;
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
365 w = parseInt(w.value());
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
366 h = n.attr("height");
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
367 if (h == null) return;
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
368 h = parseInt(h.value());
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
369 x = n.attr("x");
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
370 if (x == null) return;
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
371 x = parseInt(x.value());
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
372 y = n.attr("y");
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
373 if (y == null) return;
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
374 y = parseInt(y.value());
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
375 sys.puts("x="+x+",y="+y+",w="+w+",h="+h);
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
376 var img = this.mb_rt.image_new(x,y,w,h);
646
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
377 var img_data = ldr.load(ref);
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
378 sys.puts(img_data);
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
379 var paint = this.mb_rt.paint_image_new(img_data);
646
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
380 paint.fill(img);
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
381 coord.add_shape(img);
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
382 }
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
383
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
384 exports.parseDefs=function(root,n)
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
385 {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
386 var k;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
387 var nodes = n.childNodes();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
388
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
389 for(k in nodes) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
390 var name = nodes[k].name();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
391 if (name == "linearGradient") {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
392 //_MB_parseLinearGradient(root,nodes[k]);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
393 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
394 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
395 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
396
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
397