Installing fonts with application

Posted on 2001-06-25
Last Modified: 2013-12-03
Does anyone know a good way to temporarily (or permanently) install fonts for use with my app. I am leaning toward temporary installation because i am worried about all the ways things can break if i try the permanent install approach (e.g. win95 has limits on number of fonts)?

I'll append my own approach as a comment, which doesn't seem to work yet, in case someone has specific comments on what i have come up with so far.

Question by:djelescu
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Author Comment

ID: 6225058
Here is what I came up with so far...

Reading the microsoft white-paper 'PRB: Embedded Read-Only Fonts in Multiple...',
I thought I understood the process for doing this. And I made a little FontmanagerClass
to hand the work for me. The use of this class is simple...
1. Inside CMyApp:InitInstance() you must call...
   The help path is used as a tipoff for where ttf files may be found.
   Fontmanager looks throu that directory and copies ttf files into windows\system
   Then it folows the guidelines of the microsoft whitepaper to registre those fonts
2. Any rules for transforming the 'notmal' name of a font to the name that is really
   appropriate for filling in the FONT structure, are expected to happen inside ToInstanceName()
   Thus, one could  write...
   cMyFont.CreatePointFont(100, FontManager::ToInstanceName("Dungeon"));
   and let the FontManager transform the name (e.g. append '.ttf' or somethin) as appropriate.

I thought this class was a really cool idea.
But it just does not seem to work.
All the font registration call appear to be successful,
but in the end, I can not use any font
that has not already been registered on my system in the usual way.
How can my application use irregular fonts, if I can't get this to work?


p.s. Code below is my best effort to implement FontManager.
Try to get a wacky/unregistered font to try on your system.

// FontManager.h

class FontManager  
      static CStringArray _deletables;
      static CString _tempTtfPrefix;
      static void OnInitInstanceLoadFonts(CString helpFilePathForReference);
      static void OnExitInstanceUnloadFonts();
      static CString ToInstanceName(CString commonName);

// FontManager.cpp

CString FontManager::_tempTtfPrefix = "";
CStringArray FontManager::_deletables;

CString FontManager::ToInstanceName(CString commonName)
      // What to return here???
      // commonName  ???
      // commonName + ".ttf" ???
      // _tempTtfPrefix + commonName + ".ttf" ??
      // This is just a guess...
      return _tempTtfPrefix + commonName;

void FontManager::OnInitInstanceLoadFonts(CString helpFilePathForReference)
      // We use the help file to get a reference to the main application directory.
      // Probably there is a better way, but this seems to work.
      int iPos = helpFilePathForReference.ReverseFind('.');
      if(iPos != helpFilePathForReference.GetLength() - 4) {
      } else {
            // Now we use the application name
            // and the time to generate a unique string
            // that we will prepend to each ttf file
            // as we copy it to the system folder.
            // This will help avoid conflicts with existing copies
            // if they exist.
            _tempTtfPrefix = helpFilePathForReference.Left(iPos);
            iPos = helpFilePathForReference.ReverseFind('\\');
            CString fontDirectory = helpFilePathForReference.Left(iPos);
            _tempTtfPrefix = _tempTtfPrefix.Right(_tempTtfPrefix.GetLength() - iPos - 1);
            char systemDir[MAX_PATH+1];
            GetSystemDirectory(systemDir, MAX_PATH);
            int timeAsInt = time(NULL);
            char buffer[20];
            _itoa(timeAsInt, buffer, 10);
            _tempTtfPrefix += buffer;
            // To do the copying, we iterate through the application folder and
            // find the ttf files and copy them to the system with prefix prepended...
            CFileFind ttfFinder;
            BOOL bWorking = ttfFinder.FindFile(fontDirectory + "\\*.ttf");
            while (bWorking)    {
                  bWorking = ttfFinder.FindNextFile();
                  CString source = ttfFinder.GetFilePath();
                  CString dest = systemDir + CString("\\") + _tempTtfPrefix + ttfFinder.GetFileName();
                        if(CopyFile(source, dest, false)) {
                              // After each copy is made in system directory, we
                              // use CreateScalableFontResource to make a temporary resource
                              CString fotFile = systemDir + CString("\\") + _tempTtfPrefix + ttfFinder.GetFileTitle() + ".fot";
                                    // And we add the font resource
                                    // so we can use it
                                    int numFontsAdded = AddFontResource(fotFile);
                                    if(numFontsAdded > 0)
                              } else {
                  } catch(...) {}

void FontManager::OnExitInstanceUnloadFonts()
      for(int i=0; i<_deletables.GetSize(); i++) {
            try{ DeleteFile(_deletables[i]); } catch(...) {}

Author Comment

ID: 6225255
Did I mention that the code above does not work.
What to do?
LVL 49

Accepted Solution

DanRollins earned 300 total points
ID: 6225448
Remove the try... catch to see if an erro is occuring (or at least add AfxMessageBox("Catch caught")

How do you know that the font installation has failed? Does CreatePointFont return FALSE?  Show your test code.

Have you tried just hardcoding some font as a "proof-of-concept" test?  That is always a good idea before implementing a class:  Find out if the basic premise is viable, then build around that functionality.

Also, it looks like you may be confusing the font file name with the font face name.  They are distinct.  CreatePointFont needs to see the font face name, not a filename.

-- Dan

Expert Comment

ID: 6225659
LVL 23

Expert Comment

by:Roshan Davis
ID: 9490126
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by : DanRollins

Please leave any comments here within the next seven days.


Roshan Davis
EE Cleanup Volunteer

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sumDigits challenge 9 172
no14 challenge 14 73
sumHeights  challenge 17 79
x-bar in Google Sheets 2 91
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question