From ae098550e9aeed802e260ece4d23f376999b81d5 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 20 Sep 2017 19:18:26 +0530 Subject: [PATCH] DOCX Output: Add a n option to preserve the cover aspect ratio in the output document. Fixes #1715800 [Conversion to DOCX Results in Stretched Cover Page](https://bugs.launchpad.net/calibre/+bug/1715800) --- src/calibre/ebooks/conversion/plugins/docx_output.py | 4 ++++ src/calibre/ebooks/docx/writer/from_html.py | 2 +- src/calibre/ebooks/docx/writer/images.py | 7 ++++++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/calibre/ebooks/conversion/plugins/docx_output.py b/src/calibre/ebooks/conversion/plugins/docx_output.py index 781fce589f..32fe3219ae 100644 --- a/src/calibre/ebooks/conversion/plugins/docx_output.py +++ b/src/calibre/ebooks/conversion/plugins/docx_output.py @@ -33,6 +33,10 @@ class DOCXOutput(OutputFormatPlugin): help=_('Do not insert the book cover as an image at the start of the document.' ' If you use this option, the book cover will be discarded.')), + OptionRecommendation(name='preserve_cover_aspect_ratio', recommended_value=False, + help=_('Preserve the aspect ratio of the cover image instead of stretching' + ' it out to cover the entire page.')), + OptionRecommendation(name='docx_no_toc', recommended_value=False, help=_('Do not insert the table of contents as a page at the start of the document.')), diff --git a/src/calibre/ebooks/docx/writer/from_html.py b/src/calibre/ebooks/docx/writer/from_html.py index 2150034e3c..9ca1dab101 100644 --- a/src/calibre/ebooks/docx/writer/from_html.py +++ b/src/calibre/ebooks/docx/writer/from_html.py @@ -457,7 +457,7 @@ class Convert(object): self.blocks.resolve_language() if self.cover_img is not None: - self.cover_img = self.images_manager.create_cover_markup(self.cover_img, *page_size(self.opts)) + self.cover_img = self.images_manager.create_cover_markup(self.cover_img, self.opts.preserve_cover_aspect_ratio, *page_size(self.opts)) self.lists_manager.finalize(all_blocks) self.styles_manager.finalize(all_blocks) self.write() diff --git a/src/calibre/ebooks/docx/writer/images.py b/src/calibre/ebooks/docx/writer/images.py index c064d1b7c8..e1d4fafef2 100644 --- a/src/calibre/ebooks/docx/writer/images.py +++ b/src/calibre/ebooks/docx/writer/images.py @@ -184,9 +184,14 @@ class ImagesManager(object): finally: item.unload_data_from_memory(False) - def create_cover_markup(self, img, width, height): + def create_cover_markup(self, img, preserve_aspect_ratio, width, height): self.count += 1 makeelement, namespaces = self.document_relationships.namespace.makeelement, self.document_relationships.namespace.namespaces + if preserve_aspect_ratio: + if img.width >= img.height: + height *= img.height / img.width + else: + width *= img.width / img.height root = etree.Element('root', nsmap=namespaces) ans = makeelement(root, 'w:drawing', append=False)