2009-07-17 20:08:47 -07:00
|
|
|
// -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
|
|
|
|
/*
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Mobile Browser.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Mozilla Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2009
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Roy Frostig <rfrostig@mozilla.com>
|
2009-08-07 18:08:06 -07:00
|
|
|
* Ben Combee <bcombee@mozilla.com>
|
2009-07-17 20:08:47 -07:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
let Ci = Components.interfaces;
|
|
|
|
|
|
|
|
// -----------------------------------------------------------
|
|
|
|
// General util/convenience tools
|
|
|
|
//
|
|
|
|
|
|
|
|
let Util = {
|
|
|
|
|
|
|
|
bind: function bind(f, thisObj) {
|
|
|
|
return function() {
|
|
|
|
return f.apply(thisObj, arguments);
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
bindAll: function bindAll(instance) {
|
2009-07-17 20:12:10 -07:00
|
|
|
let bind = Util.bind;
|
2009-07-17 20:08:47 -07:00
|
|
|
for (let key in instance)
|
|
|
|
if (instance[key] instanceof Function)
|
|
|
|
instance[key] = bind(instance[key], instance);
|
2009-08-07 18:08:06 -07:00
|
|
|
},
|
2009-07-30 17:31:09 -07:00
|
|
|
|
2009-09-04 22:14:59 -07:00
|
|
|
/** printf-like dump function */
|
|
|
|
dumpf: function dumpf(str) {
|
|
|
|
var args = arguments;
|
|
|
|
var i = 1;
|
|
|
|
dump(str.replace(/%s/g, function() {
|
|
|
|
if (i >= args.length) {
|
|
|
|
throw "dumps received too many placeholders and not enough arguments";
|
|
|
|
}
|
|
|
|
return args[i++].toString();
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
2009-08-12 14:40:43 -07:00
|
|
|
/** Like dump, but each arg is handled and there's an automatic newline */
|
2009-08-07 18:08:06 -07:00
|
|
|
dumpLn: function dumpLn() {
|
2009-09-04 22:14:59 -07:00
|
|
|
for (var i = 0; i < arguments.length; i++) { dump(arguments[i] + " "); }
|
2009-08-07 18:08:06 -07:00
|
|
|
dump("\n");
|
2009-09-01 20:39:39 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/** Executes aFunc after other events have been processed. */
|
|
|
|
executeSoon: function executeSoon(aFunc) {
|
|
|
|
let tm = Cc["@mozilla.org/thread-manager;1"].getService(Ci.nsIThreadManager);
|
|
|
|
tm.mainThread.dispatch({
|
|
|
|
run: function() {
|
|
|
|
aFunc();
|
|
|
|
}
|
|
|
|
}, Ci.nsIThread.DISPATCH_NORMAL);
|
2009-08-07 18:08:06 -07:00
|
|
|
}
|
2009-09-01 20:39:39 -07:00
|
|
|
|
2009-07-17 20:08:47 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-10-15 12:50:32 -07:00
|
|
|
/**
|
|
|
|
* Simple Point class.
|
|
|
|
*
|
|
|
|
* Any method that takes an x and y may also take a point.
|
|
|
|
*/
|
2009-10-19 14:21:23 -07:00
|
|
|
function Point(x, y) {
|
2009-10-15 12:50:32 -07:00
|
|
|
this.set(x, y);
|
|
|
|
}
|
2009-07-17 20:08:47 -07:00
|
|
|
|
2009-10-15 12:50:32 -07:00
|
|
|
Point.prototype = {
|
2009-07-17 20:08:47 -07:00
|
|
|
clone: function clone() {
|
2009-10-15 12:50:32 -07:00
|
|
|
return new Point(this.x, this.y);
|
2009-07-17 20:08:47 -07:00
|
|
|
},
|
|
|
|
|
2009-10-15 12:50:32 -07:00
|
|
|
set: function set(x, y) {
|
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
|
|
|
return this;
|
2009-07-17 20:08:47 -07:00
|
|
|
},
|
2009-10-15 12:50:32 -07:00
|
|
|
|
|
|
|
equals: function equals(x, y) {
|
|
|
|
return this.x == x && this.y == y;
|
2009-07-17 20:08:47 -07:00
|
|
|
},
|
|
|
|
|
2009-10-15 12:50:32 -07:00
|
|
|
toString: function toString() {
|
|
|
|
return "(" + this.x + "," + this.y + ")";
|
2009-07-17 20:08:47 -07:00
|
|
|
},
|
|
|
|
|
2009-10-15 12:50:32 -07:00
|
|
|
map: function map(f) {
|
|
|
|
this.x = f.call(this, this.x);
|
|
|
|
this.y = f.call(this, this.y);
|
2009-07-17 20:08:47 -07:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2009-10-15 12:50:32 -07:00
|
|
|
add: function add(x, y) {
|
|
|
|
this.x += x;
|
|
|
|
this.y += y;
|
2009-07-17 20:08:47 -07:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2009-10-15 12:50:32 -07:00
|
|
|
subtract: function subtract(x, y) {
|
|
|
|
this.x -= x;
|
|
|
|
this.y -= y;
|
2009-07-17 20:08:47 -07:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2009-10-15 12:50:32 -07:00
|
|
|
scale: function scale(s) {
|
|
|
|
this.x *= s;
|
|
|
|
this.y *= s;
|
2009-07-17 20:08:47 -07:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2009-10-15 12:50:32 -07:00
|
|
|
isZero: function() {
|
|
|
|
return this.x == 0 && this.y == 0;
|
2009-07-17 20:08:47 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-10-15 12:50:32 -07:00
|
|
|
(function() {
|
|
|
|
function takePointOrArgs(f) {
|
|
|
|
return function(arg1, arg2) {
|
|
|
|
if (arg2 === undefined)
|
|
|
|
return f.call(this, arg1.x, arg1.y);
|
|
|
|
else
|
|
|
|
return f.call(this, arg1, arg2);
|
|
|
|
};
|
|
|
|
}
|
2009-07-17 20:08:47 -07:00
|
|
|
|
2009-10-15 12:50:32 -07:00
|
|
|
for each (let f in ['add', 'subtract', 'equals', 'set'])
|
|
|
|
Point.prototype[f] = takePointOrArgs(Point.prototype[f]);
|
|
|
|
})();
|
2009-07-17 20:08:47 -07:00
|
|
|
|
2009-10-15 12:50:32 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Rect is a simple data structure for representation of a rectangle supporting
|
|
|
|
* many basic geometric operations.
|
|
|
|
*
|
|
|
|
* NOTE: Since its operations are closed, rectangles may be empty and will report
|
|
|
|
* non-positive widths and heights in that case.
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Rect(x, y, w, h) {
|
2009-07-17 20:08:47 -07:00
|
|
|
this.left = x;
|
|
|
|
this.top = y;
|
|
|
|
this.right = x+w;
|
|
|
|
this.bottom = y+h;
|
2009-10-15 12:50:32 -07:00
|
|
|
};
|
2009-07-17 20:08:47 -07:00
|
|
|
|
2009-10-15 12:50:32 -07:00
|
|
|
Rect.fromRect = function fromRect(r) {
|
|
|
|
return new Rect(r.left, r.top, r.right - r.left, r.bottom - r.top);
|
|
|
|
}
|
2009-07-17 20:08:47 -07:00
|
|
|
|
2009-10-15 12:50:32 -07:00
|
|
|
Rect.prototype = {
|
2009-07-17 20:08:47 -07:00
|
|
|
get x() { return this.left; },
|
|
|
|
get y() { return this.top; },
|
|
|
|
get width() { return this.right - this.left; },
|
|
|
|
get height() { return this.bottom - this.top; },
|
|
|
|
set x(v) {
|
|
|
|
let diff = this.left - v;
|
|
|
|
this.left = v;
|
|
|
|
this.right -= diff;
|
|
|
|
},
|
|
|
|
set y(v) {
|
|
|
|
let diff = this.top - v;
|
|
|
|
this.top = v;
|
|
|
|
this.bottom -= diff;
|
|
|
|
},
|
|
|
|
set width(v) { this.right = this.left + v; },
|
|
|
|
set height(v) { this.bottom = this.top + v; },
|
|
|
|
|
2009-10-15 12:50:32 -07:00
|
|
|
isEmpty: function isEmpty() {
|
|
|
|
return this.left >= this.right || this.top >= this.bottom;
|
|
|
|
},
|
|
|
|
|
2009-07-17 20:08:47 -07:00
|
|
|
setRect: function(x, y, w, h) {
|
|
|
|
this.left = x;
|
|
|
|
this.top = y;
|
|
|
|
this.right = x+w;
|
|
|
|
this.bottom = y+h;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
setBounds: function(t, l, b, r) {
|
|
|
|
this.top = t;
|
|
|
|
this.left = l;
|
|
|
|
this.bottom = b;
|
|
|
|
this.right = r;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2009-10-15 12:50:32 -07:00
|
|
|
equals: function equals(other) {
|
|
|
|
return (other != null &&
|
|
|
|
this.top == other.top &&
|
|
|
|
this.left == other.left &&
|
|
|
|
this.bottom == other.bottom &&
|
|
|
|
this.right == other.right);
|
2009-07-17 20:08:47 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
clone: function clone() {
|
2009-10-15 12:50:32 -07:00
|
|
|
return new Rect(this.left, this.top, this.right - this.left, this.bottom - this.top);
|
2009-07-17 20:08:47 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
center: function center() {
|
2009-10-15 12:50:32 -07:00
|
|
|
if (this.isEmpty())
|
|
|
|
throw "Empty rectangles do not have centers";
|
|
|
|
return new Point(this.left + (this.right - this.left) / 2,
|
|
|
|
this.top + (this.bottom - this.top) / 2);
|
2009-07-17 20:08:47 -07:00
|
|
|
},
|
|
|
|
|
2009-10-15 12:50:32 -07:00
|
|
|
copyFrom: function(other) {
|
|
|
|
this.top = other.top;
|
|
|
|
this.left = other.left;
|
|
|
|
this.bottom = other.bottom;
|
|
|
|
this.right = other.right;
|
2009-07-17 20:08:47 -07:00
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
translate: function(x, y) {
|
|
|
|
this.left += x;
|
|
|
|
this.right += x;
|
|
|
|
this.top += y;
|
|
|
|
this.bottom += y;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
toString: function() {
|
|
|
|
return "[" + this.x + "," + this.y + "," + this.width + "," + this.height + "]";
|
|
|
|
},
|
|
|
|
|
2009-10-15 12:50:32 -07:00
|
|
|
/** return a new rect that is the union of that one and this one */
|
|
|
|
union: function(other) {
|
|
|
|
return this.clone().expandToContain(other);
|
2009-07-17 20:08:47 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
contains: function(other) {
|
2009-10-15 12:50:32 -07:00
|
|
|
if (other.isEmpty()) return true;
|
|
|
|
if (this.isEmpty()) return false;
|
2009-07-17 20:08:47 -07:00
|
|
|
|
2009-10-15 12:50:32 -07:00
|
|
|
return (other.left >= this.left &&
|
|
|
|
other.right <= this.right &&
|
|
|
|
other.top >= this.top &&
|
|
|
|
other.bottom <= this.bottom);
|
2009-07-17 20:08:47 -07:00
|
|
|
},
|
|
|
|
|
2009-10-15 12:50:32 -07:00
|
|
|
intersect: function(other) {
|
|
|
|
return this.clone().restrictTo(other);
|
2009-07-17 20:08:47 -07:00
|
|
|
},
|
|
|
|
|
2009-10-15 12:50:32 -07:00
|
|
|
intersects: function(other) {
|
|
|
|
if (this.isEmpty() || other.isEmpty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
let x1 = Math.max(this.left, other.left);
|
|
|
|
let x2 = Math.min(this.right, other.right);
|
|
|
|
let y1 = Math.max(this.top, other.top);
|
|
|
|
let y2 = Math.min(this.bottom, other.bottom);
|
|
|
|
return x1 < x2 && y1 < y2;
|
|
|
|
},
|
|
|
|
|
|
|
|
/** Restrict area of this rectangle to the intersection of both rectangles. */
|
|
|
|
restrictTo: function restrictTo(other) {
|
|
|
|
if (this.isEmpty() || other.isEmpty())
|
|
|
|
return this.setRect(0, 0, 0, 0);
|
|
|
|
|
|
|
|
let x1 = Math.max(this.left, other.left);
|
|
|
|
let x2 = Math.min(this.right, other.right);
|
|
|
|
let y1 = Math.max(this.top, other.top);
|
|
|
|
let y2 = Math.min(this.bottom, other.bottom);
|
|
|
|
// If width or height is 0, the intersection was empty.
|
|
|
|
return this.setRect(x1, y1, Math.max(0, x2 - x1), Math.max(0, y2 - y1));
|
|
|
|
},
|
|
|
|
|
|
|
|
/** Expand this rectangle to the union of both rectangles. */
|
|
|
|
expandToContain: function expandToContain(other) {
|
|
|
|
if (this.isEmpty()) return this.copyFrom(other);
|
|
|
|
if (other.isEmpty()) return this;
|
|
|
|
|
|
|
|
let l = Math.min(this.left, other.left);
|
|
|
|
let r = Math.max(this.right, other.right);
|
|
|
|
let t = Math.min(this.top, other.top);
|
|
|
|
let b = Math.max(this.bottom, other.bottom);
|
|
|
|
return this.setRect(l, t, r-l, b-t);
|
2009-07-17 20:08:47 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2009-10-15 12:50:32 -07:00
|
|
|
* Expands to the smallest rectangle that contains original rectangle and is bounded
|
|
|
|
* by lines with integer coefficients.
|
2009-07-17 20:08:47 -07:00
|
|
|
*/
|
2009-10-15 12:50:32 -07:00
|
|
|
expandToIntegers: function round() {
|
|
|
|
this.left = Math.floor(this.left);
|
|
|
|
this.top = Math.floor(this.top);
|
|
|
|
this.right = Math.ceil(this.right);
|
|
|
|
this.bottom = Math.ceil(this.bottom);
|
2009-07-17 20:08:47 -07:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
scale: function scale(xscl, yscl) {
|
|
|
|
this.left *= xscl;
|
|
|
|
this.right *= xscl;
|
|
|
|
this.top *= yscl;
|
|
|
|
this.bottom *= yscl;
|
2009-10-15 12:50:32 -07:00
|
|
|
return this;
|
|
|
|
},
|
2009-07-17 20:08:47 -07:00
|
|
|
|
2009-10-15 12:50:32 -07:00
|
|
|
map: function map(f) {
|
|
|
|
this.left = f.call(this, this.left);
|
|
|
|
this.top = f.call(this, this.top);
|
|
|
|
this.right = f.call(this, this.right);
|
|
|
|
this.bottom = f.call(this, this.bottom);
|
2009-07-17 20:08:47 -07:00
|
|
|
return this;
|
2009-10-21 16:11:38 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/** Ensure this rectangle is inside the other, if possible. Preserves w, h. */
|
|
|
|
translateInside: function translateInside(other) {
|
|
|
|
let offsetX = (this.left < other.left ? other.left - this.left :
|
|
|
|
(this.right > other.right ? this.right - other.right : 0));
|
|
|
|
let offsetY = (this.top < other.top ? other.top - this.top :
|
|
|
|
(this.bottom > other.bottom ? this.bottom - other.bottom : 0));
|
|
|
|
return this.translate(offsetX, offsetY);
|
2009-07-17 20:08:47 -07:00
|
|
|
}
|
|
|
|
};
|