Fix #1714. Interpret unitless CSS values as pixels.

This commit is contained in:
Marshall T. Vandegrift 2009-01-27 17:28:47 -05:00
parent 30f43e14b5
commit 7d61ff9939
2 changed files with 16 additions and 9 deletions

View File

@ -265,6 +265,8 @@ class Stylizer(object):
class Style(object):
UNIT_RE = re.compile(r'^(-*[0-9]*[.]?[0-9]*)\s*(%|em|px|mm|cm|in|pt|pc)$')
def __init__(self, element, stylizer):
self._element = element
self._profile = stylizer.profile
@ -319,13 +321,11 @@ class Style(object):
if isinstance(value, (int, long, float)):
return value
try:
if float(value) == 0:
return 0.0
return float(value) * 72.0 / self._profile.dpi
except:
pass
result = value
m = re.search(
r"^(-*[0-9]*\.?[0-9]*)\s*(%|em|px|mm|cm|in|pt|pc)$", value)
m = self.UNIT_RE.match(value)
if m is not None and m.group(1):
value = float(m.group(1))
unit = m.group(2)

View File

@ -23,6 +23,12 @@ from calibre.ebooks.oeb.stylizer import Stylizer
COLLAPSE = re.compile(r'[ \t\r\n\v]+')
STRIPNUM = re.compile(r'[-0-9]+$')
def asfloat(value, default):
if not isinstance(value, (int, long, float)):
value = default
return float(value)
class KeyMapper(object):
def __init__(self, sbase, dbase, dkey):
self.sbase = float(sbase)
@ -179,12 +185,13 @@ class CSSFlattener(object):
if cssdict:
if self.lineh and self.fbase and tag != 'body':
self.clean_edges(cssdict, style, psize)
margin = style['margin-left']
left += margin if isinstance(margin, float) else 0
if (left + style['text-indent']) < 0:
percent = (margin - style['text-indent']) / style['width']
margin = asfloat(style['margin-left'], 0)
indent = asfloat(style['text-indent'], 0)
left += margin
if (left + indent) < 0:
percent = (margin - indent) / style['width']
cssdict['margin-left'] = "%d%%" % (percent * 100)
left -= style['text-indent']
left -= indent
if 'display' in cssdict and cssdict['display'] == 'in-line':
cssdict['display'] = 'inline'
if self.unfloat and 'float' in cssdict \