]> git.parisson.com Git - pdf.js.git/commitdiff
Ensure dependent font data is available before calling startRenderingIRQueue
authorJulian Viereck <julian.viereck@gmail.com>
Fri, 30 Sep 2011 13:38:54 +0000 (15:38 +0200)
committerJulian Viereck <julian.viereck@gmail.com>
Fri, 30 Sep 2011 13:38:54 +0000 (15:38 +0200)
pdf.js
worker.js
worker/processor_handler.js

diff --git a/pdf.js b/pdf.js
index df3150f936cfad01b1fe98e1851fd2ae0ea2ef69..77c57e52805220c728384b008e1a534c224a7d7d 100644 (file)
--- a/pdf.js
+++ b/pdf.js
@@ -3363,12 +3363,12 @@ var Page = (function() {
         });
       };
       
-      // this.ensureFonts(fonts, function() {
+      this.ensureFonts(fonts, function() {
         displayContinuation();
-      // });
+      });
     },
 
-    getIRQueue: function(handler) {
+    getIRQueue: function(handler, dependency) {
       if (this.IRQueue) {
         // content was compiled
         return this.IRQueue;
@@ -3387,7 +3387,7 @@ var Page = (function() {
       
       var pe = this.pe = new PartialEvaluator();
       var IRQueue = {};
-      return this.IRQueue = pe.getIRQueue(content, xref, resources, IRQueue, handler, "p" + this.pageNumber + "_");
+      return this.IRQueue = pe.getIRQueue(content, xref, resources, IRQueue, handler, "p" + this.pageNumber + "_", dependency);
     },
     
     ensureFonts: function(fonts, callback) {
@@ -4353,7 +4353,7 @@ var PartialEvaluator = (function() {
                 if (font.translated) {
                   // keep track of each font we translated so the caller can
                   // load them asynchronously before calling display on a page
-                  var loadedName = uniquePrefix + "font_" + (FontLoadedCounter++);
+                  var loadedName = "font_" + getIRQueue + + (FontLoadedCounter++);
                   font.translated.properties.loadedName = loadedName;
                   FontsMap[loadedName] = font;
 
index 8ffece46fddaa38235ea55dfc2364c876e82d112..8148ff3f1859c9cf402070c0b1db8dc1fcf82afb 100644 (file)
--- a/worker.js
+++ b/worker.js
@@ -120,6 +120,8 @@ var Promise = (function() {
   };
   
   Promise.prototype = {
+    hasData: false,
+
     set data(data) {
       if (data === undefined) {
         return;
@@ -128,6 +130,11 @@ var Promise = (function() {
         throw "Promise " + this.name + ": Cannot set the data of a promise twice";
       }
       this.$data = data;
+      this.hasData = true;
+
+      if (this.$onDataCallback) {
+        this.$onDataCallback(data);
+      }
     },
     
     get data() {
@@ -136,6 +143,14 @@ var Promise = (function() {
       }
       return this.$data;
     },
+
+    onData: function(callback) {
+      if (this.$data !== EMPTY_PROMISE) {
+        callback(this.$data);
+      } else {
+        this.$onDataCallback = callback;
+      }
+    },
     
     resolve: function(data) {
       if (this.isResolved) {
@@ -203,8 +218,26 @@ var WorkerPDFDoc = (function() {
     processorHandler.on("page", function(data) {
       var pageNum = data.pageNum;
       var page = this.pageCache[pageNum];
-      
-      page.startRenderingFromIRQueue(data.IRQueue, data.fonts);
+     
+      var depFonts = data.depFonts;
+
+      function checkFontData() {
+        // Check if all fontObjs have been processed. If not, shedule a
+        // callback that is called once the data arrives and that checks
+        // the next fonts.
+        for (var i = 0; i < depFonts.length; i++) {
+          var fontName = depFonts[i];
+          var fontObj = Objects.get(fontName);
+          if (!fontObj.hasData) {
+            fontObj.onData(checkFontData);
+          }
+        }
+
+        // At this point, all font data ia loaded. Start the actuall rendering.
+        page.startRenderingFromIRQueue(data.IRQueue, depFonts);
+      }
+
+      checkFontData();
     }, this);
 
     processorHandler.on("obj", function(data) {
index b7a915f3da9c88cca129866699579bb202dc3803..28c89db100bfb64c2a74746441e6fd57af173268 100644 (file)
@@ -21,8 +21,11 @@ var WorkerProcessorHandler = {
       var gfx = new CanvasGraphics(null);
 
       var start = Date.now();
+
+      var dependency = [];
+
       // Pre compile the pdf page and fetch the fonts/images.
-      var IRQueue = page.getIRQueue(handler);
+      var IRQueue = page.getIRQueue(handler, dependency);
 
       console.log("page=%d - getIRQueue: time=%dms, len=%d", pageNum, Date.now() - start, IRQueue.fnArray.length);
 
@@ -42,11 +45,22 @@ var WorkerProcessorHandler = {
           }
         }
         console.log("cmds", JSON.stringify(cmdMap));
-      } 
+      }
+
+      // Filter the dependecies for fonts.
+      var fonts = {};
+      for (var i = 0; i < dependency.length; i++) {
+        var dep = dependency[i];
+        if (dep.indexOf('font_') == 0) {
+          fonts[dep] = true;
+        }
+      }
+
 
       handler.send("page", {
         pageNum:  pageNum,
         IRQueue:  IRQueue,
+        depFonts: Object.keys(fonts)
       });
     }, this);
   }