diff --git a/src/calibre/ebooks/docx/writer/container.py b/src/calibre/ebooks/docx/writer/container.py index 787736face..4d45910bc7 100644 --- a/src/calibre/ebooks/docx/writer/container.py +++ b/src/calibre/ebooks/docx/writer/container.py @@ -36,6 +36,20 @@ def page_size(opts): 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): namespaces = namespaces or DOCXNamespace().namespaces @@ -50,9 +64,7 @@ def create_skeleton(opts, namespaces=None): width, height = int(20 * width), int(20 * height) def margin(which): - val = getattr(opts, 'docx_page_margin_' + which) - if val == 0.0: - val = getattr(opts, 'margin_' + which) + val = page_margin(opts, which) return w(which), str(int(val * 20)) body.append(E.sectPr( E.pgSz(**{w('w'):str(width), w('h'):str(height)}), diff --git a/src/calibre/ebooks/docx/writer/from_html.py b/src/calibre/ebooks/docx/writer/from_html.py index 4238871150..be7cbb7db0 100644 --- a/src/calibre/ebooks/docx/writer/from_html.py +++ b/src/calibre/ebooks/docx/writer/from_html.py @@ -421,7 +421,7 @@ class Convert(object): 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.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.fonts_manager = FontsManager(self.docx.namespace, self.oeb, self.opts) self.blocks = Blocks(self.docx.namespace, self.styles_manager, self.links_manager) diff --git a/src/calibre/ebooks/docx/writer/images.py b/src/calibre/ebooks/docx/writer/images.py index cd71d4e88f..f136c2f18b 100644 --- a/src/calibre/ebooks/docx/writer/images.py +++ b/src/calibre/ebooks/docx/writer/images.py @@ -14,10 +14,12 @@ from future_builtins import map from lxml import etree +from calibre import fit_image from calibre.ebooks.oeb.base import urlunquote from calibre.ebooks.docx.images import pt_to_emu from calibre.utils.filenames import ascii_filename 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') @@ -40,8 +42,9 @@ def get_image_margins(style): class ImagesManager(object): - def __init__(self, oeb, document_relationships): + def __init__(self, oeb, document_relationships, opts): self.oeb, self.log = oeb, oeb.log + self.page_width, self.page_height = page_effective_area(opts) self.images = {} self.seen_filenames = set() self.document_relationships = document_relationships @@ -100,7 +103,9 @@ class ImagesManager(object): self.count += 1 img = self.images[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