pdf.js/worker/font.js

68 lines
1.7 KiB
JavaScript
Raw Normal View History

2011-09-13 02:37:33 +09:00
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2011-06-27 03:55:27 +09:00
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
2011-07-06 15:06:45 +09:00
'use strict';
2011-06-27 03:55:27 +09:00
2011-07-06 15:06:45 +09:00
importScripts('console.js');
2011-06-27 03:55:27 +09:00
2011-07-06 15:06:45 +09:00
importScripts('../pdf.js');
importScripts('../fonts.js');
importScripts('../glyphlist.js');
2011-06-27 03:55:27 +09:00
function fontDataToString(font) {
// Doing postMessage on objects make them lose their "shape". This adds the
// "shape" for all required objects agains, such that the encoding works as
// expected.
var fontFileDict = new Dict();
fontFileDict.map = font.file.dict.map;
2011-07-06 15:06:45 +09:00
var fontFile = new Stream(font.file.bytes, font.file.start,
font.file.end - font.file.start, fontFileDict);
2011-06-27 03:55:27 +09:00
font.file = new FlateStream(fontFile);
2011-07-06 15:06:45 +09:00
2011-06-27 03:55:27 +09:00
// This will encode the font.
var fontObj = new Font(font.name, font.file, font.properties);
// Create string that is used for css later.
2011-07-06 15:06:45 +09:00
var str = '';
2011-06-27 03:55:27 +09:00
var data = fontObj.data;
var length = data.length;
for (var j = 0; j < length; j++)
str += String.fromCharCode(data[j]);
2011-07-06 15:06:45 +09:00
2011-06-27 03:55:27 +09:00
return {
2011-07-06 15:06:45 +09:00
str: str,
2011-06-27 03:55:27 +09:00
encoding: font.properties.encoding
2011-07-06 15:06:45 +09:00
};
2011-06-27 03:55:27 +09:00
}
/**
* Functions to handle data sent by the MainThread.
*/
var actionHandler = {
2011-07-06 15:06:45 +09:00
'fonts': function(data) {
2011-06-27 03:55:27 +09:00
var fontData;
var result = {};
for (var i = 0; i < data.length; i++) {
fontData = data[i];
result[fontData.name] = fontDataToString(fontData);
}
2011-07-06 15:06:45 +09:00
2011-06-27 03:55:27 +09:00
postMessage({
2011-07-06 15:06:45 +09:00
action: 'fonts',
data: result
});
}
};
2011-06-27 03:55:27 +09:00
// Listen to the MainThread for data and call actionHandler on it.
this.onmessage = function(event) {
var data = event.data;
if (data.action in actionHandler) {
actionHandler[data.action].call(this, data.data);
} else {
2011-07-06 15:06:45 +09:00
throw 'Unkown action from worker: ' + data.action;
2011-06-27 03:55:27 +09:00
}
2011-07-06 15:06:45 +09:00
};
2011-09-16 05:32:44 +09:00