From 3c2fbdffe6e1d68d751cc0e0c125712094c8f5f8 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 17 Feb 2018 16:57:24 +0100 Subject: [PATCH] Move the `cMapUrl` and `cMapPacked` options from the global `PDFJS` object and into `getDocument` instead --- examples/components/pageviewer.js | 12 ++++++++---- examples/components/simpleviewer.js | 10 +++++++--- examples/components/singlepageviewer.js | 10 +++++++--- examples/mobile-viewer/viewer.js | 6 ++++-- examples/svgviewer/viewer.js | 18 ++++++++++-------- src/display/api.js | 15 ++++++++++----- src/display/dom_utils.js | 9 +++------ src/display/global.js | 13 ------------- test/driver.js | 6 ++++-- test/unit/cmap_spec.js | 5 +++-- test/unit/test_utils.js | 5 +++-- web/app.js | 15 ++------------- web/app_options.js | 11 +++++++++++ 13 files changed, 72 insertions(+), 63 deletions(-) diff --git a/examples/components/pageviewer.js b/examples/components/pageviewer.js index 0d128ebc9..de33355e0 100644 --- a/examples/components/pageviewer.js +++ b/examples/components/pageviewer.js @@ -15,7 +15,7 @@ 'use strict'; -if (!PDFJS.PDFViewer || !PDFJS.getDocument) { +if (!PDFJS.PDFPageView || !PDFJS.getDocument) { alert('Please build the pdfjs-dist library using\n' + ' `gulp dist-install`'); } @@ -27,8 +27,8 @@ PDFJS.GlobalWorkerOptions.workerSrc = // Some PDFs need external cmaps. // -// PDFJS.cMapUrl = '../../node_modules/pdfjs-dist/cmaps/'; -// PDFJS.cMapPacked = true; +var CMAP_URL = '../../node_modules/pdfjs-dist/cmaps/'; +var CMAP_PACKED = true; var DEFAULT_URL = '../../web/compressed.tracemonkey-pldi-09.pdf'; var PAGE_TO_VIEW = 1; @@ -37,7 +37,11 @@ var SCALE = 1.0; var container = document.getElementById('pageContainer'); // Loading document. -PDFJS.getDocument(DEFAULT_URL).then(function (pdfDocument) { +PDFJS.getDocument({ + url: DEFAULT_URL, + cMapUrl: CMAP_URL, + cMapPacked: CMAP_PACKED, +}).then(function(pdfDocument) { // Document loaded, retrieving the page. return pdfDocument.getPage(PAGE_TO_VIEW).then(function (pdfPage) { // Creating the page view with default parameters. diff --git a/examples/components/simpleviewer.js b/examples/components/simpleviewer.js index 5f9da0fcc..8d2e09fd2 100644 --- a/examples/components/simpleviewer.js +++ b/examples/components/simpleviewer.js @@ -27,8 +27,8 @@ PDFJS.GlobalWorkerOptions.workerSrc = // Some PDFs need external cmaps. // -// PDFJS.cMapUrl = '../../node_modules/pdfjs-dist/cmaps/'; -// PDFJS.cMapPacked = true; +var CMAP_URL = '../../node_modules/pdfjs-dist/cmaps/'; +var CMAP_PACKED = true; var DEFAULT_URL = '../../web/compressed.tracemonkey-pldi-09.pdf'; var SEARCH_FOR = ''; // try 'Mozilla'; @@ -60,7 +60,11 @@ container.addEventListener('pagesinit', function () { }); // Loading document. -PDFJS.getDocument(DEFAULT_URL).then(function (pdfDocument) { +PDFJS.getDocument({ + url: DEFAULT_URL, + cMapUrl: CMAP_URL, + cMapPacked: CMAP_PACKED, +}).then(function(pdfDocument) { // Document loaded, specifying document for the viewer and // the (optional) linkService. pdfViewer.setDocument(pdfDocument); diff --git a/examples/components/singlepageviewer.js b/examples/components/singlepageviewer.js index 90900741e..b2b9d34be 100644 --- a/examples/components/singlepageviewer.js +++ b/examples/components/singlepageviewer.js @@ -27,8 +27,8 @@ PDFJS.GlobalWorkerOptions.workerSrc = // Some PDFs need external cmaps. // -// PDFJS.cMapUrl = '../../node_modules/pdfjs-dist/cmaps/'; -// PDFJS.cMapPacked = true; +var CMAP_URL = '../../node_modules/pdfjs-dist/cmaps/'; +var CMAP_PACKED = true; var DEFAULT_URL = '../../web/compressed.tracemonkey-pldi-09.pdf'; var SEARCH_FOR = ''; // try 'Mozilla'; @@ -60,7 +60,11 @@ container.addEventListener('pagesinit', function () { }); // Loading document. -PDFJS.getDocument(DEFAULT_URL).then(function (pdfDocument) { +PDFJS.getDocument({ + url: DEFAULT_URL, + cMapUrl: CMAP_URL, + cMapPacked: CMAP_PACKED, +}).then(function(pdfDocument) { // Document loaded, specifying document for the viewer and // the (optional) linkService. pdfSinglePageViewer.setDocument(pdfDocument); diff --git a/examples/mobile-viewer/viewer.js b/examples/mobile-viewer/viewer.js index 2b8f3854a..52e152986 100644 --- a/examples/mobile-viewer/viewer.js +++ b/examples/mobile-viewer/viewer.js @@ -24,8 +24,8 @@ if (typeof PDFJS === 'undefined' || !PDFJS.PDFViewer || !PDFJS.getDocument) { var USE_ONLY_CSS_ZOOM = true; var TEXT_LAYER_MODE = 0; // DISABLE var MAX_IMAGE_SIZE = 1024 * 1024; -PDFJS.cMapUrl = '../../node_modules/pdfjs-dist/cmaps/'; -PDFJS.cMapPacked = true; +var CMAP_URL = '../../node_modules/pdfjs-dist/cmaps/'; +var CMAP_PACKED = true; PDFJS.GlobalWorkerOptions.workerSrc = '../../node_modules/pdfjs-dist/build/pdf.worker.js'; @@ -65,6 +65,8 @@ var PDFViewerApplication = { var loadingTask = PDFJS.getDocument({ url: url, maxImageSize: MAX_IMAGE_SIZE, + cMapUrl: CMAP_URL, + cMapPacked: CMAP_PACKED, }); this.pdfLoadingTask = loadingTask; diff --git a/examples/svgviewer/viewer.js b/examples/svgviewer/viewer.js index 04f92f8a2..f29ab7907 100644 --- a/examples/svgviewer/viewer.js +++ b/examples/svgviewer/viewer.js @@ -38,29 +38,31 @@ function renderDocument(pdf, svgLib) { Promise.all([System.import('pdfjs/display/api'), System.import('pdfjs/display/svg'), - System.import('pdfjs/display/global'), System.import('pdfjs/display/worker_options'), System.import('pdfjs/display/network'), System.resolve('pdfjs/worker_loader')]) .then(function (modules) { var api = modules[0]; var svg = modules[1]; - var global = modules[2]; - var GlobalWorkerOptions = modules[3].GlobalWorkerOptions; - var network = modules[4]; + var GlobalWorkerOptions = modules[2].GlobalWorkerOptions; + var network = modules[3]; api.setPDFNetworkStreamFactory((params) => { return new network.PDFNetworkStream(params); }); // In production, change this to point to the built `pdf.worker.js` file. - GlobalWorkerOptions.workerSrc = modules[5]; + GlobalWorkerOptions.workerSrc = modules[4]; // In production, change this to point to where the cMaps are placed. - global.PDFJS.cMapUrl = '../../external/bcmaps/'; - global.PDFJS.cMapPacked = true; + var CMAP_URL = '../../external/bcmaps/'; + var CMAP_PACKED = true; // Fetch the PDF document from the URL using promises. - api.getDocument(url).then(function (doc) { + api.getDocument({ + url: url, + cMapUrl: CMAP_URL, + cMapPacked: CMAP_PACKED, + }).then(function(doc) { renderDocument(doc, svg); }); }); diff --git a/src/display/api.js b/src/display/api.js index 545c6e1ee..437312678 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -142,6 +142,10 @@ function setPDFNetworkStreamFactory(pdfNetworkStreamFactory) { * with limited image support through stubs (useful for SVG conversion), * and 'none' where JPEG images will be decoded entirely by PDF.js. * The default value is 'decode'. + * @property {string} cMapUrl - (optional) The URL where the predefined + * Adobe CMaps are located. Include trailing slash. + * @property {boolean} cMapPacked - (optional) Specifies if the Adobe CMaps are + * binary packed. * @property {Object} CMapReaderFactory - (optional) The factory that will be * used when reading built-in CMap files. Providing a custom factory is useful * for environments without `XMLHttpRequest` support, such as e.g. Node.js. @@ -201,7 +205,7 @@ function getDocument(src) { let params = Object.create(null); var rangeTransport = null; let worker = null; - var CMapReaderFactory = DOMCMapReaderFactory; + let CMapReaderFactory = DOMCMapReaderFactory; for (var key in source) { if (key === 'url' && typeof window !== 'undefined') { @@ -289,7 +293,7 @@ function getDocument(src) { var messageHandler = new MessageHandler(docId, workerId, worker.port); messageHandler.postMessageTransfers = worker.postMessageTransfers; var transport = new WorkerTransport(messageHandler, task, networkStream, - CMapReaderFactory); + params, CMapReaderFactory); task._transport = transport; messageHandler.send('Ready', null); }); @@ -1550,14 +1554,15 @@ var PDFWorker = (function PDFWorkerClosure() { */ var WorkerTransport = (function WorkerTransportClosure() { function WorkerTransport(messageHandler, loadingTask, networkStream, - CMapReaderFactory) { + params, CMapReaderFactory) { this.messageHandler = messageHandler; this.loadingTask = loadingTask; this.commonObjs = new PDFObjects(); this.fontLoader = new FontLoader(loadingTask.docId); + this._params = params; this.CMapReaderFactory = new CMapReaderFactory({ - baseUrl: getDefaultSetting('cMapUrl'), - isCompressed: getDefaultSetting('cMapPacked'), + baseUrl: params.cMapUrl, + isCompressed: params.cMapPacked, }); this.destroyed = false; diff --git a/src/display/dom_utils.js b/src/display/dom_utils.js index 776115cbd..f1146f74b 100644 --- a/src/display/dom_utils.js +++ b/src/display/dom_utils.js @@ -69,8 +69,9 @@ class DOMCMapReaderFactory { fetch({ name, }) { if (!this.baseUrl) { - return Promise.reject(new Error('CMap baseUrl must be specified, ' + - 'see "PDFJS.cMapUrl" (and also "PDFJS.cMapPacked").')); + return Promise.reject(new Error( + 'The CMap "baseUrl" parameter must be specified, ensure that ' + + 'the "cMapUrl" and "cMapPacked" API parameters are provided.')); } if (!name) { return Promise.reject(new Error('CMap name must be specified.')); @@ -345,10 +346,6 @@ function getDefaultSetting(id) { return globalSettings ? globalSettings.disableFontFace : false; case 'disableCreateObjectURL': return globalSettings ? globalSettings.disableCreateObjectURL : false; - case 'cMapUrl': - return globalSettings ? globalSettings.cMapUrl : null; - case 'cMapPacked': - return globalSettings ? globalSettings.cMapPacked : false; case 'isEvalSupported': return globalSettings ? globalSettings.isEvalSupported : true; default: diff --git a/src/display/global.js b/src/display/global.js index 24d36ccdc..6fabc84df 100644 --- a/src/display/global.js +++ b/src/display/global.js @@ -65,19 +65,6 @@ PDFJS.Util = Util; PDFJS.PageViewport = PageViewport; PDFJS.createPromiseCapability = createPromiseCapability; -/** - * The url of where the predefined Adobe CMaps are located. Include trailing - * slash. - * @var {string} - */ -PDFJS.cMapUrl = (PDFJS.cMapUrl === undefined ? null : PDFJS.cMapUrl); - -/** - * Specifies if CMaps are binary packed. - * @var {boolean} - */ -PDFJS.cMapPacked = PDFJS.cMapPacked === undefined ? false : PDFJS.cMapPacked; - /** * 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 diff --git a/test/driver.js b/test/driver.js index 91cd268a6..3a28cc55d 100644 --- a/test/driver.js +++ b/test/driver.js @@ -18,6 +18,8 @@ const WAITING_TIME = 100; // ms const PDF_TO_CSS_UNITS = 96.0 / 72.0; +const CMAP_URL = '../external/bcmaps/'; +const CMAP_PACKED = true; const IMAGE_RESOURCES_PATH = '/web/images/'; const WORKER_SRC = '../build/generic/build/pdf.worker.js'; @@ -271,8 +273,6 @@ var Driver = (function DriverClosure() { // eslint-disable-line no-unused-vars // Configure the global worker options. PDFJS.GlobalWorkerOptions.workerSrc = WORKER_SRC; - PDFJS.cMapPacked = true; - PDFJS.cMapUrl = '../external/bcmaps/'; PDFJS.pdfBug = true; // Set the passed options @@ -366,6 +366,8 @@ var Driver = (function DriverClosure() { // eslint-disable-line no-unused-vars url: absoluteUrl, password: task.password, nativeImageDecoderSupport: task.nativeImageDecoderSupport, + cMapUrl: CMAP_URL, + cMapPacked: CMAP_PACKED, }).then((doc) => { task.pdfDoc = doc; this._nextPage(task, failure); diff --git a/test/unit/cmap_spec.js b/test/unit/cmap_spec.js index 8c4c3f5b0..981f695a1 100644 --- a/test/unit/cmap_spec.js +++ b/test/unit/cmap_spec.js @@ -281,8 +281,9 @@ describe('cmap', function() { done.fail('No CMap should be loaded'); }, function (reason) { expect(reason instanceof Error).toEqual(true); - expect(reason.message).toEqual('CMap baseUrl must be specified, ' + - 'see "PDFJS.cMapUrl" (and also "PDFJS.cMapPacked").'); + expect(reason.message).toEqual( + 'The CMap "baseUrl" parameter must be specified, ensure that ' + + 'the "cMapUrl" and "cMapPacked" API parameters are provided.'); done(); }); }); diff --git a/test/unit/test_utils.js b/test/unit/test_utils.js index e57247c3c..c1e0337d4 100644 --- a/test/unit/test_utils.js +++ b/test/unit/test_utils.js @@ -51,8 +51,9 @@ class NodeCMapReaderFactory { fetch({ name, }) { if (!this.baseUrl) { - return Promise.reject(new Error('CMap baseUrl must be specified, ' + - 'see "PDFJS.cMapUrl" (and also "PDFJS.cMapPacked").')); + return Promise.reject(new Error( + 'The CMap "baseUrl" parameter must be specified, ensure that ' + + 'the "cMapUrl" and "cMapPacked" API parameters are provided.')); } if (!name) { return Promise.reject(new Error('CMap name must be specified.')); diff --git a/web/app.js b/web/app.js index 05a3f5736..cfcbdbaa7 100644 --- a/web/app.js +++ b/web/app.js @@ -50,15 +50,6 @@ import { ViewHistory } from './view_history'; const DEFAULT_SCALE_DELTA = 1.1; const DISABLE_AUTO_FETCH_LOADING_BAR_TIMEOUT = 5000; -function configure(PDFJS) { - if (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('PRODUCTION')) { - PDFJS.cMapUrl = '../external/bcmaps/'; - } else { - PDFJS.cMapUrl = '../web/cmaps/'; - } - PDFJS.cMapPacked = true; -} - const DefaultExternalServices = { updateFindControlState(data) {}, initPassiveLoading(callbacks) {}, @@ -142,8 +133,6 @@ let PDFViewerApplication = { // Called once when the document is loaded. initialize(appConfig) { this.preferences = this.externalServices.createPreferences(); - - configure(PDFJS); this.appConfig = appConfig; return this._readPreferences().then(() => { @@ -298,8 +287,8 @@ let PDFViewerApplication = { } if ((typeof PDFJSDev === 'undefined' || !PDFJSDev.test('PRODUCTION')) && hashParams['disablebcmaps'] === 'true') { - PDFJS.cMapUrl = '../external/cmaps/'; - PDFJS.cMapPacked = false; + AppOptions.set('cMapUrl', '../external/cmaps/'); + AppOptions.set('cMapPacked', false); } if ('textlayer' in hashParams) { switch (hashParams['textlayer']) { diff --git a/web/app_options.js b/web/app_options.js index 638410303..f0c532bbb 100644 --- a/web/app_options.js +++ b/web/app_options.js @@ -128,6 +128,17 @@ const defaultOptions = { kind: OptionKind.VIEWER, }, + cMapPacked: { + /** @type {boolean} */ + value: true, + kind: OptionKind.API, + }, + cMapUrl: { + /** @type {string} */ + value: (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('PRODUCTION') ? + '../external/bcmaps/' : '../web/cmaps/'), + kind: OptionKind.API, + }, maxImageSize: { /** @type {number} */ value: -1,