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

Finding a Font file from a Font name

By , 10 Jul 2001
 

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
Software Developer (Senior) Hans Dietrich Software
United States United States
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.
 
For consulting and custom software development, please see www.hdsoft.org.






Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalforeign languagesmembereborger19 Aug '09 - 4:32 
How can I translate foreign languages Bold and Italic to find the correct font file name ?
I have "Arial Bold" for example, but in the registry, the name appears as "Arial Negrito" for portuguese or "Arial Fett" for german, etc... is there a generic way of finding the correct font file name if I have only the english name of the font and the currecnt language is not english ???
 
Thanks,
Edgar
GeneralUselessmemberkilt1 Dec '08 - 5:10 
You just have to call undocumented Font Resource apis and that's all !
Newbie code
GeneralRe: UselessmemberRoey C29 Jul '09 - 9:10 
Useless? could you please post your way of finding a font's file?
 
Don't believe to what you hear on the news...

QuestionBold/ItalicmemberVerifier8 Feb '07 - 21:24 
How can I get the font filename for a bold/italic font?
Questionfont enginememberSavitha Rao3 Aug '06 - 1:15 
Can I have the C version of the above code?
 
Savitha Rao
GeneralEnumerating all chinese fonts...memberLuS711 Jun '06 - 3:39 
Hi!,
 
I've to enumerate all fonts wich can sho glyphs in range 4E00 - 9FFF
Please, can you help me ?
 
Best regard,
Luca
QuestionCan I Have the C# version of the above codememberRangashan5 Oct '05 - 22:34 
I need to have the C# version of the above code. So any one can pls help me to do this.
 
Thanks In Advance
 
Rangashan
AnswerRe: Can I Have the C# version of the above codememberMohamed Elzahaby22 Nov '05 - 8:54 
Hi Rangashan
 
I have converted it into a VB.net version which will be very easy for you to convert it into C#
 
http://www.codeproject.com/useritems/Finding_a_Font_file.asp[^]
 
Please if you need any extra help let me know .
thanks .
QuestionFinding FontsmemberGary Graf31 Aug '05 - 12:40 
I just downloaded XFontTest, Hans Dietrich's code from "Finding a Font file from a Font name". On compiling I get a linker error "LINK : fatal error LNK1104: cannot open file "mfc42u.lib". Im am running it under VC++ 6.0 and the closest .lib I can find is "mfc42d.lib". The hard part is trying to figure out where the link error is being generated from. Anyone have any thoughts on how to figure it out? Thanks - Gary

AnswerRe: Finding Fontsmembermmjjkk12 Nov '07 - 4:57 
Hi,
 
In the VC setup, you probably did not select the unicode libraries to be installed. You may want to re-run the VC setup and install also the unicode libraries. Or you may compile a non-unicode version of the XFontTest.
 
Hope this helps,
/Mika
GeneralConverted into VB.netmemberMohamed Elzahaby1 May '05 - 5:14 
Thanks very much for this nice article
I converted it to VB.Net
http://www.codeproject.com/useritems/Finding_a_Font_file.asp[^]
Generalworkdir and fontssussAnonymous5 Apr '04 - 21:59 
Any idea how to load font from program directory without installing it to font dir? Is writing it registry key the only solution ?
 

GeneralSame on SolarissussReuti Raman Babu30 Oct '02 - 18:03 
I want similar functionality on Solaris.
Can anybody tell me how to that?
I am a newbie for Solaris.
 
reuti
GeneralReverse lookupmemberaxiac18 Feb '02 - 5:42 
Any idea about finding the font display name from the .ttf file? The font is not installed, so you cannot find it in registry Frown | :(
I want to write a program similar to fontview.exe
 
--
axiac
GeneralRe: Reverse lookupmemberHans Dietrich18 Feb '02 - 22:06 
That sounds like an interesting project. There are no API's that do this, you must read through font file yourself. The ttf file is basically a series of tables, linked together with offsets, etc. The table you want to find is called Name Table (tag = "name").
 
My suggestion: read entire file into memory, then treat it as byte array.
 
You can find full details of ttf file format at www.wotsit.org, search for "ttf".
 
I don't know of anyone who has done a complete ttf file analyzer/viewer - this might be good idea for shareware product!
 
Best wishes,
Hans

GeneralRe: Reverse lookupmemberPhilip Patrick17 May '02 - 13:06 
Just made it an posted an article about retrieving font name from ttf file. You can look it here:
 
http://www.codeproject.com/useritems/fontnamefromfile.asp
 
Thanks to Hans for links Smile | :) They were useful
 
Philip Patrick
Web-site: www.stpworks.com
"Two beer or not two beer?" Shakesbeer
 
Need Web-based database administrator? You already have it!
Generala bugmembersilly28 Sep '01 - 15:05 
It works well in many cases. But in some case, the font file including two or more fonts, the display name in registry likes "MyFont & XMyFont (TrueType)", while one using GetFontFileName("XMyFont"), it can not get result properly.(see GetFontfile.cpp)

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

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