From 7d61ff9939fdfc961263176b8425a0bcdcf7ecaf Mon Sep 17 00:00:00 2001 From: "Marshall T. Vandegrift" Date: Tue, 27 Jan 2009 17:28:47 -0500 Subject: [PATCH] Fix #1714. Interpret unitless CSS values as pixels. --- src/calibre/ebooks/oeb/stylizer.py | 8 ++++---- src/calibre/ebooks/oeb/transforms/flatcss.py | 17 ++++++++++++----- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/calibre/ebooks/oeb/stylizer.py b/src/calibre/ebooks/oeb/stylizer.py index 29c6c5b2b4..03a1fade10 100644 --- a/src/calibre/ebooks/oeb/stylizer.py +++ b/src/calibre/ebooks/oeb/stylizer.py @@ -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) diff --git a/src/calibre/ebooks/oeb/transforms/flatcss.py b/src/calibre/ebooks/oeb/transforms/flatcss.py index 28b72b04f3..01afcb08e2 100644 --- a/src/calibre/ebooks/oeb/transforms/flatcss.py +++ b/src/calibre/ebooks/oeb/transforms/flatcss.py @@ -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 \