From 9c244a873b0f819fda54edea84a26e14f959a6c2 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 6 Sep 2014 21:14:21 +0530 Subject: [PATCH] E-book viewer: Try to detect when a chapter has a page break near the start resulting in a blank page and override the page break. Fixes #1366074 [Private bug](https://bugs.launchpad.net/calibre/+bug/1366074) --- resources/compiled_coffeescript.zip | Bin 92292 -> 93276 bytes src/calibre/ebooks/oeb/display/paged.coffee | 40 +++++++++++++++++--- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/resources/compiled_coffeescript.zip b/resources/compiled_coffeescript.zip index cdb6a36af848c58dcceb79f1613c7bbf98c09a11..8547c11ebe6666052303a33902e138f031a9fae4 100644 GIT binary patch delta 1006 zcmZ`&-Ahwp824?=!Lq42ZRSU>WOJHKcXs9nGQ-vgMYnV-Dzn3OKHPFmn`f4YSrFk( zR6ND21txY=RAf7nAK@R+U3%3;UDQnkQFIYRWOUv$iy8D@Jn%lh-}8Ha&-0vfWljI) zwSNA_lqvqu>cx{E-y4OjETaH3Q*$L!G%+l5d?XqZYH=hq%ni$-gv`m}1zBmGzJ<1D z5a9B3Im_2#elQuATZ~veJa*%uP(qB$c8%zvLYoj}sc?OZ5!zB6a4luGXq9-s8#~gR zaZMyDK}V_*Hd48b2~kca;+W5bH*@i1EJoAgEk-qNwMuX{vOrIEA(=HHLw>rNONf&C zz8DkFlBdO}6XtFgOhhGYvr)vMLcBdbD2Tz)A+Z`)xZT*N-t0l|@gV2(AM#?W6{neh zF^|8i%j^3ue5A+k@dg6zzQbI*FA(fG>It^{eVSM`9;3a2QnC|9gF8scZdB@^FQh#j z3&oP6=7F|J^L3{Jb#_@CmTegfxR`fPS-%s`&td2qV%+(tAVMLJv2|N&!B$183Asusb(WoW2Jd`DF#948=6~qn(?ULU zJ}83^3uVeZ#jisS`25_f#M%DRYTgQdbwyH?FqV{6)Be%^AKX-=28=4NcUc} z556qrn3-PgjK)bfL(80yyX-(tXj^6s&R%Xnh2iaNCCn_BBNkSdBWkp+G$2;#TY*;P z>-XY;e|{xm5zvWkFMq;BvXD#)h|w*Y5DA%40eK}Lr>@kZ-7v9=s780cdK25|M@BLv zqCCTC31$7-Hb%}#$V~2v=#kCg5?S8iWSne>Wg4Y$Q!_7w9Stn&YLexKz4_!MHwUDmypTy2!Vz+jXx`QabQ$=hdiZjPNfiE*>|yd**X{G9mm z%#@N09fjol(!7$%t?TzrmTTkK9JqmpRW~I+ximL5uS72?Kc!MHEwiYp4~C&ije z0Sv%e6l@g?KmwC}W_wMZGTUJCq1i%{Em!hQzBk)&^7O6NlYh?RogBMeaMCW8&Gy?{ znI|9JscA-&H!hZ#v{!QSmL(cIMWs2ZKnFm?efN4W8c&|KHv>ri-)qKbyxDBu zWV^}yiQJnn|KZO@3ykR>(itO}KK__~F^y4ldO`-H5z~FH=~FTo-I-iOrr*tA^kzP- z$~gUiCZp_hk4#1Zj for script in parent.getElementsByTagName('script') eval(script.text || script.textContent || script.innerHTML || '') +first_child = (parent) -> + c = parent.firstChild + count = 0 + while c?.nodeType != 1 and count < 20 + c = c?.nextSibling + count += 1 + if c?.nodeType == 1 + return c + return null + +has_start_text = (elem) -> + # Returns true if elem has some non-whitespace text before its first child + # element + for c in elem.childNodes + if c.nodeType not in [Node.TEXT_NODE, Node.COMMENT_NODE, Node.PROCESSING_INSTRUCTION_NODE] + break + if c.nodeType == Node.TEXT_NODE and c.nodeValue != null and /\S/.test(c.nodeValue) + return true + return false + class PagedDisplay # This class is a namespace to expose functions via the # window.paged_display object. The most important functions are: @@ -144,13 +164,21 @@ class PagedDisplay # Otherwise, you could end up with an effective negative margin, I dont # understand exactly why, but see: # https://bugs.launchpad.net/calibre/+bug/1082640 for an example - c = document.body.firstChild - count = 0 - while c?.nodeType != 1 and count < 20 - c = c?.nextSibling - count += 1 - if c?.nodeType == 1 + c = first_child(document.body) + if c != null c.style.setProperty('-webkit-margin-before', '0') + # Remove page breaks on the first few elements to prevent blank pages + # at the start of a chapter + c.style.setProperty('-webkit-column-break-before', 'avoid') + if c.tagName?.toLowerCase() == 'div' + c2 = first_child(c) + if c2 != null and not has_start_text(c) + # Common pattern of all content being enclosed in a wrapper + #
, see for example: https://bugs.launchpad.net/bugs/1366074 + # In this case, we also modify the first child of the div + # as long as there was no text before it. + c2.style.setProperty('-webkit-column-break-before', 'avoid') + this.effective_margin_top = this.margin_top this.effective_margin_bottom = this.margin_bottom