Click here to Skip to main content
6,630,289 members and growing! (15,557 online)
Email Password   helpLost your password?
Multimedia » GDI » Fonts     Intermediate License: The Code Project Open License (CPOL)

Finding a Font file from a Font name

By Hans Dietrich

How to find the name of a font file, given the display name of a font
VC6Win2K, MFC, GDI, Dev
Posted:10 Jul 2001
Views:85,341
Bookmarked:22 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
20 votes for this article.
Popularity: 5.83 Rating: 4.48 out of 5
1 vote, 6.7%
1

2
1 vote, 6.7%
3
3 votes, 20.0%
4
10 votes, 66.7%
5

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)

About the Author

Hans Dietrich


Member
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.
Occupation: Software Developer (Senior)
Company: Hans Dietrich Software
Location: United States United States

Other popular GDI articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 17 of 17 (Total in Forum: 17) (Refresh)FirstPrevNext
Generalforeign languages Pinmembereborger5:32 19 Aug '09  
GeneralUseless Pinmemberkilt6:10 1 Dec '08  
GeneralRe: Useless PinmemberRoey C10:10 29 Jul '09  
QuestionBold/Italic PinmemberVerifier22:24 8 Feb '07  
Questionfont engine PinmemberSavitha Rao2:15 3 Aug '06  
GeneralEnumerating all chinese fonts... PinmemberLuS714:39 1 Jun '06  
QuestionCan I Have the C# version of the above code PinmemberRangashan23:34 5 Oct '05  
AnswerRe: Can I Have the C# version of the above code PinmemberMohamed Elzahaby9:54 22 Nov '05  
QuestionFinding Fonts PinmemberGary Graf13:40 31 Aug '05  
AnswerRe: Finding Fonts Pinmembermmjjkk5:57 12 Nov '07  
GeneralConverted into VB.net PinmemberMohamed Elzahaby6:14 1 May '05  
Generalworkdir and fonts PinsussAnonymous22:59 5 Apr '04  
GeneralSame on Solaris PinsussReuti Raman Babu19:03 30 Oct '02  
GeneralReverse lookup Pinmemberaxiac6:42 18 Feb '02  
GeneralRe: Reverse lookup PinmemberHans Dietrich23:06 18 Feb '02  
GeneralRe: Reverse lookup PinmemberPhilip Patrick14:06 17 May '02  
Generala bug Pinmembersilly16:05 28 Sep '01  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 10 Jul 2001
Editor: Nishant Sivakumar
Copyright 2001 by Hans Dietrich
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project