Clean up recently caused Base.read() mess by introducing _readNull property,m setting it to true on Color and reverting to previous behavior for Point, Size and Rectangle, where dimensions of 0 are read by default if nothing is provided.

This commit is contained in:
Jürg Lehni 2011-03-13 18:31:00 +01:00
parent 1cd6daada1
commit 9d4af0ce9e
14 changed files with 144 additions and 191 deletions

View file

@ -139,11 +139,10 @@ var Matrix = this.Matrix = Base.extend({
* @return {Matrix} This affine transform.
*/
translate: function(point) {
if (point = Point.read(arguments)) {
var x = point.x, y = point.y;
this._m02 += x * this._m00 + y * this._m01;
this._m12 += x * this._m10 + y * this._m11;
}
point = Point.read(arguments);
var x = point.x, y = point.y;
this._m02 += x * this._m00 + y * this._m01;
this._m12 += x * this._m10 + y * this._m11;
return this;
},
@ -303,14 +302,12 @@ var Matrix = this.Matrix = Base.extend({
}
return dst;
} else if (arguments.length > 0) {
var point = Point.read(arguments);
if (point) {
var x = point.x, y = point.y;
return Point.create(
x * this._m00 + y * this._m01 + this._m02,
x * this._m10 + y * this._m11 + this._m12
);
}
var point = Point.read(arguments),
x = point.x, y = point.y;
return Point.create(
x * this._m00 + y * this._m01 + this._m02,
x * this._m10 + y * this._m11 + this._m12
);
}
return null;
},
@ -417,10 +414,8 @@ var Matrix = this.Matrix = Base.extend({
* @return {Matrix} This affine transform.
*/
setToTranslation: function(delta) {
if (delta = Point.read(arguments)) {
this.set(1, 0, 0, 1, delta.x, delta.y);
}
return this;
delta = Point.read(arguments);
return this.set(1, 0, 0, 1, delta.x, delta.y);
},
/**
@ -443,17 +438,15 @@ var Matrix = this.Matrix = Base.extend({
* @return {Matrix} This affine transform.
*/
setToRotation: function(angle, center) {
if (center = Point.read(arguments, 1)) {
angle = angle * Math.PI / 180.0;
var x = center.x,
y = center.y,
cos = Math.cos(angle),
sin = Math.sin(angle);
this.set(cos, sin, -sin, cos,
x - x * cos + y * sin,
y - x * sin - y * cos);
}
return this;
center = Point.read(arguments, 1);
angle = angle * Math.PI / 180.0;
var x = center.x,
y = center.y,
cos = Math.cos(angle),
sin = Math.sin(angle);
return this.set(cos, sin, -sin, cos,
x - x * cos + y * sin,
y - x * sin - y * cos);
},
/**

View file

@ -78,33 +78,28 @@ var Point = this.Point = Base.extend({
},
add: function(point) {
return (point = Point.read(arguments))
? Point.create(this.x + point.x, this.y + point.y)
: null;
point = Point.read(arguments);
return Point.create(this.x + point.x, this.y + point.y);
},
subtract: function(point) {
return (point = Point.read(arguments))
? Point.create(this.x - point.x, this.y - point.y)
: null;
point = Point.read(arguments);
return Point.create(this.x - point.x, this.y - point.y);
},
multiply: function(point) {
return (point = Point.read(arguments))
? Point.create(this.x * point.x, this.y * point.y)
: null;
point = Point.read(arguments);
return Point.create(this.x * point.x, this.y * point.y);
},
divide: function(point) {
return (point = Point.read(arguments))
? Point.create(this.x / point.x, this.y / point.y)
: null;
point = Point.read(arguments);
return Point.create(this.x / point.x, this.y / point.y);
},
modulo: function(point) {
return (point = Point.read(arguments))
? Point.create(this.x % point.x, this.y % point.y)
: null;
point = Point.read(arguments);
return Point.create(this.x % point.x, this.y % point.y);
},
negate: function() {
@ -112,8 +107,8 @@ var Point = this.Point = Base.extend({
},
equals: function(point) {
return (point = Point.read(arguments))
&& this.x == point.x && this.y == point.y;
point = Point.read(arguments);
return this.x == point.x && this.y == point.y;
},
transform: function(matrix) {
@ -137,19 +132,17 @@ var Point = this.Point = Base.extend({
* @param py
*/
getDistance: function(point) {
if (!(point = Point.read(arguments)))
return null;
var px = point.x - this.x;
var py = point.y - this.y;
return Math.sqrt(px * px + py * py);
point = Point.read(arguments);
var x = point.x - this.x,
y = point.y - this.y;
return Math.sqrt(x * x + y * y);
},
getDistanceSquared: function(point) {
if (!(point = Point.read(arguments)))
return null;
var px = point.x - this.x;
var py = point.y - this.y;
return px * px + py * py;
point = Point.read(arguments);
var x = point.x - this.x,
y = point.y - this.y;
return x * x + y * y;
},
/**
@ -273,17 +266,15 @@ var Point = this.Point = Base.extend({
* @param point
*/
getDirectedAngle: function(point) {
if (!(point = Point.read(arguments)))
return null;
point = Point.read(arguments);
var angle = this.getAngle() - point.getAngle();
var bounds = 180;
if (angle < - bounds) {
return angle + bounds * 2;
} else if (angle > bounds) {
return angle - bounds * 2;
} else {
return angle;
}
return angle;
},
/**
@ -452,9 +443,8 @@ var Point = this.Point = Base.extend({
* @return the dot product of the two points
*/
dot: function(point) {
return (point = Point.read(arguments))
? this.x * point.x + this.y * point.y
: null;
point = Point.read(arguments);
return this.x * point.x + this.y * point.y;
},
/**
@ -463,9 +453,8 @@ var Point = this.Point = Base.extend({
* @return the cross product of the two points
*/
cross: function(point) {
return (point = Point.read(arguments))
? this.x * point.y - this.y * point.x
: null;
point = Point.read(arguments);
return this.x * point.y - this.y * point.x;
},
/**
@ -476,8 +465,7 @@ var Point = this.Point = Base.extend({
* @return the project of the point on another point
*/
project: function(point) {
if (!(point = Point.read(arguments)))
return null;
point = Point.read(arguments);
if (point.isZero()) {
return Point.create(0, 0);
} else {

View file

@ -75,8 +75,7 @@ var Rectangle = this.Rectangle = Base.extend({
},
setPoint: function(point) {
if (!(point = Point.read(arguments)))
return this;
point = Point.read(arguments);
this.x = point.x;
this.y = point.y;
return this;
@ -87,8 +86,7 @@ var Rectangle = this.Rectangle = Base.extend({
},
setSize: function(size) {
if (!(size = Size.read(arguments)))
return this;
size = Size.read(arguments);
this.width = size.width;
this.height = size.height;
return this;
@ -156,8 +154,7 @@ var Rectangle = this.Rectangle = Base.extend({
},
setCenter: function(point) {
if (!(point = Point.read(arguments)))
return this;
point = Point.read(arguments);
return this.setCenterX(point.x).setCenterY(point.y);
},
@ -197,8 +194,7 @@ var Rectangle = this.Rectangle = Base.extend({
},
intersect: function(rect) {
if (!(rect = Rectangle.read(arguments)))
return null;
rect = Rectangle.read(arguments);
var x1 = Math.max(this.x, rect.x),
y1 = Math.max(this.y, rect.y),
x2 = Math.min(this.x + this.width, rect.x + rect.width),
@ -207,8 +203,7 @@ var Rectangle = this.Rectangle = Base.extend({
},
unite: function(rect) {
if (!(rect = Rectangle.read(arguments)))
return null;
rect = Rectangle.read(arguments);
var x1 = Math.min(this.x, rect.x),
y1 = Math.min(this.y, rect.y),
x2 = Math.max(this.x + this.width, rect.x + rect.width),
@ -217,8 +212,7 @@ var Rectangle = this.Rectangle = Base.extend({
},
include: function(point) {
if (!(point = Point.read(arguments)))
return null;
point = Point.read(arguments);
var x1 = Math.min(this.x, point.x),
y1 = Math.min(this.y, point.y),
x2 = Math.max(this.x + this.width, point.x),
@ -265,8 +259,7 @@ var Rectangle = this.Rectangle = Base.extend({
return Point.create(this[getX](), this[getY]());
};
this['set' + key] = function(point) {
if (!(point = Point.read(arguments)))
return this;
point = Point.read(arguments);
return this[setX](point.x)[setY](point.y); // Note: call chaining!
};
}, { beans: true });

View file

@ -47,44 +47,38 @@ var Size = this.Size = Base.extend({
return this;
},
add: function() {
return (size = Size.read(arguments))
? Size.create(this.width + size.width, this.height + size.height)
: null;
add: function(size) {
size = Size.read(arguments);
return Size.create(this.width + size.width, this.height + size.height);
},
subtract: function() {
return (size = Size.read(arguments))
? Size.create(this.width - size.width, this.height - size.height)
: null;
;
subtract: function(size) {
size = Size.read(arguments);
return Size.create(this.width - size.width, this.height - size.height);
},
multiply: function() {
return (size = Size.read(arguments))
? Size.create(this.width * size.width, this.height * size.height)
: null;
multiply: function(size) {
size = Size.read(arguments);
return Size.create(this.width * size.width, this.height * size.height);
},
divide: function() {
return (size = Size.read(arguments))
? Size.create(this.width / size.width, this.height / size.height)
: null;
divide: function(size) {
size = Size.read(arguments);
return Size.create(this.width / size.width, this.height / size.height);
},
modulo: function() {
return (size = Size.read(arguments))
? Size.create(this.width % size.width, this.height % size.height)
: null;
modulo: function(size) {
size = Size.read(arguments);
return Size.create(this.width % size.width, this.height % size.height);
},
negate: function() {
return Size.create(-this.width, -this.height);
},
equals: function() {
return (size = Size.read(arguments))
&& this.width == size.width && this.height == size.height;
equals: function(size) {
size = Size.read(arguments);
return this.width == size.width && this.height == size.height;
},
isNaN: function() {

View file

@ -88,6 +88,7 @@ var Color = this.Color = Base.extend(new function() {
return {
beans: true,
_readNull: true,
initialize: function(arg) {
var isArray = Array.isArray(arg);

View file

@ -30,11 +30,10 @@ var GradientColor = this.GradientColor = Color.extend({
},
setOrigin: function(origin) {
if (origin = Point.read(arguments)) {
this._origin = origin;
if (this._destination)
this._radius = this._destination.getDistance(this._origin);
}
origin = Point.read(arguments);
this._origin = origin;
if (this._destination)
this._radius = this._destination.getDistance(this._origin);
return this;
},
@ -43,10 +42,9 @@ var GradientColor = this.GradientColor = Color.extend({
},
setDestination: function(destination) {
if (destination = Point.read(arguments)) {
this._destination = destination;
this._radius = this._destination.getDistance(this._origin);
}
destination = Point.read(arguments);
this._destination = destination;
this._radius = this._destination.getDistance(this._origin);
return this;
},
@ -55,14 +53,13 @@ var GradientColor = this.GradientColor = Color.extend({
},
setHilite: function(hilite) {
if (hilite = Point.read(arguments)) {
var vector = hilite.subtract(this._origin);
if (vector.getLength() > this._radius) {
this._hilite = this._origin.add(vector.normalize(
this._radius - 0.1));
} else {
this._hilite = hilite;
}
hilite = Point.read(arguments);
var vector = hilite.subtract(this._origin);
if (vector.getLength() > this._radius) {
this._hilite = this._origin.add(vector.normalize(
this._radius - 0.1));
} else {
this._hilite = hilite;
}
return this;
},

View file

@ -30,13 +30,11 @@ var DocumentView = this.DocumentView = Base.extend({
return this._center;
},
setCenter: function() {
var center = Point.read(arguments);
if (center) {
var delta = center.subtract(this._center);
this.scrollBy(delta);
this._center = center;
}
setCenter: function(center) {
center = Point.read(arguments);
var delta = center.subtract(this._center);
this.scrollBy(delta);
this._center = center;
},
getZoom: function() {
@ -51,10 +49,9 @@ var DocumentView = this.DocumentView = Base.extend({
this._zoom = zoom;
},
scrollBy: function() {
var point = Point.read(arguments).negate();
var mx = new Matrix().translate(point);
this.transform(mx);
scrollBy: function(point) {
point = Point.read(arguments);
this.transform(new Matrix().translate(point.negate()));
},
transform: function(matrix, flags) {

View file

@ -394,14 +394,12 @@ var Item = this.Item = Base.extend({
},
setBounds: function(rect) {
var bounds = this.getBounds();
rect = Rectangle.read(arguments);
if (!rect)
return;
var matrix = new Matrix();
var bounds = this.getBounds(),
matrix = new Matrix(),
center = rect.center;
// Read this from bottom to top:
// Translate to new center:
var center = rect.center;
matrix.translate(center);
// Scale to new Size, if size changes and avoid divisions by 0:
if (rect.width != bounds.width || rect.height != bounds.height) {

View file

@ -122,17 +122,17 @@ var Raster = this.Raster = Item.extend({
this._bounds = null;
},
getSubImage: function(/* rectangle */) {
var rectangle = Rectangle.read(arguments),
canvas = CanvasProvider.getCanvas(rectangle.getSize()),
getSubImage: function(rect) {
rect = Rectangle.read(arguments);
var canvas = CanvasProvider.getCanvas(rect.getSize()),
context = canvas.getContext('2d');
context.drawImage(this.getCanvas(), rectangle.x, rectangle.y,
context.drawImage(this.getCanvas(), rect.x, rect.y,
canvas.width, canvas.height, 0, 0, canvas.width, canvas.height);
return canvas;
},
drawImage: function(image, point) {
var point = Point.read(arguments, 1);
point = Point.read(arguments, 1);
this.getContext().drawImage(image, point.x, point.y);
},
@ -143,9 +143,9 @@ var Raster = this.Raster = Item.extend({
* @param x
* @param y
*/
getPixel: function() {
var point = Point.read(arguments),
ctx = this.getContext(),
getPixel: function(point) {
point = Point.read(arguments);
var ctx = this.getContext(),
pixels = ctx.getImageData(point.x, point.y, 1, 1).data,
channels = new Array(4);
for (var i = 0; i < 4; i++)
@ -156,6 +156,8 @@ var Raster = this.Raster = Item.extend({
// TODO: setPixel(point, color)
setPixel: function(x, y, color) {
color = Color.read(arguments, 2);
// TODO: Instead of getting image data, use context.createImageData()
// TODO: Cache imageData used for setting pixel, so it cna be reused.
var ctx = this.getContext(),
imageData = ctx.getImageData(x, y, 1, 1),
alpha = color.getAlpha();
@ -171,16 +173,16 @@ var Raster = this.Raster = Item.extend({
return this.getContext().createImageData(size.width, size.height);
},
getData: function(/* [rect] */) {
var rect = Rectangle.read(arguments),
ctx = this.getContext();
getData: function(rect) {
rect = Rectangle.read(arguments);
var ctx = this.getContext();
rect = rect.isEmpty() ? new Rectangle(this.getSize()) : rect;
return ctx.getImageData(rect.x, rect.y, rect.width, rect.height);
},
setData: function(data /* [, point] */) {
var point = Rectangle.read(arguments, 1),
ctx = this.getContext();
setData: function(data, point) {
point = Point.read(arguments, 1);
var ctx = this.getContext();
ctx.putImageData(data, point.x, point.y);
},

View file

@ -55,8 +55,9 @@ Base.inject({
read: function(args, index, length) {
var index = index || 0, length = length || args.length - index;
var arg = args[index];
// Return null when nothing was provided
if (arg instanceof this || arg == null && length <= 1)
// If the class defines _readNull, return null when nothing was provided
if (arg instanceof this
|| this.prototype._readNull && arg == null && length <= 1)
return arg;
var obj = new this(this.dont);
obj = obj.initialize.apply(obj, index > 0 || length < args.length

View file

@ -60,8 +60,8 @@ var Curve = this.Curve = Base.extend({
return this._segment1._point;
},
setPoint1: function() {
var point = Point.read(arguments);
setPoint1: function(point) {
point = Point.read(arguments);
this._segment1._point.set(point.x, point.y);
},
@ -72,8 +72,8 @@ var Curve = this.Curve = Base.extend({
return this._segment2._point;
},
setPoint2: function() {
var point = Point.read(arguments);
setPoint2: function(point) {
point = Point.read(arguments);
this._segment2._point.set(point.x, point.y);
},
@ -84,8 +84,8 @@ var Curve = this.Curve = Base.extend({
return this._segment1._handleOut;
},
setHandle1: function() {
var point = Point.read(arguments);
setHandle1: function(point) {
point = Point.read(arguments);
this._segment1._handleOut.set(point.x, point.y);
},
@ -96,8 +96,8 @@ var Curve = this.Curve = Base.extend({
return this._segment2._handleIn;
},
setHandle2: function() {
var point = Point.read(arguments);
setHandle2: function(point) {
point = Point.read(arguments);
this._segment2._handleIn.set(point.x, point.y);
},

View file

@ -26,18 +26,15 @@ Path.inject({ statics: new function() {
return {
Line: function() {
if (arguments.length >= 2) {
var step = Math.floor(arguments.length / 2);
return new Path(
Segment.read(arguments, 0, step),
Segment.read(arguments, step, step)
);
}
var step = Math.floor(arguments.length / 2);
return new Path(
Segment.read(arguments, 0, step),
Segment.read(arguments, step, step)
);
},
Rectangle: function(rect) {
if (!(rect = Rectangle.read(arguments)))
return null;
rect = Rectangle.read(arguments);
var path = new Path(),
corners = ['getBottomLeft', 'getTopLeft', 'getTopRight',
'getBottomRight'];
@ -56,8 +53,6 @@ Path.inject({ statics: new function() {
rect = Rectangle.read(arguments, 0, 4);
size = Size.read(arguments, 4, 2);
}
if (!rect || !size)
return null;
size = Size.min(size, rect.getSize().divide(2));
var path = new Path(),
uSize = size.multiply(kappa * 2),
@ -84,8 +79,7 @@ Path.inject({ statics: new function() {
},
Oval: function(rect) {
if (!(rect = Rectangle.read(arguments)))
return null;
rect = Rectangle.read(arguments);
var path = new Path(),
topLeft = rect.getTopLeft(),
size = new Size(rect.width, rect.height);
@ -108,8 +102,6 @@ Path.inject({ statics: new function() {
} else {
center = Point.read(arguments, 0, 1);
}
if (!center || !radius)
return null;
return Path.Oval(new Rectangle(center.subtract(radius),
new Size(radius * 2, radius * 2)));
},
@ -122,8 +114,7 @@ Path.inject({ statics: new function() {
},
RegularPolygon: function(center, numSides, radius) {
if (!(center = Point.read(arguments, 0)))
return null;
center = Point.read(arguments, 0);
var path = new Path(),
three = !(numSides % 3),
vector = new Point(0, three ? -radius : radius),

View file

@ -529,12 +529,10 @@ var Path = this.Path = PathItem.extend({
}
},
lineBy: function() {
var vector = Point.read(arguments);
if (vector) {
var current = getCurrentSegment(this);
this.lineTo(current._point.add(vector));
}
lineBy: function(vector) {
vector = Point.read(arguments);
var current = getCurrentSegment(this);
this.lineTo(current._point.add(vector));
},
curveBy: function(throughVector, toVector, parameter) {

View file

@ -55,10 +55,10 @@ var Segment = this.Segment = Base.extend({
return this._point;
},
setPoint: function() {
setPoint: function(point) {
point = Point.read(arguments);
// Do not replace the internal object but update it instead, so
// references to it are kept alive.
var point = Point.read(arguments);
this._point.set(point.x, point.y);
},
@ -66,9 +66,9 @@ var Segment = this.Segment = Base.extend({
return this._handleIn;
},
setHandleIn: function() {
setHandleIn: function(point) {
point = Point.read(arguments);
// See #setPoint:
var point = Point.read(arguments);
this._handleIn.set(point.x, point.y);
// Update corner accordingly
// this.corner = !this._handleIn.isParallel(this._handleOut);
@ -83,9 +83,9 @@ var Segment = this.Segment = Base.extend({
return this._handleOut;
},
setHandleOut: function() {
setHandleOut: function(point) {
point = Point.read(arguments);
// See #setPoint:
var point = Point.read(arguments);
this._handleOut.set(point.x, point.y);
// Update corner accordingly
// this.corner = !this._handleIn.isParallel(this._handleOut);