diff --git a/fonts.js b/fonts.js index 9782fc9a1..793ba99ef 100644 --- a/fonts.js +++ b/fonts.js @@ -124,30 +124,129 @@ var Fonts = (function () { })(); var FontLoader = { - bind: function(fonts) { - var ready = true; + bind: function(fonts, callback) { + function checkFontsLoaded() { + for (var i = 0; i < fonts.length; i++) { + var font = fonts[i]; + if (Fonts.lookup(font.name).loading) { + return false; + } + } + + document.documentElement.removeEventListener( + "pdfjsFontLoad", checkFontsLoaded, false); + + callback(); + return true; + } + var rules = [ ], names = [ ]; for (var i = 0; i < fonts.length; i++) { var font = fonts[i]; - if (Fonts.lookup(font.name)) { - ready = ready && !Fonts.lookup(font.name).loading; - continue; + if (!Fonts.lookup(font.name)) { + var obj = new Font(font.name, font.file, font.properties); + + var str = ""; + var data = Fonts.lookup(font.name).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); + if (rule) { + rules.push(rule); + names.push(font.name); + } } + } - ready = false; + if (!isWorker && rules.length) { + FontLoader.prepareFontLoadEvent(rules, names); + } - var obj = new Font(font.name, font.file, font.properties); + if (!checkFontsLoaded()) { + document.documentElement.addEventListener( + "pdfjsFontLoad", checkFontsLoaded, false); + } - var str = ""; - var data = Fonts.lookup(font.name).data; - var length = data.length; - for (var j = 0; j < length; j++) - str += String.fromCharCode(data[j]); + return; + }, + // Set things up so that at least one pdfjsFontLoad event is + // dispatched when all the @font-face |rules| for |names| have been + // loaded in a subdocument. It's expected that the load of |rules| + // has already started in this (outer) document, so that they should + // be ordered before the load in the subdocument. + prepareFontLoadEvent: function(rules, names) { + /** 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 + // ready. This code will be obsoleted by Mozilla bug 471915. + // + // The only reliable way to know if a font is loaded in Gecko + // (at the moment) is document.onload in a document with + // a @font-face rule defined in a "static" stylesheet. We use a + // subdocument in an