diff --git a/README.md b/README.md index b6ff6c19f..0d439b139 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,14 @@ yet to integrate it into Firefox. We will explore that possibility once pdf.js is production ready. Until then we aim to publish a Firefox PDF reader extension powered by pdf.js. +Our demo site is here: + + http://andreasgal.github.com/pdf.js/ + You can read more about pdf.js here: http://andreasgal.com/2011/06/15/pdf-js/ + http://blog.mozilla.com/cjones/2011/06/15/overview-of-pdf-js-guts/ follow us on twitter: @pdfjs diff --git a/fonts.js b/fonts.js index 3db99daf3..12d7ce82e 100644 --- a/fonts.js +++ b/fonts.js @@ -36,13 +36,13 @@ var kDisableFonts = false; * http://cgit.freedesktop.org/poppler/poppler/tree/poppler/GfxFont.cc#n65 */ -var Fonts = (function () { +var Fonts = (function Fonts() { var kScalePrecision = 40; - var fonts = Object.create(null); + var fonts = Object.create(null); if (!isWorker) { var ctx = document.createElement("canvas").getContext("2d"); - ctx.scale(1 / kScalePrecision, 1); + ctx.scale(1 / kScalePrecision, 1); } function Font(name, data, properties) { @@ -124,30 +124,148 @@ var Fonts = (function () { })(); var FontLoader = { - bind: function(fonts) { - var ready = true; + listeningForFontLoad: false, - 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; + 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; + } } - ready = false; + document.documentElement.removeEventListener( + "pdfjsFontLoad", checkFontsLoaded, false); - 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]); - - isWorker ? obj.bindWorker(str) : obj.bindDOM(str); + callback(); + return true; } - return ready; + var rules = [ ], names = [ ]; + for (var i = 0; i < fonts.length; i++) { + var font = fonts[i]; + 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); + } + } + } + + if (!isWorker && rules.length) { + FontLoader.prepareFontLoadEvent(rules, names); + } + + if (!checkFontsLoaded()) { + document.documentElement.addEventListener( + "pdfjsFontLoad", checkFontsLoaded, false); + } + + 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