From 0815098cf82a6195997ac0e9ab20d73c93601019 Mon Sep 17 00:00:00 2001 From: "Marshall T. Vandegrift" Date: Sat, 10 Jan 2009 00:28:56 -0500 Subject: [PATCH] Fix bug in font-size-mapping algorithm. --- src/calibre/ebooks/html.py | 12 ++++++++++- src/calibre/ebooks/oeb/profile.py | 2 +- src/calibre/ebooks/oeb/transforms/flatcss.py | 21 +++++++++++--------- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/calibre/ebooks/html.py b/src/calibre/ebooks/html.py index 84af7527bd..f2338ffdff 100644 --- a/src/calibre/ebooks/html.py +++ b/src/calibre/ebooks/html.py @@ -796,7 +796,17 @@ class Processor(Parser): setting = '' face = font.attrib.pop('face', None) if face is not None: - setting += 'font-face:%s;'%face + faces = [] + for face in face.split(','): + if ' ' in face: + face = "%s" % face + faces.append(face) + for generic in ('serif', 'sans-serif', 'monospace'): + if generic in faces: + break + else: + faces.append('serif') + setting += 'font-family:%s;'% ', '.join(faces) color = font.attrib.pop('color', None) if color is not None: setting += 'color:%s'%color diff --git a/src/calibre/ebooks/oeb/profile.py b/src/calibre/ebooks/oeb/profile.py index 3fdec6c8a5..3ecb0c5fca 100644 --- a/src/calibre/ebooks/oeb/profile.py +++ b/src/calibre/ebooks/oeb/profile.py @@ -42,7 +42,7 @@ PROFILES = { # Not really, but let's pretend 'MobiDesktop': Profile(width=280, height=300, dpi=96, fbase=18, - fsizes=[14, 14, 16, 18, 20, 22, 22, 24]), + fsizes=[14, 14, 16, 18, 20, 22, 24, 26]), # No clue on usable screen size and DPI 'CybookG3': diff --git a/src/calibre/ebooks/oeb/transforms/flatcss.py b/src/calibre/ebooks/oeb/transforms/flatcss.py index 2e6e64cdfa..5ad9042c7b 100644 --- a/src/calibre/ebooks/oeb/transforms/flatcss.py +++ b/src/calibre/ebooks/oeb/transforms/flatcss.py @@ -33,12 +33,13 @@ class KeyMapper(object): def relate(size, base): size = float(size) base = float(base) - if size == base: return 0 + if abs(size - base) < 0.1: return 0 sign = -1 if size < base else 1 endp = 0 if size < base else 36 diff = (abs(base - size) * 3) + ((36 - size) / 100) logb = abs(base - endp) - return sign * math.log(diff, logb) + result = sign * math.log(diff, logb) + return result def __getitem__(self, ssize): if ssize in self.cache: @@ -122,6 +123,8 @@ class CSSFlattener(object): fsize = self.context.source.fbase self.baseline_node(body, stylizer, sizes, fsize) sbase = max(sizes.items(), key=operator.itemgetter(1))[0] + self.oeb.logger.info( + "Source base font size is %0.05fpt" % sbase) return sbase def clean_edges(self, cssdict, style, fsize): @@ -154,14 +157,14 @@ class CSSFlattener(object): if node.tag == XHTML('font'): node.tag = XHTML('span') if 'size' in node.attrib: - size = node.attrib['size'] - if size.startswith('+'): - cssdict['font-size'] = 'larger' - elif size.startswith('-'): - cssdict['font-size'] = 'smaller' - else: + size = node.attrib['size'].strip() + if size: fnums = self.context.source.fnums - cssdict['font-size'] = fnums[int(size)] + if size[0] in ('+', '-'): + # Oh, the warcrimes + cssdict['font-size'] = fnums[3+int(size)] + else: + cssdict['font-size'] = fnums[int(size)] del node.attrib['size'] if 'color' in node.attrib: cssdict['color'] = node.attrib['color']