Finding System Font Files?
I'm writing an application which combines OpenGL and Qt. I'm using another library (FTGL) to plot text in an OpenGL viewport. Now, to load a font in FTGL, I need to supply the filename of the font, this is where the rubber hits the road. Is it possible to map a font family/face to the file it came from using Qt (possibly QFontDatabase)? Or will I have to find the font files myself and use Qt to interrogate each file individually the get both the information QFontDatabase can give me, plus the filenames? If this is the case, is it possible to determine the system font directories using Qt? Thanks.
from my mind i am using "Nokia Sans" family name.
setFamily("Nokia Sans") in the QFont to set it.
return the default font from the system.
What that reply has to do with OP's issue?!?
humm sorry misunderstanding
you may copy every needed font in a directory of your choice and use that path for your reference.
@mohsen This seems like the only way to go, but a bit inneficient and moving a ton of fonts, some which may fall under copy protection a bit of an issue. It would seem the best alternative would be to acquire the operating systems font directories - I'll look into doing this externally to Qt, if anyone else has any suggestions or ideas on this front, I'd greatly appreciate your input.
As far as I remember it, Qt relies on the operating system/window system facilities for getting the fonts and does not open the files itself.
[quote author="Volker" date="1294232781"]As far as I remember it, Qt relies on the operating system/window system facilities for getting the fonts and does not open the files itself.
Ah okay, well that might mean I'll have to write my own methods for acquiring the system fonts list. I'm honestly not entirely sure how dependent Qt is on win32 or the host operating system whatever it may be, weather it just acquires a rendering context and makes everything look pretty from there, or something else. I'm familiar with FreeType, and have used that in the past (and am now) used it to load and render fonts, does everything you could need.
The fonts directory is a sticky point, I'm going to produce something similar to what QFontDatabase provides, but with a mapping to the files a particular face/family combination came from. How the directories will enter into this, remains to be seen.
Hi, just have stumbled on "QDesktopServices::storageLocation":http://doc.qt.nokia.com/4.7/qdesktopservices.html#storageLocation and I hope it will help you. But I didn't try it myself, though.
[quote author="ixSci" date="1294612188"]Hi, just have stumbled on "QDesktopServices::storageLocation":http://doc.qt.nokia.com/4.7/qdesktopservices.html#storageLocation and I hope it will help you. But I didn't try it myself, though.[/quote]
Unfortunately this gives you only the location for the user fonts, not the system fonts :-/
Volker, I just have tested it and it gave me C:\Windows\Fonts. As long as I know there is no such a notion as user fonts in Windows. Is it there a user fonts notion in unix family?
Not necessarily a Unix issue, more of a Unix/Linux flavour. Most of them have some font dir in the user's home dir because writing to the system directory is forbidden for many of the users (no root password!). On my old KDE box it's in $HOME/.fonts. The new linux/freedesktop standards may set it different!
Mac OS X does have a user font dir too: $HOME/Library/Fonts.
ixSciixSci, great find, thanks!
Volker, the users font directory is largely sufficient, the system fonts directory can be picked up by navigating from root. If the user knows enough to move it, then he should be smart enough to specify where he moved it to in the settings/directories dialog of my application :).
I should also add, there is no notion of user fonts on a windows PC. There is however, System and user fonts on Unix, and on OSX there is a third concept, network fonts. I aim to support all concepts as much as possible, however system fonts is the minimum target at this time.
Tagging onto this old thread, because I have a similar question, which was not answered.
I need to read data directly from TTF font files when a user chooses a font from something like a QFontDialog. Unfortunately neither the QFont returned from QFontDialog or even QFontDatabase include any reference to the actual file.
So instead I iterate through the font files, parsing out the font name etc from each file so that I can populate a font selection dialog box that maps a user selected font directly to the actual file.
My problem is finding the font files, Qt 5 provides QStandardPaths::FontsLocation
or on 4.x QDesktopServices::FontsLocation. These return a path to the user fonts directory.
On Windows which does not differentiate between user and system fonts this works well, returning C:/Windows/Fonts
However on Linux it only returns the user font directory e.g /home/clivel/.fonts which by default is empty so doesn't really help because most applications offer fonts from the system font directory for the user to choose from. e.g on Ubuntu 12.04, the list of available fonts displayed by the Gimp are from the system directory which is /usr/share/fonts/truetype.
So, after that long winded explanation, my question is, how do I get the system fonts directory?
I could of course hardcode /usr/share/fonts but besides being klunky, I am not sure how universal this is across Linux systems.
This is only a partial treatment of the question. The system fonts directory varies across platforms, even distributions. Some Linux distros use /usr/share/fonts, some /usr/local/share/fonts. Libraries like fontconfig address this in a good way, but Qt may or may not be compiled with fontconfig.
One way to address this is to distribute fontconfig configured per platform supported. In other words add a dir element to the local.conf fontconfig parent element for each directory containing system or user fonts. Then you would link against your fontconfig lib and use the library api to get the font files.
I wish QFontDatabase would provide the actual font file matched...
Thanks for the response, It really would be great if QFontDatabase provided the actual font file.
What I did in the end was a multi-pronged approach:
First I check if either /etc/fonts/fonts.conf and /etc/fonts/local.conf exist, and if so parse them for the location of font files between <dir> and </dir> tags.
I then also check both the locations you mention; /usr/share/fonts and the /usr/local/share/fonts for fonts and finally check the user font directory returned by Qt (~/.fonts).
Hopefully that will take care of most Linux distributions.
Of course, there is no guarantee that fonconfig is installed on a desktop. In most Linux distro's, it is by default. There is also a chance that fontconfig is installed as a user library. You might want to check for it during your installation. FONTCONFIG_PATH is a useful environment variable to check, also.