From c9cb6a30258b786258cf636c772c4980185159a1 Mon Sep 17 00:00:00 2001 From: Yury Delendik Date: Mon, 30 Nov 2015 14:42:47 -0600 Subject: [PATCH] Replaces UnsupportedManager with callback. --- src/core/evaluator.js | 18 ++++++++++++------ src/core/pattern.js | 8 ++++---- src/core/worker.js | 16 ++++++---------- src/display/api.js | 44 +++++++++++++++++++++++++++++++++++++++---- src/display/canvas.js | 3 +-- src/shared/util.js | 17 ----------------- web/viewer.js | 7 +++---- 7 files changed, 66 insertions(+), 47 deletions(-) diff --git a/src/core/evaluator.js b/src/core/evaluator.js index 20b0daa8b..6d61fea1d 100644 --- a/src/core/evaluator.js +++ b/src/core/evaluator.js @@ -19,9 +19,8 @@ MurmurHash3_64, Name, Parser, Pattern, PDFImage, PDFJS, serifFonts, stdFontMap, symbolsFonts, getTilingPatternIR, warn, Util, Promise, RefSetCache, isRef, TextRenderingMode, IdentityToUnicodeMap, - OPS, UNSUPPORTED_FEATURES, UnsupportedManager, NormalizedUnicodes, - IDENTITY_MATRIX, reverseIfRtl, createPromiseCapability, ToUnicodeMap, - getFontType */ + OPS, UNSUPPORTED_FEATURES, NormalizedUnicodes, IDENTITY_MATRIX, + reverseIfRtl, createPromiseCapability, ToUnicodeMap, getFontType */ 'use strict'; @@ -322,6 +321,9 @@ var PartialEvaluator = (function PartialEvaluatorClosure() { then(function () { return translated; }, function (reason) { + // Error in the font data -- sending unsupported feature notification. + self.handler.send('UnsupportedFeature', + {featureId: UNSUPPORTED_FEATURES.font}); return new TranslatedFont('g_font_error', new ErrorFont('Type3 font load error: ' + reason), translated.font); }); @@ -546,6 +548,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() { translatedPromise = Promise.reject(e); } + var self = this; translatedPromise.then(function (translatedFont) { if (translatedFont.fontType !== undefined) { var xrefFontStats = xref.stats.fontTypes; @@ -556,7 +559,9 @@ var PartialEvaluator = (function PartialEvaluatorClosure() { translatedFont, font)); }, function (reason) { // TODO fontCapability.reject? - UnsupportedManager.notify(UNSUPPORTED_FEATURES.font); + // Error in the font data -- sending unsupported feature notification. + self.handler.send('UnsupportedFeature', + {featureId: UNSUPPORTED_FEATURES.font}); try { // error, but it's still nice to have font type reported @@ -609,7 +614,8 @@ var PartialEvaluator = (function PartialEvaluatorClosure() { } else if (typeNum === SHADING_PATTERN) { var shading = dict.get('Shading'); var matrix = dict.get('Matrix'); - pattern = Pattern.parseShading(shading, matrix, xref, resources); + pattern = Pattern.parseShading(shading, matrix, xref, resources, + this.handler); operatorList.addOp(fn, pattern.getIR()); return Promise.resolve(); } else { @@ -846,7 +852,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() { } var shadingFill = Pattern.parseShading(shading, null, xref, - resources); + resources, self.handler); var patternIR = shadingFill.getIR(); args = [patternIR]; fn = OPS.shadingFill; diff --git a/src/core/pattern.js b/src/core/pattern.js index 29aaafeea..a83770ba8 100644 --- a/src/core/pattern.js +++ b/src/core/pattern.js @@ -13,8 +13,7 @@ * limitations under the License. */ /* globals ColorSpace, PDFFunction, Util, error, warn, info, isArray, isStream, - assert, isPDFFunction, UnsupportedManager, UNSUPPORTED_FEATURES, - MissingDataException */ + assert, isPDFFunction, UNSUPPORTED_FEATURES, MissingDataException */ 'use strict'; @@ -43,7 +42,7 @@ var Pattern = (function PatternClosure() { }; Pattern.parseShading = function Pattern_parseShading(shading, matrix, xref, - res) { + res, handler) { var dict = isStream(shading) ? shading.dict : shading; var type = dict.get('ShadingType'); @@ -66,7 +65,8 @@ var Pattern = (function PatternClosure() { if (ex instanceof MissingDataException) { throw ex; } - UnsupportedManager.notify(UNSUPPORTED_FEATURES.shadingPattern); + handler.send('UnsupportedFeature', + {featureId: UNSUPPORTED_FEATURES.shadingPattern}); warn(ex); return new Shadings.Dummy(); } diff --git a/src/core/worker.js b/src/core/worker.js index 97842d035..ae9013806 100644 --- a/src/core/worker.js +++ b/src/core/worker.js @@ -13,7 +13,7 @@ * limitations under the License. */ /* globals PDFJS, createPromiseCapability, LocalPdfManager, NetworkPdfManager, - NetworkManager, isInt, MissingPDFException, + NetworkManager, isInt, MissingPDFException, UNSUPPORTED_FEATURES, UnexpectedResponseException, PasswordException, Promise, warn, PasswordResponses, InvalidPDFException, UnknownErrorException, XRefParseException, Ref, info, globalScope, error, MessageHandler */ @@ -482,6 +482,11 @@ var WorkerMessageHandler = PDFJS.WorkerMessageHandler = { return; // ignoring errors from the terminated thread } + // For compatibility with older behavior, generating unknown + // unsupported feature notification on errors. + handler.send('UnsupportedFeature', + {featureId: UNSUPPORTED_FEATURES.unknown}); + var minimumStackMessage = 'worker.js: while trying to getPage() and getOperatorList()'; @@ -616,15 +621,6 @@ if (typeof window === 'undefined') { globalScope.console = workerConsole; } - // Listen for unsupported features so we can pass them on to the main thread. - PDFJS.UnsupportedManager.listen(function (msg) { - globalScope.postMessage({ - targetName: 'main', - action: '_unsupported_feature', - data: msg - }); - }); - var handler = new MessageHandler('worker', 'main', this); WorkerMessageHandler.setup(handler, this); } diff --git a/src/display/api.js b/src/display/api.js index 654de95b9..646f09e9e 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -17,7 +17,7 @@ Promise, PasswordResponses, PasswordException, InvalidPDFException, MissingPDFException, UnknownErrorException, FontFaceObject, loadJpegStream, createScratchCanvas, CanvasGraphics, stringToBytes, - UnexpectedResponseException, deprecated, UnsupportedManager */ + UnexpectedResponseException, deprecated */ 'use strict'; @@ -447,6 +447,12 @@ var PDFDocumentLoadingTask = (function PDFDocumentLoadingTaskClosure() { * an {Object} with the properties: {number} loaded and {number} total. */ this.onProgress = null; + + /** + * Callback to when unsupported feature is used. The callback receives + * an {PDFJS.UNSUPPORTED_FEATURES} argument. + */ + this.onUnsupportedFeature = null; } PDFDocumentLoadingTask.prototype = @@ -1214,9 +1220,6 @@ var PDFWorker = (function PDFWorkerClosure() { messageHandler.on('console_error', function (data) { console.error.apply(console, data); }); - messageHandler.on('_unsupported_feature', function (data) { - UnsupportedManager.notify(data); - }); var testObj = new Uint8Array([PDFJS.postMessageTransfers ? 255 : 0]); // Some versions of Opera throw a DATA_CLONE_ERR on serializing the @@ -1584,6 +1587,19 @@ var WorkerTransport = (function WorkerTransportClosure() { } }, this); + messageHandler.on('UnsupportedFeature', + function transportUnsupportedFeature(data) { + if (this.destroyed) { + return; // Ignore any pending requests if the worker was terminated. + } + var featureId = data.featureId; + var loadingTask = this.loadingTask; + if (loadingTask.onUnsupportedFeature) { + loadingTask.onUnsupportedFeature(featureId); + } + PDFJS.UnsupportedManager.notify(featureId); + }, this); + messageHandler.on('JpegDecode', function(data) { if (this.destroyed) { return Promise.reject('Worker was terminated'); @@ -2000,3 +2016,23 @@ var InternalRenderTask = (function InternalRenderTaskClosure() { return InternalRenderTask; })(); + +/** + * (Deprecated) Global observer of unsupported feature usages. Use + * onUnsupportedFeature callback of the {PDFDocumentLoadingTask} instance. + */ +PDFJS.UnsupportedManager = (function UnsupportedManagerClosure() { + var listeners = []; + return { + listen: function (cb) { + deprecated('Global UnsupportedManager.listen is used: ' + + ' use PDFDocumentLoadingTask.onUnsupportedFeature instead'); + listeners.push(cb); + }, + notify: function (featureId) { + for (var i = 0, ii = listeners.length; i < ii; i++) { + listeners[i](featureId); + } + } + }; +})(); diff --git a/src/display/canvas.js b/src/display/canvas.js index 2fcf81bc5..723ec1687 100644 --- a/src/display/canvas.js +++ b/src/display/canvas.js @@ -15,7 +15,7 @@ /* globals IDENTITY_MATRIX, FONT_IDENTITY_MATRIX, TextRenderingMode, ImageData, ImageKind, PDFJS, Uint32ArrayView, error, WebGLUtils, OPS, warn, shadow, isNum, Util, TilingPattern, getShadingPatternFromIR, isArray, - info, assert, UnsupportedManager, UNSUPPORTED_FEATURES */ + info, assert */ 'use strict'; @@ -2092,7 +2092,6 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { }, paintXObject: function CanvasGraphics_paintXObject() { - UnsupportedManager.notify(UNSUPPORTED_FEATURES.unknown); warn('Unsupported \'paintXObject\' command.'); }, diff --git a/src/shared/util.js b/src/shared/util.js index a219d512e..984a21b93 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -236,7 +236,6 @@ function error(msg) { console.log('Error: ' + msg); console.log(backtrace()); } - UnsupportedManager.notify(UNSUPPORTED_FEATURES.unknown); throw new Error(msg); } @@ -263,22 +262,6 @@ var UNSUPPORTED_FEATURES = PDFJS.UNSUPPORTED_FEATURES = { font: 'font' }; -var UnsupportedManager = PDFJS.UnsupportedManager = - (function UnsupportedManagerClosure() { - var listeners = []; - return { - listen: function (cb) { - listeners.push(cb); - }, - notify: function (featureId) { - warn('Unsupported feature "' + featureId + '"'); - for (var i = 0, ii = listeners.length; i < ii; i++) { - listeners[i](featureId); - } - } - }; -})(); - // Combines two URLs. The baseUrl shall be absolute URL. If the url is an // absolute URL, it will be returned as is. function combineUrl(baseUrl, url) { diff --git a/web/viewer.js b/web/viewer.js index 6a50400c7..c8d1dda40 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -599,6 +599,9 @@ var PDFViewerApplication = { self.progress(progressData.loaded / progressData.total); }; + // Listen for unsupported features to trigger the fallback UI. + loadingTask.onUnsupportedFeature = this.fallback.bind(this); + var result = loadingTask.promise.then( function getDocumentCallback(pdfDocument) { self.load(pdfDocument, scale); @@ -1465,10 +1468,6 @@ function webViewerInitialized() { document.getElementById('viewFind').classList.add('hidden'); } - // Listen for unsupported features to trigger the fallback UI. - PDFJS.UnsupportedManager.listen( - PDFViewerApplication.fallback.bind(PDFViewerApplication)); - // Suppress context menus for some controls document.getElementById('scaleSelect').oncontextmenu = noContextMenuHandler;