From 391c26b407524e9e2ad508b512f30a2582e51fad Mon Sep 17 00:00:00 2001
From: Julian Viereck <julian.viereck@gmail.com>
Date: Thu, 30 Jun 2011 14:25:57 +0200
Subject: [PATCH] Make worker work again after latest font changes

---
 fonts.js         | 14 +++++++++-----
 worker/client.js | 25 ++++++++-----------------
 2 files changed, 17 insertions(+), 22 deletions(-)

diff --git a/fonts.js b/fonts.js
index fdc5949a5..9782fc9a1 100644
--- a/fonts.js
+++ b/fonts.js
@@ -3,6 +3,8 @@
 
 "use strict";
 
+var isWorker = (typeof window == "undefined");
+
 /**
  * Maximum file size of the font.
  */
@@ -36,9 +38,12 @@ var kDisableFonts = false;
 
 var Fonts = (function () {
   var kScalePrecision = 40;
-  var fonts = Object.create(null);
-  var ctx = document.createElement("canvas").getContext("2d");
-  ctx.scale(1 / kScalePrecision, 1);
+  var fonts = Object.create(null);  
+
+  if (!isWorker) {
+    var ctx = document.createElement("canvas").getContext("2d");
+    ctx.scale(1 / kScalePrecision, 1);    
+  }
 
   function Font(name, data, properties) {
     this.name = name;
@@ -120,7 +125,6 @@ var Fonts = (function () {
 
 var FontLoader = {
   bind: function(fonts) {
-    var worker = (typeof window == "undefined");
     var ready = true;
 
     for (var i = 0; i < fonts.length; i++) {
@@ -140,7 +144,7 @@ var FontLoader = {
       for (var j = 0; j < length; j++)
         str += String.fromCharCode(data[j]);
 
-      worker ? obj.bindWorker(str) : obj.bindDOM(str);
+      isWorker ? obj.bindWorker(str) : obj.bindDOM(str);
     }
 
     return ready;
diff --git a/worker/client.js b/worker/client.js
index 73fb12f09..fd98b857e 100644
--- a/worker/client.js
+++ b/worker/client.js
@@ -59,14 +59,15 @@ FontWorker.prototype = {
     "fonts": function(data) {
       // console.log("got processed fonts from worker", Object.keys(data));
       for (name in data) {
-        // Update the 
-        Fonts[name].properties = {
+        // Update the encoding property.
+        var font = Fonts.lookup(name);
+        font.properties = {
           encoding: data[name].encoding
         }
-        
+
         // Call `Font.prototype.bindDOM` to make the font get loaded on the page.
         Font.prototype.bindDOM.call(
-          Fonts[name],
+          font,
           data[name].str,
           // IsLoadedCallback.
           this.$handleFontLoadedCallback
@@ -84,19 +85,9 @@ FontWorker.prototype = {
         continue;
       }
     
-      // Store only the data on Fonts that is needed later on, such that we
-      // hold track on as lease memory as possible.
-      Fonts[font.name] = {
-        name:       font.name,
-        mimetype:   font.mimetype,
-        // This is set later on the worker replay. For some fonts, the encoding
-        // is calculated during the conversion process happening on the worker
-        // and therefore is not available right now.
-        // properties: {
-        //   encoding:  font.properties.encoding
-        // },
-        cache:      Object.create(null)
-      };
+      // Register the font but don't pass in any real data. The idea is to
+      // store as less data as possible to reduce memory usage.
+      Fonts.registerFont(font.name, Object.create(null), Object.create(null));
 
       // Mark this font to be handled later.
       notLoaded.push(font);