|
Location: Desktop development - C/C++ License: The Intelliproject Open License (IPOL) How to Use Fonts Without Installing themPosted by Wong Shao VoonHow to Use a Font Without Installing it on user's system |
Skill: BeginnerPosted: 14/04/2009Views: 1420Rating: 5.00 /5Popularity: 1.51 |
| Sign Up to vote for this article |
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!
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.
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 Windows Explorer right click method or TTF and TTC class, I just mentioned, to find out its name!
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. );
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.
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 );
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.
Here is how to use the font we have just added to PrivateFontCollection object, m_fontcollection.
Note: unlike the GDI's AddFontResourceEx and AddFontMemResourceEx, there is no RemoveFontFile for AddFontFile. All added fonts will be removed by PrivateFontCollection's destructor.
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.
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.
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.
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.
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.
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. 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!
| 03/09/2009 | Add classes to read TTF/OTF and TTC font information |
This article, along with any associated source code and files, is licensed under The Intelliproject Open License (IPOL)
| 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 IntelliProject; I have a few ideas to write about but never get around writing because of hectic schedule. Location: |
Sign up to post message on the article message board!