Fixing font loading issues
This commit is contained in:
parent
dd9aea21e9
commit
e15bfc00a0
63
fonts.js
63
fonts.js
@ -159,49 +159,73 @@ if (!isWorker) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var FontLoader = {
|
var FontLoader = {
|
||||||
fontLoadData: {},
|
|
||||||
fonts: {},
|
fonts: {},
|
||||||
|
fontsLoading: false,
|
||||||
|
waitingNames: [],
|
||||||
|
waitingStr: [],
|
||||||
|
|
||||||
bind: function(fonts, callback) {
|
bind: function(fonts, callback) {
|
||||||
console.log("requesting fonts", fonts[0].properties.loadedName, fonts[0].name);
|
console.log("requesting fonts", fonts[0].properties.loadedName, fonts[0].name);
|
||||||
|
|
||||||
var rules = [], names = [], objs = [];
|
var rules = [], names = [];
|
||||||
|
|
||||||
for (var i = 0; i < fonts.length; i++) {
|
for (var i = 0; i < fonts.length; i++) {
|
||||||
var font = fonts[i];
|
var font = fonts[i];
|
||||||
|
|
||||||
var obj = new Font(font.name, font.file, font.properties);
|
var obj = new Font(font.name, font.file, font.properties);
|
||||||
objs.push(obj);
|
|
||||||
|
|
||||||
var str = '';
|
var str = '';
|
||||||
var data = obj.data;
|
var data = obj.data;
|
||||||
|
var name = obj.loadedName;
|
||||||
if (data) {
|
if (data) {
|
||||||
var length = data.length;
|
var length = data.length;
|
||||||
for (var j = 0; j < length; j++)
|
for (var j = 0; j < length; j++)
|
||||||
str += String.fromCharCode(data[j]);
|
str += String.fromCharCode(data[j]);
|
||||||
|
|
||||||
var rule = isWorker ? obj.bindWorker(str) : obj.bindDOM(str);
|
|
||||||
if (rule) {
|
|
||||||
rules.push(rule);
|
|
||||||
names.push(obj.loadedName);
|
|
||||||
this.fonts[obj.loadedName] = obj;
|
this.fonts[obj.loadedName] = obj;
|
||||||
this.fontLoadData[obj.loadedName] = obj;
|
|
||||||
}
|
this.waitingNames.push(name);
|
||||||
|
this.waitingStr.push(str);
|
||||||
|
} else {
|
||||||
|
// If there is no data, then there is nothing to load and we can
|
||||||
|
// resolve the object right away.
|
||||||
|
Objects.resolve(name, obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rules.length) {
|
if (!this.fontsLoading) {
|
||||||
this.fontsLoading += rules.length;
|
this.executeWaiting();
|
||||||
FontLoader.prepareFontLoadEvent(rules, names);
|
} else {
|
||||||
|
console.log('There are currently some fonts getting loaded - waiting');
|
||||||
}
|
}
|
||||||
|
|
||||||
return objs;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
postFontLoadEvent: function(names) {
|
executeWaiting: function() {
|
||||||
|
var names = this.waitingNames;
|
||||||
|
console.log('executing fonts', names.join(', '));
|
||||||
|
|
||||||
|
var rules = [];
|
||||||
|
for (var i = 0; i < names.length; i++) {
|
||||||
|
var obj = this.fonts[names[i]];
|
||||||
|
var rule = obj.bindDOM(this.waitingStr[i]);
|
||||||
|
rules.push(rule);
|
||||||
|
}
|
||||||
|
this.prepareFontLoadEvent(rules, names);
|
||||||
|
this.waitingNames = [];
|
||||||
|
this.waitingStr = [];
|
||||||
|
},
|
||||||
|
|
||||||
|
fontLoadEvent: function(names) {
|
||||||
|
this.fontsLoading = false;
|
||||||
|
|
||||||
for (var i = 0; i < names.length; i++) {
|
for (var i = 0; i < names.length; i++) {
|
||||||
var name = names[i];
|
var name = names[i];
|
||||||
Objects.resolve(name, this.fontLoadData[name]);
|
Objects.resolve(name, this.fonts[name]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.waitingNames.length != 0) {
|
||||||
|
this.executeWaiting();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -210,7 +234,8 @@ var FontLoader = {
|
|||||||
// loaded in a subdocument. It's expected that the load of |rules|
|
// loaded in a subdocument. It's expected that the load of |rules|
|
||||||
// has already started in this (outer) document, so that they should
|
// has already started in this (outer) document, so that they should
|
||||||
// be ordered before the load in the subdocument.
|
// be ordered before the load in the subdocument.
|
||||||
prepareFontLoadEvent: function(rules, names, callback) {
|
prepareFontLoadEvent: function(rules, names) {
|
||||||
|
this.fontsLoading = true;
|
||||||
/** Hack begin */
|
/** Hack begin */
|
||||||
// There's no event when a font has finished downloading so the
|
// There's no event when a font has finished downloading so the
|
||||||
// following code is a dirty hack to 'guess' when a font is
|
// following code is a dirty hack to 'guess' when a font is
|
||||||
@ -261,7 +286,7 @@ var FontLoader = {
|
|||||||
}
|
}
|
||||||
src += ' var fontNames=[' + fontNamesArray + '];\n';
|
src += ' var fontNames=[' + fontNamesArray + '];\n';
|
||||||
src += ' window.onload = function () {\n';
|
src += ' window.onload = function () {\n';
|
||||||
src += ' parent.postMessage(JSON.stringify(fontNames), "*");\n';
|
src += ' setTimeout(function(){parent.postMessage(JSON.stringify(fontNames), "*")},0);\n';
|
||||||
src += ' }';
|
src += ' }';
|
||||||
src += '</script></head><body>';
|
src += '</script></head><body>';
|
||||||
for (var i = 0; i < names.length; ++i) {
|
for (var i = 0; i < names.length; ++i) {
|
||||||
@ -283,7 +308,7 @@ if (!isWorker) {
|
|||||||
window.addEventListener(
|
window.addEventListener(
|
||||||
'message',
|
'message',
|
||||||
function(e) {
|
function(e) {
|
||||||
FontLoader.postFontLoadEvent(JSON.parse(e.data));
|
FontLoader.fontLoadEvent(JSON.parse(e.data));
|
||||||
}.bind(this),
|
}.bind(this),
|
||||||
false);
|
false);
|
||||||
}
|
}
|
||||||
|
1
pdf.js
1
pdf.js
@ -5120,7 +5120,6 @@ var CanvasGraphics = (function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var name = fontObj.loadedName;
|
var name = fontObj.loadedName;
|
||||||
console.log("setFont", name);
|
|
||||||
if (!name) {
|
if (!name) {
|
||||||
// TODO: fontDescriptor is not available, fallback to default font
|
// TODO: fontDescriptor is not available, fallback to default font
|
||||||
name = 'sans-serif';
|
name = 'sans-serif';
|
||||||
|
Loading…
Reference in New Issue
Block a user