How to Use a Font Without Installing It

Many times, a particular font needs to be used in an applications due to inhouse graphics designer's font choice. In order for the application to use the fonts, the font needs to be installed using the installer. Too many fonts on the user machine may slow the system down considerably.

You can actually get away without installing the font: GDI and GDI+ each provide two ways for you, as a programmer, to add a font for an application to use without installing it. I'll show you how in this article!

GDI's AddFontResourceEx and AddFontMemResourceEx

AddFontResourceEx

Let me first talk about GDI's two functions for adding fonts to application for use. I'll then talk about GDI+'s own functions. You can use AddFontResourceEx to add a physical font file for the application to use.

int AddFontResourceEx(
  LPCTSTR lpszFilename, // font file name
  DWORD fl,             // font characteristics
  PVOID pdv             // reserved
);

Here is an example on how to use AddFontResourceEx.

CString szFontFile = "D:\\SkiCargo.ttf";

int nResults = AddFontResourceEx(
	m_szFontFile, // font file name
	FR_PRIVATE,             // font characteristics
	NULL);

To use the font you've added, just specify its name in the CreateFont or CreateFontIndirect function like any other installed font. To know the name of the font, just right click on the ttf extension file in the Windows Explorer and select "Open" and you will see its actual name. Or you can use the TTF and TTC class which I wrote. to know its font name

Note: The font filename("SkiCargo.ttf") in this article is actually its font name, "SkiCargo", this is usually not the case! To be on the safe side, use the right click method or TTF and TTC class, I just mentioned, to find out its name!

CClientDC dc(this);

dc.SetBkMode(TRANSPARENT);

LOGFONT lf;
memset(&lf, 0, sizeof(lf));
lf.lfHeight = -MulDiv(24, pDC->GetDeviceCaps(LOGPIXELSY), 72);
lf.lfWeight = FW_NORMAL;
lf.lfOutPrecision = OUT_TT_ONLY_PRECIS;
wcscpy_s(lf.lfFaceName, L"SkiCargo");

// create and select it
CFont newFont;
if (!newFont.CreateFontIndirect(&lf))
	return;
CFont* pOldFont = dc.SelectObject(&newFont);

// use a path to record how the text was drawn
wchar_t buf[] = _T("The quick brown fox jumps over the lazy dog!");
dc.TextOut( 10, 10, buf, wcslen(buf));

// Put back the old font
dc.SelectObject(pOldFont);

You must remember to call RemoveFontResourceEx before the application exits. You should note that the parameters must be the same as the ones that you fed into AddFontResourceEx!

BOOL RemoveFontResourceEx(
  LPCTSTR lpFileName,  // name of font file
  DWORD fl,            // font characteristics
  PVOID pdv            // Reserved.
);
CString szFontFile = "D:\\SkiCargo.ttf";

BOOL b = RemoveFontResourceEx(
	m_szFontFile,  // name of font file
	FR_PRIVATE,            // font characteristics
	NULL            // Reserved.
	);

AddFontMemResourceEx

If our font is in a resource dll, cabinet file or archival compressed file, you can extract it into the memory and then use AddFontMemResourceEx to read it from the memory.

HANDLE AddFontMemResourceEx(
  PVOID pbFont,       // font resource
  DWORD cbFont,       // number of bytes in font resource 
  PVOID pdv,          // Reserved. Must be 0.
  DWORD *pcFonts      // number of fonts installed
);

Here is an example on how to use AddFontMemResourceEx on a font file embedded in the resource.

HINSTANCE hResInstance = AfxGetResourceHandle( );

HRSRC res = FindResource(hResInstance,
	MAKEINTRESOURCE(IDR_MYFONT),L"BINARY");
if (res) 
{
	HGLOBAL mem = LoadResource(hResInstance, res);
	void *data = LockResource(mem);
	size_t len = SizeofResource(hResInstance, res);

	DWORD nFonts;
	m_fonthandle = AddFontMemResourceEx(
		data,       // font resource
		len,       // number of bytes in font resource 
		NULL,          // Reserved. Must be 0.
		&nFonts      // number of fonts installed
		);

	if(m_fonthandle==0)
	{
		MessageBox(L"Font add fails", L"Error");
	}
}

To use the font you have added, please refer to the previous AddFontResourceEx example. They are the same. Just use it like any other installed font. You should call RemoveFontMemResourceEx before the application exits. When the process goes away, the system will unload the fonts, even if you don't call RemoveFontMemResourceEx. Note: The parameters must be the same as the ones you feed into AddFontResourceEx!

BOOL RemoveFontMemResourceEx(
  HANDLE fh   // handle to the font resource
);
if(m_fonthandle)
{
	BOOL b = RemoveFontMemResourceEx(m_fonthandle);
	if(b==0)
	{
		MessageBox(L"Font remove fails", L"Error");
	}
}

GDI+'s PrivateFontCollection::AddFontFile and PrivateFontCollection::AddMemoryFont

PrivateFontCollection's AddFontFile

For GDI+, you can use its PrivateFontCollection class member, AddFontFile to add a physical font file.

Status AddFontFile(const WCHAR* filename);

Here is how to use AddFontFile to add a font file.

Gdiplus::PrivateFontCollection m_fontcollection;
//...
CString szFontFile = szExePath + L"SkiCargo.ttf";

Gdiplus::Status nResults = m_fontcollection.AddFontFile(szFontFile);

Here is how to use the font we have just added to PrivateFontCollection object, m_fontcollection.

// When painting the text
FontFamily fontFamily;
int nNumFound=0;
m_fontcollection.GetFamilies(1,&fontFamily,&nNumFound);

if(nNumFound>0)
{
	Font font(&fontFamily,28,FontStyleRegular,UnitPixel);

	StringFormat strformat;
	wchar_t buf[] = L"The quick brown fox jumps over the lazy dog!";
	graphics.DrawString(buf,wcslen(buf),&font,PointF(10.0f,10.0f),&strformat,&brush);
}

Note: unlike the GDI's AddFontResourceEx and AddFontMemResourceEx, there is no RemoveFontFile for AddFontFile. All added fonts will be removed by PrivateFontCollection's destructor.

PrivateFontCollection's AddMemoryFont

For GDI+, you can use its PrivateFontCollection class member, AddMemoryFont to add a font in memory.

Status AddMemoryFont(const VOID *memory, INT length);

Here is how to use AddMemoryFont on a font file embedded in the resource. Similar to AddFontFile, there is no RemoveMemoryFont to call. Everything will be taken care of by PrivateFontCollection's destructor.

HINSTANCE hResInstance = AfxGetResourceHandle( );

HRSRC res = FindResource(hResInstance,
	MAKEINTRESOURCE(IDR_MYFONT),L"BINARY");
if (res) 
{
	HGLOBAL mem = LoadResource(hResInstance, res);
	void *data = LockResource(mem);
	size_t len = SizeofResource(hResInstance, res);

	Gdiplus::Status nResults = m_fontcollection.AddMemoryFont(data,len);

	if(nResults!=Gdiplus::Ok)
	{
		MessageBox(L"Font add fails", L"Error");
	}
}

As to how to use the font you have just added to PrivateFontCollection object, m_fontcollection, please refer to the previous AddFontFile example, they are the same.

How to Use a Font Without Installing It

TTF and TTC font names

I have written 2 classes, namely TTF and TTC to read the font name from the TTF/OTF and TTC font file respectively. To support Matroska(mkv) file font reading or embedded font resource reading, my TTF and TTC class supports parsing the font file in memory. For your information, these Matroska file usually contains the video channel, the audio channels for multiple languages, the subtitles and the fonts used for the subtitles in the video. My classes are really easy to use. Below is a example to read a TTF file physically or in memory and display its information.

void TestReadTtfFromFile(const std::wstring& szFile)
{
	TTF ttf;
	ttf.Parse(szFile);
	Display(ttf);
}

void TestReadTtfFromMemory(const std::wstring& szFile)
{
	struct _stat bufferStat;
	int nRet = _wstat(szFile.c_str(), &bufferStat);
	FILE* pFile = _wfopen(szFile.c_str(), L"rb");
	if(pFile == NULL)
	{
		std::wcout<<L"Failed to create file"<<std::endl;
		return;
	}
	BYTE* buf = new BYTE[bufferStat.st_size];
	fread(buf,bufferStat.st_size,1,pFile);
	fclose(pFile);
	TTF ttf;
	ttf.Parse(buf, bufferStat.st_size);

	delete [] buf;

	Display(ttf);
}

void Display(TTF& ttf)
{
	std::wcout<<L"FontName : "<<ttf.GetFontName()<<std::endl;
	std::wcout<<L"Copyright : "<<ttf.GetCopyright()<<std::endl;
	std::wcout<<L"FontFamilyName : "<<ttf.GetFontFamilyName()<<std::endl;
	std::wcout<<L"FontSubFamilyName : "<<ttf.GetFontSubFamilyName()<<std::endl;
	std::wcout<<L"FontID : "<<ttf.GetFontID()<<std::endl;
	std::wcout<<L"Version : "<<ttf.GetVersion()<<std::endl;
	std::wcout<<L"PostScriptName : "<<ttf.GetPostScriptName()<<std::endl;
	std::wcout<<L"Trademark : "<<ttf.GetTrademark()<<std::endl;

	std::wstring szBold = ttf.IsBold() ? L"true" : L"false"; 
	std::wstring szItalic = ttf.IsItalic() ? L"true" : L"false"; 
	std::wstring szRegular = ttf.IsRegular() ? L"true" : L"false"; 

	std::wcout<<L"Bold : "<<szBold<<std::endl;
	std::wcout<<L"Italic : "<<szItalic<<std::endl;
	std::wcout<<L"Regular : "<<szRegular<<std::endl;

	std::wcout<<std::endl;
}

TTC is a font file which contains a collection of TTF fonts. Below is a example to read a TTC file physically or in memory and display its information.

void TestReadTtcFromFile(const std::wstring& szFile)
{
	TTC ttc;
	ttc.Parse(szFile);
	Display(ttc);
}

void TestReadTtcFromMemory(const std::wstring& szFile)
{
	struct _stat bufferStat;
	int nRet = _wstat(szFile.c_str(), &bufferStat);
	FILE* pFile = _wfopen(szFile.c_str(), L"rb");
	if(pFile == NULL)
	{
		std::wcout<<L"Failed to create file"<<std::endl;
		return;
	}
	BYTE* buf = new BYTE[bufferStat.st_size];
	fread(buf,bufferStat.st_size,1,pFile);
	fclose(pFile);
	TTC ttc;
	ttc.Parse(buf, bufferStat.st_size);

	delete [] buf;

	Display(ttc);

}

void Display(TTC& ttc)
{
	for(size_t i=0; i<ttc.Size(); ++i )
	{
		std::wcout<<L"FontName : "<<ttc.GetFontName(i)<<std::endl;
		std::wcout<<L"Copyright : "<<ttc.GetCopyright(i)<<std::endl;
		std::wcout<<L"FontFamilyName : "<<ttc.GetFontFamilyName(i)<<std::endl;
		std::wcout<<L"FontSubFamilyName : "<<ttc.GetFontSubFamilyName(i)<<std::endl;
		std::wcout<<L"FontID : "<<ttc.GetFontID(i)<<std::endl;
		std::wcout<<L"Version : "<<ttc.GetVersion(i)<<std::endl;
		std::wcout<<L"PostScriptName : "<<ttc.GetPostScriptName(i)<<std::endl;
		std::wcout<<L"Trademark : "<<ttc.GetTrademark(i)<<std::endl;

		std::wstring szBold = ttc.IsBold(i) ? L"true" : L"false"; 
		std::wstring szItalic = ttc.IsItalic(i) ? L"true" : L"false"; 
		std::wstring szRegular = ttc.IsRegular(i) ? L"true" : L"false"; 

		std::wcout<<L"Bold : "<<szBold<<std::endl;
		std::wcout<<L"Italic : "<<szItalic<<std::endl;
		std::wcout<<L"Regular : "<<szRegular<<std::endl;

		std::wcout<<std::endl;
	}
}

Note:You should always call GetFontFamilyName method to get the font name, not the GetFontName method. Most fonts belong to a font family. For example, under Arial font family, they are several Arial fonts whose font names are "Arial Bold", "Arial Bold Italic" and so on. Below is an example on how to use TTF's GetFontFamilyName method with AddFontResourceEx function

TTF m_Ttf;

//... During Initialization
CString szFontFile = "D:\\SkiCargo.ttf";

int nResults = AddFontResourceEx(
	m_szFontFile, // font file name
	FR_PRIVATE,             // font characteristics
	NULL);

m_Ttf.Parse((LPCWSTR)(m_szFontFile));
	
	
//... In the OnPaint method	
	
CClientDC dc(this);

dc.SetBkMode(TRANSPARENT);

LOGFONT lf;
memset(&lf, 0, sizeof(lf));
lf.lfHeight = -MulDiv(24, pDC->GetDeviceCaps(LOGPIXELSY), 72);
lf.lfWeight = FW_NORMAL;
lf.lfOutPrecision = OUT_TT_ONLY_PRECIS;
//wcscpy_s(lf.lfFaceName, L"SkiCargo");
wcscpy_s(lf.lfFaceName, m_Ttf.GetFontFamilyName().c_str());

// create and select it
CFont newFont;
if (!newFont.CreateFontIndirect(&lf))
	return;
CFont* pOldFont = dc.SelectObject(&newFont);

// use a path to record how the text was drawn
wchar_t buf[] = _T("The quick brown fox jumps over the lazy dog!");
dc.TextOut( 10, 10, buf, wcslen(buf));

// Put back the old font
dc.SelectObject(pOldFont);

Note:I cannot find enough information on the web to parse a fon file which is a font file with the "fon" extension. I tried reverse engineering to get the file name but failed. However, I will continue trying.

Add Font File to a Resource

To add a font file to a resource section, please follow my walkthrough example. Please note my method is to edit the resource file directly, instead of adding it though the IDE's resource editor because in my experience, the resource editor has the tendency to mess up the resource's rc file, rendering the WYSIWYG dialog editor unusable. Note: The latest resource editor may be more robust and stable now. To add a font file, we must assign a resource ID to refers to the font. To do this, close your solution or project in concern, if they are opened. To assign a resource ID, open up Resource.h:

//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by TestGDI_AddFontMem.RC
//
#define IDR_MAINFRAME			128
#define IDD_TESTGDI_ADDFONTMEM_DIALOG	102

// Next default values for new objects
// 
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS

#define _APS_NEXT_RESOURCE_VALUE		129
#define _APS_NEXT_CONTROL_VALUE		1000
#define _APS_NEXT_SYMED_VALUE		101
#define _APS_NEXT_COMMAND_VALUE		32771
#endif
#endif

I believe you have many more resource IDs than this simple project of mine. Let's name our defined ID, "IDR_MYFONT". Of course, you can name it any way you deem appropriate. We assign IDR_MYFONT, the current value of _APS_NEXT_RESOURCE_VALUE which is 129. And then we will increment _APS_NEXT_RESOURCE_VALUE; This is important we must do this or else the next resource will share the same numeric ID as your font. Below is what the manually edited Resource.h looks like:

//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by TestGDI_AddFontMem.RC
//
#define IDR_MAINFRAME			128
#define IDD_TESTGDI_ADDFONTMEM_DIALOG	102
#define IDR_MYFONT				129

// Next default values for new objects
// 
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS

#define _APS_NEXT_RESOURCE_VALUE		130
#define _APS_NEXT_CONTROL_VALUE		1000
#define _APS_NEXT_SYMED_VALUE		101
#define _APS_NEXT_COMMAND_VALUE		32771
#endif
#endif

Next we will edit the rc extension file. Open the file in your favorite text editor. Note: Your Visual Studio should not be opening the project which this rc file is in. Search for the section listed below:

/////////////////////////////////////////////////////////////////////////////
//
// BINARY
//

If you cannot find this section, you can add it yourself. Next add the font id and its font file. Your binary section may look like this.

/////////////////////////////////////////////////////////////////////////////
//
// BINARY
//
IDR_MYFONT              BINARY                  "res\\SkiCargo.ttf"

As the RC code shows, IDR_MYFONT is a binary resource which references a SkiCargo.ttf file in a "res" subfolder of the project folder.

If you find adding a font to resource is a hassle, you can rename the font filename and its extension, so that nobody will know that file is a font and mess with it. You can even encrypt or compress it. Just decrypt it or uncompress it in memory before reading file in memory.

Conclusion

You have seen two methods from GDI and GDI+ each, to either load font files physically or from memory and use them. I hope this may remove the need for programmers to install fonts on user machine to use them. I have introduced 2 classes to read TTF and TTC font files for their font names. I have also shown you how to add a font to your application's resource. Anything you do like or not like about this article, please let me know, so that I can improve on this article. I do hope you enjoy reading my article!

Reference

History

  • 8th October 2009: Added "Add Font File to a Resource" section
  • 31st August 2009 - Added classes to read TTF and TTC family font names and other information.
  • 14th April 2009 - First release


About the Author

Wong Shao Voon

I guess I'll write here what I does in my free time, than to write an accolade of skills which I currently possess. I believe the things I does in my free time, say more about me.

When I am not working, I like to watch Japanese anime. I am also writing some movie script, hoping to see my own movie on the big screen one day.

I like to jog because it makes me feel good, having done something meaningful in the morning before the day starts.

I also writes articles for CodeGuru; I have a few ideas to write about but never get around writing because of hectic schedule.

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds