Link to home
Start Free TrialLog in
Avatar of djelescu
djelescu

asked on

Installing fonts with application

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.

Cheers,
-d
Avatar of djelescu
djelescu

ASKER

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...
      FontManager::OnInitInstanceLoadFonts(m_pszHelpFilePath);
   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?
Help!!!!!!!

Regards,
-d

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  
{
protected:
      static CStringArray _deletables;
      static CString _tempTtfPrefix;
public:
      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) {
            ASSERT(false);
      } 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();
                  try{
                        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";
                              if(CreateScalableFontResource(1,
                                    fotFile.GetBuffer(MAX_PATH+1),
                                    dest.GetBuffer(MAX_PATH+1),
                                    NULL))
                              {
                                    ////////////////////////////////
                                    // And we add the font resource
                                    // so we can use it
                                    int numFontsAdded = AddFontResource(fotFile);
                                    if(numFontsAdded > 0)
                                          AfxMessageBox("Success!");
                                    else
                                          AfxMessageBox("Failure!");
                              } else {
                                    AfxMessageBox("Failure!");
                              }
                              fotFile.ReleaseBuffer();
                              dest.ReleaseBuffer();
                              _deletables.Add(fotFile);
                              _deletables.Add(dest);
                        }
                  } catch(...) {}
            }
      }
}

void FontManager::OnExitInstanceUnloadFonts()
{
      for(int i=0; i<_deletables.GetSize(); i++) {
            try{ DeleteFile(_deletables[i]); } catch(...) {}
      }
}
Did I mention that the code above does not work.
What to do?
ASKER CERTIFIED SOLUTION
Avatar of DanRollins
DanRollins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Roshan Davis
EE Cleanup Volunteer