summaryrefslogtreecommitdiff
path: root/FontInfo.cpp
blob: beed32465e0284750f079a2fae5a5ce6013fa905 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// FontInfo.cpp : Implementation of CFontInfo
#include "stdafx.h"
#include "TtfInfo.h"
#include "FontInfo.h"

/////////////////////////////////////////////////////////////////////////////
// CFontInfo
LPCTSTR CFontInfo::m_strFontsKey = NULL;

STDMETHODIMP CFontInfo::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_ITrueTypeFontInfo,
	};
	for ( int i = 0; i < sizeof( arr ) / sizeof( arr[0] ); i++ )
	{
		if ( InlineIsEqualGUID( *arr[i], riid ) )
			return S_OK;
	}
	return S_FALSE;
}

STDMETHODIMP CFontInfo::get__FileName(BSTR * pVal)
{
	*pVal = m_bstrFileName.copy();
	return S_OK;
}

STDMETHODIMP CFontInfo::put__FileName(BSTR newVal)
{
	using namespace std;
	
	Initialize();

	try
	{
		m_bstrFileName = _bstr_t(newVal).copy();
	}
	catch (_com_error Err)
	{
		m_bstrFileName = "";
		return Err.Error();
	}

	const int						BUFFER_SIZE = 512;
	const int						MAX_TABLES	= 40;

	wchar_t							namebuf[BUFFER_SIZE];
	unsigned short					numNames;
	unsigned	int					cTables;
	sfnt_OffsetTable				OffsetTable;
	sfnt_DirectoryEntry			Table;
	sfnt_NamingTable				NamingTable;
	sfnt_NameRecord				NameRecord;
	streampos						curseek;

	try
	{
	
		USES_CONVERSION;
		ifstream TtfFile( W2CA( newVal ), ios_base::in | ios_base::binary );
		
		if ( !TtfFile.is_open() )
		{
			m_bstrFileName = "";
			return AtlReportError( CLSID_FontInfo, IDERR_FileOpen, GUID_NULL, E_FAIL, _Module.GetResourceInstance() );
		}

		TtfFile.read( reinterpret_cast<char*>( &OffsetTable ), sizeof( OffsetTable ) - sizeof( sfnt_DirectoryEntry ) );
		cTables = (int) SWAPW( OffsetTable.numOffsets );

		for (int i = 0; i < cTables and i < MAX_TABLES; i++)
		{
			TtfFile.read( reinterpret_cast<char*>( &Table ), sizeof( Table ) );

			if (Table.tag == tag_NamingTable)
			{
				TtfFile.seekg( SWAPL( Table.offset ) );
				TtfFile.read( reinterpret_cast<char*>( &NamingTable ),
								sizeof( NamingTable ) );
				numNames = SWAPW( NamingTable.count );

				while ( numNames-- ) 
				{
					TtfFile.read ( reinterpret_cast<char*>( &NameRecord ), 
									 sizeof( NameRecord ) );
					
					curseek = TtfFile.tellg();

					if ( SWAPW( NameRecord.platformID ) == TTF::Microsoft )
					{
						TtfFile.seekg(  SWAPW( NameRecord.offset ) + 
										  SWAPW( NamingTable.stringOffset ) + 
										  SWAPL( Table.offset ) );
						TtfFile.read( reinterpret_cast<char*>( &namebuf ),
										min(SWAPW(NameRecord.length), BUFFER_SIZE) );
						namebuf[SWAPW( NameRecord.length ) / sizeof( wchar_t ) ] = L'\0';

						for (int i = 0; i < BUFFER_SIZE and i < SWAPW( NameRecord.length ) /  sizeof( wchar_t ); i++)
							namebuf[i] = SWAPW( namebuf[i] );

						TtfFile.seekg( curseek );

						switch ( SWAPW( NameRecord.nameID ) )
						{
						case TTF::Copyright:
							m_bstrCopyright = namebuf;
							break;
						case TTF::Family:
							m_bstrFamily = namebuf;
							break;
						case TTF::ID:
							m_bstrID = namebuf;
							break;
						case TTF::Name:
							m_bstrName = namebuf;
							break;
						case TTF::PostscriptName:
							m_bstrPostscriptName = namebuf;
							break;
						case TTF::Subfamily:
							m_bstrSubfamily = namebuf;
							break;
						case TTF::Trademark:
							m_bstrTrademark = namebuf;
							break;
						case TTF::Version:
							m_bstrVersion = namebuf;
							break;
						default:
							break;
						}
						
					}	// if ( SWAPW( NameRecord.platformID ) == TTF::Microsoft

				}	// while ( numNames-- ) 

			}	// if (Table.tag == tag_NamingTable)

		}	// for (int i = 0; i < cTables and i < 40; i++)

	}
	catch(...)
	{
		return E_UNEXPECTED;
	}

	return S_OK;
}

STDMETHODIMP CFontInfo::get_FileName(BSTR * pVal)
{
	return get__FileName(pVal);
}

STDMETHODIMP CFontInfo::put_FileName(BSTR newVal)
{
	return put__FileName(newVal);
}

STDMETHODIMP CFontInfo::get_Copyright(BSTR * pVal)
{
	*pVal = m_bstrCopyright.copy();
	return S_OK;
}

STDMETHODIMP CFontInfo::get_Family(BSTR * pVal)
{
	*pVal = m_bstrFamily.copy();
	return S_OK;
}

STDMETHODIMP CFontInfo::get_ID(BSTR * pVal)
{
	*pVal = m_bstrID.copy();
	return S_OK;
}

STDMETHODIMP CFontInfo::get_Name(BSTR * pVal)
{
	*pVal = m_bstrName.copy();
	return S_OK;
}

STDMETHODIMP CFontInfo::get_PostscriptName(BSTR * pVal)
{
	*pVal = m_bstrPostscriptName.copy();
	return S_OK;
}

STDMETHODIMP CFontInfo::get_Subfamily(BSTR * pVal)
{
	*pVal = m_bstrSubfamily.copy();
	return S_OK;
}

STDMETHODIMP CFontInfo::get_Trademark(BSTR * pVal)
{
	*pVal = m_bstrTrademark.copy();
	return S_OK;
}

STDMETHODIMP CFontInfo::get_Version(BSTR * pVal)
{
	*pVal = m_bstrVersion.copy();
	return S_OK;
}

STDMETHODIMP CFontInfo::get_RegisteredFileName(BSTR * pVal)
{
	CRegKey reg;
	LONG lRet = 0;

	lRet = OpenFontsKey( reg, KEY_READ );
	// We'll only return an error in the event that we can't open
	// the Fonts key, any other registry errors will result in an
	// empty RegisteredFileName string
   if ( ERROR_SUCCESS != lRet )
		return HRESULT_FROM_WIN32( lRet );

	// First get the size
	DWORD dwSize = 0;
	lRet = reg.QueryValue( NULL, GetRegistryFontName(), &dwSize );

	if ( 0 == dwSize )
	{
		*pVal = _bstr_t("").copy();
		return S_OK;
	}
		
	LPTSTR strFileName = new TCHAR [dwSize];
	lRet = reg.QueryValue( strFileName, GetRegistryFontName(), &dwSize );

	if ( ERROR_SUCCESS != lRet)
		*pVal = _bstr_t("").copy();
	else
		*pVal = _bstr_t(strFileName).copy();

	delete [] strFileName;
	return S_OK;
}

STDMETHODIMP CFontInfo::GetFontsDirectory(BSTR * pVal)
{
	LPITEMIDLIST pIDL = NULL;
	HRESULT hRes = SHGetSpecialFolderLocation( NULL, CSIDL_FONTS, &pIDL );

	if ( SUCCEEDED( hRes ) )
	{
		TCHAR pszPath [MAX_PATH] = {0};
		BOOL bSuccess = SHGetPathFromIDList( pIDL, pszPath );
		CoTaskMemFree( pIDL );
		if ( bSuccess )
		{
			*pVal = _bstr_t( pszPath ).copy();
			return S_OK;
		}
		else
			hRes = E_FAIL;
	}

	return AtlReportError( CLSID_FontInfo, IDERR_GetFontsDirectory, GUID_NULL, hRes, _Module.GetResourceInstance() );
}

STDMETHODIMP CFontInfo::Install(VARIANT_BOOL bPermanent)
{
	if ( _bstr_t("") == m_bstrFileName  )
		return AtlReportError( CLSID_FontInfo,
			   IDERR_NoFileName, GUID_NULL, E_FAIL, _Module.GetResourceInstance() );

	if ( VARIANT_TRUE == bPermanent )
	{
		// It's up to the user to check whether this font is already installed
		CRegKey reg;
		LONG lRet = 0;

		lRet = OpenFontsKey( reg, KEY_WRITE );
      if ( ERROR_SUCCESS != lRet )
			return AtlReportError( CLSID_FontInfo,
			   IDERR_OpenFontsKey, GUID_NULL, HRESULT_FROM_WIN32( lRet ), _Module.GetResourceInstance() );

		lRet = reg.SetValue( m_bstrFileName, GetRegistryFontName() );
      if ( ERROR_SUCCESS != lRet )
			return AtlReportError( CLSID_FontInfo,
			   IDERR_Install, GUID_NULL, HRESULT_FROM_WIN32( lRet ), _Module.GetResourceInstance() );

	}

	int ret = AddFontResource( m_bstrFileName );
	::SendMessage( HWND_BROADCAST, WM_FONTCHANGE, 0, 0 );

	return S_OK;
}

STDMETHODIMP CFontInfo::Uninstall(VARIANT_BOOL bPermanent)
{
	if ( _bstr_t("") == m_bstrFileName  )
		return AtlReportError( CLSID_FontInfo,
			   IDERR_NoFileName, GUID_NULL, E_FAIL, _Module.GetResourceInstance() );

	if ( VARIANT_TRUE == bPermanent )
	{
		// It's up to the user to ensure that they really
		// want to remove the font before calling this method
		CRegKey reg;
		LONG lRet = 0;

		lRet = OpenFontsKey(reg, KEY_WRITE );
      if ( ERROR_SUCCESS != lRet )
			return AtlReportError( CLSID_FontInfo,
			   IDERR_OpenFontsKey, GUID_NULL, HRESULT_FROM_WIN32( lRet ), _Module.GetResourceInstance() );

		lRet = reg.DeleteValue( GetRegistryFontName() );
		if ( ERROR_SUCCESS != lRet )
			return AtlReportError( CLSID_FontInfo,
			   IDERR_Uninstall, GUID_NULL, HRESULT_FROM_WIN32( lRet ), _Module.GetResourceInstance() );

	}

	int ret = RemoveFontResource( m_bstrFileName );
	::SendMessage( HWND_BROADCAST, WM_FONTCHANGE, 0, 0 );

	return S_OK;
}