Start adding a FontLoader class to isolate the font-loaded hack
This commit is contained in:
parent
bc7e2b0110
commit
07ca9bfec5
53
fonts.js
53
fonts.js
@ -80,6 +80,35 @@ var Fonts = {
|
||||
}
|
||||
};
|
||||
|
||||
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).
|
||||
@ -113,13 +142,14 @@ var Font = (function () {
|
||||
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":
|
||||
@ -127,7 +157,7 @@ var Font = (function () {
|
||||
|
||||
// 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:
|
||||
@ -135,28 +165,12 @@ var Font = (function () {
|
||||
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) {
|
||||
@ -1420,6 +1434,7 @@ CFF.prototype = {
|
||||
i++;
|
||||
}
|
||||
error("failing with i = " + i + " in charstring:" + charstring + "(" + charstring.length + ")");
|
||||
return [];
|
||||
},
|
||||
|
||||
wrap: function wrap(name, charstrings, subrs, properties) {
|
||||
|
35
viewer.js
35
viewer.js
@ -3,7 +3,7 @@
|
||||
|
||||
"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;
|
||||
@ -52,7 +52,7 @@ function gotoPage(num) {
|
||||
}
|
||||
|
||||
function displayPage(num) {
|
||||
window.clearInterval(pageInterval);
|
||||
window.clearTimeout(pageTimeout);
|
||||
|
||||
document.getElementById("pageNumber").value = num;
|
||||
|
||||
@ -75,28 +75,12 @@ function displayPage(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);
|
||||
@ -106,12 +90,7 @@ function displayPage(num) {
|
||||
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() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user