PDA

View Full Version : How to find the filepath to a font



Markus
12th November 2013, 01:09
Hello all,

I am working under linux with Qt4.7 and need to find the path to a selected QFont. So far I have tried:


QDesktopServices::storageLocation(QDesktopServices ::FontsLocation) -- points to a dir that does not exists
QFont::rawName () -- returns an empty string



Any suggestions?
Markus

ChrisW67
12th November 2013, 04:17
Qt uses FontConfig to abstract away the management of actual font files, multiple font directories, substitutions, user preferences etc. AFAICT there is no way to obtain the underlying font file (*.ttf for example) through Qt.

1. The directory returned by default is somewhere you can place fonts and FontConfig will generally find them. It is "~/.fonts" on my machines. If it does not exist that FontConfig path is simply ignored. See /etc/fonts/fonts.conf or the equivalent on your machine to see where FontConfig looks for fonts.
2. As the docs say, rawName() returns an empty string if FontConfig is in use, and XLFD string otherwise. It does not return a file path.

Why do you need to do this?

Markus
12th November 2013, 18:23
1. The directory returned by default is somewhere you can place fonts and FontConfig will generally find them. It is "~/.fonts" on my machines. If it does not exist that FontConfig path is simply ignored. See /etc/fonts/fonts.conf or the equivalent on your machine to see where FontConfig looks for fonts.
2. As the docs say, rawName() returns an empty string if FontConfig is in use, and XLFD string otherwise. It does not return a file path.

Why do you need to do this?

it returns "~/.fonts" on my OS so it seems FontConfig is used. I dont know anything about that so I will have to look into it.
I need this because I am writing a GUI to a Lua based render engine and I need to pass in the path to the selected font.

ChrisW67
12th November 2013, 19:54
Lua has some access to the same FontConfig mechanism though the gd library (possibly others) so it should get the same result if it asks for the same font by family name, size etc. Perhaps you can send a font name rather than file path?

Markus
13th November 2013, 17:55
I am using fontconfig now and just pass the family name, style and size to it. Works now!
Thanks
Markus