Get a font filename based on Font Name and Style (

2019-01-23 14:14发布

问题:

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)

回答1:

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.



回答2:

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.



回答3:

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.



回答4:

You normally do this by calling CreateFontIndirect and then getting the system to render. Perhaps you could explain why you can't use this standard approach.



回答5:

One solution would be to access the font files and extract the name from the name table to create your own lookup (an STL map would be a simple way of doing that). Details of the TTF file format can be found here.



标签: c++ winapi fonts