Browse Source

Kill global objects

Julian Viereck 14 years ago
parent
commit
e9b6ffbaf6
  1. 4
      fonts.js
  2. 30
      pdf.js
  3. 28
      worker.js

4
fonts.js

@ -210,14 +210,14 @@ var FontLoader = {
return rule; return rule;
}, },
bind: function fontLoaderBind(fonts, callback) { bind: function fontLoaderBind(fonts, callback, objects) {
function checkFontsLoaded() { function checkFontsLoaded() {
for (var i = 0; i < objs.length; i++) { for (var i = 0; i < objs.length; i++) {
var fontObj = objs[i]; var fontObj = objs[i];
if (fontObj.loading) { if (fontObj.loading) {
return false; return false;
} }
Objects.resolve(fontObj.loadedName); objects.resolve(fontObj.loadedName);
} }
document.documentElement.removeEventListener( document.documentElement.removeEventListener(

30
pdf.js

@ -860,7 +860,7 @@ var PredictorStream = (function() {
})(); })();
var JpegStreamIR = (function() { var JpegStreamIR = (function() {
function JpegStreamIR(objId, IR) { function JpegStreamIR(objId, IR, objs) {
var src = 'data:image/jpeg;base64,' + window.btoa(IR); var src = 'data:image/jpeg;base64,' + window.btoa(IR);
// create DOM image // create DOM image
@ -868,7 +868,7 @@ var JpegStreamIR = (function() {
img.onload = (function() { img.onload = (function() {
this.loaded = true; this.loaded = true;
Objects.resolve(objId, this); objs.resolve(objId, this);
if (this.onLoad) if (this.onLoad)
this.onLoad(); this.onLoad();
@ -3392,7 +3392,7 @@ var Page = (function() {
for (var i = 0; i < fonts.length; i++) { for (var i = 0; i < fonts.length; i++) {
// HACK FOR NOW. Access the data directly. This isn't allowed as the // HACK FOR NOW. Access the data directly. This isn't allowed as the
// font object isn't resolved yet. // 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 // Load all the fonts
@ -3402,7 +3402,8 @@ var Page = (function() {
this.stats.fonts = Date.now(); this.stats.fonts = Date.now();
callback.call(this); callback.call(this);
}.bind(this) }.bind(this),
this.objs
); );
}, },
@ -4852,14 +4853,15 @@ var CanvasGraphics = (function() {
// if we execute longer then `kExecutionTime`. // if we execute longer then `kExecutionTime`.
var kExecutionTimeCheck = 500; var kExecutionTimeCheck = 500;
function constructor(canvasCtx, imageCanvas) { function constructor(canvasCtx, objs) {
this.ctx = canvasCtx; this.ctx = canvasCtx;
this.current = new CanvasExtraState(); this.current = new CanvasExtraState();
this.stateStack = []; this.stateStack = [];
this.pendingClip = null; this.pendingClip = null;
this.res = null; this.res = null;
this.xobjs = null; this.xobjs = null;
this.ScratchCanvas = imageCanvas || ScratchCanvas; this.ScratchCanvas = ScratchCanvas;
this.objs = objs;
} }
var LINE_CAP_STYLES = ['butt', 'round', 'square']; var LINE_CAP_STYLES = ['butt', 'round', 'square'];
@ -4897,6 +4899,8 @@ var CanvasGraphics = (function() {
var executionEndIdx; var executionEndIdx;
var startTime = Date.now(); var startTime = Date.now();
var objs = this.objs;
do { do {
executionEndIdx = Math.min(argsArrayLen, i + kExecutionTimeCheck); executionEndIdx = Math.min(argsArrayLen, i + kExecutionTimeCheck);
@ -4910,8 +4914,8 @@ var CanvasGraphics = (function() {
// If the promise isn't resolved yet, add the continueCallback // If the promise isn't resolved yet, add the continueCallback
// to the promise and bail out. // to the promise and bail out.
if (!Objects.isResolved(depObjId)) { if (!objs.isResolved(depObjId)) {
Objects.get(depObjId, continueCallback); objs.get(depObjId, continueCallback);
return i; return i;
} }
} }
@ -5133,7 +5137,7 @@ var CanvasGraphics = (function() {
setFont: function(fontRef, size) { setFont: function(fontRef, size) {
// Lookup the fontObj using fontRef only. // Lookup the fontObj using fontRef only.
var fontRefName = fontRef.name; var fontRefName = fontRef.name;
var fontObj = Objects.get(fontRefName); var fontObj = this.objs.get(fontRefName);
if (!fontObj) { if (!fontObj) {
throw "Can't find font for " + fontRefName; throw "Can't find font for " + fontRefName;
@ -5314,7 +5318,7 @@ var CanvasGraphics = (function() {
} }
// Build the pattern based on the IR data. // 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") { } else if (IR[0] == "RadialAxialShading" || IR[0] == "DummyShading") {
var pattern = Pattern.shadingFromIR(this.ctx, IR); var pattern = Pattern.shadingFromIR(this.ctx, IR);
} else { } else {
@ -5434,7 +5438,7 @@ var CanvasGraphics = (function() {
}, },
paintJpegXObject: function(objId, w, h) { paintJpegXObject: function(objId, w, h) {
var image = Objects.get(objId); var image = this.objs.get(objId);
if (!image) { if (!image) {
error("Dependent image isn't ready yet"); error("Dependent image isn't ready yet");
} }
@ -6185,7 +6189,7 @@ var RadialAxialShading = (function() {
var TilingPatternIR = (function() { var TilingPatternIR = (function() {
var PAINT_TYPE_COLORED = 1, PAINT_TYPE_UNCOLORED = 2; var PAINT_TYPE_COLORED = 1, PAINT_TYPE_UNCOLORED = 2;
function TilingPatternIR(IR, color, ctx) { function TilingPatternIR(IR, color, ctx, objs) {
// "Unfolding" the IR. // "Unfolding" the IR.
var IRQueue = IR[2]; var IRQueue = IR[2];
this.matrix = IR[3]; this.matrix = IR[3];
@ -6223,7 +6227,7 @@ var TilingPatternIR = (function() {
// set the new canvas element context as the graphics context // set the new canvas element context as the graphics context
var tmpCtx = tmpCanvas.getContext('2d'); var tmpCtx = tmpCanvas.getContext('2d');
var graphics = new CanvasGraphics(tmpCtx); var graphics = new CanvasGraphics(tmpCtx, objs);
switch (paintType) { switch (paintType) {
case PAINT_TYPE_COLORED: case PAINT_TYPE_COLORED:

28
worker.js

@ -7,9 +7,10 @@
var useWorker = false; var useWorker = false;
var WorkerPage = (function() { var WorkerPage = (function() {
function constructor(workerPDF, page) { function constructor(workerPDF, page, objs) {
this.workerPDF = workerPDF; this.workerPDF = workerPDF;
this.page = page; this.page = page;
this.objs = objs;
this.ref = page.ref; this.ref = page.ref;
} }
@ -38,7 +39,7 @@ var WorkerPage = (function() {
}, },
startRenderingFromIRQueue: function(IRQueue, fonts) { startRenderingFromIRQueue: function(IRQueue, fonts) {
var gfx = new CanvasGraphics(this.ctx); var gfx = new CanvasGraphics(this.ctx, this.objs);
var startTime = Date.now(); var startTime = Date.now();
var callback = function(err) { var callback = function(err) {
@ -250,8 +251,6 @@ var Promise = (function() {
return Promise; return Promise;
})(); })();
var Objects = new PDFObjects();
var WorkerPDFDoc = (function() { var WorkerPDFDoc = (function() {
function constructor(data) { function constructor(data) {
@ -260,6 +259,7 @@ var WorkerPDFDoc = (function() {
this.pdf = new PDFDoc(this.stream); this.pdf = new PDFDoc(this.stream);
this.catalog = this.pdf.catalog; this.catalog = this.pdf.catalog;
this.objs = new PDFObjects();
this.pageCache = []; this.pageCache = [];
@ -290,6 +290,7 @@ var WorkerPDFDoc = (function() {
// aren't loaded on the page yet. // aren't loaded on the page yet.
var depFonts = data.depFonts; var depFonts = data.depFonts;
var fontsToLoad = []; var fontsToLoad = [];
var objs = this.objs;
function checkFontData() { function checkFontData() {
// Check if all fontObjs have been processed. If not, shedule a // Check if all fontObjs have been processed. If not, shedule a
@ -297,11 +298,11 @@ var WorkerPDFDoc = (function() {
// the next fonts. // the next fonts.
for (var i = 0; i < depFonts.length; i++) { for (var i = 0; i < depFonts.length; i++) {
var fontName = depFonts[i]; var fontName = depFonts[i];
if (!Objects.hasData(fontName)) { if (!objs.hasData(fontName)) {
console.log('need to wait for fontData', fontName); console.log('need to wait for fontData', fontName);
Objects.onData(fontObj, checkFontData); objs.onData(fontObj, checkFontData);
return; return;
} else if (!Objects.isResolved(fontName)) { } else if (!objs.isResolved(fontName)) {
fontsToLoad.push(fontName); fontsToLoad.push(fontName);
} }
} }
@ -320,7 +321,7 @@ var WorkerPDFDoc = (function() {
switch (objType) { switch (objType) {
case "JpegStream": case "JpegStream":
var IR = data[2]; var IR = data[2];
new JpegStreamIR(objId, IR); new JpegStreamIR(objId, IR, this.objs);
console.log('got image'); console.log('got image');
break; break;
case "Font": case "Font":
@ -344,11 +345,11 @@ var WorkerPDFDoc = (function() {
// If there is no string, then there is nothing to attach to the DOM. // If there is no string, then there is nothing to attach to the DOM.
if (!fontObj.str) { if (!fontObj.str) {
Objects.resolve(objId, fontObj); this.objs.resolve(objId, fontObj);
} else { } else {
Objects.setData(objId, fontObj); this.objs.setData(objId, fontObj);
} }
}); }.bind(this));
if (!useWorker) { if (!useWorker) {
// If the main thread is our worker, setup the handling for the messages // 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); 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() { destroy: function() {

Loading…
Cancel
Save