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

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..13e2adc99532c9934cc659205752dc5d9899e3ad 100644
--- a/chrome/browser/resources/pdf/viewport.js
+++ b/chrome/browser/resources/pdf/viewport.js
@@ -154,15 +154,41 @@ Viewport.prototype = {
},
/**
+ * @private
+ * @param {integer} y the y-coordinate 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 we hit a page with 0 area overlap, we must have gone past the
+ // pages visible in the viewport so we can break.
+ if (area == 0)
+ break;
if (area > mostVisiblePage.area) {
mostVisiblePage.area = area;
mostVisiblePage.number = i;
« 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