From 5ecbc64d8d4c0c232556afc5cf802e1d54d7f546 Mon Sep 17 00:00:00 2001 From: Julian Viereck Date: Thu, 15 Sep 2011 22:46:44 -0700 Subject: [PATCH] Backup --- fonts.js | 245 ++++++++++++++++++++++++++++++++++-------------------- worker.js | 5 +- 2 files changed, 158 insertions(+), 92 deletions(-) diff --git a/fonts.js b/fonts.js index 834064b46..0366b2602 100755 --- a/fonts.js +++ b/fonts.js @@ -182,86 +182,9 @@ if (!isWorker) { * unicode characters. Therefore, the test string have to be build using the * encoding of the fontObj. */ -var FontLoader = { - scratchCtx: null, - loading: {}, - - /** - * Create the canvas used for measuring the width of text. - */ - setup: function() { - var canvas = document.createElement("canvas"); - var ctx = canvas.getContext("2d"); - this.ctx = ctx; - }, - - /** - * Measures the width of some string using a fontObj and some different - * fallback fonts. - */ - measure: function(fontObj, str) { - var ctx = this.ctx; - - // THe fonts used as fallback. - var fallbacks = [ "Arial", "Courier" ]; - - var widths = []; - for (var n = 0; n < fallbacks.length; n++) { - // Choose a large font size as there are no sub-pixel returned from - // measureText. - var font = fontObj.getRule(420, fallbacks[n]); - ctx.font = font; - - widths.push(ctx.measureText(str).width); - } - return widths; - }, - - /** - * Attaches a fontObj to the DOM and calls Objects.resolve(objId) once - * the font is loaded. - */ - bind: function(objId, fontObj) { - this.loading[objId] = true; - var encoding = fontObj.encoding; - - // If the font has an encoding, build the test string based on it. If the - // font doesn't have an encoding, the font can't been used right now and - // we skip here. - if (fontObj.supported) { - var testStr = ""; - for (var enc in encoding) { - testStr += String.fromCharCode(encoding[enc].unicode); - } - } else { - // This font isn't fully supported yet. Resolve the object such that - // the execution continues but do nothing else. - Objects.resolve(objId); - return; - } + var FontLoader = { + listeningForFontLoad: false, - var before = this.measure(fontObj, testStr); - this.bindDOM(fontObj); - - var check = function() { - var measure = this.measure(fontObj, testStr); - - for (var i = 0; i < measure.length; i++) { - if (measure[i] !== before[i]) { - console.log("loaded font", objId); - delete this.loading[objId]; - Objects.resolve(objId); - return; - } - } - - setTimeout(check, 0); - }.bind(this); - - // Start checking if font is loaded. - check(); - }, - /** * Attach the fontObj to the DOM. */ @@ -285,19 +208,159 @@ var FontLoader = { var styleSheet = document.styleSheets[0]; styleSheet.insertRule(rule, styleSheet.cssRules.length); return rule; - } -}; + }, -if (!isWorker) { - FontLoader.setup(); - - window.addEventListener( - 'message', - function(e) { - FontLoader.fontLoadEvent(JSON.parse(e.data)); - }.bind(this), - false); -} + bind: function(fonts, callback) { + function checkFontsLoaded() { + for (var i = 0; i < objs.length; i++) { + var fontObj = objs[i]; + if (fontObj.loading) { + return false; + } + } + + document.documentElement.removeEventListener( + 'pdfjsFontLoad', checkFontsLoaded, false); + + callback(); + return true; + } + + var rules = [], names = [], objs = []; + + for (var i = 0; i < fonts.length; i++) { + var font = fonts[i]; + var obj = font; + + objs.push(obj); + + var str = ''; + var rule = this.bindDOM(font); + if (rule) { + rules.push(rule); + names.push(obj.loadedName); + } + } + + // console.log("bind", fonts, rules, names); + + this.listeningForFontLoad = false; + if (!isWorker && rules.length) { + FontLoader.prepareFontLoadEvent(rules, names, objs); + } + + 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 + // 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, objs) { + /** 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