DOCX Output: Add support for block images

This commit is contained in:
Kovid Goyal 2015-05-03 07:22:42 +05:30
parent 2188514cdb
commit e958df9028
2 changed files with 14 additions and 5 deletions

View File

@ -435,7 +435,7 @@ class Convert(object):
if anchor: if anchor:
block.bookmarks.add(self.bookmark_for_anchor(anchor, html_tag)) block.bookmarks.add(self.bookmark_for_anchor(anchor, html_tag))
if tagname == 'img': 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: else:
if html_tag.text: if html_tag.text:
block.add_text(html_tag.text, tag_style, ignore_leading_whitespace=True, is_parent_style=True, link=self.current_link) block.add_text(html_tag.text, tag_style, ignore_leading_whitespace=True, is_parent_style=True, link=self.current_link)

View File

@ -44,7 +44,7 @@ class ImagesManager(object):
self.document_relationships = document_relationships self.document_relationships = document_relationships
self.count = 0 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') src = img.get('src')
if not src: if not src:
return return
@ -58,16 +58,22 @@ class ImagesManager(object):
image_rid = self.document_relationships.add_image(image_fname) image_rid = self.document_relationships.add_image(image_fname)
self.images[href] = Image(image_rid, image_fname, width, height, fmt, item) self.images[href] = Image(image_rid, image_fname, width, height, fmt, item)
item.unload_data_from_memory() 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) block.add_image(drawing, bookmark=bookmark)
return self.images[href].rid 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) # TODO: img inside a link (clickable image)
style = stylizer.style(html_img) style = stylizer.style(html_img)
floating = style['float'] floating = style['float']
if floating not in {'left', 'right'}: if floating not in {'left', 'right'}:
floating = None 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 fake_margins = floating is None
self.count += 1 self.count += 1
img = self.images[href] img = self.images[href]
@ -98,6 +104,9 @@ class ImagesManager(object):
makeelement(parent, 'wp:effectExtent', l='0', r='0', t='0', b='0') makeelement(parent, 'wp:effectExtent', l='0', r='0', t='0', b='0')
if floating is not None: if floating is not None:
# The idiotic Word requires this to be after the extent settings # The idiotic Word requires this to be after the extent settings
if as_block:
makeelement(parent, 'wp:wrapTopAndBottom')
else:
makeelement(parent, 'wp:wrapSquare', wrapText='bothSides') makeelement(parent, 'wp:wrapSquare', wrapText='bothSides')
makeelement(parent, 'wp:docPr', id=str(self.count), name=name, descr=html_img.get('alt') or name) 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") makeelement(makeelement(parent, 'wp:cNvGraphicFramePr'), 'a:graphicFrameLocks', noChangeAspect="1")