Edit Book: Check Book: Fix incorrect handling of font family names with spaces in them that are not quoted. Fixes #1596053 [Edit-book: Incorrect reporting and fixing mismatched font-family name](https://bugs.launchpad.net/calibre/+bug/1596053)

This commit is contained in:
Kovid Goyal 2016-06-30 13:53:57 +05:30
parent bb4fe4d924
commit 792b7e17b3

View File

@ -15,6 +15,7 @@ from calibre.ebooks.oeb.polish.container import OEB_FONTS
from calibre.ebooks.oeb.polish.fonts import change_font_family_value from calibre.ebooks.oeb.polish.fonts import change_font_family_value
from calibre.ebooks.oeb.polish.pretty import pretty_script_or_style from calibre.ebooks.oeb.polish.pretty import pretty_script_or_style
from calibre.utils.fonts.utils import get_all_font_names from calibre.utils.fonts.utils import get_all_font_names
from tinycss.fonts3 import parse_font_family, parse_font, serialize_font_family, serialize_font
class InvalidFont(BaseError): class InvalidFont(BaseError):
@ -33,10 +34,22 @@ def fix_property(prop, css_name, font_name):
def fix_declaration(style, css_name, font_name): def fix_declaration(style, css_name, font_name):
changed = False changed = False
for x in ('font-family', 'font'): ff = style.getProperty('font-family')
prop = style.getProperty(x) if ff is not None:
if prop is not None: fams = parse_font_family(ff.propertyValue.cssText)
changed |= fix_property(prop, css_name, font_name) nfams = [font_name if x == css_name else x for x in fams]
if fams != nfams:
ff.propertyValue.cssText = serialize_font_family(nfams)
changed = True
ff = style.getProperty('font')
if ff is not None:
props = parse_font(ff.propertyValue.cssText)
fams = props.get('font-family') or []
nfams = [font_name if x == css_name else x for x in fams]
if fams != nfams:
props['font-family'] = nfams
ff.propertyValue.cssText = serialize_font(props)
changed = True
return changed return changed
def fix_sheet(sheet, css_name, font_name): def fix_sheet(sheet, css_name, font_name):
@ -120,10 +133,9 @@ def check_fonts(container):
font_name = font_map.get(fname, None) font_name = font_map.get(fname, None)
if font_name is None: if font_name is None:
continue continue
ff = rule.style.getPropertyCSSValue('font-family') families = parse_font_family(rule.style.getPropertyValue('font-family'))
if ff is not None and ff.length > 0: if families:
ff = getattr(ff.item(0), 'value', None) if families[0] != font_name:
if ff is not None and ff != font_name: errors.append(FontAliasing(font_name, families[0], name, line_offset))
errors.append(FontAliasing(font_name, ff, name, line_offset))
return errors return errors