]> git.parisson.com Git - pdf.js.git/commitdiff
Start adding a FontLoader class to isolate the font-loaded hack
authorVivien Nicolas <21@vingtetun.org>
Fri, 24 Jun 2011 09:47:22 +0000 (11:47 +0200)
committerVivien Nicolas <21@vingtetun.org>
Fri, 24 Jun 2011 09:47:22 +0000 (11:47 +0200)
fonts.js
viewer.js

index a995c55ebed96532d8251b32e2397d9049620369..0a1974571e469e88849fffea72f9b5665ddfb2fc 100644 (file)
--- a/fonts.js
+++ b/fonts.js
@@ -80,6 +80,35 @@ var Fonts = {
   }
 };
 
+var FontsLoader = {
+  bind: function(fonts) {
+    var worker = (typeof window == "undefined");
+    var ready = true;
+
+    for (var i = 0; i < fonts.length; i++) {
+      var font = fonts[i];
+      if (Fonts[font.name]) {
+        ready = ready && !Fonts[font.name].loading;
+        continue;
+      } else {
+        ready = false;
+      }
+
+      var obj = new Font(font.name, font.file, font.properties);
+
+      var str = "";
+      var data = Fonts[font.name].data;
+      var length = data.length;
+      for (var j = 0; j < length; j++)
+        str += String.fromCharCode(data[j]);
+
+      worker ? obj.bindWorker(str) : obj.bindDOM(str);
+    }
+    return ready;
+  }
+};
+
+
 /**
  * 'Font' is the class the outside world should use, it encapsulate all the font
  * decoding logics whatever type it is (assuming the font type is supported).
@@ -113,13 +142,14 @@ var Font = (function () {
       return;
     }
 
+    var data;
     switch (properties.type) {
       case "Type1":
         var cff = new CFF(name, file, properties);
         this.mimetype = "font/opentype";
 
         // Wrap the CFF data inside an OTF font file
-        this.font = this.convert(name, cff, properties);
+        data = this.convert(name, cff, properties);
         break;
 
       case "TrueType":
@@ -127,7 +157,7 @@ var Font = (function () {
 
         // Repair the TrueType file if it is can be damaged in the point of
         // view of the sanitizer
-        this.font = this.checkAndRepair(name, file, properties);
+        data = this.checkAndRepair(name, file, properties);
         break;
 
       default:
@@ -135,28 +165,12 @@ var Font = (function () {
         break;
     }
 
-    var data = this.font;
     Fonts[name] = {
       data: data,
       properties: properties,
       loading: true,
       cache: Object.create(null)
-    }
-
-    // Convert data to a string.
-    var dataStr = "";
-    var length = data.length;
-    for (var i = 0; i < length; ++i)
-      dataStr += String.fromCharCode(data[i]);
-
-    // Attach the font to the document. If this script is runnig in a worker,
-    // call `bindWorker`, which sends stuff over to the main thread.
-    if (typeof window != "undefined") {
-      this.bindDOM(dataStr);
-    } else {
-      this.bindWorker(dataStr);
-    }
-
+    };
   };
 
   function stringToArray(str) {
@@ -1420,6 +1434,7 @@ CFF.prototype = {
       i++;
     }
     error("failing with i = " + i + " in charstring:" + charstring + "(" + charstring.length + ")");
+    return [];
   },
 
   wrap: function wrap(name, charstrings, subrs, properties) {
index 41aaf354ca79db922c897a0e831f05013ffb78f2..2bcff50a6c15c4c2b0fafddda427b47311eaf3b7 100644 (file)
--- a/viewer.js
+++ b/viewer.js
@@ -3,7 +3,7 @@
 
 "use strict";
 
-var pdfDocument, canvas, pageDisplay, pageNum, numPages, pageInterval;
+var pdfDocument, canvas, pageDisplay, pageNum, numPages, pageTimeout;
 function load(userInput) {
     canvas = document.getElementById("canvas");
     canvas.mozOpaque = true;
@@ -52,7 +52,7 @@ function gotoPage(num) {
 }
 
 function displayPage(num) {
-    window.clearInterval(pageInterval);
+    window.clearTimeout(pageTimeout);
 
     document.getElementById("pageNumber").value = num;
 
@@ -75,28 +75,12 @@ function displayPage(num) {
     page.compile(gfx, fonts);
     var t2 = Date.now();
 
-    var fontsReady = true;
-
-    // Inspect fonts and translate the missing one
-    var count = fonts.length;
-    for (var i = 0; i < count; i++) {
-      var font = fonts[i];
-      if (Fonts[font.name]) {
-        fontsReady = fontsReady && !Fonts[font.name].loading;
-        continue;
+    function loadFont() {
+      if (!FontsLoader.bind(fonts)) {
+        pageTimeout = window.setTimeout(loadFont, 10);
+        return;
       }
 
-      new Font(font.name, font.file, font.properties);
-      fontsReady = false;
-    }
-
-    function delayLoadFont() {
-      for (var i = 0; i < count; i++) {
-        if (Fonts[font.name].loading)
-          return;
-      }
-      window.clearInterval(pageInterval);
-
       var t3 = Date.now();
 
       page.display(gfx);
@@ -106,12 +90,7 @@ function displayPage(num) {
       var infoDisplay = document.getElementById("info");
       infoDisplay.innerHTML = "Time to load/compile/fonts/render: "+ (t1 - t0) + "/" + (t2 - t1) + "/" + (t3 - t2) + "/" + (t4 - t3) + " ms";
     };
-
-    if (fontsReady) {
-      delayLoadFont();
-    } else {
-      pageInterval = setInterval(delayLoadFont, 10);
-    }
+    loadFont();
 }
 
 function nextPage() {