From cf7d4503d67f83c10f29d6551aea205d15991e18 Mon Sep 17 00:00:00 2001 From: Julian Viereck Date: Sat, 3 Dec 2011 09:16:40 +0100 Subject: [PATCH] Address review comments: Introduce new CanvasGraphics.putBinaryImageData and make sure feature detection work always --- src/canvas.js | 79 ++++++++++++++++++++++++++------------------------- src/core.js | 2 ++ src/fonts.js | 2 -- 3 files changed, 42 insertions(+), 41 deletions(-) diff --git a/src/canvas.js b/src/canvas.js index af2555c4e..d10469ee9 100644 --- a/src/canvas.js +++ b/src/canvas.js @@ -3,29 +3,6 @@ 'use strict'; -// Some browsers can use UInt8Array directly as imageData. -PDFJS.FEATURE_CANVAS_UINT_IMAGE_DATA = false; -if (typeof window !== 'undefined') { - window.addEventListener('load', function featureCanvasUIntImgaData() { - var canvas = document.createElement('canvas'); - canvas.width = 1; - canvas.height = 1; - var ctx = canvas.getContext('2d'); - - try { - ctx.putImageData({ - width: 1, - height: 1, - data: new Uint8Array(4) - }, 0, 0); - } catch (e) { - return; - } - - PDFJS.FEATURE_CANVAS_UINT_IMAGE_DATA = true; - }); -} - // contexts store most of the state we need natively. // However, PDF needs a bit more state, which we store here. @@ -810,27 +787,16 @@ var CanvasGraphics = (function canvasGraphics() { var tmpCanvas = new this.ScratchCanvas(w, h); var tmpCtx = tmpCanvas.getContext('2d'); - var tmpImgData; + this.putBinaryImageData(tmpCtx, imgData, w, h); - if (PDFJS.FEATURE_CANVAS_UINT_IMAGE_DATA) { - tmpImgData = imgData; - } else { - tmpImgData = tmpCtx.getImageData(0, 0, w, h); - - // Copy over the imageData pixel by pixel. - var tmpImgDataPixels = tmpImgData.data; - var len = tmpImgDataPixels.length; - - while (len--) { - tmpImgDataPixels[len] = imgData.data[len]; - } - } - - tmpCtx.putImageData(tmpImgData, 0, 0); ctx.drawImage(tmpCanvas, 0, -h); this.restore(); }, + putBinaryImageData: function canvasPutBinaryImageData() { + // + }, + // Marked content markPoint: function canvasGraphicsMarkPoint(tag) { @@ -891,3 +857,38 @@ var CanvasGraphics = (function canvasGraphics() { return constructor; })(); +if (!isWorker) { + // Feature detection if the browser can use an Uint8Array directly as imgData. + var canvas = document.createElement('canvas'); + canvas.width = 1; + canvas.height = 1; + var ctx = canvas.getContext('2d'); + + try { + ctx.putImageData({ + width: 1, + height: 1, + data: new Uint8Array(4) + }, 0, 0); + + CanvasGraphics.prototype.putBinaryImageData = + function CanvasGraphicsPutBinaryImageDataNative(ctx, imgData) { + ctx.putImageData(imgData, 0, 0); + }; + } catch (e) { + CanvasGraphics.prototype.putBinaryImageData = + function CanvasGraphicsPutBinaryImageDataShim(ctx, imgData, w, h) { + var tmpImgData = ctx.getImageData(0, 0, w, h); + + // Copy over the imageData pixel by pixel. + var tmpImgDataPixels = tmpImgData.data; + var len = tmpImgDataPixels.length; + + while (len--) { + tmpImgDataPixels[len] = imgData.data[len]; + } + + ctx.putImageData(tmpImgData, 0, 0); + }; + } +} diff --git a/src/core.js b/src/core.js index 3549eb906..5735d7415 100644 --- a/src/core.js +++ b/src/core.js @@ -5,6 +5,8 @@ var globalScope = (typeof window === 'undefined') ? this : window; +var isWorker = (typeof window == 'undefined'); + var ERRORS = 0, WARNINGS = 1, TODOS = 5; var verbosity = WARNINGS; diff --git a/src/fonts.js b/src/fonts.js index 116bb4dfc..3809123f7 100644 --- a/src/fonts.js +++ b/src/fonts.js @@ -3,8 +3,6 @@ 'use strict'; -var isWorker = (typeof window == 'undefined'); - /** * Maximum time to wait for a font to be loaded by font-face rules. */