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): 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): def __init__(self, element, stylizer):
self._element = element self._element = element
self._profile = stylizer.profile self._profile = stylizer.profile
@ -319,13 +321,11 @@ class Style(object):
if isinstance(value, (int, long, float)): if isinstance(value, (int, long, float)):
return value return value
try: try:
if float(value) == 0: return float(value) * 72.0 / self._profile.dpi
return 0.0
except: except:
pass pass
result = value result = value
m = re.search( m = self.UNIT_RE.match(value)
r"^(-*[0-9]*\.?[0-9]*)\s*(%|em|px|mm|cm|in|pt|pc)$", value)
if m is not None and m.group(1): if m is not None and m.group(1):
value = float(m.group(1)) value = float(m.group(1))
unit = m.group(2) 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]+') COLLAPSE = re.compile(r'[ \t\r\n\v]+')
STRIPNUM = re.compile(r'[-0-9]+$') 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): class KeyMapper(object):
def __init__(self, sbase, dbase, dkey): def __init__(self, sbase, dbase, dkey):
self.sbase = float(sbase) self.sbase = float(sbase)
@ -179,12 +185,13 @@ class CSSFlattener(object):
if cssdict: if cssdict:
if self.lineh and self.fbase and tag != 'body': if self.lineh and self.fbase and tag != 'body':
self.clean_edges(cssdict, style, psize) self.clean_edges(cssdict, style, psize)
margin = style['margin-left'] margin = asfloat(style['margin-left'], 0)
left += margin if isinstance(margin, float) else 0 indent = asfloat(style['text-indent'], 0)
if (left + style['text-indent']) < 0: left += margin
percent = (margin - style['text-indent']) / style['width'] if (left + indent) < 0:
percent = (margin - indent) / style['width']
cssdict['margin-left'] = "%d%%" % (percent * 100) cssdict['margin-left'] = "%d%%" % (percent * 100)
left -= style['text-indent'] left -= indent
if 'display' in cssdict and cssdict['display'] == 'in-line': if 'display' in cssdict and cssdict['display'] == 'in-line':
cssdict['display'] = 'inline' cssdict['display'] = 'inline'
if self.unfloat and 'float' in cssdict \ if self.unfloat and 'float' in cssdict \