From 519fb435c3a5d0d22afd21828593ceac9f8aea5a Mon Sep 17 00:00:00 2001
From: Julian Viereck <julian.viereck@gmail.com>
Date: Tue, 28 Jun 2011 09:50:53 +0200
Subject: [PATCH] Fonts.js: Add isLoadedCallback and add hack known from
 worker/client.js

---
 fonts.js | 33 +++++++++++++++++++++++----------
 1 file changed, 23 insertions(+), 10 deletions(-)

diff --git a/fonts.js b/fonts.js
index fbd9a0167..36dbc53d2 100644
--- a/fonts.js
+++ b/fonts.js
@@ -779,9 +779,18 @@ var Font = (function () {
       });
     },
 
-    bindDOM: function font_bindDom(data) {
+    bindDOM: function font_bindDom(data, callback) {
       var fontName = this.name;
 
+      // Just adding the font-face to the DOM doesn't make it load. It
+      // seems it's loaded once Gecko notices it's used. Therefore,
+      // add a div on the page using the loaded font.
+      var div = document.createElement("div");
+      var style = 'font-family:"' + name + 
+        '";position: absolute;top:-99999;left:-99999;z-index:-99999';
+      div.setAttribute("style", style);
+      document.body.appendChild(div);
+
       /** Hack begin */
       // Actually there is not event when a font has finished downloading so
       // the following code are a dirty hack to 'guess' when a font is ready
@@ -801,15 +810,19 @@ var Font = (function () {
 
         // For some reasons the font has not loaded, so mark it loaded for the
         // page to proceed but cry
-        if ((Date.now() - this.start) >= kMaxWaitForFontFace) {
-          window.clearInterval(interval);
-          Fonts[fontName].loading = false;
-          warn("Is " + fontName + " loaded?");
-          this.start = 0;
-        } else if (textWidth != ctx.measureText(testString).width) {
-          window.clearInterval(interval);
-          Fonts[fontName].loading = false;
-          this.start = 0;
+        if (textWidth == ctx.measureText(testString).width) {
+          if ((Date.now() - this.start) < kMaxWaitForFontFace) {
+            return;
+          } else {
+            warn("Is " + fontName + " loaded?");
+          }
+        }
+        
+        window.clearInterval(interval);
+        Fonts[fontName].loading = false;
+        this.start = 0;
+        if (callback) {
+          callback();
         }
       }, 30, this);