def metadata( self, filename, force = 0 ): """Retrieve metadata from font file filename -- fully specified path to the font file force -- if false, and the metadata is already available for this file, do not access the font file to retrieve, just return the existing metadata. return value: tuple of: filename -- fully specified absolute path modifiers -- (weightInteger, italicsFlag) specificName -- specific name of the particular font stored in the given file, the name of the "modified" font fontName -- name of the general font which the modifiers are specialising specifier -- family specifier, two-tuple of high-level and sub-level font classifications based on font characteristics as encoded in the font file. """ filename = os.path.abspath( filename ) if self.files.has_key( filename ) and not force: return self.specificFonts.get( self.files[filename] ) font = describe.openFont(filename) try: modifiers = describe.modifiers( font ) except (KeyError,AttributeError), err: modifiers = (None,None)
def __init__(self, fontfile): if not HAS_FONTTOOLS: return # describe.shortName(font) # This will return (font_name, font_family), like this: # ("MyFont Bold Italic","MyFont") # ("MyFont","MyFont") # describe.family(font) # This will return the font families based on some internal integer. # For "Arial Unicode MS", it returns ("SANS","GOTHIC-NEO-GROTESQUE"). # For many thirdy-party fonts, it returns ("ANY","ANY"). # describe.modifiers(font) # This returns two integers (weight,italic). # The first one is font weight [100..900]. The second one is 0 or 1. # describe.weightName(number) # This converts the font weight number into a name (like normal, bold). font = ttLib.TTFont(fontfile) short_name = describe.shortName(font) family = describe.family(font) modifiers = describe.modifiers(font) self.filename = fontfile self.name = short_name[0] self.name_family = short_name[1] self.family = '/'.join(family) self.weight = modifiers[0] self.weight_name = describe.weightName(modifiers[0]) if modifiers[1]: self.italic = True else: self.italic = False
def metadata(self, filename, force=0): """Retrieve metadata from font file filename -- fully specified path to the font file force -- if false, and the metadata is already available for this file, do not access the font file to retrieve, just return the existing metadata. return value: tuple of: filename -- fully specified absolute path modifiers -- (weightInteger, italicsFlag) specificName -- specific name of the particular font stored in the given file, the name of the "modified" font fontName -- name of the general font which the modifiers are specialising specifier -- family specifier, two-tuple of high-level and sub-level font classifications based on font characteristics as encoded in the font file. """ filename = os.path.abspath(filename) if self.files.has_key(filename) and not force: return self.specificFonts.get(self.files[filename]) font = describe.openFont(filename) try: modifiers = describe.modifiers(font) except (KeyError, AttributeError), err: modifiers = (None, None)
def buildTable( filenames=None, failureCallback=None ): """Build table mapping {family:(font:{modifiers:(name,file)})} filenames -- if provided, list of filenames to scan, otherwise the full set of system fonts provided by findsystem will be used. failureCallback -- if provided, a function taking three arguments, the failing filename, an error-type code, and the error object. If processing should stop, raise an error. codes: 0 -- couldn't open the font file 1 -- couldn't find modifiers in the font file 2 -- couldn't find font-name in the font file 3 -- couldn't find the generic family specifier for the font """ if filenames is None: filenames = findsystem.findFonts() table = {} for filename in filenames: try: font = describe.openFont(filename) except Exception as err: if failureCallback: failureCallback( filename, 0, err ) else: try: modifiers = describe.modifiers( font ) except (KeyError,AttributeError) as err: if failureCallback: failureCallback( filename, 1, err ) modifiers = (None,None) try: specificName, fontName = describe.shortName( font ) except (KeyError,AttributeError) as err: if failureCallback: failureCallback( filename, 2, err ) else: try: specifier = describe.family(font) except KeyError: if failureCallback: failureCallback( filename, 3, err ) else: table.setdefault( specifier, {} ).setdefault( fontName, {} )[modifiers] = (specificName,filename) return table
def buildTable(filenames=None, failureCallback=None): """Build table mapping {family:(font:{modifiers:(name,file)})} filenames -- if provided, list of filenames to scan, otherwise the full set of system fonts provided by findsystem will be used. failureCallback -- if provided, a function taking three arguments, the failing filename, an error-type code, and the error object. If processing should stop, raise an error. codes: 0 -- couldn't open the font file 1 -- couldn't find modifiers in the font file 2 -- couldn't find font-name in the font file 3 -- couldn't find the generic family specifier for the font """ if filenames is None: filenames = findsystem.findFonts() table = {} for filename in filenames: try: font = describe.openFont(filename) except Exception as err: if failureCallback: failureCallback(filename, 0, err) else: try: modifiers = describe.modifiers(font) except (KeyError, AttributeError) as err: if failureCallback: failureCallback(filename, 1, err) modifiers = (None, None) try: specificName, fontName = describe.shortName(font) except (KeyError, AttributeError) as err: if failureCallback: failureCallback(filename, 2, err) else: try: specifier = describe.family(font) except KeyError: if failureCallback: failureCallback(filename, 3, err) else: table.setdefault(specifier, {}).setdefault( fontName, {})[modifiers] = (specificName, filename) return table
def process(font: TTFont): print(describe.shortName(font)) weight, is_ita = describe.modifiers(font) print(weight, is_ita) print(describe.weightName(weight), 'italic' if is_ita else '')