Skip to content
This repository was archived by the owner on May 7, 2025. It is now read-only.

Resize images to fit a reflowable column or scrollable page view. #173

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions js/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,56 @@ Helpers.extendedThrottle = function (startCb, tickCb, endCb, tickRate, waitThres
};


Helpers.fitImages = function ($html, options) {
if(!$html) {
return;
}

options = options || {};

var $elem;
var height;
var width;
var $body = $('body', $html);
//maxHeight is (html el height) - (body el padding+margin+border)
//we add 3 to the maxHeight as a buffer, this fixes a strange scaling issue on IE11
// if we set max-width/max-height to 100% columnizing engine chops images embedded in the text
// (but not if we set it to 99-98%) go figure.
var maxDimensions = {
maxHeight: $html.height() - ($body.outerHeight(true) - $body.height()
+ Math.floor($html.height() * 0.02)),
maxWidth: ($body[0].getClientRects()[0] ? $body[0].getClientRects()[0].width : $html.width())
- Math.floor($html.width() * 0.02)
};

$('img, svg', $html).each(function(){

$elem = $(this);

if (options.doNotChangeWidth) {
delete maxDimensions.maxWidth;
}
if (options.doNotChangeHeight) {
delete maxDimensions.maxHeight;
}

$elem.css(maxDimensions);

var ratiosUnbalanced = false;
if ($elem[0].tagName.toLowerCase() === "img") {
ratiosUnbalanced =
(($elem[0].naturalWidth / $elem[0].naturalHeight) * 10 | 0) !==
(($elem[0].clientWidth / $elem[0].clientHeight) * 10 | 0);
}
if (!$elem.css('height') || ratiosUnbalanced) {
$elem.css('height', 'auto');
}
if (!$elem.css('width') || ratiosUnbalanced) {
$elem.css('width', 'auto');
}
});
};

//TODO: consider using CSSOM escape() or polyfill
//https://github.com/mathiasbynens/CSS.escape/blob/master/css.escape.js
//http://mathiasbynens.be/notes/css-escapes
Expand Down
7 changes: 7 additions & 0 deletions js/views/one_page_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,13 @@ var OnePageView = function (options, classes, enableBookStyleOverrides, reader)
return _$iframe.offset();
}
return undefined;
};

/**
* @private
*/
this._fitImages = function (options) {
return ReadiumSDK.Helpers.fitImages(_$epubHtml, options);
}
};

Expand Down
Loading