From 2b562831d70605758b07b867bf31d198f8963a68 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 2 Dec 2016 09:30:33 +0530 Subject: [PATCH] DOCX Input: Fix images wider than 50% of the page width being incorrectly right aligned even when anchored to the center of the page. Fixes #1646086 [Folow up to Location of Figures on Page](https://bugs.launchpad.net/calibre/+bug/1646086) --- src/calibre/ebooks/docx/images.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/calibre/ebooks/docx/images.py b/src/calibre/ebooks/docx/images.py index 81103407c7..0a4273b7cf 100644 --- a/src/calibre/ebooks/docx/images.py +++ b/src/calibre/ebooks/docx/images.py @@ -77,34 +77,34 @@ def get_image_margins(elem): return ans -def get_hpos(anchor, page_width, XPath, get): +def get_hpos(anchor, page_width, XPath, get, width_frac): for ph in XPath('./wp:positionH')(anchor): rp = ph.get('relativeFrom', None) if rp == 'leftMargin': - return 0 + return 0 + width_frac if rp == 'rightMargin': - return 1 + return 1 + width_frac + al = None + almap = {'left':0, 'center':0.5, 'right':1} for align in XPath('./wp:align')(ph): - al = align.text - if al == 'left': - return 0 - if al == 'center': - return 0.5 - if al == 'right': - return 1 + al = almap.get(align.text) + if al is not None: + if rp == 'page': + return al + return al + width_frac for po in XPath('./wp:posOffset')(ph): try: pos = emu_to_pt(int(po.text)) except (TypeError, ValueError): continue - return pos/page_width + return pos/page_width + width_frac for sp in XPath('./wp:simplePos')(anchor): try: x = emu_to_pt(sp.get('x', None)) except (TypeError, ValueError): continue - return x/page_width + return x/page_width + width_frac return 0 @@ -298,7 +298,7 @@ class Images(object): # Ignore margins page_width = page.width - hpos = get_hpos(anchor, page_width, XPath, get) + width/(2*page_width) + hpos = get_hpos(anchor, page_width, XPath, get, width/(2*page_width)) wrap_elem = None dofloat = False