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

This commit is contained in:
Kovid Goyal 2016-11-15 17:17:26 +05:30
parent 72471d6430
commit 77fd17c763
7 changed files with 11 additions and 57 deletions

View File

@ -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']

View File

@ -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(';')))

View File

@ -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'):

View File

@ -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):

View File

@ -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

View File

@ -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'

View File

@ -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: