Cleanup font loading.
This commit is contained in:
parent
c8d7d88d0a
commit
889d6d100a
@ -556,7 +556,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
|
||||
this.current.leading = -leading;
|
||||
},
|
||||
setFont: function canvasGraphicsSetFont(fontRefName, size) {
|
||||
var fontObj = this.objs.get(fontRefName).fontObj;
|
||||
var fontObj = this.objs.get(fontRefName);
|
||||
var current = this.current;
|
||||
|
||||
if (!fontObj)
|
||||
|
28
src/core.js
28
src/core.js
@ -228,14 +228,13 @@ var Page = (function PageClosure() {
|
||||
}
|
||||
|
||||
// Load all the fonts
|
||||
var fontObjs = FontLoader.bind(
|
||||
FontLoader.bind(
|
||||
fonts,
|
||||
function pageEnsureFontsFontObjs(fontObjs) {
|
||||
this.stats.timeEnd('Font Loading');
|
||||
|
||||
callback.call(this);
|
||||
}.bind(this),
|
||||
this.objs
|
||||
}.bind(this)
|
||||
);
|
||||
},
|
||||
|
||||
@ -761,31 +760,16 @@ var PDFDoc = (function PDFDocClosure() {
|
||||
file = new Stream(file, 0, file.length, fontFileDict);
|
||||
}
|
||||
|
||||
// For now, resolve the font object here direclty. The real font
|
||||
// object is then created in FontLoader.bind().
|
||||
this.objs.resolve(id, {
|
||||
name: name,
|
||||
file: file,
|
||||
properties: properties
|
||||
});
|
||||
// At this point, only the font object is created but the font is not
|
||||
// yet attached to the DOM. This is done in `FontLoader.bind`.
|
||||
var font = new Font(name, file, properties);
|
||||
this.objs.resolve(id, font);
|
||||
break;
|
||||
default:
|
||||
error('Got unkown object type ' + type);
|
||||
}
|
||||
}, this);
|
||||
|
||||
messageHandler.on('font_ready', function pdfDocFontReady(data) {
|
||||
var id = data[0];
|
||||
var font = new FontShape(data[1]);
|
||||
|
||||
// If there is no string, then there is nothing to attach to the DOM.
|
||||
if (!font.str) {
|
||||
this.objs.resolve(id, font);
|
||||
} else {
|
||||
this.objs.setData(id, font);
|
||||
}
|
||||
}.bind(this));
|
||||
|
||||
messageHandler.on('page_error', function pdfDocError(data) {
|
||||
var page = this.pageCache[data.pageNum];
|
||||
if (page.displayReadyPromise)
|
||||
|
52
src/fonts.js
52
src/fonts.js
@ -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;
|
||||
|
||||
|
@ -155,55 +155,6 @@ var WorkerMessageHandler = {
|
||||
depFonts: Object.keys(fonts)
|
||||
});
|
||||
}, this);
|
||||
|
||||
handler.on('font', function wphSetupFont(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 objData = obj.data;
|
||||
if (objData) {
|
||||
var length = objData.length;
|
||||
for (var j = 0; j < length; ++j)
|
||||
str += String.fromCharCode(objData[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]);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user