Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #50455 > unrolled thread

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

Started byMetallicow <metaliobovinus@gmail.com>
First post2013-07-11 08:32 -0700
Last post2013-07-13 07:20 +0000
Articles 15 — 8 participants

Back to article view | Back to comp.lang.python


Contents

  How do I get the OS System Font Directory(Cross-Platform) in python? Metallicow <metaliobovinus@gmail.com> - 2013-07-11 08:32 -0700
    Re: How do I get the OS System Font Directory(Cross-Platform) in python? Chris “Kwpolska” Warrick <kwpolska@gmail.com> - 2013-07-11 17:48 +0200
    Re: How do I get the OS System Font Directory(Cross-Platform) in python? Metallicow <metaliobovinus@gmail.com> - 2013-07-11 08:54 -0700
      Re: How do I get the OS System Font Directory(Cross-Platform) in python? Chris “Kwpolska” Warrick <kwpolska@gmail.com> - 2013-07-11 18:12 +0200
    Re: How do I get the OS System Font Directory(Cross-Platform) in python? Metallicow <metaliobovinus@gmail.com> - 2013-07-11 10:19 -0700
      Re: How do I get the OS System Font Directory(Cross-Platform) in python? Christian Heimes <christian@python.org> - 2013-07-12 03:27 +0200
        Re: How do I get the OS System Font Directory(Cross-Platform) in python? Metallicow <metaliobovinus@gmail.com> - 2013-07-11 21:24 -0700
          Re: How do I get the OS System Font Directory(Cross-Platform) in python? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-12 04:55 +0000
          Re: How do I get the OS System Font Directory(Cross-Platform) in python? Chris Angelico <rosuav@gmail.com> - 2013-07-12 14:58 +1000
    Re: How do I get the OS System Font Directory(Cross-Platform) in python? Nobody <nobody@nowhere.com> - 2013-07-11 18:47 +0100
      Re: How do I get the OS System Font Directory(Cross-Platform) in python? Metallicow <metaliobovinus@gmail.com> - 2013-07-11 11:25 -0700
        Re: How do I get the OS System Font Directory(Cross-Platform) in python? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-07-11 20:13 -0400
        Re: How do I get the OS System Font Directory(Cross-Platform) in python? Tim Roberts <timr@probo.com> - 2013-07-12 22:36 -0700
          Re: How do I get the OS System Font Directory(Cross-Platform) in python? Metallicow <metaliobovinus@gmail.com> - 2013-07-12 23:38 -0700
            Re: How do I get the OS System Font Directory(Cross-Platform) in python? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-13 07:20 +0000

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

FromMetallicow <metaliobovinus@gmail.com>
Date2013-07-11 08:32 -0700
SubjectHow do I get the OS System Font Directory(Cross-Platform) in python?
Message-ID<3d97de86-f690-40ad-aba8-972120af7ff3@googlegroups.com>
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.

[toc] | [next] | [standalone]


#50457

FromChris “Kwpolska” Warrick <kwpolska@gmail.com>
Date2013-07-11 17:48 +0200
Message-ID<mailman.4591.1373557706.3114.python-list@python.org>
In reply to#50455
On Thu, Jul 11, 2013 at 5:32 PM, Metallicow <metaliobovinus@gmail.com> wrote:
> 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.
> --
> http://mail.python.org/mailman/listinfo/python-list

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?
--
Kwpolska <http://kwpolska.tk> | GPG KEY: 5EAAEA16
stop html mail                | always bottom-post
http://asciiribbon.org        | http://caliburn.nl/topposting.html

[toc] | [prev] | [next] | [standalone]


#50459

FromMetallicow <metaliobovinus@gmail.com>
Date2013-07-11 08:54 -0700
Message-ID<faa7fdb3-99e9-40ed-8a62-1df85d49edd4@googlegroups.com>
In reply to#50455
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..

[toc] | [prev] | [next] | [standalone]


#50462

FromChris “Kwpolska” Warrick <kwpolska@gmail.com>
Date2013-07-11 18:12 +0200
Message-ID<mailman.4594.1373559184.3114.python-list@python.org>
In reply to#50459

[Multipart message — attachments visible in raw view] — view raw

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

[toc] | [prev] | [next] | [standalone]


#50467

FromMetallicow <metaliobovinus@gmail.com>
Date2013-07-11 10:19 -0700
Message-ID<dcca3b88-52fb-474f-8cce-4829149fcfaf@googlegroups.com>
In reply to#50455
@ 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 instructions and 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

[toc] | [prev] | [next] | [standalone]


#50482

FromChristian Heimes <christian@python.org>
Date2013-07-12 03:27 +0200
Message-ID<mailman.4607.1373592436.3114.python-list@python.org>
In reply to#50467
Am 11.07.2013 19:19, schrieb 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....?

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

[toc] | [prev] | [next] | [standalone]


#50491

FromMetallicow <metaliobovinus@gmail.com>
Date2013-07-11 21:24 -0700
Message-ID<9218c1f3-f107-4942-90d6-26c017f1e4e7@googlegroups.com>
In reply to#50482
On Thursday, July 11, 2013 8:27:04 PM UTC-5, Christian Heimes wrote:
> Am 11.07.2013 19:19, schrieb 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....?
> 
> 
> 
> 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?

[toc] | [prev] | [next] | [standalone]


#50495

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-07-12 04:55 +0000
Message-ID<51df8c46$0$9505$c3e8da3$5496439d@news.astraweb.com>
In reply to#50491
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

[toc] | [prev] | [next] | [standalone]


#50496

FromChris Angelico <rosuav@gmail.com>
Date2013-07-12 14:58 +1000
Message-ID<mailman.4613.1373605132.3114.python-list@python.org>
In reply to#50491
On Fri, Jul 12, 2013 at 2:24 PM, Metallicow <metaliobovinus@gmail.com> wrote:
> On Thursday, July 11, 2013 8:27:04 PM UTC-5, Christian Heimes wrote:
>> Am 11.07.2013 19:19, schrieb 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....?
>>
>>
>>
>> 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

[toc] | [prev] | [next] | [standalone]


#50468

FromNobody <nobody@nowhere.com>
Date2013-07-11 18:47 +0100
Message-ID<pan.2013.07.11.17.47.00.761000@nowhere.com>
In reply to#50455
On Thu, 11 Jul 2013 08:32:34 -0700, Metallicow wrote:

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

[toc] | [prev] | [next] | [standalone]


#50471

FromMetallicow <metaliobovinus@gmail.com>
Date2013-07-11 11:25 -0700
Message-ID<5768a87e-1544-45d8-bd4b-bd8e78c711ae@googlegroups.com>
In reply to#50468
On Thursday, July 11, 2013 12:47:01 PM UTC-5, Nobody wrote:
> 
> 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.

[toc] | [prev] | [next] | [standalone]


#50479

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2013-07-11 20:13 -0400
Message-ID<mailman.4604.1373588017.3114.python-list@python.org>
In reply to#50471
On Thu, 11 Jul 2013 11:25:55 -0700 (PDT), Metallicow
<metaliobovinus@gmail.com> declaimed the following:

>On Thursday, July 11, 2013 12:47:01 PM UTC-5, Nobody wrote:
>> 
>> 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.

	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.
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#50570

FromTim Roberts <timr@probo.com>
Date2013-07-12 22:36 -0700
Message-ID<hmp1u85d7qnqmaca86ru45o928i20t00mu@4ax.com>
In reply to#50471
Metallicow <metaliobovinus@gmail.com> wrote:
>
>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. 

Really?  Because Windows is the ONLY one of the major operating systems
that actually has a dedicated system fonts directory.  Linux doesn't even
have a dedicated windowing system.
-- 
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

[toc] | [prev] | [next] | [standalone]


#50577

FromMetallicow <metaliobovinus@gmail.com>
Date2013-07-12 23:38 -0700
Message-ID<d5e75080-60a3-4918-8ccf-f5dafec59473@googlegroups.com>
In reply to#50570
On Saturday, July 13, 2013 12:36:45 AM UTC-5, Tim Roberts wrote:
> Really?  Because Windows is the ONLY one of the major operating systems
> 
> that actually has a dedicated system fonts directory.  Linux doesn't even
> 
> have a dedicated windowing system.

So... Is it expected to install duplicates to multiple locations with Mac and Linux...?

[toc] | [prev] | [next] | [standalone]


#50579

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-07-13 07:20 +0000
Message-ID<51e0ffbd$0$9505$c3e8da3$5496439d@news.astraweb.com>
In reply to#50577
On Fri, 12 Jul 2013 23:38:18 -0700, Metallicow wrote:

> On Saturday, July 13, 2013 12:36:45 AM UTC-5, Tim Roberts wrote:
>> Really?  Because Windows is the ONLY one of the major operating systems
>> 
>> that actually has a dedicated system fonts directory.  Linux doesn't
>> even
>> 
>> have a dedicated windowing system.
> 
> So... Is it expected to install duplicates to multiple locations with
> Mac and Linux...?

No. Mac does have dedicated font locations. There are five official 
locations, with distinct meanings:

http://support.apple.com/kb/ht2435


(I do not know if OS-X ever localises directory names according to the 
user's language.) There is no need to install fonts in multiple locations.


Linux does not expect to have fonts installed *at all*. A graphical 
interface is entirely optional for Linux, so Linux may have no fonts.

There are two standard font systems available on Linux. The 15+ year old 
X Font Server ("xfs") technology is still in use, but many Linux distros 
are moving towards the newer Fontconfig system. Fontconfig allows you to 
define as many font directories as you want, although there are a few de-
facto standard locations.

For more information, see for example:

http://www.centos.org/docs/5/html/Deployment_Guide-en-US/s1-x-fonts.html



-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web