def win32FontDirectory(): """ Return the user-specified font directory for Win32. This is looked up from the registry key:: \\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Fonts If the key is not found, $WINDIR/Fonts will be returned. """ try: from matplotlib.externals.six.moves import winreg except ImportError: pass # Fall through to default else: try: user = winreg.OpenKey(winreg.HKEY_CURRENT_USER, MSFolders) try: try: return winreg.QueryValueEx(user, 'Fonts')[0] except OSError: pass # Fall through to default finally: winreg.CloseKey(user) except OSError: pass # Fall through to default return os.path.join(os.environ['WINDIR'], 'Fonts')
def win32InstalledFonts(directory=None, fontext='ttf'): """ Search for fonts in the specified font directory, or use the system directories if none given. A list of TrueType font filenames are returned by default, or AFM fonts if *fontext* == 'afm'. """ from matplotlib.externals.six.moves import winreg if directory is None: directory = win32FontDirectory() fontext = get_fontext_synonyms(fontext) key, items = None, {} for fontdir in MSFontDirectories: try: local = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, fontdir) except OSError: continue if not local: return list_fonts(directory, fontext) try: for j in range(winreg.QueryInfoKey(local)[1]): try: key, direc, any = winreg.EnumValue( local, j) if not is_string_like(direc): continue if not os.path.dirname(direc): direc = os.path.join(directory, direc) direc = direc.split('\0', 1)[0] # direc = os.path.abspath(direc).lower() if os.path.splitext(direc)[1][1:] in fontext: items[direc] = 1 except EnvironmentError: continue except WindowsError: continue except MemoryError: continue return list(six.iterkeys(items)) finally: winreg.CloseKey(local) return None