def check_fonts(container): font_map = {} errors = [] for name, mt in container.mime_map.iteritems(): if mt in OEB_FONTS: raw = container.raw_data(name) try: name_map = get_all_font_names(raw) except Exception as e: errors.append(InvalidFont(_('Not a valid font: %s') % e, name)) continue font_map[name] = name_map.get('family_name', None) or name_map.get( 'preferred_family_name', None) or name_map.get( 'wws_family_name', None) try: embeddable, fs_type = is_font_embeddable(raw) except UnsupportedFont: embeddable = True if not embeddable: errors.append(NotEmbeddable(name, fs_type)) sheets = [] for name, mt in container.mime_map.iteritems(): if mt in OEB_STYLES: try: sheets.append((name, container.parsed(name), None)) except Exception: pass # Could not parse, ignore elif mt in OEB_DOCS: for style in container.parsed(name).xpath( '//*[local-name()="style"]'): if style.get('type', 'text/css') == 'text/css' and style.text: sheets.append((name, container.parse_css(style.text), style.sourceline)) for name, sheet, line_offset in sheets: for rule in sheet.cssRules.rulesOfType(CSSRule.FONT_FACE_RULE): src = rule.style.getPropertyCSSValue('src') if src is not None and src.length > 0: href = getattr(src.item(0), 'uri', None) if href is not None: fname = container.href_to_name(href, name) font_name = font_map.get(fname, None) if font_name is None: continue families = parse_font_family( rule.style.getPropertyValue('font-family')) if families: if families[0] != font_name: errors.append( FontAliasing(font_name, families[0], name, line_offset)) return errors
def check_fonts(container): font_map = {} errors = [] for name, mt in iteritems(container.mime_map): if mt in OEB_FONTS: raw = container.raw_data(name) try: name_map = get_all_font_names(raw) except Exception as e: errors.append(InvalidFont(_('Not a valid font: %s') % e, name)) continue font_map[name] = name_map.get('family_name', None) or name_map.get('preferred_family_name', None) or name_map.get('wws_family_name', None) try: embeddable, fs_type = is_font_embeddable(raw) except UnsupportedFont: embeddable = True if not embeddable: errors.append(NotEmbeddable(name, fs_type)) sheets = [] for name, mt in iteritems(container.mime_map): if mt in OEB_STYLES: try: sheets.append((name, container.parsed(name), None)) except Exception: pass # Could not parse, ignore elif mt in OEB_DOCS: for style in container.parsed(name).xpath('//*[local-name()="style"]'): if style.get('type', 'text/css') == 'text/css' and style.text: sheets.append((name, container.parse_css(style.text), style.sourceline)) for name, sheet, line_offset in sheets: for rule in sheet.cssRules.rulesOfType(CSSRule.FONT_FACE_RULE): src = rule.style.getPropertyCSSValue('src') if src is not None and src.length > 0: href = getattr(src.item(0), 'uri', None) if href is not None: fname = container.href_to_name(href, name) font_name = font_map.get(fname, None) if font_name is None: continue families = parse_font_family(rule.style.getPropertyValue('font-family')) if families: if families[0] != font_name: errors.append(FontAliasing(font_name, families[0], name, line_offset)) return errors