MCPcopy Create free account
hub / github.com/PageBot/PageBot / getFontPaths

Function getFontPaths

Lib/pagebot/fonttoolbox/fontpaths.py:95–175  ·  view source on GitHub ↗

Answers a dictionary with all available font paths on the platform, key is the single file name. Typically on Mac OS Xthe font paths available in these respective directories are returned: ('/Library/Fonts', '/Users/ /Library/Fonts', '/Users/ / /PageBot/Fon

(extraPaths=None)

Source from the content-addressed store, hash-verified

93 return paths
94
95def getFontPaths(extraPaths=None):
96 """Answers a dictionary with all available font paths on the platform, key
97 is the single file name. Typically on Mac OS Xthe font paths
98 available in these respective directories are returned:
99
100 ('/Library/Fonts', '/Users/<username>/Library/Fonts', '/Users/<username>/<path-to>/PageBot/Fonts')
101
102 Locally defined fonts with the same file name will overwrite the font files
103 that are located deeper.
104 TODO: also check app resources path.
105
106 >>> import os
107 >>> testFontsPath = getTestFontsPath()
108 >>> os.path.exists(testFontsPath + '/fontbureau/Amstelvar-Roman-VF.ttf')
109 True
110 >>> fontPaths = getFontPaths() # Only default paths on the platform
111 >>> 'Amstelvar-Roman-VF' in fontPaths
112 True
113 >>> fontPaths = getFontPaths(testFontsPath + '/fontbureau/Amstelvar-Roman-VF.ttf') # As single extra path
114 >>> 'Amstelvar-Roman-VF' in fontPaths
115 True
116 >>> fontPaths = getFontPaths([testFontsPath + '/fontbureau/Amstelvar-Roman-VF.ttf']) # As list of extra paths
117 >>> 'Amstelvar-Roman-VF' in fontPaths
118 True
119 >>> fontPaths = getFontPaths(testFontsPath + '/OtherFont.ttf')
120 >>> 'OtherFont' in fontPaths # Ignore if extra paths don't exists.
121 False
122 >>> fontPaths = getFontPaths()
123 """
124 global FONT_PATHS
125 if extraPaths is not None:
126 FONT_PATHS = {} # Force (new) initialization
127
128 if not FONT_PATHS:
129
130 # If forced or initial call, collect the font paths on this
131 # platform.
132 if os.name == 'posix':
133 paths = []
134
135 # TODO: only for darwin platform.
136 # Try typical OSX font folders:
137 paths += ['/Library/Fonts', os.path.expanduser('~/Library/Fonts')]
138
139 # Add other typical GNU+Linux font folders here to look at:
140 paths += ['/usr/share/fonts']
141
142 for path in paths:
143 if os.path.exists(path):
144 _recursivelyCollectFontPaths(path, FONT_PATHS)
145
146 elif os.name in ('nt', 'os2', 'ce', 'java', 'riscos'):
147 # Add other typical Windows font folders here to look at.
148 pass
149 else:
150 raise NotImplementedError('Unknown platform type "%s"' % os.name)
151
152 # Add the name name:fileName combinations from the predefined

Callers 4

getFamilyPathsFunction · 0.90
findFontsFunction · 0.90
findFontFunction · 0.90
getFontPathOfFontFunction · 0.85

Calls 2

getTestFontsPathFunction · 0.85

Tested by

no test coverage detected