Edit book/Book polishing: Fix subsetting of fonts whose @font-face rules use the local() or format() CSS functions not working

This commit is contained in:
Kovid Goyal 2014-12-03 19:09:54 +05:30
parent b8b794175f
commit e4013c9e11

View File

@ -275,6 +275,8 @@ class StatsCollector(object):
# Weed out invalid font-face rules # Weed out invalid font-face rules
rules = [] rules = []
import tinycss
parser = tinycss.make_full_parser()
for rule in font_face_rules: for rule in font_face_rules:
ff = rule.get('font-family', None) ff = rule.get('font-family', None)
if not ff: if not ff:
@ -288,16 +290,22 @@ class StatsCollector(object):
src = rule.get('src', None) src = rule.get('src', None)
if not src: if not src:
continue continue
if src.startswith('url(') and src.endswith(')') and src[4] not in {'"', "'"}: try:
# Quote the url otherwise cssutils fails to parse it if it has tokens = parser.parse_stylesheet('@font-face { src: %s }' % src).rules[0].declarations[0].value
# ' or " in it except Exception:
src = "url('" + src[4:-1].replace("'", "\\'") + "')" self.log.warn('Failed to parse @font-family src: %s' % src)
style = self.parser.parseStyle('background-image:%s'%src, validate=False) continue
src = style.getProperty('background-image').propertyValue[0].uri for token in tokens:
name = self.href_to_name(src, '@font-face rule') if token.type == 'URI':
if name is None: uv = token.value
if uv:
sn = self.href_to_name(uv, '@font-face rule')
if sn is not None:
rule['src'] = sn
break
else:
self.log.warn('The @font-face rule refers to a font file that does not exist in the book: %s' % src)
continue continue
rule['src'] = name
normalize_font_properties(rule) normalize_font_properties(rule)
rule['width'] = widths[rule['font-stretch']] rule['width'] = widths[rule['font-stretch']]
rule['weight'] = int(rule['font-weight']) rule['weight'] = int(rule['font-weight'])