From b82edf32f7b51b76368d9bd05cadb8acf025520a Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 19 Jul 2012 16:00:50 +0530 Subject: [PATCH] Paged display: Allow customization of the margins and number of pages per screen --- resources/compiled_coffeescript.zip | Bin 48348 -> 49166 bytes src/calibre/ebooks/oeb/display/paged.coffee | 41 +++- src/calibre/gui2/viewer/config.py | 15 ++ src/calibre/gui2/viewer/config.ui | 222 ++++++++++++++++++-- src/calibre/gui2/viewer/documentview.py | 12 ++ 5 files changed, 260 insertions(+), 30 deletions(-) diff --git a/resources/compiled_coffeescript.zip b/resources/compiled_coffeescript.zip index 57049c6da7aa36acf37bf6bdff76bcbcf433d58b..7ec0f242309ec5f872fe9e527e47e71eaa145256 100644 GIT binary patch delta 1194 zcmaJ>&rcIU6sFrB;!;Z~rCWjE0F`ZJ-AJPskfaF*~fZ0VGaZg)4k+eTsu z5D$3K1MlJyO!UG9#AqVKcsC{(W8%TH|9~eG56@Y^97fJ6BfBIigu; z92FJS!tjZkx@$a_zU3i0HVLv%l zTQ;b!eW~)S2}ZSh(TU5JJ+ET9N-UBmDzWD&P%M!`0c+x41a+XHTf`>1#$Za`($$KM zn}CfRdA%?%D~5r!!bFLvg(R&ZbwHRiF|2wA;G=g27JR+%);9saeSOgDza-JUVw&<# zi_95OLL&8oEv`)Vau7n}Gg5b%CAnXYd86DW~SYzU{Ud2aQQ=gSCzs%&r-W{Qxk{+sXXhe3! z-k_K!UlD35lpaf^(Eg~w2G`T(aMsm_UnN-e0oif$xJ0RXqFc>M5+J4b$dY~F^hjl)F#x3{QKO-pKZcjgSoa}@MA7RDz$aEG^t%h9n zIr`QZDp-p|J>#&+#dz5r6$%7{kAav7ovI(ONC6`U)arUcA@qb$ulInN0qK0*d@Ru1 z3Ao5784bLQgh1y7SVd>Izwuu;Lx(;wJ3CX{yA0%ZyI^=X&5qI{6#sXj7 Hvk%iB8_aQu delta 747 zcmZ8f&ubGw7)>|X-LT!n&~DQ7hwey`>_QUUg;FpC^de{x^&&`#G2-Tj-Dbn?CLW~t z<6>z+EDyZ4`WGlYiUz%T5WRTQL%e&bCvVNnkec>`*>Ao#Z{C~zW?npr{`@B_+*pam zvterLTACN{9MbQ*KfrIg=wojeJejuYldujb)w9 zx?xaK2#Z#o81pnTpNoV3yOp(M>G#7j)?E3Aa=Gmj0 - if false and this.document_margins is null + # Read page margins from the document. First checks for an @page rule. + # If that is not found, side margins are set to the side margins of the + # body element. + if this.document_margins is null + this.document_margins = {left:null, top:null, right:null, bottom:null} + tmp = document.createElement('div') + tmp.style.visibility = 'hidden' + tmp.style.position = 'absolute' + document.body.appendChild(tmp) 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) + for prop in ['left', 'top', 'bottom', 'right'] + val = rule.style.getPropertyValue('margin-'+prop) if val - log(val) + tmp.style.height = val + pxval = parseInt(window.getComputedStyle(tmp).height) + if not isNaN(pxval) + this.document_margins[prop] = pxval + document.body.removeChild(tmp) + if this.document_margins.left is null + val = parseInt(window.getComputedStyle(document.body).marginLeft) + if not isNaN(val) + this.document_margins.left = val + if this.document_margins.right is null + val = parseInt(window.getComputedStyle(document.body).marginRight) + if not isNaN(val) + this.document_margins.right = val set_geometry: (cols_per_screen=1, margin_top=20, margin_side=40, margin_bottom=20) -> - this.margin_top = margin_top - this.margin_side = margin_side - this.margin_bottom = margin_bottom this.cols_per_screen = cols_per_screen + if this.use_document_margins and this.document_margins != null + this.margin_top = this.document_margins.top or margin_top + this.margin_bottom = this.document_margins.bottom or margin_bottom + this.margin_side = this.document_margins.left or this.document_margins.right or margin_side + else + this.margin_top = margin_top + this.margin_side = margin_side + this.margin_bottom = margin_bottom layout: () -> # start_time = new Date().getTime() @@ -374,4 +400,3 @@ if window? # TODO: # Highlight on jump_to_anchor -# Handle document specified margins and allow them to be overridden diff --git a/src/calibre/gui2/viewer/config.py b/src/calibre/gui2/viewer/config.py index dccd541380..383699ff98 100644 --- a/src/calibre/gui2/viewer/config.py +++ b/src/calibre/gui2/viewer/config.py @@ -53,6 +53,11 @@ def config(defaults=None): '0 and 1.')) c.add_opt('fullscreen_clock', default=False, action='store_true', help=_('Show a clock in fullscreen mode.')) + c.add_opt('cols_per_screen', default=1) + c.add_opt('use_book_margins', default=False, action='store_true') + c.add_opt('top_margin', default=20) + c.add_opt('side_margin', default=40) + c.add_opt('bottom_margin', default=20) fonts = c.add_group('FONTS', _('Font options')) fonts('serif_family', default='Times New Roman' if iswindows else 'Liberation Serif', @@ -120,6 +125,11 @@ class ConfigDialog(QDialog, Ui_Dialog): self.hyphenate_default_lang.setVisible(False) self.hyphenate_label.setVisible(False) self.opt_fullscreen_clock.setChecked(opts.fullscreen_clock) + self.opt_cols_per_screen.setValue(opts.cols_per_screen) + self.opt_override_book_margins.setChecked(not opts.use_book_margins) + for x in ('top', 'bottom', 'side'): + getattr(self, 'opt_%s_margin'%x).setValue(getattr(opts, + x+'_margin')) def accept(self, *args): if self.shortcut_config.is_editing: @@ -152,6 +162,11 @@ class ConfigDialog(QDialog, Ui_Dialog): c.set('line_scrolling_stops_on_pagebreaks', self.opt_line_scrolling_stops_on_pagebreaks.isChecked()) c.set('fullscreen_clock', self.opt_fullscreen_clock.isChecked()) + c.set('cols_per_screen', int(self.opt_cols_per_screen.value())) + c.set('use_book_margins', not + self.opt_override_book_margins.isChecked()) + for x in ('top', 'bottom', 'side'): + c.set(x+'_margin', int(getattr(self, 'opt_%s_margin'%x).value())) return QDialog.accept(self, *args) diff --git a/src/calibre/gui2/viewer/config.ui b/src/calibre/gui2/viewer/config.ui index 546ed4ac44..a3c74681c1 100644 --- a/src/calibre/gui2/viewer/config.ui +++ b/src/calibre/gui2/viewer/config.ui @@ -18,16 +18,6 @@ :/images/config.png:/images/config.png - - - - Qt::Horizontal - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - - @@ -68,7 +58,7 @@ QToolBox::tab:hover { 0 0 811 - 416 + 384 @@ -212,13 +202,143 @@ QToolBox::tab:hover { + + + + 0 + 0 + 811 + 384 + + + + Text &layout in paged mode + + + + QFormLayout::ExpandingFieldsGrow + + + + + <p>These options only apply in "paged" mode, where the text is broken up into pages, as in a paper book. To get into this mode, use the button with the yellow scroll icon in the top right corner of the viewer window. + + + true + + + + + + + The number of &pages of text to show on screen + + + opt_cols_per_screen + + + + + + + page(s) + + + 1 + + + 5 + + + + + + + &Override the page margin settings specified in the book + + + true + + + + + + + &Top margin + + + opt_top_margin + + + + + + + px + + + 10 + + + 1000 + + + + + + + &Side margin + + + opt_side_margin + + + + + + + px + + + 10 + + + 1000 + + + + + + + &Bottom margin + + + opt_bottom_margin + + + + + + + px + + + 10 + + + 1000 + + + + + 0 0 811 - 416 + 384 @@ -266,7 +386,7 @@ QToolBox::tab:hover { 0 0 811 - 416 + 384 @@ -337,7 +457,7 @@ QToolBox::tab:hover { 0 0 811 - 416 + 384 @@ -430,6 +550,16 @@ QToolBox::tab:hover { + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + @@ -446,8 +576,8 @@ QToolBox::tab:hover { accept() - 252 - 569 + 258 + 623 157 @@ -462,8 +592,8 @@ QToolBox::tab:hover { reject() - 320 - 569 + 326 + 623 286 @@ -478,12 +608,60 @@ QToolBox::tab:hover { setEnabled(bool) - 83 - 279 + 89 + 226 - 349 - 312 + 332 + 259 + + + + + opt_override_book_margins + toggled(bool) + opt_top_margin + setEnabled(bool) + + + 137 + 189 + + + 367 + 218 + + + + + opt_override_book_margins + toggled(bool) + opt_side_margin + setEnabled(bool) + + + 71 + 193 + + + 347 + 253 + + + + + opt_override_book_margins + toggled(bool) + opt_bottom_margin + setEnabled(bool) + + + 513 + 196 + + + 371 + 281 diff --git a/src/calibre/gui2/viewer/documentview.py b/src/calibre/gui2/viewer/documentview.py index 554019d2d1..76b57629cb 100644 --- a/src/calibre/gui2/viewer/documentview.py +++ b/src/calibre/gui2/viewer/documentview.py @@ -134,6 +134,10 @@ class Document(QWebPage): # {{{ # Leave some space for the scrollbar and some border self.max_fs_width = min(opts.max_fs_width, screen_width-50) self.fullscreen_clock = opts.fullscreen_clock + self.use_book_margins = opts.use_book_margins + self.cols_per_screen = opts.cols_per_screen + self.side_margin = opts.side_margin + self.top_margin, self.bottom_margin = opts.top_margin, opts.bottom_margin def fit_images(self): if self.do_fit_images and not self.in_paged_mode: @@ -216,6 +220,14 @@ class Document(QWebPage): # {{{ def switch_to_paged_mode(self, onresize=False): if onresize and not self.loaded_javascript: return + self.javascript(''' + window.paged_display.use_document_margins = %s; + window.paged_display.set_geometry(%d, %d, %d, %d); + '''%( + ('true' if self.use_book_margins else 'false'), + self.cols_per_screen, self.top_margin, self.side_margin, + self.bottom_margin + )) side_margin = self.javascript('window.paged_display.layout()', typ=int) # Setup the contents size to ensure that there is a right most margin. # Without this webkit renders the final column with no margin, as the