ReactOS 0.4.16-dev-2284-g3529151
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-2021 Mark Jansen <mark.jansen@reactos.org>
6 */
7
8#include "precomp.h"
9
11
13
15 : m_Name(name)
16 , m_File(value)
17 , m_FileRead(false)
18 , m_AttrsRead(false)
19 , m_FileWriteTime({})
20 , m_dwFileAttributes(0)
21{
23}
24
26{
27 return m_Name;
28}
29
30const bool CFontInfo::Valid() const
31{
32 return !m_Name.IsEmpty();
33}
34
36{
37 if (!m_FileRead)
38 {
39 if (Valid() && m_File.IsEmpty())
40 {
41 // Read the filename stored in the registry.
42 // This can be either a filename or a full path
45 {
47 DWORD dwAllocated = MAX_PATH;
49 do
50 {
51 DWORD dwSize = dwAllocated;
52 PWSTR Buffer = Value.GetBuffer(dwSize);
53 Status = key.QueryStringValue(m_Name, Buffer, &dwSize);
54 Value.ReleaseBuffer(dwSize);
55 if (Status == ERROR_SUCCESS)
56 {
57 // Ensure we do not re-use the same string object, by passing it a PCWSTR
59 break;
60 }
61 dwAllocated += 128;
62 } while (Status == ERROR_MORE_DATA);
63 }
64 }
65 m_FileRead = true;
66 }
67 return m_File;
68}
69
71{
72 CStringW File = g_FontCache->Filename(this, true);
73
74 m_AttrsRead = true;
75
76 WIN32_FIND_DATAW findFileData;
77 HANDLE hFile = FindFirstFileW(File, &findFileData);
79 {
80
81 // File write time
83
84 // File size
85 m_FileSize.HighPart = findFileData.nFileSizeHigh;
86 m_FileSize.LowPart = findFileData.nFileSizeLow;
87
90 }
91}
92
94{
95 if (!m_AttrsRead)
96 ReadAttrs();
97
98 return m_FileSize;
99}
100
102{
103 if (!m_AttrsRead)
104 ReadAttrs();
105
106 return m_FileWriteTime;
107}
108
110{
111 if (!m_AttrsRead)
112 ReadAttrs();
113
114 return m_dwFileAttributes;
115}
116
118{
119}
120
122{
125}
126
128{
129 if (m_Fonts.GetSize() == 0)
130 Read();
131
132 return m_Fonts.GetSize();
133}
134
136{
137 if (m_Fonts.GetSize() == 0)
138 Read();
139
140 if ((INT)Index >= m_Fonts.GetSize())
141 return L"";
142
143 return m_Fonts[Index].Name();
144}
145
147{
148 if (m_Fonts.GetSize() == 0)
149 Read();
150
151 if ((INT)Index >= m_Fonts.GetSize())
152 return L"";
153
154 return m_Fonts[Index].File();
155}
156
158{
159 if (m_Fonts.GetSize() == 0)
160 Read();
161
162 for (INT i = 0; i < m_Fonts.GetSize(); ++i)
163 {
164 if (m_Fonts[i].Name().CompareNoCase(fontEntry->Name()) == 0)
165 {
166 return &m_Fonts[i];
167 }
168 }
169 return nullptr;
170}
171
173{
174 if ((INT)Index >= m_Fonts.GetSize())
175 return FALSE;
176 return m_Fonts[Index].IsMarkDeleted();
177}
178
179// The item must exist until its visibility is removed, because the change
180// notification UI must work for existing items.
182{
183 for (INT i = 0; i < m_Fonts.GetSize(); ++i)
184 {
185 if (m_Fonts[i].Name().CompareNoCase(fontEntry->Name()) == 0)
186 {
187 m_Fonts[i].MarkDeleted();
188 break;
189 }
190 }
191}
192
194{
196 if (info)
197 {
198 File = info->File();
199
200 if (!File.IsEmpty() && alwaysFullPath)
201 {
202 // Ensure this is a full path
204 {
206 }
207 }
208 }
209
210 return File;
211}
212
214{
215 CFontInfo newInfo(KeyName, Value);
216
217 CStringW strFontFile = g_FontCache->GetFontFilePath(newInfo.File());
218 if (!PathFileExistsW(strFontFile))
219 return;
220
221 POSITION it = fonts.GetHeadPosition();
222 while (it != NULL)
223 {
224 POSITION lastit = it;
225 const CFontInfo& info = fonts.GetNext(it);
226 if (info.Name().CompareNoCase(KeyName) >= 0)
227 {
228 fonts.InsertBefore(lastit, newInfo);
229 return;
230 }
231 }
232
233 fonts.AddTail(newInfo);
234}
235
237{
239 return m_FontFolderPath + Path;
240 return Path;
241}
242
244{
245 CRegKey key;
247 if (error != ERROR_SUCCESS)
248 {
249 ERR("Can't open registry: %ld\n", error);
250 return;
251 }
252
254 DWORD cchName = MAX_PATH, cchValue = MAX_PATH;
255 if (!Name.Allocate(cchName) || !Value.Allocate(cchValue))
256 {
257 ERR("Out of memory\n");
258 return;
259 }
260
261 // Enumerate all registered font names
263 for (DWORD iItem = 0;; ++iItem)
264 {
265 DWORD cchName2 = cchName, cbValue = cchValue * sizeof(WCHAR);
266 error = RegEnumValueW(key, iItem, Name, &cchName2, NULL, NULL,
267 (PBYTE)(PWSTR)Value, &cbValue);
268 if (error == ERROR_MORE_DATA)
269 {
270 cchName += 128;
271 cchValue += 128;
272 if (!Name.Reallocate(cchName) || !Value.Reallocate(cchValue))
273 {
274 ERR("Out of memory\n");
275 break;
276 }
277 --iItem;
278 continue;
279 }
280
281 if (error != ERROR_SUCCESS)
282 break;
283
284 if (Name[0] && Value[0])
285 {
286 // Insert will create an ordered list (sorted)
287 Insert(fonts, (PCWSTR)Name, (PCWSTR)Value);
288 }
289 }
290
291 // Move the fonts from a list to an array (for easy indexing)
292 m_Fonts.RemoveAll();
293 POSITION it = fonts.GetHeadPosition();
294 while (it != NULL)
295 m_Fonts.Add(fonts.GetNext(it));
296}
CFontCache * g_FontCache
Definition: CFontCache.cpp:12
CFontCache * g_FontCache
Definition: CFontCache.cpp:12
PRTL_UNICODE_STRING_BUFFER Path
#define WINE_DEFAULT_DEBUG_CHANNEL(t)
Definition: precomp.h:23
#define ERR(fmt,...)
Definition: precomp.h:57
POSITION AddTail(INARGTYPE element)
Definition: atlcoll.h:629
POSITION GetHeadPosition() const
Definition: atlcoll.h:554
POSITION InsertBefore(_In_ POSITION pos, INARGTYPE element)
Definition: atlcoll.h:704
E & GetNext(_Inout_ POSITION &pos)
Definition: atlcoll.h:566
bool IsEmpty() const noexcept
Definition: atlsimpstr.h:394
PXSTR GetString() noexcept
Definition: atlsimpstr.h:367
Definition: bufpool.h:45
CStringW m_FontFolderPath
Definition: CFontCache.hpp:43
CStringW Filename(CFontInfo *info, bool alwaysFullPath=false)
Definition: CFontCache.cpp:193
CStringW File(size_t Index)
Definition: CFontCache.cpp:146
void MarkDeleted(const FontPidlEntry *fontEntry)
Definition: CFontCache.cpp:181
CStringW GetFontFilePath(const PCWSTR Path) const
Definition: CFontCache.cpp:236
BOOL IsMarkDeleted(size_t Index) const
Definition: CFontCache.cpp:172
CFontInfo * Find(const FontPidlEntry *fontEntry)
Definition: CFontCache.cpp:157
CStringW Name(size_t Index)
Definition: CFontCache.cpp:135
CSimpleArray< CFontInfo > m_Fonts
Definition: CFontCache.hpp:42
void Read()
Definition: CFontCache.cpp:243
size_t Size()
Definition: CFontCache.cpp:127
void Insert(CAtlList< CFontInfo > &fonts, const CStringW &KeyName, PCWSTR Value)
Definition: CFontCache.cpp:213
void SetFontDir(const LPCWSTR Path)
Definition: CFontCache.cpp:121
CStringW m_Name
Definition: CFontCache.hpp:13
FILETIME m_FileWriteTime
Definition: CFontCache.hpp:19
LARGE_INTEGER m_FileSize
Definition: CFontCache.hpp:18
CStringW m_File
Definition: CFontCache.hpp:14
const CStringW & Name() const
Definition: CFontCache.cpp:25
bool m_FileRead
Definition: CFontCache.hpp:16
const bool Valid() const
Definition: CFontCache.cpp:30
CFontInfo(PCWSTR name=L"", PCWSTR value=L"")
Definition: CFontCache.cpp:14
const CStringW & File()
Definition: CFontCache.cpp:35
const FILETIME & FileWriteTime()
Definition: CFontCache.cpp:101
const LARGE_INTEGER & FileSize()
Definition: CFontCache.cpp:93
DWORD m_dwFileAttributes
Definition: CFontCache.hpp:20
void ReadAttrs()
Definition: CFontCache.cpp:70
DWORD FileAttributes()
Definition: CFontCache.cpp:109
bool m_AttrsRead
Definition: CFontCache.hpp:17
Definition: File.h:16
#define ERROR_MORE_DATA
Definition: dderror.h:13
#define ERROR_SUCCESS
Definition: deptool.c:10
LPWSTR Name
Definition: desk.c:124
static LSTATUS(WINAPI *pRegDeleteTreeW)(HKEY
#define NULL
Definition: types.h:112
#define FALSE
Definition: types.h:117
#define FONT_HIVE
Definition: precomp.h:50
#define FONT_KEY
Definition: precomp.h:51
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:2830
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define MAX_PATH
Definition: compat.h:34
HANDLE WINAPI FindFirstFileW(IN LPCWSTR lpFileName, OUT LPWIN32_FIND_DATAW lpFindFileData)
Definition: find.c:320
BOOL WINAPI FindClose(HANDLE hFindFile)
Definition: find.c:502
BOOL WINAPI FileTimeToLocalFileTime(IN CONST FILETIME *lpFileTime, OUT LPFILETIME lpLocalFileTime)
Definition: time.c:221
BOOL WINAPI PathIsRelativeW(const WCHAR *path)
Definition: path.c:1030
BOOL WINAPI PathFileExistsW(const WCHAR *path)
Definition: path.c:2607
#define false
Definition: stdbool.h:25
#define L(x)
Definition: resources.c:13
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
Status
Definition: gdiplustypes.h:25
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 const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define error(str)
Definition: mkdosfs.c:1605
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:56
_In_ HANDLE hFile
Definition: mswsock.h:90
#define KEY_READ
Definition: nt_native.h:1026
BYTE * PBYTE
Definition: pedump.c:66
LPWSTR Name()
Definition: fontpidl.hpp:18
FILETIME ftLastWriteTime
Definition: minwinbase.h:286
DWORD dwFileAttributes
Definition: minwinbase.h:283
Definition: copy.c:22
Definition: name.c:39
uint16_t * PWSTR
Definition: typedefs.h:56
const uint16_t * PCWSTR
Definition: typedefs.h:57
int32_t INT
Definition: typedefs.h:58
LONGLONG QuadPart
Definition: typedefs.h:114
ULONG LowPart
Definition: typedefs.h:106
Definition: pdh_main.c:96
_In_ WDFCOLLECTION _In_ ULONG Index
_Must_inspect_result_ _In_ WDFDEVICE _In_ PCUNICODE_STRING KeyName
Definition: wdfdevice.h:2705
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _Out_opt_ PUSHORT _Inout_opt_ PUNICODE_STRING Value
Definition: wdfregistry.h:413
_In_ PSID _Out_writes_to_opt_ cchName LPSTR _Inout_ LPDWORD cchName
Definition: winbase.h:2521
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
__wchar_t WCHAR
Definition: xmlstorage.h:180