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

M

Metallicow

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

Need a simple script for
Windows, Linux, Mac, etc..

Or using wxPython.

I can't seem to find anything that works, and I don't want to hard-code paths.
 
C

Chris “Kwpolska†Warrick

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

Need a simple script for
Windows, Linux, Mac, etc..

Or using wxPython.

I can't seem to find anything that works, and I don't want to hard-code paths.

Why do you want the paths to the fonts? You do not need them 99.9% of
the time. So, what *exactly* are you trying to accomplish?
 
M

Metallicow

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..
 
C

Chris “Kwpolska†Warrick

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..

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

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.20 (GNU/Linux)

iQEcBAEBAgAGBQJR3tl4AAoJEHECPb1equoW6H8IAK5oDUsJ0QCvzJzokstoYJte
FewqkDVoBj5X1VDoazDRqZ2IDvcEv1VgDqfqk6js8kf7dFb/oEQNh03+evegxupq
kei0SN8dQ6a7zrUHiYEsVSLp8pWHGylxJ/K6aYtpL8si2h06xxnrYIxsOtOm7Ii8
fXS98gQy8s6j8O0YaGQYkTxQo6GLbyiFvi9i2ZyrbuNo+5Xf9XjXsGO6o6SeMw2A
8FUnl3TBD5isNN9wAonOv4iw4cQ15KG9Zjfjqr4A0wSnrGI9JAXFCubJxDpu28xj
eI1/hbk/3jUTGFK3x8ANiqL3ndf88J/aeqUxlfDRsmO4A+YWX3Hp1aOUuqItLY4=
=pjFt
-----END PGP SIGNATURE-----
 
M

Metallicow

@ Chris “Kwpolska” Warrick
Thanks, that is a start anyway.
a Pure-Python way was what I was wanting, not win32api stuff.

"C:\Windows\Fonts"
The windows path proves valid. Works on XP, Vista, 7. Not sure about win8....?

Don't have a mac handy, but the link should be enough to help write some code before testing. I don't own a mac, but IIRC macs come with python installed, so a trip to the apple shop might be in order.

As far as linux goes, basically Ubuntu/Kubuntu(dirivs), Mandriva/Mageia, Mint, and some of the more popular distros is all I am really looking for. Checking for these is VirtualBox shouldn't be too hard with your instructionsand platform.linux_distribution()

Thanks again.
Here is the 'Open Font License 1.1 (OFL 1.1)' Fonts I will be using.
http://sourceforge.net/projects/sourcecodepro.adobe/
http://sourceforge.net/projects/sourcesans.adobe/

Also found these links that might help with pathing.
http://www.fontation.com/feature/
http://www.yearbooks.biz/?event=FAQ.Detail&faq=3
 
N

Nobody

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

What makes you think the system *has* a system font directory?

In the traditional X11 model, the only program which needs fonts is the X
server, and that can be configured to get its fonts from a font server
rather than from a local directory. Even if it doesn't use a font server,
the font directory will be on the system running the X server, not the one
running the client.
 
M

Metallicow

What makes you think the system *has* a system font directory?

Way back when I was kid, I remember a computer that had two colors and 1 built-in font and no mouse. Heck the keyboard was even attached in front a tube screen box.
Wow... those were the days. And to think I hated that thing...

If the OS doesn't *have* a dedicated system fonts dir that is accessable by the user, then I am not that much interested in dealing with it. Rendering a font from a other location will eventually be worked in, but that isn't the problem at hand.
 
D

Dennis Lee Bieber

Way back when I was kid, I remember a computer that had two colors and 1 built-in font and no mouse. Heck the keyboard was even attached in front a tube screen box.
Wow... those were the days. And to think I hated that thing...

If the OS doesn't *have* a dedicated system fonts dir that is accessable by the user, then I am not that much interested in dealing with it. Rendering a font from a other location will eventually be worked in, but that isn't the problem at hand.

Back in history, my W98se box ran a program called Adobe Type Manager.
I had fonts spread across a number of directories, in bitmap, TrueType, and
PostScript formats.

My WinXP box ran a program called "Font Reserve", using the same
mishmash of directories -- but it created shortcuts of the active fonts in
the system fonts directory. Unfortunately, it didn't support "Multiple
Master" font types, which ATM did, so I lost access to one font family
("Multiple Master" fonts were configurable -- for a simple one, one could
maybe specify stroke width, and character width, then generate a "use" font
from those specs).

I need to study how Win7 handles fonts, since I don't want to dump the
entire mishmash into the system directory.
 
M

Metallicow

Am 11.07.2013 19:19, schrieb Metallicow:






That's the wrong way to do it. You have to use the proper Windows API to

get to the font directory. It's SHGetKnownFolderPath() with

FOLDERID_Font or SHGetFolderPath() with CSIDL_FONTS.



See http://bugs.python.org/issue1763



Christian

typo'd instead of copy/pasted. What I meant was... Chris's code...
FONTDIRS = os.path.join(os.environ['WINDIR'], 'Fonts')
'C:\\WINDOWS\\Fonts'

returned valid as returned string. Forgot to add >>> part.
Is there any way to edit posts? Don't see an edit linky anywhere.
Is there something wrong with using os.environ...?
win32api stuff is another dependency. Is it really needed?
 
S

Steven D'Aprano

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?


:)
 
C

Chris Angelico

Am 11.07.2013 19:19, schrieb Metallicow:






That's the wrong way to do it. You have to use the proper Windows API to

get to the font directory. It's SHGetKnownFolderPath() with

FOLDERID_Font or SHGetFolderPath() with CSIDL_FONTS.



See http://bugs.python.org/issue1763



Christian

typo'd instead of copy/pasted. What I meant was... Chris's code...
FONTDIRS = os.path.join(os.environ['WINDIR'], 'Fonts')
'C:\\WINDOWS\\Fonts'

returned valid as returned string. Forgot to add >>> part.
Is there any way to edit posts? Don't see an edit linky anywhere.
Is there something wrong with using os.environ...?
win32api stuff is another dependency. Is it really needed?

No, you fundamentally cannot edit posts. This is a Usenet newsgroup
and a mailing list; once your post is sent, it gets carried by lots of
different servers separately. Just send a followup, same as you did
there.

Since you seem to be using Google Groups, please read this to learn
how to avoid antagonizing a significant proportion of the list's best
responders:

http://wiki.python.org/moin/GoogleGroupsPython

ChrisA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,097
Latest member
RayE496148

Latest Threads

Top