How do I get the OS System Font Directory(Cross-Platform) in python?

Chris “Kwpolska” Warrick kwpolska at gmail.com
Thu Jul 11 18:12:43 CEST 2013


On Thu, Jul 11, 2013 at 08:54:17AM -0700, Metallicow wrote:
> For a portable font install tool.
> 
> Finding if a particular font exists,
> useful when testing apps in virtual environent,
> rendering text from a font,
> Font file manipulations,
> etc..
> -- 
> http://mail.python.org/mailman/listinfo/python-list

Then do a nice little `if` checking the user’s OS.

http://docs.python.org/2/library/sys.html#sys.platform hints what to use,
and you need to do:

if sys.platform.startswith('win') or sys.platform.startswith('cygwin'):
    FONTDIRS = [os.path.join(os.environ['WINDIR'], 'Fonts')
elif sys.platform.startswith('darwin'):
    # [see below and devise it yourself]
else: # linux, *bsd and everything else
    # [see below, commit suicide and develop this bit]

Windows:
    The easiest of those three, all the fonts are in %WINDIR%/Fonts.
    In Python: os.path.join(os.environ['WINDIR'], 'Fonts')

Mac OS X:
    http://support.apple.com/kb/ht2435 (and not all of them are
    guaranteed to exist).  `os.expanduser()` may be useful for
    that first one.

Linux, and everything else running that fancy open stuff (eg. *BSD):
    Hardcore.  You just need to parse a nice tiny 155-line XML file.

    It’s /etc/fonts/fonts.conf, and it has some <dir> tags.
    Some of them may have a `prefix="xdg"` attribute which makes you
    prepend the value with $XDG_DATA_HOME (~/.local/share if that is
    empty).  You also need `os.expanduser()`.

    Oh: and you need to go recursively through them, as subdirectories
    are also checked for fonts (and it probably goes further)

-- 
Kwpolska <http://kwpolska.tk> | GPG KEY: 5EAAEA16
stop html mail                | always bottom-post
http://asciiribbon.org        | http://caliburn.nl/topposting.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20130711/1f462dd3/attachment.sig>


More information about the Python-list mailing list