}
};
+var FontsLoader = {
+ bind: function(fonts) {
+ var worker = (typeof window == "undefined");
+ var ready = true;
+
+ for (var i = 0; i < fonts.length; i++) {
+ var font = fonts[i];
+ if (Fonts[font.name]) {
+ ready = ready && !Fonts[font.name].loading;
+ continue;
+ } else {
+ ready = false;
+ }
+
+ var obj = new Font(font.name, font.file, font.properties);
+
+ var str = "";
+ var data = Fonts[font.name].data;
+ var length = data.length;
+ for (var j = 0; j < length; j++)
+ str += String.fromCharCode(data[j]);
+
+ worker ? obj.bindWorker(str) : obj.bindDOM(str);
+ }
+ return ready;
+ }
+};
+
+
/**
* 'Font' is the class the outside world should use, it encapsulate all the font
* decoding logics whatever type it is (assuming the font type is supported).
return;
}
+ var data;
switch (properties.type) {
case "Type1":
var cff = new CFF(name, file, properties);
this.mimetype = "font/opentype";
// Wrap the CFF data inside an OTF font file
- this.font = this.convert(name, cff, properties);
+ data = this.convert(name, cff, properties);
break;
case "TrueType":
// Repair the TrueType file if it is can be damaged in the point of
// view of the sanitizer
- this.font = this.checkAndRepair(name, file, properties);
+ data = this.checkAndRepair(name, file, properties);
break;
default:
break;
}
- var data = this.font;
Fonts[name] = {
data: data,
properties: properties,
loading: true,
cache: Object.create(null)
- }
-
- // Convert data to a string.
- var dataStr = "";
- var length = data.length;
- for (var i = 0; i < length; ++i)
- dataStr += String.fromCharCode(data[i]);
-
- // Attach the font to the document. If this script is runnig in a worker,
- // call `bindWorker`, which sends stuff over to the main thread.
- if (typeof window != "undefined") {
- this.bindDOM(dataStr);
- } else {
- this.bindWorker(dataStr);
- }
-
+ };
};
function stringToArray(str) {
i++;
}
error("failing with i = " + i + " in charstring:" + charstring + "(" + charstring.length + ")");
+ return [];
},
wrap: function wrap(name, charstrings, subrs, properties) {
"use strict";
-var pdfDocument, canvas, pageDisplay, pageNum, numPages, pageInterval;
+var pdfDocument, canvas, pageDisplay, pageNum, numPages, pageTimeout;
function load(userInput) {
canvas = document.getElementById("canvas");
canvas.mozOpaque = true;
}
function displayPage(num) {
- window.clearInterval(pageInterval);
+ window.clearTimeout(pageTimeout);
document.getElementById("pageNumber").value = num;
page.compile(gfx, fonts);
var t2 = Date.now();
- var fontsReady = true;
-
- // Inspect fonts and translate the missing one
- var count = fonts.length;
- for (var i = 0; i < count; i++) {
- var font = fonts[i];
- if (Fonts[font.name]) {
- fontsReady = fontsReady && !Fonts[font.name].loading;
- continue;
+ function loadFont() {
+ if (!FontsLoader.bind(fonts)) {
+ pageTimeout = window.setTimeout(loadFont, 10);
+ return;
}
- new Font(font.name, font.file, font.properties);
- fontsReady = false;
- }
-
- function delayLoadFont() {
- for (var i = 0; i < count; i++) {
- if (Fonts[font.name].loading)
- return;
- }
- window.clearInterval(pageInterval);
-
var t3 = Date.now();
page.display(gfx);
var infoDisplay = document.getElementById("info");
infoDisplay.innerHTML = "Time to load/compile/fonts/render: "+ (t1 - t0) + "/" + (t2 - t1) + "/" + (t3 - t2) + "/" + (t4 - t3) + " ms";
};
-
- if (fontsReady) {
- delayLoadFont();
- } else {
- pageInterval = setInterval(delayLoadFont, 10);
- }
+ loadFont();
}
function nextPage() {