From 77fd17c763f62224a7b23b95fa004816e2c9cb57 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 15 Nov 2016 17:17:26 +0530 Subject: [PATCH] Get rid of alt_font_family hack. Instead bump the font cache version and use family_name as the key instead of preferred_family_name --- src/calibre/ebooks/docx/fonts.py | 24 +------------------ src/calibre/ebooks/docx/styles.py | 4 +--- src/calibre/ebooks/oeb/polish/embed.py | 9 +++---- .../ebooks/oeb/transforms/embed_fonts.py | 9 +++---- src/calibre/gui2/tweak_book/manage_fonts.py | 5 +--- src/calibre/utils/fonts/metadata.py | 3 +-- src/calibre/utils/fonts/scanner.py | 14 +---------- 7 files changed, 11 insertions(+), 57 deletions(-) diff --git a/src/calibre/ebooks/docx/fonts.py b/src/calibre/ebooks/docx/fonts.py index d1011858d0..a196e0f070 100644 --- a/src/calibre/ebooks/docx/fonts.py +++ b/src/calibre/ebooks/docx/fonts.py @@ -114,7 +114,7 @@ class Fonts(object): name = f.name if variant in f.embedded else f.family_name return '"%s", %s' % (name.replace('"', ''), f.css_generic_family) - def embed_fonts(self, dest_dir, docx, embedded_families=None): + def embed_fonts(self, dest_dir, docx): defs = [] dest_dir = os.path.join(dest_dir, 'fonts') for name, variant in self.used: @@ -132,8 +132,6 @@ class Fonts(object): d = ['%s: %s' % (k, v) for k, v in d.iteritems()] d = ';\n\t'.join(d) defs.append('@font-face {\n\t%s\n}\n' % d) - if embedded_families is not None: - embedded_families.add(name) return '\n'.join(defs) def write(self, name, dest_dir, docx, variant): @@ -155,23 +153,3 @@ class Fonts(object): dest.write(raw[32:]) return fname - - def modify_font_properties(self, css, embedded_families): - ff = css.get('font-family', '') - m = re.match(r'"([^"]+)"', ff.strip()) - if m is not None and 'font-weight' not in css: - ff = m.group(1) - if ff and ff not in embedded_families: - if not has_system_fonts(ff): - try: - fonts = font_scanner.alt_fonts_for_family(ff) - except NoFonts: - return - if fonts: - font = get_best_font(fonts, css.get('font-style', 'normal'), css.get('font-stretch', 'normal')) - if font is not None: - rest = ', '.join(css['font-family'].split(',')[1:]) - if rest: - rest = ', ' + rest - css['font-family'] = '"%s"' % font['font-family'].replace('"', '') + rest - css['font-weight'] = font['font-weight'] diff --git a/src/calibre/ebooks/docx/styles.py b/src/calibre/ebooks/docx/styles.py index f083d21c6e..9eb7b485d0 100644 --- a/src/calibre/ebooks/docx/styles.py +++ b/src/calibre/ebooks/docx/styles.py @@ -437,8 +437,7 @@ class Styles(object): return self.classes.get(h, (None, None))[0] def generate_css(self, dest_dir, docx, notes_nopb, nosupsub): - embedded_families = set() - ef = self.fonts.embed_fonts(dest_dir, docx, embedded_families) + ef = self.fonts.embed_fonts(dest_dir, docx) s = '''\ body { font-family: %s; font-size: %s; color: %s } @@ -488,7 +487,6 @@ class Styles(object): ans = [] for (cls, css) in sorted(self.classes.itervalues(), key=lambda x:x[0]): - self.fonts.modify_font_properties(css, embedded_families) b = ('\t%s: %s;' % (k, v) for k, v in css.iteritems()) b = '\n'.join(b) ans.append('.%s {\n%s\n}\n' % (cls, b.rstrip(';'))) diff --git a/src/calibre/ebooks/oeb/polish/embed.py b/src/calibre/ebooks/oeb/polish/embed.py index 72a746e51d..5e2dbe7a6b 100644 --- a/src/calibre/ebooks/oeb/polish/embed.py +++ b/src/calibre/ebooks/oeb/polish/embed.py @@ -171,12 +171,9 @@ def embed_font(container, font, all_font_rules, report, warned): try: fonts = font_scanner.fonts_for_family(ff) except NoFonts: - try: - fonts = font_scanner.alt_fonts_for_family(ff) - except NoFonts: - report(_('Failed to find fonts for family: %s, not embedding') % ff) - warned.add(ff) - return + report(_('Failed to find fonts for family: %s, not embedding') % ff) + warned.add(ff) + return wt = weight_as_number(font.get('font-weight')) for f in fonts: if f['weight'] == wt and f['font-style'] == font.get('font-style', 'normal') and f['font-stretch'] == font.get('font-stretch', 'normal'): diff --git a/src/calibre/ebooks/oeb/transforms/embed_fonts.py b/src/calibre/ebooks/oeb/transforms/embed_fonts.py index abf5539af0..0da3f5556c 100644 --- a/src/calibre/ebooks/oeb/transforms/embed_fonts.py +++ b/src/calibre/ebooks/oeb/transforms/embed_fonts.py @@ -213,12 +213,9 @@ class EmbedFonts(object): try: fonts = font_scanner.fonts_for_family(ff) except NoFonts: - try: - fonts = font_scanner.alt_fonts_for_family(ff) - except NoFonts: - self.log.warn('Failed to find fonts for family:', ff, 'not embedding') - self.warned.add(ff) - return + self.log.warn('Failed to find fonts for family:', ff, 'not embedding') + self.warned.add(ff) + return weight = weight_as_number(style.get('font-weight', '400')) def do_embed(f): diff --git a/src/calibre/gui2/tweak_book/manage_fonts.py b/src/calibre/gui2/tweak_book/manage_fonts.py index b41a95e110..e7ec2abb40 100644 --- a/src/calibre/gui2/tweak_book/manage_fonts.py +++ b/src/calibre/gui2/tweak_book/manage_fonts.py @@ -129,10 +129,7 @@ class AllFonts(QAbstractTableModel): try: return font_scanner.fonts_for_family(name) except NoFonts: - try: - return font_scanner.alt_fonts_for_family(name) - except NoFonts: - return [] + return [] else: return name diff --git a/src/calibre/utils/fonts/metadata.py b/src/calibre/utils/fonts/metadata.py index b5ad0c4fd5..94abe6e69d 100644 --- a/src/calibre/utils/fonts/metadata.py +++ b/src/calibre/utils/fonts/metadata.py @@ -40,8 +40,7 @@ class FontMetadata(object): self.read_characteristics(f) f.seek(0) - self.font_family = (self.names.wws_family_name or - self.names.preferred_family_name or self.names.family_name) + self.font_family = self.names.family_name wt = self.characteristics.weight if wt == 400: wt = 'normal' diff --git a/src/calibre/utils/fonts/scanner.py b/src/calibre/utils/fonts/scanner.py index 39a2930a18..33e5c7d0dd 100644 --- a/src/calibre/utils/fonts/scanner.py +++ b/src/calibre/utils/fonts/scanner.py @@ -192,7 +192,7 @@ def build_families(cached_fonts, folders, family_attr='font-family'): class FontScanner(Thread): - CACHE_VERSION = 1 + CACHE_VERSION = 2 def __init__(self, folders=[], allowed_extensions={'ttf', 'otf'}): Thread.__init__(self) @@ -222,17 +222,6 @@ class FontScanner(Thread): except KeyError: raise NoFonts('No fonts found for the family: %r'%family) - def alt_fonts_for_family(self, family): - ''' Same as fonts_for_family() except that it uses the family name key - instead of the preferred_family_name key. This is needed because some - software, like Word uses the family name to refer to fonts instead of - the preferred family name. ''' - self.join() - try: - return self.alt_font_family_map[icu_lower(family)] - except KeyError: - raise NoFonts('No fonts found for the family: %r'%family) - def legacy_fonts_for_family(self, family): ''' Return a simple set of regular, bold, italic and bold-italic faces for @@ -371,7 +360,6 @@ class FontScanner(Thread): def build_families(self): self.font_family_map, self.font_families = build_families(self.cached_fonts, self.folders) - self.alt_font_family_map = build_families(self.cached_fonts, self.folders, 'family_name')[0] def write_cache(self): with self.cache: