public PageBox getPage(CssContext c, int yOffset) {
List pages = getPages();
if (yOffset < 0) {
return null;
} else {
PageBox lastRequested = getLastRequestedPage();
if (lastRequested != null) {
if (yOffset >= lastRequested.getTop() && yOffset < lastRequested.getBottom()) {
return lastRequested;
}
}
PageBox last = (PageBox) pages.get(pages.size()-1);
if (yOffset < last.getBottom()) {
// The page we're looking for is probably at the end of the
// document so do a linear search for the first few pages
// and then fall back to a binary search if that doesn't work
// out
int count = pages.size();
for (int i = count-1; i >= 0 && i >= count-5; i--) {
PageBox pageBox = (PageBox)pages.get(i);
if (yOffset >= pageBox.getTop() && yOffset < pageBox.getBottom()) {
setLastRequestedPage(pageBox);
return pageBox;
}
}
int low = 0;
int high = count-6;
while (low <= high) {
int mid = (low + high) >> 1;
PageBox pageBox = (PageBox)pages.get(mid);
if (yOffset >= pageBox.getTop() && yOffset < pageBox.getBottom()) {
setLastRequestedPage(pageBox);
return pageBox;
}
if (pageBox.getTop() < yOffset) {
low = mid + 1;
} else {
high = mid - 1;
}
}
} else {
addPagesUntilPosition(c, yOffset);
PageBox result = (PageBox) pages.get(pages.size()-1);
setLastRequestedPage(result);
return result;
}
}