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)

This commit is contained in:
Kovid Goyal 2017-09-20 19:18:26 +05:30
parent f19fbaf61c
commit ae098550e9
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 11 additions and 2 deletions

View File

@ -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.')),

View File

@ -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()

View File

@ -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)