From d43b6d86b50e2b0cbd527345a3c7aa1e65467f8f Mon Sep 17 00:00:00 2001
From: Antoine Vandecreme <antoine.vandecreme@nist.gov>
Date: Thu, 30 Jan 2014 16:43:35 -0500
Subject: [PATCH] Add layersAspectRatioEpsilon check

---
 src/openseadragon.js |  6 ++++++
 src/viewer.js        | 13 +++++++++++++
 test/layers.js       | 13 +++++++++++--
 3 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/src/openseadragon.js b/src/openseadragon.js
index 9ad73a17..4e85c905 100644
--- a/src/openseadragon.js
+++ b/src/openseadragon.js
@@ -193,6 +193,9 @@
   * @property {Number} [opacity=1]
   *     Opacity of the drawer (1=opaque, 0=transparent)
   *
+  * @property {Number} [layersAspectRatioEpsilon=0.0001]
+  *     Maximum aspectRatio mismatch between 2 layers.
+  *
   * @property {Number} [degrees=0]
   *     Initial rotation.
   *
@@ -721,6 +724,9 @@ window.OpenSeadragon = window.OpenSeadragon || function( options ){
             // APPEARANCE
             opacity:                1,
 
+            // LAYERS SETTINGS
+            layersAspectRatioEpsilon:   0.0001,
+
             //REFERENCE STRIP SETTINGS
             showReferenceStrip:          false,
             referenceStripScroll:       'horizontal',
diff --git a/src/viewer.js b/src/viewer.js
index fcd54dc1..bd5ff377 100644
--- a/src/viewer.js
+++ b/src/viewer.js
@@ -1044,6 +1044,19 @@ $.extend( $.Viewer.prototype, $.EventSource.prototype, $.ControlDock.prototype,
                 return;
             }
 
+            for ( var i = 0; i < _this.drawers.length; i++ ) {
+                var otherAspectRatio = _this.drawers[ i ].source.aspectRatio;
+                var diff = otherAspectRatio - tileSource.aspectRatio;
+                if ( Math.abs( diff ) > _this.layersAspectRatioEpsilon ) {
+                    raiseAddLayerFailed({
+                        message: "Aspect ratio mismatch with layer " + i + ".",
+                        source: tileSource,
+                        options: options
+                    });
+                    return;
+                }
+            }
+
             var drawer = new $.Drawer({
                 viewer: _this,
                 source: tileSource,
diff --git a/test/layers.js b/test/layers.js
index 78606303..a20c92d0 100644
--- a/test/layers.js
+++ b/test/layers.js
@@ -27,7 +27,7 @@
 
     // ----------
     asyncTest( 'Layers operations', function() {
-        expect( 22 );
+        expect( 23 );
         viewer.addHandler( "open", function( ) {
             equal( 1, viewer.getLayersCount( ),
                 "One layer should be present after opening." );
@@ -112,7 +112,16 @@
 
                         });
                         viewer.removeLayer( layer2 );
-                        start();
+
+                        options.tileSource.levels[0].width = 500;
+                        viewer.addHandler( "add-layer-failed", function addLayerFailed( event ) {
+                            viewer.removeHandler( "add-layer-failed", addLayerFailed );
+
+                            equal( viewer.getLayersCount(), 3 );
+
+                            start();
+                        });
+                        viewer.addLayer( options );
                     });
                 });
             });