Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FontInstaller.cpp « DSUtil « src - github.com/mpc-hc/mpc-hc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 842bdd837cdde12bd61ed6f90c3e41304b1df318 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "stdafx.h"
#include "./fontinstaller.h"

CFontInstaller::CFontInstaller()
{
	if(HMODULE hGdi = GetModuleHandle(_T("gdi32.dll")))
	{
		pAddFontMemResourceEx = (HANDLE (WINAPI *)(PVOID,DWORD,PVOID,DWORD*))GetProcAddress(hGdi, "AddFontMemResourceEx");
		pAddFontResourceEx = (int (WINAPI *)(LPCTSTR,DWORD,PVOID))GetProcAddress(hGdi, "AddFontResourceExA");
		pRemoveFontMemResourceEx = (BOOL (WINAPI *)(HANDLE))GetProcAddress(hGdi, "RemoveFontMemResourceEx");
		pRemoveFontResourceEx = (BOOL (WINAPI *)(LPCTSTR,DWORD,PVOID))GetProcAddress(hGdi, "RemoveFontResourceExA");
	}

	if(HMODULE hGdi = GetModuleHandle(_T("kernel32.dll")))
	{
		pMoveFileEx = (BOOL (WINAPI *)(LPCTSTR, LPCTSTR, DWORD))GetProcAddress(hGdi, "MoveFileExA");
	}
}

CFontInstaller::~CFontInstaller()
{
	UninstallFonts();
}

bool CFontInstaller::InstallFont(const CAtlArray<BYTE>& data)
{
	return InstallFont(data.GetData(), data.GetCount());
}

bool CFontInstaller::InstallFont(const void* pData, UINT len)
{
	return InstallFontFile(pData, len) || InstallFontMemory(pData, len);
}

void CFontInstaller::UninstallFonts()
{
	if(pRemoveFontMemResourceEx)
	{
		POSITION pos = m_fonts.GetHeadPosition();
		while(pos) pRemoveFontMemResourceEx(m_fonts.GetNext(pos));
		m_fonts.RemoveAll();
	}

	if(pRemoveFontResourceEx)
	{
		POSITION pos = m_files.GetHeadPosition();
		while(pos)
		{
			CString fn = m_files.GetNext(pos);
			pRemoveFontResourceEx(fn, FR_PRIVATE, 0);
			if(!DeleteFile(fn) && pMoveFileEx)
				pMoveFileEx(fn, NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
		}
		
		m_files.RemoveAll();
	}
}

bool CFontInstaller::InstallFontMemory(const void* pData, UINT len)
{
	if(!pAddFontMemResourceEx)
		return false;

	DWORD nFonts = 0;
	HANDLE hFont = pAddFontMemResourceEx((PVOID)pData, len, NULL, &nFonts);
	if(hFont && nFonts > 0) m_fonts.AddTail(hFont);
	return hFont && nFonts > 0;
}

bool CFontInstaller::InstallFontFile(const void* pData, UINT len)
{
	if(!pAddFontResourceEx) 
		return false;

	CFile f;
	TCHAR path[_MAX_PATH], fn[_MAX_PATH];
	if(!GetTempPath(MAX_PATH, path) || !GetTempFileName(path, _T("g_font"), 0, fn))
		return false;

	if(f.Open(fn, CFile::modeWrite))
	{
		f.Write(pData, len);
		f.Close();

		if(pAddFontResourceEx(fn, FR_PRIVATE, 0) > 0)
		{
			m_files.AddTail(fn);
			return true;
		}
	}

	DeleteFile(fn);
	return false;
}