pdf.js/worker/pdf.js

96 lines
2.4 KiB
JavaScript
Raw Normal View History

/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
2011-07-06 15:06:45 +09:00
'use strict';
2011-06-22 06:33:11 +09:00
var consoleTimer = {};
2011-06-22 06:33:11 +09:00
var console = {
log: function log() {
var args = Array.prototype.slice.call(arguments);
postMessage({
2011-07-06 15:06:45 +09:00
action: 'log',
data: args
});
},
2011-07-06 15:06:45 +09:00
time: function(name) {
consoleTimer[name] = Date.now();
},
2011-07-06 15:06:45 +09:00
timeEnd: function(name) {
var time = consoleTimer[name];
if (time == null) {
2011-07-06 15:06:45 +09:00
throw 'Unkown timer name ' + name;
}
2011-07-06 15:06:45 +09:00
this.log('Timer:', name, Date.now() - time);
}
2011-07-06 15:06:45 +09:00
};
2011-06-22 06:33:11 +09:00
//
2011-07-06 15:06:45 +09:00
importScripts('console.js');
importScripts('canvas.js');
importScripts('../pdf.js');
importScripts('../fonts.js');
importScripts('../crypto.js');
importScripts('../glyphlist.js');
2011-06-22 06:33:11 +09:00
// Use the JpegStreamProxy proxy.
JpegStream = JpegStreamProxy;
// Create the WebWorkerProxyCanvas.
2011-06-22 06:33:11 +09:00
var canvas = new CanvasProxy(1224, 1584);
// Listen for messages from the main thread.
var pdfDocument = null;
2011-06-22 06:33:11 +09:00
onmessage = function(event) {
var data = event.data;
// If there is no pdfDocument yet, then the sent data is the PDFDocument.
if (!pdfDocument) {
pdfDocument = new PDFDoc(new Stream(data));
postMessage({
2011-07-06 15:06:45 +09:00
action: 'pdf_num_pages',
data: pdfDocument.numPages
});
return;
}
// User requested to render a certain page.
else {
2011-07-06 15:06:45 +09:00
console.time('compile');
// Let's try to render the first page...
var page = pdfDocument.getPage(parseInt(data));
var pdfToCssUnitsCoef = 96.0 / 72.0;
var pageWidth = (page.mediaBox[2] - page.mediaBox[0]) * pdfToCssUnitsCoef;
var pageHeight = (page.mediaBox[3] - page.mediaBox[1]) * pdfToCssUnitsCoef;
postMessage({
2011-07-06 15:06:45 +09:00
action: 'setup_page',
data: pageWidth + ',' + pageHeight
});
// Set canvas size.
canvas.width = pageWidth;
canvas.height = pageHeight;
// page.compile will collect all fonts for us, once we have loaded them
// we can trigger the actual page rendering with page.display
var fonts = [];
2011-07-06 15:06:45 +09:00
var gfx = new CanvasGraphics(canvas.getContext('2d'), CanvasProxy);
page.compile(gfx, fonts);
2011-07-06 15:06:45 +09:00
console.timeEnd('compile');
2011-06-27 03:55:27 +09:00
// Send fonts to the main thread.
2011-07-06 15:06:45 +09:00
console.time('fonts');
2011-06-27 03:55:27 +09:00
postMessage({
2011-07-06 15:06:45 +09:00
action: 'fonts',
data: fonts
2011-06-27 03:55:27 +09:00
});
2011-07-06 15:06:45 +09:00
console.timeEnd('fonts');
console.time('display');
page.display(gfx);
canvas.flush();
2011-07-06 15:06:45 +09:00
console.timeEnd('display');
}
2011-07-06 15:06:45 +09:00
};