Rename some stuff and add missing font_handler.js

This commit is contained in:
Julian Viereck 2011-09-16 09:04:12 -07:00
parent 921f8bd669
commit 00290b9ff4
7 changed files with 94 additions and 19 deletions

View File

@ -10,7 +10,8 @@
<script type="text/javascript" src="driver.js"></script> <script type="text/javascript" src="driver.js"></script>
<script type="text/javascript" src="../worker.js"></script> <script type="text/javascript" src="../worker.js"></script>
<script type="text/javascript" src="../worker/message_handler.js"></script> <script type="text/javascript" src="../worker/message_handler.js"></script>
<script type="text/javascript" src="../worker/handler.js"></script> <script type="text/javascript" src="../worker/processor_handler.js"></script>
<script type="text/javascript" src="../worker/font_handler.js"></script>
</head> </head>
<body onload="load();"> <body onload="load();">

View File

@ -13,7 +13,8 @@
<script type="text/javascript" src="../metrics.js"></script> <script type="text/javascript" src="../metrics.js"></script>
<script type="text/javascript" src="../worker.js"></script> <script type="text/javascript" src="../worker.js"></script>
<script type="text/javascript" src="../worker/message_handler.js"></script> <script type="text/javascript" src="../worker/message_handler.js"></script>
<script type="text/javascript" src="../worker/handler.js"></script> <script type="text/javascript" src="../worker/processor_handler.js"></script>
<script type="text/javascript" src="../worker/font_handler.js"></script>
</head> </head>
<body> <body>

View File

@ -3,6 +3,9 @@
'use strict'; 'use strict';
// Set this to true if you want to use workers.
var useWorker = false;
var WorkerPage = (function() { var WorkerPage = (function() {
function constructor(workerPDF, page) { function constructor(workerPDF, page) {
this.workerPDF = workerPDF; this.workerPDF = workerPDF;
@ -179,10 +182,9 @@ var WorkerPDFDoc = (function() {
this.pageCache = []; this.pageCache = [];
var useWorker = true;
if (useWorker) { if (useWorker) {
var worker = this.worker = new Worker("../worker/boot_processor.js"); var worker = this.worker = new Worker("../worker/processor_boot.js");
var fontWorker = this.fontWorker = new Worker('../worker/font_boot.js');
} else { } else {
// If we don't use a worker, just post/sendMessage to the main thread. // If we don't use a worker, just post/sendMessage to the main thread.
var worker = { var worker = {
@ -190,21 +192,22 @@ var WorkerPDFDoc = (function() {
worker.onmessage({data: obj}); worker.onmessage({data: obj});
} }
} }
var fontWorker = {
postMessage: function(obj) {
fontWorker.onmessage({data: obj});
}
}
} }
var fontWorker = new Worker('../worker/boot_font.js'); var processorHandler = this.processorHandler = new MessageHandler("main", worker);
var fontHandler = this.fontHandler = new MessageHandler('font', fontWorker); processorHandler.on("page", function(data) {
var handler = this.handler = new MessageHandler("main", worker);
handler.on("page", function(data) {
var pageNum = data.pageNum; var pageNum = data.pageNum;
var page = this.pageCache[pageNum]; var page = this.pageCache[pageNum];
page.startRenderingFromIRQueue(data.IRQueue, data.fonts); page.startRenderingFromIRQueue(data.IRQueue, data.fonts);
}, this); }, this);
handler.on("obj", function(data) { processorHandler.on("obj", function(data) {
var objId = data[0]; var objId = data[0];
var objType = data[1]; var objType = data[1];
@ -225,6 +228,7 @@ var WorkerPDFDoc = (function() {
} }
}, this); }, this);
var fontHandler = this.fontHandler = new MessageHandler('font', fontWorker);
fontHandler.on('font_ready', function(data) { fontHandler.on('font_ready', function(data) {
var objId = data[0]; var objId = data[0];
var fontObj = new FontShape(data[1]); var fontObj = new FontShape(data[1]);
@ -240,10 +244,11 @@ var WorkerPDFDoc = (function() {
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
// the main thread sends to it self. // the main thread sends to it self.
WorkerHandler.setup(handler); WorkerProcessorHandler.setup(processorHandler);
WorkerFontHandler.setup(fontHandler);
} }
handler.send("doc", data); processorHandler.send("doc", data);
} }
constructor.prototype = { constructor.prototype = {
@ -252,7 +257,7 @@ var WorkerPDFDoc = (function() {
}, },
startRendering: function(page) { startRendering: function(page) {
this.handler.send("page_request", page.page.pageNumber + 1); this.processorHandler.send("page_request", page.page.pageNumber + 1);
}, },
getPage: function(n) { getPage: function(n) {

16
worker/font_boot.js Normal file
View File

@ -0,0 +1,16 @@
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
'use strict';
importScripts('console.js');
importScripts('message_handler.js');
importScripts('../pdf.js');
importScripts('../fonts.js');
importScripts('../crypto.js');
importScripts('../glyphlist.js');
importScripts('font_handler.js');
var handler = new MessageHandler("worker_font", this);
WorkerFontHandler.setup(handler);

52
worker/font_handler.js Normal file
View File

@ -0,0 +1,52 @@
var WorkerFontHandler = {
setup: function(handler) {
handler.on("font", function(data) {
var objId = data[0];
var name = data[1];
var file = data[2];
var properties = data[3];
var font = {
name: name,
file: file,
properties: properties
};
// Some fonts don't have a file, e.g. the build in ones like Arial.
if (file) {
var fontFileDict = new Dict();
fontFileDict.map = file.dict.map;
var fontFile = new Stream(file.bytes, file.start,
file.end - file.start, fontFileDict);
// Check if this is a FlateStream. Otherwise just use the created
// Stream one. This makes complex_ttf_font.pdf work.
var cmf = file.bytes[0];
if ((cmf & 0x0f) == 0x08) {
font.file = new FlateStream(fontFile);
} else {
font.file = fontFile;
}
}
var obj = new Font(font.name, font.file, font.properties);
var str = '';
var data = obj.data;
if (data) {
var length = data.length;
for (var j = 0; j < length; j++)
str += String.fromCharCode(data[j]);
}
obj.str = str;
// Remove the data array form the font object, as it's not needed
// anymore as we sent over the ready str.
delete obj.data;
handler.send("font_ready", [objId, obj]);
});
}
}

View File

@ -10,10 +10,10 @@ importScripts('../fonts.js');
importScripts('../crypto.js'); importScripts('../crypto.js');
importScripts('../glyphlist.js'); importScripts('../glyphlist.js');
importScripts('../metrics.js'); importScripts('../metrics.js');
importScripts('handler.js'); importScripts('processor_handler.js');
// Listen for messages from the main thread. // Listen for messages from the main thread.
var pdfDoc = null; var pdfDoc = null;
var handler = new MessageHandler("worker", this); var handler = new MessageHandler("worker_processor", this);
WorkerHandler.setup(handler); WorkerProcessorHandler.setup(handler);

View File

@ -3,7 +3,7 @@
'use strict'; 'use strict';
var WorkerHandler = { var WorkerProcessorHandler = {
setup: function(handler) { setup: function(handler) {
var pdfDoc = null; var pdfDoc = null;