Dynamic iframe implementation
authorMichael Ang <mang@archive.org>
Fri, 18 Sep 2009 02:57:19 +0000 (02:57 +0000)
committerMichael Ang <mang@archive.org>
Fri, 18 Sep 2009 02:57:19 +0000 (02:57 +0000)
GnuBook/GnuBook.js
GnuBookIA/datanode/GnuBookJSIA.php

index e7530eb..75caed9 100644 (file)
@@ -507,7 +507,7 @@ GnuBook.prototype.drawLeafsOnePage = function() {
             $('#GBpageview').append(div);
 
             var img = document.createElement("img");
-            img.src = this.getPageURI(index);
+            img.src = this.getPageURI(index, this.reduce, 0);
             $(img).css('width', width+'px');
             $(img).css('height', height+'px');
             $(div).append(img);
@@ -2418,13 +2418,45 @@ GnuBook.prototype.printPage = function() {
         indexToPrint = this.twoPage.currentIndexL;
     }
     
-    var aspectRatio = this.getPageWidth(indexToPrint) / this.getPageHeight(indexToPrint);
+    var imageAspect = this.getPageWidth(indexToPrint) / this.getPageHeight(indexToPrint);    
+    var paperAspect = 8.5 / 11; // Use US Letter in portrait as guesstimate
+    var rotate = 0;
+    
+    // Rotate if possible and appropriate, to get larger image size on printed page
+    if (this.canRotatePage(indexToPrint)) {
+        if (imageAspect > 1 && imageAspect > paperAspect) {
+            // more wide than square, and more wide than paper
+            rotate = 90;
+            imageAspect = 1/imageAspect;
+        }
+    }
+    
+    var fitAttrs;
+    if (imageAspect > paperAspect) {
+        // wider than paper, fit width
+        fitAttrs = 'width="95%"';
+    } else {
+        // taller than paper, fit height
+        fitAttrs = 'height="95%"';
+    }
+    
+    var imageURL = this.getPageURI(indexToPrint, 1, rotate);
+    
+    var iframeStr = '<html><head><title>' + this.bookTitle + '</title></head><body>';
+    iframeStr += '<p style="text-align:center;"><img src="' + imageURL + '" ' + fitAttrs + ' /></p>';
+    iframeStr += '</body></html>';
     
     htmlStr =  '<p style="text-align:center;"><b><a href="javascript:void(0);" onclick="window.frames[0].focus(); window.frames[0].print(); return false;">Click here to print this page</a></b></p>';
-    htmlStr += '<iframe name ="printFrame" id="printFrame" src="/bookreader/GnuBookPrint.php?id='+this.bookId+'&server='+this.server+'&zip='+this.zip+'&index='+this.leafMap[indexToPrint]+'&format='+this.imageFormat+'&aspect=' + aspectRatio + '" width="500px" height="400px"></iframe>';
+    htmlStr += '<iframe name ="printFrame" id="printFrame" width="500px" height="400px"></iframe>';
     htmlStr += '<p style="text-align:center;"><a href="" onclick="gb.printPopup = null; $(this.parentNode.parentNode).remove(); return false">Close popup</a></p>';    
-
+    
     this.printPopup.innerHTML = htmlStr;    
+    
+    $('#printFrame').load(function() {
+        // $$$ Not firing in Safari
+        var doc = GnuBook.util.getIFrameDocument(this);
+        $('body', doc).html(iframeStr)
+    });
 }
 
 // showEmbedCode()
@@ -3091,5 +3123,11 @@ GnuBook.prototype.canSwitchToMode = function(mode) {
 GnuBook.util = {
     clamp: function(value, min, max) {
         return Math.min(Math.max(value, min), max);
+    },
+
+    getIFrameDocument: function(iframe) {
+        // Adapted from http://xkr.us/articles/dom/iframe-document/
+        var outer = (iframe.contentWindow || iframe.contentDocument);
+        return (outer.document || outer);
     }
 }
index 3edff3a..d8566b0 100755 (executable)
@@ -127,7 +127,24 @@ gb.getPageHeight = function(index) {
     return this.pageH[index];
 }
 
-gb.getPageURI = function(index) {
+// Returns true if page image is available rotated
+gb.canRotatePage = function(index) {
+    return 'jp2' == this.imageFormat; // Assume single format for now
+}
+
+// reduce defaults to 1 (no reduction)
+// rotate defaults to 0 (no rotation)
+gb.getPageURI = function(index, reduce, rotate) {
+
+    var _reduce;
+    var _rotate;
+
+    if ('undefined' == typeof(reduce)) {
+        _reduce = 1;
+    }
+    if ('undefined' == typeof(rotate)) {
+        _rotate = 0;
+    }
     var leafStr = '0000';
     var imgStr = this.leafMap[index].toString();
     var re = new RegExp("0{"+imgStr.length+"}$");
@@ -137,26 +154,30 @@ gb.getPageURI = function(index) {
     
     // $$$ add more image stack formats here
     if (1==this.mode) {
-        var url = 'http://'+this.server+'/GnuBook/GnuBookImages.php?zip='+this.zip+'&file='+file+'&scale='+this.reduce;
+        var url = 'http://'+this.server+'/GnuBook/GnuBookImages.php?zip='+this.zip+'&file='+file+'&scale='+_reduce+'&rotate='+_rotate;
     } else {
-        var ratio = this.getPageHeight(index) / this.twoPage.height;
-        var scale;
-        // $$$ we make an assumption here that the scales are available pow2 (like kakadu)
-        if (ratio < 2) {
-            scale = 1;
-        } else if (ratio < 4) {
-            scale = 2;
-        } else if (ratio < 8) {
-            scale = 4;
-        } else if (ratio < 16) {
-            scale = 8;
-        } else  if (ratio < 32) {
-            scale = 16;
-        } else {
-            scale = 32;
+        if ('undefined' == typeof(reduce)) {
+            // reduce not passed in
+            var ratio = this.getPageHeight(index) / this.twoPage.height;
+            var scale;
+            // $$$ we make an assumption here that the scales are available pow2 (like kakadu)
+            if (ratio < 2) {
+                scale = 1;
+            } else if (ratio < 4) {
+                scale = 2;
+            } else if (ratio < 8) {
+                scale = 4;
+            } else if (ratio < 16) {
+                scale = 8;
+            } else  if (ratio < 32) {
+                scale = 16;
+            } else {
+                scale = 32;
+            }
+            _reduce = scale;
         }
     
-        var url = 'http://'+this.server+'/GnuBook/GnuBookImages.php?zip='+this.zip+'&file='+file+'&scale='+scale;
+        var url = 'http://'+this.server+'/GnuBook/GnuBookImages.php?zip='+this.zip+'&file='+file+'&scale='+_reduce+'&rotate='+_rotate;
         
     }
     return url;