Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to get the system default font or even all installed fonts?  (Read 5083 times)

0 Members and 1 Guest are viewing this topic.

codecodecode

  • Newbie
  • *
  • Posts: 26
    • View Profile
I don't know which OS my program is going to be used with, so I can't know which the font folder is. What I've tried, since I'm working with wxWidgets, which has a way of obtaining the system default font, is this:
wfont = wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT);
defaultFont.loadFromMemory(&wfont,sizeof(wfont));
//later in code
sf::Text txt("",defaultFont);
//...
txt.setString("lorem ipsum");
//...
draw(txt);
 
But it doesn't display anything.

And then every self-respecting multi-OS text handling program can make use of all installed fonts in the system. Therefore, it looks, such thing should be trivial to implement. What is the SFML way of doing it?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to get the system default font or even all installed fonts?
« Reply #1 on: March 04, 2017, 08:37:12 pm »
Although I don't know the solution to this or even if there is an SFML solution to this but the reason your code doesn't work is because "loadFromMemory" requires an actual "file" stored in memory (complete with file header etc.)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

codecodecode

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: How to get the system default font or even all installed fonts?
« Reply #2 on: March 04, 2017, 08:45:06 pm »
So what is the standard SFML way of loading fonts while maintaining mulit OS support? Adding some fonts in the final product's folder? That's limiting (I'm making something of a mini text editor as a component) and not many fonts are without any licenses.
Prompting the user to select the font folder & pick font files from there? That'd be annoying for the user, but is at least something that can be done.

And would loadFromStream() be the thing I'm looking for in the above code?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to get the system default font or even all installed fonts?
« Reply #3 on: March 04, 2017, 09:01:28 pm »
Quote
And would loadFromStream() be the thing I'm looking for in the above code?
To be clear: there's no function in SFML that works with a wxFont (obviously). The only way wxWidgets could help, is by giving the paths of system fonts.

Otherwise you'll have to implement it yourself, either by finding some code or library that enumerates the system fonts on all platforms, or by writing the OS-specific code that does it on every OS that you want to support. It's probably simple, but you have to do it (and do it right) for each OS.
Laurent Gomila - SFML developer

codecodecode

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: How to get the system default font or even all installed fonts?
« Reply #4 on: March 05, 2017, 04:35:23 pm »
i see. It doesn't seem to provide any path names. I went with this method. WxWidgets is also open source, so I'll see if I'll be able to cobble up anything to link them in the future.