Exemplo n.º 1
0
    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 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
Exemplo n.º 3
0
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
Exemplo n.º 4
0
	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 filename in self.files and not force:
			return self.specificFonts.get( self.files[filename] )
		font = describe.openFont(filename)
		try:
			modifiers = describe.modifiers( font )
		except (KeyError,AttributeError) as err:
			modifiers = (None,None)
		specificName, fontName = describe.shortName( font )
		specifier = describe.family(font)
		return (
			filename,
			modifiers,
			specificName,
			fontName,
			specifier,
		)
Exemplo n.º 5
0
    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:
            modifiers = (None, None)
        except AttributeError:
            modifiers = (None, None)
        specificName, fontName = describe.shortName(font)
        specifier = describe.family(font)
        return (
            filename,
            modifiers,
            specificName,
            fontName,
            specifier,
        )
Exemplo n.º 6
0
def glyphName(font, char, encoding=None, warnOnFailure=1):
    """Retrieve the glyph name for the given character

    XXX
        Not sure what the effect of the Unicode mapping
        will be given the use of ord...
    """
    glyfName = explicitGlyph(font, char, encoding)
    if glyfName is None:
        encoding = describe.guessEncoding(font)  #KH
        cmap = font['cmap']  #KH
        table = cmap.getcmap(*encoding)  #KH
        glyfName = table.cmap.get(-1)
        if glyfName is None:
            glyfName = font['glyf'].glyphOrder[0]
            if text_log and warnOnFailure:
                text_log.warn(
                    """Unable to find glyph name for %r, in %r using first glyph in table (%r)""",
                    char, describe.shortName(font), glyfName)
    return glyfName
Exemplo n.º 7
0
def glyphName( font, char, encoding=None, warnOnFailure=1 ):
    """Retrieve the glyph name for the given character

    XXX
        Not sure what the effect of the Unicode mapping
        will be given the use of ord...
    """
    glyfName = explicitGlyph( font, char, encoding )
    if glyfName is None:
        encoding = describe.guessEncoding( font )       #KH
        cmap = font['cmap']                             #KH
        table = cmap.getcmap( *encoding )               #KH
        glyfName = table.cmap.get( -1)
        if glyfName is None:
            glyfName = font['glyf'].glyphOrder[0]
            if text_log and warnOnFailure:
                text_log.warn(
                    """Unable to find glyph name for %r, in %r using first glyph in table (%r)""",
                    char,
                    describe.shortName(font),
                    glyfName
                )
    return glyfName
Exemplo n.º 8
0
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 '')
Exemplo n.º 9
0
class Registry(object):
    """Object providing centralized registration of TTF files

    Attributes:
        families -- mapping from TrueType font families
            to sub-families and then to general fonts
            (as a set of font-names).
        fonts -- mapping from general fonts to modifiers to
            specific font instances
        specificFonts -- mapping from specific font names
            to the entire "metrics" set for the particular
            font.
        files -- mapping from (absolute) filenames to
            specific font names
        shortFiles -- mapping from font filename basenames
            to font-file-lists
        DIRTY -- flag indicating whether the registry has
            had a new font registered (i.e. whether it should
            be saved out to disk).
    """
    DIRTY = 0
    filename = ""

    def __init__(self):
        """Initialize the Registry"""
        self.families = {}
        self.fonts = {}
        self.specificFonts = {}
        self.files = {}
        self.shortFiles = {}

    def clear(self):
        """Clear out the all tables and mark unchanged"""
        self.families.clear()
        self.fonts.clear()
        self.specificFonts.clear()
        self.files.clear()
        self.shortFiles.clear()
        self.dirty(0)

    def dirty(self, dirty=1):
        """Mark the registry as changed/unchanged"""
        self.DIRTY = dirty

    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)
        specificName, fontName = describe.shortName(font)
        specifier = describe.family(font)
        return (
            filename,
            modifiers,
            specificName,
            fontName,
            specifier,
        )