Mercurial > MadButterfly
comparison nodejs/svg.js @ 783:a47431293043
Re-indent ts=8 to make editor and cat seeing the same thing
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Mon, 30 Aug 2010 23:52:59 +0800 |
parents | 77b561bb7929 |
children | 37a1bd3e3ce1 |
comparison
equal
deleted
inserted
replaced
781:9367508842b2 | 783:a47431293043 |
---|---|
1 // vim: ts=4 | 1 // -*- indent-tabs-mode: t; tab-width: 4; c-basic-offset: 4; -*- |
2 // vim: sw=4:ts=8:sts=4:ai | |
2 var libxml = require('libxmljs'); | 3 var libxml = require('libxmljs'); |
3 var sys=require('sys'); | 4 var sys=require('sys'); |
4 var mbfly = require("mbfly"); | 5 var mbfly = require("mbfly"); |
5 var ldr = mbfly.img_ldr_new("."); | 6 var ldr = mbfly.img_ldr_new("."); |
6 | 7 |
17 | 18 |
18 function loadSVG(mb_rt, root, filename) { | 19 function loadSVG(mb_rt, root, filename) { |
19 var doc = libxml.parseXmlFile(filename); | 20 var doc = libxml.parseXmlFile(filename); |
20 var nodes = doc.root().childNodes(); | 21 var nodes = doc.root().childNodes(); |
21 var coord = mb_rt.coord_new(root); | 22 var coord = mb_rt.coord_new(root); |
22 var k; | 23 var k; |
23 var accu=[1,0,0,0,1,0]; | 24 var accu=[1,0,0,0,1,0]; |
24 this.mb_rt = mb_rt; | 25 this.mb_rt = mb_rt; |
25 this.stop_ref={}; | 26 this.stop_ref={}; |
26 this.gradients={}; | 27 this.gradients={}; |
27 root.center=new Object(); | 28 root.center=new Object(); |
28 root.center.x = 10000; | 29 root.center.x = 10000; |
29 root.center.y = 10000; | 30 root.center.y = 10000; |
30 | 31 |
31 for(k in nodes) { | 32 for(k in nodes) { |
32 var n = nodes[k].name(); | 33 var n = nodes[k].name(); |
33 if (n == "defs") { | 34 if (n == "defs") { |
34 this.parseDefs(root,nodes[k]); | 35 this.parseDefs(root,nodes[k]); |
35 } else if (n == "g") { | 36 } else if (n == "g") { |
36 this.parseGroup(accu,root,'root_coord',nodes[k]); | 37 this.parseGroup(accu,root,'root_coord',nodes[k]); |
37 } | 38 } |
38 } | 39 } |
39 } | 40 } |
40 | 41 |
41 function make_mbnames(mb_rt, n, obj) { | 42 function make_mbnames(mb_rt, n, obj) { |
42 var mbname; | 43 var mbname; |
54 | 55 |
55 function getInteger(n,name) | 56 function getInteger(n,name) |
56 { | 57 { |
57 if (n == null) return 0; | 58 if (n == null) return 0; |
58 var a = n.attr(name); | 59 var a = n.attr(name); |
59 if (a==null) return 0; | 60 if (a==null) return 0; |
60 return parseInt(a.value()); | 61 return parseInt(a.value()); |
61 } | 62 } |
62 | 63 |
63 function parsePointSize(s) | 64 function parsePointSize(s) |
64 { | 65 { |
65 var fs=0; | 66 var fs=0; |
66 var i; | 67 var i; |
67 | 68 |
68 for(i=0;i<s.length;i++) { | 69 for(i=0;i<s.length;i++) { |
69 if (s[i]<'0' || s[i] > '9') break; | 70 if (s[i]<'0' || s[i] > '9') break; |
70 fs = fs*10 + (s[i]-'0'); | 71 fs = fs*10 + (s[i]-'0'); |
71 } | 72 } |
72 return fs; | 73 return fs; |
73 | 74 |
74 } | 75 } |
75 | 76 |
76 function parseColor(c) | 77 function parseColor(c) |
77 { | 78 { |
78 if (c[0] == '#') { | 79 if (c[0] == '#') { |
79 return parseInt(c.substring(1,3),16)<<16 | parseInt(c.substring(3,5),16)<<8 | parseInt(c.substring(5,7),16); | 80 return parseInt(c.substring(1,3),16)<<16 | parseInt(c.substring(3,5),16)<<8 | parseInt(c.substring(5,7),16); |
80 } | 81 } |
81 } | 82 } |
82 | 83 |
83 function parseTextStyle(style,n) | 84 function parseTextStyle(style,n) |
84 { | 85 { |
85 var attr; | 86 var attr; |
86 if (n) { | 87 if (n) { |
87 attr = n.attr('style'); | 88 attr = n.attr('style'); |
88 } else { | 89 } else { |
89 attr = null; | 90 attr = null; |
90 } | 91 } |
91 if (attr == null) { | 92 if (attr == null) { |
92 return; | 93 return; |
93 } | 94 } |
94 var f = attr.value().split(';'); | 95 var f = attr.value().split(';'); |
95 | 96 |
96 for(i in f) { | 97 for(i in f) { |
97 var kv = f[i].split(':'); | 98 var kv = f[i].split(':'); |
98 if (kv[0] == 'font-size') { | 99 if (kv[0] == 'font-size') { |
99 style.fs = parsePointSize(kv[1]); | 100 style.fs = parsePointSize(kv[1]); |
100 } else if (kv[0] == "font-style") { | 101 } else if (kv[0] == "font-style") { |
101 } else if (kv[0] == "font-weight") { | 102 } else if (kv[0] == "font-weight") { |
102 } else if (kv[0] == "fill") { | 103 } else if (kv[0] == "fill") { |
103 style.color = parseColor(kv[1]); | 104 style.color = parseColor(kv[1]); |
104 } else if (kv[0] == "fill-opacity") { | 105 } else if (kv[0] == "fill-opacity") { |
105 } else if (kv[0] == "stroke-opacity") { | 106 } else if (kv[0] == "stroke-opacity") { |
106 } else if (kv[0] == "stroke") { | 107 } else if (kv[0] == "stroke") { |
107 } else if (kv[0] == "stroke-width") { | 108 } else if (kv[0] == "stroke-width") { |
108 } else if (kv[0] == "stroke-linecap") { | 109 } else if (kv[0] == "stroke-linecap") { |
109 } else if (kv[0] == "stroke-linejoin") { | 110 } else if (kv[0] == "stroke-linejoin") { |
110 } else if (kv[0] == "stroke-lineopacity") { | 111 } else if (kv[0] == "stroke-lineopacity") { |
111 } else if (kv[0] == "font-family") { | 112 } else if (kv[0] == "font-family") { |
112 style.family = kv[1]; | 113 style.family = kv[1]; |
113 } else if (kv[0] == "font-stretch") { | 114 } else if (kv[0] == "font-stretch") { |
114 } else if (kv[0] == "font-variant") { | 115 } else if (kv[0] == "font-variant") { |
115 } else if (kv[0] == "text-anchor") { | 116 } else if (kv[0] == "text-anchor") { |
116 } else if (kv[0] == "text-align") { | 117 } else if (kv[0] == "text-align") { |
117 } else if (kv[0] == "writing-mode") { | 118 } else if (kv[0] == "writing-mode") { |
118 } else if (kv[0] == "line-height") { | 119 } else if (kv[0] == "line-height") { |
119 } else { | 120 } else { |
120 sys.puts("Unknown style: "+kv[0]); | 121 sys.puts("Unknown style: "+kv[0]); |
121 } | 122 } |
122 } | 123 } |
123 } | 124 } |
124 function tspan_set_text(text) | 125 function tspan_set_text(text) |
125 { | 126 { |
126 this.text.set_text(text); | 127 this.text.set_text(text); |
127 } | 128 } |
128 | 129 |
129 loadSVG.prototype.parseTSpan=function(coord, n,style) | 130 loadSVG.prototype.parseTSpan = function(coord, n,style) { |
130 { | |
131 var x = getInteger(n,'x'); | 131 var x = getInteger(n,'x'); |
132 var y = getInteger(n,'y'); | 132 var y = getInteger(n,'y'); |
133 var tcoord = this.mb_rt.coord_new(coord); | 133 var tcoord = this.mb_rt.coord_new(coord); |
134 var nodes = n.childNodes(); | 134 var nodes = n.childNodes(); |
135 var k; | 135 var k; |
136 | 136 |
137 var obj = this.mb_rt.stext_new(n.text(),x,y); | 137 var obj = this.mb_rt.stext_new(n.text(),x,y); |
138 parseTextStyle(style,n); | 138 parseTextStyle(style,n); |
139 style.paint = this.mb_rt.paint_color_new(1,1,1,1); | 139 style.paint = this.mb_rt.paint_color_new(1,1,1,1); |
140 style.face=this.mb_rt.font_face_query(style.family, 2, 100); | 140 style.face=this.mb_rt.font_face_query(style.family, 2, 100); |
141 obj.set_style([[20,style.face,style.fs]]); | 141 obj.set_style([[20,style.face,style.fs]]); |
142 style.paint.fill(obj); | 142 style.paint.fill(obj); |
143 tcoord.add_shape(obj); | 143 tcoord.add_shape(obj); |
144 for(k in nodes) { | 144 for(k in nodes) { |
145 var name = nodes[k].name(); | 145 var name = nodes[k].name(); |
146 if (name == "tspan") { | 146 if (name == "tspan") { |
147 this.parseTSpan(tcoord,nodes[k]); | 147 this.parseTSpan(tcoord,nodes[k]); |
148 } else { | 148 } else { |
149 } | 149 } |
150 } | 150 } |
151 tcoord.set_text=tspan_set_text; | 151 tcoord.set_text=tspan_set_text; |
152 tcoord.text = obj; | 152 tcoord.text = obj; |
153 make_mbnames(this.mb_rt, n, tcoord); | 153 make_mbnames(this.mb_rt, n, tcoord); |
154 } | 154 }; |
155 | 155 |
156 loadSVG.prototype._prepare_paint_color=function(color, alpha) { | 156 loadSVG.prototype._prepare_paint_color = function(color, alpha) { |
157 var paint; | 157 var paint; |
158 var c; | 158 var c; |
159 | 159 |
160 if (color[0]=='#') { | 160 if (color[0]=='#') { |
161 var r,g,b; | 161 var r,g,b; |
169 } else { | 169 } else { |
170 paint = this.mb_rt.paint_color_new(0,0,0,1); | 170 paint = this.mb_rt.paint_color_new(0,0,0,1); |
171 } | 171 } |
172 return paint; | 172 return paint; |
173 } | 173 } |
174 | 174 |
175 function guessPathBoundingBox(coord,d) | 175 function guessPathBoundingBox(coord,d) |
176 { | 176 { |
177 return; | 177 return; |
178 var items = d.split(' '); | 178 var items = d.split(' '); |
179 var len = items.length; | 179 var len = items.length; |
180 var pair; | 180 var pair; |
181 var i; | 181 var i; |
182 var minx,miny; | 182 var minx,miny; |
183 | 183 |
184 minx = 10000; | 184 minx = 10000; |
185 miny = 10000; | 185 miny = 10000; |
186 | 186 |
187 for(i=0;i<len;i++) { | 187 for(i=0;i<len;i++) { |
188 var type = items[i].toLowerCase(); | 188 var type = items[i].toLowerCase(); |
189 x = minx;y = miny; | 189 x = minx;y = miny; |
190 switch(type) { | 190 switch(type) { |
191 case 'm': | 191 case 'm': |
192 case 'l': | 192 case 'l': |
193 case 'a': | 193 case 'a': |
194 case 'x': | 194 case 'x': |
195 pair = items[i+1].split(','); | 195 pair = items[i+1].split(','); |
196 if (pair.length==2) { | 196 if (pair.length==2) { |
197 x = parseFloat(pair[0]); | 197 x = parseFloat(pair[0]); |
198 y = parseFloat(pair[1]); | 198 y = parseFloat(pair[1]); |
199 i++; | 199 i++; |
200 } else { | 200 } else { |
201 x = parseFloat(items[i+1]); | 201 x = parseFloat(items[i+1]); |
202 y = parseFloat(items[i+2]); | 202 y = parseFloat(items[i+2]); |
203 i+=2; | 203 i+=2; |
204 } | |
205 break; | |
206 case 'q': | |
207 // Implement this latter | |
208 break; | |
209 case 'c': | |
210 // Implement this latter | |
211 break; | |
212 case 's': | |
213 // Implement this latter | |
214 break; | |
215 case 'h': | |
216 x = parseFloat(items[i+1]); | |
217 break; | |
218 case 'v': | |
219 y = parseFloat(items[i+1]); | |
220 break; | |
221 default: | |
222 continue; | |
223 } | 204 } |
224 if (x < minx) minx = x; | 205 break; |
225 if (y < miny) miny = y; | 206 case 'q': |
207 // Implement this latter | |
208 break; | |
209 case 'c': | |
210 // Implement this latter | |
211 break; | |
212 case 's': | |
213 // Implement this latter | |
214 break; | |
215 case 'h': | |
216 x = parseFloat(items[i+1]); | |
217 break; | |
218 case 'v': | |
219 y = parseFloat(items[i+1]); | |
220 break; | |
221 default: | |
222 continue; | |
223 } | |
224 if (x < minx) minx = x; | |
225 if (y < miny) miny = y; | |
226 } | 226 } |
227 if (coord.center.x > minx) | 227 if (coord.center.x > minx) |
228 coord.center.x = minx; | 228 coord.center.x = minx; |
229 if (coord.center.y > miny) | 229 if (coord.center.y > miny) |
230 coord.center.y = miny; | 230 coord.center.y = miny; |
231 } | 231 }; |
232 | 232 |
233 | 233 |
234 loadSVG.prototype.parsePath=function(coord,id, n) | 234 loadSVG.prototype.parsePath=function(coord,id, n) |
235 { | 235 { |
236 var d = n.attr('d').value(); | 236 var d = n.attr('d').value(); |
287 black_paint.stroke(path); | 287 black_paint.stroke(path); |
288 } | 288 } |
289 coord.add_shape(path); | 289 coord.add_shape(path); |
290 | 290 |
291 make_mbnames(this.mb_rt, n, path); | 291 make_mbnames(this.mb_rt, n, path); |
292 } | 292 }; |
293 | 293 |
294 loadSVG.prototype.parseText=function(accu,coord,id, n) | 294 loadSVG.prototype.parseText=function(accu,coord,id, n) |
295 { | 295 { |
296 var x = getInteger(n,'x'); | 296 var x = getInteger(n,'x'); |
297 var y = getInteger(n,'y'); | 297 var y = getInteger(n,'y'); |
298 var tcoord = this.mb_rt.coord_new(coord); | 298 var tcoord = this.mb_rt.coord_new(coord); |
299 var style = new Object(); | 299 var style = new Object(); |
300 | 300 |
301 if (n.attr('x')) { | 301 if (n.attr('x')) { |
302 var nx = coord[0]*x+coord[1]*y+coord[2]; | 302 var nx = coord[0]*x+coord[1]*y+coord[2]; |
303 if (coord.center.x > nx) | 303 if (coord.center.x > nx) |
304 coord.center.x = nx; | 304 coord.center.x = nx; |
305 } | 305 } |
306 if (n.attr('y')) { | 306 if (n.attr('y')) { |
307 var ny = coord[3]*x+coord[4]*y+coord[5]; | 307 var ny = coord[3]*x+coord[4]*y+coord[5]; |
308 if (coord.center.y > ny) | 308 if (coord.center.y > ny) |
309 coord.center.y = ny; | 309 coord.center.y = ny; |
310 } | 310 } |
311 style.fs = 20; | 311 style.fs = 20; |
312 style.family = 'courier'; | 312 style.family = 'courier'; |
313 parseTextStyle(style,n); | 313 parseTextStyle(style,n); |
314 var nodes = n.childNodes(); | 314 var nodes = n.childNodes(); |
315 var k; | 315 var k; |
316 for(k in nodes) { | 316 for(k in nodes) { |
317 var c= nodes[k].name(); | 317 var c= nodes[k].name(); |
318 if (c == "tspan") { | 318 if (c == "tspan") { |
319 this.parseTSpan(tcoord,nodes[k],style); | 319 this.parseTSpan(tcoord,nodes[k],style); |
320 } else { | 320 } else { |
321 } | 321 } |
322 } | 322 } |
323 | 323 |
324 make_mbnames(this.mb_rt, n, tcoord); | 324 make_mbnames(this.mb_rt, n, tcoord); |
325 } | 325 }; |
326 | 326 |
327 | 327 |
328 function multiply(s,d) { | 328 function multiply(s,d) { |
329 var m=[]; | 329 var m=[]; |
330 m[0] = s[0]*d[0]+s[1]*d[3]; | 330 m[0] = s[0]*d[0]+s[1]*d[3]; |
331 m[1] = s[0]*d[1]+s[1]*d[4]; | 331 m[1] = s[0]*d[1]+s[1]*d[4]; |
332 m[2] = s[0]*d[2]+s[1]*d[5]+s[2]; | 332 m[2] = s[0]*d[2]+s[1]*d[5]+s[2]; |
333 m[3] = s[3]*d[0]+s[4]*d[3]; | 333 m[3] = s[3]*d[0]+s[4]*d[3]; |
334 m[4] = s[3]*d[1]+s[4]*d[4]; | 334 m[4] = s[3]*d[1]+s[4]*d[4]; |
335 m[5] = s[3]*d[2]+s[4]*d[5]+s[5]; | 335 m[5] = s[3]*d[2]+s[4]*d[5]+s[5]; |
336 s[0] = m[0]; | 336 s[0] = m[0]; |
337 s[1] = m[1]; | 337 s[1] = m[1]; |
338 s[2] = m[2]; | 338 s[2] = m[2]; |
339 s[3] = m[3]; | 339 s[3] = m[3]; |
340 s[4] = m[4]; | 340 s[4] = m[4]; |
341 s[5] = m[5]; | 341 s[5] = m[5]; |
342 } | 342 }; |
343 | 343 |
344 function parseTransform(coord, s) | 344 function parseTransform(coord, s) |
345 { | 345 { |
346 var off = s.indexOf('translate'); | 346 var off = s.indexOf('translate'); |
347 if (off != -1) { | 347 if (off != -1) { |
348 var ss = s.substring(off+9); | 348 var ss = s.substring(off+9); |
349 for(i=0;i<ss.length;i++) { | 349 for(i=0;i<ss.length;i++) { |
350 if (ss[i] == '(') break; | 350 if (ss[i] == '(') break; |
351 } | 351 } |
352 ss = ss.substring(i+1); | 352 ss = ss.substring(i+1); |
353 for(i=0;i<ss.length;i++) { | 353 for(i=0;i<ss.length;i++) { |
354 if (ss[i] == ')') { | 354 if (ss[i] == ')') { |
355 ss = ss.substring(0,i); | 355 ss = ss.substring(0,i); |
356 break; | 356 break; |
357 } | 357 } |
358 } | 358 } |
359 var f = ss.split(','); | 359 var f = ss.split(','); |
360 var x,y; | 360 var x,y; |
361 x = parseFloat(f[0]); | 361 x = parseFloat(f[0]); |
362 y = parseFloat(f[1]); | 362 y = parseFloat(f[1]); |
363 coord[2] += x; | 363 coord[2] += x; |
364 coord[5] += y; | 364 coord[5] += y; |
365 } | 365 } |
366 off = s.indexOf('matrix'); | 366 off = s.indexOf('matrix'); |
367 if (off != -1) { | 367 if (off != -1) { |
368 var end = s.indexOf(')'); | 368 var end = s.indexOf(')'); |
369 var m = s.substring(7,end); | 369 var m = s.substring(7,end); |
370 var fields = m.split(','); | 370 var fields = m.split(','); |
371 var newm=[]; | 371 var newm=[]; |
372 newm[0] = parseFloat(fields[0]); | 372 newm[0] = parseFloat(fields[0]); |
373 newm[1] = parseFloat(fields[2]); | 373 newm[1] = parseFloat(fields[2]); |
374 newm[2] = parseFloat(fields[4]); | 374 newm[2] = parseFloat(fields[4]); |
375 newm[3] = parseFloat(fields[1]); | 375 newm[3] = parseFloat(fields[1]); |
376 newm[4] = parseFloat(fields[3]); | 376 newm[4] = parseFloat(fields[3]); |
377 newm[5] = parseFloat(fields[5]); | 377 newm[5] = parseFloat(fields[5]); |
378 multiply(coord,newm); | 378 multiply(coord,newm); |
379 } | 379 } |
380 } | 380 } |
381 | 381 |
382 loadSVG.prototype.parseRect=function(accu_matrix,coord, id, n) | 382 loadSVG.prototype.parseRect=function(accu_matrix,coord, id, n) |
383 { | 383 { |
384 var x = getInteger(n,'x'); | 384 var x = getInteger(n,'x'); |
385 var y = getInteger(n,'y'); | 385 var y = getInteger(n,'y'); |
386 var rx,ry; | 386 var rx,ry; |
387 var w = getInteger(n,'width'); | 387 var w = getInteger(n,'width'); |
388 var h = getInteger(n,'height'); | 388 var h = getInteger(n,'height'); |
389 var trans = n.attr('transform'); | 389 var trans = n.attr('transform'); |
390 var paint; | 390 var paint; |
391 var tcoord = this.mb_rt.coord_new(coord); | 391 var tcoord = this.mb_rt.coord_new(coord); |
392 | 392 |
393 var style = n.attr('style'); | 393 var style = n.attr('style'); |
394 | 394 |
395 if (trans) { | 395 if (trans) { |
396 parseTransform(tcoord,trans.value()); | 396 parseTransform(tcoord,trans.value()); |
397 //var m = [1,0,0,0,1,0]; | 397 //var m = [1,0,0,0,1,0]; |
398 //multiply(m,tcoord); | 398 //multiply(m,tcoord); |
399 rx = tcoord[0]*x+tcoord[1]*y+tcoord[2]; | 399 rx = tcoord[0]*x+tcoord[1]*y+tcoord[2]; |
400 ry = tcoord[3]*x+tcoord[4]*y+tcoord[5]; | 400 ry = tcoord[3]*x+tcoord[4]*y+tcoord[5]; |
401 } | 401 } |
402 | 402 |
403 if (coord.center.x > rx) | 403 if (coord.center.x > rx) |
404 coord.center.x = rx; | 404 coord.center.x = rx; |
405 if (coord.center.y > ry) | 405 if (coord.center.y > ry) |
406 coord.center.y = ry; | 406 coord.center.y = ry; |
407 | 407 |
408 if (style==null) { | 408 if (style==null) { |
409 paint = this.mb_rt.paint_color_new(0,0,0,0.1); | 409 paint = this.mb_rt.paint_color_new(0,0,0,0.1); |
410 } else { | 410 } else { |
411 var items = style.value().split(';'); | 411 var items = style.value().split(';'); |
412 var fill = ''; | 412 var fill = ''; |
413 var alpha = 1; | 413 var alpha = 1; |
414 display = 'on'; | 414 display = 'on'; |
415 for(i in items) { | 415 for(i in items) { |
416 var f = items[i].split(':'); | 416 var f = items[i].split(':'); |
417 if (f[0] == 'opacity') { | 417 if (f[0] == 'opacity') { |
418 alpha = f[1]; | 418 alpha = f[1]; |
419 } else if (f[0] == 'fill') { | 419 } else if (f[0] == 'fill') { |
420 fill = f[1]; | 420 fill = f[1]; |
421 } else if (f[0] == 'fill-opacity') { | 421 } else if (f[0] == 'fill-opacity') { |
422 } else if (f[0] == 'stroke') { | 422 } else if (f[0] == 'stroke') { |
423 } else if (f[0] == 'stroken-width') { | 423 } else if (f[0] == 'stroken-width') { |
424 } else if (f[0] == 'stroke-opacity') { | 424 } else if (f[0] == 'stroke-opacity') { |
425 } else if (f[0] == 'display') { | 425 } else if (f[0] == 'display') { |
426 display = f[1]; | 426 display = f[1]; |
427 } | 427 } |
428 } | 428 } |
429 if (display == 'none') { | 429 if (display == 'none') { |
430 return; | 430 return; |
431 } | 431 } |
432 if (fill[0]=='#') { | 432 if (fill[0]=='#') { |
433 var r,g,b; | 433 var r,g,b; |
434 r = parseInt(fill.substring(1,3),16)/256; | 434 r = parseInt(fill.substring(1,3),16)/256; |
435 g = parseInt(fill.substring(3,5),16)/256; | 435 g = parseInt(fill.substring(3,5),16)/256; |
436 b = parseInt(fill.substring(5,7),16)/256; | 436 b = parseInt(fill.substring(5,7),16)/256; |
437 | 437 |
438 paint = this.mb_rt.paint_color_new(r,g,b,parseFloat(alpha)); | 438 paint = this.mb_rt.paint_color_new(r,g,b,parseFloat(alpha)); |
439 } else if (fill.substring(0,3) == 'url') { | 439 } else if (fill.substring(0,3) == 'url') { |
440 var id = fill.substring(5,fill.length-1); | 440 var id = fill.substring(5,fill.length-1); |
441 var gr = this.gradients[id]; | 441 var gr = this.gradients[id]; |
442 paint = this.mb_rt.paint_linear_new(gr[0],gr[1],gr[2],gr[3]); | 442 paint = this.mb_rt.paint_linear_new(gr[0],gr[1],gr[2],gr[3]); |
443 paint.set_stops(this.stop_ref[id]); | 443 paint.set_stops(this.stop_ref[id]); |
444 } else { | 444 } else { |
445 paint = this.mb_rt.paint_color_new(0,0,0,1); | 445 paint = this.mb_rt.paint_color_new(0,0,0,1); |
446 } | 446 } |
447 } | 447 } |
448 var rect = this.mb_rt.rect_new(x,y,w,h,10, 10); | 448 var rect = this.mb_rt.rect_new(x,y,w,h,10, 10); |
449 paint.fill(rect); | 449 paint.fill(rect); |
450 tcoord.add_shape(rect); | 450 tcoord.add_shape(rect); |
451 } | 451 }; |
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. | 452 |
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 | 453 // When we parse a group, we need to calculate the origin of the group |
454 // matrix. For group, we need to send the acculumated matrix so that each group can get their origin correctly. | 454 // so that we can resize the group without changing its origin point. |
455 // This must be done recursively. For text/rect/image, we can get its | |
456 // origin point directly by using the (x,y) and apply their | |
457 // transformation matrix. For group, we need to send the acculumated | |
458 // matrix so that each group can get their origin correctly. | |
455 // | 459 // |
456 // Each element must be responsible to calculate its absolute origin point and update the origin of its parent. | 460 // Each element must be responsible to calculate its absolute origin |
457 loadSVG.prototype.parseGroup=function(accu_matrix,root, group_id, n) | 461 // point and update the origin of its parent. |
458 { | 462 |
463 loadSVG.prototype.parseGroup=function(accu_matrix,root, group_id, n) { | |
459 var k; | 464 var k; |
460 var nodes = n.childNodes(); | 465 var nodes = n.childNodes(); |
461 var coord = this.mb_rt.coord_new(root); | 466 var coord = this.mb_rt.coord_new(root); |
462 // Parse the transform and style here | 467 // Parse the transform and style here |
463 var trans = n.attr('transform'); | 468 var trans = n.attr('transform'); |
464 var accu=[1,0,0,0,1,0]; | 469 var accu=[1,0,0,0,1,0]; |
465 coord.center= new Object(); | 470 coord.center= new Object(); |
466 coord.center.x = 10000; | 471 coord.center.x = 10000; |
467 coord.center.y = 10000; | 472 coord.center.y = 10000; |
468 if (trans!=null) { | 473 if (trans!=null) { |
469 parseTransform(coord, trans.value()); | 474 parseTransform(coord, trans.value()); |
470 } | 475 } |
471 multiply(accu,accu_matrix); | 476 multiply(accu,accu_matrix); |
472 multiply(accu,coord); | 477 multiply(accu,coord); |
473 | 478 |
474 for(k in nodes) { | 479 for(k in nodes) { |
475 var c = nodes[k].name(); | 480 var c = nodes[k].name(); |
476 var attr = nodes[k].attr('id'); | 481 var attr = nodes[k].attr('id'); |
477 var id; | 482 var id; |
478 if (attr) { | 483 if (attr) { |
479 id = attr.value(); | 484 id = attr.value(); |
485 } | |
486 if (c == "g") { | |
487 this.parseGroup(accu,coord, id, nodes[k]); | |
488 } else if (c == "path") { | |
489 this.parsePath(accu,coord, id, nodes[k]); | |
490 } else if (c == "text") { | |
491 this.parseText(accu,coord, id, nodes[k]); | |
492 } else if (c == "rect") { | |
493 this.parseRect(accu_matrix,coord, id, nodes[k]); | |
494 } else if (c == "image") { | |
495 this.parseImage(accu_matrix,coord, id, nodes[k]); | |
496 } | |
497 } | |
498 if (root.center.x > coord.center.x) | |
499 root.center.x = coord.center.x; | |
500 if (root.center.y > coord.center.y) | |
501 root.center.y = coord.center.y; | |
502 make_mbnames(this.mb_rt, n, coord); | |
503 }; | |
504 | |
505 loadSVG.prototype.parseImage=function(accu,coord,id, n) | |
506 { | |
507 var ref = n.attr('href').value(); | |
508 var tcoord = this.mb_rt.coord_new(coord); | |
509 var trans = n.attr('transform'); | |
510 | |
511 if (ref == null) return; | |
512 if (ref.substr(0,7) == "file://") { | |
513 ref = ref.substring(7); | |
514 } else if (ref.substr(0,5)=="file:") { | |
515 ref = ref.substring(5); | |
516 } else { | |
517 return; | |
518 } | |
519 var w; | |
520 var h; | |
521 var x,y,nx,ny; | |
522 coord.center= new Object(); | |
523 coord.center.x = 10000; | |
524 coord.center.y = 10000; | |
525 if (trans!=null) { | |
526 parseTransform(coord, trans.value()); | |
527 } | |
528 | |
529 w = n.attr("width"); | |
530 if (w == null) return; | |
531 w = parseFloat(w.value()); | |
532 h = n.attr("height"); | |
533 if (h == null) return; | |
534 h = parseFloat(h.value()); | |
535 x = n.attr("x"); | |
536 if (x == null) return; | |
537 x = parseFloat(x.value()); | |
538 y = n.attr("y"); | |
539 if (y == null) return; | |
540 y = parseFloat(y.value()); | |
541 nx = tcoord[0]*x+tcoord[1]*y+tcoord[2]; | |
542 ny = tcoord[3]*x+tcoord[4]*y+tcoord[5]; | |
543 if (coord.center.x > nx) | |
544 coord.center.x = nx; | |
545 if (coord.center.y > ny) | |
546 coord.center.y = ny; | |
547 var img = this.mb_rt.image_new(x,y,w,h); | |
548 var img_data = ldr.load(ref); | |
549 var paint = this.mb_rt.paint_image_new(img_data); | |
550 paint.fill(img); | |
551 tcoord.add_shape(img); | |
552 make_mbnames(this.mb_rt, n, img); | |
553 }; | |
554 | |
555 loadSVG.prototype._MB_parseLinearGradient=function(root,n) | |
556 { | |
557 var id = n.attr('id'); | |
558 var k; | |
559 var nodes = n.childNodes(); | |
560 | |
561 if (id == null) return; | |
562 var x1 = n.attr("x1"); | |
563 var y1 = n.attr("y1"); | |
564 var x2 = n.attr("x2"); | |
565 var y2 = n.attr("y2"); | |
566 var gr; | |
567 var color, opacity; | |
568 var stops; | |
569 var r,g,b; | |
570 stops=[]; | |
571 for(k in nodes) { | |
572 var ss = nodes[k]; | |
573 if (ss.name()=="stop") { | |
574 var style = ss.attr("style").value(); | |
575 var items = style.split(';'); | |
576 var off = parseInt(ss.attr('offset').value()); | |
577 color = 'black'; | |
578 opacity = 1; | |
579 for (i in items) { | |
580 it = items[i]; | |
581 var f = it.split(':'); | |
582 k = f[0]; | |
583 v = f[1]; | |
584 if (k == 'stop-color') { | |
585 color = v.substring(1); | |
586 if (v == 'white') { | |
587 r = 1; | |
588 g = 1; | |
589 b = 1; | |
590 } else if (v == 'black') { | |
591 r = 0; | |
592 g = 0; | |
593 b = 0; | |
594 } else { | |
595 r = parseInt(color.substring(0,2),16)/255.0; | |
596 g = parseInt(color.substring(2,4),16)/255.0; | |
597 b = parseInt(color.substring(4,6),16)/255.0; | |
598 } | |
599 } else if (k=='stop-opacity') { | |
600 opacity = parseFloat(v); | |
480 } | 601 } |
481 if (c == "g") { | 602 } |
482 this.parseGroup(accu,coord, id, nodes[k]); | 603 stops.push([off, r,g,b,opacity]); |
483 } else if (c == "path") { | 604 } |
484 this.parsePath(accu,coord, id, nodes[k]); | 605 } |
485 } else if (c == "text") { | 606 var href = n.attr('href'); |
486 this.parseText(accu,coord, id, nodes[k]); | 607 if (href != null) { |
487 } else if (c == "rect") { | 608 href = href.value(); |
488 this.parseRect(accu_matrix,coord, id, nodes[k]); | 609 pstops = this.stop_ref[href.substring(1)]; |
489 } else if (c == "image") { | 610 stops = pstops.concat(stops); |
490 this.parseImage(accu_matrix,coord, id, nodes[k]); | 611 } |
491 } | 612 id = id.value(); |
492 } | 613 this.stop_ref[id] = stops; |
493 if (root.center.x > coord.center.x) | 614 if (x1) |
494 root.center.x = coord.center.x; | 615 x1 = parseFloat(x1.value()); |
495 if (root.center.y > coord.center.y) | 616 if (x2) |
496 root.center.y = coord.center.y; | 617 x2 = parseFloat(x2.value()); |
497 make_mbnames(this.mb_rt, n, coord); | 618 if (y1) |
498 } | 619 y1 = parseFloat(y1.value()); |
499 | 620 if (y2) |
500 loadSVG.prototype.parseImage=function(accu,coord,id, n) | 621 y2 = parseFloat(y2.value()); |
501 { | 622 this.gradients[id] = [x1,y1,x2,y2]; |
502 var ref = n.attr('href').value(); | 623 }; |
503 var tcoord = this.mb_rt.coord_new(coord); | 624 |
504 var trans = n.attr('transform'); | 625 loadSVG.prototype.parseDefs=function(root,n) |
505 | |
506 if (ref == null) return; | |
507 if (ref.substr(0,7) == "file://") { | |
508 ref = ref.substring(7); | |
509 } else if (ref.substr(0,5)=="file:") { | |
510 ref = ref.substring(5); | |
511 } else { | |
512 return; | |
513 } | |
514 var w; | |
515 var h; | |
516 var x,y,nx,ny; | |
517 coord.center= new Object(); | |
518 coord.center.x = 10000; | |
519 coord.center.y = 10000; | |
520 if (trans!=null) { | |
521 parseTransform(coord, trans.value()); | |
522 } | |
523 | |
524 w = n.attr("width"); | |
525 if (w == null) return; | |
526 w = parseFloat(w.value()); | |
527 h = n.attr("height"); | |
528 if (h == null) return; | |
529 h = parseFloat(h.value()); | |
530 x = n.attr("x"); | |
531 if (x == null) return; | |
532 x = parseFloat(x.value()); | |
533 y = n.attr("y"); | |
534 if (y == null) return; | |
535 y = parseFloat(y.value()); | |
536 nx = tcoord[0]*x+tcoord[1]*y+tcoord[2]; | |
537 ny = tcoord[3]*x+tcoord[4]*y+tcoord[5]; | |
538 if (coord.center.x > nx) | |
539 coord.center.x = nx; | |
540 if (coord.center.y > ny) | |
541 coord.center.y = ny; | |
542 var img = this.mb_rt.image_new(x,y,w,h); | |
543 var img_data = ldr.load(ref); | |
544 var paint = this.mb_rt.paint_image_new(img_data); | |
545 paint.fill(img); | |
546 tcoord.add_shape(img); | |
547 make_mbnames(this.mb_rt, n, img); | |
548 } | |
549 | |
550 loadSVG.prototype._MB_parseLinearGradient=function(root,n) | |
551 { | |
552 var id = n.attr('id'); | |
553 var k; | |
554 var nodes = n.childNodes(); | |
555 | |
556 if (id == null) return; | |
557 var x1 = n.attr("x1"); | |
558 var y1 = n.attr("y1"); | |
559 var x2 = n.attr("x2"); | |
560 var y2 = n.attr("y2"); | |
561 var gr; | |
562 var color, opacity; | |
563 var stops; | |
564 var r,g,b; | |
565 stops=[]; | |
566 for(k in nodes) { | |
567 var ss = nodes[k]; | |
568 if (ss.name()=="stop") { | |
569 var style = ss.attr("style").value(); | |
570 var items = style.split(';'); | |
571 var off = parseInt(ss.attr('offset').value()); | |
572 color = 'black'; | |
573 opacity = 1; | |
574 for (i in items) { | |
575 it = items[i]; | |
576 var f = it.split(':'); | |
577 k = f[0]; | |
578 v = f[1]; | |
579 if (k == 'stop-color') { | |
580 color = v.substring(1); | |
581 if (v == 'white') { | |
582 r = 1; | |
583 g = 1; | |
584 b = 1; | |
585 } else if (v == 'black') { | |
586 r = 0; | |
587 g = 0; | |
588 b = 0; | |
589 } else { | |
590 r = parseInt(color.substring(0,2),16)/255.0; | |
591 g = parseInt(color.substring(2,4),16)/255.0; | |
592 b = parseInt(color.substring(4,6),16)/255.0; | |
593 } | |
594 } else if (k=='stop-opacity') { | |
595 opacity = parseFloat(v); | |
596 } | |
597 } | |
598 stops.push([off, r,g,b,opacity]); | |
599 } | |
600 } | |
601 var href = n.attr('href'); | |
602 if (href != null) { | |
603 href = href.value(); | |
604 pstops = this.stop_ref[href.substring(1)]; | |
605 stops = pstops.concat(stops); | |
606 } | |
607 id = id.value(); | |
608 this.stop_ref[id] = stops; | |
609 if (x1) | |
610 x1 = parseFloat(x1.value()); | |
611 if (x2) | |
612 x2 = parseFloat(x2.value()); | |
613 if (y1) | |
614 y1 = parseFloat(y1.value()); | |
615 if (y2) | |
616 y2 = parseFloat(y2.value()); | |
617 this.gradients[id] = [x1,y1,x2,y2]; | |
618 } | |
619 | |
620 loadSVG.prototype.parseDefs=function(root,n) | |
621 { | 626 { |
622 var k; | 627 var k; |
623 var nodes = n.childNodes(); | 628 var nodes = n.childNodes(); |
624 | 629 |
625 for(k in nodes) { | 630 for(k in nodes) { |
626 var name = nodes[k].name(); | 631 var name = nodes[k].name(); |
627 if (name == "linearGradient") { | 632 if (name == "linearGradient") { |
628 this._MB_parseLinearGradient(root,nodes[k]); | 633 this._MB_parseLinearGradient(root,nodes[k]); |
629 } | 634 } |
630 } | 635 } |
631 } | 636 }; |
632 | 637 |
633 | 638 |