36 #if defined(__WXMSW__)
38 #include <wx/platform.h>
48 #if defined(__VISUALC__)
49 #pragma warning(disable : 4127) // conditional expression is constant
54 static LONG GetNextNameValue( HKEY key, LPCTSTR subkey, LPTSTR szName, LPTSTR szData );
55 static BOOL GetWinVer( LPTSTR lpszVersion,
int nVersionSize,
int* nVersion );
59 #define WUNKNOWNSTR _T("unknown Windows version")
61 #define W95STR _T("Windows 95")
62 #define W95SP1STR _T("Windows 95 SP1")
63 #define W95OSR2STR _T("Windows 95 OSR2")
64 #define W98STR _T("Windows 98")
65 #define W98SP1STR _T("Windows 98 SP1")
66 #define W98SESTR _T("Windows 98 SE")
67 #define WMESTR _T("Windows ME")
69 #define WNT351STR _T("Windows NT 3.51")
70 #define WNT4STR _T("Windows NT 4")
71 #define W2KSTR _T("Windows 2000")
72 #define WXPSTR _T("Windows XP")
73 #define W2003STR _T("Windows Server 2003")
74 #define WUNKNOWNPSTR _T("Windows Unknown")
76 #define WCESTR _T("Windows CE")
108 typedef struct _tagFONT_PROPERTIES_ANSI
111 char csCopyright[1024];
112 char csTrademark[1024];
115 FONT_PROPERTIES_ANSI;
117 typedef struct _tagTT_OFFSET_TABLE
119 USHORT uMajorVersion;
120 USHORT uMinorVersion;
123 USHORT uEntrySelector;
128 typedef struct _tagTT_TABLE_DIRECTORY
137 typedef struct _tagTT_NAME_TABLE_HEADER
141 USHORT uStorageOffset;
143 TT_NAME_TABLE_HEADER;
145 typedef struct _tagTT_NAME_RECORD
151 USHORT uStringLength;
152 USHORT uStringOffset;
156 #define SWAPWORD(x) MAKEWORD(HIBYTE(x), LOBYTE(x))
157 #define SWAPLONG(x) MAKELONG(SWAPWORD(HIWORD(x)), SWAPWORD(LOWORD(x)))
186 LPTSTR lpszDisplayName,
187 int nDisplayNameSize,
191 assert( lpszFontName && lpszFontName[0] != 0 );
192 if ( !lpszFontName || lpszFontName[0] == 0 )
195 assert( lpszDisplayName );
196 if ( !lpszDisplayName )
199 assert( lpszFontFile );
203 lpszDisplayName[0] = _T(
'\0' );
204 lpszFontFile[0] = _T(
'\0' );
206 TCHAR szName[2 * MAX_PATH];
207 TCHAR szData[2 * MAX_PATH];
210 TCHAR szVersion[100];
211 GetWinVer( szVersion,
sizeof( szVersion ) /
sizeof( TCHAR ) - 1, &nVersion );
213 TCHAR szFontPath[1000];
215 if ( ( nVersion >= WNTFIRST ) && ( nVersion <= WNTLAST ) )
216 _tcscpy( szFontPath, _T(
"Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts" ) );
218 _tcscpy( szFontPath, _T(
"Software\\Microsoft\\Windows\\CurrentVersion\\Fonts" ) );
220 BOOL bResult = FALSE;
222 while ( GetNextNameValue( HKEY_LOCAL_MACHINE, szFontPath, szName, szData ) == ERROR_SUCCESS )
224 if ( _tcsnicmp( lpszFontName, szName, _tcslen( lpszFontName ) ) == 0 )
226 _tcsncpy( lpszDisplayName, szName, nDisplayNameSize - 1 );
227 _tcsncpy( lpszFontFile, szData, nFontFileSize - 1 );
232 szFontPath[0] = _T(
'\0' );
235 GetNextNameValue( HKEY_LOCAL_MACHINE, NULL, NULL, NULL );
254 FONT_PROPERTIES_ANSI fp;
255 FONT_PROPERTIES_ANSI* lpFontProps = &fp;
257 memset( lpFontProps, 0,
sizeof( FONT_PROPERTIES_ANSI ) );
259 HANDLE hFile = INVALID_HANDLE_VALUE;
260 hFile = ::CreateFile( lpszFilePath,
265 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
268 if ( hFile == INVALID_HANDLE_VALUE )
274 DWORD dwFileSize = ::GetFileSize( hFile, NULL );
276 if ( dwFileSize == INVALID_FILE_SIZE )
278 ::CloseHandle( hFile );
283 HANDLE hMappedFile = NULL;
284 hMappedFile = ::CreateFileMapping( hFile,
291 if ( hMappedFile == NULL )
293 ::CloseHandle( hFile );
297 LPBYTE lpMapAddress = ( LPBYTE ) ::MapViewOfFile( hMappedFile,
303 if ( lpMapAddress == NULL )
305 ::CloseHandle( hMappedFile );
306 ::CloseHandle( hFile );
310 BOOL bRetVal = FALSE;
313 TT_OFFSET_TABLE ttOffsetTable;
314 memcpy( &ttOffsetTable, &lpMapAddress[index],
sizeof( TT_OFFSET_TABLE ) );
315 index +=
sizeof( TT_OFFSET_TABLE );
317 ttOffsetTable.uNumOfTables = SWAPWORD( ttOffsetTable.uNumOfTables );
318 ttOffsetTable.uMajorVersion = SWAPWORD( ttOffsetTable.uMajorVersion );
319 ttOffsetTable.uMinorVersion = SWAPWORD( ttOffsetTable.uMinorVersion );
322 if ( ttOffsetTable.uMajorVersion != 1 || ttOffsetTable.uMinorVersion != 0 )
325 TT_TABLE_DIRECTORY tblDir;
326 memset( &tblDir, 0,
sizeof( TT_TABLE_DIRECTORY ) );
329 memset( szTemp, 0,
sizeof( szTemp ) );
331 for (
int i = 0; i < ttOffsetTable.uNumOfTables; i++ )
334 memcpy( &tblDir, &lpMapAddress[index],
sizeof( TT_TABLE_DIRECTORY ) );
335 index +=
sizeof( TT_TABLE_DIRECTORY );
337 strncpy( szTemp, tblDir.szTag, 4 );
338 if ( stricmp( szTemp,
"name" ) == 0 )
341 tblDir.uLength = SWAPLONG( tblDir.uLength );
342 tblDir.uOffset = SWAPLONG( tblDir.uOffset );
345 else if ( szTemp[0] == 0 )
353 index = tblDir.uOffset;
355 TT_NAME_TABLE_HEADER ttNTHeader;
356 memcpy( &ttNTHeader, &lpMapAddress[index],
sizeof( TT_NAME_TABLE_HEADER ) );
357 index +=
sizeof( TT_NAME_TABLE_HEADER );
359 ttNTHeader.uNRCount = SWAPWORD( ttNTHeader.uNRCount );
360 ttNTHeader.uStorageOffset = SWAPWORD( ttNTHeader.uStorageOffset );
361 TT_NAME_RECORD ttRecord;
365 i < ttNTHeader.uNRCount &&
366 ( lpFontProps->csCopyright[0] == 0 ||
367 lpFontProps->csName[0] == 0 ||
368 lpFontProps->csTrademark[0] == 0 ||
369 lpFontProps->csFamily[0] == 0 );
372 memcpy( &ttRecord, &lpMapAddress[index],
sizeof( TT_NAME_RECORD ) );
373 index +=
sizeof( TT_NAME_RECORD );
375 ttRecord.uNameID = SWAPWORD( ttRecord.uNameID );
376 ttRecord.uStringLength = SWAPWORD( ttRecord.uStringLength );
377 ttRecord.uStringOffset = SWAPWORD( ttRecord.uStringOffset );
379 if ( ttRecord.uNameID == 1 || ttRecord.uNameID == 0 || ttRecord.uNameID == 7 )
383 index = tblDir.uOffset + ttRecord.uStringOffset + ttNTHeader.uStorageOffset;
385 memset( szTemp, 0,
sizeof( szTemp ) );
387 memcpy( szTemp, &lpMapAddress[index], ttRecord.uStringLength );
388 index += ttRecord.uStringLength;
390 if ( szTemp[0] != 0 )
392 assert( strlen( szTemp ) <
sizeof( lpFontProps->csName ) );
394 switch ( ttRecord.uNameID )
397 if ( lpFontProps->csCopyright[0] == 0 )
398 strncpy( lpFontProps->csCopyright, szTemp,
399 sizeof( lpFontProps->csCopyright ) - 1 );
403 if ( lpFontProps->csFamily[0] == 0 )
404 strncpy( lpFontProps->csFamily, szTemp,
405 sizeof( lpFontProps->csFamily ) - 1 );
410 if ( lpFontProps->csName[0] == 0 )
411 strncpy( lpFontProps->csName, szTemp,
412 sizeof( lpFontProps->csName ) - 1 );
416 if ( lpFontProps->csTrademark[0] == 0 )
417 strncpy( lpFontProps->csTrademark, szTemp,
418 sizeof( lpFontProps->csTrademark ) - 1 );
430 ::UnmapViewOfFile( lpMapAddress );
431 ::CloseHandle( hMappedFile );
432 ::CloseHandle( hFile );
434 if ( lpFontProps->csName[0] == 0 )
435 strcpy( lpFontProps->csName, lpFontProps->csFamily );
440 ::MultiByteToWideChar( CP_ACP, 0, lpFontProps->csName, -1, lpFontPropsX->csName,
441 sizeof( lpFontPropsX->csName ) /
sizeof( TCHAR ) - 1 );
442 ::MultiByteToWideChar( CP_ACP, 0, lpFontProps->csCopyright, -1, lpFontPropsX->csCopyright,
443 sizeof( lpFontPropsX->csCopyright ) /
sizeof( TCHAR ) - 1 );
444 ::MultiByteToWideChar( CP_ACP, 0, lpFontProps->csTrademark, -1, lpFontPropsX->csTrademark,
445 sizeof( lpFontPropsX->csTrademark ) /
sizeof( TCHAR ) - 1 );
446 ::MultiByteToWideChar( CP_ACP, 0, lpFontProps->csFamily, -1, lpFontPropsX->csFamily,
447 sizeof( lpFontPropsX->csFamily ) /
sizeof( TCHAR ) - 1 );
449 strcpy( lpFontPropsX->csName, lpFontProps->csName );
450 strcpy( lpFontPropsX->csCopyright, lpFontProps->csCopyright );
451 strcpy( lpFontPropsX->csTrademark, lpFontProps->csTrademark );
452 strcpy( lpFontPropsX->csFamily, lpFontProps->csFamily );
479 static LONG GetNextNameValue( HKEY key, LPCTSTR pszSubkey, LPTSTR pszName, LPTSTR pszData )
481 static HKEY hkey = NULL;
482 static DWORD dwIndex = 0;
486 if ( pszSubkey == NULL && pszName == NULL && pszData == NULL )
491 return ERROR_SUCCESS;
495 if ( pszSubkey && pszSubkey[0] != 0 )
497 retval = RegOpenKeyEx( key, pszSubkey, 0, KEY_QUERY_VALUE, &hkey );
498 if ( retval != ERROR_SUCCESS )
511 assert( pszName != NULL && pszData != NULL );
516 TCHAR szValueName[MAX_PATH];
517 DWORD dwValueNameSize =
sizeof( szValueName ) - 1;
518 BYTE szValueData[MAX_PATH];
519 DWORD dwValueDataSize =
sizeof( szValueData ) - 1;
522 retval = RegEnumValue( hkey, dwIndex, szValueName, &dwValueNameSize, NULL,
523 &dwType, szValueData, &dwValueDataSize );
524 if ( retval == ERROR_SUCCESS )
526 lstrcpy( pszName, ( LPTSTR )szValueName );
527 lstrcpy( pszData, ( LPTSTR )szValueData );
537 #ifndef VER_PLATFORM_WIN32s
538 #define VER_PLATFORM_WIN32s 0
540 #ifndef VER_PLATFORM_WIN32_WINDOWS
541 #define VER_PLATFORM_WIN32_WINDOWS 1
543 #ifndef VER_PLATFORM_WIN32_NT
544 #define VER_PLATFORM_WIN32_NT 2
546 #ifndef VER_PLATFORM_WIN32_CE
547 #define VER_PLATFORM_WIN32_CE 3
590 static BOOL GetWinVer( LPTSTR lpszVersion,
int nVersionSize,
int* pnVersion )
592 _tcsncpy( lpszVersion, WUNKNOWNSTR, nVersionSize - 1 );
593 *pnVersion = WUNKNOWN;
595 TCHAR* cp = WUNKNOWNSTR;
597 OSVERSIONINFO osinfo;
598 osinfo.dwOSVersionInfoSize =
sizeof( OSVERSIONINFO );
600 if ( !GetVersionEx( &osinfo ) )
603 DWORD dwPlatformId = osinfo.dwPlatformId;
604 DWORD dwMinorVersion = osinfo.dwMinorVersion;
605 DWORD dwMajorVersion = osinfo.dwMajorVersion;
606 DWORD dwBuildNumber = osinfo.dwBuildNumber & 0xFFFF;
608 if ( ( dwPlatformId == VER_PLATFORM_WIN32_WINDOWS ) && ( dwMajorVersion == 4 ) )
610 if ( ( dwMinorVersion < 10 ) && ( dwBuildNumber == 950 ) )
615 else if ( ( dwMinorVersion < 10 ) &&
616 ( ( dwBuildNumber > 950 ) && ( dwBuildNumber <= 1080 ) ) )
621 else if ( ( dwMinorVersion < 10 ) && ( dwBuildNumber > 1080 ) )
624 *pnVersion = W95OSR2;
626 else if ( ( dwMinorVersion == 10 ) && ( dwBuildNumber == 1998 ) )
631 else if ( ( dwMinorVersion == 10 ) &&
632 ( ( dwBuildNumber > 1998 ) && ( dwBuildNumber < 2183 ) ) )
637 else if ( ( dwMinorVersion == 10 ) && ( dwBuildNumber >= 2183 ) )
642 else if ( dwMinorVersion == 90 )
648 else if ( dwPlatformId == VER_PLATFORM_WIN32_NT )
650 if ( ( dwMajorVersion == 3 ) && ( dwMinorVersion == 51 ) )
655 else if ( ( dwMajorVersion == 4 ) && ( dwMinorVersion == 0 ) )
660 else if ( ( dwMajorVersion == 5 ) && ( dwMinorVersion == 0 ) )
665 else if ( ( dwMajorVersion == 5 ) && ( dwMinorVersion == 1 ) )
670 else if ( ( dwMajorVersion == 5 ) && ( dwMinorVersion == 2 ) )
677 else if ( dwPlatformId == VER_PLATFORM_WIN32_CE )
683 _tcsncpy( lpszVersion, cp, nVersionSize - 1 );
688 #endif //defined(__WXMSW__)
BOOL GetFontProperties(LPCTSTR lpszFilePath, LPFONT_PROPERTIES lpFontProps)
get font name from the file
BOOL GetFontFile(LPCTSTR lpszFontName, LPTSTR lpszDisplayName, int nDisplayNameSize, LPTSTR lpszFontFile, int nFontFileSize)
find a font that is close to a wxDC font.
MSW specific font properties.