]> git.parisson.com Git - pdf.js.git/commitdiff
Update the multi_page_viewer.js code to work with the new FontLoader API
authorVivien Nicolas <21@vingtetun.org>
Fri, 24 Jun 2011 19:46:48 +0000 (21:46 +0200)
committerVivien Nicolas <21@vingtetun.org>
Fri, 24 Jun 2011 19:46:48 +0000 (21:46 +0200)
multi_page_viewer.js

index 3a02ea332cd63c28dcdc01a05d6cfc3a17ec04fe..1631433ca1fa7d4576a9c57f01b7bfc734f926dc 100644 (file)
@@ -3,6 +3,8 @@
 
 "use strict";
 
+var pageTimeout;
+
 var PDFViewer = {
   queryParams: {},
   
@@ -75,81 +77,45 @@ var PDFViewer = {
   },
   
   drawPage: function(num) {
-    if (!PDFViewer.pdf) {
+    if (!PDFViewer.pdf)
       return;
-    }
-    
+
     var div = document.getElementById('pageContainer' + num);
     var canvas = document.createElement('canvas');
-    
+
     if (div && !div.hasChildNodes()) {
-      div.appendChild(canvas);
-      
       var page = PDFViewer.pdf.getPage(num);
-      
+
       canvas.id = 'page' + num;
       canvas.mozOpaque = true;
-      
+
       // Canvas dimensions must be specified in CSS pixels. CSS pixels
       // are always 96 dpi. These dimensions are 8.5in x 11in at 96dpi.
       canvas.width = PDFViewer.pageWidth();
       canvas.height = PDFViewer.pageHeight();
-      
+      div.appendChild(canvas);
+
       var ctx = canvas.getContext('2d');
       ctx.save();
       ctx.fillStyle = 'rgb(255, 255, 255)';
       ctx.fillRect(0, 0, canvas.width, canvas.height);
       ctx.restore();
-      
+
       var gfx = new CanvasGraphics(ctx);
-      var fonts = [];
-      
+
       // page.compile will collect all fonts for us, once we have loaded them
       // we can trigger the actual page rendering with page.display
+      var fonts = [];
       page.compile(gfx, fonts);
-      
-      var areFontsReady = true;
-      
-      // Inspect fonts and translate the missing one
-      var fontCount = fonts.length;
-      
-      for (var i = 0; i < fontCount; i++) {
-        var font = fonts[i];
-        
-        if (Fonts[font.name]) {
-          areFontsReady = areFontsReady && !Fonts[font.name].loading;
-          continue;
-        }
-        
-        new Font(font.name, font.file, font.properties);
-        
-        areFontsReady = false;
-      }
-      
-      var pageInterval;
-      
-      var delayLoadFont = function() {
-        for (var i = 0; i < fontCount; i++) {
-          if (Fonts[font.name].loading) {
-            return;
-          }
-        }
-        
-        clearInterval(pageInterval);
-        
-        while (div.hasChildNodes()) {
-          div.removeChild(div.firstChild);
+
+      var loadFont = function() {
+        if (!FontsLoader.bind(fonts)) {
+          pageTimeout = window.setTimeout(loadFont, 10);
+          return;
         }
-        
-        PDFViewer.drawPage(num);
-      }
-      
-      if (!areFontsReady) {
-        pageInterval = setInterval(delayLoadFont, 10);
-        return;
+        page.display(gfx);
       }
-      
-      page.display(gfx);
+      loadFont();
     }
   },
   
@@ -258,7 +224,6 @@ var PDFViewer = {
 };
 
 window.onload = function() {
-  
   // Parse the URL query parameters into a cached object.
   PDFViewer.queryParams = function() {
     var qs = window.location.search.substring(1);