| | 134 | buildFills: function(g) { |
|---|
| | 135 | var loopss = this.fillLoops; |
|---|
| | 136 | if (!loopss) return; |
|---|
| | 137 | |
|---|
| | 138 | var len = loopss.length; |
|---|
| | 139 | |
|---|
| | 140 | for (var i = 0;i < len;i++) { |
|---|
| | 141 | this.buildAFill(loopss[i].loops, this.fillStyles[loopss[i].fill_index-1], g); |
|---|
| | 142 | } |
|---|
| | 143 | }, |
|---|
| | 144 | |
|---|
| | 145 | buildAFill: function(list, style, g) { |
|---|
| | 146 | if (!style) return; |
|---|
| | 147 | |
|---|
| | 148 | var style_str = null; |
|---|
| | 149 | if (style.rgb) |
|---|
| | 150 | style_str = '#'+style.rgb; |
|---|
| | 151 | |
|---|
| | 152 | if (!style_str) return; |
|---|
| | 153 | |
|---|
| | 154 | |
|---|
| | 155 | var len = list.length; |
|---|
| | 156 | var commands = [], lp, k, llen, E; |
|---|
| | 157 | for (var i = 0;i < len;i++) { |
|---|
| | 158 | lp = list[i]; |
|---|
| | 159 | llen = lp.length; |
|---|
| | 160 | |
|---|
| | 161 | if (i) { |
|---|
| | 162 | commands.push('Z'); |
|---|
| | 163 | } |
|---|
| | 164 | |
|---|
| | 165 | for (k = 0;k < llen;k++) { |
|---|
| | 166 | E = lp[k]; |
|---|
| | 167 | E.calcPx(); |
|---|
| | 168 | if (k==0) |
|---|
| | 169 | commands.push('M '+E.px1+','+E.py1); |
|---|
| | 170 | |
|---|
| | 171 | if (E.original.curved) { |
|---|
| | 172 | commands.push('Q ' + E.pxC +','+ E.pyC); |
|---|
| | 173 | commands.push(E.px2 +','+ E.py2); |
|---|
| | 174 | } |
|---|
| | 175 | else |
|---|
| | 176 | commands.push('L ' + E.px2 +','+ E.py2); |
|---|
| | 177 | } |
|---|
| | 178 | } |
|---|
| | 179 | |
|---|
| | 180 | commands.push('Z'); |
|---|
| | 181 | |
|---|
| | 182 | g.appendChild( JSplash.$svg('path', {d: commands.join(' '), fill: style_str}) ); |
|---|
| | 183 | }, |
|---|
| | 184 | |
|---|
| | 237 | commitFill: function() { |
|---|
| | 238 | var len, i; |
|---|
| | 239 | |
|---|
| | 240 | var fi; |
|---|
| | 241 | var elist; |
|---|
| | 242 | var nFills = this.fillStyles.length; |
|---|
| | 243 | |
|---|
| | 244 | this.fillLoops = []; |
|---|
| | 245 | for (fi = 1;fi <= nFills;fi++) |
|---|
| | 246 | { |
|---|
| | 247 | var collected_edges = []; |
|---|
| | 248 | elist = this.rightFillEdges[fi]; |
|---|
| | 249 | if (elist) { |
|---|
| | 250 | len = elist.length; |
|---|
| | 251 | for (i = 0;i < len;i++) |
|---|
| | 252 | collected_edges.push(elist[i].tearOff(false, this.te_id++)); |
|---|
| | 253 | } |
|---|
| | 254 | |
|---|
| | 255 | elist = this.leftFillEdges[fi]; |
|---|
| | 256 | if (elist) { |
|---|
| | 257 | len = elist.length; |
|---|
| | 258 | for (i = 0;i < len;i++) |
|---|
| | 259 | collected_edges.push(elist[i].tearOff(true, this.te_id++)); |
|---|
| | 260 | |
|---|
| | 261 | } |
|---|
| | 262 | |
|---|
| | 263 | var lps = this.makeLoop(collected_edges, fi); |
|---|
| | 264 | if (lps.length > 0) { |
|---|
| | 265 | this.fillLoops.push({fill_index: fi, loops: lps}); |
|---|
| | 266 | } |
|---|
| | 267 | } |
|---|
| | 268 | |
|---|
| | 269 | /* clear */ |
|---|
| | 270 | this.leftFillEdges = {}; |
|---|
| | 271 | this.rightFillEdges = {}; |
|---|
| | 272 | }, |
|---|
| | 273 | |
|---|
| | 274 | makeLoop: function(elist, fi) { |
|---|
| | 275 | var pmap = {}, loops = []; |
|---|
| | 276 | var i, len, te, k; |
|---|
| | 277 | |
|---|
| | 278 | len = elist.length; |
|---|
| | 279 | for (i = 0;i < len;i++) |
|---|
| | 280 | { |
|---|
| | 281 | te = elist[i]; |
|---|
| | 282 | k = this.make_key(te.getTx1(), te.getTy1()); |
|---|
| | 283 | if (!pmap[k]) pmap[k] = []; |
|---|
| | 284 | pmap[k].push(te); |
|---|
| | 285 | } |
|---|
| | 286 | |
|---|
| | 287 | var parts, start_k, fst, nxt; |
|---|
| | 288 | len = elist.length; |
|---|
| | 289 | for (i = 0;i < len;i++) |
|---|
| | 290 | { |
|---|
| | 291 | te = elist[i]; |
|---|
| | 292 | if (te.has_loop) |
|---|
| | 293 | continue; |
|---|
| | 294 | |
|---|
| | 295 | // assume that this is the first part |
|---|
| | 296 | te.has_loop = true; |
|---|
| | 297 | parts = [te]; |
|---|
| | 298 | fst = te; |
|---|
| | 299 | var usedmap = {}; |
|---|
| | 300 | for (start_k = this.make_key( te.getTx1(), te.getTy1() );;) |
|---|
| | 301 | { |
|---|
| | 302 | k = this.make_key( te.getTx2(), te.getTy2() ); |
|---|
| | 303 | if (k == start_k) // looped! |
|---|
| | 304 | { |
|---|
| | 305 | this.use_all(parts); |
|---|
| | 306 | loops.push(parts); |
|---|
| | 307 | break; |
|---|
| | 308 | } |
|---|
| | 309 | |
|---|
| | 310 | if (!pmap[k] || pmap[k].length == 0) break; // no next edge |
|---|
| | 311 | |
|---|
| | 312 | nxt = this.seq_next(pmap[k], elist[i+1], usedmap); |
|---|
| | 313 | if (!nxt) break; |
|---|
| | 314 | |
|---|
| | 315 | te = nxt; |
|---|
| | 316 | usedmap[te.eid] = true; |
|---|
| | 317 | parts.push(te); |
|---|
| | 318 | } |
|---|
| | 319 | } |
|---|
| | 320 | |
|---|
| | 321 | return loops; |
|---|
| | 322 | }, |
|---|
| | 323 | |
|---|
| | 324 | |
|---|
| | 325 | seq_next: function(list, t, usedmap) { |
|---|
| | 326 | if (!t) return null; |
|---|
| | 327 | |
|---|
| | 328 | var i, len = list.length; |
|---|
| | 329 | for (i = 0;i < len;i++) |
|---|
| | 330 | { |
|---|
| | 331 | if (list[i] == t && !list[i].has_loop) |
|---|
| | 332 | { |
|---|
| | 333 | var ret = list[i]; |
|---|
| | 334 | list.splice(i, 1); |
|---|
| | 335 | |
|---|
| | 336 | return ret; |
|---|
| | 337 | } |
|---|
| | 338 | } |
|---|
| | 339 | |
|---|
| | 340 | for (i = 0;i < len;i++) |
|---|
| | 341 | { |
|---|
| | 342 | if (!list[i].has_loop && !usedmap[list[i].eid]) |
|---|
| | 343 | return list[i]; |
|---|
| | 344 | } |
|---|
| | 345 | |
|---|
| | 346 | return null; |
|---|
| | 347 | }, |
|---|
| | 348 | |
|---|
| | 349 | use_all: function(a) { |
|---|
| | 350 | var i, len = a.length; |
|---|
| | 351 | for (i = 0;i < len;i++) |
|---|
| | 352 | a[i].has_loop = true; |
|---|
| | 353 | }, |
|---|
| | 354 | |
|---|
| | 355 | make_key: function(x, y) { |
|---|
| | 356 | return (x+50000)*100000+y; |
|---|
| | 357 | }, |
|---|
| | 358 | |
|---|
| 185 | | |
|---|
| | 360 | var lines = [], fi; |
|---|
| | 361 | var parts = []; |
|---|
| | 362 | |
|---|
| | 363 | lines.push("===== LEFT ====="); |
|---|
| | 364 | for (fi in this.leftFillEdges) |
|---|
| | 365 | parts.push(fi+"("+(this.leftFillEdges[fi].length)+")"); |
|---|
| | 366 | |
|---|
| | 367 | lines.push( parts.join(' ') ); |
|---|
| | 368 | |
|---|
| | 369 | parts = []; |
|---|
| | 370 | lines.push("===== RIGHT ====="); |
|---|
| | 371 | for (fi in this.rightFillEdges) |
|---|
| | 372 | parts.push(fi+"("+(this.rightFillEdges[fi].length)+")"); |
|---|
| | 373 | |
|---|
| | 374 | lines.push( parts.join(' ') ); |
|---|
| | 375 | |
|---|
| | 376 | console.log(lines.join("\n")); |
|---|
| | 387 | |
|---|
| | 388 | JSplash.EdgePart.prototype = { |
|---|
| | 389 | tearOff: function(rev, eid) { |
|---|
| | 390 | return new JSplash.TearedEdge(this, rev, eid); |
|---|
| | 391 | }, |
|---|
| | 392 | } |
|---|
| | 393 | |
|---|
| | 394 | |
|---|
| | 395 | JSplash.TearedEdge = function(org, rev, eid) |
|---|
| | 396 | { |
|---|
| | 397 | this.rev = rev; |
|---|
| | 398 | this.original = org; |
|---|
| | 399 | this.has_loop = false; |
|---|
| | 400 | this.px1 = null; |
|---|
| | 401 | this.eid = eid; |
|---|
| | 402 | } |
|---|
| | 403 | |
|---|
| | 404 | JSplash.TearedEdge.prototype = { |
|---|
| | 405 | getTx1: function() { |
|---|
| | 406 | return this.rev ? this.original.tx2 : this.original.tx1; |
|---|
| | 407 | }, |
|---|
| | 408 | |
|---|
| | 409 | getTy1: function() { |
|---|
| | 410 | return this.rev ? this.original.ty2 : this.original.ty1; |
|---|
| | 411 | }, |
|---|
| | 412 | |
|---|
| | 413 | |
|---|
| | 414 | getTx2: function() { |
|---|
| | 415 | return this.rev ? this.original.tx1 : this.original.tx2; |
|---|
| | 416 | }, |
|---|
| | 417 | |
|---|
| | 418 | getTy2: function() { |
|---|
| | 419 | return this.rev ? this.original.ty1 : this.original.ty2; |
|---|
| | 420 | }, |
|---|
| | 421 | |
|---|
| | 422 | calcPx: function() { |
|---|
| | 423 | if (this.px1 == null) { |
|---|
| | 424 | this.px1 = JSplash.$twips(this.getTx1()); |
|---|
| | 425 | this.py1 = JSplash.$twips(this.getTy1()); |
|---|
| | 426 | this.px2 = JSplash.$twips(this.getTx2()); |
|---|
| | 427 | this.py2 = JSplash.$twips(this.getTy2()); |
|---|
| | 428 | this.pxC = JSplash.$twips(this.original.txC); |
|---|
| | 429 | this.pyC = JSplash.$twips(this.original.tyC); |
|---|
| | 430 | } |
|---|
| | 431 | } |
|---|
| | 432 | } |
|---|
| | 433 | |
|---|
| | 434 | |
|---|