[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] xlibconf: easy access to X11 font and color lookup



Ok, so my curious streak has been salved (for tonight, anyway). 
The following script, for me, produces output like (following wrapped for
clarity)

    You have 80 fonts
    andale mono  : C:\WINDOWS\Fonts\Andalemo.TTF
    arial 
       : C:\WINDOWS\Fonts\ARIAL.TTF
      bold : C:\WINDOWS\Fonts\ARIALBD.TTF 
      bold narrow : C:\WINDOWS\Fonts\ARIALNB.TTF
      italic : C:\WINDOWS\Fonts\ARIALI.TTF 
      italic bold : C:\WINDOWS\Fonts\ARIALBI.TTF 
      italic bold narrow : C:\WINDOWS\Fonts\ARIALNBI.TTF 
      italic narrow : C:\WINDOWS\Fonts\ARIALNI.TTF 
      narrow : C:\WINDOWS\Fonts\ARIALN.TTF 
    arial black 
       : C:\WINDOWS\Fonts\ARIBLK.TTF
    berylium 
       : C:\WINDOWS\Fonts\Berylium.TTF 
      bold : C:\WINDOWS\Fonts\Beryliub.TTF 
      italic : C:\WINDOWS\Fonts\Beryliui.TTF 
      italic bold : C:\WINDOWS\Fonts\Berylibi.TTF
    bookman old style 
       : C:\WINDOWS\Fonts\bookos.ttf 
      bold : C:\WINDOWS\Fonts\bookosb.ttf 
      italic : C:\WINDOWS\Fonts\bookosi.ttf 
      italic bold : C:\WINDOWS\Fonts\bookosbi.ttf

where the indented lines consist of 'modifiers', 'fontfile'. I plan to make 
some sensible defaults for the modifiers to make them more closely match
the XLFD format that I'm already using. I'll then try and find a compromise
API, or else one that accepts keywords in whichever format is useful...

Anyway, if people could try the following on their windows box, that
would be good. Let me know, particularly, if there's any font modifiers
I don't know about.

The following, if I've stuffed it up, is also available from 
http://www.interlink.com.au/anthony/tech/xlibconf.py/winfont.py


"""
    This is a small test script that tries to find out 
    all the fonts and their names, as installed on a 
    windows box.
"""

def getAllWindowsFonts():
    import _winreg, win32api, os
    aReg = _winreg.ConnectRegistry(None,_winreg.HKEY_LOCAL_MACHINE)
    aKey = _winreg.OpenKey(aReg, 
             r"SOFTWARE\Microsoft\Windows\CurrentVersion\Fonts")
    fontdict={}
    WindowsDir = win32api.GetWindowsDirectory()
    FontDir = os.path.join(WindowsDir, 'Fonts')
    for i in range(1024):                                           
        try:
            name, fn, t = _winreg.EnumValue(aKey,i)
	    if os.sep not in fn:
		# it's a relative path
		fn = os.path.join(FontDir, fn)
            fontdict[name] = fn
        except EnvironmentError:                                               
            print "You have",i," fonts"
            break          
    _winreg.CloseKey(aKey)                                                  
    _winreg.CloseKey(aReg)
    return fontdict

def getFontDict():
    from sets import Set
    modifiers = Set([ 'italic', 'bold', 'demibold', 
                      'narrow', 'light', 'unicode' ])
    allfonts = getAllWindowsFonts()
    Fonts = {}
    for name, fontfile in allfonts.items():
        name=name.lower()
        if name.endswith('(truetype)'):
            name=name[:-11]
            nameEls = name.split()
            mods = []
            while 1:
                if nameEls[-1] in modifiers:
                    mods.append(nameEls[-1])
                    nameEls.pop()
                else:
                    fontname = ' '.join(nameEls)
                    break
            Fonts.setdefault(fontname,[]).append((mods,fontfile))
    return Fonts
    
if __name__ == "__main__":
    FontDict = getFontDict()
    fontlist = FontDict.keys()
    fontlist.sort()
    for fname in fontlist:
	fonts = FontDict[fname]
	fonts.sort()
	print fname
	for mods,fontfile in fonts:
	    print '  ', ' '.join(mods), ':', fontfile


____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org