1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-26 10:08:06 +02:00

Cleanup font loading.

This commit is contained in:
Julian Viereck 2012-02-21 13:59:22 +01:00
parent c8d7d88d0a
commit 889d6d100a
4 changed files with 24 additions and 107 deletions

View file

@ -409,8 +409,8 @@ var FontLoader = {
bind: function fontLoaderBind(fonts, callback) {
function checkFontsLoaded() {
for (var i = 0, ii = objs.length; i < ii; i++) {
var fontObj = objs[i];
for (var i = 0, ii = fonts.length; i < ii; i++) {
var fontObj = fonts[i];
if (fontObj.loading) {
return false;
}
@ -423,52 +423,45 @@ var FontLoader = {
return true;
}
var rules = [], names = [], objs = [];
var rules = [], names = [], fontsToLoad = [];
var fontCreateTimer = 0;
for (var i = 0, ii = fonts.length; i < ii; i++) {
var font = fonts[i];
// If there is already a fontObj on the font, then it was loaded/attached
// to the page already and we don't have to do anything for this font
// here future.
if (font.fontObj) {
// Add the font to the DOM only once or skip if the font
// is already loaded.
if (font.attached || font.loading == false) {
continue;
}
font.attached = true;
var obj = new Font(font.name, font.file, font.properties);
// Store the fontObj on the font such that `setFont` in CanvasGraphics
// can reuse it later again.
font.fontObj = obj;
objs.push(obj);
fontsToLoad.push(font);
var str = '';
var data = obj.data;
var data = font.data;
if (data) {
var length = data.length;
for (var j = 0; j < length; j++)
str += String.fromCharCode(data[j]);
var rule = isWorker ? obj.bindWorker(str) : obj.bindDOM(str);
var rule = font.bindDOM(str);
if (rule) {
rules.push(rule);
names.push(obj.loadedName);
names.push(font.loadedName);
}
}
}
this.listeningForFontLoad = false;
if (!isWorker && rules.length) {
FontLoader.prepareFontLoadEvent(rules, names, objs);
FontLoader.prepareFontLoadEvent(rules, names, fontsToLoad);
}
if (!checkFontsLoaded()) {
document.documentElement.addEventListener(
'pdfjsFontLoad', checkFontsLoaded, false);
}
return objs;
},
// Set things up so that at least one pdfjsFontLoad event is
// dispatched when all the @font-face |rules| for |names| have been
@ -476,7 +469,7 @@ var FontLoader = {
// has already started in this (outer) document, so that they should
// be ordered before the load in the subdocument.
prepareFontLoadEvent: function fontLoaderPrepareFontLoadEvent(rules, names,
objs) {
fonts) {
/** Hack begin */
// There's no event when a font has finished downloading so the
// following code is a dirty hack to 'guess' when a font is
@ -517,8 +510,8 @@ var FontLoader = {
'message',
function fontLoaderMessage(e) {
var fontNames = JSON.parse(e.data);
for (var i = 0, ii = objs.length; i < ii; ++i) {
var font = objs[i];
for (var i = 0, ii = fonts.length; i < ii; ++i) {
var font = fonts[i];
font.loading = false;
}
var evt = document.createEvent('Events');
@ -862,7 +855,7 @@ var Font = (function FontClosure() {
this.widthMultiplier = !properties.fontMatrix ? 1.0 :
1.0 / properties.fontMatrix[0];
this.encoding = properties.baseEncoding;
this.loadedName = getUniqueName();
this.loadedName = properties.loadedName;
this.loading = true;
};
@ -2272,17 +2265,6 @@ var Font = (function FontClosure() {
}
},
bindWorker: function font_bindWorker(data) {
postMessage({
action: 'font',
data: {
raw: data,
fontName: this.loadedName,
mimetype: this.mimetype
}
});
},
bindDOM: function font_bindDom(data) {
var fontName = this.loadedName;