Quantize reduction factor in 2up. See https://bugs.edge.launchpad.net/gnubook/+bug...
authorMichael Ang <mang@archive.org>
Fri, 14 Aug 2009 21:38:24 +0000 (21:38 +0000)
committerMichael Ang <mang@archive.org>
Fri, 14 Aug 2009 21:38:24 +0000 (21:38 +0000)
GnuBook/GnuBook.js

index 09c291c..2d41041 100644 (file)
@@ -661,10 +661,12 @@ GnuBook.prototype.zoom2up = function(direction) {
     } else if (1 == direction) {
         if (this.reduce <= 0.5) return;
         this.reduce*=0.5;           //zoom in
+        this.reduce = this.quantizeReductionFactor(this.reduce);        
         this.twoPage.autofit = false;
     } else {
         if (this.reduce >= 8) return;
         this.reduce *= 2; // zoom out
+        this.reduce = this.quantizeReductionFactor(this.reduce);
         this.twoPage.autofit = false;
     }
 
@@ -685,6 +687,25 @@ GnuBook.prototype.zoom2up = function(direction) {
     this.twoPageCenterView(oldCenter.percentageX, oldCenter.percentageY);
 }
 
+
+// quantizeReductionFactor(reduce)
+//______________________________________________________________________________
+// Quantizes the given reduction factor to closest power of two from set from 12.5% to 200%
+GnuBook.prototype.quantizeReductionFactor = function(reduce) {
+    var reductionFactors = [0.5, 1, 2, 4, 8, 16];
+    var quantized = reductionFactors[0];
+    var distance = Math.abs(reduce - quantized);
+    for (var i = 1; i < reductionFactors.length; i++) {
+        newDistance = Math.abs(reduce - reductionFactors[i]);
+        if (newDistance < distance) {
+            distance = newDistance;
+            quantized = reductionFactors[i];
+        }
+    }
+    
+    return quantized;
+}
+
 // jumpToPage()
 //______________________________________________________________________________
 // Attempts to jump to page.  Returns true if page could be found, false otherwise.