mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
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:
parent
72471d6430
commit
77fd17c763
@ -114,7 +114,7 @@ class Fonts(object):
|
|||||||
name = f.name if variant in f.embedded else f.family_name
|
name = f.name if variant in f.embedded else f.family_name
|
||||||
return '"%s", %s' % (name.replace('"', ''), f.css_generic_family)
|
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 = []
|
defs = []
|
||||||
dest_dir = os.path.join(dest_dir, 'fonts')
|
dest_dir = os.path.join(dest_dir, 'fonts')
|
||||||
for name, variant in self.used:
|
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 = ['%s: %s' % (k, v) for k, v in d.iteritems()]
|
||||||
d = ';\n\t'.join(d)
|
d = ';\n\t'.join(d)
|
||||||
defs.append('@font-face {\n\t%s\n}\n' % 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)
|
return '\n'.join(defs)
|
||||||
|
|
||||||
def write(self, name, dest_dir, docx, variant):
|
def write(self, name, dest_dir, docx, variant):
|
||||||
@ -155,23 +153,3 @@ class Fonts(object):
|
|||||||
dest.write(raw[32:])
|
dest.write(raw[32:])
|
||||||
|
|
||||||
return fname
|
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']
|
|
||||||
|
@ -437,8 +437,7 @@ class Styles(object):
|
|||||||
return self.classes.get(h, (None, None))[0]
|
return self.classes.get(h, (None, None))[0]
|
||||||
|
|
||||||
def generate_css(self, dest_dir, docx, notes_nopb, nosupsub):
|
def generate_css(self, dest_dir, docx, notes_nopb, nosupsub):
|
||||||
embedded_families = set()
|
ef = self.fonts.embed_fonts(dest_dir, docx)
|
||||||
ef = self.fonts.embed_fonts(dest_dir, docx, embedded_families)
|
|
||||||
|
|
||||||
s = '''\
|
s = '''\
|
||||||
body { font-family: %s; font-size: %s; color: %s }
|
body { font-family: %s; font-size: %s; color: %s }
|
||||||
@ -488,7 +487,6 @@ class Styles(object):
|
|||||||
|
|
||||||
ans = []
|
ans = []
|
||||||
for (cls, css) in sorted(self.classes.itervalues(), key=lambda x:x[0]):
|
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 = ('\t%s: %s;' % (k, v) for k, v in css.iteritems())
|
||||||
b = '\n'.join(b)
|
b = '\n'.join(b)
|
||||||
ans.append('.%s {\n%s\n}\n' % (cls, b.rstrip(';')))
|
ans.append('.%s {\n%s\n}\n' % (cls, b.rstrip(';')))
|
||||||
|
@ -171,12 +171,9 @@ def embed_font(container, font, all_font_rules, report, warned):
|
|||||||
try:
|
try:
|
||||||
fonts = font_scanner.fonts_for_family(ff)
|
fonts = font_scanner.fonts_for_family(ff)
|
||||||
except NoFonts:
|
except NoFonts:
|
||||||
try:
|
report(_('Failed to find fonts for family: %s, not embedding') % ff)
|
||||||
fonts = font_scanner.alt_fonts_for_family(ff)
|
warned.add(ff)
|
||||||
except NoFonts:
|
return
|
||||||
report(_('Failed to find fonts for family: %s, not embedding') % ff)
|
|
||||||
warned.add(ff)
|
|
||||||
return
|
|
||||||
wt = weight_as_number(font.get('font-weight'))
|
wt = weight_as_number(font.get('font-weight'))
|
||||||
for f in fonts:
|
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'):
|
if f['weight'] == wt and f['font-style'] == font.get('font-style', 'normal') and f['font-stretch'] == font.get('font-stretch', 'normal'):
|
||||||
|
@ -213,12 +213,9 @@ class EmbedFonts(object):
|
|||||||
try:
|
try:
|
||||||
fonts = font_scanner.fonts_for_family(ff)
|
fonts = font_scanner.fonts_for_family(ff)
|
||||||
except NoFonts:
|
except NoFonts:
|
||||||
try:
|
self.log.warn('Failed to find fonts for family:', ff, 'not embedding')
|
||||||
fonts = font_scanner.alt_fonts_for_family(ff)
|
self.warned.add(ff)
|
||||||
except NoFonts:
|
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'))
|
weight = weight_as_number(style.get('font-weight', '400'))
|
||||||
|
|
||||||
def do_embed(f):
|
def do_embed(f):
|
||||||
|
@ -129,10 +129,7 @@ class AllFonts(QAbstractTableModel):
|
|||||||
try:
|
try:
|
||||||
return font_scanner.fonts_for_family(name)
|
return font_scanner.fonts_for_family(name)
|
||||||
except NoFonts:
|
except NoFonts:
|
||||||
try:
|
return []
|
||||||
return font_scanner.alt_fonts_for_family(name)
|
|
||||||
except NoFonts:
|
|
||||||
return []
|
|
||||||
else:
|
else:
|
||||||
return name
|
return name
|
||||||
|
|
||||||
|
@ -40,8 +40,7 @@ class FontMetadata(object):
|
|||||||
self.read_characteristics(f)
|
self.read_characteristics(f)
|
||||||
|
|
||||||
f.seek(0)
|
f.seek(0)
|
||||||
self.font_family = (self.names.wws_family_name or
|
self.font_family = self.names.family_name
|
||||||
self.names.preferred_family_name or self.names.family_name)
|
|
||||||
wt = self.characteristics.weight
|
wt = self.characteristics.weight
|
||||||
if wt == 400:
|
if wt == 400:
|
||||||
wt = 'normal'
|
wt = 'normal'
|
||||||
|
@ -192,7 +192,7 @@ def build_families(cached_fonts, folders, family_attr='font-family'):
|
|||||||
|
|
||||||
class FontScanner(Thread):
|
class FontScanner(Thread):
|
||||||
|
|
||||||
CACHE_VERSION = 1
|
CACHE_VERSION = 2
|
||||||
|
|
||||||
def __init__(self, folders=[], allowed_extensions={'ttf', 'otf'}):
|
def __init__(self, folders=[], allowed_extensions={'ttf', 'otf'}):
|
||||||
Thread.__init__(self)
|
Thread.__init__(self)
|
||||||
@ -222,17 +222,6 @@ class FontScanner(Thread):
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
raise NoFonts('No fonts found for the family: %r'%family)
|
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):
|
def legacy_fonts_for_family(self, family):
|
||||||
'''
|
'''
|
||||||
Return a simple set of regular, bold, italic and bold-italic faces for
|
Return a simple set of regular, bold, italic and bold-italic faces for
|
||||||
@ -371,7 +360,6 @@ class FontScanner(Thread):
|
|||||||
|
|
||||||
def build_families(self):
|
def build_families(self):
|
||||||
self.font_family_map, self.font_families = build_families(self.cached_fonts, self.folders)
|
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):
|
def write_cache(self):
|
||||||
with self.cache:
|
with self.cache:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user