From e958df9028d885f766c630143b0b45f96a8ea001 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 3 May 2015 07:22:42 +0530 Subject: [PATCH] DOCX Output: Add support for block images --- src/calibre/ebooks/docx/writer/from_html.py | 2 +- src/calibre/ebooks/docx/writer/images.py | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/calibre/ebooks/docx/writer/from_html.py b/src/calibre/ebooks/docx/writer/from_html.py index 6fa25b7de2..f310577fae 100644 --- a/src/calibre/ebooks/docx/writer/from_html.py +++ b/src/calibre/ebooks/docx/writer/from_html.py @@ -435,7 +435,7 @@ class Convert(object): if anchor: block.bookmarks.add(self.bookmark_for_anchor(anchor, html_tag)) if tagname == 'img': - self.images_manager.add_image(html_tag, block, stylizer) + self.images_manager.add_image(html_tag, block, stylizer, as_block=True) else: if html_tag.text: block.add_text(html_tag.text, tag_style, ignore_leading_whitespace=True, is_parent_style=True, link=self.current_link) diff --git a/src/calibre/ebooks/docx/writer/images.py b/src/calibre/ebooks/docx/writer/images.py index 357d4ac179..48a1364c0d 100644 --- a/src/calibre/ebooks/docx/writer/images.py +++ b/src/calibre/ebooks/docx/writer/images.py @@ -44,7 +44,7 @@ class ImagesManager(object): self.document_relationships = document_relationships self.count = 0 - def add_image(self, img, block, stylizer, bookmark=None): + def add_image(self, img, block, stylizer, bookmark=None, as_block=False): src = img.get('src') if not src: return @@ -58,16 +58,22 @@ class ImagesManager(object): image_rid = self.document_relationships.add_image(image_fname) self.images[href] = Image(image_rid, image_fname, width, height, fmt, item) item.unload_data_from_memory() - drawing = self.create_image_markup(img, stylizer, href) + drawing = self.create_image_markup(img, stylizer, href, as_block=as_block) block.add_image(drawing, bookmark=bookmark) return self.images[href].rid - def create_image_markup(self, html_img, stylizer, href): + def create_image_markup(self, html_img, stylizer, href, as_block=False): # TODO: img inside a link (clickable image) style = stylizer.style(html_img) floating = style['float'] if floating not in {'left', 'right'}: floating = None + if as_block: + ml, mr = style._get('margin-left'), style._get('margin-right') + if ml == 'auto': + floating = 'center' if mr == 'auto' else 'right' + if mr == 'auto': + floating = 'center' if ml == 'auto' else 'right' fake_margins = floating is None self.count += 1 img = self.images[href] @@ -98,7 +104,10 @@ class ImagesManager(object): makeelement(parent, 'wp:effectExtent', l='0', r='0', t='0', b='0') if floating is not None: # The idiotic Word requires this to be after the extent settings - makeelement(parent, 'wp:wrapSquare', wrapText='bothSides') + if as_block: + makeelement(parent, 'wp:wrapTopAndBottom') + else: + makeelement(parent, 'wp:wrapSquare', wrapText='bothSides') makeelement(parent, 'wp:docPr', id=str(self.count), name=name, descr=html_img.get('alt') or name) makeelement(makeelement(parent, 'wp:cNvGraphicFramePr'), 'a:graphicFrameLocks', noChangeAspect="1") g = makeelement(parent, 'a:graphic')