From e9b6ffbaf6cae2bea5908be90b31c6f7f4a778c2 Mon Sep 17 00:00:00 2001 From: Julian Viereck Date: Sun, 2 Oct 2011 22:17:32 +0200 Subject: [PATCH] Kill global objects --- fonts.js | 4 ++-- pdf.js | 30 +++++++++++++++++------------- worker.js | 28 ++++++++++++++++------------ 3 files changed, 35 insertions(+), 27 deletions(-) diff --git a/fonts.js b/fonts.js index 1fb7577b1..32f879563 100755 --- a/fonts.js +++ b/fonts.js @@ -210,14 +210,14 @@ var FontLoader = { return rule; }, - bind: function fontLoaderBind(fonts, callback) { + bind: function fontLoaderBind(fonts, callback, objects) { function checkFontsLoaded() { for (var i = 0; i < objs.length; i++) { var fontObj = objs[i]; if (fontObj.loading) { return false; } - Objects.resolve(fontObj.loadedName); + objects.resolve(fontObj.loadedName); } document.documentElement.removeEventListener( diff --git a/pdf.js b/pdf.js index 9d2d27899..c0fa98d5f 100644 --- a/pdf.js +++ b/pdf.js @@ -860,7 +860,7 @@ var PredictorStream = (function() { })(); var JpegStreamIR = (function() { - function JpegStreamIR(objId, IR) { + function JpegStreamIR(objId, IR, objs) { var src = 'data:image/jpeg;base64,' + window.btoa(IR); // create DOM image @@ -868,7 +868,7 @@ var JpegStreamIR = (function() { img.onload = (function() { this.loaded = true; - Objects.resolve(objId, this); + objs.resolve(objId, this); if (this.onLoad) this.onLoad(); @@ -3392,7 +3392,7 @@ var Page = (function() { for (var i = 0; i < fonts.length; i++) { // HACK FOR NOW. Access the data directly. This isn't allowed as the // font object isn't resolved yet. - fonts[i] = Objects.objs[fonts[i]].data; + fonts[i] = this.objs.objs[fonts[i]].data; } // Load all the fonts @@ -3402,7 +3402,8 @@ var Page = (function() { this.stats.fonts = Date.now(); callback.call(this); - }.bind(this) + }.bind(this), + this.objs ); }, @@ -4852,14 +4853,15 @@ var CanvasGraphics = (function() { // if we execute longer then `kExecutionTime`. var kExecutionTimeCheck = 500; - function constructor(canvasCtx, imageCanvas) { + function constructor(canvasCtx, objs) { this.ctx = canvasCtx; this.current = new CanvasExtraState(); this.stateStack = []; this.pendingClip = null; this.res = null; this.xobjs = null; - this.ScratchCanvas = imageCanvas || ScratchCanvas; + this.ScratchCanvas = ScratchCanvas; + this.objs = objs; } var LINE_CAP_STYLES = ['butt', 'round', 'square']; @@ -4897,6 +4899,8 @@ var CanvasGraphics = (function() { var executionEndIdx; var startTime = Date.now(); + var objs = this.objs; + do { executionEndIdx = Math.min(argsArrayLen, i + kExecutionTimeCheck); @@ -4910,8 +4914,8 @@ var CanvasGraphics = (function() { // If the promise isn't resolved yet, add the continueCallback // to the promise and bail out. - if (!Objects.isResolved(depObjId)) { - Objects.get(depObjId, continueCallback); + if (!objs.isResolved(depObjId)) { + objs.get(depObjId, continueCallback); return i; } } @@ -5133,7 +5137,7 @@ var CanvasGraphics = (function() { setFont: function(fontRef, size) { // Lookup the fontObj using fontRef only. var fontRefName = fontRef.name; - var fontObj = Objects.get(fontRefName); + var fontObj = this.objs.get(fontRefName); if (!fontObj) { throw "Can't find font for " + fontRefName; @@ -5314,7 +5318,7 @@ var CanvasGraphics = (function() { } // Build the pattern based on the IR data. - var pattern = new TilingPatternIR(IR, color, this.ctx); + var pattern = new TilingPatternIR(IR, color, this.ctx, this.objs); } else if (IR[0] == "RadialAxialShading" || IR[0] == "DummyShading") { var pattern = Pattern.shadingFromIR(this.ctx, IR); } else { @@ -5434,7 +5438,7 @@ var CanvasGraphics = (function() { }, paintJpegXObject: function(objId, w, h) { - var image = Objects.get(objId); + var image = this.objs.get(objId); if (!image) { error("Dependent image isn't ready yet"); } @@ -6185,7 +6189,7 @@ var RadialAxialShading = (function() { var TilingPatternIR = (function() { var PAINT_TYPE_COLORED = 1, PAINT_TYPE_UNCOLORED = 2; - function TilingPatternIR(IR, color, ctx) { + function TilingPatternIR(IR, color, ctx, objs) { // "Unfolding" the IR. var IRQueue = IR[2]; this.matrix = IR[3]; @@ -6223,7 +6227,7 @@ var TilingPatternIR = (function() { // set the new canvas element context as the graphics context var tmpCtx = tmpCanvas.getContext('2d'); - var graphics = new CanvasGraphics(tmpCtx); + var graphics = new CanvasGraphics(tmpCtx, objs); switch (paintType) { case PAINT_TYPE_COLORED: diff --git a/worker.js b/worker.js index 6db8232d1..a89ffb7a7 100644 --- a/worker.js +++ b/worker.js @@ -7,9 +7,10 @@ var useWorker = false; var WorkerPage = (function() { - function constructor(workerPDF, page) { + function constructor(workerPDF, page, objs) { this.workerPDF = workerPDF; this.page = page; + this.objs = objs; this.ref = page.ref; } @@ -38,7 +39,7 @@ var WorkerPage = (function() { }, startRenderingFromIRQueue: function(IRQueue, fonts) { - var gfx = new CanvasGraphics(this.ctx); + var gfx = new CanvasGraphics(this.ctx, this.objs); var startTime = Date.now(); var callback = function(err) { @@ -250,8 +251,6 @@ var Promise = (function() { return Promise; })(); -var Objects = new PDFObjects(); - var WorkerPDFDoc = (function() { function constructor(data) { @@ -260,6 +259,7 @@ var WorkerPDFDoc = (function() { this.pdf = new PDFDoc(this.stream); this.catalog = this.pdf.catalog; + this.objs = new PDFObjects(); this.pageCache = []; @@ -290,6 +290,7 @@ var WorkerPDFDoc = (function() { // aren't loaded on the page yet. var depFonts = data.depFonts; var fontsToLoad = []; + var objs = this.objs; function checkFontData() { // Check if all fontObjs have been processed. If not, shedule a @@ -297,11 +298,11 @@ var WorkerPDFDoc = (function() { // the next fonts. for (var i = 0; i < depFonts.length; i++) { var fontName = depFonts[i]; - if (!Objects.hasData(fontName)) { + if (!objs.hasData(fontName)) { console.log('need to wait for fontData', fontName); - Objects.onData(fontObj, checkFontData); + objs.onData(fontObj, checkFontData); return; - } else if (!Objects.isResolved(fontName)) { + } else if (!objs.isResolved(fontName)) { fontsToLoad.push(fontName); } } @@ -320,7 +321,7 @@ var WorkerPDFDoc = (function() { switch (objType) { case "JpegStream": var IR = data[2]; - new JpegStreamIR(objId, IR); + new JpegStreamIR(objId, IR, this.objs); console.log('got image'); break; case "Font": @@ -344,11 +345,11 @@ var WorkerPDFDoc = (function() { // If there is no string, then there is nothing to attach to the DOM. if (!fontObj.str) { - Objects.resolve(objId, fontObj); + this.objs.resolve(objId, fontObj); } else { - Objects.setData(objId, fontObj); + this.objs.setData(objId, fontObj); } - }); + }.bind(this)); if (!useWorker) { // If the main thread is our worker, setup the handling for the messages @@ -375,7 +376,10 @@ var WorkerPDFDoc = (function() { } var page = this.pdf.getPage(n); - return this.pageCache[n] = new WorkerPage(this, page); + // Add a reference to the objects such that Page can forward the reference + // to the CanvasGraphics and so on. + page.objs = this.objs; + return this.pageCache[n] = new WorkerPage(this, page, this.objs); }, destroy: function() {