DOCX Output: Prevent images from being larger than the page size. See #1688690 (links do not work when converted azw3 to docx; pictures too big)

This commit is contained in:
Kovid Goyal 2017-05-06 10:10:15 +05:30
parent ee895f53cf
commit b2e702667c
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 23 additions and 6 deletions

View File

@ -36,6 +36,20 @@ def page_size(opts):
return width, height return width, height
def page_margin(opts, which):
val = getattr(opts, 'docx_page_margin_' + which)
if val == 0.0:
val = getattr(opts, 'margin_' + which)
return val
def page_effective_area(opts):
width, height = page_size(opts)
width -= page_margin(opts, 'left') + page_margin(opts, 'right')
height -= page_margin(opts, 'top') + page_margin(opts, 'bottom')
return width, height # in pts
def create_skeleton(opts, namespaces=None): def create_skeleton(opts, namespaces=None):
namespaces = namespaces or DOCXNamespace().namespaces namespaces = namespaces or DOCXNamespace().namespaces
@ -50,9 +64,7 @@ def create_skeleton(opts, namespaces=None):
width, height = int(20 * width), int(20 * height) width, height = int(20 * width), int(20 * height)
def margin(which): def margin(which):
val = getattr(opts, 'docx_page_margin_' + which) val = page_margin(opts, which)
if val == 0.0:
val = getattr(opts, 'margin_' + which)
return w(which), str(int(val * 20)) return w(which), str(int(val * 20))
body.append(E.sectPr( body.append(E.sectPr(
E.pgSz(**{w('w'):str(width), w('h'):str(height)}), E.pgSz(**{w('w'):str(width), w('h'):str(height)}),

View File

@ -421,7 +421,7 @@ class Convert(object):
self.styles_manager = StylesManager(self.docx.namespace, self.log, self.mi.language) self.styles_manager = StylesManager(self.docx.namespace, self.log, self.mi.language)
self.links_manager = LinksManager(self.docx.namespace, self.docx.document_relationships, self.log) self.links_manager = LinksManager(self.docx.namespace, self.docx.document_relationships, self.log)
self.images_manager = ImagesManager(self.oeb, self.docx.document_relationships) self.images_manager = ImagesManager(self.oeb, self.docx.document_relationships, self.opts)
self.lists_manager = ListsManager(self.docx) self.lists_manager = ListsManager(self.docx)
self.fonts_manager = FontsManager(self.docx.namespace, self.oeb, self.opts) self.fonts_manager = FontsManager(self.docx.namespace, self.oeb, self.opts)
self.blocks = Blocks(self.docx.namespace, self.styles_manager, self.links_manager) self.blocks = Blocks(self.docx.namespace, self.styles_manager, self.links_manager)

View File

@ -14,10 +14,12 @@ from future_builtins import map
from lxml import etree from lxml import etree
from calibre import fit_image
from calibre.ebooks.oeb.base import urlunquote from calibre.ebooks.oeb.base import urlunquote
from calibre.ebooks.docx.images import pt_to_emu from calibre.ebooks.docx.images import pt_to_emu
from calibre.utils.filenames import ascii_filename from calibre.utils.filenames import ascii_filename
from calibre.utils.imghdr import identify from calibre.utils.imghdr import identify
from calibre.ebooks.docx.writer.container import page_effective_area
Image = namedtuple('Image', 'rid fname width height fmt item') Image = namedtuple('Image', 'rid fname width height fmt item')
@ -40,8 +42,9 @@ def get_image_margins(style):
class ImagesManager(object): class ImagesManager(object):
def __init__(self, oeb, document_relationships): def __init__(self, oeb, document_relationships, opts):
self.oeb, self.log = oeb, oeb.log self.oeb, self.log = oeb, oeb.log
self.page_width, self.page_height = page_effective_area(opts)
self.images = {} self.images = {}
self.seen_filenames = set() self.seen_filenames = set()
self.document_relationships = document_relationships self.document_relationships = document_relationships
@ -100,7 +103,9 @@ class ImagesManager(object):
self.count += 1 self.count += 1
img = self.images[href] img = self.images[href]
name = urlunquote(posixpath.basename(href)) name = urlunquote(posixpath.basename(href))
width, height = map(pt_to_emu, style.img_size(img.width, img.height)) width, height = style.img_size(img.width, img.height)
scaled, width, height = fit_image(width, height, self.page_width, self.page_height)
width, height = map(pt_to_emu, (width, height))
makeelement, namespaces = self.document_relationships.namespace.makeelement, self.document_relationships.namespace.namespaces makeelement, namespaces = self.document_relationships.namespace.makeelement, self.document_relationships.namespace.namespaces