Skip to content

Commit

Permalink
Modify rectangle-based visibility detection so it supports vertical w…
Browse files Browse the repository at this point in the history
…riting mode.
  • Loading branch information
Juan Corona committed Sep 6, 2014
1 parent 1b735f3 commit 9e4dfb7
Showing 1 changed file with 36 additions and 7 deletions.
43 changes: 36 additions & 7 deletions js/views/cfi_navigation_logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,30 @@ ReadiumSDK.Views.CfiNavigationLogic = function($viewport, $iframe, options){
return options.paginationInfo && !!options.paginationInfo.rightToLeft;
}

/**
* @private
* Checks whether or not pages are rendered with vertical writing mode
*
* @returns {boolean}
*/
function isVerticalWritingMode() {
return options.paginationInfo && !!options.paginationInfo.isVerticalWritingMode;
}


/**
* @private
* Checks whether or not a (fully adjusted) rectangle is at least partly visible
*
* @param {Object} rect
* @param {Object} frameDimensions
* @param {boolean} [isVwm] isVerticalWritingMode
* @returns {boolean}
*/
function isRectVisible(rect, frameDimensions) {
function isRectVisible(rect, frameDimensions, isVwm) {
if (isVwm) {
return rect.top >= 0 && rect.top < frameDimensions.height;
}
return rect.left >= 0 && rect.left < frameDimensions.width;
}

Expand All @@ -81,7 +96,7 @@ ReadiumSDK.Views.CfiNavigationLogic = function($viewport, $iframe, options){
*/
function getColumnFullWidth() {

if (!options.paginationInfo || options.paginationInfo.isVerticalWritingMode)
if (!options.paginationInfo || isVerticalWritingMode())
{
return $iframe.width();
}
Expand Down Expand Up @@ -169,6 +184,7 @@ ReadiumSDK.Views.CfiNavigationLogic = function($viewport, $iframe, options){
}

var isRtl = isPageProgressionRightToLeft();
var isVwm = isVerticalWritingMode();
var columnFullWidth = getColumnFullWidth();
var frameDimensions = {
width: $iframe.width(),
Expand All @@ -179,15 +195,15 @@ ReadiumSDK.Views.CfiNavigationLogic = function($viewport, $iframe, options){
// because of webkit inconsistency, that single rectangle should be adjusted
// until it hits the end OR will be based on the FIRST column that is visible
adjustRectangle(clientRectangles[0], frameDimensions, columnFullWidth,
isRtl, true);
isRtl, isVwm, true);
}

// for an element split between several CSS columns,
// both Firefox and IE produce as many client rectangles;
// each of those should be checked
var visibilityPercentage = 0;
for (var i = 0, l = clientRectangles.length; i < l; ++i) {
if (isRectVisible(clientRectangles[i], frameDimensions)) {
if (isRectVisible(clientRectangles[i], frameDimensions, isVwm)) {
visibilityPercentage = shouldCalculateVisibilityPercentage
? measureVisibilityPercentageByRectangles(clientRectangles, i)
: 100;
Expand Down Expand Up @@ -387,13 +403,20 @@ ReadiumSDK.Views.CfiNavigationLogic = function($viewport, $iframe, options){
* @param {Object} frameDimensions
* @param {number} columnFullWidth
* @param {boolean} isRtl
* @param {boolean} isVwm isVerticalWritingMode
* @param {boolean} shouldLookForFirstVisibleColumn
* If set, there'll be two-phase adjustment
* (to align a rectangle with a viewport)
*/
function adjustRectangle(rect, frameDimensions, columnFullWidth, isRtl,
function adjustRectangle(rect, frameDimensions, columnFullWidth, isRtl, isVwm,
shouldLookForFirstVisibleColumn) {

// Rectangle adjustment is not needed in VWM since it does not deal with columns
if (isVwm) {
return;
}

if (isRtl) {
columnFullWidth *= -1; // horizontal shifts are reverted in RTL mode
}
Expand All @@ -409,7 +432,7 @@ ReadiumSDK.Views.CfiNavigationLogic = function($viewport, $iframe, options){
// (i.e., is the first visible one).
if (shouldLookForFirstVisibleColumn) {
while (rect.bottom >= frameDimensions.height) {
if (isRectVisible(rect, frameDimensions)) {
if (isRectVisible(rect, frameDimensions, isVwm)) {
break;
}
offsetRectangle(rect, columnFullWidth, -frameDimensions.height);
Expand All @@ -426,9 +449,15 @@ ReadiumSDK.Views.CfiNavigationLogic = function($viewport, $iframe, options){
* @param {number} frameHeight
* @param {number} columnFullWidth
* @param {boolean} isRtl
* @param {boolean} isVwm isVerticalWritingMode
*/
function trimRectanglesByVertOffset(
rects, verticalOffset, frameHeight, columnFullWidth, isRtl) {
rects, verticalOffset, frameHeight, columnFullWidth, isRtl, isVwm) {

//TODO: Support vertical writing mode
if (isVwm) {
return;
}

var totalHeight = _.reduce(rects, function(prev, cur) {
return prev + cur.height;
Expand Down

0 comments on commit 9e4dfb7

Please sign in to comment.