Add NV12/NV21 support.
[xy_vsfilter.git] / src / dsutil / FontInstaller.cpp
blobfedc60abe5541a01c19a924afd26fbdbafc0ad1d
1 #include "StdAfx.h"
2 #include ".\fontinstaller.h"
4 CFontInstaller::CFontInstaller()
6 if(HMODULE hGdi = GetModuleHandle(_T("gdi32.dll")))
8 pAddFontMemResourceEx = (HANDLE (WINAPI *)(PVOID,DWORD,PVOID,DWORD*))GetProcAddress(hGdi, "AddFontMemResourceEx");
9 pAddFontResourceEx = (int (WINAPI *)(LPCTSTR,DWORD,PVOID))GetProcAddress(hGdi, "AddFontResourceExA");
10 pRemoveFontMemResourceEx = (BOOL (WINAPI *)(HANDLE))GetProcAddress(hGdi, "RemoveFontMemResourceEx");
11 pRemoveFontResourceEx = (BOOL (WINAPI *)(LPCTSTR,DWORD,PVOID))GetProcAddress(hGdi, "RemoveFontResourceExA");
14 if(HMODULE hGdi = GetModuleHandle(_T("kernel32.dll")))
16 pMoveFileEx = (BOOL (WINAPI *)(LPCTSTR, LPCTSTR, DWORD))GetProcAddress(hGdi, "MoveFileExA");
20 CFontInstaller::~CFontInstaller()
22 UninstallFonts();
25 bool CFontInstaller::InstallFont(const CAtlArray<BYTE>& data)
27 return InstallFont(data.GetData(), data.GetCount());
30 bool CFontInstaller::InstallFont(const void* pData, UINT len)
32 return InstallFontFile(pData, len) || InstallFontMemory(pData, len);
35 void CFontInstaller::UninstallFonts()
37 if(pRemoveFontMemResourceEx)
39 POSITION pos = m_fonts.GetHeadPosition();
40 while(pos) pRemoveFontMemResourceEx(m_fonts.GetNext(pos));
41 m_fonts.RemoveAll();
44 if(pRemoveFontResourceEx)
46 POSITION pos = m_files.GetHeadPosition();
47 while(pos)
49 CString fn = m_files.GetNext(pos);
50 pRemoveFontResourceEx(fn, FR_PRIVATE, 0);
51 if(!DeleteFile(fn) && pMoveFileEx)
52 pMoveFileEx(fn, NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
55 m_files.RemoveAll();
59 bool CFontInstaller::InstallFontMemory(const void* pData, UINT len)
61 if(!pAddFontMemResourceEx)
62 return false;
64 DWORD nFonts = 0;
65 HANDLE hFont = pAddFontMemResourceEx((PVOID)pData, len, NULL, &nFonts);
66 if(hFont && nFonts > 0) m_fonts.AddTail(hFont);
67 return hFont && nFonts > 0;
70 bool CFontInstaller::InstallFontFile(const void* pData, UINT len)
72 if(!pAddFontResourceEx)
73 return false;
75 CFile f;
76 TCHAR path[MAX_PATH], fn[MAX_PATH];
77 if(!GetTempPath(MAX_PATH, path) || !GetTempFileName(path, _T("g_font"), 0, fn))
78 return false;
80 if(f.Open(fn, CFile::modeWrite))
82 f.Write(pData, len);
83 f.Close();
85 if(pAddFontResourceEx(fn, FR_PRIVATE, 0) > 0)
87 m_files.AddTail(fn);
88 return true;
92 DeleteFile(fn);
93 return false;