uLib  User mode C/C++ extended API library for Win32 programmers.
ResFont.cpp
Go to the documentation of this file.
1 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 // Project: uLib - User mode utility library.
3 // Module: Resource fonts - Allow using custom fonts without installing them.
4 // Author: Copyright (c) Love Nystrom
5 // License: NNOSL (BSD descendant, see NNOSL.txt in the base directory).
6 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7 
8 #include <uLib/ResFont.h>
9 #include <uLib/UtilFunc.h>
10 #include <uLib/StrFunc.h>
11 
13 : _pDir( (PTTFDirectory)NULL ), _hfMemo( NULL ), _Size( 0 ), _nFonts( 0 )
14 {
15  memset( TypeFace, 0, sizeof(TypeFace) );
16 }
17 
18 TTResFont::TTResFont( HMODULE hModule, CSTR Id, CSTR Type )
19 : _pDir( (PTTFDirectory)NULL ), _hfMemo( NULL ), _Size( 0 ), _nFonts( 0 )
20 {
21  memset( TypeFace, 0, sizeof(TypeFace) );
22  Load( hModule, Id, Type );
23 }
24 
26 {
27  _freeFontMem();
28 }
29 
30 bool TTResFont::Load( HMODULE hModule, CSTR Id, CSTR Type )
31 {
32  TypeFace[0] = 0;
33  _freeFontMem(); // Free previous resource font, if any
34  if (!hModule) hModule = GetModuleHandle( NULL );
35 
36  _pDir = (PTTFDirectory) LoadCustomResource( hModule, Id, Type, &_Size );
37  // NOTE: ReactOS is missing AddFontMemResourceEx...
38  _hfMemo = _pDir ? AddFontMemResourceEx( _pDir, _Size, NULL, (DWORD*)&_nFonts ) : NULL;
39 
40  if (_hfMemo) _getTypeface();
41  return (_hfMemo != NULL);
42 }
43 
44 HFONT TTResFont::CreateFont( int Points, int Weight, DWORD CharSet, DWORD Quality )
45 {
46  return _hfMemo
47  ? CreateFontEx( TypeFace, Points, Weight, CharSet, Quality )
48  : NULL;
49 }
50 
51 void TTResFont::_freeFontMem()
52 {
53  if (_hfMemo)
54  {
55  RemoveFontMemResourceEx( _hfMemo );
56  _hfMemo = NULL;
57  }
58 }
59 
60 bool TTResFont::_getTypeface()
61 {
62  TypeFace[0] = 0;
63 
64  // Scan for the name table directory entry.
65 
66  PTTFDirEntry pNameTbl = NULL;
67  UINT ix = 0, nTbl = _bswap16( _pDir->NrTables );
68  while( ix < nTbl )
69  {
70  PTTFDirEntry pEntry = &_pDir->Entry[ix];
71  if (pEntry->Tag == TTT_NAME)
72  {
73  pNameTbl = pEntry;
74  break;
75  }
76  ix++;
77  }
78  if (pNameTbl)
79  {
80  // Get the name table itself.
81 
82  ULONG tblOffs = _bswap32( pNameTbl->Offset );
83  PTTFNames pNames = PTTFNames( PBYTE(_pDir) + tblOffs );
84 
85  // Scan for the family name record.
86 
87  USHORT nNames = _bswap16( pNames->NrNames );
88  ix = 0;
89  while( ix < nNames )
90  {
91  PTTFNameRec pRec = &pNames->Names[ix];
92  USHORT recId = _bswap16( pRec->Id );
93  if (recId == TTNI_FAMILY) // Check the "name id".
94  {
95  // Locate the name in the string space.
96 
97  USHORT strOffs = _bswap16( pNames->Offset );
98  USHORT nameOffs = _bswap16( pRec->Offset );
99  USHORT nameLen = _bswap16( pRec->Length );
100  if (nameLen >= dimof(TypeFace)) nameLen = dimof(TypeFace)-1; // Clip the excess.
101 
102  CSTR pName = CSTR( PBYTE(pNames) + strOffs + nameOffs );
103  _tcsncpyz( TypeFace, pName, nameLen+1 );
104  break;
105  }
106  ix++;
107  }
108  }
109  return (TypeFace[0] != 0);
110 }
unsigned long DWORD
Definition: Common.h:414
#define CSTR
Definition: Common.h:329
HFONT WINAPI CreateFontEx(CSTR Typeface, int Points, int Weight, DWORD Charset=ANSI_CHARSET, DWORD Quality=ANTIALIASED_QUALITY)
Definition: GdiUtil.cpp:30
unsigned char * PBYTE
Definition: Common.h:412
UINT16 __cdecl _bswap16(UINT16 Val)
Definition: Common.cpp:428
#define dimof(x)
Definition: Common.h:949
bool Load(HMODULE hModule, CSTR Id, CSTR Type)
Definition: ResFont.cpp:30
virtual ~TTResFont()
Definition: ResFont.cpp:25
HFONT CreateFont(int Points, int Weight, DWORD CharSet=ANSI_CHARSET, DWORD Quality=ANTIALIASED_QUALITY)
Definition: ResFont.cpp:44
UINT32 __cdecl _bswap32(UINT32 Val)
Definition: Common.cpp:438
PVOID WINAPI LoadCustomResource(HMODULE hModule, CSTR Id, CSTR Type, UINT *pSize OPTOUT=NULL)
'name' - Naming table
Definition: TTFFile.h:53
TCHAR TypeFace[LF_FACESIZE]
Definition: ResFont.h:30
TTResFont()
Definition: ResFont.cpp:12
#define _tcsncpyz
Definition: StrFunc.h:77