Go Premium for a chance to win a PS4. Enter to Win


Installing fonts with application

Posted on 2001-06-25
Medium Priority
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

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 fontfreak.com 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 1200 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
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

876 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