Skip to content

Commit

Permalink
cleanup some cruft
Browse files Browse the repository at this point in the history
  • Loading branch information
bmtm committed Jan 16, 2016
1 parent 1b51fa1 commit b4f217d
Showing 1 changed file with 0 additions and 382 deletions.
382 changes: 0 additions & 382 deletions svgparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,171 +59,7 @@
this.recurse(this.svgRoot, this.splitPath);

return this.svgRoot;

/*dedupe(this.svgRoot.firstElementChild);
dedupe(this.svgRoot.lastElementChild);
var i;
var A = [];
for(i=0; i<this.svgRoot.firstElementChild.points.length; i++){
A.push({
x: this.svgRoot.firstElementChild.points[i].x,
y: this.svgRoot.firstElementChild.points[i].y,
});
}
var B = [];
for(i=0; i<this.svgRoot.lastElementChild.points.length; i++){
B.push({
x: this.svgRoot.lastElementChild.points[i].x,
y: this.svgRoot.lastElementChild.points[i].y,
});
}
if(GeometryUtil.polygonArea(A) < 0){
A.reverse();
}
if(GeometryUtil.polygonArea(B) < 0){
B.reverse();
}
A.push(A[0]);
B.push(B[0]);
var nfp = GeometryUtil.noFitPolygon(A, B, true);
console.log(nfp);
for(var i=0; i<nfp.length; i++){
var nfpe = this.svg.createElementNS(this.svgRoot.namespaceURI, 'polyline');
for(var j=0; j<nfp[i].length; j++){
var point = this.svgRoot.createSVGPoint();
point.x = nfp[i][j].x;
point.y = nfp[i][j].y;
nfpe.points.appendItem(point);
}
this.svgRoot.appendChild(nfpe);
}
return this.svgRoot;
// make our own copy of the SVG to preserve the original path data
// replace all elements with polygons/polyline approximations
this.recurse(this.svgRoot, this.polygonify.bind(this));
// place the paths in a bin
var bin = [];
for(var i=0; i<this.svgRoot.firstElementChild.points.length; i++){
bin.push({
x: this.svgRoot.firstElementChild.points[i].x,
y: this.svgRoot.firstElementChild.points[i].y,
});
}
var placed = [];
var len = this.svgRoot.children.length;
for(i=1; i<len; i++){
var child = this.svgRoot.children[i];
dedupe(child);
var A = [];
for(var j=0; j<child.points.length; j++){
A.push({
x: child.points[j].x,
y: child.points[j].y,
});
}
var NFP = GeometryUtil.noFitPolygon(bin, A, true);
if(NFP.length == 0){
console.log('COULDNT PLACE, FIX THIS!!!!! OMG');
continue;
}
placed.push(A);
for(j=0; j<NFP.length; j++){
var nfpe = this.svg.createElementNS(this.svgRoot.namespaceURI, 'polyline');
for(var k=0; k<NFP[j].length; k++){
var point = this.svgRoot.createSVGPoint();
point.x = NFP[j][k].x;
point.y = NFP[j][k].y;
nfpe.points.appendItem(point);
}
this.svgRoot.appendChild(nfpe);
}
NFP = NFP[0];
console.log(NFP);
child.setAttribute('transform','translate('+(NFP[0].x-A[0].x)+' '+(NFP[0].y-A[0].y)+')');
}*/
//var polylines = [];
/*for(var i=0; i< this.svgRoot.children.length; i++){
var edge = GeometryUtil.polygonEdge(this.svgRoot.children[i].points, {x: 1, y: 0});
var color = getRandomColor();
for(var j=0; j<edge.length; j++){ // each point
var circle = this.svg.createElementNS(this.svgRoot.namespaceURI, 'circle');
circle.setAttribute('fill',color);
circle.setAttribute('r', 1);
circle.setAttribute('cx', edge[j].x);
circle.setAttribute('cy', edge[j].y);
this.svgRoot.appendChild(circle);
}
}*/
//console.log(GeometryUtil.polygonSlideDistance(this.svgRoot.firstElementChild.points, this.svgRoot.lastElementChild.points, {x: -1, y: 0}));


function dedupe(poly){
var i=0;
while(i<poly.points.length-1){
var p = poly.points[i];
var pnext = poly.points[i+1];
//if(p.x == pnext.x && p.y == pnext.y){
if(GeometryUtil.withinDistance(p, pnext, 0.00000001)){
poly.points.removeItem(i+1);
i--;
}
i++;
//console.log(poly, poly.points);
}
}

//var intersect = GeometryUtil.polylineIntersect(polylines, true);
//console.log(intersect);
/*for(i=0; i<intersect.length; i++){ // each shape
var color = getRandomColor();
for(var j=0; j<intersect[i].length; j++){ // each point
var circle = this.svg.createElementNS(this.svgRoot.namespaceURI, 'circle');
circle.setAttribute('fill',color);
circle.setAttribute('r', 1);
circle.setAttribute('cx', intersect[i][j].x);
circle.setAttribute('cy', intersect[i][j].y);
this.svgRoot.appendChild(circle);
}
}*/

//var edge = GeometryUtil.polygonEdge(A, {x: -1.7611770629882812, y: 1.7601804733276367});
/*var edge = GeometryUtil.polygonEdge(A, {x: -1.8775253295898438, y: -1.8785877227783203});

for(i=0; i<edge.length; i++){ // each shape
//var color = getRandomColor();
var circle = this.svg.createElementNS(this.svgRoot.namespaceURI, 'circle');
circle.setAttribute('r', 1);
circle.setAttribute('cx', edge[i].x);
circle.setAttribute('cy', edge[i].y);
this.svgRoot.appendChild(circle);
}*/



function getRandomColor() {
var letters = '0123456789ABCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++ ) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
}

// set the given path as absolute coords (capital commands)
Expand Down Expand Up @@ -629,224 +465,6 @@
func(element);
}

// make every element a polygon or polyline
/*SvgParser.prototype.polygonify = function(element){
var poly;
switch(element.tagName){
case 'rect':
poly = this.svg.createElementNS(element.namespaceURI, 'polygon');
var p1 = this.svgRoot.createSVGPoint();
var p2 = this.svgRoot.createSVGPoint();
var p3 = this.svgRoot.createSVGPoint();
var p4 = this.svgRoot.createSVGPoint();
p1.x = element.getAttribute('x') || 0;
p1.y = element.getAttribute('y') || 0;
p2.x = p1.x + parseFloat(element.getAttribute('width'));
p2.y = p1.y;
p3.x = p2.x;
p3.y = p1.y + parseFloat(element.getAttribute('height'));
p4.x = p1.x;
p4.y = p3.y;
poly.points.appendItem(p1);
poly.points.appendItem(p2);
poly.points.appendItem(p3);
poly.points.appendItem(p4);
break;
case 'circle':
poly = this.svg.createElementNS(element.namespaceURI, 'polygon');
var radius = parseFloat(element.getAttribute('r'));
var cx = parseFloat(element.getAttribute('cx'));
var cy = parseFloat(element.getAttribute('cy'));
// num is the smallest number of segments required to approximate the circle to the given tolerance
var num = Math.ceil((2*Math.PI)/Math.acos(1 - (this.conf.tolerance/radius)));
if(num < 3){
num = 3;
}
for(var i=0; i<num; i++){
var theta = i * ( (2*Math.PI) / num);
var point = this.svgRoot.createSVGPoint();
point.x = radius*Math.cos(theta) + cx;
point.y = radius*Math.sin(theta) + cy;
poly.points.appendItem(point);
}
break;
case 'ellipse':
poly = this.svg.createElementNS(element.namespaceURI, 'polygon');
// same as circle case. There is probably a way to reduce points but for convenience we will just flatten the equivalent circular polygon
var rx = parseFloat(element.getAttribute('rx'))
var ry = parseFloat(element.getAttribute('ry'));
var maxradius = Math.max(rx, ry);
var cx = parseFloat(element.getAttribute('cx'));
var cy = parseFloat(element.getAttribute('cy'));
var num = Math.ceil((2*Math.PI)/Math.acos(1 - (this.conf.tolerance/maxradius)));
if(num < 3){
num = 3;
}
for(var i=0; i<num; i++){
var theta = i * ( (2*Math.PI) / num);
var point = this.svgRoot.createSVGPoint();
point.x = rx*Math.cos(theta) + cx;
point.y = ry*Math.sin(theta) + cy;
poly.points.appendItem(point);
}
break;
case 'path':
// first split into individual paths so each path will only have one M command
var paths = this.splitPath(element);
if(paths){
for(var i=0; i<paths.length; i++){
this.polygonify(paths[i]);
}
}
else{
var seglist = element.pathSegList;
var firstCommand = seglist[0];
var lastCommand = seglist[seglist.length-1];
if(lastCommand.pathSegTypeAsLetter == 'Z' || lastCommand.pathSegTypeAsLetter == 'z' ||
GeometryUtil.withinDistance(firstCommand, lastCommand, this.conf.tolerance)){
// if a Z is not explicitly specified but the ending point is very close to the starting point, we assume they mean to close it but forgot.
poly = this.svg.createElementNS(element.namespaceURI, 'polygon');
}
else{
poly = this.svg.createElementNS(element.namespaceURI, 'polyline'); // open path
}
var x=0, y=0, x0=0, y0=0, x1=0, y1=0, x2=0, y2=0, prevx=0, prevy=0, prevx1=0, prevy1=0, prevx2=0, prevy2=0;
for(var i=0; i<seglist.length; i++){
var s = seglist[i];
var command = s.pathSegTypeAsLetter;
prevx = x;
prevy = y;
prevx1 = x1;
prevy1 = y1;
prevx2 = x2;
prevy2 = y2;
if (/[MLHVCSQTA]/.test(command)){
if ('x1' in s) x1=s.x1;
if ('x2' in s) x2=s.x2;
if ('y1' in s) y1=s.y1;
if ('y2' in s) y2=s.y2;
if ('x' in s) x=s.x;
if ('y' in s) y=s.y;
}
else{
if ('x1' in s) x1=x+s.x1;
if ('x2' in s) x2=x+s.x2;
if ('y1' in s) y1=y+s.y1;
if ('y2' in s) y2=y+s.y2;
if ('x' in s) x+=s.x;
if ('y' in s) y+=s.y;
}
switch(command){
// linear line types
case 'm':
case 'M':
case 'l':
case 'L':
case 'h':
case 'H':
case 'v':
case 'V':
var point = this.svgRoot.createSVGPoint();
point.x = x;
point.y = y;
poly.points.appendItem(point);
break;
// Quadratic Beziers
case 't':
case 'T':
// implicit control point
if(i > 0 && /[QqTt]/.test(seglist[i-1].pathSegTypeAsLetter)){
x1 = prevx + (prevx-prevx1);
y1 = prevy + (prevy-prevy1);
}
else{
x1 = prevx;
y1 = prevy;
}
case 'q':
case 'Q':
var pointlist = GeometryUtil.QuadraticBezier.linearize({x: prevx, y: prevy}, {x: x, y: y}, {x: x1, y: y1}, this.conf.tolerance);
pointlist.shift(); // firstpoint would already be in the poly
for(var j=0; j<pointlist.length; j++){
var point = this.svgRoot.createSVGPoint();
point.x = pointlist[j].x;
point.y = pointlist[j].y;
poly.points.appendItem(point);
}
break;
case 's':
case 'S':
if(i > 0 && /[CcSs]/.test(seglist[i-1].pathSegTypeAsLetter)){
x1 = prevx + (prevx-prevx2);
y1 = prevy + (prevy-prevy2);
}
else{
x1 = prevx;
y1 = prevy;
}
case 'c':
case 'C':
var pointlist = GeometryUtil.CubicBezier.linearize({x: prevx, y: prevy}, {x: x, y: y}, {x: x1, y: y1}, {x: x2, y: y2}, this.conf.tolerance);
pointlist.shift(); // firstpoint would already be in the poly
for(var j=0; j<pointlist.length; j++){
var point = this.svgRoot.createSVGPoint();
point.x = pointlist[j].x;
point.y = pointlist[j].y;
poly.points.appendItem(point);
}
break;
case 'a':
case 'A':
var pointlist = GeometryUtil.Arc.linearize({x: prevx, y: prevy}, {x: x, y: y}, s.r1, s.r2, s.angle, s.largeArcFlag,s.sweepFlag, this.conf.tolerance);
pointlist.shift();
for(var j=0; j<pointlist.length; j++){
var point = this.svgRoot.createSVGPoint();
point.x = pointlist[j].x;
point.y = pointlist[j].y;
poly.points.appendItem(point);
}
break;
case 'z': case 'Z': x=x0; y=y0; break;
}
// Record the start of a subpath
if (command=='M' || command=='m') x0=x, y0=y;
}
}
break;
}
if(poly && element.parentElement){
element.parentElement.replaceChild(poly, element);
}
}*/

// return a polygon from the given SVG element in the form of an array of points
SvgParser.prototype.polygonify = function(element){
var poly = [];
Expand Down

0 comments on commit b4f217d

Please sign in to comment.