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

Author Topic: Font Finder  (Read 6387 times)

0 Members and 1 Guest are viewing this topic.

wintertime

  • Sr. Member
  • ****
  • Posts: 255
    • View Profile
Font Finder
« on: October 18, 2013, 12:27:22 pm »
I'm sure you fear reading that someone wants the default font back, again; but I do not as there were reasons for removing it. :)

Now there is still something lacking: you pretty much have to include a font file in your project, because its difficult to in a crossplatform way find some preinstalled font, which all systems should have.

A good first step would be a function that only finds out the directory where font files are stored and returns a string containing the path, for example "C:/WINDOWS/Fonts/". I think there would be some way available to find that out on all OS, as other programs can do that, too. If that gives no security concern it could help people already a little if they want to try loading some random or less random font from there, otherwise hide it inside the second part.

This second step would be to base a class thats the equivalent of sf::VideoMode for font files on that foundation and give only a few common options like getting some monospace or serif or sans serif or handwriting or ... font, maybe also add the font attributes to choose a premade normal, bold, italic, ... font. This class could then have a method to return a filename to be fed to the font class or maybe just also add another constructor to sf::Font to take advantage of information the font finder class internally collected already.

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Re: Font Finder
« Reply #1 on: October 18, 2013, 12:35:43 pm »
Simply include a font with your program. There are a ton of free fonts that you can use. You don't have to take care of searching the user's system for a specific font, and things will look the same on all platforms.

It's as easy as copy&paste followed by a font.loadFromFile( "font.ttf" ) call.

Edit: Maybe I got your post wrong. Ignore mine if that's the case. ;)
« Last Edit: October 18, 2013, 12:39:02 pm by Tank »

wintertime

  • Sr. Member
  • ****
  • Posts: 255
    • View Profile
Re: Font Finder
« Reply #2 on: October 18, 2013, 12:41:07 pm »
Yes, I'm already including a font file, it's just I think this would still be a nice addition to SFML. ;)

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Font Finder
« Reply #3 on: October 18, 2013, 01:04:09 pm »
If every system stored all of its fonts in a single place, then there might be some point to this.  But some quick googling implies it's far worse than that.

Windows
Quote
When we go Control Panel >> Appearance and Personalization >> Fonts, the window shows a list of fonts currently installed on the system. We can copy those font files out (Ctrl-C) to a new location,

But where are the original physical font files stored?

Based on this thread, I'd thought that the files are stored in C:\Windows\Font, but it seems to be a virtual folder, Specifically when I download TypeLight (runned the program as administrator) and tried to open the font files in C:\Windows\Font, none are listed

Mac OS
Quote
The folder in which a font is located determines who can access and use the font. For example: If a user manually installs a new font at ~/Library/Fonts/, the font is available only to that user. If a root or admin user installs the same font at /Network/Library/Fonts/, all network users can use the fonts (assuming that the network administrator has set up computers for this type of sharing).

Changes to fonts take effect when an application is opened or a user logs in to the account or computer on which the changes occurred. Duplicate fonts are resolved based on the order of precedence defined for the standard Fonts folders and are described from highest to lowest priority below.

I see no reason to continue investigating these issues because I have no doubt they would give us all headaches.

As Tank pointed out, including the font yourself has the added advantage of ensuring it looks the same on all systems, so there's literally no reason not to do this unless you really, really like a particular non-free font.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10895
    • View Profile
    • development blog
    • Email
Re: Font Finder
« Reply #4 on: October 18, 2013, 03:54:51 pm »
I see no reason to continue investigating these issues because I have no doubt they would give us all headaches.
Just because something seems to need a bit more research and a nice abstraction doesn't mean we should just give up the idea.

For Windows it shouldn't matter whether it's a virtual directory or not as long as you can load the font.
For Mac OS it shouldn't be an issue to map multiple directories depending on the access levels.

Sure you can always ship fonts with the executable, on the other hand it's also quite common for UI applications to use system fonts. It's not a feature we need to work with SFML, but it would be a nice to have feature. We even had a similar discussion when Laurent first removed the default font. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Re: Font Finder
« Reply #5 on: October 22, 2013, 08:12:38 am »
Quote
on the other hand it's also quite common for UI applications to use system fonts
I think that's true for text processors and similar, but for multimedia programs or even games? Not sure.

What I would definitely agree with however is something that tells me where I can find such stuff. Some functions in the system module that provide cross-platform access to important directories or even system configs: User's home directory, font directory etc.

"Finding fonts" however is a very specific thing in my opinion.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10895
    • View Profile
    • development blog
    • Email
Re: Font Finder
« Reply #6 on: October 22, 2013, 12:13:03 pm »
"Finding fonts" however is a very specific thing in my opinion.
Hmm yeah I think SFML shouldn't go look for certain fonts, but it should rather just point towards where the system fonts can be found. How or which font the user then loads should not be of SFML's concern. And since SFML (currently) returns a boolean up on loading, it's also somewhat possible to check whether a font exists or not, so the user is not left alone without a way to make sure a certain font really exists.

Again yes system fonts get rarely used for games, but SFML is not only about games and it the feature is still nice to have. :D
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Font Finder
« Reply #7 on: October 22, 2013, 12:22:50 pm »
Isn't something as simple as the following draft enough?

class FontFinder
{
    static const std::vector<std::string> paths = /* choose wisely the order of the paths */
    {
        "~/Library/Fonts/",
        "/Library/Fonts/",
        "/System/Library/Fonts/",
        "/Windows/Fonts/",
        "/usr/share/fonts/",
        "/usr/local/share/fonts/",
        "~/.fonts/",
        resourcePath() // <- application specific path, e.g. working directory or *.app/Content/Resource
    };

public:
    static sf::Font find(std::string name)
    {
        sf::Font font;
        for (auto path : paths) if (font.loadFromFile(path + name)) return font;
        throw FontNotFound();
    }
};
 

Not tested. Maybe the ' ~' path need to be replaced with the full path. In that case, something like that can be useful.

http://support.apple.com/kb/HT2435
http://support.microsoft.com/kb/314960/en-us
https://wiki.ubuntu.com/Fonts
SFML / OS X developer

wintertime

  • Sr. Member
  • ****
  • Posts: 255
    • View Profile
Re: Font Finder
« Reply #8 on: October 22, 2013, 04:35:12 pm »
That looks a bit too simplistic. If the user has to know the exact filename its not of much help as there would be a high chance of fail and users would have to include hundreds of alternative filenames in their project to try.
At minimum it should generate a list of all filenames and make the list available. Then as a further help do some clever string substitution/substring testing on the filenames to reduce the list and return a few halfway good choices(and exclude weird fonts like wingdings).
Ideally it would also read a few attributes from all the font files and then make a choice from some enums about monospace/serif/sansserif/handwriting/..., none/bold/italic/hollow/..., needed character ranges, character size, prefered file formats and/or a list of substrings the user puts in.
« Last Edit: October 22, 2013, 04:44:19 pm by wintertime »