]> git.parisson.com Git - pdf.js.git/commitdiff
Merge font and processor working into one worker and fix some bugs in font-loading...
authorJulian Viereck <julian.viereck@gmail.com>
Wed, 5 Oct 2011 20:24:38 +0000 (22:24 +0200)
committerJulian Viereck <julian.viereck@gmail.com>
Wed, 5 Oct 2011 20:24:38 +0000 (22:24 +0200)
fonts.js
pdf.js
web/viewer.html
worker.js
worker/font_boot.js [deleted file]
worker/font_handler.js [deleted file]
worker/pdf_worker_loader.js [new file with mode: 0644]
worker/processor_boot.js [deleted file]
worker/processor_handler.js

index 32f879563767af7813bea51089e4ebf0fccff104..974c8e79a5a08dfecfac06d7ed2d591b77fedbfa 100755 (executable)
--- a/fonts.js
+++ b/fonts.js
@@ -211,6 +211,17 @@ var FontLoader = {
   },
 
   bind: function fontLoaderBind(fonts, callback, objects) {
+    var fontsToLoad = {};
+    // check if there are twice the same font.
+    for (var i = 0; i < fonts.length; i++) {
+      var fontName = fonts[i].loadedName;
+      if (fontsToLoad[fontName]) {
+        throw "Got twice the same font!";
+      } else {
+        fontsToLoad[fontName] = true;
+      }
+    }
+    
     function checkFontsLoaded() {
       for (var i = 0; i < objs.length; i++) {
         var fontObj = objs[i];
diff --git a/pdf.js b/pdf.js
index c0fa98d5fc4c9de93f115b521d0c9f3e2770b90b..d7088e67c3a86de059970c37030064d70005b6f0 100644 (file)
--- a/pdf.js
+++ b/pdf.js
@@ -3387,7 +3387,7 @@ var Page = (function() {
     },
     
     ensureFonts: function(fonts, callback) {
-      console.log('--ensureFonts--');
+      console.log('--ensureFonts--', '' + fonts);
       // Convert the font names to the corresponding font obj.
       for (var i = 0; i < fonts.length; i++) {
         // HACK FOR NOW. Access the data directly. This isn't allowed as the
index 33026a49256aa870ac0a0178a91776bd374e0d80..6168710e43da4343471404f3eab19cab6ef8ca79 100644 (file)
@@ -14,7 +14,6 @@
         <script type="text/javascript" src="../worker.js"></script>
         <script type="text/javascript" src="../worker/message_handler.js"></script>
         <script type="text/javascript" src="../worker/processor_handler.js"></script>
-        <script type="text/javascript" src="../worker/font_handler.js"></script>
   </head>
 
   <body>
index a89ffb7a7004ecb1ed820f383c5ad41f4bc6e1ab..0f686b42bc9be362fd418bdebcd29623a250f8e9 100644 (file)
--- a/worker.js
+++ b/worker.js
@@ -264,8 +264,7 @@ var WorkerPDFDoc = (function() {
     this.pageCache = [];
     
     if (useWorker) {
-      var worker = this.worker = new Worker("../worker/processor_boot.js");
-      var fontWorker = this.fontWorker = new Worker('../worker/font_boot.js');
+      var worker = this.worker = new Worker("../worker/pdf_worker_loader.js");
     } else {
       // If we don't use a worker, just post/sendMessage to the main thread.
       var worker = {
@@ -273,12 +272,9 @@ var WorkerPDFDoc = (function() {
           worker.onmessage({data: obj});
         }
       }
-      var fontWorker = {
-        postMessage: function(obj) {
-          fontWorker.onmessage({data: obj});
-        }
-      }
     }
+    
+    this.fontsLoading = {};
 
     var processorHandler = this.processorHandler = new MessageHandler("main", worker);
     processorHandler.on("page", function(data) {
@@ -289,23 +285,43 @@ var WorkerPDFDoc = (function() {
       // are all the fonts that are required to render the page AND that
       // aren't loaded on the page yet.
       var depFonts = data.depFonts;
-      var fontsToLoad = [];
       var objs = this.objs;
+      var fontsToLoad = [];
+      var fontsLoading = this.fontsLoading;
+      // The `i` for the checkFontData is stored here to keep the state in
+      // the closure.
+      var i = 0;  
+                  
 
       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++) {
+        for (i; i < depFonts.length; i++) {
           var fontName = depFonts[i];
           if (!objs.hasData(fontName)) {
             console.log('need to wait for fontData', fontName);
-            objs.onData(fontObj, checkFontData);
+            objs.onData(fontName, checkFontData);
             return;
           } else if (!objs.isResolved(fontName)) {
             fontsToLoad.push(fontName);
           }
         }
+        
+        // There can be edge cases where two pages wait for one font and then
+        // call startRenderingFromIRQueue twice with the same font. That makes
+        // the font getting loaded twice and throw an error later as the font
+        // promise gets resolved twice.
+        // This prevents thats fonts are loaded really only once.
+        for (var j = 0; j < fontsToLoad.length; j++) {
+          var fontName = fontsToLoad[j];
+          if (fontsLoading[fontName]) {
+            fontsToLoad.splice(j, 1);
+            j--;
+          } else {
+            fontsLoading[fontName] = true;
+          }
+        }
 
         // At this point, all font data ia loaded. Start the actuall rendering.
         page.startRenderingFromIRQueue(data.IRQueue, fontsToLoad);
@@ -329,15 +345,14 @@ var WorkerPDFDoc = (function() {
           var file = data[3];
           var properties = data[4];
 
-          fontHandler.send("font", [objId, name, file, properties]);
+          processorHandler.send("font", [objId, name, file, properties]);
         break;
         default:
           throw "Got unkown object type " + objType;
       }
     }, this);
 
-    var fontHandler = this.fontHandler = new MessageHandler('font', fontWorker);
-    fontHandler.on('font_ready', function(data) {
+    processorHandler.on('font_ready', function(data) {
       var objId   = data[0];
       var fontObj = new FontShape(data[1]);
 
@@ -355,7 +370,6 @@ var WorkerPDFDoc = (function() {
       // If the main thread is our worker, setup the handling for the messages
       // the main thread sends to it self.
       WorkerProcessorHandler.setup(processorHandler);
-      WorkerFontHandler.setup(fontHandler);
     }
     
     processorHandler.send("doc", data);
diff --git a/worker/font_boot.js b/worker/font_boot.js
deleted file mode 100644 (file)
index 9158d20..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
-/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
-
-'use strict';
-
-importScripts('console.js');
-importScripts('message_handler.js');
-importScripts('../pdf.js');
-importScripts('../fonts.js');
-importScripts('../crypto.js');
-importScripts('../glyphlist.js');
-importScripts('font_handler.js');
-
-
-var handler = new MessageHandler("worker_font", this);
-WorkerFontHandler.setup(handler);
diff --git a/worker/font_handler.js b/worker/font_handler.js
deleted file mode 100644 (file)
index 3479090..0000000
+++ /dev/null
@@ -1,52 +0,0 @@
-var WorkerFontHandler = {
-  setup: function(handler) {
-    handler.on("font", function(data) {  
-      var objId      = data[0];
-      var name       = data[1];
-      var file       = data[2];
-      var properties = data[3];
-
-      var font = {
-        name: name,
-        file: file,
-        properties: properties
-      };
-
-      // Some fonts don't have a file, e.g. the build in ones like Arial.
-      if (file) {
-        var fontFileDict = new Dict();
-        fontFileDict.map = file.dict.map;
-
-        var fontFile = new Stream(file.bytes, file.start,
-                                  file.end - file.start, fontFileDict);
-                         
-        // Check if this is a FlateStream. Otherwise just use the created 
-        // Stream one. This makes complex_ttf_font.pdf work.
-        var cmf = file.bytes[0];
-        if ((cmf & 0x0f) == 0x08) {
-          font.file = new FlateStream(fontFile);
-        } else {
-          font.file = fontFile;
-        }          
-      }
-
-      var obj = new Font(font.name, font.file, font.properties);
-
-      var str = '';
-      var data = obj.data;
-      if (data) {
-        var length = data.length;
-        for (var j = 0; j < length; j++)
-          str += String.fromCharCode(data[j]);
-      }
-
-      obj.str = str;
-
-      // Remove the data array form the font object, as it's not needed
-      // anymore as we sent over the ready str.
-      delete obj.data;
-
-      handler.send("font_ready", [objId, obj]);
-    });
-  }
-}
diff --git a/worker/pdf_worker_loader.js b/worker/pdf_worker_loader.js
new file mode 100644 (file)
index 0000000..deb2778
--- /dev/null
@@ -0,0 +1,19 @@
+/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
+/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
+
+'use strict';
+
+importScripts('console.js');
+importScripts('message_handler.js');
+importScripts('../pdf.js');
+importScripts('../fonts.js');
+importScripts('../crypto.js');
+importScripts('../glyphlist.js');
+importScripts('../metrics.js');
+importScripts('processor_handler.js');
+
+// Listen for messages from the main thread.
+var pdfDoc = null;
+
+var handler = new MessageHandler("worker_processor", this);
+WorkerProcessorHandler.setup(handler);
diff --git a/worker/processor_boot.js b/worker/processor_boot.js
deleted file mode 100644 (file)
index deb2778..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
-/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
-
-'use strict';
-
-importScripts('console.js');
-importScripts('message_handler.js');
-importScripts('../pdf.js');
-importScripts('../fonts.js');
-importScripts('../crypto.js');
-importScripts('../glyphlist.js');
-importScripts('../metrics.js');
-importScripts('processor_handler.js');
-
-// Listen for messages from the main thread.
-var pdfDoc = null;
-
-var handler = new MessageHandler("worker_processor", this);
-WorkerProcessorHandler.setup(handler);
index 28c89db100bfb64c2a74746441e6fd57af173268..7c218e64dcc5cac05efeffd14eefde0df03e53f6 100644 (file)
@@ -63,5 +63,54 @@ var WorkerProcessorHandler = {
         depFonts: Object.keys(fonts)
       });
     }, this);
+    
+    handler.on("font", function(data) {  
+      var objId      = data[0];
+      var name       = data[1];
+      var file       = data[2];
+      var properties = data[3];
+
+      var font = {
+        name: name,
+        file: file,
+        properties: properties
+      };
+
+      // Some fonts don't have a file, e.g. the build in ones like Arial.
+      if (file) {
+        var fontFileDict = new Dict();
+        fontFileDict.map = file.dict.map;
+
+        var fontFile = new Stream(file.bytes, file.start,
+                                  file.end - file.start, fontFileDict);
+                         
+        // Check if this is a FlateStream. Otherwise just use the created 
+        // Stream one. This makes complex_ttf_font.pdf work.
+        var cmf = file.bytes[0];
+        if ((cmf & 0x0f) == 0x08) {
+          font.file = new FlateStream(fontFile);
+        } else {
+          font.file = fontFile;
+        }          
+      }
+
+      var obj = new Font(font.name, font.file, font.properties);
+
+      var str = '';
+      var data = obj.data;
+      if (data) {
+        var length = data.length;
+        for (var j = 0; j < length; j++)
+          str += String.fromCharCode(data[j]);
+      }
+
+      obj.str = str;
+
+      // Remove the data array form the font object, as it's not needed
+      // anymore as we sent over the ready str.
+      delete obj.data;
+
+      handler.send("font_ready", [objId, obj]);
+    });
   }
 }