From 7282a3b480b5526eaf3224aafee9665f37a0a879 Mon Sep 17 00:00:00 2001 From: Diego Pino Navarro Date: Wed, 15 Jul 2020 00:06:10 -0400 Subject: [PATCH] Issue 74b: IABookreader, please cooperate with old friend PDF? (#83) * Use Canvas Aspect ration to deduce missing resource dimension I think a lot of folks have missread the IIIF API Presentation V2.1 specs. Canvas width and height are there for aspect ration. Means we can deduce one of the missing dimensions of a resource based on that. I will try to do that. This is all for LEVEL0 service (no info.json) * Also notify the right thing * Some further JS goodness I wish i knew how to document this! * Honestly? A typo? C'mon * Aspect ratio needs to divide if going height based on width * Remove console.log * And the last one --- js/mirador_strawberry.js | 2 +- js/plugin.iiif-iabookreader_strawberry.js | 97 ++++++++++++++++++----- 2 files changed, 78 insertions(+), 21 deletions(-) diff --git a/js/mirador_strawberry.js b/js/mirador_strawberry.js index e45858af..6d6b9405 100644 --- a/js/mirador_strawberry.js +++ b/js/mirador_strawberry.js @@ -43,7 +43,7 @@ } //@TODO add an extra Manifests key with every other one so people can select the others. var miradorInstance = Mirador.viewer($options); - console.log('initializing Mirador 3.0.0-beta.4') + console.log('initializing Mirador 3.0.0-RC3') } })}} })(jQuery, Drupal, drupalSettings, window.Mirador); \ No newline at end of file diff --git a/js/plugin.iiif-iabookreader_strawberry.js b/js/plugin.iiif-iabookreader_strawberry.js index eafd7ce9..331dd3c8 100644 --- a/js/plugin.iiif-iabookreader_strawberry.js +++ b/js/plugin.iiif-iabookreader_strawberry.js @@ -100,7 +100,56 @@ BookReader.prototype.parseSequence = function (sequenceId) { var tmpdata = []; jQuery.each(self.IIIFsequence.imagesList, function(index,image) { var imageuri = null; + var infojson = null; + + // If serviceURL is null then we can not call infoJSON which also means, if width and height are not + // present, render will fail + // Options we have here + // If either width or height is missing, use canvas ratio, which will have to require if all fails then simply + // use the default 4:3 and log into console so the user/admin/webmaster knows this is happening + // Example of what can happen with Service Level 0 and no correct dimensions + /* + aspectRatio: Infinity // HAHAHA + canvasHeight: 4 + canvasWidth: 3 + height: 0 + imageGetArgument: "?page=2" + imageUrl: "http://localhost:8183/iiif/2/65f%2Fapplication-williams-college-yearbook-01-d708c989-229b-4ba9-a547-ed2faf568e0f.pdf/full/full/0/default.jpg…" + serviceUrl: null + width: 800 + */ + + if (image.width == 0 || image.width == null) { + if ((image.canvasWidth != 0 && image.canvasWidth != null) && + (image.canvasHeight != 0 && image.canvasHeight != null) + ) { + // We have a full ration + var aspectRatio = (image.canvasWidth / image.canvasHeight) || 0.75; + image.width = Math.round(image.height * aspectRatio); + } else { + console.log('canvas is incorrect and has no ratio for ' + image.imageUrl); + console.log('usign a fallback of 3:4, please correct your manifest'); + image.width = Math.round(image.height * 0.75); + } + } else if (image.height == 0 || image.height == null) { + if ((image.canvasWidth != 0 && image.canvasWidth != null) && + (image.canvasHeight != 0 && image.canvasHeight != null) + ) { + // We have a full ration + var aspectRatio = (image.canvasWidth / image.canvasHeight) || 0.75; + image.height = Math.round(image.width / aspectRatio); + } else { + image.height = Math.round(image.width / 0.75); + console.log('canvas is incorrect and has no ratio for ' + image.imageUrl); + console.log('usign a fallback of 3:4, please correct your manifest'); + } + } + + // infojson will be empty if service URL is empty + if (image.serviceUrl != null) { + // Pass also the imageGerArgument to the info.json -- Cantaloupe 4.1.6 + infojson = image.serviceUrl + "/info.json" + image.imageGetArgument; imageuri = image.serviceUrl + "/full/" + image.width + ",/0/default.jpg" + image.imageGetArgument; } else { imageuri = image.imageUrl; @@ -112,10 +161,11 @@ BookReader.prototype.parseSequence = function (sequenceId) { height: image.height, uri: imageuri, pageNum: index+1, - infojson: image.serviceUrl + "/info.json", + infojson: infojson, }); }); self.options.data.push(tmpdata); + delete self.jsonLd; function getImagesList(sequence) { @@ -291,28 +341,35 @@ BookReader.prototype.getRemoteInfoJson = function(index) { var remotedimensions = {}; var infojsonurl = this.getPageProp(index, 'infojson'); + if (infojsonurl == null) { + console.log('Service Level 0, Defaulting to fixed Dimensions ' + self.options.maxWidth + ' px') + console.log(this.getPageProp(index, 'width')); + console.log(this.getPageProp(index, 'height')); + remotedimensions.width = self.options.maxWidth; + remotedimensions.height = self.options.maxWidth; + } else { + jQuery.ajax({ + url: infojsonurl.replace(/^\s+|\s+$/g, ''), + dataType: 'json', + async: false, + success: function (infojson) { + remotedimensions.width = infojson.width; + remotedimensions.height = infojson.height; - jQuery.ajax({ - url: infojsonurl.replace(/^\s+|\s+$/g, ''), - dataType: 'json', - async: false, - success: function (infojson) { - remotedimensions.width = infojson.width; - remotedimensions.height = infojson.height; - - }, - - error: function () { - console.log('Failed loading ' + infojsonurl); - // default to our only known number - // Chances whole manifest is wrong - console.log('Defaulting to fixed Dimensions ' + self.options.maxWidth + ' px') - remotedimensions.width = self.options.maxWidth; - remotedimensions.height = self.options.maxWidth; - } + }, + + error: function () { + console.log('Failed loading ' + infojsonurl); + // default to our only known number + // Chances whole manifest is wrong + console.log('Defaulting to fixed Dimensions ' + self.options.maxWidth + ' px') + remotedimensions.width = self.options.maxWidth; + remotedimensions.height = self.options.maxWidth; + } - }); + }); + } return remotedimensions; }