def _CheckNameMatching(path): """Verifies the various name fields in the METADATA.pb file are sane. Args: path: A directory containing a METADATA.pb file. Returns: A list of ResultMessageTuple, one per validation performed. """ results = [] metadata = fonts.Metadata(path) name = metadata.name for font in metadata.fonts: # We assume style/weight is correct in METADATA style = font.style weight = font.weight values = [('name', name, font.name), ('filename', fonts.FilenameFor( name, style, weight, '.ttf'), font.filename), ('postScriptName', fonts.FilenameFor(name, style, weight), font.post_script_name), ('fullName', fonts.FullnameFor( name, style, weight), font.full_name)] for (key, expected, actual) in values: if expected != actual: results.append( _SadResult('%s METADATA %s/%d %s expected %s, got %s' % (name, style, weight, key, expected, actual), path, _FixMetadata(style, weight, key, expected))) if not results: results.append( _HappyResult('METADATA name consistently derived from "%s"' % name, path)) return results
def _CheckFontNameValues(path, name, font, ttf): """Check sanity of values in the 'name' table. Specifically the fullname and postScriptName. Args: path: Path to directory containing font. name: The name of the family. font: A font record from a METADATA.pb. ttf: A fontTools.ttLib.TTFont for the font. Returns: A list of ResultMessageTuple for tests performed. """ results = [] style = font.style weight = font.weight full_font_file = os.path.join(path, font.filename) expectations = [('family', fonts.NAME_FAMILY, name), ('postScriptName', fonts.NAME_PSNAME, fonts.FilenameFor(name, style, weight)), ('fullName', fonts.NAME_FULLNAME, fonts.FullnameFor(name, style, weight))] for (friendly_name, name_id, expected) in expectations: # If you have lots of name records they should ALL have the right value actuals = fonts.ExtractNames(ttf, name_id) for (idx, actual) in enumerate(actuals): results.append( ResultMessageTuple( expected == actual, '%s %s/%d \'name\' %s[%d] expected %s, got %s' % (name, style, weight, friendly_name, idx, expected, actual), full_font_file, _FixBadNameRecord(friendly_name, name_id, expected))) # should have at least one actual if not actuals: results.append( _SadResult( '%s %s/%d \'name\' %s has NO values' % (name, style, weight, friendly_name), full_font_file, _FixMissingNameRecord(friendly_name, name_id, expected))) return results