More SVG code refactoring.

Follow coding conventions and fix some forgotten renamings.
This commit is contained in:
Jürg Lehni 2012-10-22 16:31:08 -07:00
parent 160095d6e3
commit d4a60fb62a
12 changed files with 378 additions and 381 deletions

View file

@ -11,8 +11,8 @@
// strokeWidth: 2,
// strokeCap: 'round'
};
var isvg = new ImportSvg;
isvg.importSVG(document.getElementById('svg'));
var isvg = new ImportSvg();
isvg.importSvg(document.getElementById('svg'));
</script>
</head>
<body>

View file

@ -11,8 +11,8 @@
// strokeWidth: 2,
// strokeCap: 'round'
};
var isvg = new ImportSvg;
isvg.importSVG(document.getElementById('svg'));
var isvg = new ImportSvg();
isvg.importSvg(document.getElementById('svg'));
</script>
</head>
<body>

View file

@ -11,8 +11,8 @@
// strokeWidth: 2,
// strokeCap: 'round'
};
var isvg = new ImportSvg;
isvg.importSVG(document.getElementById('svg'));
var isvg = new ImportSvg();
isvg.importSvg(document.getElementById('svg'));
</script>
</head>
<body>

View file

@ -11,8 +11,8 @@
// strokeWidth: 2,
// strokeCap: 'round'
};
var isvg = new ImportSvg;
isvg.importSVG(document.getElementById('svg'));
var isvg = new ImportSvg();
isvg.importSvg(document.getElementById('svg'));
</script>
</head>
<body>

View file

@ -12,8 +12,8 @@
// strokeWidth: 2,
// strokeCap: 'round'
};
var isvg = new ImportSvg;
isvg.importSVG(document.getElementById('svg'));
var isvg = new ImportSvg();
isvg.importSvg(document.getElementById('svg'));
</script>
</head>
<body>

View file

@ -11,8 +11,8 @@
// strokeWidth: 2,
// strokeCap: 'round'
};
var isvg = new ImportSvg;
isvg.importSVG(document.getElementById('svg'));
var isvg = new ImportSvg();
isvg.importSvg(document.getElementById('svg'));
</script>
</head>
<body>

View file

@ -11,8 +11,8 @@
// strokeWidth: 2,
// strokeCap: 'round'
};
var isvg = new ImportSvg;
isvg.importSVG(document.getElementById('svg'));
var isvg = new ImportSvg();
isvg.importSvg(document.getElementById('svg'));
</script>
</head>
<body>

View file

@ -11,8 +11,8 @@
// strokeWidth: 2,
// strokeCap: 'round'
};
var isvg = new ImportSvg;
isvg.importSVG(document.getElementById('svg'));
var isvg = new ImportSvg();
isvg.importSvg(document.getElementById('svg'));
</script>
</head>
<body>

View file

@ -11,8 +11,8 @@
// strokeWidth: 2,
// strokeCap: 'round'
};
var isvg = new ImportSvg;
isvg.importSVG(document.getElementById('svg'));
var isvg = new ImportSvg();
isvg.importSvg(document.getElementById('svg'));
</script>
</head>
<body>

View file

@ -111,23 +111,24 @@ var ExportSvg = this.ExportSvg = Base.extend(/** @Lends ExportSvg# */{
var pointArray;
var handleInArray;
var handleOutArray;
var type;
//finding the type of path to export
if(path.content) {
if (path.content) {
type = 'text';
} else {
//Values are only defined if the path is not text because
// text does not have these values
segArray = path.getSegments();
pointArray = new Array();
handleInArray = new Array();
handleOutArray = new Array();
pointArray = [];
handleInArray = [];
handleOutArray = [];
for (i = 0; i < segArray.length; i++) {
pointArray[i] = segArray[i].getPoint();
handleInArray[i] = segArray[i].getHandleIn();
handleOutArray[i] = segArray[i].getHandleOut();
}
var exp = this;
var type = exp._determineType(path, segArray, pointArray, handleInArray, handleOutArray);
type = exp._determineType(path, segArray, pointArray, handleInArray, handleOutArray);
}
//switch statement that determines what type of SVG element to add to the SVG Object
switch (type) {
@ -204,13 +205,13 @@ var ExportSvg = this.ExportSvg = Base.extend(/** @Lends ExportSvg# */{
svgEle = document.createElementNS(this.NS, 'text');
svgEle.setAttribute('x', path.getPoint().getX());
svgEle.setAttribute('y', path.getPoint().getY());
if(path.style.font != undefined) {
if (path.style.font != undefined) {
svgEle.setAttribute('font', path.style.font);
}
if(path.characterStyle.font != undefined) {
if (path.characterStyle.font != undefined) {
svgEle.setAttribute('font-family', path.characterStyle.font);
}
if(path.characterStyle.fontSize != undefined) {
if (path.characterStyle.fontSize != undefined) {
svgEle.setAttribute('font-size',path.characterStyle.fontSize);
}
svgEle.textContent = path.getContent();
@ -222,12 +223,12 @@ var ExportSvg = this.ExportSvg = Base.extend(/** @Lends ExportSvg# */{
}
//If the object is a circle, ellipse, rectangle, or rounded rectangle, it will find the angle
//found by the determineIfTransformed method and make a path that accommodates for the transformed object
if(type != 'text' && type != undefined && type != 'polygon' && type != 'polyline' && type != 'line') {
if (type != 'text' && type != undefined && type != 'polygon' && type != 'polyline' && type != 'line') {
//TODO: Need to implement exported transforms for circle, ellipse, and rectangles instead of
//making them paths
var angle = this._determineIfTransformed(path, pointArray, type) + 90;
if(angle != 0) {
if(type == 'rect' || type == 'roundRect') {
if (angle != 0) {
if (type == 'rect' || type == 'roundRect') {
svgEle = document.createElementNS(this.NS, 'path');
svgEle = this.pathSetup(path, pointArray, handleInArray, handleOutArray);
} else {
@ -236,10 +237,10 @@ var ExportSvg = this.ExportSvg = Base.extend(/** @Lends ExportSvg# */{
}
}
}
if(type == 'text') {
if (type == 'text') {
svgEle.setAttribute('transform','rotate(' + path.matrix.getRotation() + ',' + path.getPoint().getX() + ',' +path.getPoint().getY() +')');
}
if(path.id != undefined) {
if (path.id != undefined) {
svgEle.setAttribute('id', path.id);
}
//checks if there is a stroke color in the passed in path
@ -254,30 +255,30 @@ var ExportSvg = this.ExportSvg = Base.extend(/** @Lends ExportSvg# */{
svgEle.setAttribute('fill', 'rgba(0,0,0,0)');
}
//same thing as stroke color except with stroke width
if(path.strokeWidth != undefined){
if (path.strokeWidth != undefined) {
svgEle.setAttribute('stroke-width', path.strokeWidth);
}
//same thing as stroke color except with the path name
if(path.name != undefined) {
if (path.name != undefined) {
svgEle.setAttribute('name', path.name);
}
//same thing as stroke color except with the strokeCap
if(path.strokeCap != undefined) {
if (path.strokeCap != undefined) {
svgEle.setAttribute('stroke-linecap', path.strokeCap);
}
//same thing as stroke color except with the strokeJoin
if(path.strokeJoin != undefined) {
if (path.strokeJoin != undefined) {
svgEle.setAttribute('stroke-linejoin', path.strokeJoin);
}
//same thing as stroke color except with the opacity
if(path.opacity != undefined) {
if (path.opacity != undefined) {
svgEle.setAttribute('opacity', path.opacity);
}
//checks to see if there the dashArray is set, then adds the attribute if there is.
if(path.dashArray[0] != undefined) {
if (path.dashArray[0] != undefined) {
var dashVals = '';
for (var i in path.dashArray) {
if(i != path.dashArray.length -1) {
if (i != path.dashArray.length -1) {
dashVals += path.dashArray[i] + ", ";
} else {
dashVals += path.dashArray[i];
@ -286,17 +287,17 @@ var ExportSvg = this.ExportSvg = Base.extend(/** @Lends ExportSvg# */{
svgEle.setAttribute('stroke-dasharray', dashVals);
}
//same thing as stroke color except with the dash offset
if(path.dashOffset != undefined) {
if (path.dashOffset != undefined) {
svgEle.setAttribute('stroke-dashoffset', path.dashOffset);
}
//same thing as stroke color except with the miter limit
if(path.miterLimit != undefined) {
if (path.miterLimit != undefined) {
svgEle.setAttribute('stroke-miterlimit', path.miterLimit);
}
//same thing as stroke color except with the visibility
if(path.visibility != undefined) {
if (path.visibility != undefined) {
var visString = '';
if(path.visibility) {
if (path.visibility) {
visString = 'visible';
} else {
visString = 'hidden';
@ -362,7 +363,7 @@ var ExportSvg = this.ExportSvg = Base.extend(/** @Lends ExportSvg# */{
y2 = pointArray[i + 1].getY();
handleOut1 = hOArray[i];
handleIn2 = hIArray[i+1];
if(handleOut1.getX() == 0 && handleOut1.getY() == 0 && handleIn2.getX() == 0 && handleIn2.getY() ==0) {
if (handleOut1.getX() == 0 && handleOut1.getY() == 0 && handleIn2.getX() == 0 && handleIn2.getY() ==0) {
//L is lineto, moving to a point with drawing
pointString+= 'L' + x2 + ',' + y2 + ' ';
} else {
@ -410,7 +411,7 @@ var ExportSvg = this.ExportSvg = Base.extend(/** @Lends ExportSvg# */{
var curves = false;
var segHandleIn;
var segHandleOut;
for( var i in segArray){
for( var i in segArray) {
//Checks for any curves (if the handles have values). Differentiates between straight objects(line, polyline, rect, and polygon) and
//and objects with curves(circle, ellipse, roundedRectangle).
segHandleIn = segArray[i].getHandleIn();
@ -421,46 +422,46 @@ var ExportSvg = this.ExportSvg = Base.extend(/** @Lends ExportSvg# */{
//Checks for curves in the passed in segments
//Checks if the type of the passed in path is a rounded rectangle, an ellipse, a circle, or if it's simply a path
//If there aren't any curves (if curves = false), then it checks if the type is a rectangle, a polygon, a polyline, or simply a line.
if(curves){
if(segArray.length == 8) {
if (curves) {
if (segArray.length == 8) {
//if the distance between (point0 and point3) and (point7 and point4) are equal then it is a roundedRectangle
dPoint12 = Math.round(pointArray[0].getDistance(pointArray[3], false));
dPoint34 = Math.round(pointArray[7].getDistance(pointArray[4], false));
if(dPoint12 == dPoint34) {
if (dPoint12 == dPoint34) {
type = 'roundRect';
}
} else if(segArray.length == 4) {
} else if (segArray.length == 4) {
//checks if the values of the point have values similar to circles and ellipses
var checkPointValues = true;
for(i = 0; i < pointArray.length && checkPointValues == true; i++) {
if(handleInArray[i].getX() != 0 || handleInArray[i].getY() != 0 && Math.round(Math.abs(handleInArray[i].getX())) === Math.round(Math.abs(handleOutArray[i].getX())) && Math.round(Math.abs(handleInArray[i].getY())) === Math.round(Math.abs(handleOutArray[i].getY()))) {
if (handleInArray[i].getX() != 0 || handleInArray[i].getY() != 0 && Math.round(Math.abs(handleInArray[i].getX())) === Math.round(Math.abs(handleOutArray[i].getX())) && Math.round(Math.abs(handleInArray[i].getY())) === Math.round(Math.abs(handleOutArray[i].getY()))) {
checkPointValues = true;
} else {
checkPointValues = false;
}
}
if(checkPointValues == true) {
if (checkPointValues == true) {
//if the distance between (point0 and point2) and (point1 and point3) are equal, then it is a circle
var d1 = Math.round(pointArray[0].getDistance(pointArray[2], false));
var d2 = Math.round(pointArray[1].getDistance(pointArray[3], false));
if(d1 == d2) {
if (d1 == d2) {
type = 'circle';
} else {
type = 'ellipse';
}
}
}
} else if(!curves) {
if(segArray.length == 4) {
} else if (!curves) {
if (segArray.length == 4) {
//if the distance between (point0 and point1) and (point2 and point3) are equal, then it is a rectangle
dPoint12 = Math.round(pointArray[0].getDistance(pointArray[1], false));
dPoint34 = Math.round(pointArray[3].getDistance(pointArray[2], false));
if(dPoint12 == dPoint34) {
if (dPoint12 == dPoint34) {
type = 'rect';
}
} else if(segArray.length >= 3) {
} else if (segArray.length >= 3) {
//If it is an object with more than 3 segments and the path is closed, it is a polygon
if(path.getClosed()) {
if (path.getClosed()) {
type = 'polygon';
} else {
type = 'polyline';

View file

@ -39,7 +39,7 @@ var ImportSvg = this.ImportSvg = Base.extend(/** @Lends ImportSvg# */{
* @param {SVG DOM} svg An SVG DOM object with parameters
* @return {item} A Paper.js layer
*/
importSVG: function(svg) {
importSvg: function(svg) {
var item;
var symbol;
switch (svg.nodeName.toLowerCase()) {
@ -75,12 +75,11 @@ var ImportSvg = this.ImportSvg = Base.extend(/** @Lends ImportSvg# */{
symbol = new Symbol(item);
item = null;
default:
//Not supported yet.
// Not supported yet.
}
if (item) {
if (item)
this._importAttributesAndStyles(svg, item);
}
return item;
},
@ -101,14 +100,12 @@ var ImportSvg = this.ImportSvg = Base.extend(/** @Lends ImportSvg# */{
var child;
for (var i in svg.childNodes) {
child = svg.childNodes[i];
if (child.nodeType != 1) {
if (child.nodeType != 1)
continue;
}
item = this.importSVG(child);
if (item) {
item = this.importSvg(child);
if (item)
group.addChild(item);
}
}
return group;
},
@ -282,7 +279,7 @@ var ImportSvg = this.ImportSvg = Base.extend(/** @Lends ImportSvg# */{
var controlPoint;
var prevCommand;
var segmentTo;
for (var i = 0; i < segments.numberOfItems; ++i){
for (var i = 0; i < segments.numberOfItems; ++i) {
segment = segments.getItem(i);
if (segment.pathSegType == SVGPathSeg.PATHSEG_UNKNOWN) {
continue;
@ -554,7 +551,6 @@ var ImportSvg = this.ImportSvg = Base.extend(/** @Lends ImportSvg# */{
break;
case SVGTransform.SVG_TRANSFORM_SCALE:
break;
//Compensate for SVG's theta rotation going the opposite direction
case SVGTransform.SVG_TRANSFORM_MATRIX:
var temp = transformMatrix.getShearX();

View file

@ -31,7 +31,7 @@ test('make an svg line', function() {
shape.setAttribute('y2', y2);
var isvg = new ImportSvg();
var importedLine = isvg.importSVG(shape);
var importedLine = isvg.importSvg(shape);
var line = new Path.Line([x1, y1], [x2, y2]);
@ -47,7 +47,7 @@ test('make an svg line with invalid values', function() {
shape.setAttribute('y2', null);
var isvg = new ImportSvg();
var importedLine = isvg.importSVG(shape);
var importedLine = isvg.importSvg(shape);
var line = new Path.Line([0, 0], [0, 0]);
@ -68,7 +68,7 @@ test('compare rectangle values', function() {
shape.setAttribute('height', height);
var isvg = new ImportSvg();
var importedRectangle = isvg.importSVG(shape);
var importedRectangle = isvg.importSvg(shape);
var topLeft = new Point(x, y);
var size = new Size(width, height);
@ -92,7 +92,7 @@ test('compare negative rectangle values', function() {
shape.setAttribute('height', height);
var isvg = new ImportSvg();
var importedRectangle = isvg.importSVG(shape);
var importedRectangle = isvg.importSvg(shape);
var topLeft = new Point(x, y);
var size = new Size(width, height);
var rectangle = new Rectangle(topLeft, size);
@ -112,7 +112,7 @@ test('compare invalid rectangle values', function() {
shape.setAttribute('height', null);
var isvg = new ImportSvg();
var importedRectangle = isvg.importSVG(shape);
var importedRectangle = isvg.importSvg(shape);
var topLeft = new Point(0, 0);
var size = new Size(0, 0);
@ -139,7 +139,7 @@ test('compare round rectangle values', function() {
shape.setAttribute('height', height);
var isvg = new ImportSvg();
var importedRectangle = isvg.importSVG(shape);
var importedRectangle = isvg.importSvg(shape);
var topLeft = new Point(x, y);
var size = new Size(width, height);
@ -167,7 +167,7 @@ test('compare negative round rectangle values', function() {
shape.setAttribute('height', height);
var isvg = new ImportSvg();
var importedRectangle = isvg.importSVG(shape);
var importedRectangle = isvg.importSvg(shape);
var topLeft = new Point(x, y);
var size = new Size(width, height);
@ -195,7 +195,7 @@ test('compare invalid round rectangle values', function() {
shape.setAttribute('height', height);
var isvg = new ImportSvg();
var importedRectangle = isvg.importSVG(shape);
var importedRectangle = isvg.importSvg(shape);
var topLeft = new Point(x, y);
var size = new Size(width, height);
@ -219,7 +219,7 @@ test('compare oval values', function() {
shape.setAttribute('ry', ry);
var isvg = new ImportSvg();
var importedOval = isvg.importSVG(shape);
var importedOval = isvg.importSvg(shape);
var center = new Point(cx, cy);
var offset = new Point(rx, ry);
@ -247,7 +247,7 @@ test('compare negative oval values', function() {
shape.setAttribute('ry', ry);
var isvg = new ImportSvg();
var importedOval = isvg.importSVG(shape);
var importedOval = isvg.importSvg(shape);
var center = new Point(cx, cy);
var offset = new Point(rx, ry);
@ -270,7 +270,7 @@ test('compare invalid oval values', function() {
shape.setAttribute('ry', null);
var isvg = new ImportSvg();
var importedOval = isvg.importSVG(shape);
var importedOval = isvg.importSvg(shape);
var center = new Point(0, 0);
var offset = new Point(0, 0);
@ -295,7 +295,7 @@ test('compare circle values', function() {
shape.setAttribute('r', r);
var isvg = new ImportSvg();
var importedCircle = isvg.importSVG(shape);
var importedCircle = isvg.importSvg(shape);
var center = new Point(cx, cy);
var circle = new Path.Circle(center, r);
@ -315,7 +315,7 @@ test('compare negative circle values', function() {
shape.setAttribute('r', r);
var isvg = new ImportSvg();
var importedCircle = isvg.importSVG(shape);
var importedCircle = isvg.importSvg(shape);
var center = new Point(cx, cy);
var circle = new Path.Circle(center, r);
@ -333,7 +333,7 @@ test('compare invalid circle values', function() {
shape.setAttribute('r', null);
var isvg = new ImportSvg();
var importedCircle = isvg.importSVG(shape);
var importedCircle = isvg.importSvg(shape);
var center = new Point(0, 0);
var circle = new Path.Circle(center, 0);
@ -349,7 +349,7 @@ test('compare polygon values', function() {
shape.setAttribute('points', svgpoints);
var isvg = new ImportSvg();
var importedPolygon = isvg.importSVG(shape);
var importedPolygon = isvg.importSvg(shape);
var poly = new Path();
var points = shape.points;
@ -376,7 +376,7 @@ test('compare negative polygon values', function() {
shape.setAttribute('points', svgpoints);
var isvg = new ImportSvg();
var importedPolygon = isvg.importSVG(shape);
var importedPolygon = isvg.importSvg(shape);
var poly = new Path();
var points = shape.points;
@ -403,7 +403,7 @@ test('compare polyline values', function() {
shape.setAttribute('points', svgpoints);
var isvg = new ImportSvg();
var importedPolyline = isvg.importSVG(shape);
var importedPolyline = isvg.importSvg(shape);
var poly = new Path();
var points = shape.points;
@ -430,7 +430,7 @@ test('compare polyline values', function() {
shape.setAttribute('points', svgpoints);
var isvg = new ImportSvg();
var importedPolyline = isvg.importSVG(shape);
var importedPolyline = isvg.importSvg(shape);
var poly = new Path();
var points = shape.points;