Sign in to follow this  
Aressera

How to enumerate the installed font paths on a system?

Recommended Posts

I'm looking to see if there is a better solution to this problem than my current implementation. Currently, I search certain predefined system paths for font files ("C:\Windows\Fonts\" on Windows and "/System/Library/Fonts/", "/Library/Fonts/" on OS X), then open each file with Freetype to determine the font's family and face names, then cache this information, along with the file's path, for later use. The problem is that this is really slow (a couple of seconds).

 

I've looked at the available functionality on OS X and Windows and the only font management tools allow you to list the installed font names but don't allow you to get the path on the system where the font file is stored. Therefore, there's no way for me to use a library like Freetype to open the file.

 

Am I missing something or is there no better way to do this?

Share this post


Link to post
Share on other sites

It seems very odd that you would ever need to to this on both platforms.  Either you are using a MacOS / Cocoa based interface, or you are using a Windows-based interface, or you are using .NET or Qt or some other framework that handles the cross-platform stuff.

 

Without using a cross-platform library the same code won't work on both OSX and Windows.

 

For Windows you can use EnumFontFamilies to get the list.

 

For OS X you can use NSFontManager::availableFontFamilies to get the list.

 

For .NET or Qt or other libraries, these also have their own font family enumeration capabilities.

Share this post


Link to post
Share on other sites

I'm aware of those methods but they only seem to return the font names, but not the path the associated font files. I'm totally OK with using a platform-specific API (with #ifdefs) to do this but the provided ones on both platforms don't provide the necessary information.

 

I'm using the fonts as part of an OpenGL-based GUI system, so I can't really use the built-in text rendering either. I just don't want to have to scan the system myself for the necessary files when the OS has probably already done this and cached the information.

Share this post


Link to post
Share on other sites

Your method of enumerating all the fonts by parsing each individual file is going to be slow.  Look at other apps that do it, such as GIMP, and observe that it can take a long time for it to enumerate all the fonts.

 

If you are going to use your own font rendering system then yes, it will be slow.  That's just the cost of parsing a ton of files.

 

The whole point of using the OS to get the font list is that the OS has already parsed the data and has an index of the details you need.

Share this post


Link to post
Share on other sites

Yeah, just commenting on what Frob said ... I think you would be better off presenting the user with the font list you get from the OS and then when the user actually selects a font somehow mapping that font back to a font file (and possibly erring out if something goes wrong) rather than opening all the font files with FreeType which is what, I'm guessing, is really slow. (However, a couple of seconds actually isn't that bad -- I mean don't you just need to do this once at startup?)

Share this post


Link to post
Share on other sites

Yes, that's what I'd like to do, I'm just having trouble with the "somehow mapping that font back to a font file" part. Since the font files aren't guaranteed to be named with any convention I can't assume that they'll even contain the font name in their filenames. I know my current solution isn't ideal, that's why I'm asking for ideas.

 

Here is the basic interface for my font management system. Currently it caches the available font files behind the scenes (slow!) in a hash map internally so that the user can get the file containing the font with the given name. In general use (as part of a game's GUI) this won't be a huge issue because the game will just bundle the necessary font file with the game itself at a known path, however I still need to be able to access commonly available fonts (with fallbacks in case of failure) so that I can have a good default font for the rendered GUI elements if none is provided by the game.

FontManager fonts;
FontInfo fontInfo; // contains path to font file.

// Get a font file path for the given font family/style name
fonts.getFont( "times new roman", // font family name
               "" // optional font face name (i.e. bold, italic)
                &fontInfo );

// Get a sensible default system font file.
fonts.getDefaultFont( &fontInfo );
Edited by Aressera

Share this post


Link to post
Share on other sites

I'm just having trouble with the "somehow mapping that font back to a font file" part.

 

Well, that code project code is doing the following ( I think)

 

(1) Uses the registry to get a list of registry key -> font filename mappings, where the registry keys are like marked-up display names.

(2) Given a font display name, returns the filename associated with the first registry key that includes the display name as a substring.

 

You could use (1) because it is presumably faster than iterating over the file system, but do something more intelligent for (2). Namely you could write a function that given a font display name returns the set of all likely registry keys and open just those font files to find the one you want. But I  don't know exactly what is in the registry; maybe what the code project project is doing is good enough.

Edited by jwezorek

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this