Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

fonts

winapi

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)

like image 261
Brad Avatar asked Jan 02 '11 09:01

Brad


3 Answers

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.

like image 176
Frédéric Hamidi Avatar answered Oct 11 '22 14:10

Frédéric Hamidi


Related to the earlier posts, this seems to be a reliable way:

1) Read the registered Windows font list from HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Fonts\ You will obtain file names and alternate file paths here. The Font names are not useful as they can change with user's locale.

2) Load the TrueType files (.ttf, .ttc, .otf): Use FreeType https://www.freetype.org/). Just initialize the freetype library and load face with FT_New_Face(library, path, 0, &face).

3) Obtain the font Family name using FreeType. Use FT_Get_Sfnt_Name_Count() and FT_Get_Sfnt_Name() to obtain the string table. You will need to check if the encoding is Ansi, UTF16 or other, as some strings will be in multiple different languages and encodings.

4) Obtain the OS2 TrueType properties. Use (TT_OS2 *) FT_Get_Sfnt_Table (face, ft_sfnt_os2) to get the OS2 structure. Interpret the structure using docs like https://www.microsoft.com/typography/otspec/os2.htm#fc

5) Now you have font file path, family name, style properties and other information. Build a list of these and function to search for a file based on font family and style.

like image 32
Greg Avatar answered Oct 11 '22 12:10

Greg


This Code Project project does what you want. As-is it fails on Windows 7 because the GetWinVer function stops at XP. It is trivial to add the case for Windows 7.

like image 25
Simon Parker Avatar answered Oct 11 '22 14:10

Simon Parker