MOBI Output: Fix using percentage units for margins resulting in too large margins when using the tablet output profile. Fixes #1932392 ["margin-top" property is converted into <number>em when converting into MOBI breaking iOS rendering](https://bugs.launchpad.net/calibre/+bug/1932392)

This commit is contained in:
Kovid Goyal 2021-06-23 08:43:51 +05:30
parent 298ede817b
commit 2a057203e2
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -51,6 +51,16 @@ def asfloat(value):
return float(value)
def convert_margin(style, which):
# percentage values come out too large when the user uses a non kindle
# output profile like the tablet profile
ans = asfloat(style[which])
raw = style._get(which)
if isinstance(raw, str) and '%' in raw:
ans = min(style._unit_convert(raw, base=600), ans)
return ans
def isspace(text):
if not text:
return True
@ -372,12 +382,12 @@ class MobiMLizer(object):
if style['margin-left'] == 'auto' \
and style['margin-right'] == 'auto':
istate.halign = 'center'
margin = asfloat(style['margin-left'])
margin = convert_margin(style, 'margin-left')
padding = asfloat(style['padding-left'])
if tag != 'body':
left = margin + padding
istate.left += left
vmargin = asfloat(style['margin-top'])
vmargin = convert_margin(style, 'margin-top')
bstate.vmargin = max((bstate.vmargin, vmargin))
vpadding = asfloat(style['padding-top'])
if vpadding > 0:
@ -385,13 +395,13 @@ class MobiMLizer(object):
bstate.vmargin = 0
bstate.vpadding += vpadding
elif not istate.href:
margin = asfloat(style['margin-left'])
margin = convert_margin(style, 'margin-left')
padding = asfloat(style['padding-left'])
lspace = margin + padding
if lspace > 0:
spaces = int(round((lspace * 3) / style['font-size']))
elem.text = ('\xa0' * spaces) + (elem.text or '')
margin = asfloat(style['margin-right'])
margin = convert_margin(style, 'margin-right')
padding = asfloat(style['padding-right'])
rspace = margin + padding
if rspace > 0:
@ -610,7 +620,7 @@ class MobiMLizer(object):
para.getparent().remove(para)
bstate.para = None
bstate.istate = None
vmargin = asfloat(style['margin-bottom'])
vmargin = convert_margin(style, 'margin-bottom')
bstate.vmargin = max((bstate.vmargin, vmargin))
vpadding = asfloat(style['padding-bottom'])
if vpadding > 0: