Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Finding a Font file from a Font name

0.00/5 (No votes)
10 Jul 2001 1  
How to find the name of a font file, given the display name of a font

Sample Image - FontFile.jpg

Introduction

This topic seems to recycle through news groups every so often, so I thought to share one solution I came up with. The problem is to determine name of an installed font file (e.g., ARIAL.TTF), given display name of a font (e.g., Arial). The solution involves enumerating fonts listed in registry, after first checking Windows OS version.

What it does

The demo FontFileTest project shows how to retrieve font file name from a font's display name (which is name you will see in MS Word font combobox, for example). All Win32 versions of Windows, going back to original Win95, have a directory called "Fonts" under Windows directory. This directory is mirrored in registry under key HKLM\Software\Microsoft\Windows\CurrentVersion\Fonts. Except, of course, under NT, where "Windows" is replaced with "Windows NT".

How to use it

There is only one function that you need to call, GetFontFile. This takes care of checking OS version and enumerating fonts. The prototype looks like this: BOOL GetFontFile(LPCTSTR lpszFontName, CString& strDisplayName, CString& strFontFile). The argument lpszFontName passes in font's display name, and strDisplayName and strFontFile contain font's display name and file name upon successful return.

To integrate this function into your own program, you'll first need to add following files to your project:

  • GetFontFile.cpp
  • GetFontFile.h
  • GetNameValue.cpp
  • GetNameValue.h
  • GetWinVer.cpp
  • GetWinVer.h

Next, include header file GetFontFile.h in module where you want to call GetFontFile function. For an example of how to call GetFontFile, see FontFileTestDlg.cpp in demo project.

This code was built under VC 6.0, and has been tested under Win95, Win98, NT4, and Win2000. One thing to keep in mind is that GetFontFile() tries to match whatever string you pass in via lpszFontName. It doesn't have to be the complete display name. This means that if you call it like this: GetFontFile("a", strName, strFile), it will match the first font in the registry whose display name begins with an "a". Since the fonts in registry are not sorted when you enumerate them (although they appear to be when use Regedit), it is hard to say which font this would be. To see how this works, look at the TRACE output in the debug output window.

Why it may not work for you

Unfortunately, this cannot be considered a 100% effective method for finding name of a font file. If a font has been installed in a normal manner, and if it is in Windows "Font" directory, then this method will probably work. It will probably work for most screen fonts and TrueType fonts. However, this method might not work for fonts that are created or installed dynamically, or that are specific to a particular device, or that are not installed into font directory. I have noticed that on some systems, there are certain fonts that seem to be "hidden", such as "System" font and sometimes "MS Sans Serif" and "MS Serif". Before posting a message or sending me email, please check first to see if font is listed in the registry under key I give above. You have to decide for yourself whether problems I mention here can be lived with.

Acknowledgments

Thanks to Chris Maunder and all the other people who post here, for wonderful useful source code.

Usage

This software is released into the public domain. You are free to use it in any way you like. If you modify it or extend it, please to consider posting new code here for everyone to share. This software is provided "as is" with no expressed or implied warranty. I accept no liability for any damage or loss of business that this software may cause.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here