-
Notifications
You must be signed in to change notification settings - Fork 101
/
reflowable_view.js
1218 lines (941 loc) · 43.9 KB
/
reflowable_view.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// LauncherOSX
//
// Created by Boris Schneiderman.
// Copyright (c) 2014 Readium Foundation and/or its licensees. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation and/or
// other materials provided with the distribution.
// 3. Neither the name of the organization nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.
define(["../globals", "jquery", "underscore", "eventEmitter", "../models/bookmark_data", "./cfi_navigation_logic",
"../models/current_pages_info", "../helpers", "../models/page_open_request",
"../models/viewer_settings", "ResizeSensor"],
function(Globals, $, _, EventEmitter, BookmarkData, CfiNavigationLogic,
CurrentPagesInfo, Helpers, PageOpenRequest,
ViewerSettings, ResizeSensor) {
/**
* Renders reflowable content using CSS columns
* @param options
* @constructor
*/
var ReflowableView = function(options, reader){
$.extend(this, new EventEmitter());
var self = this;
var _$viewport = $(options.$viewport);
var _spine = options.spine;
var _userStyles = options.userStyles;
var _bookStyles = options.bookStyles;
var _iframeLoader = options.iframeLoader;
var _currentSpineItem;
var _isWaitingFrameRender = false;
var _deferredPageRequest;
var _fontSize = 100;
var _fontSelection = 0;
var _$contentFrame;
var _navigationLogic;
var _$el;
var _$iframe;
var _$epubHtml;
var _lastPageRequest = undefined;
var _cfiClassBlacklist = ["cfi-marker", "mo-cfi-highlight", "resize-sensor", "resize-sensor-expand", "resize-sensor-shrink", "resize-sensor-inner", "js-hypothesis-config", "js-hypothesis-embed"];
var _cfiElementBlacklist = ["hypothesis-adder"];
var _cfiIdBlacklist = ["MathJax_Message", "MathJax_SVG_Hidden"];
var _$htmlBody;
var _htmlBodyIsVerticalWritingMode;
var _htmlBodyIsLTRDirection;
var _htmlBodyIsLTRWritingMode;
var _currentOpacity = -1;
var _lastViewPortSize = {
width: undefined,
height: undefined
};
var _lastBodySize = {
width: undefined,
height: undefined
};
var _paginationInfo = {
visibleColumnCount : 2,
columnGap : 20,
columnMaxWidth: 550,
columnMinWidth: 400,
spreadCount : 0,
currentSpreadIndex : 0,
currentPageIndex: 0,
columnWidth : undefined,
pageOffset : 0,
columnCount: 0
};
this.render = function(){
var template = Helpers.loadTemplate("reflowable_book_frame", {});
_$el = $(template);
_$viewport.append(_$el);
var settings = reader.viewerSettings();
if (!settings || typeof settings.enableGPUHardwareAccelerationCSS3D === "undefined")
{
//defaults
settings = new ViewerSettings({});
}
if (settings.enableGPUHardwareAccelerationCSS3D) {
// This fixes rendering issues with WebView (native apps), which clips content embedded in iframes unless GPU hardware acceleration is enabled for CSS rendering.
_$el.css("transform", "translateZ(0)");
}
// See ReaderView.handleViewportResize
// var lazyResize = _.debounce(self.onViewportResize, 100);
// $(window).on("resize.ReadiumSDK.reflowableView", _.bind(lazyResize, self));
renderIframe();
return self;
};
function setFrameSizesToRectangle(rectangle) {
_$contentFrame.css("left", rectangle.left + "px");
_$contentFrame.css("top", rectangle.top + "px");
_$contentFrame.css("right", rectangle.right + "px");
_$contentFrame.css("bottom", rectangle.bottom + "px");
}
this.remove = function() {
//$(window).off("resize.ReadiumSDK.reflowableView");
_$el.remove();
};
this.isReflowable = function() {
return true;
};
this.onViewportResize = function() {
if(updateViewportSize()) {
updatePagination();
}
};
var _viewSettings = undefined;
this.setViewSettings = function(settings, docWillChange) {
_viewSettings = settings;
_paginationInfo.columnGap = settings.columnGap;
_paginationInfo.columnMaxWidth = settings.columnMaxWidth;
_paginationInfo.columnMinWidth = settings.columnMinWidth;
_fontSize = settings.fontSize;
_fontSelection = settings.fontSelection;
updateViewportSize();
if (!docWillChange) {
updateColumnGap();
updateHtmlFontInfo();
}
};
function getFrameDimensions() {
return {
width: _$iframe[0].clientWidth,
height: _$iframe[0].clientHeight
};
}
function getPageOffset() {
if (_paginationInfo.rightToLeft && !_paginationInfo.isVerticalWritingMode) {
return -_paginationInfo.pageOffset;
}
return _paginationInfo.pageOffset;
}
function getPaginationOffsets() {
var offset = getPageOffset();
if (_paginationInfo.isVerticalWritingMode) {
return {
top: offset,
left: 0
};
}
return {
top: 0,
left: offset
};
}
function renderIframe() {
if (_$contentFrame) {
//destroy old contentFrame
_$contentFrame.remove();
}
var template = Helpers.loadTemplate("reflowable_book_page_frame", {});
var $bookFrame = $(template);
$bookFrame = _$el.append($bookFrame);
_$contentFrame = $("#reflowable-content-frame", $bookFrame);
_$iframe = $("#epubContentIframe", $bookFrame);
_$iframe.css("left", "");
_$iframe.css("right", "");
_$iframe.css("position", "relative");
//_$iframe.css(_spine.isLeftToRight() ? "left" : "right", "0px");
_$iframe.css("overflow", "hidden");
_navigationLogic = new CfiNavigationLogic({
$iframe: _$iframe,
frameDimensionsGetter: getFrameDimensions,
paginationInfo: _paginationInfo,
paginationOffsetsGetter: getPaginationOffsets,
classBlacklist: _cfiClassBlacklist,
elementBlacklist: _cfiElementBlacklist,
idBlacklist: _cfiIdBlacklist
});
}
function loadSpineItem(spineItem) {
if(_currentSpineItem != spineItem) {
//create & append iframe to container frame
renderIframe();
if (_currentSpineItem) {
Globals.logEvent("CONTENT_DOCUMENT_UNLOADED", "EMIT", "reflowable_view.js [ " + _currentSpineItem.href + " ]");
self.emit(Globals.Events.CONTENT_DOCUMENT_UNLOADED, _$iframe, _currentSpineItem);
}
self.resetCurrentPosition();
_paginationInfo.pageOffset = 0;
_paginationInfo.currentSpreadIndex = 0;
_paginationInfo.currentPageIndex = 0;
_currentSpineItem = spineItem;
// TODO: this is a dirty hack!!
_currentSpineItem.paginationInfo = _paginationInfo;
_isWaitingFrameRender = true;
var src = _spine.package.resolveRelativeUrl(spineItem.href);
Globals.logEvent("CONTENT_DOCUMENT_LOAD_START", "EMIT", "reflowable_view.js [ " + spineItem.href + " -- " + src + " ]");
self.emit(Globals.Events.CONTENT_DOCUMENT_LOAD_START, _$iframe, spineItem);
_$iframe.css("opacity", "0.01");
_iframeLoader.loadIframe(_$iframe[0], src, onIFrameLoad, self, {spineItem : spineItem});
}
}
function updateHtmlFontInfo() {
if(_$epubHtml) {
var i = _fontSelection;
var useDefault = !reader.fonts || !reader.fonts.length || i <= 0 || (i-1) >= reader.fonts.length;
var font = (useDefault ?
{} :
reader.fonts[i - 1]);
Helpers.UpdateHtmlFontAttributes(_$epubHtml, _fontSize, font, function() {self.applyStyles();});
}
}
function updateColumnGap() {
if(_$epubHtml) {
_$epubHtml.css("column-gap", _paginationInfo.columnGap + "px");
}
}
function onIFrameLoad(success) {
_isWaitingFrameRender = false;
//while we where loading frame new request came
if(_deferredPageRequest && _deferredPageRequest.spineItem != _currentSpineItem) {
loadSpineItem(_deferredPageRequest.spineItem);
return;
}
if(!success) {
_$iframe.css("opacity", "1");
_deferredPageRequest = undefined;
return;
}
Globals.logEvent("CONTENT_DOCUMENT_LOADED", "EMIT", "reflowable_view.js [ " + _currentSpineItem.href + " ]");
self.emit(Globals.Events.CONTENT_DOCUMENT_LOADED, _$iframe, _currentSpineItem);
var epubContentDocument = _$iframe[0].contentDocument;
_$epubHtml = $("html", epubContentDocument);
_$htmlBody = $("body", _$epubHtml);
// TODO: how to address this correctly across all the affected platforms?!
// Video surface sometimes (depends on the video codec) disappears from CSS column (i.e. reflow page) during playback
// (audio continues to play normally, but video canvas is invisible).
// https://github.com/readium/readium-js-viewer/issues/265#issuecomment-73018762
// ...Meanwhile, reverting https://github.com/readium/readium-js-viewer/issues/239
// by commenting the code below (which unfortunately only works with some GPU / codec configurations,
// but actually fails on several other machines!!)
/*
if(window.chrome
&& window.navigator.vendor === "Google Inc.") // TODO: Opera (WebKit) sometimes suffers from this rendering bug too (depends on the video codec), but unfortunately GPU-accelerated rendering makes the video controls unresponsive!!
{
$("video", _$htmlBody).css("transform", "translateZ(0)");
}
*/
_htmlBodyIsVerticalWritingMode = false;
_htmlBodyIsLTRDirection = true;
_htmlBodyIsLTRWritingMode = undefined;
var win = _$iframe[0].contentDocument.defaultView || _$iframe[0].contentWindow;
//Helpers.isIframeAlive
var htmlBodyComputedStyle = win.getComputedStyle(_$htmlBody[0], null);
if (htmlBodyComputedStyle)
{
_htmlBodyIsLTRDirection = htmlBodyComputedStyle.direction === "ltr";
var writingMode = undefined;
if (htmlBodyComputedStyle.getPropertyValue)
{
writingMode = htmlBodyComputedStyle.getPropertyValue("-webkit-writing-mode") || htmlBodyComputedStyle.getPropertyValue("-moz-writing-mode") || htmlBodyComputedStyle.getPropertyValue("-ms-writing-mode") || htmlBodyComputedStyle.getPropertyValue("-o-writing-mode") || htmlBodyComputedStyle.getPropertyValue("-epub-writing-mode") || htmlBodyComputedStyle.getPropertyValue("writing-mode");
}
else
{
writingMode = htmlBodyComputedStyle.webkitWritingMode || htmlBodyComputedStyle.mozWritingMode || htmlBodyComputedStyle.msWritingMode || htmlBodyComputedStyle.oWritingMode || htmlBodyComputedStyle.epubWritingMode || htmlBodyComputedStyle.writingMode;
}
if (writingMode)
{
_htmlBodyIsLTRWritingMode = writingMode.indexOf("-lr") >= 0; // || writingMode.indexOf("horizontal-") >= 0; we need explicit!
if (writingMode.indexOf("vertical") >= 0 || writingMode.indexOf("tb-") >= 0 || writingMode.indexOf("bt-") >= 0)
{
_htmlBodyIsVerticalWritingMode = true;
}
}
}
if (_htmlBodyIsLTRDirection)
{
if (_$htmlBody[0].getAttribute("dir") === "rtl" || _$epubHtml[0].getAttribute("dir") === "rtl")
{
_htmlBodyIsLTRDirection = false;
}
}
// Some EPUBs may not have explicit RTL content direction (via CSS "direction" property or @dir attribute) despite having a RTL page progression direction. Readium consequently tweaks the HTML in order to restore the correct block flow in the browser renderer, resulting in the appropriate CSS columnisation (which is used to emulate pagination).
if (!_spine.isLeftToRight() && _htmlBodyIsLTRDirection && !_htmlBodyIsVerticalWritingMode)
{
_$htmlBody[0].setAttribute("dir", "rtl");
_htmlBodyIsLTRDirection = false;
_htmlBodyIsLTRWritingMode = false;
}
_paginationInfo.isVerticalWritingMode = _htmlBodyIsVerticalWritingMode;
hideBook();
_$iframe.css("opacity", "1");
updateViewportSize();
_$epubHtml.css("height", _lastViewPortSize.height + "px");
_$epubHtml.css("position", "relative");
_$epubHtml.css("margin", "0");
_$epubHtml.css("padding", "0");
_$epubHtml.css("column-axis", (_htmlBodyIsVerticalWritingMode ? "vertical" : "horizontal"));
//
// /////////
// //Columns Debugging
//
// _$epubHtml.css("column-rule-color", "red");
// _$epubHtml.css("column-rule-style", "dashed");
// _$epubHtml.css("column-rule-width", "1px");
// _$epubHtml.css("background-color", '#b0c4de');
//
// ////
self.applyBookStyles();
resizeImages();
updateColumnGap();
updateHtmlFontInfo();
}
this.applyStyles = function() {
Helpers.setStyles(_userStyles.getStyles(), _$el.parent());
//because left, top, bottom, right setting ignores padding of parent container
//we have to take it to account manually
var elementMargins = Helpers.Margins.fromElement(_$el);
setFrameSizesToRectangle(elementMargins.padding);
updateViewportSize();
updatePagination();
};
this.applyBookStyles = function() {
if(_$epubHtml) { // implies _$iframe
Helpers.setStyles(_bookStyles.getStyles(), _$iframe[0].contentDocument); //_$epubHtml
}
};
function openDeferredElement() {
if(!_deferredPageRequest) {
return;
}
var deferredData = _deferredPageRequest;
_deferredPageRequest = undefined;
self.openPage(deferredData);
}
function _openPageInternal(pageRequest) {
if(_isWaitingFrameRender) {
_deferredPageRequest = pageRequest;
return false;
}
// if no spine item specified we are talking about current spine item
if(pageRequest.spineItem && pageRequest.spineItem != _currentSpineItem) {
_deferredPageRequest = pageRequest;
loadSpineItem(pageRequest.spineItem);
return true;
}
var pageIndex = undefined;
if(pageRequest.spineItemPageIndex !== undefined) {
pageIndex = pageRequest.spineItemPageIndex;
}
else if(pageRequest.elementId) {
pageIndex = _paginationInfo.currentPageIndex + _navigationLogic.getPageIndexDeltaForElementId(pageRequest.elementId);
}
else if(pageRequest.firstVisibleCfi && pageRequest.lastVisibleCfi) {
var firstPageIndex;
var lastPageIndex;
try
{
firstPageIndex = _navigationLogic.getPageIndexDeltaForCfi(pageRequest.firstVisibleCfi,
_cfiClassBlacklist,
_cfiElementBlacklist,
_cfiIdBlacklist);
}
catch (e)
{
firstPageIndex = 0;
console.error(e);
}
try
{
lastPageIndex = _navigationLogic.getPageIndexDeltaForCfi(pageRequest.lastVisibleCfi,
_cfiClassBlacklist,
_cfiElementBlacklist,
_cfiIdBlacklist);
}
catch (e)
{
lastPageIndex = 0;
console.error(e);
}
// Go to the page in the middle of the two elements
pageIndex = _paginationInfo.currentPageIndex + Math.round((firstPageIndex + lastPageIndex) / 2);
}
else if(pageRequest.elementCfi) {
try
{
pageIndex = _paginationInfo.currentPageIndex + _navigationLogic.getPageIndexDeltaForCfi(pageRequest.elementCfi,
_cfiClassBlacklist,
_cfiElementBlacklist,
_cfiIdBlacklist);
}
catch (e)
{
pageIndex = 0;
console.error(e);
}
}
else if(pageRequest.firstPage) {
pageIndex = 0;
}
else if(pageRequest.lastPage) {
pageIndex = _paginationInfo.columnCount - 1;
}
else {
console.debug("No criteria in pageRequest");
pageIndex = 0;
}
if (pageIndex < 0 || pageIndex > _paginationInfo.columnCount) {
console.log('Illegal pageIndex value: ', pageIndex, 'column count is ', _paginationInfo.columnCount);
pageIndex = pageIndex < 0 ? 0 : _paginationInfo.columnCount;
}
_paginationInfo.currentPageIndex = pageIndex;
_paginationInfo.currentSpreadIndex = Math.floor(pageIndex / _paginationInfo.visibleColumnCount) ;
onPaginationChanged(pageRequest.initiator, pageRequest.spineItem, pageRequest.elementId);
return true;
}
this.openPage = function(pageRequest) {
// Go to request page, it will save the new position in onPaginationChanged
_openPageInternal(pageRequest);
// Save it for when pagination is updated
_lastPageRequest = pageRequest;
};
this.resetCurrentPosition = function() {
_lastPageRequest = undefined;
};
this.saveCurrentPosition = function() {
// If there's a deferred page request, there's no point in saving the current position
// as it's going to change soon
if (_deferredPageRequest) {
return;
}
var _firstVisibleCfi = self.getFirstVisibleCfi();
var _lastVisibleCfi = self.getLastVisibleCfi();
_lastPageRequest = new PageOpenRequest(_currentSpineItem, self);
_lastPageRequest.setFirstAndLastVisibleCfi(_firstVisibleCfi.contentCFI, _lastVisibleCfi.contentCFI);
};
this.restoreCurrentPosition = function() {
if (_lastPageRequest) {
_openPageInternal(_lastPageRequest);
}
};
function redraw() {
var offsetVal = -_paginationInfo.pageOffset + "px";
if (_htmlBodyIsVerticalWritingMode)
{
_$epubHtml.css("top", offsetVal);
}
else
{
var ltr = _htmlBodyIsLTRDirection || _htmlBodyIsLTRWritingMode;
_$epubHtml.css("left", ltr ? offsetVal : "");
_$epubHtml.css("right", !ltr ? offsetVal : "");
}
showBook(); // as it's no longer hidden by shifting the position
}
function updateViewportSize() {
var newWidth = _$contentFrame.width();
// Ensure that the new viewport width is always even numbered
// this is to prevent a rendering inconsistency between browsers when odd-numbered bounds are used for CSS columns
// See https://github.com/readium/readium-shared-js/issues/37
newWidth -= newWidth % 2;
var newHeight = _$contentFrame.height();
if(_lastViewPortSize.width !== newWidth || _lastViewPortSize.height !== newHeight){
_lastViewPortSize.width = newWidth;
_lastViewPortSize.height = newHeight;
return true;
}
return false;
}
function onPaginationChanged_(initiator, paginationRequest_spineItem, paginationRequest_elementId) {
_paginationInfo.currentPageIndex = _paginationInfo.currentSpreadIndex * _paginationInfo.visibleColumnCount;
_paginationInfo.pageOffset = (_paginationInfo.columnWidth + _paginationInfo.columnGap) * _paginationInfo.visibleColumnCount * _paginationInfo.currentSpreadIndex;
redraw();
_.defer(function () {
if (_lastPageRequest == undefined) {
self.saveCurrentPosition();
}
Globals.logEvent("InternalEvents.CURRENT_VIEW_PAGINATION_CHANGED", "EMIT", "reflowable_view.js");
self.emit(Globals.InternalEvents.CURRENT_VIEW_PAGINATION_CHANGED, {
paginationInfo: self.getPaginationInfo(),
initiator: initiator,
spineItem: paginationRequest_spineItem,
elementId: paginationRequest_elementId
});
});
}
var onPaginationChanged = _.debounce(onPaginationChanged_, 100);
this.openPagePrev = function (initiator) {
if(!_currentSpineItem) {
return;
}
if(_paginationInfo.currentSpreadIndex > 0) {
// Page will change, the current position is not valid any more
// Reset it so it's saved next time onPaginationChanged is called
this.resetCurrentPosition();
_paginationInfo.currentSpreadIndex--;
onPaginationChanged(initiator, _currentSpineItem);
}
else {
var prevSpineItem = _spine.prevItem(_currentSpineItem, true);
if(prevSpineItem) {
var pageRequest = new PageOpenRequest(prevSpineItem, initiator);
pageRequest.setLastPage();
self.openPage(pageRequest);
}
}
};
this.openPageNext = function (initiator) {
if(!_currentSpineItem) {
return;
}
if(_paginationInfo.currentSpreadIndex < _paginationInfo.spreadCount - 1) {
// Page will change, the current position is not valid any more
// Reset it so it's saved next time onPaginationChanged is called
this.resetCurrentPosition();
_paginationInfo.currentSpreadIndex++;
onPaginationChanged(initiator, _currentSpineItem);
}
else {
var nextSpineItem = _spine.nextItem(_currentSpineItem, true);
if(nextSpineItem) {
var pageRequest = new PageOpenRequest(nextSpineItem, initiator);
pageRequest.setFirstPage();
self.openPage(pageRequest);
}
}
};
function updatePagination_() {
// At 100% font-size = 16px (on HTML, not body or descendant markup!)
var MAXW = _paginationInfo.columnMaxWidth;
var MINW = _paginationInfo.columnMinWidth;
var isDoublePageSyntheticSpread = Helpers.deduceSyntheticSpread(_$viewport, _currentSpineItem, _viewSettings);
var forced = (isDoublePageSyntheticSpread === false) || (isDoublePageSyntheticSpread === true);
// excludes 0 and 1 falsy/truthy values which denote non-forced result
// console.debug("isDoublePageSyntheticSpread: " + isDoublePageSyntheticSpread);
// console.debug("forced: " + forced);
//
if (isDoublePageSyntheticSpread === 0)
{
isDoublePageSyntheticSpread = 1; // try double page, will shrink if doesn't fit
// console.debug("TRYING SPREAD INSTEAD OF SINGLE...");
}
_paginationInfo.visibleColumnCount = isDoublePageSyntheticSpread ? 2 : 1;
if (_htmlBodyIsVerticalWritingMode)
{
MAXW *= 2;
isDoublePageSyntheticSpread = false;
forced = true;
_paginationInfo.visibleColumnCount = 1;
// console.debug("Vertical Writing Mode => single CSS column, but behaves as if two-page spread");
}
if(!_$epubHtml) {
return;
}
hideBook(); // shiftBookOfScreen();
// "borderLeft" is the blank vertical strip (e.g. 40px wide) where the left-arrow button resides, i.e. previous page command
var borderLeft = parseInt(_$viewport.css("border-left-width"));
// The "columnGap" separates two consecutive columns in a 2-page synthetic spread (e.g. 60px wide).
// This middle gap (blank vertical strip) actually corresponds to the left page's right-most margin, combined with the right page's left-most margin.
// So, "adjustedGapLeft" is half of the center strip...
var adjustedGapLeft = _paginationInfo.columnGap/2;
// ...but we include the "borderLeft" strip to avoid wasting valuable rendering real-estate:
adjustedGapLeft = Math.max(0, adjustedGapLeft-borderLeft);
// Typically, "adjustedGapLeft" is zero because the space available for the 'previous page' button is wider than half of the column gap!
// "borderRight" is the blank vertical strip (e.g. 40px wide) where the right-arrow button resides, i.e. next page command
var borderRight = parseInt(_$viewport.css("border-right-width"));
// The "columnGap" separates two consecutive columns in a 2-page synthetic spread (e.g. 60px wide).
// This middle gap (blank vertical strip) actually corresponds to the left page's right-most margin, combined with the right page's left-most margin.
// So, "adjustedGapRight" is half of the center strip...
var adjustedGapRight = _paginationInfo.columnGap/2;
// ...but we include the "borderRight" strip to avoid wasting valuable rendering real-estate:
adjustedGapRight = Math.max(0, adjustedGapRight-borderRight);
// Typically, "adjustedGapRight" is zero because the space available for the 'next page' button is wider than half of the column gap! (in other words, the right-most and left-most page margins are fully included in the strips reserved for the arrow buttons)
// Note that "availableWidth" does not contain "borderLeft" and "borderRight" (.width() excludes the padding and border and margin in the CSS box model of div#epub-reader-frame)
var availableWidth = _$viewport.width();
// ...So, we substract the page margins and button spacing to obtain the width available for actual text:
var textWidth = availableWidth - adjustedGapLeft - adjustedGapRight;
// ...and if we have 2 pages / columns, then we split the text width in half:
if (isDoublePageSyntheticSpread)
{
textWidth = (textWidth - _paginationInfo.columnGap) * 0.5;
}
var filler = 0;
// Now, if the resulting width actually available for document content is greater than the maximum allowed value, we create even more left+right blank space to "compress" the horizontal run of text.
if (textWidth > MAXW)
{
var eachPageColumnReduction = textWidth - MAXW;
// if we have a 2-page synthetic spread, then we "trim" left and right sides by adding "eachPageColumnReduction" blank space.
// if we have a single page / column, then this loss of text real estate is shared between right and left sides
filler = Math.floor(eachPageColumnReduction * (isDoublePageSyntheticSpread ? 1 : 0.5));
}
// Let's check whether a narrow two-page synthetic spread (impeded reabability) can be reduced down to a single page / column:
else if (!forced && textWidth < MINW && isDoublePageSyntheticSpread)
{
isDoublePageSyntheticSpread = false;
_paginationInfo.visibleColumnCount = 1;
textWidth = availableWidth - adjustedGapLeft - adjustedGapRight;
if (textWidth > MAXW)
{
filler = Math.floor((textWidth - MAXW) * 0.5);
}
}
_$el.css({"left": (filler+adjustedGapLeft + "px"), "right": (filler+adjustedGapRight + "px")});
updateViewportSize(); //_$contentFrame ==> _lastViewPortSize
var resultingColumnWidth = _$el.width();
if (isDoublePageSyntheticSpread) {
resultingColumnWidth = (resultingColumnWidth - _paginationInfo.columnGap) / 2;
}
resultingColumnWidth = Math.floor(resultingColumnWidth);
if ((resultingColumnWidth-1) > MAXW) {
console.debug("resultingColumnWidth > MAXW ! " + resultingColumnWidth + " > " + MAXW);
}
_$iframe.css("width", _lastViewPortSize.width + "px");
_$iframe.css("height", _lastViewPortSize.height + "px");
_$epubHtml.css("height", _lastViewPortSize.height + "px");
// below min- max- are required in vertical writing mode (height is not enough, in some cases...weird!)
_$epubHtml.css("min-height", _lastViewPortSize.height + "px");
_$epubHtml.css("max-height", _lastViewPortSize.height + "px");
//normalise spacing to avoid interference with column-isation
_$epubHtml.css('margin', 0);
_$epubHtml.css('padding', 0);
_$epubHtml.css('border', 0);
// In order for the ResizeSensor to work, the content body needs to be "positioned".
// This may be an issue since it changes the assumptions some content authors might make when positioning their content.
_$htmlBody.css('position', 'relative');
_$htmlBody.css('margin', 0);
_$htmlBody.css('padding', 0);
_paginationInfo.rightToLeft = _spine.isRightToLeft();
_paginationInfo.columnWidth = Math.round(((_htmlBodyIsVerticalWritingMode ? _lastViewPortSize.height : _lastViewPortSize.width) - _paginationInfo.columnGap * (_paginationInfo.visibleColumnCount - 1)) / _paginationInfo.visibleColumnCount);
var useColumnCountNotWidth = _paginationInfo.visibleColumnCount > 1; // column-count == 1 does not work in Chrome, and is not needed anyway (HTML width is full viewport width, no Firefox video flickering)
if (useColumnCountNotWidth) {
_$epubHtml.css("width", _lastViewPortSize.width + "px");
_$epubHtml.css("column-width", "auto");
_$epubHtml.css("column-count", _paginationInfo.visibleColumnCount);
} else {
_$epubHtml.css("width", (_htmlBodyIsVerticalWritingMode ? _lastViewPortSize.width : _paginationInfo.columnWidth) + "px");
_$epubHtml.css("column-count", "auto");
_$epubHtml.css("column-width", _paginationInfo.columnWidth + "px");
}
_$epubHtml.css("column-fill", "auto");
_$epubHtml.css({left: "0", right: "0", top: "0"});
Helpers.triggerLayout(_$iframe);
var dim = (_htmlBodyIsVerticalWritingMode ? _$epubHtml[0].scrollHeight : _$epubHtml[0].scrollWidth);
if (dim == 0) {
console.error("Document dimensions zero?!");
}
_paginationInfo.columnCount = (dim + _paginationInfo.columnGap) / (_paginationInfo.columnWidth + _paginationInfo.columnGap);
_paginationInfo.columnCount = Math.round(_paginationInfo.columnCount);
if (_paginationInfo.columnCount == 0) {
console.error("Column count zero?!");
}
var totalGaps = (_paginationInfo.columnCount-1) * _paginationInfo.columnGap;
var colWidthCheck = (dim - totalGaps) / _paginationInfo.columnCount;
colWidthCheck = Math.round(colWidthCheck);
if (colWidthCheck > _paginationInfo.columnWidth)
{
console.debug("ADJUST COLUMN");
console.log(_paginationInfo.columnWidth);
console.log(colWidthCheck);
_paginationInfo.columnWidth = colWidthCheck;
}
_paginationInfo.spreadCount = Math.ceil(_paginationInfo.columnCount / _paginationInfo.visibleColumnCount);
if(_paginationInfo.currentSpreadIndex >= _paginationInfo.spreadCount) {
_paginationInfo.currentSpreadIndex = _paginationInfo.spreadCount - 1;
}
if(_deferredPageRequest) {
//if there is a request for specific page we get here
openDeferredElement();
}
else {
// we get here on resizing the viewport
if (_lastPageRequest) {
// Make sure we stay on the same page after the content or the viewport
// has been resized
_paginationInfo.currentPageIndex = 0; // current page index is not stable, reset it
self.restoreCurrentPosition();
} else {
onPaginationChanged(self, _currentSpineItem); // => redraw() => showBook(), so the trick below is not needed
}
//onPaginationChanged(self, _currentSpineItem); // => redraw() => showBook(), so the trick below is not needed
// //We do this to force re-rendering of the document in the iframe.
// //There is a bug in WebView control with right to left columns layout - after resizing the window html document
// //is shifted in side the containing div. Hiding and showing the html element puts document in place.
// _$epubHtml.hide();
// setTimeout(function() {
// _$epubHtml.show();
// onPaginationChanged(self, _currentSpineItem); // => redraw() => showBook()
// }, 50);
}
// Only initializes the resize sensor once the content has been paginated once,
// to avoid the pagination process to trigger a resize event during its first
// execution, provoking a flicker
initResizeSensor();
}
var updatePagination = _.debounce(updatePagination_, 100);
function initResizeSensor() {
var bodyElement = _$htmlBody[0];
if (bodyElement.resizeSensor) {
return;
}
// We need to make sure the content has indeed be resized, especially
// the first time it is triggered
_lastBodySize.width = $(bodyElement).width();
_lastBodySize.height = $(bodyElement).height();
bodyElement.resizeSensor = new ResizeSensor(bodyElement, function() {
var newBodySize = {
width: $(bodyElement).width(),
height: $(bodyElement).height()
};
console.debug("ReflowableView content resized ...", newBodySize.width, newBodySize.height, _currentSpineItem.idref);
if (newBodySize.width != _lastBodySize.width || newBodySize.height != _lastBodySize.height) {
_lastBodySize.width = newBodySize.width;
_lastBodySize.height = newBodySize.height;
console.debug("... updating pagination.");
updatePagination();
} else {
console.debug("... ignored (identical dimensions).");
}
});
}
// function shiftBookOfScreen() {
//
// if(_spine.isLeftToRight()) {
// _$epubHtml.css("left", (_lastViewPortSize.width + 1000) + "px");
// }
// else {
// _$epubHtml.css("right", (_lastViewPortSize.width + 1000) + "px");
// }
// }
function hideBook()
{
if (_currentOpacity != -1) return; // already hidden
// css('opacity') produces invalid result in Firefox, when iframes are involved and when is called
// directly after set, i.e. after showBook(), see: https://github.com/jquery/jquery/issues/2622
//_currentOpacity = $epubHtml.css('opacity');
_currentOpacity = _$epubHtml[0].style.opacity;
_$epubHtml.css('opacity', "0");
}
function showBook()
{
if (_currentOpacity != -1)
{
_$epubHtml.css('opacity', _currentOpacity);
}
_currentOpacity = -1;
}
this.getPaginationInfo = function() {
var paginationInfo = new CurrentPagesInfo(_spine, false);
if(!_currentSpineItem) {
return paginationInfo;
}
var pageIndexes = getOpenPageIndexes();
for(var i = 0, count = pageIndexes.length; i < count; i++) {
paginationInfo.addOpenPage(pageIndexes[i], _paginationInfo.columnCount, _currentSpineItem.idref, _currentSpineItem.index);
}
return paginationInfo;
};
function getOpenPageIndexes() {
var indexes = [];
var currentPage = _paginationInfo.currentSpreadIndex * _paginationInfo.visibleColumnCount;
for(var i = 0; i < _paginationInfo.visibleColumnCount && (currentPage + i) < _paginationInfo.columnCount; i++) {
indexes.push(currentPage + i);
}
return indexes;
}
//we need this styles for css columnizer not to chop big images
function resizeImages() {
if(!_$epubHtml) {
return;
}
var $elem;
var height;
var width;
$('img, svg', _$epubHtml).each(function(){