From 438e3c8f6d2bd5af38a158cfae20be90760cda80 Mon Sep 17 00:00:00 2001 From: Pimm Hogeling Date: Fri, 31 Aug 2012 13:37:44 +0200 Subject: [PATCH 1/3] Fixes for subtile bugs that were introduced in 59283bdf6d439fdcd1a0ab07b318b48031091b34 and 99440ab6912dee9b780d28b9c425434f4afd8e44, among others. --- external/jpgjs/jpg.js | 9 +++++---- src/colorspace.js | 2 +- src/core.js | 2 +- src/fonts.js | 2 +- src/function.js | 2 +- src/worker.js | 4 ++-- 6 files changed, 11 insertions(+), 10 deletions(-) diff --git a/external/jpgjs/jpg.js b/external/jpgjs/jpg.js index 941d01a87..6873fc6c3 100644 --- a/external/jpgjs/jpg.js +++ b/external/jpgjs/jpg.js @@ -516,6 +516,10 @@ var JpegImage = (function jpegImage() { return lines; } + function clampTo8bit(a) { + return a < 0 ? 0 : a > 255 ? 255 : a; + } + constructor.prototype = { load: function load(path) { var xhr = new XMLHttpRequest(); @@ -766,9 +770,6 @@ var JpegImage = (function jpegImage() { } }, getData: function getData(width, height) { - function clampTo8bit(a) { - return a < 0 ? 0 : a > 255 ? 255 : a; - } var scaleX = this.width / width, scaleY = this.height / height; var component1, component2, component3, component4; @@ -935,4 +936,4 @@ var JpegImage = (function jpegImage() { }; return constructor; -})(); \ No newline at end of file +})(); diff --git a/src/colorspace.js b/src/colorspace.js index 5d6eee7a7..0c8f48b57 100644 --- a/src/colorspace.js +++ b/src/colorspace.js @@ -179,7 +179,7 @@ var ColorSpace = (function ColorSpaceClosure() { return true; if (n * 2 !== decode.length) { - warning('The decode map is not the correct length'); + warn('The decode map is not the correct length'); return true; } for (var i = 0, ii = decode.length; i < ii; i += 2) { diff --git a/src/core.js b/src/core.js index aa366a6db..704bc7356 100644 --- a/src/core.js +++ b/src/core.js @@ -222,7 +222,7 @@ var Page = (function PageClosure() { }, getLinks: function Page_getLinks() { var links = []; - var annotations = pageGetAnnotations(); + var annotations = this.getAnnotations(); var i, n = annotations.length; for (i = 0; i < n; ++i) { if (annotations[i].type != 'Link') diff --git a/src/fonts.js b/src/fonts.js index 4af42265d..e2b760542 100644 --- a/src/fonts.js +++ b/src/fonts.js @@ -4671,7 +4671,7 @@ var CFFParser = (function CFFParserClosure() { var data = charstring; var length = data.length; - for (var j = 0; j <= length; j) { + for (var j = 0; j <= length;) { var value = data[j++]; if (value == 12 && data[j++] == 0) { data[j - 2] = 139; diff --git a/src/function.js b/src/function.js index 2088ee219..5577aaf1e 100644 --- a/src/function.js +++ b/src/function.js @@ -153,7 +153,7 @@ var PDFFunction = (function PDFFunctionClosure() { var range = IR[9]; if (m != args.length) - error('Incorrect number of arguments: ' + inputSize + ' != ' + + error('Incorrect number of arguments: ' + m + ' != ' + args.length); var x = args; diff --git a/src/worker.js b/src/worker.js index 791a12d9c..3b3b68026 100644 --- a/src/worker.js +++ b/src/worker.js @@ -220,7 +220,7 @@ var WorkerMessageHandler = { return; } - console.log('page=%d - getOperatorList: time=%dms, len=%d', pageNum, + log('page=%d - getOperatorList: time=%dms, len=%d', pageNum, Date.now() - start, operatorList.fnArray.length); // Filter the dependecies for fonts. @@ -252,7 +252,7 @@ var WorkerMessageHandler = { promise.reject(e); } - console.log('text indexing: page=%d - time=%dms', + log('text indexing: page=%d - time=%dms', pageNum, Date.now() - start); }); } From 0fbbc5a840e1890e0ef0aa30b18e64e8fb53f584 Mon Sep 17 00:00:00 2001 From: Pimm Hogeling Date: Fri, 31 Aug 2012 13:40:37 +0200 Subject: [PATCH 2/3] Syntax changed that will help minifiers/compilers (such as the Closure Compiler) in their processing. In particular, preventing said minifiers/compilers from making dangerous renames. --- src/api.js | 5 +++-- src/fonts.js | 5 +++-- src/jpx.js | 2 +- src/util.js | 27 ++++++++++++++++++++------- src/worker.js | 20 +++++++++++++------- 5 files changed, 40 insertions(+), 19 deletions(-) diff --git a/src/api.js b/src/api.js index 6c862a91b..f1f6bc66f 100644 --- a/src/api.js +++ b/src/api.js @@ -345,8 +345,9 @@ var PDFPageProxy = (function PDFPageProxyClosure() { var length = this.operatorList.fnArray.length; var operatorList = this.operatorList; var stepper = null; - if (PDFJS.pdfBug && StepperManager.enabled) { - stepper = StepperManager.create(this.pageNumber - 1); + if (PDFJS.pdfBug && 'StepperManager' in globalScope && + globalScope['StepperManager'].enabled) { + stepper = globalScope['StepperManager'].create(this.pageNumber - 1); stepper.init(operatorList); stepper.nextBreakPoint = stepper.getNextBreakPoint(); } diff --git a/src/fonts.js b/src/fonts.js index e2b760542..c26a39ca1 100644 --- a/src/fonts.js +++ b/src/fonts.js @@ -3141,8 +3141,9 @@ var Font = (function FontClosure() { var styleSheet = styleElement.sheet; styleSheet.insertRule(rule, styleSheet.cssRules.length); - if (PDFJS.pdfBug && FontInspector.enabled) - FontInspector.fontAdded(this, url); + if (PDFJS.pdfBug && 'FontInspector' in globalScope && + globalScope['FontInspector'].enabled) + globalScope['FontInspector'].fontAdded(this, url); return rule; }, diff --git a/src/jpx.js b/src/jpx.js index fdf41f48b..c0c5d4116 100644 --- a/src/jpx.js +++ b/src/jpx.js @@ -255,7 +255,7 @@ var JpxImage = (function JpxImageClosure() { cod.resetContextProbabilities || cod.terminationOnEachCodingPass || cod.verticalyStripe || cod.predictableTermination) - throw 'Unsupported COD options: ' + uneval(cod); + throw 'Unsupported COD options: ' + globalScope.JSON.stringify(cod); if (context.mainHeader) context.COD = cod; diff --git a/src/util.js b/src/util.js index df91f6064..da721c57e 100644 --- a/src/util.js +++ b/src/util.js @@ -5,12 +5,16 @@ // Use only for debugging purposes. This should not be used in any code that is // in mozilla master. -function log(msg) { - if (console && console.log) - console.log(msg); - else if (print) - print(msg); -} +var log = (function() { + if ('console' in globalScope && 'log' in globalScope['console']) { + return globalScope['console']['log'].bind(globalScope['console']); + } else if ('print' in globalScope) { + return globalScope['print'].bind(globalScope); + } else { + return function nop() { + }; + } +})(); // A notice for devs that will not trigger the fallback UI. These are good // for things that are helpful to devs, such as warning that Workers were @@ -33,7 +37,16 @@ function warn(msg) { // Fatal errors that should trigger the fallback UI and halt execution by // throwing an exception. function error(msg) { - log('Error: ' + msg); + // If multiple arguments were passed, pass them all to the log function. + if (arguments.length > 1) { + var logArguments = ['Error:']; + logArguments.push.apply(logArguments, arguments); + log.apply(null, logArguments); + // Join the arguments into a single string for the lines below. + msg = [].join.call(arguments, ' '); + } else { + log('Error: ' + msg); + } log(backtrace()); PDFJS.LogManager.notify('error', msg); throw new Error(msg); diff --git a/src/worker.js b/src/worker.js index 3b3b68026..25a509e80 100644 --- a/src/worker.js +++ b/src/worker.js @@ -11,11 +11,17 @@ function MessageHandler(name, comObj) { var ah = this.actionHandler = {}; ah['console_log'] = [function ahConsoleLog(data) { - console.log.apply(console, data); - }]; - ah['console_error'] = [function ahConsoleError(data) { - console.error.apply(console, data); + log.apply(null, data); }]; + // If there's no console available, console_error in the action handler will do nothing. + if ('console' in globalScope) { + ah['console_error'] = [function ahConsoleError(data) { + globalScope['console'].error.apply(null, data); + }]; + } else { + ah['console_error'] = [function ahConsoleError(data) { + }]; + } ah['_warn'] = [function ah_Warn(data) { warn(data); }]; @@ -263,7 +269,7 @@ var consoleTimer = {}; var workerConsole = { log: function log() { var args = Array.prototype.slice.call(arguments); - postMessage({ + globalScope.postMessage({ action: 'console_log', data: args }); @@ -271,7 +277,7 @@ var workerConsole = { error: function error() { var args = Array.prototype.slice.call(arguments); - postMessage({ + globalScope.postMessage({ action: 'console_error', data: args }); @@ -299,7 +305,7 @@ if (typeof window === 'undefined') { // throw an exception which will be forwarded on automatically. PDFJS.LogManager.addLogger({ warn: function(msg) { - postMessage({ + globalScope.postMessage({ action: '_warn', data: msg }); From 080ab94a0d023934b03e07081e3de27f8559cc21 Mon Sep 17 00:00:00 2001 From: Pimm Hogeling Date: Fri, 31 Aug 2012 15:51:31 +0200 Subject: [PATCH 3/3] Lint syntax fixes. --- src/jpx.js | 3 ++- src/worker.js | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/jpx.js b/src/jpx.js index c0c5d4116..c32ab18e2 100644 --- a/src/jpx.js +++ b/src/jpx.js @@ -255,7 +255,8 @@ var JpxImage = (function JpxImageClosure() { cod.resetContextProbabilities || cod.terminationOnEachCodingPass || cod.verticalyStripe || cod.predictableTermination) - throw 'Unsupported COD options: ' + globalScope.JSON.stringify(cod); + throw 'Unsupported COD options: ' + + globalScope.JSON.stringify(cod); if (context.mainHeader) context.COD = cod; diff --git a/src/worker.js b/src/worker.js index 25a509e80..940453350 100644 --- a/src/worker.js +++ b/src/worker.js @@ -13,7 +13,8 @@ function MessageHandler(name, comObj) { ah['console_log'] = [function ahConsoleLog(data) { log.apply(null, data); }]; - // If there's no console available, console_error in the action handler will do nothing. + // If there's no console available, console_error in the + // action handler will do nothing. if ('console' in globalScope) { ah['console_error'] = [function ahConsoleError(data) { globalScope['console'].error.apply(null, data);