Skip to content

Commit

Permalink
Issue 74b: IABookreader, please cooperate with old friend PDF? (#83)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
DiegoPino authored Jul 15, 2020
1 parent 060bf66 commit 7282a3b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 21 deletions.
2 changes: 1 addition & 1 deletion js/mirador_strawberry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
97 changes: 77 additions & 20 deletions js/plugin.iiif-iabookreader_strawberry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}

0 comments on commit 7282a3b

Please sign in to comment.