2012-04-10 14:20:57 +09:00
|
|
|
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
2012-09-01 07:48:21 +09:00
|
|
|
/* Copyright 2012 Mozilla Foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2013-08-13 02:48:06 +09:00
|
|
|
/* globals CanvasGraphics, combineUrl, createScratchCanvas, error,
|
|
|
|
FontLoader, globalScope, info, isArrayBuffer, loadJpegStream,
|
|
|
|
MessageHandler, PDFJS, Promise, StatTimer, warn,
|
2014-04-30 00:07:05 +09:00
|
|
|
PasswordResponses, Util, loadScript, createPromiseCapability,
|
2013-08-13 02:48:06 +09:00
|
|
|
FontFace */
|
2012-04-10 14:20:57 +09:00
|
|
|
|
2013-10-16 19:07:27 +09:00
|
|
|
'use strict';
|
2012-09-05 02:22:32 +09:00
|
|
|
|
2013-07-11 01:52:37 +09:00
|
|
|
/**
|
|
|
|
* The maximum allowed image size in total pixels e.g. width * height. Images
|
|
|
|
* above this value will not be drawn. Use -1 for no limit.
|
2014-01-22 04:28:18 +09:00
|
|
|
* @var {number}
|
2013-07-11 01:52:37 +09:00
|
|
|
*/
|
2014-03-14 21:24:04 +09:00
|
|
|
PDFJS.maxImageSize = (PDFJS.maxImageSize === undefined ?
|
|
|
|
-1 : PDFJS.maxImageSize);
|
2013-07-11 01:52:37 +09:00
|
|
|
|
2013-08-20 08:33:20 +09:00
|
|
|
/**
|
2014-02-12 03:27:09 +09:00
|
|
|
* The url of where the predefined Adobe CMaps are located. Include trailing
|
|
|
|
* slash.
|
|
|
|
* @var {string}
|
|
|
|
*/
|
2014-03-14 21:24:04 +09:00
|
|
|
PDFJS.cMapUrl = (PDFJS.cMapUrl === undefined ? null : PDFJS.cMapUrl);
|
2014-02-12 03:27:09 +09:00
|
|
|
|
2014-03-15 03:22:02 +09:00
|
|
|
/**
|
|
|
|
* Specifies if CMaps are binary packed.
|
|
|
|
* @var {boolean}
|
|
|
|
*/
|
|
|
|
PDFJS.cMapPacked = PDFJS.cMapPacked === undefined ? false : PDFJS.cMapPacked;
|
|
|
|
|
2014-02-12 03:27:09 +09:00
|
|
|
/*
|
2013-08-20 08:33:20 +09:00
|
|
|
* By default fonts are converted to OpenType fonts and loaded via font face
|
|
|
|
* rules. If disabled, the font will be rendered using a built in font renderer
|
|
|
|
* that constructs the glyphs with primitive path commands.
|
2014-01-22 04:28:18 +09:00
|
|
|
* @var {boolean}
|
2013-08-20 08:33:20 +09:00
|
|
|
*/
|
2014-03-14 21:24:04 +09:00
|
|
|
PDFJS.disableFontFace = (PDFJS.disableFontFace === undefined ?
|
|
|
|
false : PDFJS.disableFontFace);
|
2013-10-16 05:41:49 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Path for image resources, mainly for annotation icons. Include trailing
|
|
|
|
* slash.
|
2014-01-22 04:28:18 +09:00
|
|
|
* @var {string}
|
2013-10-16 05:41:49 +09:00
|
|
|
*/
|
2014-03-14 21:24:04 +09:00
|
|
|
PDFJS.imageResourcesPath = (PDFJS.imageResourcesPath === undefined ?
|
|
|
|
'' : PDFJS.imageResourcesPath);
|
2013-10-16 05:41:49 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable the web worker and run all code on the main thread. This will happen
|
|
|
|
* automatically if the browser doesn't support workers or sending typed arrays
|
|
|
|
* to workers.
|
2014-01-22 04:28:18 +09:00
|
|
|
* @var {boolean}
|
2013-10-16 05:41:49 +09:00
|
|
|
*/
|
2014-03-14 21:24:04 +09:00
|
|
|
PDFJS.disableWorker = (PDFJS.disableWorker === undefined ?
|
|
|
|
false : PDFJS.disableWorker);
|
2013-10-16 05:41:49 +09:00
|
|
|
|
|
|
|
/**
|
2013-11-19 07:37:01 +09:00
|
|
|
* Path and filename of the worker file. Required when the worker is enabled in
|
|
|
|
* development mode. If unspecified in the production build, the worker will be
|
|
|
|
* loaded based on the location of the pdf.js file.
|
2014-01-22 04:28:18 +09:00
|
|
|
* @var {string}
|
2013-10-16 05:41:49 +09:00
|
|
|
*/
|
2014-03-14 21:24:04 +09:00
|
|
|
PDFJS.workerSrc = (PDFJS.workerSrc === undefined ? null : PDFJS.workerSrc);
|
2013-10-16 05:41:49 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable range request loading of PDF files. When enabled and if the server
|
|
|
|
* supports partial content requests then the PDF will be fetched in chunks.
|
2013-10-16 19:07:27 +09:00
|
|
|
* Enabled (false) by default.
|
2014-01-22 04:28:18 +09:00
|
|
|
* @var {boolean}
|
2013-10-16 05:41:49 +09:00
|
|
|
*/
|
2014-03-14 21:24:04 +09:00
|
|
|
PDFJS.disableRange = (PDFJS.disableRange === undefined ?
|
|
|
|
false : PDFJS.disableRange);
|
2013-10-16 05:41:49 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable pre-fetching of PDF file data. When range requests are enabled PDF.js
|
|
|
|
* will automatically keep fetching more data even if it isn't needed to display
|
|
|
|
* the current page. This default behavior can be disabled.
|
2014-01-22 04:28:18 +09:00
|
|
|
* @var {boolean}
|
2013-10-16 05:41:49 +09:00
|
|
|
*/
|
2014-03-14 21:24:04 +09:00
|
|
|
PDFJS.disableAutoFetch = (PDFJS.disableAutoFetch === undefined ?
|
|
|
|
false : PDFJS.disableAutoFetch);
|
2013-10-16 05:41:49 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enables special hooks for debugging PDF.js.
|
2014-01-22 04:28:18 +09:00
|
|
|
* @var {boolean}
|
2013-10-16 05:41:49 +09:00
|
|
|
*/
|
2014-03-14 21:24:04 +09:00
|
|
|
PDFJS.pdfBug = (PDFJS.pdfBug === undefined ? false : PDFJS.pdfBug);
|
2013-08-20 08:33:20 +09:00
|
|
|
|
2013-11-12 12:30:26 +09:00
|
|
|
/**
|
|
|
|
* Enables transfer usage in postMessage for ArrayBuffers.
|
|
|
|
* @var {boolean}
|
|
|
|
*/
|
2014-03-14 21:24:04 +09:00
|
|
|
PDFJS.postMessageTransfers = (PDFJS.postMessageTransfers === undefined ?
|
|
|
|
true : PDFJS.postMessageTransfers);
|
2013-12-19 06:39:03 +09:00
|
|
|
|
2014-01-11 07:30:41 +09:00
|
|
|
/**
|
|
|
|
* Disables URL.createObjectURL usage.
|
|
|
|
* @var {boolean}
|
|
|
|
*/
|
2014-03-14 21:24:04 +09:00
|
|
|
PDFJS.disableCreateObjectURL = (PDFJS.disableCreateObjectURL === undefined ?
|
|
|
|
false : PDFJS.disableCreateObjectURL);
|
2014-01-11 07:30:41 +09:00
|
|
|
|
2014-02-13 23:44:58 +09:00
|
|
|
/**
|
|
|
|
* Disables WebGL usage.
|
|
|
|
* @var {boolean}
|
|
|
|
*/
|
|
|
|
PDFJS.disableWebGL = (PDFJS.disableWebGL === undefined ?
|
|
|
|
true : PDFJS.disableWebGL);
|
|
|
|
|
2013-12-19 06:39:03 +09:00
|
|
|
/**
|
|
|
|
* Controls the logging level.
|
|
|
|
* The constants from PDFJS.VERBOSITY_LEVELS should be used:
|
|
|
|
* - errors
|
|
|
|
* - warnings [default]
|
|
|
|
* - infos
|
2014-01-22 04:28:18 +09:00
|
|
|
* @var {number}
|
2013-12-19 06:39:03 +09:00
|
|
|
*/
|
2014-03-14 21:24:04 +09:00
|
|
|
PDFJS.verbosity = (PDFJS.verbosity === undefined ?
|
|
|
|
PDFJS.VERBOSITY_LEVELS.warnings : PDFJS.verbosity);
|
2013-12-19 06:39:03 +09:00
|
|
|
|
2014-01-22 04:28:18 +09:00
|
|
|
/**
|
|
|
|
* Document initialization / loading parameters object.
|
|
|
|
*
|
|
|
|
* @typedef {Object} DocumentInitParameters
|
|
|
|
* @property {string} url - The URL of the PDF.
|
|
|
|
* @property {TypedArray} data - A typed array with PDF data.
|
|
|
|
* @property {Object} httpHeaders - Basic authentication headers.
|
|
|
|
* @property {boolean} withCredentials - Indicates whether or not cross-site
|
|
|
|
* Access-Control requests should be made using credentials such as cookies
|
|
|
|
* or authorization headers. The default is false.
|
|
|
|
* @property {string} password - For decrypting password-protected PDFs.
|
|
|
|
* @property {TypedArray} initialData - A typed array with the first portion or
|
|
|
|
* all of the pdf data. Used by the extension since some data is already
|
|
|
|
* loaded before the switch to range requests.
|
|
|
|
*/
|
|
|
|
|
2012-04-14 01:25:08 +09:00
|
|
|
/**
|
|
|
|
* This is the main entry point for loading a PDF and interacting with it.
|
|
|
|
* NOTE: If a URL is used to fetch the PDF data a standard XMLHttpRequest(XHR)
|
|
|
|
* is used, which means it must follow the same origin rules that any XHR does
|
|
|
|
* e.g. No cross domain requests without CORS.
|
|
|
|
*
|
2014-01-22 04:28:18 +09:00
|
|
|
* @param {string|TypedArray|DocumentInitParameters} source Can be a url to
|
|
|
|
* where a PDF is located, a typed array (Uint8Array) already populated with
|
|
|
|
* data or parameter object.
|
2012-05-15 03:45:07 +09:00
|
|
|
*
|
2014-01-22 04:28:18 +09:00
|
|
|
* @param {Object} pdfDataRangeTransport is optional. It is used if you want
|
2013-02-07 08:19:29 +09:00
|
|
|
* to manually serve range requests for data in the PDF. See viewer.js for
|
|
|
|
* an example of pdfDataRangeTransport's interface.
|
|
|
|
*
|
2013-05-10 07:35:23 +09:00
|
|
|
* @param {function} passwordCallback is optional. It is used to request a
|
|
|
|
* password if wrong or no password was provided. The callback receives two
|
|
|
|
* parameters: function that needs to be called with new password and reason
|
|
|
|
* (see {PasswordResponses}).
|
|
|
|
*
|
2014-01-22 04:28:18 +09:00
|
|
|
* @return {Promise} A promise that is resolved with {@link PDFDocumentProxy}
|
|
|
|
* object.
|
2012-04-14 01:25:08 +09:00
|
|
|
*/
|
2013-05-10 07:35:23 +09:00
|
|
|
PDFJS.getDocument = function getDocument(source,
|
|
|
|
pdfDataRangeTransport,
|
2013-06-06 04:28:31 +09:00
|
|
|
passwordCallback,
|
|
|
|
progressCallback) {
|
2014-04-30 00:07:05 +09:00
|
|
|
var workerInitializedCapability, workerReadyCapability, transport;
|
2012-05-15 03:45:07 +09:00
|
|
|
|
2012-06-28 19:33:32 +09:00
|
|
|
if (typeof source === 'string') {
|
|
|
|
source = { url: source };
|
|
|
|
} else if (isArrayBuffer(source)) {
|
|
|
|
source = { data: source };
|
|
|
|
} else if (typeof source !== 'object') {
|
|
|
|
error('Invalid parameter in getDocument, need either Uint8Array, ' +
|
|
|
|
'string or a parameter object');
|
|
|
|
}
|
|
|
|
|
2014-03-14 21:24:04 +09:00
|
|
|
if (!source.url && !source.data) {
|
2012-06-24 04:48:33 +09:00
|
|
|
error('Invalid parameter array, need either .data or .url');
|
2014-03-14 21:24:04 +09:00
|
|
|
}
|
2012-06-24 04:48:33 +09:00
|
|
|
|
2012-07-27 02:11:28 +09:00
|
|
|
// copy/use all keys as is except 'url' -- full path is required
|
|
|
|
var params = {};
|
|
|
|
for (var key in source) {
|
|
|
|
if (key === 'url' && typeof window !== 'undefined') {
|
|
|
|
params[key] = combineUrl(window.location.href, source[key]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
params[key] = source[key];
|
|
|
|
}
|
|
|
|
|
2014-04-30 00:07:05 +09:00
|
|
|
workerInitializedCapability = createPromiseCapability();
|
|
|
|
workerReadyCapability = createPromiseCapability();
|
|
|
|
transport = new WorkerTransport(workerInitializedCapability,
|
|
|
|
workerReadyCapability, pdfDataRangeTransport,
|
|
|
|
progressCallback);
|
|
|
|
workerInitializedCapability.promise.then(function transportInitialized() {
|
2013-05-10 07:35:23 +09:00
|
|
|
transport.passwordCallback = passwordCallback;
|
2012-07-27 02:11:28 +09:00
|
|
|
transport.fetchDocument(params);
|
2012-06-28 19:26:16 +09:00
|
|
|
});
|
2014-04-30 00:07:05 +09:00
|
|
|
return workerReadyCapability.promise;
|
2012-04-13 04:11:22 +09:00
|
|
|
};
|
|
|
|
|
2012-04-14 01:25:08 +09:00
|
|
|
/**
|
|
|
|
* Proxy to a PDFDocument in the worker thread. Also, contains commonly used
|
|
|
|
* properties that can be read synchronously.
|
2014-01-22 04:28:18 +09:00
|
|
|
* @class
|
2012-04-14 01:25:08 +09:00
|
|
|
*/
|
2012-05-02 02:54:16 +09:00
|
|
|
var PDFDocumentProxy = (function PDFDocumentProxyClosure() {
|
2012-04-13 04:11:22 +09:00
|
|
|
function PDFDocumentProxy(pdfInfo, transport) {
|
|
|
|
this.pdfInfo = pdfInfo;
|
|
|
|
this.transport = transport;
|
|
|
|
}
|
2014-01-22 04:28:18 +09:00
|
|
|
PDFDocumentProxy.prototype = /** @lends PDFDocumentProxy.prototype */ {
|
2012-04-14 01:25:08 +09:00
|
|
|
/**
|
|
|
|
* @return {number} Total number of pages the PDF contains.
|
|
|
|
*/
|
2012-04-13 04:11:22 +09:00
|
|
|
get numPages() {
|
|
|
|
return this.pdfInfo.numPages;
|
|
|
|
},
|
2012-04-14 01:25:08 +09:00
|
|
|
/**
|
|
|
|
* @return {string} A unique ID to identify a PDF. Not guaranteed to be
|
|
|
|
* unique.
|
|
|
|
*/
|
2012-04-13 04:11:22 +09:00
|
|
|
get fingerprint() {
|
|
|
|
return this.pdfInfo.fingerprint;
|
|
|
|
},
|
2012-04-14 01:25:08 +09:00
|
|
|
/**
|
2014-01-22 04:28:18 +09:00
|
|
|
* @param {number} pageNumber The page number to get. The first page is 1.
|
|
|
|
* @return {Promise} A promise that is resolved with a {@link PDFPageProxy}
|
2012-04-14 01:25:08 +09:00
|
|
|
* object.
|
|
|
|
*/
|
2014-01-22 04:28:18 +09:00
|
|
|
getPage: function PDFDocumentProxy_getPage(pageNumber) {
|
|
|
|
return this.transport.getPage(pageNumber);
|
2012-04-13 04:11:22 +09:00
|
|
|
},
|
2013-11-14 08:27:46 +09:00
|
|
|
/**
|
2014-01-22 04:28:18 +09:00
|
|
|
* @param {{num: number, gen: number}} ref The page reference. Must have
|
|
|
|
* the 'num' and 'gen' properties.
|
2013-11-14 08:27:46 +09:00
|
|
|
* @return {Promise} A promise that is resolved with the page index that is
|
|
|
|
* associated with the reference.
|
|
|
|
*/
|
|
|
|
getPageIndex: function PDFDocumentProxy_getPageIndex(ref) {
|
|
|
|
return this.transport.getPageIndex(ref);
|
|
|
|
},
|
2012-04-14 01:25:08 +09:00
|
|
|
/**
|
|
|
|
* @return {Promise} A promise that is resolved with a lookup table for
|
|
|
|
* mapping named destinations to reference numbers.
|
|
|
|
*/
|
2012-05-02 02:48:07 +09:00
|
|
|
getDestinations: function PDFDocumentProxy_getDestinations() {
|
2013-02-07 08:19:29 +09:00
|
|
|
return this.transport.getDestinations();
|
2012-04-13 04:11:22 +09:00
|
|
|
},
|
2014-03-19 05:32:47 +09:00
|
|
|
/**
|
|
|
|
* @return {Promise} A promise that is resolved with a lookup table for
|
|
|
|
* mapping named attachments to their content.
|
|
|
|
*/
|
|
|
|
getAttachments: function PDFDocumentProxy_getAttachments() {
|
|
|
|
return this.transport.getAttachments();
|
|
|
|
},
|
2013-03-01 08:29:07 +09:00
|
|
|
/**
|
|
|
|
* @return {Promise} A promise that is resolved with an array of all the
|
|
|
|
* JavaScript strings in the name tree.
|
|
|
|
*/
|
2013-12-17 05:40:43 +09:00
|
|
|
getJavaScript: function PDFDocumentProxy_getJavaScript() {
|
2014-04-30 00:07:05 +09:00
|
|
|
return new Promise(function (resolve) {
|
|
|
|
var js = this.pdfInfo.javaScript;
|
|
|
|
resolve(js);
|
|
|
|
}.bind(this));
|
2013-03-01 08:29:07 +09:00
|
|
|
},
|
2012-04-14 01:25:08 +09:00
|
|
|
/**
|
2014-01-22 04:28:18 +09:00
|
|
|
* @return {Promise} A promise that is resolved with an {Array} that is a
|
2012-04-14 01:25:08 +09:00
|
|
|
* tree outline (if it has one) of the PDF. The tree is in the format of:
|
|
|
|
* [
|
|
|
|
* {
|
|
|
|
* title: string,
|
|
|
|
* bold: boolean,
|
|
|
|
* italic: boolean,
|
|
|
|
* color: rgb array,
|
|
|
|
* dest: dest obj,
|
|
|
|
* items: array of more items like this
|
|
|
|
* },
|
|
|
|
* ...
|
|
|
|
* ].
|
|
|
|
*/
|
2012-05-02 02:48:07 +09:00
|
|
|
getOutline: function PDFDocumentProxy_getOutline() {
|
2014-04-30 00:07:05 +09:00
|
|
|
return new Promise(function (resolve) {
|
|
|
|
var outline = this.pdfInfo.outline;
|
|
|
|
resolve(outline);
|
|
|
|
}.bind(this));
|
2012-04-13 04:11:22 +09:00
|
|
|
},
|
2012-04-14 01:25:08 +09:00
|
|
|
/**
|
2014-01-22 04:28:18 +09:00
|
|
|
* @return {Promise} A promise that is resolved with an {Object} that has
|
|
|
|
* info and metadata properties. Info is an {Object} filled with anything
|
2012-04-14 01:25:08 +09:00
|
|
|
* available in the information dictionary and similarly metadata is a
|
|
|
|
* {Metadata} object with information from the metadata section of the PDF.
|
|
|
|
*/
|
2012-05-02 02:48:07 +09:00
|
|
|
getMetadata: function PDFDocumentProxy_getMetadata() {
|
2014-04-30 00:07:05 +09:00
|
|
|
return new Promise(function (resolve) {
|
|
|
|
var info = this.pdfInfo.info;
|
|
|
|
var metadata = this.pdfInfo.metadata;
|
|
|
|
resolve({
|
|
|
|
info: info,
|
|
|
|
metadata: (metadata ? new PDFJS.Metadata(metadata) : null)
|
|
|
|
});
|
|
|
|
}.bind(this));
|
2012-04-13 04:11:22 +09:00
|
|
|
},
|
2012-06-02 06:17:09 +09:00
|
|
|
/**
|
|
|
|
* @return {Promise} A promise that is resolved with a TypedArray that has
|
|
|
|
* the raw data from the PDF.
|
|
|
|
*/
|
|
|
|
getData: function PDFDocumentProxy_getData() {
|
2014-04-30 00:07:05 +09:00
|
|
|
var capability = createPromiseCapability();
|
|
|
|
this.transport.getData(capability);
|
|
|
|
return capability.promise;
|
2012-06-02 06:17:09 +09:00
|
|
|
},
|
2013-02-07 08:19:29 +09:00
|
|
|
/**
|
|
|
|
* @return {Promise} A promise that is resolved when the document's data
|
2014-01-29 06:13:47 +09:00
|
|
|
* is loaded. It is resolved with an {Object} that contains the length
|
|
|
|
* property that indicates size of the PDF data in bytes.
|
2013-02-07 08:19:29 +09:00
|
|
|
*/
|
2014-01-29 06:13:47 +09:00
|
|
|
getDownloadInfo: function PDFDocumentProxy_getDownloadInfo() {
|
2014-04-30 00:07:05 +09:00
|
|
|
return this.transport.downloadInfoCapability.promise;
|
2013-02-07 08:19:29 +09:00
|
|
|
},
|
2014-01-22 04:28:18 +09:00
|
|
|
/**
|
|
|
|
* Cleans up resources allocated by the document, e.g. created @font-face.
|
|
|
|
*/
|
2013-11-15 06:43:38 +09:00
|
|
|
cleanup: function PDFDocumentProxy_cleanup() {
|
|
|
|
this.transport.startCleanup();
|
|
|
|
},
|
2014-01-22 04:28:18 +09:00
|
|
|
/**
|
|
|
|
* Destroys current document instance and terminates worker.
|
|
|
|
*/
|
2012-05-02 02:48:07 +09:00
|
|
|
destroy: function PDFDocumentProxy_destroy() {
|
2012-04-13 04:11:22 +09:00
|
|
|
this.transport.destroy();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return PDFDocumentProxy;
|
|
|
|
})();
|
|
|
|
|
2014-04-10 08:44:07 +09:00
|
|
|
/**
|
|
|
|
* Page text content.
|
|
|
|
*
|
|
|
|
* @typedef {Object} TextContent
|
|
|
|
* @property {array} items - array of {@link TextItem}
|
|
|
|
* @property {Object} styles - {@link TextStyles} objects, indexed by font
|
|
|
|
* name.
|
|
|
|
*/
|
|
|
|
|
2014-01-22 04:28:18 +09:00
|
|
|
/**
|
|
|
|
* Page text content part.
|
|
|
|
*
|
2014-04-10 08:44:07 +09:00
|
|
|
* @typedef {Object} TextItem
|
2014-01-22 04:28:18 +09:00
|
|
|
* @property {string} str - text content.
|
|
|
|
* @property {string} dir - text direction: 'ttb', 'ltr' or 'rtl'.
|
2014-04-10 08:44:07 +09:00
|
|
|
* @property {array} transform - transformation matrix.
|
|
|
|
* @property {number} width - width in device space.
|
|
|
|
* @property {number} height - height in device space.
|
|
|
|
* @property {string} fontName - font name used by pdf.js for converted font.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2014-04-12 00:57:48 +09:00
|
|
|
* Text style.
|
|
|
|
*
|
2014-04-10 08:44:07 +09:00
|
|
|
* @typedef {Object} TextStyle
|
|
|
|
* @property {number} ascent - font ascent.
|
|
|
|
* @property {number} descent - font descent.
|
|
|
|
* @property {boolean} vertical - text is in vertical mode.
|
|
|
|
* @property {string} fontFamily - possible font family
|
2014-01-22 04:28:18 +09:00
|
|
|
*/
|
|
|
|
|
2014-04-12 00:57:48 +09:00
|
|
|
/**
|
|
|
|
* Page render parameters.
|
|
|
|
*
|
|
|
|
* @typedef {Object} RenderParameters
|
|
|
|
* @property {Object} canvasContext - A 2D context of a DOM Canvas object.
|
|
|
|
* @property {PageViewport} viewport - Rendering viewport obtained by
|
|
|
|
* calling of PDFPage.getViewport method.
|
|
|
|
* @property {string} intent - Rendering intent, can be 'display' or 'print'
|
|
|
|
* (default value is 'display').
|
|
|
|
* @property {Object} imageLayer - (optional) An object that has beginLayout,
|
|
|
|
* endLayout and appendImage functions.
|
|
|
|
* @property {function} continueCallback - (optional) A function that will be
|
|
|
|
* called each time the rendering is paused. To continue
|
|
|
|
* rendering call the function that is the first argument
|
|
|
|
* to the callback.
|
|
|
|
*/
|
|
|
|
|
2014-01-22 04:28:18 +09:00
|
|
|
/**
|
|
|
|
* Proxy to a PDFPage in the worker thread.
|
|
|
|
* @class
|
|
|
|
*/
|
2012-04-13 04:11:22 +09:00
|
|
|
var PDFPageProxy = (function PDFPageProxyClosure() {
|
|
|
|
function PDFPageProxy(pageInfo, transport) {
|
|
|
|
this.pageInfo = pageInfo;
|
|
|
|
this.transport = transport;
|
2012-04-13 06:07:11 +09:00
|
|
|
this.stats = new StatTimer();
|
2012-04-13 06:11:22 +09:00
|
|
|
this.stats.enabled = !!globalScope.PDFJS.enableStats;
|
2012-10-29 05:10:34 +09:00
|
|
|
this.commonObjs = transport.commonObjs;
|
|
|
|
this.objs = new PDFObjects();
|
|
|
|
this.cleanupAfterRender = false;
|
2013-08-07 09:35:54 +09:00
|
|
|
this.pendingDestroy = false;
|
2014-03-07 23:48:42 +09:00
|
|
|
this.intentStates = {};
|
2012-04-13 04:11:22 +09:00
|
|
|
}
|
2014-01-22 04:28:18 +09:00
|
|
|
PDFPageProxy.prototype = /** @lends PDFPageProxy.prototype */ {
|
2012-04-14 01:25:08 +09:00
|
|
|
/**
|
|
|
|
* @return {number} Page number of the page. First page is 1.
|
|
|
|
*/
|
2012-04-13 04:11:22 +09:00
|
|
|
get pageNumber() {
|
|
|
|
return this.pageInfo.pageIndex + 1;
|
|
|
|
},
|
2012-04-14 01:25:08 +09:00
|
|
|
/**
|
|
|
|
* @return {number} The number of degrees the page is rotated clockwise.
|
|
|
|
*/
|
2012-04-13 04:11:22 +09:00
|
|
|
get rotate() {
|
|
|
|
return this.pageInfo.rotate;
|
|
|
|
},
|
2012-04-14 01:25:08 +09:00
|
|
|
/**
|
2014-01-22 04:28:18 +09:00
|
|
|
* @return {Object} The reference that points to this page. It has 'num' and
|
2012-04-14 01:25:08 +09:00
|
|
|
* 'gen' properties.
|
|
|
|
*/
|
2012-04-13 04:11:22 +09:00
|
|
|
get ref() {
|
|
|
|
return this.pageInfo.ref;
|
|
|
|
},
|
2012-04-14 01:25:08 +09:00
|
|
|
/**
|
2014-01-22 04:28:18 +09:00
|
|
|
* @return {Array} An array of the visible portion of the PDF page in the
|
2012-04-14 01:25:08 +09:00
|
|
|
* user space units - [x1, y1, x2, y2].
|
|
|
|
*/
|
2012-04-13 04:11:22 +09:00
|
|
|
get view() {
|
|
|
|
return this.pageInfo.view;
|
|
|
|
},
|
2012-04-14 01:25:08 +09:00
|
|
|
/**
|
|
|
|
* @param {number} scale The desired scale of the viewport.
|
|
|
|
* @param {number} rotate Degrees to rotate the viewport. If omitted this
|
|
|
|
* defaults to the page rotation.
|
|
|
|
* @return {PageViewport} Contains 'width' and 'height' properties along
|
|
|
|
* with transforms required for rendering.
|
|
|
|
*/
|
2012-05-02 02:48:07 +09:00
|
|
|
getViewport: function PDFPageProxy_getViewport(scale, rotate) {
|
2014-03-14 21:24:04 +09:00
|
|
|
if (arguments.length < 2) {
|
2012-04-13 04:11:22 +09:00
|
|
|
rotate = this.rotate;
|
2014-03-14 21:24:04 +09:00
|
|
|
}
|
2012-04-13 04:11:22 +09:00
|
|
|
return new PDFJS.PageViewport(this.view, scale, rotate, 0, 0);
|
|
|
|
},
|
2012-04-14 01:25:08 +09:00
|
|
|
/**
|
2014-01-22 04:28:18 +09:00
|
|
|
* @return {Promise} A promise that is resolved with an {Array} of the
|
2012-04-14 01:25:08 +09:00
|
|
|
* annotation objects.
|
|
|
|
*/
|
2012-05-02 02:48:07 +09:00
|
|
|
getAnnotations: function PDFPageProxy_getAnnotations() {
|
2014-04-30 00:07:05 +09:00
|
|
|
if (this.annotationsCapability) {
|
|
|
|
return this.annotationsCapability.promise;
|
2014-03-14 21:24:04 +09:00
|
|
|
}
|
2012-04-15 05:54:31 +09:00
|
|
|
|
2014-04-30 00:07:05 +09:00
|
|
|
var capability = createPromiseCapability();
|
|
|
|
this.annotationsCapability = capability;
|
2012-04-15 05:54:31 +09:00
|
|
|
this.transport.getAnnotations(this.pageInfo.pageIndex);
|
2014-04-30 00:07:05 +09:00
|
|
|
return capability.promise;
|
2012-04-13 04:11:22 +09:00
|
|
|
},
|
2012-04-14 01:25:08 +09:00
|
|
|
/**
|
|
|
|
* Begins the process of rendering a page to the desired context.
|
2014-04-12 00:57:48 +09:00
|
|
|
* @param {RenderParameters} params Page render parameters.
|
|
|
|
* @return {RenderTask} An object that contains the promise, which
|
|
|
|
* is resolved when the page finishes rendering.
|
2012-04-14 01:25:08 +09:00
|
|
|
*/
|
2012-05-02 02:48:07 +09:00
|
|
|
render: function PDFPageProxy_render(params) {
|
2012-04-13 04:11:22 +09:00
|
|
|
var stats = this.stats;
|
|
|
|
stats.time('Overall');
|
2013-08-01 03:17:36 +09:00
|
|
|
|
2013-08-07 09:35:54 +09:00
|
|
|
// If there was a pending destroy cancel it so no cleanup happens during
|
|
|
|
// this call to render.
|
|
|
|
this.pendingDestroy = false;
|
|
|
|
|
2014-03-14 21:24:04 +09:00
|
|
|
var renderingIntent = ('intent' in params ?
|
|
|
|
(params.intent == 'print' ? 'print' : 'display') : 'display');
|
2014-03-07 23:48:42 +09:00
|
|
|
|
|
|
|
if (!this.intentStates[renderingIntent]) {
|
|
|
|
this.intentStates[renderingIntent] = {};
|
|
|
|
}
|
|
|
|
var intentState = this.intentStates[renderingIntent];
|
|
|
|
|
2014-04-30 00:07:05 +09:00
|
|
|
// If there's no displayReadyCapability yet, then the operatorList
|
|
|
|
// was never requested before. Make the request and create the promise.
|
|
|
|
if (!intentState.displayReadyCapability) {
|
2014-03-07 23:48:42 +09:00
|
|
|
intentState.receivingOperatorList = true;
|
2014-04-30 00:07:05 +09:00
|
|
|
intentState.displayReadyCapability = createPromiseCapability();
|
2014-03-07 23:48:42 +09:00
|
|
|
intentState.operatorList = {
|
2013-08-01 03:17:36 +09:00
|
|
|
fnArray: [],
|
|
|
|
argsArray: [],
|
|
|
|
lastChunk: false
|
|
|
|
};
|
2012-04-13 04:11:22 +09:00
|
|
|
|
|
|
|
this.stats.time('Page Request');
|
|
|
|
this.transport.messageHandler.send('RenderPageRequest', {
|
2014-03-07 23:48:42 +09:00
|
|
|
pageIndex: this.pageNumber - 1,
|
|
|
|
intent: renderingIntent
|
2012-04-13 04:11:22 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-08-01 03:17:36 +09:00
|
|
|
var internalRenderTask = new InternalRenderTask(complete, params,
|
2014-03-14 21:24:04 +09:00
|
|
|
this.objs,
|
|
|
|
this.commonObjs,
|
|
|
|
intentState.operatorList,
|
|
|
|
this.pageNumber);
|
2014-03-07 23:48:42 +09:00
|
|
|
if (!intentState.renderTasks) {
|
|
|
|
intentState.renderTasks = [];
|
|
|
|
}
|
|
|
|
intentState.renderTasks.push(internalRenderTask);
|
2013-08-01 03:17:36 +09:00
|
|
|
var renderTask = new RenderTask(internalRenderTask);
|
2012-04-13 04:11:22 +09:00
|
|
|
|
2013-08-01 03:17:36 +09:00
|
|
|
var self = this;
|
2014-04-30 00:07:05 +09:00
|
|
|
intentState.displayReadyCapability.promise.then(
|
2013-08-01 03:17:36 +09:00
|
|
|
function pageDisplayReadyPromise(transparency) {
|
2013-08-07 09:35:54 +09:00
|
|
|
if (self.pendingDestroy) {
|
2012-04-17 06:46:26 +09:00
|
|
|
complete();
|
|
|
|
return;
|
|
|
|
}
|
2013-08-01 03:17:36 +09:00
|
|
|
stats.time('Rendering');
|
|
|
|
internalRenderTask.initalizeGraphics(transparency);
|
|
|
|
internalRenderTask.operatorListChanged();
|
|
|
|
},
|
2012-04-13 04:11:22 +09:00
|
|
|
function pageDisplayReadPromiseError(reason) {
|
2012-04-13 06:07:11 +09:00
|
|
|
complete(reason);
|
2012-04-13 04:11:22 +09:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2013-08-01 03:17:36 +09:00
|
|
|
function complete(error) {
|
2014-03-07 23:48:42 +09:00
|
|
|
var i = intentState.renderTasks.indexOf(internalRenderTask);
|
2013-08-01 03:17:36 +09:00
|
|
|
if (i >= 0) {
|
2014-03-07 23:48:42 +09:00
|
|
|
intentState.renderTasks.splice(i, 1);
|
2012-04-13 04:11:22 +09:00
|
|
|
}
|
2013-08-01 03:17:36 +09:00
|
|
|
|
2013-08-07 09:35:54 +09:00
|
|
|
if (self.cleanupAfterRender) {
|
|
|
|
self.pendingDestroy = true;
|
2012-08-30 06:11:56 +09:00
|
|
|
}
|
2013-08-07 09:35:54 +09:00
|
|
|
self._tryDestroy();
|
|
|
|
|
2013-08-01 03:17:36 +09:00
|
|
|
if (error) {
|
2014-04-30 00:07:05 +09:00
|
|
|
internalRenderTask.capability.reject(error);
|
2013-08-01 03:17:36 +09:00
|
|
|
} else {
|
2014-04-30 00:07:05 +09:00
|
|
|
internalRenderTask.capability.resolve();
|
2012-12-01 08:31:22 +09:00
|
|
|
}
|
2013-08-01 03:17:36 +09:00
|
|
|
stats.timeEnd('Rendering');
|
|
|
|
stats.timeEnd('Overall');
|
2012-04-13 04:11:22 +09:00
|
|
|
}
|
|
|
|
|
2013-08-01 03:17:36 +09:00
|
|
|
return renderTask;
|
2012-04-13 04:11:22 +09:00
|
|
|
},
|
2012-04-14 01:25:08 +09:00
|
|
|
/**
|
2014-04-10 08:44:07 +09:00
|
|
|
* @return {Promise} That is resolved a {@link TextContent}
|
|
|
|
* object that represent the page text content.
|
2012-04-14 01:25:08 +09:00
|
|
|
*/
|
2012-05-02 02:48:07 +09:00
|
|
|
getTextContent: function PDFPageProxy_getTextContent() {
|
2014-04-30 00:07:05 +09:00
|
|
|
return new Promise(function (resolve) {
|
|
|
|
this.transport.messageHandler.send('GetTextContent', {
|
|
|
|
pageIndex: this.pageNumber - 1
|
|
|
|
},
|
|
|
|
function textContentCallback(textContent) {
|
|
|
|
resolve(textContent);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}.bind(this));
|
2012-04-13 04:11:22 +09:00
|
|
|
},
|
2012-04-17 04:13:41 +09:00
|
|
|
/**
|
2012-04-17 05:28:34 +09:00
|
|
|
* Destroys resources allocated by the page.
|
2012-04-17 04:13:41 +09:00
|
|
|
*/
|
2012-05-02 02:48:07 +09:00
|
|
|
destroy: function PDFPageProxy_destroy() {
|
2013-08-07 09:35:54 +09:00
|
|
|
this.pendingDestroy = true;
|
|
|
|
this._tryDestroy();
|
2013-08-01 03:17:36 +09:00
|
|
|
},
|
|
|
|
/**
|
2013-08-07 09:35:54 +09:00
|
|
|
* For internal use only. Attempts to clean up if rendering is in a state
|
|
|
|
* where that's possible.
|
2014-01-22 04:28:18 +09:00
|
|
|
* @ignore
|
2013-08-01 03:17:36 +09:00
|
|
|
*/
|
2013-08-07 09:35:54 +09:00
|
|
|
_tryDestroy: function PDFPageProxy__destroy() {
|
|
|
|
if (!this.pendingDestroy ||
|
2014-03-07 23:48:42 +09:00
|
|
|
Object.keys(this.intentStates).some(function(intent) {
|
|
|
|
var intentState = this.intentStates[intent];
|
2014-03-14 21:24:04 +09:00
|
|
|
return (intentState.renderTasks.length !== 0 ||
|
|
|
|
intentState.receivingOperatorList);
|
2014-03-07 23:48:42 +09:00
|
|
|
}, this)) {
|
2013-08-07 09:35:54 +09:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-07 23:48:42 +09:00
|
|
|
Object.keys(this.intentStates).forEach(function(intent) {
|
|
|
|
delete this.intentStates[intent];
|
|
|
|
}, this);
|
2013-08-01 03:17:36 +09:00
|
|
|
this.objs.clear();
|
2013-08-07 09:35:54 +09:00
|
|
|
this.pendingDestroy = false;
|
2013-08-01 03:17:36 +09:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* For internal use only.
|
2014-01-22 04:28:18 +09:00
|
|
|
* @ignore
|
2013-08-01 03:17:36 +09:00
|
|
|
*/
|
2014-03-07 23:48:42 +09:00
|
|
|
_startRenderPage: function PDFPageProxy_startRenderPage(transparency,
|
|
|
|
intent) {
|
|
|
|
var intentState = this.intentStates[intent];
|
2014-04-30 00:07:05 +09:00
|
|
|
intentState.displayReadyCapability.resolve(transparency);
|
2013-08-01 03:17:36 +09:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* For internal use only.
|
2014-01-22 04:28:18 +09:00
|
|
|
* @ignore
|
2013-08-01 03:17:36 +09:00
|
|
|
*/
|
2014-03-07 23:48:42 +09:00
|
|
|
_renderPageChunk: function PDFPageProxy_renderPageChunk(operatorListChunk,
|
|
|
|
intent) {
|
|
|
|
var intentState = this.intentStates[intent];
|
2014-04-09 04:48:16 +09:00
|
|
|
var i, ii;
|
2013-08-01 03:17:36 +09:00
|
|
|
// Add the new chunk to the current operator list.
|
2014-04-09 04:48:16 +09:00
|
|
|
for (i = 0, ii = operatorListChunk.length; i < ii; i++) {
|
2014-03-07 23:48:42 +09:00
|
|
|
intentState.operatorList.fnArray.push(operatorListChunk.fnArray[i]);
|
|
|
|
intentState.operatorList.argsArray.push(
|
|
|
|
operatorListChunk.argsArray[i]);
|
2013-11-14 04:43:38 +09:00
|
|
|
}
|
2014-03-07 23:48:42 +09:00
|
|
|
intentState.operatorList.lastChunk = operatorListChunk.lastChunk;
|
2013-08-01 03:17:36 +09:00
|
|
|
|
|
|
|
// Notify all the rendering tasks there are more operators to be consumed.
|
2014-04-09 04:48:16 +09:00
|
|
|
for (i = 0; i < intentState.renderTasks.length; i++) {
|
2014-03-07 23:48:42 +09:00
|
|
|
intentState.renderTasks[i].operatorListChanged();
|
2012-04-17 04:49:55 +09:00
|
|
|
}
|
2013-08-07 09:35:54 +09:00
|
|
|
|
|
|
|
if (operatorListChunk.lastChunk) {
|
2014-03-07 23:48:42 +09:00
|
|
|
intentState.receivingOperatorList = false;
|
2013-08-07 09:35:54 +09:00
|
|
|
this._tryDestroy();
|
|
|
|
}
|
2012-04-13 04:11:22 +09:00
|
|
|
}
|
|
|
|
};
|
|
|
|
return PDFPageProxy;
|
|
|
|
})();
|
2014-01-22 04:28:18 +09:00
|
|
|
|
2012-04-14 01:25:08 +09:00
|
|
|
/**
|
|
|
|
* For internal use only.
|
2014-01-22 04:28:18 +09:00
|
|
|
* @ignore
|
2012-04-14 01:25:08 +09:00
|
|
|
*/
|
2012-04-13 04:11:22 +09:00
|
|
|
var WorkerTransport = (function WorkerTransportClosure() {
|
2014-04-30 00:07:05 +09:00
|
|
|
function WorkerTransport(workerInitializedCapability, workerReadyCapability,
|
2014-03-14 21:24:04 +09:00
|
|
|
pdfDataRangeTransport, progressCallback) {
|
2013-02-07 08:19:29 +09:00
|
|
|
this.pdfDataRangeTransport = pdfDataRangeTransport;
|
|
|
|
|
2014-04-30 00:07:05 +09:00
|
|
|
this.workerReadyCapability = workerReadyCapability;
|
2013-06-06 04:28:31 +09:00
|
|
|
this.progressCallback = progressCallback;
|
2012-10-29 05:10:34 +09:00
|
|
|
this.commonObjs = new PDFObjects();
|
2012-04-12 07:52:15 +09:00
|
|
|
|
|
|
|
this.pageCache = [];
|
2014-04-30 00:07:05 +09:00
|
|
|
this.pageCapabilities = [];
|
|
|
|
this.downloadInfoCapability = createPromiseCapability();
|
2013-05-10 07:35:23 +09:00
|
|
|
this.passwordCallback = null;
|
|
|
|
|
2012-04-12 07:52:15 +09:00
|
|
|
// If worker support isn't disabled explicit and the browser has worker
|
|
|
|
// support, create a new web worker and test if it/the browser fullfills
|
|
|
|
// all requirements to run parts of pdf.js in a web worker.
|
|
|
|
// Right now, the requirement is, that an Uint8Array is still an Uint8Array
|
|
|
|
// as it arrives on the worker. Chrome added this with version 15.
|
2013-12-20 00:18:47 +09:00
|
|
|
//#if !SINGLE_FILE
|
2012-04-12 07:52:15 +09:00
|
|
|
if (!globalScope.PDFJS.disableWorker && typeof Worker !== 'undefined') {
|
|
|
|
var workerSrc = PDFJS.workerSrc;
|
2013-10-16 05:41:49 +09:00
|
|
|
if (!workerSrc) {
|
2012-04-12 07:52:15 +09:00
|
|
|
error('No PDFJS.workerSrc specified');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2012-08-02 03:29:13 +09:00
|
|
|
// Some versions of FF can't create a worker on localhost, see:
|
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=683280
|
2012-10-30 04:44:18 +09:00
|
|
|
var worker = new Worker(workerSrc);
|
2012-04-12 07:52:15 +09:00
|
|
|
var messageHandler = new MessageHandler('main', worker);
|
2012-04-12 08:47:42 +09:00
|
|
|
this.messageHandler = messageHandler;
|
2012-04-12 07:52:15 +09:00
|
|
|
|
2013-11-12 12:30:26 +09:00
|
|
|
messageHandler.on('test', function transportTest(data) {
|
|
|
|
var supportTypedArray = data && data.supportTypedArray;
|
2012-04-12 07:52:15 +09:00
|
|
|
if (supportTypedArray) {
|
|
|
|
this.worker = worker;
|
2013-11-12 12:30:26 +09:00
|
|
|
if (!data.supportTransfers) {
|
|
|
|
PDFJS.postMessageTransfers = false;
|
|
|
|
}
|
2012-04-12 07:52:15 +09:00
|
|
|
this.setupMessageHandler(messageHandler);
|
2014-04-30 00:07:05 +09:00
|
|
|
workerInitializedCapability.resolve();
|
2012-04-12 07:52:15 +09:00
|
|
|
} else {
|
|
|
|
globalScope.PDFJS.disableWorker = true;
|
2013-10-03 01:22:33 +09:00
|
|
|
this.loadFakeWorkerFiles().then(function() {
|
|
|
|
this.setupFakeWorker();
|
2014-04-30 00:07:05 +09:00
|
|
|
workerInitializedCapability.resolve();
|
2013-10-03 01:22:33 +09:00
|
|
|
}.bind(this));
|
2012-04-12 07:52:15 +09:00
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
|
2013-11-12 12:30:26 +09:00
|
|
|
var testObj = new Uint8Array([PDFJS.postMessageTransfers ? 255 : 0]);
|
|
|
|
// Some versions of Opera throw a DATA_CLONE_ERR on serializing the
|
|
|
|
// typed array. Also, checking if we can use transfers.
|
|
|
|
try {
|
|
|
|
messageHandler.send('test', testObj, null, [testObj.buffer]);
|
|
|
|
} catch (ex) {
|
|
|
|
info('Cannot use postMessage transfers');
|
|
|
|
testObj[0] = 0;
|
|
|
|
messageHandler.send('test', testObj);
|
|
|
|
}
|
2012-04-12 07:52:15 +09:00
|
|
|
return;
|
|
|
|
} catch (e) {
|
2012-05-15 09:19:09 +09:00
|
|
|
info('The worker has been disabled.');
|
2012-04-12 07:52:15 +09:00
|
|
|
}
|
|
|
|
}
|
2014-04-03 20:41:24 +09:00
|
|
|
//#endif
|
2012-04-12 07:52:15 +09:00
|
|
|
// Either workers are disabled, not supported or have thrown an exception.
|
|
|
|
// Thus, we fallback to a faked worker.
|
|
|
|
globalScope.PDFJS.disableWorker = true;
|
2013-08-13 02:48:06 +09:00
|
|
|
this.loadFakeWorkerFiles().then(function() {
|
|
|
|
this.setupFakeWorker();
|
2014-04-30 00:07:05 +09:00
|
|
|
workerInitializedCapability.resolve();
|
2013-08-13 02:48:06 +09:00
|
|
|
}.bind(this));
|
2012-04-12 07:52:15 +09:00
|
|
|
}
|
|
|
|
WorkerTransport.prototype = {
|
2012-04-12 09:09:55 +09:00
|
|
|
destroy: function WorkerTransport_destroy() {
|
2012-04-13 02:01:07 +09:00
|
|
|
this.pageCache = [];
|
2014-04-30 00:07:05 +09:00
|
|
|
this.pageCapabilities = [];
|
2013-04-13 03:37:49 +09:00
|
|
|
var self = this;
|
|
|
|
this.messageHandler.send('Terminate', null, function () {
|
2014-01-22 04:28:18 +09:00
|
|
|
FontLoader.clear();
|
2013-04-13 03:37:49 +09:00
|
|
|
if (self.worker) {
|
|
|
|
self.worker.terminate();
|
|
|
|
}
|
|
|
|
});
|
2012-04-12 09:09:55 +09:00
|
|
|
},
|
2013-08-13 02:48:06 +09:00
|
|
|
|
|
|
|
loadFakeWorkerFiles: function WorkerTransport_loadFakeWorkerFiles() {
|
2014-04-30 00:07:05 +09:00
|
|
|
if (!PDFJS.fakeWorkerFilesLoadedCapability) {
|
|
|
|
PDFJS.fakeWorkerFilesLoadedCapability = createPromiseCapability();
|
2013-08-13 02:48:06 +09:00
|
|
|
// In the developer build load worker_loader which in turn loads all the
|
|
|
|
// other files and resolves the promise. In production only the
|
|
|
|
// pdf.worker.js file is needed.
|
|
|
|
//#if !PRODUCTION
|
|
|
|
Util.loadScript(PDFJS.workerSrc);
|
2013-12-20 00:18:47 +09:00
|
|
|
//#endif
|
|
|
|
//#if PRODUCTION && SINGLE_FILE
|
2014-04-30 00:07:05 +09:00
|
|
|
// PDFJS.fakeWorkerFilesLoadedCapability.resolve();
|
2013-12-20 00:18:47 +09:00
|
|
|
//#endif
|
|
|
|
//#if PRODUCTION && !SINGLE_FILE
|
2013-08-13 02:48:06 +09:00
|
|
|
// Util.loadScript(PDFJS.workerSrc, function() {
|
2014-04-30 00:07:05 +09:00
|
|
|
// PDFJS.fakeWorkerFilesLoadedCapability.resolve();
|
2013-08-13 02:48:06 +09:00
|
|
|
// });
|
|
|
|
//#endif
|
|
|
|
}
|
2014-04-30 00:07:05 +09:00
|
|
|
return PDFJS.fakeWorkerFilesLoadedCapability.promise;
|
2013-08-13 02:48:06 +09:00
|
|
|
},
|
|
|
|
|
2012-04-12 07:52:15 +09:00
|
|
|
setupFakeWorker: function WorkerTransport_setupFakeWorker() {
|
2012-09-23 01:44:49 +09:00
|
|
|
warn('Setting up fake worker.');
|
2012-04-12 07:52:15 +09:00
|
|
|
// If we don't use a worker, just post/sendMessage to the main thread.
|
|
|
|
var fakeWorker = {
|
|
|
|
postMessage: function WorkerTransport_postMessage(obj) {
|
|
|
|
fakeWorker.onmessage({data: obj});
|
|
|
|
},
|
|
|
|
terminate: function WorkerTransport_terminate() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
var messageHandler = new MessageHandler('main', fakeWorker);
|
|
|
|
this.setupMessageHandler(messageHandler);
|
|
|
|
|
|
|
|
// If the main thread is our worker, setup the handling for the messages
|
|
|
|
// the main thread sends to it self.
|
2013-08-13 02:48:06 +09:00
|
|
|
PDFJS.WorkerMessageHandler.setup(messageHandler);
|
2012-04-12 07:52:15 +09:00
|
|
|
},
|
|
|
|
|
2012-04-12 09:09:55 +09:00
|
|
|
setupMessageHandler:
|
|
|
|
function WorkerTransport_setupMessageHandler(messageHandler) {
|
2012-04-12 07:52:15 +09:00
|
|
|
this.messageHandler = messageHandler;
|
|
|
|
|
2013-05-10 07:35:23 +09:00
|
|
|
function updatePassword(password) {
|
|
|
|
messageHandler.send('UpdatePassword', password);
|
|
|
|
}
|
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
var pdfDataRangeTransport = this.pdfDataRangeTransport;
|
|
|
|
if (pdfDataRangeTransport) {
|
2013-04-20 05:53:22 +09:00
|
|
|
pdfDataRangeTransport.addRangeListener(function(begin, chunk) {
|
2013-02-07 08:19:29 +09:00
|
|
|
messageHandler.send('OnDataRange', {
|
|
|
|
begin: begin,
|
|
|
|
chunk: chunk
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-04-20 05:53:22 +09:00
|
|
|
pdfDataRangeTransport.addProgressListener(function(loaded) {
|
|
|
|
messageHandler.send('OnDataProgress', {
|
|
|
|
loaded: loaded
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
messageHandler.on('RequestDataRange',
|
|
|
|
function transportDataRange(data) {
|
|
|
|
pdfDataRangeTransport.requestDataRange(data.begin, data.end);
|
|
|
|
}, this);
|
|
|
|
}
|
|
|
|
|
2012-04-13 04:11:22 +09:00
|
|
|
messageHandler.on('GetDoc', function transportDoc(data) {
|
2012-04-12 07:52:15 +09:00
|
|
|
var pdfInfo = data.pdfInfo;
|
2014-02-24 03:16:14 +09:00
|
|
|
this.numPages = data.pdfInfo.numPages;
|
2012-04-13 04:11:22 +09:00
|
|
|
var pdfDocument = new PDFDocumentProxy(pdfInfo, this);
|
2012-04-12 07:52:15 +09:00
|
|
|
this.pdfDocument = pdfDocument;
|
2014-04-30 00:07:05 +09:00
|
|
|
this.workerReadyCapability.resolve(pdfDocument);
|
2012-04-12 07:52:15 +09:00
|
|
|
}, this);
|
|
|
|
|
2012-05-15 03:45:07 +09:00
|
|
|
messageHandler.on('NeedPassword', function transportPassword(data) {
|
2013-05-10 07:35:23 +09:00
|
|
|
if (this.passwordCallback) {
|
|
|
|
return this.passwordCallback(updatePassword,
|
|
|
|
PasswordResponses.NEED_PASSWORD);
|
|
|
|
}
|
2014-04-30 00:07:05 +09:00
|
|
|
this.workerReadyCapability.reject(data.exception.message,
|
|
|
|
data.exception);
|
2012-05-15 03:45:07 +09:00
|
|
|
}, this);
|
|
|
|
|
|
|
|
messageHandler.on('IncorrectPassword', function transportBadPass(data) {
|
2013-05-10 07:35:23 +09:00
|
|
|
if (this.passwordCallback) {
|
|
|
|
return this.passwordCallback(updatePassword,
|
|
|
|
PasswordResponses.INCORRECT_PASSWORD);
|
|
|
|
}
|
2014-04-30 00:07:05 +09:00
|
|
|
this.workerReadyCapability.reject(data.exception.message,
|
|
|
|
data.exception);
|
2012-05-15 03:45:07 +09:00
|
|
|
}, this);
|
|
|
|
|
2012-10-16 19:10:37 +09:00
|
|
|
messageHandler.on('InvalidPDF', function transportInvalidPDF(data) {
|
2014-04-30 00:07:05 +09:00
|
|
|
this.workerReadyCapability.reject(data.exception.name, data.exception);
|
2012-10-16 19:10:37 +09:00
|
|
|
}, this);
|
|
|
|
|
2013-01-30 03:13:28 +09:00
|
|
|
messageHandler.on('MissingPDF', function transportMissingPDF(data) {
|
2014-04-30 00:07:05 +09:00
|
|
|
this.workerReadyCapability.reject(data.exception.message,
|
|
|
|
data.exception);
|
2013-01-30 03:13:28 +09:00
|
|
|
}, this);
|
|
|
|
|
2012-10-16 19:10:37 +09:00
|
|
|
messageHandler.on('UnknownError', function transportUnknownError(data) {
|
2014-04-30 00:07:05 +09:00
|
|
|
this.workerReadyCapability.reject(data.exception.message,
|
|
|
|
data.exception);
|
2012-10-16 19:10:37 +09:00
|
|
|
}, this);
|
|
|
|
|
2014-01-29 06:13:47 +09:00
|
|
|
messageHandler.on('DataLoaded', function transportPage(data) {
|
2014-04-30 00:07:05 +09:00
|
|
|
this.downloadInfoCapability.resolve(data);
|
2014-01-29 06:13:47 +09:00
|
|
|
}, this);
|
|
|
|
|
2012-04-13 04:11:22 +09:00
|
|
|
messageHandler.on('GetPage', function transportPage(data) {
|
2012-04-12 07:52:15 +09:00
|
|
|
var pageInfo = data.pageInfo;
|
2012-04-13 04:11:22 +09:00
|
|
|
var page = new PDFPageProxy(pageInfo, this);
|
2012-04-13 02:01:07 +09:00
|
|
|
this.pageCache[pageInfo.pageIndex] = page;
|
2014-04-30 00:07:05 +09:00
|
|
|
var promise = this.pageCapabilities[pageInfo.pageIndex];
|
2012-04-13 02:01:07 +09:00
|
|
|
promise.resolve(page);
|
2012-04-12 07:52:15 +09:00
|
|
|
}, this);
|
|
|
|
|
2012-04-15 05:54:31 +09:00
|
|
|
messageHandler.on('GetAnnotations', function transportAnnotations(data) {
|
|
|
|
var annotations = data.annotations;
|
2014-04-30 00:07:05 +09:00
|
|
|
var promise = this.pageCache[data.pageIndex].annotationsCapability;
|
2012-04-15 05:54:31 +09:00
|
|
|
promise.resolve(annotations);
|
|
|
|
}, this);
|
|
|
|
|
2013-08-01 03:17:36 +09:00
|
|
|
messageHandler.on('StartRenderPage', function transportRender(data) {
|
2012-04-13 02:01:07 +09:00
|
|
|
var page = this.pageCache[data.pageIndex];
|
2012-04-12 07:52:15 +09:00
|
|
|
|
2012-04-13 02:01:07 +09:00
|
|
|
page.stats.timeEnd('Page Request');
|
2014-03-07 23:48:42 +09:00
|
|
|
page._startRenderPage(data.transparency, data.intent);
|
2013-08-01 03:17:36 +09:00
|
|
|
}, this);
|
|
|
|
|
|
|
|
messageHandler.on('RenderPageChunk', function transportRender(data) {
|
|
|
|
var page = this.pageCache[data.pageIndex];
|
|
|
|
|
2014-03-07 23:48:42 +09:00
|
|
|
page._renderPageChunk(data.operatorList, data.intent);
|
2012-04-12 07:52:15 +09:00
|
|
|
}, this);
|
|
|
|
|
2012-10-29 05:10:34 +09:00
|
|
|
messageHandler.on('commonobj', function transportObj(data) {
|
2012-04-12 07:52:15 +09:00
|
|
|
var id = data[0];
|
|
|
|
var type = data[1];
|
2014-03-14 21:24:04 +09:00
|
|
|
if (this.commonObjs.hasData(id)) {
|
2012-04-17 08:44:51 +09:00
|
|
|
return;
|
2014-03-14 21:24:04 +09:00
|
|
|
}
|
2012-04-12 07:52:15 +09:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case 'Font':
|
2012-09-13 09:31:04 +09:00
|
|
|
var exportedData = data[2];
|
2012-04-12 07:52:15 +09:00
|
|
|
|
2012-08-30 06:11:56 +09:00
|
|
|
var font;
|
2013-08-01 03:17:36 +09:00
|
|
|
if ('error' in exportedData) {
|
2013-08-01 06:01:55 +09:00
|
|
|
var error = exportedData.error;
|
|
|
|
warn('Error during font loading: ' + error);
|
|
|
|
this.commonObjs.resolve(id, error);
|
2013-08-01 03:17:36 +09:00
|
|
|
break;
|
|
|
|
} else {
|
2013-08-13 02:48:06 +09:00
|
|
|
font = new FontFace(exportedData);
|
2013-08-01 03:17:36 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
FontLoader.bind(
|
|
|
|
[font],
|
|
|
|
function fontReady(fontObjs) {
|
|
|
|
this.commonObjs.resolve(id, font);
|
|
|
|
}.bind(this)
|
|
|
|
);
|
2012-10-29 05:10:34 +09:00
|
|
|
break;
|
2013-08-20 08:33:20 +09:00
|
|
|
case 'FontPath':
|
|
|
|
this.commonObjs.resolve(id, data[2]);
|
|
|
|
break;
|
2012-10-29 05:10:34 +09:00
|
|
|
default:
|
|
|
|
error('Got unknown common object type ' + type);
|
|
|
|
}
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
messageHandler.on('obj', function transportObj(data) {
|
|
|
|
var id = data[0];
|
|
|
|
var pageIndex = data[1];
|
|
|
|
var type = data[2];
|
|
|
|
var pageProxy = this.pageCache[pageIndex];
|
2014-04-09 04:48:16 +09:00
|
|
|
var imageData;
|
2014-03-07 23:48:42 +09:00
|
|
|
if (pageProxy.objs.hasData(id)) {
|
2012-10-29 05:10:34 +09:00
|
|
|
return;
|
2014-03-07 23:48:42 +09:00
|
|
|
}
|
2012-10-29 05:10:34 +09:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case 'JpegStream':
|
2014-04-09 04:48:16 +09:00
|
|
|
imageData = data[3];
|
2012-10-29 05:10:34 +09:00
|
|
|
loadJpegStream(id, imageData, pageProxy.objs);
|
|
|
|
break;
|
|
|
|
case 'Image':
|
2014-04-09 04:48:16 +09:00
|
|
|
imageData = data[3];
|
2012-10-29 05:10:34 +09:00
|
|
|
pageProxy.objs.resolve(id, imageData);
|
|
|
|
|
|
|
|
// heuristics that will allow not to store large data
|
|
|
|
var MAX_IMAGE_SIZE_TO_STORE = 8000000;
|
2014-04-15 05:22:35 +09:00
|
|
|
if (imageData && 'data' in imageData &&
|
2012-10-29 05:10:34 +09:00
|
|
|
imageData.data.length > MAX_IMAGE_SIZE_TO_STORE) {
|
|
|
|
pageProxy.cleanupAfterRender = true;
|
|
|
|
}
|
2012-04-12 07:52:15 +09:00
|
|
|
break;
|
|
|
|
default:
|
2012-10-29 05:10:34 +09:00
|
|
|
error('Got unknown object type ' + type);
|
2012-04-12 07:52:15 +09:00
|
|
|
}
|
|
|
|
}, this);
|
|
|
|
|
2012-06-24 04:48:33 +09:00
|
|
|
messageHandler.on('DocProgress', function transportDocProgress(data) {
|
2013-06-15 23:04:54 +09:00
|
|
|
if (this.progressCallback) {
|
|
|
|
this.progressCallback({
|
|
|
|
loaded: data.loaded,
|
|
|
|
total: data.total
|
|
|
|
});
|
|
|
|
}
|
2012-06-24 04:48:33 +09:00
|
|
|
}, this);
|
|
|
|
|
|
|
|
messageHandler.on('DocError', function transportDocError(data) {
|
2014-04-30 00:07:05 +09:00
|
|
|
this.workerReadyCapability.reject(data);
|
2012-06-24 04:48:33 +09:00
|
|
|
}, this);
|
|
|
|
|
2014-03-07 23:48:42 +09:00
|
|
|
messageHandler.on('PageError', function transportError(data, intent) {
|
2012-04-13 07:14:18 +09:00
|
|
|
var page = this.pageCache[data.pageNum - 1];
|
2014-03-07 23:48:42 +09:00
|
|
|
var intentState = page.intentStates[intent];
|
2014-04-30 00:07:05 +09:00
|
|
|
if (intentState.displayReadyCapability.promise) {
|
|
|
|
intentState.displayReadyCapability.reject(data.error);
|
2014-03-14 21:24:04 +09:00
|
|
|
} else {
|
2012-04-12 07:52:15 +09:00
|
|
|
error(data.error);
|
2014-03-14 21:24:04 +09:00
|
|
|
}
|
2012-04-12 07:52:15 +09:00
|
|
|
}, this);
|
|
|
|
|
2014-01-04 09:17:05 +09:00
|
|
|
messageHandler.on('JpegDecode', function(data, deferred) {
|
2013-11-12 13:25:03 +09:00
|
|
|
var imageUrl = data[0];
|
2012-04-12 07:52:15 +09:00
|
|
|
var components = data[1];
|
2014-03-14 21:24:04 +09:00
|
|
|
if (components != 3 && components != 1) {
|
2012-04-12 07:52:15 +09:00
|
|
|
error('Only 3 component or 1 component can be returned');
|
2014-03-14 21:24:04 +09:00
|
|
|
}
|
2012-04-12 07:52:15 +09:00
|
|
|
|
|
|
|
var img = new Image();
|
|
|
|
img.onload = (function messageHandler_onloadClosure() {
|
|
|
|
var width = img.width;
|
|
|
|
var height = img.height;
|
|
|
|
var size = width * height;
|
|
|
|
var rgbaLength = size * 4;
|
|
|
|
var buf = new Uint8Array(size * components);
|
|
|
|
var tmpCanvas = createScratchCanvas(width, height);
|
|
|
|
var tmpCtx = tmpCanvas.getContext('2d');
|
|
|
|
tmpCtx.drawImage(img, 0, 0);
|
|
|
|
var data = tmpCtx.getImageData(0, 0, width, height).data;
|
2014-04-09 04:48:16 +09:00
|
|
|
var i, j;
|
2012-04-12 07:52:15 +09:00
|
|
|
|
|
|
|
if (components == 3) {
|
2014-04-09 04:48:16 +09:00
|
|
|
for (i = 0, j = 0; i < rgbaLength; i += 4, j += 3) {
|
2012-04-12 07:52:15 +09:00
|
|
|
buf[j] = data[i];
|
|
|
|
buf[j + 1] = data[i + 1];
|
|
|
|
buf[j + 2] = data[i + 2];
|
|
|
|
}
|
|
|
|
} else if (components == 1) {
|
2014-04-09 04:48:16 +09:00
|
|
|
for (i = 0, j = 0; i < rgbaLength; i += 4, j++) {
|
2012-04-12 07:52:15 +09:00
|
|
|
buf[j] = data[i];
|
|
|
|
}
|
|
|
|
}
|
2014-01-04 09:17:05 +09:00
|
|
|
deferred.resolve({ data: buf, width: width, height: height});
|
2012-04-12 07:52:15 +09:00
|
|
|
}).bind(this);
|
2013-11-12 13:25:03 +09:00
|
|
|
img.src = imageUrl;
|
2012-04-12 07:52:15 +09:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2012-06-24 04:48:33 +09:00
|
|
|
fetchDocument: function WorkerTransport_fetchDocument(source) {
|
2013-04-13 03:37:49 +09:00
|
|
|
source.disableAutoFetch = PDFJS.disableAutoFetch;
|
2013-02-07 08:19:29 +09:00
|
|
|
source.chunkedViewerLoading = !!this.pdfDataRangeTransport;
|
|
|
|
this.messageHandler.send('GetDocRequest', {
|
|
|
|
source: source,
|
2013-07-11 01:52:37 +09:00
|
|
|
disableRange: PDFJS.disableRange,
|
2013-08-20 08:33:20 +09:00
|
|
|
maxImageSize: PDFJS.maxImageSize,
|
2014-02-12 03:27:09 +09:00
|
|
|
cMapUrl: PDFJS.cMapUrl,
|
2014-03-15 03:22:02 +09:00
|
|
|
cMapPacked: PDFJS.cMapPacked,
|
2013-12-19 06:39:03 +09:00
|
|
|
disableFontFace: PDFJS.disableFontFace,
|
2014-01-11 07:30:41 +09:00
|
|
|
disableCreateObjectURL: PDFJS.disableCreateObjectURL,
|
2013-12-19 06:39:03 +09:00
|
|
|
verbosity: PDFJS.verbosity
|
2013-02-07 08:19:29 +09:00
|
|
|
});
|
2012-04-12 07:52:15 +09:00
|
|
|
},
|
|
|
|
|
2014-04-30 00:07:05 +09:00
|
|
|
getData: function WorkerTransport_getData(capability) {
|
2012-06-02 06:17:09 +09:00
|
|
|
this.messageHandler.send('GetData', null, function(data) {
|
2014-04-30 00:07:05 +09:00
|
|
|
capability.resolve(data);
|
2012-06-02 06:17:09 +09:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-04-30 00:07:05 +09:00
|
|
|
getPage: function WorkerTransport_getPage(pageNumber, capability) {
|
2014-02-24 03:16:14 +09:00
|
|
|
if (pageNumber <= 0 || pageNumber > this.numPages ||
|
|
|
|
(pageNumber|0) !== pageNumber) {
|
2014-04-30 00:07:05 +09:00
|
|
|
return new Promise.reject(new Error('Invalid page request'));
|
2014-02-24 03:16:14 +09:00
|
|
|
}
|
|
|
|
|
2012-04-13 02:01:07 +09:00
|
|
|
var pageIndex = pageNumber - 1;
|
2014-04-30 00:07:05 +09:00
|
|
|
if (pageIndex in this.pageCapabilities) {
|
|
|
|
return this.pageCapabilities[pageIndex].promise;
|
2014-03-14 21:24:04 +09:00
|
|
|
}
|
2014-04-30 00:07:05 +09:00
|
|
|
capability = createPromiseCapability();
|
|
|
|
this.pageCapabilities[pageIndex] = capability;
|
2012-04-13 04:11:22 +09:00
|
|
|
this.messageHandler.send('GetPageRequest', { pageIndex: pageIndex });
|
2014-04-30 00:07:05 +09:00
|
|
|
return capability.promise;
|
2012-04-15 05:54:31 +09:00
|
|
|
},
|
|
|
|
|
2013-11-14 08:27:46 +09:00
|
|
|
getPageIndex: function WorkerTransport_getPageIndexByRef(ref) {
|
2014-04-30 00:07:05 +09:00
|
|
|
return new Promise(function (resolve) {
|
|
|
|
this.messageHandler.send('GetPageIndex', { ref: ref },
|
|
|
|
function (pageIndex) {
|
|
|
|
resolve(pageIndex);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}.bind(this));
|
2013-11-14 08:27:46 +09:00
|
|
|
},
|
|
|
|
|
2012-04-15 05:54:31 +09:00
|
|
|
getAnnotations: function WorkerTransport_getAnnotations(pageIndex) {
|
|
|
|
this.messageHandler.send('GetAnnotationsRequest',
|
|
|
|
{ pageIndex: pageIndex });
|
2013-02-07 08:19:29 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
getDestinations: function WorkerTransport_getDestinations() {
|
2014-04-30 00:07:05 +09:00
|
|
|
return new Promise(function (resolve) {
|
|
|
|
this.messageHandler.send('GetDestinations', null,
|
|
|
|
function transportDestinations(destinations) {
|
|
|
|
resolve(destinations);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}.bind(this));
|
2013-11-15 06:43:38 +09:00
|
|
|
},
|
|
|
|
|
2014-03-19 05:32:47 +09:00
|
|
|
getAttachments: function WorkerTransport_getAttachments() {
|
2014-04-30 00:07:05 +09:00
|
|
|
return new Promise(function (resolve) {
|
|
|
|
this.messageHandler.send('GetAttachments', null,
|
|
|
|
function transportAttachments(attachments) {
|
|
|
|
resolve(attachments);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}.bind(this));
|
2014-03-19 05:32:47 +09:00
|
|
|
},
|
|
|
|
|
2013-11-15 06:43:38 +09:00
|
|
|
startCleanup: function WorkerTransport_startCleanup() {
|
|
|
|
this.messageHandler.send('Cleanup', null,
|
|
|
|
function endCleanup() {
|
|
|
|
for (var i = 0, ii = this.pageCache.length; i < ii; i++) {
|
|
|
|
var page = this.pageCache[i];
|
|
|
|
if (page) {
|
|
|
|
page.destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.commonObjs.clear();
|
|
|
|
FontLoader.clear();
|
|
|
|
}.bind(this)
|
|
|
|
);
|
2012-04-12 07:52:15 +09:00
|
|
|
}
|
|
|
|
};
|
2012-04-13 04:11:22 +09:00
|
|
|
return WorkerTransport;
|
2012-04-12 07:52:15 +09:00
|
|
|
|
2012-04-10 14:20:57 +09:00
|
|
|
})();
|
2013-08-01 03:17:36 +09:00
|
|
|
|
|
|
|
/**
|
2013-08-13 02:48:06 +09:00
|
|
|
* A PDF document and page is built of many objects. E.g. there are objects
|
|
|
|
* for fonts, images, rendering code and such. These objects might get processed
|
|
|
|
* inside of a worker. The `PDFObjects` implements some basic functions to
|
|
|
|
* manage these objects.
|
2014-01-22 04:28:18 +09:00
|
|
|
* @ignore
|
2013-08-13 02:48:06 +09:00
|
|
|
*/
|
|
|
|
var PDFObjects = (function PDFObjectsClosure() {
|
|
|
|
function PDFObjects() {
|
|
|
|
this.objs = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
PDFObjects.prototype = {
|
|
|
|
/**
|
|
|
|
* Internal function.
|
|
|
|
* Ensures there is an object defined for `objId`.
|
|
|
|
*/
|
|
|
|
ensureObj: function PDFObjects_ensureObj(objId) {
|
2014-03-14 21:24:04 +09:00
|
|
|
if (this.objs[objId]) {
|
2013-08-13 02:48:06 +09:00
|
|
|
return this.objs[objId];
|
2014-03-14 21:24:04 +09:00
|
|
|
}
|
2013-08-13 02:48:06 +09:00
|
|
|
|
|
|
|
var obj = {
|
2014-04-30 00:07:05 +09:00
|
|
|
capability: createPromiseCapability(),
|
2013-08-13 02:48:06 +09:00
|
|
|
data: null,
|
|
|
|
resolved: false
|
|
|
|
};
|
|
|
|
this.objs[objId] = obj;
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If called *without* callback, this returns the data of `objId` but the
|
|
|
|
* object needs to be resolved. If it isn't, this function throws.
|
|
|
|
*
|
|
|
|
* If called *with* a callback, the callback is called with the data of the
|
|
|
|
* object once the object is resolved. That means, if you call this
|
|
|
|
* function and the object is already resolved, the callback gets called
|
|
|
|
* right away.
|
|
|
|
*/
|
|
|
|
get: function PDFObjects_get(objId, callback) {
|
|
|
|
// If there is a callback, then the get can be async and the object is
|
|
|
|
// not required to be resolved right now
|
|
|
|
if (callback) {
|
2014-04-30 00:07:05 +09:00
|
|
|
this.ensureObj(objId).capability.promise.then(callback);
|
2013-08-13 02:48:06 +09:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there isn't a callback, the user expects to get the resolved data
|
|
|
|
// directly.
|
|
|
|
var obj = this.objs[objId];
|
|
|
|
|
|
|
|
// If there isn't an object yet or the object isn't resolved, then the
|
|
|
|
// data isn't ready yet!
|
2014-03-14 21:24:04 +09:00
|
|
|
if (!obj || !obj.resolved) {
|
2013-08-13 02:48:06 +09:00
|
|
|
error('Requesting object that isn\'t resolved yet ' + objId);
|
2014-03-14 21:24:04 +09:00
|
|
|
}
|
2013-08-13 02:48:06 +09:00
|
|
|
|
|
|
|
return obj.data;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolves the object `objId` with optional `data`.
|
|
|
|
*/
|
|
|
|
resolve: function PDFObjects_resolve(objId, data) {
|
|
|
|
var obj = this.ensureObj(objId);
|
|
|
|
|
|
|
|
obj.resolved = true;
|
|
|
|
obj.data = data;
|
2014-04-30 00:07:05 +09:00
|
|
|
obj.capability.resolve(data);
|
2013-08-13 02:48:06 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
isResolved: function PDFObjects_isResolved(objId) {
|
|
|
|
var objs = this.objs;
|
|
|
|
|
|
|
|
if (!objs[objId]) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return objs[objId].resolved;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
hasData: function PDFObjects_hasData(objId) {
|
|
|
|
return this.isResolved(objId);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the data of `objId` if object exists, null otherwise.
|
|
|
|
*/
|
|
|
|
getData: function PDFObjects_getData(objId) {
|
|
|
|
var objs = this.objs;
|
|
|
|
if (!objs[objId] || !objs[objId].resolved) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return objs[objId].data;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
clear: function PDFObjects_clear() {
|
|
|
|
this.objs = {};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return PDFObjects;
|
|
|
|
})();
|
2014-01-04 09:17:05 +09:00
|
|
|
|
2014-01-22 04:28:18 +09:00
|
|
|
/**
|
|
|
|
* Allows controlling of the rendering tasks.
|
|
|
|
* @class
|
|
|
|
*/
|
2013-08-01 03:17:36 +09:00
|
|
|
var RenderTask = (function RenderTaskClosure() {
|
|
|
|
function RenderTask(internalRenderTask) {
|
|
|
|
this.internalRenderTask = internalRenderTask;
|
2014-01-22 04:28:18 +09:00
|
|
|
/**
|
|
|
|
* Promise for rendering task completion.
|
|
|
|
* @type {Promise}
|
|
|
|
*/
|
2014-04-30 00:07:05 +09:00
|
|
|
this.promise = this.internalRenderTask.capability.promise;
|
2013-08-01 03:17:36 +09:00
|
|
|
}
|
|
|
|
|
2014-01-22 04:28:18 +09:00
|
|
|
RenderTask.prototype = /** @lends RenderTask.prototype */ {
|
|
|
|
/**
|
|
|
|
* Cancels the rendering task. If the task is currently rendering it will
|
|
|
|
* not be cancelled until graphics pauses with a timeout. The promise that
|
|
|
|
* this object extends will resolved when cancelled.
|
|
|
|
*/
|
|
|
|
cancel: function RenderTask_cancel() {
|
|
|
|
this.internalRenderTask.cancel();
|
2014-04-12 02:10:42 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers callback to indicate the rendering task completion.
|
|
|
|
*
|
|
|
|
* @param {function} onFulfilled The callback for the rendering completion.
|
|
|
|
* @param {function} onRejected The callback for the rendering failure.
|
|
|
|
* @return {Promise} A promise that is resolved after the onFulfilled or
|
|
|
|
* onRejected callback.
|
|
|
|
*/
|
|
|
|
then: function RenderTask_then(onFulfilled, onRejected) {
|
|
|
|
return this.promise.then(onFulfilled, onRejected);
|
2014-01-22 04:28:18 +09:00
|
|
|
}
|
2013-08-01 03:17:36 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
return RenderTask;
|
|
|
|
})();
|
|
|
|
|
2014-01-22 04:28:18 +09:00
|
|
|
/**
|
|
|
|
* For internal use only.
|
|
|
|
* @ignore
|
|
|
|
*/
|
2013-08-01 03:17:36 +09:00
|
|
|
var InternalRenderTask = (function InternalRenderTaskClosure() {
|
|
|
|
|
|
|
|
function InternalRenderTask(callback, params, objs, commonObjs, operatorList,
|
|
|
|
pageNumber) {
|
|
|
|
this.callback = callback;
|
|
|
|
this.params = params;
|
|
|
|
this.objs = objs;
|
|
|
|
this.commonObjs = commonObjs;
|
|
|
|
this.operatorListIdx = null;
|
|
|
|
this.operatorList = operatorList;
|
|
|
|
this.pageNumber = pageNumber;
|
|
|
|
this.running = false;
|
|
|
|
this.graphicsReadyCallback = null;
|
|
|
|
this.graphicsReady = false;
|
|
|
|
this.cancelled = false;
|
2014-04-30 00:07:05 +09:00
|
|
|
this.capability = createPromiseCapability();
|
2013-08-01 03:17:36 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
InternalRenderTask.prototype = {
|
|
|
|
|
|
|
|
initalizeGraphics:
|
|
|
|
function InternalRenderTask_initalizeGraphics(transparency) {
|
|
|
|
|
|
|
|
if (this.cancelled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (PDFJS.pdfBug && 'StepperManager' in globalScope &&
|
|
|
|
globalScope.StepperManager.enabled) {
|
|
|
|
this.stepper = globalScope.StepperManager.create(this.pageNumber - 1);
|
|
|
|
this.stepper.init(this.operatorList);
|
|
|
|
this.stepper.nextBreakPoint = this.stepper.getNextBreakPoint();
|
|
|
|
}
|
|
|
|
|
|
|
|
var params = this.params;
|
|
|
|
this.gfx = new CanvasGraphics(params.canvasContext, this.commonObjs,
|
2014-04-10 08:44:07 +09:00
|
|
|
this.objs, params.imageLayer);
|
2013-08-01 03:17:36 +09:00
|
|
|
|
|
|
|
this.gfx.beginDrawing(params.viewport, transparency);
|
|
|
|
this.operatorListIdx = 0;
|
|
|
|
this.graphicsReady = true;
|
|
|
|
if (this.graphicsReadyCallback) {
|
|
|
|
this.graphicsReadyCallback();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
cancel: function InternalRenderTask_cancel() {
|
|
|
|
this.running = false;
|
|
|
|
this.cancelled = true;
|
2013-08-06 08:34:57 +09:00
|
|
|
this.callback('cancelled');
|
2013-08-01 03:17:36 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
operatorListChanged: function InternalRenderTask_operatorListChanged() {
|
|
|
|
if (!this.graphicsReady) {
|
|
|
|
if (!this.graphicsReadyCallback) {
|
|
|
|
this.graphicsReadyCallback = this._continue.bind(this);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.stepper) {
|
|
|
|
this.stepper.updateOperatorList(this.operatorList);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.running) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._continue();
|
|
|
|
},
|
|
|
|
|
|
|
|
_continue: function InternalRenderTask__continue() {
|
|
|
|
this.running = true;
|
|
|
|
if (this.cancelled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.params.continueCallback) {
|
|
|
|
this.params.continueCallback(this._next.bind(this));
|
|
|
|
} else {
|
|
|
|
this._next();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_next: function InternalRenderTask__next() {
|
|
|
|
if (this.cancelled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.operatorListIdx = this.gfx.executeOperatorList(this.operatorList,
|
|
|
|
this.operatorListIdx,
|
|
|
|
this._continue.bind(this),
|
|
|
|
this.stepper);
|
2013-11-14 04:43:38 +09:00
|
|
|
if (this.operatorListIdx === this.operatorList.argsArray.length) {
|
2013-08-01 03:17:36 +09:00
|
|
|
this.running = false;
|
|
|
|
if (this.operatorList.lastChunk) {
|
|
|
|
this.gfx.endDrawing();
|
|
|
|
this.callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
return InternalRenderTask;
|
|
|
|
})();
|