]> git.parisson.com Git - pdf.js.git/commitdiff
Adding support for page rotation
authorBrendan Dahl <brendan.dahl@gmail.com>
Sat, 6 Aug 2011 21:41:18 +0000 (14:41 -0700)
committerBrendan Dahl <brendan.dahl@gmail.com>
Sat, 6 Aug 2011 21:41:18 +0000 (14:41 -0700)
pdf.js
test/driver.js
test/pdfs/rotation.pdf [new file with mode: 0644]
test/test_manifest.json
web/viewer.js

diff --git a/pdf.js b/pdf.js
index 9d915b8eca2f3096d06c02c37d42a86ead2d13dd..50594f2ad98e2ea2659944b682dc7f9f88251be1 100644 (file)
--- a/pdf.js
+++ b/pdf.js
@@ -2995,7 +2995,7 @@ var Page = (function() {
     inheritPageProp: function(key) {
       var dict = this.pageDict;
       var obj = dict.get(key);
-      while (!obj) {
+      while (typeof obj == 'undefined') {
         dict = this.xref.fetchIfRef(dict.get('Parent'));
         if (!dict)
           break;
@@ -3014,6 +3014,32 @@ var Page = (function() {
       return shadow(this, 'mediaBox',
                     ((IsArray(obj) && obj.length == 4) ? obj : null));
     },
+    get width() {
+      var mediaBox = this.mediaBox;
+      var rotate = this.rotate;
+      var width;
+      if (rotate == 0 || rotate == 180) {
+        width = (mediaBox[2] - mediaBox[0]);
+      } else {
+        width = (mediaBox[3] - mediaBox[1]);
+      }
+      return shadow(this, 'width', width);
+    },
+    get height() {
+      var mediaBox = this.mediaBox;
+      var rotate = this.rotate;
+      var height;
+      if (rotate == 0 || rotate == 180) {
+        height = (mediaBox[3] - mediaBox[1]);
+      } else {
+        height = (mediaBox[2] - mediaBox[0]);
+      }
+      return shadow(this, 'height', height);
+    },
+    get rotate() {
+      var rotate = this.inheritPageProp("Rotate") || 0;
+      return shadow(this, 'rotate', rotate);
+    },
     startRendering: function(canvasCtx, continuation, onerror) {
       var self = this;
       var stats = self.stats;
@@ -3085,8 +3111,9 @@ var Page = (function() {
       var mediaBox = xref.fetchIfRef(this.mediaBox);
       assertWellFormed(IsDict(resources), 'invalid page resources');
       gfx.beginDrawing({ x: mediaBox[0], y: mediaBox[1],
-            width: mediaBox[2] - mediaBox[0],
-            height: mediaBox[3] - mediaBox[1] });
+            width: this.width,
+            height: this.height,
+            rotate: this.rotate });
       gfx.execute(this.code, xref, resources);
       gfx.endDrawing();
     }
@@ -3994,8 +4021,21 @@ var CanvasGraphics = (function() {
     beginDrawing: function(mediaBox) {
       var cw = this.ctx.canvas.width, ch = this.ctx.canvas.height;
       this.ctx.save();
-      this.ctx.scale(cw / mediaBox.width, -ch / mediaBox.height);
-      this.ctx.translate(0, -mediaBox.height);
+      switch (mediaBox.rotate) {
+        case 0:
+          this.ctx.transform(1, 0, 0, -1, 0, ch);
+          break;
+        case 90:
+          this.ctx.transform(0, 1, 1, 0, 0, 0);
+          break;
+        case 180:
+          this.ctx.transform(-1, 0, 0, 1, cw, 0);
+          break;
+        case 270:
+          this.ctx.transform(0, -1, -1, 0, cw, ch);
+          break;
+      }
+      this.ctx.scale(cw / mediaBox.width, ch / mediaBox.height);
     },
 
     compile: function(stream, xref, resources, fonts) {
index 642c5273c1a69309d3dd0bf9904150ada930d7d8..8d962eac88420098d420538d8f8585501c2f109a 100644 (file)
@@ -105,8 +105,8 @@ function nextPage(task, loadError) {
 
             var pdfToCssUnitsCoef = 96.0 / 72.0;
             // using mediaBox for the canvas size
-            var pageWidth = (page.mediaBox[2] - page.mediaBox[0]);
-            var pageHeight = (page.mediaBox[3] - page.mediaBox[1]);
+            var pageWidth = page.width;
+            var pageHeight = page.height;
             canvas.width = pageWidth * pdfToCssUnitsCoef;
             canvas.height = pageHeight * pdfToCssUnitsCoef;
             clear(ctx);
diff --git a/test/pdfs/rotation.pdf b/test/pdfs/rotation.pdf
new file mode 100644 (file)
index 0000000..807dafe
Binary files /dev/null and b/test/pdfs/rotation.pdf differ
index bea4cf4447a6a6de9c1fc65083f67bfd53f8dca2..8b0e2b7195e6cf77d28596d2a121cf100513173b 100644 (file)
        "link": true,
        "rounds": 1,
        "type": "load"
+    },
+    {  "id": "rotation",
+       "file": "pdfs/rotation.pdf",
+       "rounds": 1,
+       "type": "load"
     }
 ]
index 3283883967d9418aa81f3f5c7ba3f95a980b5637..37a0d89b666d52ff69ca69afb06ef7662d7277c7 100644 (file)
@@ -104,10 +104,7 @@ var PDFView = {
     var thumbnails = this.thumbnails = [];
     for (var i = 1; i <= pagesCount; i++) {
       var page = pdf.getPage(i);
-      var mediaBox = page.mediaBox;
-      var width = (mediaBox[2] - mediaBox[0]);
-      var height = (mediaBox[3] - mediaBox[1]);
-      pages.push(new PageView(container, page, i, width, height, page.stats));
+      pages.push(new PageView(container, page, i, page.width, page.height, page.stats));
       thumbnails.push(new ThumbnailView(sidebar, pages[i - 1]));
     };