ReactOS  0.4.15-dev-1619-g69dffed
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,2020 Mark Jansen (mark.jansen@reactos.org)
6  */
7 
8 #include "precomp.h"
9 
11 
13 
15  : m_Name(name)
16  , m_FileRead(false)
17 {
18 }
19 
20 const CStringW& CFontInfo::Name() const
21 {
22  return m_Name;
23 }
24 
25 const bool CFontInfo::Valid() const
26 {
27  return !m_Name.IsEmpty();
28 }
29 
31 {
32  if (!m_FileRead)
33  {
34  if (Valid())
35  {
36  // Read the filename stored in the registry.
37  // This can be either a filename or a full path
38  CRegKey key;
40  {
42  DWORD dwAllocated = 128;
44  do
45  {
46  DWORD dwSize = dwAllocated;
47  PWSTR Buffer = Value.GetBuffer(dwSize);
48  Status = key.QueryStringValue(m_Name, Buffer, &dwSize);
49  Value.ReleaseBuffer(dwSize);
50  if (Status == ERROR_SUCCESS)
51  {
52  // Ensure we do not re-use the same string object, by passing it a PCWSTR
54  break;
55  }
56  dwAllocated += 128;
57  } while (Status == ERROR_MORE_DATA);
58  }
59  }
60  m_FileRead = true;
61  }
62  return m_File;
63 }
64 
65 
66 
68 {
69 }
70 
72 {
75 }
76 
78 {
79  if (m_Fonts.GetCount() == 0u)
80  Read();
81 
82  return m_Fonts.GetCount();
83 }
84 
86 {
87  if (m_Fonts.GetCount() == 0u)
88  Read();
89 
90  if (Index >= m_Fonts.GetCount())
91  return CStringW();
92 
93  return m_Fonts[Index].Name();
94 }
95 
96 CStringW CFontCache::Filename(const FontPidlEntry* fontEntry, bool alwaysFullPath)
97 {
98  CStringW File;
99 
100  if (fontEntry->Index < m_Fonts.GetCount())
101  {
102  CFontInfo& info = m_Fonts[fontEntry->Index];
103 
104  if (info.Name().CompareNoCase(fontEntry->Name) == 0)
105  File = info.File();
106  }
107 
108  for (UINT n = 0; File.IsEmpty() && n < Size(); ++n)
109  {
110  if (m_Fonts[n].Name().CompareNoCase(fontEntry->Name) == 0)
111  File = m_Fonts[n].File();
112  }
113 
114  if (!File.IsEmpty() && alwaysFullPath)
115  {
116  // Ensure this is a full path
117  if (PathIsRelativeW(File))
118  {
120  }
121  }
122 
123  return File;
124 }
125 
127 {
128  POSITION it = fonts.GetHeadPosition();
129  while (it != NULL)
130  {
131  POSITION lastit = it;
132  const CFontInfo& info = fonts.GetNext(it);
133  if (info.Name().CompareNoCase(KeyName) >= 0)
134  {
135  fonts.InsertBefore(lastit, CFontInfo(KeyName));
136  return;
137  }
138  }
139  fonts.AddTail(CFontInfo(KeyName));
140 }
141 
143 {
144  CAtlList<CFontInfo> fonts;
145  CRegKey key;
146 
147  // Enumerate all registered font names
149  {
150  LSTATUS Status;
151  DWORD dwAllocated = 128;
152  DWORD ilIndex = 0;
154  do
155  {
156  DWORD dwSize = dwAllocated;
157  PWSTR Buffer = KeyName.GetBuffer(dwSize);
158  Status = RegEnumValueW(key.m_hKey, ilIndex, Buffer, &dwSize, NULL, NULL, NULL, NULL);
159  KeyName.ReleaseBuffer(dwSize);
160  if (Status == ERROR_SUCCESS)
161  {
162  // Insert will create an ordered list
163  Insert(fonts, KeyName);
164  ilIndex++;
165  continue;
166  }
168  break;
169  else if (Status == ERROR_MORE_DATA)
170  {
171  dwAllocated += 128;
172  }
173  } while (Status == ERROR_MORE_DATA || Status == ERROR_SUCCESS);
174  }
175 
176  // Move the fonts from a list to an array (for easy indexing)
177  m_Fonts.SetCount(fonts.GetCount());
178  size_t Index = 0;
179  POSITION it = fonts.GetHeadPosition();
180  while (it != NULL)
181  {
182  m_Fonts[Index] = fonts.GetNext(it);
183  Index++;
184  }
185 }
186 
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
POSITION GetHeadPosition() const
Definition: atlcoll.h:549
BOOL WINAPI PathIsRelativeW(LPCWSTR lpszPath)
Definition: path.c:1558
CStringW m_File
Definition: CFontCache.hpp:15
#define ERROR_SUCCESS
Definition: deptool.c:10
const bool Valid() const
Definition: CFontCache.cpp:25
void Insert(CAtlList< CFontInfo > &fonts, const CStringW &KeyName)
Definition: CFontCache.cpp:126
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
#define ERROR_NO_MORE_ITEMS
Definition: compat.h:105
CStringW m_Name
Definition: CFontCache.hpp:14
#define KEY_READ
Definition: nt_native.h:1023
uint16_t * PWSTR
Definition: typedefs.h:56
int CompareNoCase(_In_z_ PCXSTR psz) const
Definition: cstringt.h:658
GLdouble n
Definition: glext.h:7729
POSITION InsertBefore(_In_ POSITION pos, INARGTYPE element)
Definition: atlcoll.h:681
WINE_DEFAULT_DEBUG_CHANNEL(fontext)
#define FONT_KEY
Definition: precomp.h:31
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
#define false
Definition: osdep.h:35
Definition: bufpool.h:45
size_t GetCount() const
Definition: atlcoll.h:537
_Must_inspect_result_ _In_ WDFDEVICE _In_ PCUNICODE_STRING KeyName
Definition: wdfdevice.h:2697
void SetFontDir(const LPCWSTR Path)
Definition: CFontCache.cpp:71
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _Out_opt_ PUSHORT _Inout_opt_ PUNICODE_STRING Value
Definition: wdfregistry.h:406
Status
Definition: gdiplustypes.h:24
CStringW m_FontFolderPath
Definition: CFontCache.hpp:30
E & GetNext(_Inout_ POSITION &pos)
Definition: atlcoll.h:561
#define FONT_HIVE
Definition: precomp.h:30
_In_ WDFCOLLECTION _In_ ULONG Index
CAtlArray< CFontInfo > m_Fonts
Definition: CFontCache.hpp:29
PCXSTR GetString() const
Definition: atlsimpstr.h:361
unsigned long DWORD
Definition: ntddk_ex.h:95
static LSTATUS(WINAPI *pRegDeleteTreeW)(HKEY
HKEY key
Definition: reg.c:42
CFontCache * g_FontCache
Definition: CFontCache.cpp:12
WCHAR Name[1]
Definition: fontpidl.hpp:17
void Read()
Definition: CFontCache.cpp:142
#define ERROR_MORE_DATA
Definition: dderror.h:13
bool IsEmpty() const
Definition: atlsimpstr.h:379
POSITION AddTail(INARGTYPE element)
Definition: atlcoll.h:624
CStringW Name(size_t Index)
Definition: CFontCache.cpp:85
PRTL_UNICODE_STRING_BUFFER Path
CStringW Filename(const FontPidlEntry *fontEntry, bool alwaysFullPath=false)
Definition: CFontCache.cpp:96
const CStringW & File()
Definition: CFontCache.cpp:30
bool m_FileRead
Definition: CFontCache.hpp:16
unsigned int UINT
Definition: ndis.h:50
#define NULL
Definition: types.h:112
size_t Size()
Definition: CFontCache.cpp:77
Definition: name.c:38
CAtlStringW CStringW
Definition: atlstr.h:130
Definition: File.h:15
const CStringW & Name() const
Definition: CFontCache.cpp:20
_Must_inspect_result_ _In_ WDFDEVICE _In_ BOOLEAN _In_opt_ PVOID _In_ LONG _In_z_ PCHAR File
Definition: wdfdevice.h:4061
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:54
CFontInfo(LPCWSTR name=L"")
Definition: CFontCache.cpp:14
Definition: path.c:41