Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(293)

Unified Diff: chrome/browser/resources/pdf/viewport.js

Issue 170163003: Convert search for most visible page in the document to a binary search. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/resources/pdf/viewport.js
diff --git a/chrome/browser/resources/pdf/viewport.js b/chrome/browser/resources/pdf/viewport.js
index 44d7fa18e43b5ab8188a213366c21077a2ccb812..3482f7350296c6a844bb0dcbe534479db4355ed9 100644
--- a/chrome/browser/resources/pdf/viewport.js
+++ b/chrome/browser/resources/pdf/viewport.js
@@ -154,19 +154,43 @@ Viewport.prototype = {
},
/**
+ * @private
+ * @param {integer} y the y-cordinate to get the page at.
+ * @return {integer} the index of a page overlapping the given y-coordinate.
+ */
+ getPageAtY_: function(y) {
+ var min = 0;
+ var max = this.pageDimensions_.length - 1;
+ while (max >= min) {
+ var page = Math.floor(min + ((max - min) / 2));
+ var top = this.pageDimensions_[page].y;
+ var bottom = top + this.pageDimensions_[page].height;
+ if (top <= y && bottom > y)
+ return page;
+ else if (top > y)
+ max = page - 1;
+ else
+ min = page + 1;
+ }
+ return 0;
+ },
+
+ /**
* Returns the page with the most pixels in the current viewport.
* @return {int} the index of the most visible page.
*/
getMostVisiblePage: function() {
- // TODO(raymes): Do a binary search here.
+ var firstVisiblePage = this.getPageAtY_(this.getCurrentViewportRect_().y);
var mostVisiblePage = {'number': 0, 'area': 0};
- for (var i = 0; i < this.pageDimensions_.length; i++) {
+ for (var i = firstVisiblePage; i < this.pageDimensions_.length; i++) {
var area = getIntersectionArea(this.pageDimensions_[i],
this.getCurrentViewportRect_());
if (area > mostVisiblePage.area) {
mostVisiblePage.area = area;
mostVisiblePage.number = i;
}
+ if (area == 0)
koz (OOO until 15th September) 2014/02/18 07:04:34 nit: Maybe move this before the if statement and a
raymes 2014/02/19 05:39:37 Done.
+ break;
}
return mostVisiblePage.number;
},
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698