FontInstaller.cpp

00001 #include "StdAfx.h"
00002 #include ".\fontinstaller.h"
00003 
00004 CFontInstaller::CFontInstaller()
00005 {
00006         if(HMODULE hGdi = GetModuleHandle(_T("gdi32.dll")))
00007         {
00008                 pAddFontMemResourceEx = (HANDLE (WINAPI *)(PVOID,DWORD,PVOID,DWORD*))GetProcAddress(hGdi, "AddFontMemResourceEx");
00009                 pAddFontResourceEx = (int (WINAPI *)(LPCTSTR,DWORD,PVOID))GetProcAddress(hGdi, "AddFontResourceExA");
00010                 pRemoveFontMemResourceEx = (BOOL (WINAPI *)(HANDLE))GetProcAddress(hGdi, "RemoveFontMemResourceEx");
00011                 pRemoveFontResourceEx = (BOOL (WINAPI *)(LPCTSTR,DWORD,PVOID))GetProcAddress(hGdi, "RemoveFontResourceExA");
00012         }
00013 
00014         if(HMODULE hGdi = GetModuleHandle(_T("kernel32.dll")))
00015         {
00016                 pMoveFileEx = (BOOL (WINAPI *)(LPCTSTR, LPCTSTR, DWORD))GetProcAddress(hGdi, "MoveFileExA");
00017         }
00018 }
00019 
00020 CFontInstaller::~CFontInstaller()
00021 {
00022         UninstallFonts();
00023 }
00024 
00025 bool CFontInstaller::InstallFont(const CArray<BYTE>& data)
00026 {
00027         return InstallFont(data.GetData(), data.GetSize());
00028 }
00029 
00030 bool CFontInstaller::InstallFont(const void* pData, UINT len)
00031 {
00032         return InstallFontFile(pData, len) || InstallFontMemory(pData, len);
00033 }
00034 
00035 void CFontInstaller::UninstallFonts()
00036 {
00037         if(pRemoveFontMemResourceEx)
00038         {
00039                 POSITION pos = m_fonts.GetHeadPosition();
00040                 while(pos) pRemoveFontMemResourceEx(m_fonts.GetNext(pos));
00041                 m_fonts.RemoveAll();
00042         }
00043 
00044         if(pRemoveFontResourceEx)
00045         {
00046                 POSITION pos = m_files.GetHeadPosition();
00047                 while(pos)
00048                 {
00049                         CString fn = m_files.GetNext(pos);
00050                         pRemoveFontResourceEx(fn, FR_PRIVATE, 0);
00051                         if(!DeleteFile(fn) && pMoveFileEx)
00052                                 pMoveFileEx(fn, NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
00053                 }
00054                 
00055                 m_files.RemoveAll();
00056         }
00057 }
00058 
00059 bool CFontInstaller::InstallFontMemory(const void* pData, UINT len)
00060 {
00061         if(!pAddFontMemResourceEx)
00062                 return false;
00063 
00064         DWORD nFonts = 0;
00065         HANDLE hFont = pAddFontMemResourceEx((PVOID)pData, len, NULL, &nFonts);
00066         if(hFont && nFonts > 0) m_fonts.AddTail(hFont);
00067         return hFont && nFonts > 0;
00068 }
00069 
00070 bool CFontInstaller::InstallFontFile(const void* pData, UINT len)
00071 {
00072         if(!pAddFontResourceEx) 
00073                 return false;
00074 
00075         CFile f;
00076         TCHAR path[MAX_PATH], fn[MAX_PATH];
00077         if(!GetTempPath(MAX_PATH, path) || !GetTempFileName(path, _T("g_font"), 0, fn))
00078                 return false;
00079 
00080         if(f.Open(fn, CFile::modeWrite))
00081         {
00082                 f.Write(pData, len);
00083                 f.Close();
00084 
00085                 if(pAddFontResourceEx(fn, FR_PRIVATE, 0) > 0)
00086                 {
00087                         m_files.AddTail(fn);
00088                         return true;
00089                 }
00090         }
00091 
00092         DeleteFile(fn);
00093         return false;
00094 }

Generated on Tue Dec 13 14:47:07 2005 for guliverkli by  doxygen 1.4.5