0

How do I retrieve a font from the system in SDL-TTF? TTF_OpenFont really wants an absolute path. I'm trying to code cross-platform as much as possible.

Non-working code ready to drop in working code:

public static IntPtr ResolveFont(string font, int size)
{
     var pfont = TTF_OpenFont(font + ".ttf", size);
     if (pfont == IntPtr.Zero) throw new InvalidOperationException(SDL_GetError());

     return pfont;
}

I found a related question to get a list of fonts however this is not that. I intend to fetch only fonts I know exist (in the end, by using a package manager and declaring a dependency on the font package).

(This question is langauge agnostic: C# tag is needed only for syntax highlighting.)

stuartd
  • 70,509
  • 14
  • 132
  • 163
Joshua
  • 40,822
  • 8
  • 72
  • 132
  • [TTF\_OpenFont() returns NULL](https://stackoverflow.com/questions/14043437/ttf-openfont-returns-null) –  Aug 15 '21 at 23:22
  • 1
    @OlivierRogier: I am well aware that TTF_OpenFont() returns null; however none of the answers present are usable to me. Passing the absolute path to a system file and cross-platform do not mix, and selecting a different font so I can ship it is too much trouble. – Joshua Aug 15 '21 at 23:25
  • @.Joshua Thus what is the problem and the question. I didn't quite understand actually. If TTF needs an absolute path, and if you don't want to provide it, what to do? –  Aug 15 '21 at 23:28
  • @OlivierRogier: The problem is the absolute path changes from system to system and I need a way to find the fonts. – Joshua Aug 15 '21 at 23:31
  • 1
    What does it look like? Can't you use a path according to the target platform? Or even an application setting? –  Aug 15 '21 at 23:33
  • @OlivierRogier: I'm pretty sure you can see the problem with trying to make an application setting when you can't render your UI because you can't load any fonts at all. Incidentally, the question is not _quite_ language agonstic as I'm trying to use SDL2 as the only UI renderer because as far as I can tell it's the only truly cross-platform GUI renderer for C#. – Joshua Aug 15 '21 at 23:37
  • But as for what it looks like, if there's some API somewhere for GetSystemFontDirectories() or something even remotely like that it would work. – Joshua Aug 15 '21 at 23:38
  • What do you call a "*GUI renderer*" ? What about for example .NET5 or Xamarin or Avalonia or QT# or wxWidgets ? –  Aug 15 '21 at 23:44
  • @OlivierRogier: Are you really suggesting toss SDL and use something else altogether (with your first two suggestions being completely unusable) because of a strange limitation that really should have an answer? – Joshua Aug 16 '21 at 00:03
  • I don't know. I don't know SDL at all. It is certainly an old library but rather obscure and little frequented. It is necessary to evaluate its interest compared to equivalent technologies, that can be better, more evolved and having more audience, which theoretically ensures to have less problems and a greater capacity of maneuver and creativity. I just found out about Avalonia, it's XAML, but it looks cool. Otherwise my thing is VS WinForms (I'm from Delphi VCL). DotNET 5 has WinForms, not tested. –  Aug 16 '21 at 00:08
  • @OlivierRogier: Had you tried it, you would have found .NET 5 WinForms fail to compile unless the target is Windows. – Joshua Aug 16 '21 at 00:11
  • 1
    Thin then! https://github.com/dotnet/maui –  Aug 16 '21 at 00:25
  • check out https://stackoverflow.com/questions/3954223/platform-independent-way-to-get-font-directory . If you intend to set dependency on package than you know where said package installs fonts, or am i missing something here? You can use something like `fc-list` on linux but i'm not sure how reliable its output is. – keltar Aug 16 '21 at 12:59
  • @keltar: Package dependency is not enough to ensure same path unless I want to make a ridiculous number of builds. `fc-list` is the beginning of a possible answer. You found interesting stuff but there's more work to do as the answers to that question have rotted (see comments). – Joshua Aug 16 '21 at 13:24
  • @OlivierRogier: Turns out that maui does not work on my workstation. – Joshua Aug 22 '21 at 18:48

0 Answers0