update pdf.js to improve range request support in chrome

This commit is contained in:
j 2013-03-09 11:39:48 +05:30
parent e3f43007a4
commit 3e99e43015
2 changed files with 345 additions and 341 deletions

View file

@ -16,8 +16,8 @@
*/
var PDFJS = {};
PDFJS.version = '0.7.290';
PDFJS.build = '009bc18';
PDFJS.version = '0.7.315';
PDFJS.build = '547a8d6';
(function pdfjsWrapper() {
// Use strict in our context only - users might not want it
@ -46,6 +46,7 @@ PDFJS.build = '009bc18';
var NetworkManager = (function NetworkManagerClosure() {
function NetworkManager(url, args) {
args = args || {};
this.url = url;
this.httpHeaders = args.httpHeaders;
this.getXhr = args.getXhr ||
@ -54,8 +55,8 @@ var NetworkManager = (function NetworkManagerClosure() {
};
this.currXhrId = 0;
this.xhrRequests = {};
this.loadedXhrs = {};
this.pendingRequests = {};
this.loadedRequests = {};
}
function getArrayBuffer(xhr) {
@ -78,9 +79,8 @@ var NetworkManager = (function NetworkManagerClosure() {
// e.g. If we already have chunk 3, and we need chunks [2, 5), do not
// fetch 2; instead fetch [2, 3) and [4, 5); Realistically though, this
// should not happen often w/ PDFs
// TODO(mack): this optimization might do more harm than good; investigate
requestRange: function NetworkManager_requestRange(begin, end, callback) {
this.request({
return this.request({
begin: begin,
end: end,
onDone: callback
@ -88,13 +88,13 @@ var NetworkManager = (function NetworkManagerClosure() {
},
requestFull: function NetworkManager_requestRange(args) {
this.request(args);
return this.request(args);
},
request: function NetworkManager_requestRange(args) {
var xhr = this.getXhr();
var xhrId = this.currXhrId++;
this.xhrRequests[xhrId] = xhr;
this.pendingRequests[xhrId] = xhr;
xhr.open('GET', this.url);
if (this.httpHeaders) {
@ -131,35 +131,33 @@ var NetworkManager = (function NetworkManagerClosure() {
xhr.onDone = args.onDone;
xhr.send(null);
return xhrId;
},
onStateChange: function NetworkManager_onStateChange(xhrId, evt) {
// xhr seems to queue requests for different readyState,
// so that we come here multiple times; the problem is when we pause
// via debugger, then, xhr.readyState will be 4 multiple times. This
// is a fix for this issue.
if (xhrId in this.loadedXhrs) {
return;
}
var xhr = this.xhrRequests[xhrId];
var xhr = this.pendingRequests[xhrId];
if (!xhr) {
// Maybe abortRequest was called...
return;
}
if (xhr.readyState >= 2 && xhr.onHeadersReceived) {
xhr.onHeadersReceived({
xhrId: xhrId
});
xhr.onHeadersReceived();
delete xhr.onHeadersReceived;
return;
} else if (xhr.readyState !== 4) {
}
if (xhr.readyState !== 4) {
return;
}
this.loadedXhrs[xhrId] = true;
delete this.xhrRequests[xhrId];
if (!(xhrId in this.pendingRequests)) {
// The XHR request might have been aborted in onHeadersReceived()
// callback, in which case we should abort request
return;
}
delete this.pendingRequests[xhrId];
if (xhr.status === 0) {
//warn('Received xhr.status of 0');
@ -174,6 +172,8 @@ var NetworkManager = (function NetworkManagerClosure() {
return;
}
this.loadedRequests[xhrId] = true;
var chunk = getArrayBuffer(xhr);
if (xhr.expectedStatus === 206) {
var rangeHeader = xhr.getResponseHeader('Content-Range');
@ -196,28 +196,36 @@ var NetworkManager = (function NetworkManagerClosure() {
},
hasPendingRequests: function NetworkManager_hasPendingRequests() {
for (var xhrId in this.xhrRequests) {
for (var xhrId in this.pendingRequests) {
return true;
}
return false;
},
getRequestXhr: function NetworkManager_getXhr(xhrId) {
return this.pendingRequests[xhrId];
},
isPendingRequest: function NetworkManager_isPendingRequest(xhrId) {
return xhrId in this.xhrRequests;
return xhrId in this.pendingRequests;
},
isLoadedRequest: function NetworkManager_isLoadedRequest(xhrId) {
return xhrId in this.loadedRequests;
},
abortXhrs: function NetworkManager_abortXhrs() {
for (var xhrId in this.xhrRequests) {
for (var xhrId in this.pendingRequests) {
xhrId = xhrId | 0;
var xhr = this.xhrRequests[xhrId];
delete this.xhrRequests[xhrId];
var xhr = this.pendingRequests[xhrId];
delete this.pendingRequests[xhrId];
xhr.abort();
}
},
abortRequest: function NetworkManager_abortRequest(xhrId) {
var xhr = this.xhrRequests[xhrId];
delete this.xhrRequests[xhrId];
var xhr = this.pendingRequests[xhrId];
delete this.pendingRequests[xhrId];
xhr.abort();
}
};
@ -241,7 +249,6 @@ if (!globalScope.PDFJS) {
globalScope.PDFJS = {};
}
globalScope.PDFJS.pdfBug = false;
var Page = (function PageClosure() {
@ -594,6 +601,10 @@ var PDFDocument = (function PDFDocumentClosure() {
linearization = false;
}
} catch (err) {
if (err instanceof MissingDataException) {
throw err;
}
warn('The linearization data is not available ' +
'or unreadable pdf data is found');
linearization = false;
@ -672,13 +683,12 @@ var PDFDocument = (function PDFDocumentClosure() {
}
// May not be a PDF file, continue anyway.
},
setXrefStart: function PDFDocument_setXrefStart() {
var startXRef = this.startXRef;
this.xref.startXrefQueue = [startXRef];
},
setup: function PDFDocument_setup(recoveryMode) {
this.checkHeader();
var startXRef = this.startXRef;
// FIXME(mack): Clean up how this done
if (!this.xref.startXrefQueue) {
this.xref.startXrefQueue = [[startXRef]];
}
this.xref.parse(recoveryMode);
this.catalog = new Catalog(this.xref);
},
@ -1109,6 +1119,12 @@ var Util = PDFJS.Util = (function UtilClosure() {
var PageViewport = PDFJS.PageViewport = (function PageViewportClosure() {
function PageViewport(viewBox, scale, rotate, offsetX, offsetY) {
this.viewBox = viewBox;
this.scale = scale;
this.rotate = rotate;
this.offsetX = offsetX;
this.offsetY = offsetY;
// creating transform to convert pdf coordinate system to the normal
// canvas like coordinates taking in account scale and rotation
var centerX = (viewBox[2] + viewBox[0]) / 2;
@ -1158,13 +1174,22 @@ var PageViewport = PDFJS.PageViewport = (function PageViewportClosure() {
offsetCanvasY - rotateB * scale * centerX - rotateD * scale * centerY
];
this.offsetX = offsetX;
this.offsetY = offsetY;
this.width = width;
this.height = height;
this.fontScale = scale;
}
PageViewport.prototype = {
clone: function PageViewPort_clone(args) {
args = args || {};
var scale = 'scale' in args ? args.scale : this.scale;
var rotate = 'rotate' in args ? args.rotate : this.rotate;
var viewBoxClone = [];
for (var i = 0; i < this.viewBox.length; ++i) {
viewBoxClone[i] = this.viewBox[i];
}
return new PageViewport(viewBoxClone, scale, rotate,
this.offsetX, this.offsetY);
},
convertToViewportPoint: function PageViewport_convertToViewportPoint(x, y) {
return Util.applyTransform([x, y], this.transform);
},
@ -1536,7 +1561,7 @@ PDFJS.createBlob = function createBlob(data, contentType) {
*
* @return {Promise} A promise that is resolved with {PDFDocumentProxy} object.
*/
PDFJS.getDocument = function getDocument(source) {
PDFJS.getDocument = function getDocument(source, pdfDataRangeTransport) {
var workerInitializedPromise, workerReadyPromise, transport;
if (typeof source === 'string') {
@ -1564,9 +1589,9 @@ PDFJS.getDocument = function getDocument(source) {
workerInitializedPromise = new PDFJS.Promise();
workerReadyPromise = new PDFJS.Promise();
transport = new WorkerTransport(workerInitializedPromise,
workerReadyPromise, params);
workerReadyPromise, pdfDataRangeTransport);
workerInitializedPromise.then(function transportInitialized() {
transport.fetchDocument();
transport.fetchDocument(params);
});
return workerReadyPromise;
};
@ -1950,8 +1975,8 @@ var PDFPageProxy = (function PDFPageProxyClosure() {
*/
var WorkerTransport = (function WorkerTransportClosure() {
function WorkerTransport(workerInitializedPromise, workerReadyPromise,
source) {
this.source = source;
pdfDataRangeTransport) {
this.pdfDataRangeTransport = pdfDataRangeTransport;
this.workerReadyPromise = workerReadyPromise;
this.commonObjs = new PDFObjects();
@ -2034,15 +2059,19 @@ var WorkerTransport = (function WorkerTransportClosure() {
function WorkerTransport_setupMessageHandler(messageHandler) {
this.messageHandler = messageHandler;
if (this.source.chunkedChromeLoading) {
window.addEventListener('message', function window_message(evt) {
this.messageHandler.send('SendDataRange', evt.data);
}.bind(this));
}
var pdfDataRangeTransport = this.pdfDataRangeTransport;
if (pdfDataRangeTransport) {
pdfDataRangeTransport.addListener(function(args) {
messageHandler.send('OnDataRange', args);
});
messageHandler.on('RequestDataRange', function transportDataRange(args) {
FirefoxCom.request('requestDataRange', args);
}, this);
messageHandler.on('RequestDataRange',
function transportDataRange(data) {
var begin = data.begin;
var end = data.end;
pdfDataRangeTransport.requestDataRange(begin, end);
}, this);
}
messageHandler.on('GetDoc', function transportDoc(data) {
var pdfInfo = data.pdfInfo;
@ -2111,11 +2140,10 @@ var WorkerTransport = (function WorkerTransportClosure() {
// At this point, only the font object is created but the font is
// not yet attached to the DOM. This is done in `FontLoader.bind`.
var font;
if ('error' in exportedData) {
if ('error' in exportedData)
font = new ErrorFont(exportedData.error);
} else {
else
font = new Font(exportedData);
}
this.commonObjs.resolve(id, font);
break;
default:
@ -2207,8 +2235,12 @@ var WorkerTransport = (function WorkerTransportClosure() {
});
},
fetchDocument: function WorkerTransport_fetchDocument() {
this.messageHandler.send('GetDocRequest', {source: this.source});
fetchDocument: function WorkerTransport_fetchDocument(source) {
extend(source, { chunkedViewerLoading: !!this.pdfDataRangeTransport });
this.messageHandler.send('GetDocRequest', {
source: source,
rangeSupport: PDFJS.rangeSupport
});
},
getData: function WorkerTransport_getData(promise) {
@ -4084,7 +4116,6 @@ var XRef = (function XRefClosure() {
// prepare the XRef cache
this.cache = [];
this.password = password;
this.startXrefQueue = undefined;
}
XRef.prototype = {
@ -4248,11 +4279,6 @@ var XRef = (function XRefClosure() {
},
readXRefStream: function XRef_readXRefStream(stream) {
//var streamParameters = stream.parameters;
//var byteWidths = streamParameters.get('W');
//var range = streamParameters.get('Index');
//if (!range)
// range = [0, streamParameters.get('Size')];
var i, j;
stream.pos = this.streamState.pos;
@ -4308,7 +4334,6 @@ var XRef = (function XRefClosure() {
this.streamState.pos = stream.pos;
this.streamState.range.splice(0, 2);
}
//return streamParameters;
},
indexObjects: function XRef_indexObjects() {
// Simple scan through the PDF content to find objects,
@ -4396,7 +4421,7 @@ var XRef = (function XRefClosure() {
}
// reading XRef streams
for (var i = 0, ii = xrefStms.length; i < ii; ++i) {
this.startXrefQueue.push([xrefStms[i], true]);
this.startXrefQueue.push(xrefStms[i]);
this.readXRef(/* recoveryMode */ true);
}
// finding main trailer
@ -4427,7 +4452,7 @@ var XRef = (function XRefClosure() {
try {
while (this.startXrefQueue.length) {
var startXRef = this.startXrefQueue[0][0];
var startXRef = this.startXrefQueue[0];
stream.pos = startXRef;
@ -4453,7 +4478,7 @@ var XRef = (function XRefClosure() {
// (possible infinite recursion)
if (!(pos in this.xrefstms)) {
this.xrefstms[pos] = 1;
this.startXrefQueue.push([pos]);
this.startXrefQueue.push(pos);
}
}
} else if (isInt(obj)) {
@ -4477,11 +4502,11 @@ var XRef = (function XRefClosure() {
// Recursively get previous dictionary, if any
obj = dict.get('Prev');
if (isInt(obj)) {
this.startXrefQueue.push([obj, recoveryMode]);
this.startXrefQueue.push(obj);
} else if (isRef(obj)) {
// The spec says Prev must not be a reference, i.e. "/Prev NNN"
// This is a fallback for non-compliant PDFs, i.e. "/Prev NNN 0 R"
this.startXrefQueue.push([obj.num, recoveryMode]);
this.startXrefQueue.push(obj.num);
}
this.startXrefQueue.shift();
@ -4496,9 +4521,8 @@ var XRef = (function XRefClosure() {
log('(while reading XRef): ' + e);
}
if (recoveryMode) {
if (recoveryMode)
return;
}
throw new XrefParseException();
},
@ -30602,13 +30626,7 @@ var Lexer = (function LexerClosure() {
}
stream.skip();
}
// TODO(mack): gotta test this optimization
var value;
if (floating) {
value = parseFloat(str);
} else {
value = str | 0;
}
var value = parseFloat(str);
if (isNaN(value))
error('Invalid floating point number: ' + value);
return value;
@ -30793,7 +30811,6 @@ var Lexer = (function LexerClosure() {
return Cmd.get(ch);
// hex string or dict punctuation
case '<':
var position = stream.pos;
ch = stream.lookChar();
if (ch == '<') {
// dict punctuation
@ -31319,7 +31336,6 @@ var Stream = (function StreamClosure() {
return this.end - this.start;
},
getByte: function Stream_getByte() {
//throw new Error('getByte');
if (this.pos >= this.end)
return null;
return this.bytes[this.pos++];
@ -31327,7 +31343,6 @@ var Stream = (function StreamClosure() {
// returns subarray of original buffer
// should only be read
getBytes: function Stream_getBytes(length) {
//throw new Error('getBytes');
var bytes = this.bytes;
var pos = this.pos;
var strEnd = this.end;
@ -31343,13 +31358,11 @@ var Stream = (function StreamClosure() {
return bytes.subarray(pos, end);
},
lookChar: function Stream_lookChar() {
//throw new Error('lookChar');
if (this.pos >= this.end)
return null;
return String.fromCharCode(this.bytes[this.pos]);
},
getChar: function Stream_getChar() {
//throw new Error('getChar');
if (this.pos >= this.end)
return null;
return String.fromCharCode(this.bytes[this.pos++]);
@ -33678,7 +33691,6 @@ var ChunkedStream = (function ChunkedStreamClosure() {
},
allChunksLoaded: function ChunkedStream_allChunksLoaded() {
// TODO(mack): might want to use list of unloaded chunks to be safer...
return this.numChunksLoaded === this.numChunks;
},
@ -33773,9 +33785,6 @@ var ChunkedStream = (function ChunkedStreamClosure() {
return bytes.subarray(pos, end);
},
// TODO(mack): make getBytes call this
// returns subarray of original buffer
// should only be read
getByteRange: function ChunkedStream_getBytes(begin, end) {
this.ensureRange(begin, end);
return this.bytes.subarray(begin, end);
@ -33836,23 +33845,10 @@ var ChunkedStreamManager = (function ChunkedStreamManagerClosure() {
this.length = stream.length;
this.url = url;
// Prefill stream with fully loaded chunks
if (args.loadedChunk && isInt(args.loadedBegin)) {
var loadedBegin = args.loadedBegin;
var actualChunk = args.loadedChunk;
var actualEnd = loadedBegin + actualChunk.byteLength;
var loadedEnd = Math.floor(actualEnd / this.chunkSize) * this.chunkSize;
var loadedChunk = actualChunk.subarray(loadedBegin, loadedEnd);
this.stream.onReceiveData(loadedBegin, loadedChunk);
}
if (args.fetchByChrome) {
args.msgHandler.on('SendDataRange', this.onReceiveData.bind(this));
this.sendRequest = function streamManager_sendRequest(begin, end) {
args.msgHandler.send(
'RequestDataRange',
{ begin: begin, end: end }
);
if (args.chunkedViewerLoading) {
args.msgHandler.on('OnDataRange', this.onReceiveData.bind(this));
this.sendRequest = function ChunkedStreamManager_sendRequest(begin, end) {
args.msgHandler.send('RequestDataRange', { begin: begin, end: end });
};
} else {
@ -33860,7 +33856,7 @@ var ChunkedStreamManager = (function ChunkedStreamManagerClosure() {
return new XMLHttpRequest();
};
this.networkManager = new NetworkManager(this.url, { getXhr: getXhr });
this.sendRequest = function streamManager_sendRequest(begin, end) {
this.sendRequest = function ChunkedStreamManager_sendRequest(begin, end) {
this.networkManager.requestRange(
begin, end, this.onReceiveData.bind(this));
};
@ -34180,9 +34176,7 @@ var NetworkPdfManager = (function NetworkPdfManagerClosure() {
var params = {
msgHandler: msgHandler,
fetchByChrome: args.chunkedChromeLoading,
loadedBegin: args.loadedBegin,
loadedChunk: args.loadedChunk
chunkedViewerLoading: args.chunkedViewerLoading
};
this.streamManager = new ChunkedStreamManager(
this.stream, this.pdfUrl, params);
@ -34270,14 +34264,6 @@ var NetworkPdfManager = (function NetworkPdfManagerClosure() {
var START_TIME = (new Date()).getTime() / 1000;
function timeLog() {
var elapsedTime = (new Date()).getTime() / 1000 - START_TIME;
var args = [].slice.call(arguments);
args.unshift(elapsedTime);
console.log.apply(console, args);
}
function MessageHandler(name, comObj) {
this.name = name;
this.comObj = comObj;
@ -34304,16 +34290,13 @@ function MessageHandler(name, comObj) {
}];
comObj.onmessage = function messageHandlerComObjOnMessage(event) {
var handler;
var data = event.data;
if (data.isReply) {
var callbackId = data.callbackId;
if (data.callbackId in callbacks) {
var callback = callbacks[callbackId];
delete callbacks[callbackId];
handler = callback.bind(undefined, data.data);
callback(data.data);
} else {
error('Cannot resolve callback ' + callbackId);
}
@ -34328,19 +34311,13 @@ function MessageHandler(name, comObj) {
data: resolvedData
});
});
handler = action[0].bind(action[1], data.data, promise);
action[0].call(action[1], data.data, promise);
} else {
handler = action[0].bind(action[1], data.data);
action[0].call(action[1], data.data);
}
} else {
error('Unkown action from worker: ' + data.action);
}
if (!handler) {
return;
}
handler();
};
}
@ -34406,8 +34383,10 @@ var WorkerMessageHandler = {
loadDocumentPromise.reject(e);
};
pdfManager.ensureModel('parse', recoveryMode).then(
parseSuccess, parseFailure);
pdfManager.ensureModel('setXrefStart').then(function() {
pdfManager.ensureModel('parse', recoveryMode).then(
parseSuccess, parseFailure);
});
return loadDocumentPromise;
}
@ -34416,11 +34395,12 @@ var WorkerMessageHandler = {
var pdfManagerPromise = new PDFJS.Promise();
var source = data.source;
var rangeSupport = data.rangeSupport;
if (source.data) {
pdfManager = new LocalPdfManager(source.data, source.password);
pdfManagerPromise.resolve();
return pdfManagerPromise;
} else if (source.chunkedChromeLoading) {
} else if (source.chunkedViewerLoading) {
pdfManager = new NetworkPdfManager(source, handler);
pdfManagerPromise.resolve();
return pdfManagerPromise;
@ -34429,30 +34409,33 @@ var WorkerMessageHandler = {
var networkManager = new NetworkManager(source.url, {
httpHeaders: source.httpHeaders
});
networkManager.requestFull({
onHeadersReceived: function onHeadersReceieved(args) {
var fullRequestXhrId = args.xhrId;
var fullRequestXhrId = networkManager.requestFull({
onHeadersReceived: function onHeadersReceived() {
if (rangeSupport === 'disable') {
return;
}
// This is done in onHeadersReceived because we want to
// guarantee that the full request is sent first in
// case that the server will only allow max 1 request
// (eg. pay-wall)
networkManager.requestRange(0, 1, function getPdfLength(args) {
// The full request is already finished, in which case we do not
// need to fetch chunks
if (!networkManager.isPendingRequest(fullRequestXhrId)) {
return;
}
var fullRequestXhr = networkManager.getRequestXhr(fullRequestXhrId);
if (fullRequestXhr.getResponseHeader('Accept-Ranges') !== 'bytes') {
return;
}
// TODO(mack): In this case, we should access the data that
// is already fetched from the full request so we do not refetch
// the same data
networkManager.abortRequest(fullRequestXhrId);
var contentLength = fullRequestXhr.getResponseHeader(
'Content-Length');
contentLength = parseInt(contentLength, 10);
if (!isInt(contentLength)) {
return;
}
source.totalLength = args.totalLength;
pdfManager = new NetworkPdfManager(source, handler);
pdfManagerPromise.resolve(pdfManager);
});
// NOTE: by cancelling the full request, and then issuing range
// requests, there will be an issue for sites where you can only
// request the pdf once. However, if this is the case, then the
// server should not be returning that it can support range requests.
networkManager.abortRequest(fullRequestXhrId);
source.totalLength = contentLength;
pdfManager = new NetworkPdfManager(source, handler);
pdfManagerPromise.resolve(pdfManager);
},
onDone: function onDone(args) {
// the data is array, instantiating directly from it

View file

@ -708,6 +708,10 @@ var PDFView = {
}, false);
},
getPage: function pdfViewGetPage(n) {
return this.pdfDocument.getPage(n);
},
// Helper function to keep track whether a div was scrolled up or down and
// then call a callback.
watchScroll: function pdfViewWatchScroll(viewAreaElement, state, callback) {
@ -890,28 +894,43 @@ var PDFView = {
PDFView.loadingBar = new ProgressBar('#loadingBar', {});
}
function windowMessage(e) {
var PdfDataRangeTransport = {
listeners: [],
addListener: function PdfDataRangeTransport_addListener(listener) {
this.listeners.push(listener);
},
onDataRange: function PdfDataRangeTransport_onDataRange(args) {
for (var i = 0; i < this.listeners.length; ++i) {
this.listeners[i](args);
}
},
requestDataRange: function PdfDataRangeTransport_requestDataRange(
begin, end) {
FirefoxCom.request('requestDataRange', { begin: begin, end: end });
}
};
window.addEventListener('message', function windowMessage(e) {
var args = e.data;
if (typeof args !== 'object' || !('pdfjsLoadAction' in args))
return;
switch (args.pdfjsLoadAction) {
case 'supportsChunkedLoading':
window.removeEventListener('message', windowMessage);
PDFView.open({
url: args.pdfUrl,
chunkedChromeLoading: true,
totalLength: args.totalLength,
loadedBegin: args.loadedBegin,
loadedChunk: args.loadedChunk
}, 0);
PDFView.open(args.pdfUrl, 0, undefined, PdfDataRangeTransport, {
totalLength: args.totalLength
});
break;
case 'chunk':
PdfDataRangeTransport.onDataRange(args);
break;
case 'progress':
PDFView.progress(args.loaded / args.total);
break;
case 'complete':
// TODO(mack): remove the listener when file loading is complete
// window.removeEventListener('message', windowMessage);
if (!args.data) {
PDFView.error(mozL10n.get('loading_error', null,
'An error occurred while loading the PDF.'), e);
@ -920,8 +939,7 @@ var PDFView = {
PDFView.open(args.data, 0);
break;
}
}
window.addEventListener('message', windowMessage);
});
FirefoxCom.requestSync('initPassiveLoading', null);
},
@ -940,17 +958,21 @@ var PDFView = {
document.title = title;
},
open: function pdfViewOpen(url, scale, password) {
// TODO(mack): This function signature should really be pdfViewOpen(url, args)
open: function pdfViewOpen(url, scale, password,
pdfDataRangeTransport, args) {
scale = scale || 1;
var parameters = {password: password};
if (typeof url === 'string') { // URL
this.setTitleUsingUrl(url);
parameters.url = url;
} else if (url && 'byteLength' in url) { // ArrayBuffer
parameters.data = url;
} else {
this.setTitleUsingUrl(url.url);
parameters = url;
parameters.password = password;
}
if (args) {
for (var prop in args) {
parameters[prop] = args[prop];
}
}
if (!PDFView.loadingBar) {
@ -960,7 +982,7 @@ var PDFView = {
this.pdfDocument = null;
var self = this;
self.loading = true;
PDFJS.getDocument(parameters).then(
PDFJS.getDocument(parameters, pdfDataRangeTransport).then(
function getDocumentCallback(pdfDocument) {
self.load(pdfDocument, scale);
self.loading = false;
@ -1192,7 +1214,6 @@ var PDFView = {
container.removeChild(container.lastChild);
var pagesCount = pdfDocument.numPages;
var id = pdfDocument.fingerprint;
document.getElementById('numPages').textContent =
mozL10n.get('page_of', {pageCount: pagesCount}, 'of {{pageCount}}');
@ -1209,79 +1230,70 @@ var PDFView = {
this.startedTextExtraction = false;
var pagesRefMap = this.pagesRefMap = {};
var thumbnails = this.thumbnails = [];
var pagePromises = [];
var firstPageLoaded = true;
var defaultViewportPromise = new PDFJS.Promise();
var pagesPromise = new PDFJS.Promise();
var self = this;
for (var pageIdx = 0; pageIdx < pagesCount; ++pageIdx) {
var pageNum = pageIdx + 1;
var pagePromise = pdfDocument.getPage(pageNum);
pagePromise.then(function(page) {
if (firstPageLoaded) {
var pagesFrag = document.createDocumentFragment();
// TODO(mack): clone
var defaultView = page.view;
for (var pageIdx = 0; pageIdx < pagesCount; ++pageIdx) {
var pageNum = pageIdx + 1;
var pageView = new PageView(container, pageNum, scale,
self.navigateTo.bind(self), defaultView);
pages[pageIdx] = pageView;
pagesFrag.appendChild(pageView.element);
}
container.appendChild(pagesFrag);
// TODO(mack): Verify is safe to assume at least one page if
// we're here.
pdfDocument.getPage(1).then(function(pdfPage) {
var viewport = pdfPage.getViewport(scale);
defaultViewportPromise.resolve(viewport);
});
var storedHash = null;
if (store.get('exists', false)) {
var page_ = store.get('page', '1');
var zoom = store.get('zoom', PDFView.currentScale);
var left = store.get('scrollLeft', '0');
var top = store.get('scrollTop', '0');
storedHash =
'page=' + page_ + '&zoom=' + zoom + ',' + left +',' + top;
}
self.setInitialView(storedHash, scale);
firstPageLoaded = false;
}
var pageIdx = page.pageInfo.pageIndex;
var pageNum = pageIdx + 1;
var pageView = pages[pageIdx];
pageView.setPdfPage(page);
pageView.setStats(page.stats);
var thumbnailView = new ThumbnailView(thumbsView, page, pageNum);
defaultViewportPromise.then(function(viewport) {
var pagePromises = [];
for (var pageNum = 1; pageNum <= pagesCount; ++pageNum) {
var viewportClone = viewport.clone();
var pageView = new PageView(container, pageNum, scale,
self.navigateTo.bind(self),
viewportClone);
var thumbnailView = new ThumbnailView(thumbsView, pageNum,
viewportClone);
bindOnAfterDraw(pageView, thumbnailView);
pages.push(pageView);
thumbnails.push(thumbnailView);
pages[pageIdx] = pageView;
thumbnails[pageIdx] = thumbnailView;
var pageRef = page.ref;
pagesRefMap[pageRef.num + ' ' + pageRef.gen + ' R'] = pageNum;
var pagePromise = pdfDocument.getPage(pageNum);
pagePromise.then(function(pdfPage) {
pageView.setPdfPage(pdfPage);
thumbnailView.setPdfPage(pdfPage);
var visiblePages = self.visiblePages;
var firstNum = visiblePages.first.id;
var lastNum = visiblePages.last.id;
if (pageNum >= firstNum && pageNum <= lastNum) {
// TODO(mack): This logic could probably be better, as it's probably
// going in here more often than necessary
self.renderHighestPriority();
//updateViewarea();
}
var pageRef = pdfPage.ref;
var refStr = pageRef.num + ' ' + pageRef.gen + ' R';
pagesRefMap[refStr] = pdfPage.pageNumber;
});
pagePromises.push(pagePromise);
}
var event = document.createEvent('CustomEvent');
event.initCustomEvent('documentload', true, true, {});
window.dispatchEvent(event);
var storedHash = null;
if (store.get('exists', false)) {
var page_ = store.get('page', '1');
var zoom = store.get('zoom', PDFView.currentScale);
var left = store.get('scrollLeft', '0');
var top = store.get('scrollTop', '0');
storedHash = 'page=' + page_ + '&zoom=' + zoom + ',' + left +',' + top;
}
self.setInitialView(storedHash, scale);
PDFJS.Promise.all(pagePromises).then(function(pages) {
pagesPromise.resolve(pages);
});
pagePromises.push(pagePromise);
}
var pagesPromise = PDFJS.Promise.all(pagePromises);
});
var destinationsPromise = pdfDocument.getDestinations();
destinationsPromise.then(function(destinations) {
self.destinations = destinations;
});
// outline and initial view depends on destinations and pagesRefMap
// outline depends on destinations and pagesRefMap
var promises = [pagesPromise, destinationsPromise, storePromise,
PDFView.animationStartedPromise];
PDFJS.Promise.all(promises).then(function() {
@ -1345,8 +1357,8 @@ var PDFView = {
renderHighestPriority: function pdfViewRenderHighestPriority() {
// Pages have a higher priority than thumbnails, so check them first.
this.visiblePages = this.getVisiblePages();
var pageView = this.getHighestPriority(this.visiblePages, this.pages,
var visiblePages = this.getVisiblePages();
var pageView = this.getHighestPriority(visiblePages, this.pages,
this.pageViewScroll.down);
if (pageView) {
this.renderView(pageView, 'page');
@ -1379,7 +1391,7 @@ var PDFView = {
}
for (var i = 0; i < numVisible; ++i) {
var view = visibleViews[i].view;
if (!this.isViewRenderable(view))
if (!this.isViewFinished(view))
return view;
}
@ -1387,22 +1399,19 @@ var PDFView = {
if (scrolledDown) {
var nextPageIndex = visible.last.id;
// ID's start at 1 so no need to add 1.
if (views[nextPageIndex] && !this.isViewRenderable(views[nextPageIndex]))
if (views[nextPageIndex] && !this.isViewFinished(views[nextPageIndex]))
return views[nextPageIndex];
} else {
var previousPageIndex = visible.first.id - 2;
if (views[previousPageIndex] &&
!this.isViewRenderable(views[previousPageIndex]))
!this.isViewFinished(views[previousPageIndex]))
return views[previousPageIndex];
}
// Everything that needs to be rendered has been.
return false;
},
isViewRenderable: function pdfViewNeedsRendering(view) {
if (view instanceof PageView && !view.pdfPage) {
return false;
}
isViewFinished: function pdfViewNeedsRendering(view) {
return view.renderingState === RenderingStates.FINISHED;
},
@ -1682,7 +1691,7 @@ var PDFView = {
for (var i = 0, l = this.thumbnails.length; i < l; i++) {
var thumb = this.thumbnails[i];
thumb.updateRotation(this.pageRotation);
thumb.update(this.pageRotation);
}
var currentPage = this.pages[this.page - 1];
@ -1764,29 +1773,13 @@ var PDFView = {
};
var PageView = function pageView(container, id, scale,
navigateTo, defaultView) {
navigateTo, defaultViewport) {
this.id = id;
//this.pdfPage = pdfPage;
this.rotation = 0;
this.scale = scale || 1.0;
//this.viewport = this.pdfPage.getViewport(this.scale, this.pdfPage.rotate);
this.defaultView = defaultView;
this.getViewport = function pageViewGetViewport() {
var view;
var totalRotation;
if (this.pdfPage) {
totalRotation = (this.rotation + this.pdfPage.rotate) % 360;
view = this.pdfPage.view;
} else {
totalRotation = this.rotation;
view = this.defaultView;
}
return new PDFJS.PageViewport(view, this.scale, totalRotation, 0, 0);
};
this.viewport = this.getViewport();
this.viewport = defaultViewport;
this.pdfPageRotate = defaultViewport.rotate;
this.renderingState = RenderingStates.INITIAL;
this.resume = null;
@ -1798,38 +1791,27 @@ var PageView = function pageView(container, id, scale,
anchor.name = '' + this.id;
var div = this.el = document.createElement('div');
//var div = this.el = element;
div.id = 'pageContainer' + this.id;
div.className = 'page';
div.style.width = Math.floor(this.viewport.width) + 'px';
div.style.height = Math.floor(this.viewport.height) + 'px';
this.container = container;
//this.container.appendChild(anchor);
//this.container.appendChild(div);
var docFrag = document.createDocumentFragment();
docFrag.appendChild(anchor);
docFrag.appendChild(div);
Object.defineProperty(this, 'element', {
get: function PlaceholderPageView_getElement() {
return docFrag;
},
enumerable: true
});
container.appendChild(anchor);
container.appendChild(div);
this.setPdfPage = function pageViewSetPdfPage(pdfPage) {
this.pdfPage = pdfPage;
};
this.setStats = function pageViewSetStats(stats) {
this.stats = stats;
this.pdfPageRotate = pdfPage.rotate;
this.viewport = pdfPage.getViewport(this.scale);
this.stats = pdfPage.stats;
this.update();
};
this.destroy = function pageViewDestroy() {
this.update();
this.pdfPage.destroy();
if (this.pdfPage) {
this.pdfPage.destroy();
}
};
this.update = function pageViewUpdate(scale, rotation) {
@ -1841,11 +1823,14 @@ var PageView = function pageView(container, id, scale,
}
this.scale = scale || this.scale;
var viewport = this.getViewport();
var totalRotation = (this.rotation + this.pdfPageRotate) % 360;
this.viewport = this.viewport.clone({
scale: this.scale,
rotation: totalRotation
});
this.viewport = viewport;
div.style.width = Math.floor(viewport.width) + 'px';
div.style.height = Math.floor(viewport.height) + 'px';
div.style.width = Math.floor(this.viewport.width) + 'px';
div.style.height = Math.floor(this.viewport.height) + 'px';
while (div.hasChildNodes())
div.removeChild(div.lastChild);
@ -2047,11 +2032,16 @@ var PageView = function pageView(container, id, scale,
};
this.draw = function pageviewDraw(callback) {
if (!this.pdfPage) {
console.log('Cannot draw without page');
var pdfPage = this.pdfPage;
if (!pdfPage) {
var promise = PDFView.getPage(this.id);
promise.then(function(pdfPage) {
this.setPdfPage(pdfPage);
this.draw(callback);
}.bind(this));
return;
}
var pdfPage = this.pdfPage;
if (this.renderingState !== RenderingStates.INITIAL) {
console.error('Must be in new state before drawing');
@ -2110,7 +2100,6 @@ var PageView = function pageView(container, id, scale,
ctx.scale(outputScale.sx, outputScale.sy);
}
// Rendering area
var self = this;
@ -2138,6 +2127,15 @@ var PageView = function pageView(container, id, scale,
self.onAfterDraw();
cache.push(self);
// TODO(mack): Verify this will not happend for thumbnails
// TODO(mack): Maybe should be attaching event to canvas
var event = document.createEvent('CustomEvent');
event.initCustomEvent('pagerender', true, true, {
pageNum: pdfPage.pageNumber
});
div.dispatchEvent(event);
callback();
}
@ -2183,13 +2181,14 @@ var PageView = function pageView(container, id, scale,
setupAnnotations(this.pdfPage, this.viewport);
div.setAttribute('data-loaded', true);
};
this.beforePrint = function pageViewBeforePrint() {
var pdfPage = this.pdfPage;
if (!pdfPage) {
console.log('Pdf page has not been rendered and thus cannot be printed');
// TODO(mack): What to do if page is not loaded?
return;
}
@ -2244,7 +2243,7 @@ var PageView = function pageView(container, id, scale,
this.updateStats = function pageViewUpdateStats() {
if (!this.stats) {
console.log('No stats available');
return;
}
if (PDFJS.pdfBug && Stats.enabled) {
@ -2254,7 +2253,7 @@ var PageView = function pageView(container, id, scale,
};
};
var ThumbnailView = function thumbnailView(container, pdfPage, id) {
var ThumbnailView = function thumbnailView(container, id, defaultViewport) {
var anchor = document.createElement('a');
anchor.href = PDFView.getAnchorUrl('#page=' + id);
anchor.title = mozL10n.get('thumb_page_title', {page: id}, 'Page {{page}}');
@ -2263,18 +2262,20 @@ var ThumbnailView = function thumbnailView(container, pdfPage, id) {
return false;
};
var rotation = 0;
var totalRotation = (rotation + pdfPage.rotate) % 360;
var viewport = pdfPage.getViewport(1, totalRotation);
var pageWidth = this.width = viewport.width;
var pageHeight = this.height = viewport.height;
var pageRatio = pageWidth / pageHeight;
this.pdfPage = undefined;
this.viewport = defaultViewport;
this.pdfPageRotate = defaultViewport.rotate;
this.rotation = 0;
this.totalRotation = (this.rotation + this.pdfPageRotate) % 360;
this.pageWidth = this.viewport.width;
this.pageHeight = this.viewport.height;
this.pageRatio = this.pageWidth / this.pageHeight;
this.id = id;
var canvasWidth = 98;
var canvasHeight = canvasWidth / this.width * this.height;
var scaleX = this.scaleX = (canvasWidth / pageWidth);
var scaleY = this.scaleY = (canvasHeight / pageHeight);
this.canvasWidth = 98;
this.canvasHeight = this.canvasWidth / this.pageWidth * this.pageHeight;
this.scale = (this.canvasWidth / this.pageWidth);
var div = this.el = document.createElement('div');
div.id = 'thumbnailContainer' + id;
@ -2288,8 +2289,8 @@ var ThumbnailView = function thumbnailView(container, pdfPage, id) {
var ring = document.createElement('div');
ring.className = 'thumbnailSelectionRing';
ring.style.width = canvasWidth + 'px';
ring.style.height = canvasHeight + 'px';
ring.style.width = this.canvasWidth + 'px';
ring.style.height = this.canvasHeight + 'px';
div.appendChild(ring);
anchor.appendChild(div);
@ -2298,36 +2299,46 @@ var ThumbnailView = function thumbnailView(container, pdfPage, id) {
this.hasImage = false;
this.renderingState = RenderingStates.INITIAL;
this.updateRotation = function(rot) {
this.setPdfPage = function thumbnailViewSetPdfPage(pdfPage) {
this.pdfPage = pdfPage;
this.pdfPageRotate = pdfPage.rotate;
this.viewport = pdfPage.getViewport(this.scale);
this.update();
};
rotation = rot;
totalRotation = (rotation + pdfPage.rotate) % 360;
viewport = pdfPage.getViewport(1, totalRotation);
pageWidth = this.width = viewport.width;
pageHeight = this.height = viewport.height;
pageRatio = pageWidth / pageHeight;
this.update = function(rot) {
if (rot !== undefined) {
this.rotation = rot;
}
this.totalRotation = (this.rotation + this.pdfPageRotate) % 360;
this.viewport = this.viewport.clone({
scale: 1,
rotation: this.totalRotation
});
this.pageWidth = this.viewport.width;
this.pageHeight = this.viewport.height;
this.pageRatio = this.pageWidth / this.pageHeight;
canvasHeight = canvasWidth / this.width * this.height;
scaleX = this.scaleX = (canvasWidth / pageWidth);
scaleY = this.scaleY = (canvasHeight / pageHeight);
this.canvasHeight = this.canvasWidth / this.pageWidth * this.pageHeight;
this.scale = (this.canvasHeight / this.pageHeight);
div.removeAttribute('data-loaded');
ring.textContent = '';
ring.style.width = canvasWidth + 'px';
ring.style.height = canvasHeight + 'px';
ring.style.width = this.canvasWidth + 'px';
ring.style.height = this.canvasHeight + 'px';
this.hasImage = false;
this.renderingState = RenderingStates.INITIAL;
this.resume = null;
};
function getPageDrawContext() {
this.getPageDrawContext = function thumnailViewGetPageDrawContext() {
var canvas = document.createElement('canvas');
canvas.id = 'thumbnail' + id;
canvas.mozOpaque = true;
canvas.width = canvasWidth;
canvas.height = canvasHeight;
canvas.width = this.canvasWidth;
canvas.height = this.canvasHeight;
canvas.className = 'thumbnailImage';
canvas.setAttribute('aria-label', mozL10n.get('thumb_page_canvas',
{page: id}, 'Thumbnail of Page {{page}}'));
@ -2339,16 +2350,25 @@ var ThumbnailView = function thumbnailView(container, pdfPage, id) {
var ctx = canvas.getContext('2d');
ctx.save();
ctx.fillStyle = 'rgb(255, 255, 255)';
ctx.fillRect(0, 0, canvasWidth, canvasHeight);
ctx.fillRect(0, 0, this.canvasWidth, this.canvasHeight);
ctx.restore();
return ctx;
}
},
this.drawingRequired = function thumbnailViewDrawingRequired() {
return !this.hasImage;
};
this.draw = function thumbnailViewDraw(callback) {
if (!this.pdfPage) {
var promise = PDFView.getPage(this.id);
promise.then(function(pdfPage) {
this.setPdfPage(pdfPage);
this.draw(callback);
}.bind(this));
return;
}
if (this.renderingState !== RenderingStates.INITIAL) {
console.error('Must be in new state before drawing');
}
@ -2360,8 +2380,9 @@ var ThumbnailView = function thumbnailView(container, pdfPage, id) {
}
var self = this;
var ctx = getPageDrawContext();
var drawViewport = pdfPage.getViewport(scaleX, totalRotation);
var ctx = this.getPageDrawContext();
var drawViewport = this.pdfPage.getViewport(this.scale,
this.totalRotation);
var renderContext = {
canvasContext: ctx,
viewport: drawViewport,
@ -2377,7 +2398,7 @@ var ThumbnailView = function thumbnailView(container, pdfPage, id) {
cont();
}
};
pdfPage.render(renderContext).then(
this.pdfPage.render(renderContext).then(
function pdfPageRenderCallback() {
self.renderingState = RenderingStates.FINISHED;
callback();
@ -2394,7 +2415,7 @@ var ThumbnailView = function thumbnailView(container, pdfPage, id) {
if (this.hasImage || !img)
return;
this.renderingState = RenderingStates.FINISHED;
var ctx = getPageDrawContext();
var ctx = this.getPageDrawContext();
ctx.drawImage(img, 0, 0, img.width, img.height,
0, 0, ctx.canvas.width, ctx.canvas.height);
@ -2837,8 +2858,13 @@ document.addEventListener('DOMContentLoaded', function webViewerLoad(evt) {
var hash = document.location.hash.substring(1);
var hashParams = PDFView.parseQueryString(hash);
if ('disableWorker' in hashParams)
if ('disableWorker' in hashParams) {
PDFJS.disableWorker = (hashParams['disableWorker'] === 'true');
}
if ('rangeSupport' in hashParams) {
PDFJS.rangeSupport = hashParams['rangeSupport'];
}
var locale = navigator.language;
if ('locale' in hashParams)
@ -3157,24 +3183,19 @@ window.addEventListener('pagechange', function pagechange(evt) {
var selected = document.querySelector('.thumbnail.selected');
if (selected)
selected.classList.remove('selected');
var thumbnail = document.getElementById('thumbnailContainer' + page);
// FIXME(mack): remove checking if thumbnail is null hack
if (thumbnail) {
thumbnail.classList.add('selected');
var visibleThumbs = PDFView.getVisibleThumbs();
var numVisibleThumbs = visibleThumbs.views.length;
// If the thumbnail isn't currently visible scroll it into view.
if (numVisibleThumbs > 0) {
var first = visibleThumbs.first.id;
// Account for only one thumbnail being visible.
var last = numVisibleThumbs > 1 ?
visibleThumbs.last.id : first;
if (page <= first || page >= last)
scrollIntoView(thumbnail);
}
thumbnail.classList.add('selected');
var visibleThumbs = PDFView.getVisibleThumbs();
var numVisibleThumbs = visibleThumbs.views.length;
// If the thumbnail isn't currently visible scroll it into view.
if (numVisibleThumbs > 0) {
var first = visibleThumbs.first.id;
// Account for only one thumbnail being visible.
var last = numVisibleThumbs > 1 ?
visibleThumbs.last.id : first;
if (page <= first || page >= last)
scrollIntoView(thumbnail);
}
}
document.getElementById('previous').disabled = (page <= 1);
document.getElementById('next').disabled = (page >= PDFView.pages.length);