PDF Output: Resize large images so that they do not get off at the right edge of the page.

This commit is contained in:
Kovid Goyal 2012-07-05 19:46:58 +05:30
parent 79bd4403d0
commit 25758c4348
4 changed files with 28 additions and 11 deletions

Binary file not shown.

View File

@ -7,8 +7,6 @@
### ###
log = window.calibre_utils.log log = window.calibre_utils.log
viewport_to_document = window.calibre_utils.viewport_to_document
absleft = window.calibre_utils.absleft
class PagedDisplay class PagedDisplay
# This class is a namespace to expose functions via the # This class is a namespace to expose functions via the
@ -126,7 +124,25 @@ class PagedDisplay
return sm return sm
fit_images: () -> fit_images: () ->
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_pos: (frac) ->
# Scroll to the position represented by frac (number between 0 and 1) # Scroll to the position represented by frac (number between 0 and 1)
@ -263,7 +279,7 @@ class PagedDisplay
elem.scrollIntoView() elem.scrollIntoView()
if this.in_paged_mode if this.in_paged_mode
# Ensure we are scrolled to the column containing elem # 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: () -> snap_to_selection: () ->
# Ensure that the viewport is positioned at the start of the column # Ensure that the viewport is positioned at the start of the column
@ -272,7 +288,7 @@ class PagedDisplay
sel = window.getSelection() sel = window.getSelection()
r = sel.getRangeAt(0).getBoundingClientRect() r = sel.getRangeAt(0).getBoundingClientRect()
node = sel.anchorNode 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 # Ensure we are scrolled to the column containing the start of the
# selection # selection
@ -331,5 +347,5 @@ if window?
window.paged_display = new PagedDisplay() window.paged_display = new PagedDisplay()
# TODO: # TODO:
# Resizing of images
# Highlight on jump_to_anchor # Highlight on jump_to_anchor
# Handle document specified margins and allow them to be overridden

View File

@ -117,7 +117,6 @@ class PDFMetadata(object):
if len(oeb_metadata.creator) >= 1: if len(oeb_metadata.creator) >= 1:
self.author = authors_to_string([x.value for x in oeb_metadata.creator]) self.author = authors_to_string([x.value for x in oeb_metadata.creator])
class PDFWriter(QObject): # {{{ class PDFWriter(QObject): # {{{
def __init__(self, opts, log, cover_data=None): def __init__(self, opts, log, cover_data=None):
@ -185,8 +184,8 @@ class PDFWriter(QObject): # {{{
from PyQt4.Qt import QSize, QPainter from PyQt4.Qt import QSize, QPainter
if self.paged_js is None: if self.paged_js is None:
from calibre.utils.resources import compiled_coffeescript from calibre.utils.resources import compiled_coffeescript
self.paged_js = compiled_coffeescript('ebooks.oeb.display.paged', self.paged_js = compiled_coffeescript('ebooks.oeb.display.utils')
dynamic=False) self.paged_js += compiled_coffeescript('ebooks.oeb.display.paged')
printer = get_pdf_printer(self.opts, output_file_name=outpath) printer = get_pdf_printer(self.opts, output_file_name=outpath)
painter = QPainter(printer) painter = QPainter(printer)
zoomx = printer.logicalDpiX()/self.view.logicalDpiX() zoomx = printer.logicalDpiX()/self.view.logicalDpiX()
@ -202,6 +201,7 @@ class PDFWriter(QObject): # {{{
document.body.style.backgroundColor = "white"; document.body.style.backgroundColor = "white";
paged_display.set_geometry(1, 0, 0, 0); paged_display.set_geometry(1, 0, 0, 0);
paged_display.layout(); paged_display.layout();
paged_display.fit_images();
''') ''')
mf = self.view.page().mainFrame() mf = self.view.page().mainFrame()
while True: while True:

View File

@ -26,8 +26,8 @@ class Printing(QObject):
for x in (Qt.Horizontal, Qt.Vertical): for x in (Qt.Horizontal, Qt.Vertical):
mf.setScrollBarPolicy(x, Qt.ScrollBarAlwaysOff) mf.setScrollBarPolicy(x, Qt.ScrollBarAlwaysOff)
self.view.loadFinished.connect(self.load_finished) self.view.loadFinished.connect(self.load_finished)
self.paged_js = compiled_coffeescript('ebooks.oeb.display.paged', self.paged_js = compiled_coffeescript('ebooks.oeb.display.utils')
dynamic=False) self.paged_js += compiled_coffeescript('ebooks.oeb.display.paged')
def load_finished(self, ok): def load_finished(self, ok):
self.loaded_ok = ok self.loaded_ok = ok
@ -70,6 +70,7 @@ class Printing(QObject):
document.body.style.backgroundColor = "white"; document.body.style.backgroundColor = "white";
paged_display.set_geometry(1, 0, 0, 0); paged_display.set_geometry(1, 0, 0, 0);
paged_display.layout(); paged_display.layout();
paged_display.fit_images();
''') ''')
while True: while True: