Click here to Skip to main content
Click here to Skip to main content

Finding a Font file from a Font name

, 10 Jul 2001 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Hans Dietrich
Software Developer (Senior) Hans Dietrich Software
United States United States
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.
 
Recently, I have moved to Los Angeles where I am doing consulting and development work.
 
For consulting and custom software development, please see www.hdsoft.org.






Comments and Discussions

 
GeneralUseless Pinmemberkilt1-Dec-08 6:10 
GeneralRe: Useless PinmemberRoey C29-Jul-09 10:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 11 Jul 2001
Article Copyright 2001 by Hans Dietrich
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid