def guessEncoding(font, given=None): """Attempt to guess/retrieve an encoding from the font itself Basically this will try to get the given encoding (unless it is None). If given is a single integer or a single-item tuple, we will attempt scan looking for any table matching given as the platform ID and returning the first sub table. If given is a two-value tuple, we will require explicit matching, and raise errors if the encoding cannot be retrieved. if given is None, we will return the first encoding in the font. XXX This needs some work, particularly for non-win32 platforms, where there is no preference embodied for the native encoding. """ if isinstance(given, tuple) and given: if len(given) == 2: if __debug__: if text_log: text_log.info("""Checking for explicitly required encoding %r""", given) if not font["cmap"].getcmap(*given): raise ValueError( """The specified font encoding %r does not appear to be available within the font %r. Available encodings: %s""" % (given, shortName(font), [(table.platformID, table.platEncID) for table in font["cmap"].tables]) ) return given elif len(given) > 2: raise TypeError("""Encoding must be None, a two-tuple, or an integer, got %r""" % (given,)) else: # treat as a single integer, regardless of number of integer's given = given[0] if isinstance(given, (int, long)): for table in font["cmap"].tables: if table.platformID == given: return (table.platformID, table.platEncID) raise ValueError( """Could not find encoding with specified platformID==%s within the font %r. Available encodings: %s""" % (given, shortName(font), [(table.platformID, table.platEncID) for table in font["cmap"].tables]) ) if sys.platform == "win32": prefered = (3, 1) # should have prefered values for Linux and Mac as well... for table in font["cmap"].tables: if (table.platformID, table.platEncID) == prefered: return prefered # just retrieve the first table's values for table in font["cmap"].tables: return (table.platformID, table.platEncID) raise ValueError( """There are no encoding tables within the font %r, likely a corrupt font-file""" % (shortName(font),) )
def __init__( self, fontStyle = None, filename = None, size = None, ): self._displayLists = {} self.fontStyle = fontStyle or None if filename is None or size is None: fontFile, weight, italics, size = PyGameFontProvider.match( fontStyle ) self.font = pygame_font.Font( fontFile, int(size)) self.filename = filename self.size = size if __debug__: text_log.info( """Created font %s""", self)
def lists( self, value, mode=None ): """Get a sequence of display-list integers for value Basically, this does a bit of trickery to do as-required compilation of display-lists, so that only those characters actually required by the displayed text are compiled. NOTE: Must be called from within the rendering thread and within the rendering pass! """ if __debug__: text_log.info( """lists %s(%s)""", self, repr(value)) lists = [] font = self.font for char in value: list, metrics = self.getChar( char, mode=mode ) if list is not None: lists.append( list ) if __debug__: text_log.info( """lists %s(%s)->%s""", self, repr(value), lists) return lists
def guessEncoding(font, given=None): """Attempt to guess/retrieve an encoding from the font itself Basically this will try to get the given encoding (unless it is None). If given is a single integer or a single-item tuple, we will attempt scan looking for any table matching given as the platform ID and returning the first sub table. If given is a two-value tuple, we will require explicit matching, and raise errors if the encoding cannot be retrieved. if given is None, we will return the first encoding in the font. XXX This needs some work, particularly for non-win32 platforms, where there is no preference embodied for the native encoding. """ if isinstance(given, tuple) and given: if len(given) == 2: if __debug__: if text_log: text_log.info( """Checking for explicitly required encoding %r""", given) if not font['cmap'].getcmap(*given): raise ValueError( """The specified font encoding %r does not appear to be available within the font %r. Available encodings: %s""" % ( given, shortName(font), [(table.platformID, table.platEncID) for table in font['cmap'].tables], )) return given elif len(given) > 2: raise TypeError( """Encoding must be None, a two-tuple, or an integer, got %r""" % (given, )) else: # treat as a single integer, regardless of number of integer's given = given[0] if isinstance(given, int): for table in font['cmap'].tables: if table.platformID == given: return (table.platformID, table.platEncID) raise ValueError( """Could not find encoding with specified platformID==%s within the font %r. Available encodings: %s""" % ( given, shortName(font), [(table.platformID, table.platEncID) for table in font['cmap'].tables], )) if sys.platform == 'win32': prefered = (3, 1) # should have prefered values for Linux and Mac as well... for table in font['cmap'].tables: if (table.platformID, table.platEncID) == prefered: return prefered # just retrieve the first table's values for table in font['cmap'].tables: return (table.platformID, table.platEncID) raise ValueError( """There are no encoding tables within the font %r, likely a corrupt font-file""" % (shortName(font), ))