annotate nodejs/svg.js @ 776:77b561bb7929

Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object. However, the image does not work here since it does not use the transformation of the group.
author wycc
date Mon, 30 Aug 2010 08:56:44 +0800
parents ae1ae29348d1
children a47431293043
rev   line source
776
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
1 // vim: ts=4
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
2 var libxml = require('libxmljs');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
3 var sys=require('sys');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
4 var mbfly = require("mbfly");
646
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
5 var ldr = mbfly.img_ldr_new(".");
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
6
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
7
713
e60ae262127b Recognize color name
Thinker K.F. Li <thinker@branda.to>
parents: 712
diff changeset
8 var _std_colors = {
e60ae262127b Recognize color name
Thinker K.F. Li <thinker@branda.to>
parents: 712
diff changeset
9 "white": [1, 1, 1],
e60ae262127b Recognize color name
Thinker K.F. Li <thinker@branda.to>
parents: 712
diff changeset
10 "black": [0, 0, 0],
e60ae262127b Recognize color name
Thinker K.F. Li <thinker@branda.to>
parents: 712
diff changeset
11 "red": [1, 0, 0]
e60ae262127b Recognize color name
Thinker K.F. Li <thinker@branda.to>
parents: 712
diff changeset
12 };
e60ae262127b Recognize color name
Thinker K.F. Li <thinker@branda.to>
parents: 712
diff changeset
13
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
14 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
15 return new loadSVG(mb_rt, root, filename);
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
0cd59ce76e67 Refactor loadSVG as a class
Thinker K.F. Li <thinker@branda.to>
parents: 717
diff changeset
18 function loadSVG(mb_rt, root, filename) {
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
19 var doc = libxml.parseXmlFile(filename);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
20 var nodes = doc.root().childNodes();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
21 var coord = mb_rt.coord_new(root);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
22 var k;
759
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
23 var accu=[1,0,0,0,1,0];
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
24 this.mb_rt = mb_rt;
750
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
25 this.stop_ref={};
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
26 this.gradients={};
776
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
27 root.center=new Object();
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
28 root.center.x = 10000;
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
29 root.center.y = 10000;
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
30
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
31 for(k in nodes) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
32 var n = nodes[k].name();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
33 if (n == "defs") {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
34 this.parseDefs(root,nodes[k]);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
35 } else if (n == "g") {
759
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
36 this.parseGroup(accu,root,'root_coord',nodes[k]);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
37 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
38 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
39 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
40
719
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
41 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
42 var mbname;
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
43 var name;
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
44
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
45 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
46 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
47
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
48 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
49 if(mbname) {
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
50 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
51 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
52 }
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
53 }
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
54
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
55 function getInteger(n,name)
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
56 {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
57 if (n == null) return 0;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
58 var a = n.attr(name);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
59 if (a==null) return 0;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
60 return parseInt(a.value());
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
61 }
719
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
62
631
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
63 function parsePointSize(s)
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
64 {
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
65 var fs=0;
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
66 var i;
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
67
631
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
68 for(i=0;i<s.length;i++) {
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
69 if (s[i]<'0' || s[i] > '9') break;
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
70 fs = fs*10 + (s[i]-'0');
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
71 }
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
72 return fs;
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
73
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
74 }
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
75
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
76 function parseColor(c)
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
77 {
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
78 if (c[0] == '#') {
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
79 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
80 }
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
81 }
719
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
82
631
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
83 function parseTextStyle(style,n)
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
84 {
625
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
85 var attr;
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
86 if (n) {
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
87 attr = n.attr('style');
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
88 } else {
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
89 attr = null;
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 if (attr == null) {
631
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
92 return;
625
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
93 }
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
94 var f = attr.value().split(';');
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 for(i in f) {
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
97 var kv = f[i].split(':');
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
98 if (kv[0] == 'font-size') {
631
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
99 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
100 } 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
101 } 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
102 } else if (kv[0] == "fill") {
631
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
103 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
104 } 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
105 } 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
106 } else if (kv[0] == "stroke") {
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
107 } 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
108 } 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
109 } 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
110 } 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
111 } else if (kv[0] == "font-family") {
631
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
112 style.family = kv[1];
625
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
113 } 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
114 } 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
115 } 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
116 } 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
117 } 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
118 } else if (kv[0] == "line-height") {
632
e517bbefe0e9 Add the missing 'else'
wycc
parents: 631
diff changeset
119 } else {
625
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
120 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
121 }
9f2080b68f8e Add the text style parser. This can not handle the recursive tspan yet.
wycc
parents: 624
diff changeset
122 }
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
123 }
732
6879aa403306 Add set_text to the coordinate of the coord_t of the text.
wycc
parents: 720
diff changeset
124 function tspan_set_text(text)
6879aa403306 Add set_text to the coordinate of the coord_t of the text.
wycc
parents: 720
diff changeset
125 {
6879aa403306 Add set_text to the coordinate of the coord_t of the text.
wycc
parents: 720
diff changeset
126 this.text.set_text(text);
6879aa403306 Add set_text to the coordinate of the coord_t of the text.
wycc
parents: 720
diff changeset
127 }
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
128
718
0cd59ce76e67 Refactor loadSVG as a class
Thinker K.F. Li <thinker@branda.to>
parents: 717
diff changeset
129 loadSVG.prototype.parseTSpan=function(coord, n,style)
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
130 {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
131 var x = getInteger(n,'x');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
132 var y = getInteger(n,'y');
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
133 var tcoord = this.mb_rt.coord_new(coord);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
134 var nodes = n.childNodes();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
135 var k;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
136
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
137 var obj = this.mb_rt.stext_new(n.text(),x,y);
631
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
138 parseTextStyle(style,n);
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
139 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
140 style.face=this.mb_rt.font_face_query(style.family, 2, 100);
631
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
141 obj.set_style([[20,style.face,style.fs]]);
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
142 style.paint.fill(obj);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
143 tcoord.add_shape(obj);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
144 for(k in nodes) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
145 var name = nodes[k].name();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
146 if (name == "tspan") {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
147 this.parseTSpan(tcoord,nodes[k]);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
148 } else {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
149 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
150 }
732
6879aa403306 Add set_text to the coordinate of the coord_t of the text.
wycc
parents: 720
diff changeset
151 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
152 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
153 make_mbnames(this.mb_rt, n, tcoord);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
154 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
155
718
0cd59ce76e67 Refactor loadSVG as a class
Thinker K.F. Li <thinker@branda.to>
parents: 717
diff changeset
156 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
157 var paint;
713
e60ae262127b Recognize color name
Thinker K.F. Li <thinker@branda.to>
parents: 712
diff changeset
158 var c;
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
159
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
160 if (color[0]=='#') {
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
161 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
162 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
163 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
164 b = parseInt(color.substring(5,7),16)/255;
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
165 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
166 } else if(_std_colors[color]) {
e60ae262127b Recognize color name
Thinker K.F. Li <thinker@branda.to>
parents: 712
diff changeset
167 c = _std_colors[color];
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
168 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
169 } else {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
170 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
171 }
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
172 return paint;
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
173 }
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
174
759
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
175 function guessPathBoundingBox(coord,d)
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
176 {
776
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
177 return;
759
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
178 var items = d.split(' ');
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
179 var len = items.length;
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
180 var pair;
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
181 var i;
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
182 var minx,miny;
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
183
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
184 minx = 10000;
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
185 miny = 10000;
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
186
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
187 for(i=0;i<len;i++) {
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
188 var type = items[i].toLowerCase();
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
189 x = minx;y = miny;
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
190 switch(type) {
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
191 case 'm':
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
192 case 'l':
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
193 case 'a':
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
194 case 'x':
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
195 pair = items[i+1].split(',');
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
196 if (pair.length==2) {
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
197 x = parseFloat(pair[0]);
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
198 y = parseFloat(pair[1]);
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
199 i++;
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
200 } else {
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
201 x = parseFloat(items[i+1]);
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
202 y = parseFloat(items[i+2]);
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
203 i+=2;
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
204 }
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
205 break;
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
206 case 'q':
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
207 // Implement this latter
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
208 break;
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
209 case 'c':
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
210 // Implement this latter
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
211 break;
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
212 case 's':
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
213 // Implement this latter
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
214 break;
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
215 case 'h':
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
216 x = parseFloat(items[i+1]);
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
217 break;
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
218 case 'v':
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
219 y = parseFloat(items[i+1]);
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
220 break;
776
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
221 default:
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
222 continue;
759
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
223 }
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
224 if (x < minx) minx = x;
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
225 if (y < miny) miny = y;
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
226 }
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
227 if (coord.center.x > minx)
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
228 coord.center.x = minx;
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
229 if (coord.center.y > miny)
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
230 coord.center.y = miny;
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
231 }
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
232
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
233
718
0cd59ce76e67 Refactor loadSVG as a class
Thinker K.F. Li <thinker@branda.to>
parents: 717
diff changeset
234 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
235 {
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
236 var d = n.attr('d').value();
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
237 var style = n.attr('style');
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
238 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
239 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
240 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
241 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
242 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
243 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
244 var black_paint;
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
245
759
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
246 guessPathBoundingBox(coord,d);
717
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
247 if(style != null) {
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
248 var items = style.value().split(';');
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
249 var alpha;
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
250
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
251 for(i in items) {
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
252 var f = items[i].split(':');
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
253 if (f[0] == 'opacity') {
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
254 alpha = f[1];
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
255 } 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
256 fill_color = f[1];
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
257 } else if (f[0] == 'fill-opacity') {
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
258 fill_alpha = parseFloat(f[1]);
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
259 } 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
260 stroke_color = f[1];
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
261 } else if (f[0] == 'stroke-width') {
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
262 path.stroke_width = parseFloat(f[1]);
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
263 } else if (f[0] == 'stroke-opacity') {
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
264 stroke_alpha = parseFloat(f[1]);
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
265 }
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
266 }
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
267
717
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
268 }
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
269
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
270 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
271 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
272
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
273 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
274 if(fill_color != "none") {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
275 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
276 paint.fill(path);
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
277 }
717
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
278 } else {
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
279 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
280 }
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
281 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
282 if(stroke_color != "none") {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
283 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
284 paint.stroke(path);
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
285 }
717
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
286 } else {
b822b1912d67 Paint with black for unspecified, not "none", fill and stroke.
Thinker K.F. Li <thinker@branda.to>
parents: 714
diff changeset
287 black_paint.stroke(path);
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
288 }
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
289 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
290
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
291 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
292 }
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
293
776
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
294 loadSVG.prototype.parseText=function(accu,coord,id, n)
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
295 {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
296 var x = getInteger(n,'x');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
297 var y = getInteger(n,'y');
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
298 var tcoord = this.mb_rt.coord_new(coord);
631
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
299 var style = new Object();
759
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
300
776
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
301 if (n.attr('x')) {
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
302 var nx = coord[0]*x+coord[1]*y+coord[2];
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
303 if (coord.center.x > nx)
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
304 coord.center.x = nx;
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
305 }
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
306 if (n.attr('y')) {
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
307 var ny = coord[3]*x+coord[4]*y+coord[5];
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
308 if (coord.center.y > ny)
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
309 coord.center.y = ny;
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
310 }
631
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
311 style.fs = 20;
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
312 style.family = 'courier';
01e960bfc9ff Implement text style parser
wycc
parents: 625
diff changeset
313 parseTextStyle(style,n);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
314 var nodes = n.childNodes();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
315 var k;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
316 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
317 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
318 if (c == "tspan") {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
319 this.parseTSpan(tcoord,nodes[k],style);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
320 } else {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
321 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
322 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
323
719
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
324 make_mbnames(this.mb_rt, n, tcoord);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
325 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
326
759
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
327
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
328 function multiply(s,d) {
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
329 var m=[];
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
330 m[0] = s[0]*d[0]+s[1]*d[3];
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
331 m[1] = s[0]*d[1]+s[1]*d[4];
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
332 m[2] = s[0]*d[2]+s[1]*d[5]+s[2];
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
333 m[3] = s[3]*d[0]+s[4]*d[3];
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
334 m[4] = s[3]*d[1]+s[4]*d[4];
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
335 m[5] = s[3]*d[2]+s[4]*d[5]+s[5];
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
336 s[0] = m[0];
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
337 s[1] = m[1];
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
338 s[2] = m[2];
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
339 s[3] = m[3];
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
340 s[4] = m[4];
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
341 s[5] = m[5];
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
342 }
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
343
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
344 function parseTransform(coord, s)
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
345 {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
346 var off = s.indexOf('translate');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
347 if (off != -1) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
348 var ss = s.substring(off+9);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
349 for(i=0;i<ss.length;i++) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
350 if (ss[i] == '(') break;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
351 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
352 ss = ss.substring(i+1);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
353 for(i=0;i<ss.length;i++) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
354 if (ss[i] == ')') {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
355 ss = ss.substring(0,i);
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
356 break;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
357 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
358 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
359 var f = ss.split(',');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
360 var x,y;
706
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
361 x = parseFloat(f[0]);
fdd68e69c59f Parse path tag for SVG
Thinker K.F. Li <thinker@branda.to>
parents: 704
diff changeset
362 y = parseFloat(f[1]);
759
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
363 coord[2] += x;
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
364 coord[5] += y;
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
365 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
366 off = s.indexOf('matrix');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
367 if (off != -1) {
703
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
368 var end = s.indexOf(')');
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
369 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
370 var fields = m.split(',');
759
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
371 var newm=[];
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
372 newm[0] = parseFloat(fields[0]);
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
373 newm[1] = parseFloat(fields[2]);
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
374 newm[2] = parseFloat(fields[4]);
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
375 newm[3] = parseFloat(fields[1]);
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
376 newm[4] = parseFloat(fields[3]);
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
377 newm[5] = parseFloat(fields[5]);
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
378 multiply(coord,newm);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
379 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
380 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
381
759
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
382 loadSVG.prototype.parseRect=function(accu_matrix,coord, id, n)
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
383 {
703
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
384 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
385 var y = getInteger(n,'y');
759
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
386 var rx,ry;
703
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
387 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
388 var h = getInteger(n,'height');
759
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
389 var trans = n.attr('transform');
703
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
390 var paint;
776
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
391 var tcoord = this.mb_rt.coord_new(coord);
703
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
392
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
393 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
394
759
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
395 if (trans) {
776
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
396 parseTransform(tcoord,trans.value());
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
397 //var m = [1,0,0,0,1,0];
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
398 //multiply(m,tcoord);
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
399 rx = tcoord[0]*x+tcoord[1]*y+tcoord[2];
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
400 ry = tcoord[3]*x+tcoord[4]*y+tcoord[5];
759
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
401 }
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
402
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
403 if (coord.center.x > rx)
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
404 coord.center.x = rx;
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
405 if (coord.center.y > ry)
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
406 coord.center.y = ry;
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
407
703
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
408 if (style==null) {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
409 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
410 } else {
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
411 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
412 var fill = '';
708
ac9c20db953e Default alpha of a tag is '1'
Thinker K.F. Li <thinker@branda.to>
parents: 706
diff changeset
413 var alpha = 1;
776
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
414 display = 'on';
703
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
415 for(i in items) {
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
416 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
417 if (f[0] == 'opacity') {
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
418 alpha = f[1];
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
419 } 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
420 fill = f[1];
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
421 } 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
422 } 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
423 } 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
424 } else if (f[0] == 'stroke-opacity') {
776
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
425 } else if (f[0] == 'display') {
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
426 display = f[1];
703
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
427 }
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
428 }
776
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
429 if (display == 'none') {
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
430 return;
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
431 }
703
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
432 if (fill[0]=='#') {
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
433 var r,g,b;
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
434 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
435 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
436 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
437
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
438 paint = this.mb_rt.paint_color_new(r,g,b,parseFloat(alpha));
750
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
439 } else if (fill.substring(0,3) == 'url') {
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
440 var id = fill.substring(5,fill.length-1);
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
441 var gr = this.gradients[id];
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
442 paint = this.mb_rt.paint_linear_new(gr[0],gr[1],gr[2],gr[3]);
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
443 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
444 } else {
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
445 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
446 }
3457519e3b9c Add rect and matrix support. The test.svg can be rendered almost correctly now.
wycc
parents: 647
diff changeset
447 }
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
448 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
449 paint.fill(rect);
776
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
450 tcoord.add_shape(rect);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
451 }
776
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
452 // When we parse a group, we need to calculate the origin of the group so that we can resize the group without changing its origin point.
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
453 // This must be done recursively. For text/rect/image, we can get its origin point directly by using the (x,y) and apply their transformation
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
454 // matrix. For group, we need to send the acculumated matrix so that each group can get their origin correctly.
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
455 //
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
456 // Each element must be responsible to calculate its absolute origin point and update the origin of its parent.
759
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
457 loadSVG.prototype.parseGroup=function(accu_matrix,root, group_id, n)
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
458 {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
459 var k;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
460 var nodes = n.childNodes();
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
461 var coord = this.mb_rt.coord_new(root);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
462 // Parse the transform and style here
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
463 var trans = n.attr('transform');
759
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
464 var accu=[1,0,0,0,1,0];
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
465 coord.center= new Object();
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
466 coord.center.x = 10000;
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
467 coord.center.y = 10000;
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
468 if (trans!=null) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
469 parseTransform(coord, trans.value());
759
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
470 }
776
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
471 multiply(accu,accu_matrix);
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
472 multiply(accu,coord);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
473
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
474 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
475 var c = nodes[k].name();
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
476 var attr = nodes[k].attr('id');
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
477 var id;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
478 if (attr) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
479 id = attr.value();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
480 }
719
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
481 if (c == "g") {
759
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
482 this.parseGroup(accu,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
483 } else if (c == "path") {
776
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
484 this.parsePath(accu,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
485 } else if (c == "text") {
776
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
486 this.parseText(accu,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
487 } else if (c == "rect") {
759
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
488 this.parseRect(accu_matrix,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
489 } else if (c == "image") {
776
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
490 this.parseImage(accu_matrix,coord, id, nodes[k]);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
491 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
492 }
776
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
493 if (root.center.x > coord.center.x)
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
494 root.center.x = coord.center.x;
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
495 if (root.center.y > coord.center.y)
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
496 root.center.y = coord.center.y;
719
1b6856fda760 Collect mbnames and mapping to object through mb_rt.mbnames
Thinker K.F. Li <thinker@branda.to>
parents: 718
diff changeset
497 make_mbnames(this.mb_rt, n, coord);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
498 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
499
776
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
500 loadSVG.prototype.parseImage=function(accu,coord,id, n)
646
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
501 {
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
502 var ref = n.attr('href').value();
776
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
503 var tcoord = this.mb_rt.coord_new(coord);
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
504 var trans = n.attr('transform');
646
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
505
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
506 if (ref == null) return;
647
492da72e6537 Add image tag support.
wycc
parents: 646
diff changeset
507 if (ref.substr(0,7) == "file://") {
492da72e6537 Add image tag support.
wycc
parents: 646
diff changeset
508 ref = ref.substring(7);
492da72e6537 Add image tag support.
wycc
parents: 646
diff changeset
509 } else if (ref.substr(0,5)=="file:") {
492da72e6537 Add image tag support.
wycc
parents: 646
diff changeset
510 ref = ref.substring(5);
492da72e6537 Add image tag support.
wycc
parents: 646
diff changeset
511 } else {
646
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
512 return;
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
513 }
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
514 var w;
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
515 var h;
776
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
516 var x,y,nx,ny;
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
517 coord.center= new Object();
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
518 coord.center.x = 10000;
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
519 coord.center.y = 10000;
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
520 if (trans!=null) {
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
521 parseTransform(coord, trans.value());
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
522 }
646
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
523
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
524 w = n.attr("width");
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
525 if (w == null) return;
759
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
526 w = parseFloat(w.value());
646
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
527 h = n.attr("height");
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
528 if (h == null) return;
759
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
529 h = parseFloat(h.value());
646
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
530 x = n.attr("x");
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
531 if (x == null) return;
759
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
532 x = parseFloat(x.value());
646
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
533 y = n.attr("y");
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
534 if (y == null) return;
759
ae1ae29348d1 Add origin calculation support
wycc
parents: 750
diff changeset
535 y = parseFloat(y.value());
776
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
536 nx = tcoord[0]*x+tcoord[1]*y+tcoord[2];
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
537 ny = tcoord[3]*x+tcoord[4]*y+tcoord[5];
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
538 if (coord.center.x > nx)
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
539 coord.center.x = nx;
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
540 if (coord.center.y > ny)
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
541 coord.center.y = ny;
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
542 var img = this.mb_rt.image_new(x,y,w,h);
646
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
543 var img_data = ldr.load(ref);
714
f53e45d1fcd0 Translate the svg.js as a nodejs module.
wycc
parents: 713
diff changeset
544 var paint = this.mb_rt.paint_image_new(img_data);
646
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
545 paint.fill(img);
776
77b561bb7929 Implement new algorithm to calculate the origin of the SVG elemnts so that we can implement object resize without changing the position of the object.
wycc
parents: 759
diff changeset
546 tcoord.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
547 make_mbnames(this.mb_rt, n, img);
646
3a1e80de44ff Add image support
wycc
parents: 632
diff changeset
548 }
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
549
750
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
550 loadSVG.prototype._MB_parseLinearGradient=function(root,n)
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
551 {
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
552 var id = n.attr('id');
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
553 var k;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
554 var nodes = n.childNodes();
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
555
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
556 if (id == null) return;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
557 var x1 = n.attr("x1");
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
558 var y1 = n.attr("y1");
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
559 var x2 = n.attr("x2");
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
560 var y2 = n.attr("y2");
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
561 var gr;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
562 var color, opacity;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
563 var stops;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
564 var r,g,b;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
565 stops=[];
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
566 for(k in nodes) {
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
567 var ss = nodes[k];
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
568 if (ss.name()=="stop") {
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
569 var style = ss.attr("style").value();
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
570 var items = style.split(';');
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
571 var off = parseInt(ss.attr('offset').value());
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
572 color = 'black';
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
573 opacity = 1;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
574 for (i in items) {
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
575 it = items[i];
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
576 var f = it.split(':');
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
577 k = f[0];
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
578 v = f[1];
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
579 if (k == 'stop-color') {
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
580 color = v.substring(1);
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
581 if (v == 'white') {
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
582 r = 1;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
583 g = 1;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
584 b = 1;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
585 } else if (v == 'black') {
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
586 r = 0;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
587 g = 0;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
588 b = 0;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
589 } else {
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
590 r = parseInt(color.substring(0,2),16)/255.0;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
591 g = parseInt(color.substring(2,4),16)/255.0;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
592 b = parseInt(color.substring(4,6),16)/255.0;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
593 }
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
594 } else if (k=='stop-opacity') {
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
595 opacity = parseFloat(v);
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
596 }
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
597 }
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
598 stops.push([off, r,g,b,opacity]);
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
599 }
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
600 }
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
601 var href = n.attr('href');
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
602 if (href != null) {
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
603 href = href.value();
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
604 pstops = this.stop_ref[href.substring(1)];
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
605 stops = pstops.concat(stops);
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
606 }
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
607 id = id.value();
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
608 this.stop_ref[id] = stops;
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
609 if (x1)
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
610 x1 = parseFloat(x1.value());
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
611 if (x2)
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
612 x2 = parseFloat(x2.value());
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
613 if (y1)
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
614 y1 = parseFloat(y1.value());
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
615 if (y2)
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
616 y2 = parseFloat(y2.value());
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
617 this.gradients[id] = [x1,y1,x2,y2];
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
618 }
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
619
718
0cd59ce76e67 Refactor loadSVG as a class
Thinker K.F. Li <thinker@branda.to>
parents: 717
diff changeset
620 loadSVG.prototype.parseDefs=function(root,n)
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
621 {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
622 var k;
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
623 var nodes = n.childNodes();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
625 for(k in nodes) {
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
626 var name = nodes[k].name();
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
627 if (name == "linearGradient") {
750
199bac90b90a Add linearGradient support.
wycc
parents: 732
diff changeset
628 this._MB_parseLinearGradient(root,nodes[k]);
624
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
629 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
630 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
631 }
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
632
d45c928f6523 Add SVG parser sample code.
wycc
parents:
diff changeset
633