From dec33ad46f4558141187a87ddb41aafbfada71ec Mon Sep 17 00:00:00 2001 From: Michael Ang Date: Fri, 18 Sep 2009 02:57:19 +0000 Subject: [PATCH] Dynamic iframe implementation --- GnuBook/GnuBook.js | 46 +++++++++++++++++++++--- GnuBookIA/datanode/GnuBookJSIA.php | 57 ++++++++++++++++++++---------- 2 files changed, 81 insertions(+), 22 deletions(-) diff --git a/GnuBook/GnuBook.js b/GnuBook/GnuBook.js index e7530eb..75caed9 100644 --- a/GnuBook/GnuBook.js +++ b/GnuBook/GnuBook.js @@ -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 = '' + this.bookTitle + ''; + iframeStr += '

'; + iframeStr += ''; htmlStr = '

Click here to print this page

'; - htmlStr += ''; + htmlStr += ''; htmlStr += '

Close popup

'; - + 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); } } diff --git a/GnuBookIA/datanode/GnuBookJSIA.php b/GnuBookIA/datanode/GnuBookJSIA.php index 3edff3a..d8566b0 100755 --- a/GnuBookIA/datanode/GnuBookJSIA.php +++ b/GnuBookIA/datanode/GnuBookJSIA.php @@ -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; -- 2.20.1