From 9da41e175681ee8faff0ba6dacd2e7821a7959d3 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 19 Jul 2012 00:23:19 +0530 Subject: [PATCH] Paged display: Fix handling of top margin --- resources/compiled_coffeescript.zip | Bin 46282 -> 48348 bytes src/calibre/ebooks/oeb/display/paged.coffee | 32 ++++++++++++++++++-- src/calibre/ebooks/pdf/writer.py | 1 + src/calibre/gui2/viewer/documentview.py | 2 ++ src/calibre/gui2/viewer/printing.py | 1 + 5 files changed, 33 insertions(+), 3 deletions(-) diff --git a/resources/compiled_coffeescript.zip b/resources/compiled_coffeescript.zip index 8e6bbbaf23de88da9b5faff5f6ab46c74b701400..57049c6da7aa36acf37bf6bdff76bcbcf433d58b 100644 GIT binary patch delta 1358 zcmah}O=uHA6fT=~TVqJHO`9~Oc1kh3Nz<6bKl}+=N)>6LwN5Q*edBw5qk9?B6t(Tqu@n6iBR3y=_Z>} z|6<&eW0q!T6B%EbpJxZ;{NsseQ`1Bg?j7xh50$o8CyLBLgm5OS22*mfAPG4fmlDc+ zHm9N}$`wTMloe^C1p9k?or;i1#T)EktD^^=v~*l3Cd2@WXSGKZa@t!F()wF1h*+g4 z2xZfVPbWlGKwj@|VPVVC(M;T7&E9861WCux2+hLAivhSE^0ufMLBOzdwewodAeb+x z8Jv>!CY0nfl6lM ze020i3y3(*X?Zk8UXk;#>TpAP-!S~XX@%$eTzk7z(MZim1A;O}&2U-JssJH1$Gvl=2Do-8TZH{C{NG=ix3Jz;a zE_gZ6?2rNoO9m8{;3Ip)Lx^Ar^+*4S4|iDvew(MA(l-3GHbOj)m3KUMBTmnnSZkGN z7ErybWJ7OBHO*+q#ITx6qZI|`q)k6_YgYfezS+gDy(Z1`FDAN-S^xk5 delta 177 zcmccflj+n;CW!!VW)=|!5HPoS<{ + if false and this.document_margins is null + for sheet in document.styleSheets + for rule in sheet.rules + if rule.type == CSSRule.PAGE_RULE + for prop in ['margin-top', 'margin-bottom', 'margin-left', 'margin-right'] + val = rule.style.getPropertyValue(prop) + if val + log(val) set_geometry: (cols_per_screen=1, margin_top=20, margin_side=40, margin_bottom=20) -> this.margin_top = margin_top @@ -36,6 +47,7 @@ class PagedDisplay layout: () -> # start_time = new Date().getTime() body_style = window.getComputedStyle(document.body) + bs = document.body.style # When laying body out in columns, webkit bleeds the top margin of the # first block element out above the columns, leading to an extra top # margin for the page. We compensate for that here. Computing the @@ -43,8 +55,10 @@ class PagedDisplay # it before the column layout is applied. first_layout = false if not this.in_paged_mode - document.body.style.marginTop = '0px' + bs.setProperty('margin-top', '0px') extra_margin = document.body.getBoundingClientRect().top + if extra_margin <= this.margin_top + extra_margin = 0 margin_top = (this.margin_top - extra_margin) + 'px' # Check if the current document is a full screen layout like # cover, if so we treat it specially. @@ -78,7 +92,6 @@ class PagedDisplay this.screen_width = this.page_width * this.cols_per_screen fgcolor = body_style.getPropertyValue('color') - bs = document.body.style bs.setProperty('-webkit-column-gap', (2*sm)+'px') bs.setProperty('-webkit-column-width', col_width+'px') @@ -101,7 +114,7 @@ class PagedDisplay # Convert page-breaks to column-breaks for sheet in document.styleSheets for rule in sheet.rules - if rule.type == 1 # CSSStyleRule + if rule.type == CSSRule.STYLE_RULE for prop in ['page-break-before', 'page-break-after', 'page-break-inside'] val = rule.style.getPropertyValue(prop) if val @@ -144,6 +157,19 @@ class PagedDisplay img.style.setProperty('max-width', max_width+'px') calibre_utils.store(img, 'width-limited', true) + check_top_margin: () -> + # This is needed to handle the case when a descendant of body specifies + # a top margin as a percentage, which messes up the top margin + # calculations above + tm = document.body.getBoundingClientRect().top + if tm != this.margin_top + document.body.style.setProperty('margin-top', '0px') + tm = document.body.getBoundingClientRect().top + if tm <= this.margin_top + tm = 0 + m = this.margin_top - tm + document.body.style.setProperty('margin-top', m+'px') + scroll_to_pos: (frac) -> # Scroll to the position represented by frac (number between 0 and 1) xpos = Math.floor(document.body.scrollWidth * frac) diff --git a/src/calibre/ebooks/pdf/writer.py b/src/calibre/ebooks/pdf/writer.py index 51b03f96c5..2b3cfd7a4a 100644 --- a/src/calibre/ebooks/pdf/writer.py +++ b/src/calibre/ebooks/pdf/writer.py @@ -202,6 +202,7 @@ class PDFWriter(QObject): # {{{ paged_display.set_geometry(1, 0, 0, 0); paged_display.layout(); paged_display.fit_images(); + paged_display.check_top_margin(); ''') mf = self.view.page().mainFrame() while True: diff --git a/src/calibre/gui2/viewer/documentview.py b/src/calibre/gui2/viewer/documentview.py index ec4584121e..554019d2d1 100644 --- a/src/calibre/gui2/viewer/documentview.py +++ b/src/calibre/gui2/viewer/documentview.py @@ -179,6 +179,7 @@ class Document(QWebPage): # {{{ fset=_pass_json_value_setter) def after_load(self): + self.javascript('window.paged_display.read_document_margins()') self.set_bottom_padding(0) self.fit_images() self.init_hyphenate() @@ -228,6 +229,7 @@ class Document(QWebPage): # {{{ sz.setWidth(scroll_width+side_margin) self.setPreferredContentsSize(sz) self.javascript('window.paged_display.fit_images()') + self.javascript('window.paged_display.check_top_margin()') @property def column_boundaries(self): diff --git a/src/calibre/gui2/viewer/printing.py b/src/calibre/gui2/viewer/printing.py index f860edb12c..476cd949f5 100644 --- a/src/calibre/gui2/viewer/printing.py +++ b/src/calibre/gui2/viewer/printing.py @@ -71,6 +71,7 @@ class Printing(QObject): paged_display.set_geometry(1, 0, 0, 0); paged_display.layout(); paged_display.fit_images(); + paged_display.check_top_margin(); ''') while True: