Click here to Skip to main content
15,867,950 members
Articles / Multimedia / GDI
Article

XFont - Get font name and file information

Rate me:
Please Sign up or sign in to vote.
4.73/5 (15 votes)
21 May 2003CPOL2 min read 113K   2.9K   19   9
XFont retrieves the name of the font file from the regsitry, and the font name from the font file

Introduction

XFont comprises two functions that are useful when trying to determine the name of the font file given the font name, and conversely when trying to find the font name from the font file. I have already introduced GetFontFile() in a previous article. This article presents a new non-MFC version of GetFontFile() and also GetFontProperties(), which together allow you to find the name of the font or the name of the font file.

First, let me acknowledge the work of Philip Patrick, who researched the font file format to come up with GetFontProperties(). I have taken his code and removed the MFC dependencies, added Unicode support, and converted the CFile file I/O to a memory-mapped file.

XFont Functions

  • GetFontFile() - Find the name of font file.
    ///////////////////////////////////////////////////////////////////////////////
    //
    // GetFontFile()
    //
    // Purpose:     Find the name of font file from the font name
    //
    // Parameters:  lpszFontName     - name of font
    //              lpszDisplayName  - pointer to buffer where font display name
    //                                 will be copied
    //              nDisplayNameSize - size of display name buffer in TCHARs
    //              lpszFontFile     - pointer to buffer where font file name
    //                                 will be copied
    //              nFontFileSize    - size of font file buffer in TCHARs
    //
    // Returns:     BOOL - TRUE = success
    //
    // Notes:       This is *not* a foolproof method for finding the name of a
    //              font file. If a font has been installed in a normal manner,
    //              and if it is in the 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 the font directory.
    // 
  • GetFontProperties() - Get font name from font file.
    ///////////////////////////////////////////////////////////////////////////////
    //
    // GetFontProperties()
    //
    // Purpose:     Get font name from font file
    //
    // Parameters:  lpszFilePath - file path of font file
    //              lpFontPropsX - pointer to font properties struct
    //
    // Returns:     BOOL - TRUE = success
    // 

How To Use

To integrate XFont into your app, you first need to add following files to your project:

  • XFont.cpp
  • XFont.h

If you include XFont in project that uses precompiled headers, you must change C/C++ Precompiled Headers settings to Not using precompiled headers for XFont.cpp.

Next, include the header file XFont.h in appropriate project files. Now you are ready to start using XFont. The two XFont functions are demonstrated fully in the XFont demo app.

Demo App

The XFontTest.exe demo tests the APIs in XFont. Here is some of the output:

XFont screenshot

Revision History

Version 1.1 - 2003 May 21

  • Removed MFC dependency from GetFontProperties
  • Converted CFile file I/O to memory mapped file
  • Added Unicode support
  • Combined with my GetFontFile routine

Version 1.0

  • Initial (separate) release of GetFontFile and GetFontProperties

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)


Written By
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

 
GeneralMy vote of 4 Pin
brianbacon8-Jan-11 3:36
brianbacon8-Jan-11 3:36 
GeneralXFont on Vista 64bits Pin
Mirco Babin1-Jul-09 1:40
Mirco Babin1-Jul-09 1:40 
GeneralXFont on Vista Pin
shanem2-Oct-07 1:06
shanem2-Oct-07 1:06 
QuestionCan I copy font data? Pin
Member 118951717-Jun-05 1:14
Member 118951717-Jun-05 1:14 
GeneralRegestry Path for Type 1 Fonts Pin
King Nak2-Mar-04 1:30
King Nak2-Mar-04 1:30 
GeneralA little bug in the GetNextNameValue Pin
Zwer2k2-Dec-03 23:07
Zwer2k2-Dec-03 23:07 
GeneralPostscript outline Opentype fonts Pin
RajanChaturvedi29-Oct-03 6:27
RajanChaturvedi29-Oct-03 6:27 
Hi,
I am using the postscript outline open type fonts in my
Application(QuarkXPress). While printing these fonts to the printer i am
using wingdi function EXtEscape() for GETFACENAME and DOWNLOADFACENAME.
The facename returned by the ExtEscape() for GETFACENAME is different in
different printer for these Postscript(T1) outline opentype fonts.
Like I have used the Xerox Phaser 2135 PS3(here ExtEscape returns the face
name TT*******)
and Lexmark W820 PS3 (Extescape() returns the facename like
postscriptfontname021*** eg.. TektonPro-Regular0123***).
So my question is is this the correct behaviour of returning the different
facename in different printer?
How the printer driver returns these face names ?
while printing these fonts how it download these fonts?

Regards
Smile | :)
GeneralPostscript outline Opentype fonts Pin
rajendra chaturvedi29-Oct-03 6:27
rajendra chaturvedi29-Oct-03 6:27 
Questionttf font character set ??? Pin
Manikandan31-Aug-03 16:29
Manikandan31-Aug-03 16:29 

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

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