ReactOS  0.4.14-dev-297-g23e575c
CFontCache.cpp
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Font Shell Extension
3  * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4  * PURPOSE: font list cache handling
5  * COPYRIGHT: Copyright 2019 Mark Jansen (mark.jansen@reactos.org)
6  */
7 
8 #include "precomp.h"
9 
11 
12 
13 #define FONT_HIVE HKEY_LOCAL_MACHINE
14 #define FONT_KEY L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts"
15 
17 
19  : m_Name(name)
20  , m_FileRead(false)
21 {
22 }
23 
24 const CStringW& CFontInfo::Name() const
25 {
26  return m_Name;
27 }
28 
29 const bool CFontInfo::Valid() const
30 {
31  return !m_Name.IsEmpty();
32 }
33 
35 {
36  if (!m_FileRead)
37  {
38  if (Valid())
39  {
40  // Read the filename stored in the registry.
41  // This can be either a filename or a full path
42  CRegKey key;
44  {
46  DWORD dwAllocated = 128;
48  do
49  {
50  DWORD dwSize = dwAllocated;
51  PWSTR Buffer = Value.GetBuffer(dwSize);
52  Status = key.QueryStringValue(m_Name, Buffer, &dwSize);
53  Value.ReleaseBuffer(dwSize);
54  if (Status == ERROR_SUCCESS)
55  {
56  // Ensure we do not re-use the same string object, by passing it a PCWSTR
57  m_File = Value.GetString();
58  break;
59  }
60  dwAllocated += 128;
61  } while (Status == ERROR_MORE_DATA);
62  }
63  }
64  m_FileRead = true;
65  }
66  return m_File;
67 }
68 
69 
70 
72 {
73 }
74 
76 {
77  if (m_Fonts.GetCount() == 0u)
78  Read();
79 
80  return m_Fonts.GetCount();
81 }
82 
84 {
85  if (m_Fonts.GetCount() == 0u)
86  Read();
87 
88  if (Index >= m_Fonts.GetCount())
89  return CStringW();
90 
91  return m_Fonts[Index].Name();
92 }
93 
95 {
96  if (fontEntry->Index < m_Fonts.GetCount())
97  {
98  CFontInfo& info = m_Fonts[fontEntry->Index];
99 
100  if (info.Name().CompareNoCase(fontEntry->Name) == 0)
101  return info.File();
102  }
103 
104  for (UINT n = 0; n < Size(); ++n)
105  {
106  if (m_Fonts[n].Name().CompareNoCase(fontEntry->Name) == 0)
107  return m_Fonts[n].File();
108  }
109 
110  return CStringW();
111 }
112 
114 {
115  POSITION it = fonts.GetHeadPosition();
116  while (it != NULL)
117  {
118  POSITION lastit = it;
119  const CFontInfo& info = fonts.GetNext(it);
120  if (info.Name().CompareNoCase(KeyName) >= 0)
121  {
122  fonts.InsertBefore(lastit, CFontInfo(KeyName));
123  return;
124  }
125  }
126  fonts.AddTail(CFontInfo(KeyName));
127 }
128 
130 {
131  CAtlList<CFontInfo> fonts;
132  CRegKey key;
133 
134  // Enumerate all registered font names
136  {
137  LSTATUS Status;
138  DWORD dwAllocated = 128;
139  DWORD ilIndex = 0;
141  do
142  {
143  DWORD dwSize = dwAllocated;
144  PWSTR Buffer = KeyName.GetBuffer(dwSize);
145  Status = RegEnumValueW(key.m_hKey, ilIndex, Buffer, &dwSize, NULL, NULL, NULL, NULL);
146  KeyName.ReleaseBuffer(dwSize);
147  if (Status == ERROR_SUCCESS)
148  {
149  // Insert will create an ordered list
150  Insert(fonts, KeyName);
151  ilIndex++;
152  continue;
153  }
155  break;
156  else if (Status == ERROR_MORE_DATA)
157  {
158  dwAllocated += 128;
159  }
160  } while (Status == ERROR_MORE_DATA || Status == ERROR_SUCCESS);
161  }
162 
163  // Move the fonts from a list to an array (for easy indexing)
164  m_Fonts.SetCount(fonts.GetCount());
165  size_t Index = 0;
166  POSITION it = fonts.GetHeadPosition();
167  while (it != NULL)
168  {
169  m_Fonts[Index] = fonts.GetNext(it);
170  Index++;
171  }
172 }
173 
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble * u
Definition: glfuncs.h:240
_In_opt_ ULONG _Out_ PULONG Value
Definition: rtlfuncs.h:2343
_Must_inspect_result_ _Out_ PNDIS_STATUS _In_ NDIS_HANDLE _In_ ULONG _Out_ PNDIS_STRING KeyName
Definition: ndis.h:4711
POSITION GetHeadPosition() const
Definition: atlcoll.h:523
CStringW m_File
Definition: CFontCache.hpp:15
#define ERROR_SUCCESS
Definition: deptool.c:10
#define FONT_HIVE
Definition: CFontCache.cpp:13
const bool Valid() const
Definition: CFontCache.cpp:29
void Insert(CAtlList< CFontInfo > &fonts, const CStringW &KeyName)
Definition: CFontCache.cpp:113
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
#define ERROR_NO_MORE_ITEMS
Definition: compat.h:95
CStringW m_Name
Definition: CFontCache.hpp:14
#define KEY_READ
Definition: nt_native.h:1023
uint16_t * PWSTR
Definition: typedefs.h:54
int CompareNoCase(_In_z_ PCXSTR psz) const
Definition: cstringt.h:610
GLdouble n
Definition: glext.h:7729
POSITION InsertBefore(_In_ POSITION pos, INARGTYPE element)
Definition: atlcoll.h:655
WINE_DEFAULT_DEBUG_CHANNEL(fontext)
_In_opt_ PVOID _In_ PCSTR File
Definition: iofuncs.h:615
LONG WINAPI RegEnumValueW(_In_ HKEY hKey, _In_ DWORD index, _Out_ LPWSTR value, _Inout_ PDWORD val_count, _Reserved_ PDWORD reserved, _Out_opt_ PDWORD type, _Out_opt_ LPBYTE data, _Inout_opt_ PDWORD count)
Definition: reg.c:2853
smooth NULL
Definition: ftsmooth.c:416
Definition: bufpool.h:45
size_t GetCount() const
Definition: atlcoll.h:511
E & GetNext(_Inout_ POSITION &pos)
Definition: atlcoll.h:535
CAtlArray< CFontInfo > m_Fonts
Definition: CFontCache.hpp:29
CStringW Filename(const FontPidlEntry *fontEntry)
Definition: CFontCache.cpp:94
static const UCHAR Index[8]
Definition: usbohci.c:18
unsigned long DWORD
Definition: ntddk_ex.h:95
static LSTATUS(WINAPI *pRegDeleteTreeW)(HKEY
HKEY key
Definition: reg.c:42
#define false
Definition: stdbool.h:38
CFontCache * g_FontCache
Definition: CFontCache.cpp:16
WCHAR Name[1]
Definition: fontpidl.hpp:17
void Read()
Definition: CFontCache.cpp:129
#define ERROR_MORE_DATA
Definition: dderror.h:13
Status
Definition: gdiplustypes.h:24
bool IsEmpty() const
Definition: atlsimpstr.h:379
POSITION AddTail(INARGTYPE element)
Definition: atlcoll.h:598
CStringW Name(size_t Index)
Definition: CFontCache.cpp:83
const CStringW & File()
Definition: CFontCache.cpp:34
bool m_FileRead
Definition: CFontCache.hpp:16
unsigned int UINT
Definition: ndis.h:50
size_t Size()
Definition: CFontCache.cpp:75
#define FONT_KEY
Definition: CFontCache.cpp:14
Definition: name.c:36
CAtlStringW CStringW
Definition: atlstr.h:130
const CStringW & Name() const
Definition: CFontCache.cpp:24
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:54
CFontInfo(LPCWSTR name=L"")
Definition: CFontCache.cpp:18
Definition: path.c:42