]> git.parisson.com Git - pdf.js.git/commitdiff
#123, part 2: move more PDF-page rendering stuff into pdf.js
authorChris Jones <jones.chris.g@gmail.com>
Mon, 11 Jul 2011 06:21:13 +0000 (23:21 -0700)
committerChris Jones <jones.chris.g@gmail.com>
Mon, 11 Jul 2011 06:21:13 +0000 (23:21 -0700)
pdf.js
test/driver.js
web/multi_page_viewer.js
web/viewer.js

diff --git a/pdf.js b/pdf.js
index 2bd6d6e6a1914deb67dc9c992b0adb9155c82f2d..c450b6ddddf066af615609b6fb8b9c8c0643897a 100644 (file)
--- a/pdf.js
+++ b/pdf.js
@@ -2923,9 +2923,15 @@ var XRef = (function() {
 
 var Page = (function() {
   function constructor(xref, pageNumber, pageDict) {
-    this.xref = xref;
     this.pageNumber = pageNumber;
     this.pageDict = pageDict;
+    this.stats = {
+      create: Date.now(),
+      compile: 0.0,
+      fonts: 0.0,
+      render: 0.0,
+    };
+    this.xref = xref;
   }
 
   constructor.prototype = {
@@ -2954,6 +2960,32 @@ var Page = (function() {
       return shadow(this, 'mediaBox',
                     ((IsArray(obj) && obj.length == 4) ? obj : null));
     },
+    startRendering: function(canvasCtx, continuation) {
+      var self = this;
+      var stats = self.stats;
+      stats.compile = stats.fonts = stats.render = 0;
+
+      var gfx = new CanvasGraphics(canvasCtx);
+      var fonts = [ ];
+
+      this.compile(gfx, fonts);
+      stats.compile = Date.now();
+
+      FontLoader.bind(
+        fonts,
+        function() {
+          stats.fonts = Date.now();
+          // Always defer call to display() to work around bug in
+          // Firefox error reporting from XHR callbacks.
+          setTimeout(function () {
+            self.display(gfx);
+            stats.render = Date.now();
+            continuation();
+          });
+        });
+    },
+
+
     compile: function(gfx, fonts) {
       if (this.code) {
         // content was compiled
index b4c2c64e03707a65e228253d1f30674dde682847..e397f108be3936a8865effcb254800af5de49534 100644 (file)
@@ -1,8 +1,10 @@
+/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
+/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
+
 /*
  * A Test Driver for PDF.js
  */
 
-
 var appPath, browser, canvas, currentTaskIdx, manifest, stdout;
 
 function queryParams() {
@@ -21,16 +23,16 @@ function load() {
     browser = params.browser;
     manifestFile = params.manifestFile;
     appPath = params.path;
-    
+
     canvas = document.createElement("canvas");
     canvas.mozOpaque = true;
     stdout = document.getElementById("stdout");
-    
+
     log("load...\n");
 
     log("Harness thinks this browser is '"+ browser + "' with path " + appPath + "\n");
     log("Fetching manifest "+ manifestFile +"...");
-    
+
     var r = new XMLHttpRequest();
     r.open("GET", manifestFile, false);
     r.onreadystatechange = function(e) {
@@ -61,15 +63,15 @@ function nextTask() {
         if (r.readyState == 4) {
             var data = r.mozResponseArrayBuffer || r.mozResponse ||
                 r.responseArrayBuffer || r.response;
-        
+
             try {
                 task.pdfDoc = new PDFDoc(new Stream(data));
             } catch(e) {
                 failure = 'load PDF doc: '+ e.toString();
             }
-            
+
             task.pageNum = 1, nextPage(task, failure);
-        }    
+        }
     };
     r.send(null);
 }
@@ -92,25 +94,13 @@ function nextPage(task, loadError) {
     var failure = loadError || '';
 
     var ctx = null;
-    var fonts;
-    var gfx = null;
     var page = null;
-
     if (!failure) {
-        log("    loading page "+ task.pageNum +"... ");
-        ctx = canvas.getContext("2d");
-        fonts = [];
         try {
-            gfx = new CanvasGraphics(ctx);
+            log("    loading page "+ task.pageNum +"... ");
+            ctx = canvas.getContext("2d");
             page = task.pdfDoc.getPage(task.pageNum);
-            page.compile(gfx, fonts);
-        } catch(e) {
-            failure = 'compile: '+ e.toString();
-        }
-    }
 
-    if (!failure) {
-        try {
             var pdfToCssUnitsCoef = 96.0 / 72.0;
             // using mediaBox for the canvas size
             var pageWidth = (page.mediaBox[2] - page.mediaBox[0]);
@@ -118,42 +108,28 @@ function nextPage(task, loadError) {
             canvas.width = pageWidth * pdfToCssUnitsCoef;
             canvas.height = pageHeight * pdfToCssUnitsCoef;
             clear(ctx);
-        } catch(e) {
-            failure = 'page setup: '+ e.toString();
-        }
-    }
 
-    if (!failure) {
-        try {
-            FontLoader.bind(fonts, function() { 
-                snapshotCurrentPage(gfx, page, task, failure); 
-                });
+            page.startRendering(
+              ctx,
+              function() { snapshotCurrentPage(page, task, failure); });
         } catch(e) {
-            failure = 'fonts: '+ e.toString();
+            failure = 'page setup: '+ e.toString();
         }
     }
 
     if (failure) {
         // Skip right to snapshotting if there was a failure, since the
         // fonts might be in an inconsistent state.
-        snapshotCurrentPage(gfx, page, task, failure);
+        snapshotCurrentPage(page, task, failure);
     }
 }
 
-function snapshotCurrentPage(gfx, page, task, failure) {
+function snapshotCurrentPage(page, task, failure) {
     log("done, snapshotting... ");
-    
-    if (!failure) {
-        try {
-            page.display(gfx);
-        } catch(e) {
-            failure = 'render: '+ e.toString();
-        }
-    }
 
     sendTaskResult(canvas.toDataURL("image/png"), task, failure);
     log("done"+ (failure ? " (failed!: "+ failure +")" : "") +"\n");
-    
+
     // Set up the next request
     backoff = (inFlightRequests > 0) ? inFlightRequests * 10 : 0;
     setTimeout(function() {
index 3a64420fb262b866a4341a1ffeb841154c28a28c..3e7e122e07bf30cf831f5bc307e068facf906665 100644 (file)
@@ -123,14 +123,7 @@ var PDFViewer = {
       ctx.fillRect(0, 0, canvas.width, canvas.height);
       ctx.restore();
 
-      var gfx = new CanvasGraphics(ctx);
-
-      // 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);
-
-      FontLoader.bind(fonts, function() { page.display(gfx); });
+      page.startRendering(ctx, function() { });
     }
   },
 
@@ -183,14 +176,7 @@ var PDFViewer = {
       ctx.fillRect(0, 0, canvas.width, canvas.height);
       ctx.restore();
 
-      var gfx = new CanvasGraphics(ctx);
-
-      // 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);
-
-      FontLoader.bind(fonts, function() { page.display(gfx); });
+      page.startRendering(ctx, function() { });
     }
   },
 
index 770ddb039bcc3d466441bf04063d9033b450ab08..0e7cd59dba9d5624550b4b71b3ea3f9da8770997 100644 (file)
@@ -72,29 +72,15 @@ function displayPage(num) {
   ctx.fillRect(0, 0, canvas.width, canvas.height);
   ctx.restore();
 
-  var gfx = new CanvasGraphics(ctx);
-
-  // 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 t2 = Date.now();
-
-  function displayPage() {
-    var t3 = Date.now();
-
-    page.display(gfx);
-
-    var t4 = Date.now();
-
-    var infoDisplay = document.getElementById('info');
-    infoDisplay.innerHTML = 'Time to load/compile/fonts/render: ' +
-      (t1 - t0) + '/' + (t2 - t1) + '/' + (t3 - t2) + '/' + (t4 - t3) + ' ms';
-  }
-
-  // Always defer call to displayPage() to work around bug in
-  // Firefox error reporting from XHR callbacks.
-  FontLoader.bind(fonts, function() { setTimeout(displayPage, 0); });
+  page.startRendering(
+    ctx,
+    function() {
+      var infoDisplay = document.getElementById('info');
+      var stats = page.stats;
+      var t2 = stats.compile, t3 = stats.fonts, t4 = stats.render;
+      infoDisplay.innerHTML = 'Time to load/compile/fonts/render: ' +
+        (t1 - t0) + '/' + (t2 - t1) + '/' + (t3 - t2) + '/' + (t4 - t3) + ' ms';
+  });
 }
 
 function nextPage() {