From e8f0e6f6dc39251eea304d41fc69858d4bb7b282 Mon Sep 17 00:00:00 2001
From: Chris Beer <cabeer@stanford.edu>
Date: Thu, 28 May 2020 10:47:08 -0700
Subject: [PATCH] Support clipping fragments to display in the OSD viewer

---
 src/components/OpenSeadragonViewer.js | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/src/components/OpenSeadragonViewer.js b/src/components/OpenSeadragonViewer.js
index 5db718e75..461ebdbc7 100644
--- a/src/components/OpenSeadragonViewer.js
+++ b/src/components/OpenSeadragonViewer.js
@@ -36,6 +36,14 @@ export class OpenSeadragonViewer extends Component {
     });
   }
 
+  /** */
+  static resourceClip(contentResource) {
+    const fragmentMatch = contentResource.id.match(/xywh=(.*)$/);
+    if (!fragmentMatch) return undefined;
+    const bounds = fragmentMatch[1].split(',').map(str => parseInt(str, 10));
+    return new OpenSeadragon.Rect(...bounds);
+  }
+
   /**
    * @param {Object} props
    */
@@ -223,7 +231,10 @@ export class OpenSeadragonViewer extends Component {
       if (!this.viewer) {
         return;
       }
+
+      const clip = OpenSeadragonViewer.resourceClip(contentResource);
       this.viewer.addSimpleImage({
+        ...(clip && { clip }),
         error: event => reject(event),
         fitBounds: new OpenSeadragon.Rect(
           ...canvasWorld.contentResourceToWorldCoordinates(contentResource),
@@ -249,8 +260,10 @@ export class OpenSeadragonViewer extends Component {
       const contentResource = canvasWorld.contentResource(infoResponse.id);
 
       if (!contentResource) return;
+      const clip = OpenSeadragonViewer.resourceClip(contentResource);
 
       this.viewer.addTiledImage({
+        ...(clip && { clip }),
         error: event => reject(event),
         fitBounds: new OpenSeadragon.Rect(
           ...canvasWorld.contentResourceToWorldCoordinates(contentResource),
-- 
GitLab