Brad Brad - 3 months ago 22
C++ Question

Get a font filename based on Font Name and Style (Bold/Italic)

This has been driving me crazy all day.

I need to get a font filename (eg. Arial.ttf) based on its name (Arial in this case) and whether it is bold, italic or both. Using those pieces of information, I need to find the font file so I can use it for rendering.

Some more examples:


  • Calibri, Bold would resolve to calibrib.ttf.

  • Calibri, Italic would resolve to calibrii.ttf.



Any ideas on how I could achieve this in C++ (Win32)

Answer

First, to my knowledge, there is no reliable way to do that.

The Windows API deals with font families and mappings, not with font files, which are dealt with at a lower level. Also note that even if you manage to get the file name of a font, no rendering function (that I know of) will accept it, so what will you do with it?

That said, you can look in the registry key HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Fonts in order to obtain the file name of a font from its logical name. An implementation of that solution can be found here.