From 25758c4348a5c067359d663793259b098ca61c91 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 5 Jul 2012 19:46:58 +0530 Subject: [PATCH] PDF Output: Resize large images so that they do not get off at the right edge of the page. --- resources/compiled_coffeescript.zip | Bin 44408 -> 46282 bytes src/calibre/ebooks/oeb/display/paged.coffee | 28 +++++++++++++++----- src/calibre/ebooks/pdf/writer.py | 6 ++--- src/calibre/gui2/viewer/printing.py | 5 ++-- 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/resources/compiled_coffeescript.zip b/resources/compiled_coffeescript.zip index c026c894ded0cb354244edef135a793e5f4ad608..7ba1aa2fbf721917cca42bd18f56b8c16577c710 100644 GIT binary patch delta 1669 zcmb_d&uk$S0mp>JlrP7LJ6TD$9+_vV}Lecv1H zzrTSs}E*Bh8U04v)%lyBqhke2_<;ENJVJ{K*2c@ z@RxG?NHQf1%wWWyDwc1TWNYD!j1<#29`+Y6KZ_m2tqhO#9Kv5tpNwFPZy$=UfykHJ z_>4wP_LxiebC}T%(KP;M?)YQ?!JjWA`9^H2|9mEUY-s(g9?TaVqC=h;lUOtb7IzLnBL{Ptpau1gtDHBB?-`o;ESFe zkSp&H{`Q=*C2ihEg|6w0s<5~Ik5Q_oZS6T9zIr3|;aP3fStF1FKzL+$`${S;f<;i2 zp|*RvNNOVw-~G)&oIm<8ej?}(sb&$?Dxn})JP)5w_LbaxZ+i4Qz_0D(;l0L{AB^D9 xUFLu1V|+6+$$!~d7)ySML*R**3uEo8GIIIbiFo1C_=H#x#a}WU3T^kr=zkTX5N`kg delta 257 zcmX^0lIh1SCW!!VW)=|!5NIrT;?T(!t0`>Az#wcmxzAK`@`cSD6D=FL6O)Q_QqxLw z6ms$>-** - null + # Ensure no images are wider than the available width in a column. Note + # that this method use getBoundingClientRect() which means it will + # force a relayout if the render tree is dirty. + images = [] + for img in document.getElementsByTagName('img') + previously_limited = calibre_utils.retrieve(img, 'width-limited', false) + br = img.getBoundingClientRect() + left = calibre_utils.viewport_to_document(br.left, 0, doc=img.ownerDocument)[0] + col = this.column_at(left) * this.page_width + rleft = left - col - this.current_margin_side + width = br.right - br.left + rright = rleft + width + col_width = this.page_width - 2*this.current_margin_side + if previously_limited or rright > col_width + images.push([img, col_width - rleft]) + + for [img, max_width] in images + img.style.setProperty('max-width', max_width+'px') + calibre_utils.store(img, 'width-limited', true) scroll_to_pos: (frac) -> # Scroll to the position represented by frac (number between 0 and 1) @@ -263,7 +279,7 @@ class PagedDisplay elem.scrollIntoView() if this.in_paged_mode # Ensure we are scrolled to the column containing elem - this.scroll_to_xpos(absleft(elem) + 5) + this.scroll_to_xpos(calibre_utils.absleft(elem) + 5) snap_to_selection: () -> # Ensure that the viewport is positioned at the start of the column @@ -272,7 +288,7 @@ class PagedDisplay sel = window.getSelection() r = sel.getRangeAt(0).getBoundingClientRect() node = sel.anchorNode - left = viewport_to_document(r.left, r.top, doc=node.ownerDocument)[0] + left = calibre_utils.viewport_to_document(r.left, r.top, doc=node.ownerDocument)[0] # Ensure we are scrolled to the column containing the start of the # selection @@ -331,5 +347,5 @@ if window? window.paged_display = new PagedDisplay() # TODO: -# Resizing of images # Highlight on jump_to_anchor +# Handle document specified margins and allow them to be overridden diff --git a/src/calibre/ebooks/pdf/writer.py b/src/calibre/ebooks/pdf/writer.py index ced03bb1fb..ac371b74a6 100644 --- a/src/calibre/ebooks/pdf/writer.py +++ b/src/calibre/ebooks/pdf/writer.py @@ -117,7 +117,6 @@ class PDFMetadata(object): if len(oeb_metadata.creator) >= 1: self.author = authors_to_string([x.value for x in oeb_metadata.creator]) - class PDFWriter(QObject): # {{{ def __init__(self, opts, log, cover_data=None): @@ -185,8 +184,8 @@ class PDFWriter(QObject): # {{{ from PyQt4.Qt import QSize, QPainter if self.paged_js is None: from calibre.utils.resources import compiled_coffeescript - self.paged_js = compiled_coffeescript('ebooks.oeb.display.paged', - dynamic=False) + self.paged_js = compiled_coffeescript('ebooks.oeb.display.utils') + self.paged_js += compiled_coffeescript('ebooks.oeb.display.paged') printer = get_pdf_printer(self.opts, output_file_name=outpath) painter = QPainter(printer) zoomx = printer.logicalDpiX()/self.view.logicalDpiX() @@ -202,6 +201,7 @@ class PDFWriter(QObject): # {{{ document.body.style.backgroundColor = "white"; paged_display.set_geometry(1, 0, 0, 0); paged_display.layout(); + paged_display.fit_images(); ''') mf = self.view.page().mainFrame() while True: diff --git a/src/calibre/gui2/viewer/printing.py b/src/calibre/gui2/viewer/printing.py index fe0db18df4..f860edb12c 100644 --- a/src/calibre/gui2/viewer/printing.py +++ b/src/calibre/gui2/viewer/printing.py @@ -26,8 +26,8 @@ class Printing(QObject): for x in (Qt.Horizontal, Qt.Vertical): mf.setScrollBarPolicy(x, Qt.ScrollBarAlwaysOff) self.view.loadFinished.connect(self.load_finished) - self.paged_js = compiled_coffeescript('ebooks.oeb.display.paged', - dynamic=False) + self.paged_js = compiled_coffeescript('ebooks.oeb.display.utils') + self.paged_js += compiled_coffeescript('ebooks.oeb.display.paged') def load_finished(self, ok): self.loaded_ok = ok @@ -70,6 +70,7 @@ class Printing(QObject): document.body.style.backgroundColor = "white"; paged_display.set_geometry(1, 0, 0, 0); paged_display.layout(); + paged_display.fit_images(); ''') while True: