Getting installed font directory

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
User avatar
Disch
Experienced Solver
Experienced Solver
Posts: 99
Joined: Wed Oct 17, 2007 2:01 am

Getting installed font directory

Post by Disch » Tue Oct 07, 2008 5:11 pm

I have a situation where I need to load a font via it's .ttf file, but want to use wxFontDialog to have the user select the desired font.

I searched and found this thread:
http://forums.wxwidgets.org/viewtopic.php?t=19804


Which pretty much tells me wx cannot directly do what I want. However I began looking for workarounds to solve this problem.

One thing that came to mind was to enumerate all the installed .ttf files until I find one that matched the user selected font. Sure it may be a round-about way to do it, but it could work in theory (though I won't know how well until I try).

As I started to look into this approach further, I hit a somewhat unexpected hurdle. I couldn't figure out how to get the installed font directory with wx! Is it even possible? I expected there to be a GetSystemDirectory or similar function which could receive common directories -- however I didn't see anything like that. I know it can be done under msw with the SHGetFolderPath function -- however I'd prefer something portable.

Short of scanning the entire HD for font files.. is there any way to find out where installed fonts are located with wx?

Thanks

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 3573
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Post by ONEEYEMAN » Wed Oct 08, 2008 9:01 pm

Hi,
If the user selects a font thru the font dialog, just create this font.
Why do you need to look for a workarounds?

Thank you.

User avatar
Disch
Experienced Solver
Experienced Solver
Posts: 99
Joined: Wed Oct 17, 2007 2:01 am

Post by Disch » Wed Oct 08, 2008 9:20 pm

Because I need to load the font's .ttf file directly, which you can't get from the wxFontDialog.

More specificially, this is for a font bitmapping process to generate bitmaps of fonts using the Freetype lib to generate and dump .png files of antialiased fonts which are to be later loaded into OpenGL textures for text rendering in another program.

Freetype decodes glyphs and font data directly from the font file on the computer, so I either need the path to the .ttf file, or a pointer to a memory buffer containing the file's data. A wxFont object is pretty much useless for what I need.

That thread I linked to in my previous post came up with a MSW specific way of getting a memory pointer to the font data (iirc), but I would really prefer something more portable if possible.

utelle
Moderator
Moderator
Posts: 898
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Re: Getting installed font directory

Post by utelle » Wed Oct 08, 2008 9:50 pm

Disch wrote:I have a situation where I need to load a font via it's .ttf file, but want to use wxFontDialog to have the user select the desired font.

I searched and found this thread:
http://forums.wxwidgets.org/viewtopic.php?t=19804

Which pretty much tells me wx cannot directly do what I want.
Up to now the situation hasn't changed. There is no method I know of to find the font file corresponding to a font selected using wxFontDialog. But as the thread also tells you, on Windows you have access to the font data, i.e. the content of the underlying .ttf file. In most situations this is just as good as accessing the .ttf file directly.

Unfortunately there is no direct way to find out the file name and location of a selected font. This is not a wxWidgets issue, but an OS issue.
Disch wrote:One thing that came to mind was to enumerate all the installed .ttf files until I find one that matched the user selected font. Sure it may be a round-about way to do it, but it could work in theory (though I won't know how well until I try).
It works - not only in theory. On Windows you can access the file names of all installed fonts by reading the Windows registry (Key: HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Fonts). But you would have to open each font file and to extract at least the font name to be able to match a selected font by name to one of the installed fonts. I dismissed this approach since it causes quite some overhead if a user has installed a great number of fonts. And the matching isn't at all fool proof, since the font name you get from the font selection dialog might be locale dependent.
Disch wrote:As I started to look into this approach further, I hit a somewhat unexpected hurdle. I couldn't figure out how to get the installed font directory with wx! Is it even possible?
Yes. If you are still interested in this approach after my above comments, I could send you the code for Windows.
Disch wrote:I expected there to be a GetSystemDirectory or similar function which could receive common directories -- however I didn't see anything like that. I know it can be done under msw with the SHGetFolderPath function -- however I'd prefer something portable.
For Windows there is function wxGetOSDirectory() (which points to the Windows directory and fonts are located in the subdirectory Fonts thereof), but I don't think there is a portable way of finding installed fonts. For example on Linux systems there is usually not a single location where fonts are installed; on Linux fonts are usually managed by FontConfig or similar tools.

Regards,

Ulrich

utelle
Moderator
Moderator
Posts: 898
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Post by utelle » Wed Oct 08, 2008 9:55 pm

Disch wrote:Freetype decodes glyphs and font data directly from the font file on the computer, so I either need the path to the .ttf file, or a pointer to a memory buffer containing the file's data. A wxFont object is pretty much useless for what I need.

That thread I linked to in my previous post came up with a MSW specific way of getting a memory pointer to the font data (iirc), but I would really prefer something more portable if possible.
To the best of my knowledge there is no portable way to solve this issue. As stated in the mentioned thread there is a solution for Windows. And it also can be done on Mac OS X. But during the last 2 years no one has come up with a solution for Linux.

If you find a way please let me know.

Regards,

Ulrich

User avatar
Disch
Experienced Solver
Experienced Solver
Posts: 99
Joined: Wed Oct 17, 2007 2:01 am

Post by Disch » Wed Oct 08, 2008 10:24 pm

Bummer.

I'm not really interested in solutions that aren't cross platform (to me, doing platform specific code kind of defeats the whole point of using a lib like wx). So thanks but no thanks.

I have a fallback plan that will be a bit less user friendly, but should still work. I'll just have to have the user load the .ttf files some other way and not use wxFontDialog at all.

Unfortunately that probably isn't applicable to what you're doing, so I doubt it'll help you.

Thanks for the help!

carpenter_mike2001
In need of some credit
In need of some credit
Posts: 1
Joined: Mon Nov 03, 2008 6:04 pm
Contact:

Well you may have given up too soon.

Post by carpenter_mike2001 » Mon Nov 03, 2008 6:12 pm

If you just need the file name & path why don't you do a search for all files on the system with the *.ttf extension? This should work fine as long as you don't need a font preview. Under linux you could do a system call with 'find / -iname "*.ttf" >> fonts.txt' and there you have a text file with all the true type fonts. Under windows there should be some api function to do the same. I think with some macros you could come up with a solution that is close to platform independent. Maybe write your own find function.
Don't forget rule #1

vsp
Knows some wx things
Knows some wx things
Posts: 35
Joined: Mon Feb 21, 2005 12:52 pm

Post by vsp » Tue Nov 04, 2008 7:09 am

You can alternatively try with
wxString sysFontDir = ::wxOSGetDirectory();
sysFontDir.Append ("\\fonts");
Should work fine.

Post Reply