Use all over the place and cleanup/renomve not longer needed code

This commit is contained in:
Julian Viereck 2011-09-07 17:02:01 -07:00
parent a7d1c84c92
commit 790816bbdd
3 changed files with 32 additions and 117 deletions

129
pdf.js
View File

@ -3374,50 +3374,12 @@ var Page = (function() {
} }
return shadow(this, 'rotate', rotate); return shadow(this, 'rotate', rotate);
}, },
startRendering: function(canvasCtx, continuation) {
var gfx = new CanvasGraphics(canvasCtx);
// If there is already some code to render, then use it directly.
if (this.code) {
this.display(gfx);
return;
}
startRenderingFromIRQueue: function(gfx, IRQueue, fonts, images, continuation) {
var self = this; var self = this;
var stats = self.stats; this.IRQueue = IRQueue;
stats.compile = stats.fonts = stats.render = 0;
var fonts = []; var displayContinuation = function() {
var images = new ImagesLoader();
var preCompilation = this.preCompile(gfx, fonts, images);
stats.compile = Date.now();
// Make a copy of the necessary datat to build a font later. The `font`
// object will be sent to the main thread later on.
var fontsBackup = fonts;
fonts = [];
for (var i = 0; i < fontsBackup.length; i++) {
var orgFont = fontsBackup[i];
var font = {
name: orgFont.name,
file: orgFont.file,
properties: orgFont.properties
}
fonts.push(font);
}
this.startRenderingFromPreCompilation(gfx, preCompilation, fonts, images, continuation);
},
startRenderingFromPreCompilation: function(gfx, preCompilation, fonts, images, continuation) {
var self = this;
var displayContinuation = function() {
self.code = gfx.postCompile(preCompilation);
// Always defer call to display() to work around bug in // Always defer call to display() to work around bug in
// Firefox error reporting from XHR callbacks. // Firefox error reporting from XHR callbacks.
setTimeout(function() { setTimeout(function() {
@ -3440,10 +3402,10 @@ var Page = (function() {
}) })
}, },
preCompile: function(gfx, fonts, images) { getIRQueue: function(fonts, images) {
if (this.code) { if (this.IRQueue) {
// content was compiled // content was compiled
return; return this.IRQueue;
} }
var xref = this.xref; var xref = this.xref;
@ -3456,8 +3418,9 @@ var Page = (function() {
content[i] = xref.fetchIfRef(content[i]); content[i] = xref.fetchIfRef(content[i]);
content = new StreamsSequenceStream(content); content = new StreamsSequenceStream(content);
} }
return gfx.preCompile(content, xref, resources, fonts, images,
this.pageNumber + "_"); var pe = this.pe = new PartialEvaluator();
return this.IRQueue = pe.getIRQueue(content, xref, resources, fonts, images, this.pageNumber + "_");
}, },
ensureFonts: function(fonts, callback) { ensureFonts: function(fonts, callback) {
@ -3481,8 +3444,6 @@ var Page = (function() {
}, },
display: function(gfx) { display: function(gfx) {
assert(this.code instanceof Function,
'page content must be compiled first');
var xref = this.xref; var xref = this.xref;
var resources = xref.fetchIfRef(this.resources); var resources = xref.fetchIfRef(this.resources);
var mediaBox = xref.fetchIfRef(this.mediaBox); var mediaBox = xref.fetchIfRef(this.mediaBox);
@ -3491,7 +3452,7 @@ var Page = (function() {
width: this.width, width: this.width,
height: this.height, height: this.height,
rotate: this.rotate }); rotate: this.rotate });
gfx.execute(this.code, xref, resources); gfx.executeIRQueue(this.IRQueue);
gfx.endDrawing(); gfx.endDrawing();
}, },
rotatePoint: function(x, y) { rotatePoint: function(x, y) {
@ -4224,7 +4185,7 @@ var PartialEvaluator = (function() {
}; };
constructor.prototype = { constructor.prototype = {
evalRaw: function(stream, xref, resources, fonts, images, uniquePrefix) { getIRQueue: function(stream, xref, resources, fonts, images, uniquePrefix) {
uniquePrefix = uniquePrefix || ""; uniquePrefix = uniquePrefix || "";
resources = xref.fetchIfRef(resources) || new Dict(); resources = xref.fetchIfRef(resources) || new Dict();
@ -4257,8 +4218,8 @@ var PartialEvaluator = (function() {
// Type1 is TilingPattern // Type1 is TilingPattern
if (typeNum == 1) { if (typeNum == 1) {
// Create an IR of the pattern code. // Create an IR of the pattern code.
var codeIR = this.evalRaw(pattern, xref, var codeIR = this.getIRQueue(pattern, xref,
dict.get('Resources'), fonts); dict.get('Resources'), fonts, images, uniquePrefix);
args = TilingPattern.getIR(codeIR, dict); args = TilingPattern.getIR(codeIR, dict);
} }
@ -4289,7 +4250,7 @@ var PartialEvaluator = (function() {
if ('Form' == type.name) { if ('Form' == type.name) {
// console.log("got xobj that is a Form"); // console.log("got xobj that is a Form");
var raw = this.evalRaw(xobj, xref, xobj.dict.get('Resources'), var raw = this.getIRQueue(xobj, xref, xobj.dict.get('Resources'),
fonts, images, uniquePrefix); fonts, images, uniquePrefix);
var matrix = xobj.dict.get('Matrix'); var matrix = xobj.dict.get('Matrix');
var bbox = xobj.dict.get('BBox'); var bbox = xobj.dict.get('BBox');
@ -4425,26 +4386,6 @@ var PartialEvaluator = (function() {
argsArray: argsArray argsArray: argsArray
}; };
}, },
eval: function(stream, xref, resources, fonts, images, uniquePrefix) {
var ret = this.evalRaw(stream, xref, resources, fonts, images, uniquePrefix);
return function(gfx) {
var argsArray = ret.argsArray;
var fnArray = ret.fnArray;
for (var i = 0, length = argsArray.length; i < length; i++)
gfx[fnArray[i]].apply(gfx, argsArray[i]);
};
},
evalFromRaw: function(raw) {
return function(gfx) {
var argsArray = raw.argsArray;
var fnArray = raw.fnArray;
for (var i = 0, length = argsArray.length; i < length; i++)
gfx[fnArray[i]].apply(gfx, argsArray[i]);
};
},
extractEncoding: function(dict, xref, properties) { extractEncoding: function(dict, xref, properties) {
var type = properties.type, encoding; var type = properties.type, encoding;
@ -4907,37 +4848,12 @@ var CanvasGraphics = (function() {
this.ctx.scale(cw / mediaBox.width, ch / mediaBox.height); this.ctx.scale(cw / mediaBox.width, ch / mediaBox.height);
}, },
preCompile: function(stream, xref, resources, fonts, images) { executeIRQueue: function(codeIR) {
var pe = this.pe = new PartialEvaluator();
return pe.evalRaw(stream, xref, resources, fonts, images);
},
postCompile: function(raw) {
if (!this.pe) {
this.pe = new PartialEvaluator();
}
return this.pe.evalFromRaw(raw);
},
execute: function(code, xref, resources) {
resources = xref.fetchIfRef(resources) || new Dict();
var savedXref = this.xref, savedRes = this.res, savedXobjs = this.xobjs;
this.xref = xref;
this.res = resources || new Dict();
this.xobjs = xref.fetchIfRef(this.res.get('XObject')) || new Dict();
code(this);
this.xobjs = savedXobjs;
this.res = savedRes;
this.xref = savedXref;
},
executeIR: function(codeIR) {
var argsArray = codeIR.argsArray; var argsArray = codeIR.argsArray;
var fnArray = codeIR.fnArray; var fnArray = codeIR.fnArray;
for (var i = 0, length = argsArray.length; i < length; i++) for (var i = 0, length = argsArray.length; i < length; i++) {
this[fnArray[i]].apply(this, argsArray[i]); this[fnArray[i]].apply(this, argsArray[i]);
}
}, },
endDrawing: function() { endDrawing: function() {
@ -5417,7 +5333,7 @@ var CanvasGraphics = (function() {
this.paintImageXObject(null, image, true); this.paintImageXObject(null, image, true);
}, },
paintFormXObject: function(raw, matrix, bbox) { paintFormXObject: function(IRQueue, matrix, bbox) {
this.save(); this.save();
if (matrix && IsArray(matrix) && 6 == matrix.length) if (matrix && IsArray(matrix) && 6 == matrix.length)
@ -5429,9 +5345,8 @@ var CanvasGraphics = (function() {
this.endPath(); this.endPath();
} }
var code = this.pe.evalFromRaw(raw)
// this.execute(code, this.xref, stream.dict.get('Resources')); // this.execute(code, this.xref, stream.dict.get('Resources'));
this.execute(code, this.xref, null); this.executeIRQueue(IRQueue);
this.restore(); this.restore();
}, },
@ -6210,7 +6125,7 @@ var TilingPatternIR = (function() {
function TilingPatternIR(IR, color, ctx) { function TilingPatternIR(IR, color, ctx) {
// "Unfolding" the IR. // "Unfolding" the IR.
var codeIR = IR[1]; var IRQueue = IR[1];
this.matrix = IR[2]; this.matrix = IR[2];
var bbox = IR[3]; var bbox = IR[3];
var xstep = IR[4]; var xstep = IR[4];
@ -6277,7 +6192,7 @@ var TilingPatternIR = (function() {
graphics.endPath(); graphics.endPath();
} }
graphics.executeIR(codeIR); graphics.executeIRQueue(IRQueue);
this.canvas = tmpCanvas; this.canvas = tmpCanvas;
} }

View File

@ -33,13 +33,13 @@ var WorkerPage = (function() {
this.workerPDF.startRendering(this) this.workerPDF.startRendering(this)
}, },
startRenderingFromPreCompilation: function(preCompilation, fonts, images) { startRenderingFromIRQueue: function(IRQueue, fonts, images) {
var gfx = new CanvasGraphics(this.ctx); var gfx = new CanvasGraphics(this.ctx);
// TODO: Add proper handling for images loaded by the worker. // TODO: Add proper handling for images loaded by the worker.
var images = new ImagesLoader(); var images = new ImagesLoader();
this.page.startRenderingFromPreCompilation(gfx, preCompilation, fonts, images, this.callback); this.page.startRenderingFromIRQueue(gfx, IRQueue, fonts, images, this.callback);
}, },
getLinks: function() { getLinks: function() {
@ -63,7 +63,7 @@ var WorkerPDFDoc = (function() {
this.pageCache = []; this.pageCache = [];
var useWorker = false; var useWorker = true;
if (useWorker) { if (useWorker) {
var worker = new Worker("../worker/boot.js"); var worker = new Worker("../worker/boot.js");
@ -97,7 +97,7 @@ var WorkerPDFDoc = (function() {
var imageLoadingDone = function() { var imageLoadingDone = function() {
var timeStart = new Date(); var timeStart = new Date();
console.log("startRenderingFromPreCompilation:", "numberOfFonts", fonts.length); console.log("startRenderingFromPreCompilation:", "numberOfFonts", fonts.length);
page.startRenderingFromPreCompilation(data.preCompilation, data.fonts, data.images); page.startRenderingFromIRQueue(data.IRQueue, data.fonts, data.images);
console.log("RenderingTime", (new Date()) - timeStart); console.log("RenderingTime", (new Date()) - timeStart);
} }

View File

@ -25,7 +25,7 @@ var WorkerHandler = {
var images = []; var images = [];
// Pre compile the pdf page and fetch the fonts/images. // Pre compile the pdf page and fetch the fonts/images.
var preCompilation = page.preCompile(gfx, fonts, images); var IRQueue = page.getIRQueue(fonts, images);
// Extract the minimum of font data that is required to build all required // Extract the minimum of font data that is required to build all required
// font stuff on the main thread. // font stuff on the main thread.
@ -47,7 +47,7 @@ var WorkerHandler = {
if (true /* show used commands */) { if (true /* show used commands */) {
var cmdMap = {}; var cmdMap = {};
var fnArray = preCompilation.fnArray; var fnArray = IRQueue .fnArray;
for (var i = 0; i < fnArray.length; i++) { for (var i = 0; i < fnArray.length; i++) {
var entry = fnArray[i]; var entry = fnArray[i];
if (entry == "paintReadyFormXObject") { if (entry == "paintReadyFormXObject") {
@ -73,10 +73,10 @@ var WorkerHandler = {
} }
handler.send("page", { handler.send("page", {
pageNum: pageNum, pageNum: pageNum,
fonts: fontsMin, fonts: fontsMin,
images: images, images: images,
preCompilation: preCompilation, IRQueue: IRQueue,
}); });
}, this); }, this);
} }