How do I get the OS System Font Directory(Cross-Platform) in python?
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Fri Jul 12 00:55:35 EDT 2013
On Thu, 11 Jul 2013 21:24:00 -0700, Metallicow wrote:
> Forgot to add >>> part. Is there any way to edit posts?
Not unless thousands of people give you access to their computer so you
can edit the emails in their inboxes.
When you send a post to a public mailing list, its out their on fifty
thousand computers and a dozen public archives. No, you can't change your
mind and edit what you've already said.
> Don't see an edit linky anywhere. Is there something
> wrong with using os.environ...? win32api stuff is another dependency. Is
> it really needed?
If you want to do it the right way, yes.
In principle, any Windows machine could set the font directory to any
valid directory. The only way to be sure you have got the right one is to
ask Windows for the font directory, and that requires win32api.
If you intend for this to be usable everywhere, my approach would be this:
1) You need a separate function for each platform you intend to support.
At the very least, you should support Windows (2000, XP, Vista, 7 and 8),
OS-X (Mac), Linux. You should also strongly consider supporting Android,
FreeBSD, OpenBSD, and Solaris. If you intend running under older versions
of Python, you should also consider supporting Windows CE. You might even
like to support iOS.
2) Some of these (Windows, probably Mac and iOS) will require a call to
the OS to find the current directory. That means a win32api or equivalent
call.
3) If win32api is not available, you might like to fall back on a
heuristic to guess the right directory. But remember that is only a
little bit better than hard-coding a directory.
4) For Linux and Free- and OpenBSD, there is no standard font directory.
You may be able to make a call to the GUI toolkit to ask what it thinks
the font directory (or directories!) is, but there are many different
toolkits, and they can all be active at the same time. E.g. I might run a
Gnome app and a KDE app both under XFCE, plus OpenOffice, and all three
might disagree as to what fonts I have available.
The standard way to locate fonts in Linux is to look at two files,
/etc/fonts/fonts.conf and /etc/fonts/local.conf, which list the locations
you should check. Either, or both, files may be missing. Standard
locations include:
/usr/share/fonts
/usr/local/share/fonts
/home/<username>/.fonts
/usr/X11R6/lib/X11/fonts
/usr/share/X11/fonts/Type1
/usr/share/X11/fonts/OTF
And of course, applications like OpenOffice might keep their own,
private, font directory, just because.
5) Now that you have a function for each OS you support, you can create a
master function that detects the OS and calls the appropriate one. Now at
last you have a simple function get_font_directory() that works
everywhere.
Now that you see how complex it is to write this "simple" function, are
you surprised that one doesn't yet exist?
:-)
--
Steven
More information about the Python-list
mailing list