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.

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!

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!

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.

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.

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

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:

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:

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:

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!