How can I get a list of TTF fonts (ideally in a cross platform way) for use with SDL and the SDL TTF library?
An initial idea of how the problem might be approached:
Here is how I imagine one might go about solving this question:
I assume that there is no such cross platform function as something like
std::vector<std::string> GetSystemFontDirectories()
which would be a function returning a list of commonly used system font locations.
With the above said, it should be fairly easy to write such a function, even if it is written in a very simplistic way.
if(system is windows)
{
std::vector return_paths;
return_paths.push_back("C:\Windows\Fonts\example\directory");
return_paths.push_back("C:\Windows\Fonts\example\directory\2");
}
else if(system is linux)
{
std::vector return_paths;
return_paths.push_back("/usr/local/fonts/truetype");
for(each user)
{
return_paths.push_back("/home/" + user + "/.local/fonts/maybe");
}
}
else if(system is mac os x) ...
Problems with this approach:
The above is just an initial idea about how this might be done. It probably isn't a particularly good solution. In particular, although I know that compiler preprocessor switches can solve the if system is windows / else if system is linux
problem, I don't personally know of a list of system directories which are guaranteed to exist on either Windows or Linux systems, where fonts should be stored.
For Linux I am pretty sure there are mutiple different locations, and some of those are local to users home directories, so a list of users would have to be obtained as well.
I am less familar with Windows and OS X.
Note: It would be possible (on Linux) to "solve" this problem by running find
and doing a search system wide for ttf files. But clearly this isn't a "good" solution, even if the results were "saved" for use next time the program was run.
Note Note: I am fairly sure program such as GIMP do actually search for fonts on first run...
Summary of how SDL TTF helps and where it cannot help:
As stated in the question title, I am writing some software using the SDL library and associated SDL TTF library. (So I can only use ttf fonts, yet another caveat to the problem to be solved.)
SDL TTF is provided to make working with (TTF) fonts simple. And indeed it does, provided that the compiled binary uses a small number of TTF fonts which are stored in the current working directory.
For something like a game, this makes sense, as a game would usually be shipped with an installer which would also install required fonts in the required directories.
However, if you are writing an application which should "just use some appropriate fonts available on the system", then SDL TTF doesn't help much to solve the problem of "finding out where those fonts are" and "which fonts are available".
From a Linux point of view, this is a bit of a difficult question, as different Linux systems certainly do ship with different fonts by default and might have different directory structures to store them as well.
Final comment: Existing software which has solved this problem:
Linux / Windows / OS X systems usually ship with a text editor by default, and there are thousands of "aftermarket" text editors available. (Some free, some paid.) Further, there are thousands more programs which use text rendering in some way.
How do these programs solve this problem? Presumably when downloading a "text editor" like Atom, Visual Studio Code, Mousepad, Leafpad, etc, they do not all ship with their own bundle of fonts?
What solution to this problem would such programs typically use?
Final Note: Question tagged with C++
, however the language is not that important. If this problem can be solved in any language, then perhaps there is a way to solve it using either C or C++.