ReactOS 0.4.16-dev-336-gb667d82
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_FileRead(false)
17 , m_AttrsRead(false)
18 , m_FileWriteTime({})
19 , m_dwFileAttributes(0)
20{
22}
23
25{
26 return m_Name;
27}
28
29const 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
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
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
70{
71 CStringW File = g_FontCache->Filename(this, true);
72
73 m_AttrsRead = true;
74
75 WIN32_FIND_DATAW findFileData;
76 HANDLE hFile = FindFirstFileW(File, &findFileData);
78 {
79
80 // File write time
81 FileTimeToLocalFileTime(&findFileData.ftLastWriteTime, &m_FileWriteTime);
82
83 // File size
84 m_FileSize.HighPart = findFileData.nFileSizeHigh;
85 m_FileSize.LowPart = findFileData.nFileSizeLow;
86
87 m_dwFileAttributes = findFileData.dwFileAttributes;
89 }
90}
91
93{
94 if (!m_AttrsRead)
95 ReadAttrs();
96
97 return m_FileSize;
98}
99
101{
102 if (!m_AttrsRead)
103 ReadAttrs();
104
105 return m_FileWriteTime;
106}
107
109{
110 if (!m_AttrsRead)
111 ReadAttrs();
112
113 return m_dwFileAttributes;
114}
115
117{
118}
119
121{
124}
125
127{
128 if (m_Fonts.GetCount() == 0u)
129 Read();
130
131 return m_Fonts.GetCount();
132}
133
135{
136 if (m_Fonts.GetCount() == 0u)
137 Read();
138
139 if (Index >= m_Fonts.GetCount())
140 return CStringW();
141
142 return m_Fonts[Index].Name();
143}
144
146{
147 if (fontEntry->Index < m_Fonts.GetCount())
148 {
149 if (m_Fonts[fontEntry->Index].Name().CompareNoCase(fontEntry->Name) == 0)
150 return &m_Fonts[fontEntry->Index];
151 }
152
153 for (UINT n = 0; n < Size(); ++n)
154 {
155 if (m_Fonts[n].Name().CompareNoCase(fontEntry->Name) == 0)
156 {
157 return &m_Fonts[n];
158 }
159 }
160 return nullptr;
161}
162
163
165{
167 if (info)
168 {
169 File = info->File();
170
171 if (!File.IsEmpty() && alwaysFullPath)
172 {
173 // Ensure this is a full path
175 {
177 }
178 }
179 }
180
181 return File;
182}
183
185{
186 POSITION it = fonts.GetHeadPosition();
187 while (it != NULL)
188 {
189 POSITION lastit = it;
190 const CFontInfo& info = fonts.GetNext(it);
191 if (info.Name().CompareNoCase(KeyName) >= 0)
192 {
193 fonts.InsertBefore(lastit, CFontInfo(KeyName));
194 return;
195 }
196 }
197 fonts.AddTail(CFontInfo(KeyName));
198}
199
201{
203 CRegKey key;
204
205 // Enumerate all registered font names
207 {
209 DWORD dwAllocated = 128;
210 DWORD ilIndex = 0;
212 do
213 {
214 DWORD dwSize = dwAllocated;
215 PWSTR Buffer = KeyName.GetBuffer(dwSize);
216 Status = RegEnumValueW(key.m_hKey, ilIndex, Buffer, &dwSize, NULL, NULL, NULL, NULL);
217 KeyName.ReleaseBuffer(dwSize);
218 if (Status == ERROR_SUCCESS)
219 {
220 // Insert will create an ordered list
221 Insert(fonts, KeyName);
222 ilIndex++;
223 continue;
224 }
226 break;
227 else if (Status == ERROR_MORE_DATA)
228 {
229 dwAllocated += 128;
230 }
231 } while (Status == ERROR_MORE_DATA || Status == ERROR_SUCCESS);
232 }
233
234 // Move the fonts from a list to an array (for easy indexing)
235 m_Fonts.SetCount(fonts.GetCount());
236 size_t Index = 0;
237 POSITION it = fonts.GetHeadPosition();
238 while (it != NULL)
239 {
240 m_Fonts[Index] = fonts.GetNext(it);
241 Index++;
242 }
243}
244
CFontCache * g_FontCache
Definition: CFontCache.cpp:12
CFontCache * g_FontCache
Definition: CFontCache.cpp:12
PRTL_UNICODE_STRING_BUFFER Path
struct NameRec_ * Name
Definition: cdprocs.h:460
#define WINE_DEFAULT_DEBUG_CHANNEL(t)
Definition: precomp.h:23
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
size_t GetCount() const
Definition: atlcoll.h:542
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:42
CStringW Filename(CFontInfo *info, bool alwaysFullPath=false)
Definition: CFontCache.cpp:164
CFontInfo * Find(const FontPidlEntry *fontEntry)
Definition: CFontCache.cpp:145
CStringW Name(size_t Index)
Definition: CFontCache.cpp:134
void Read()
Definition: CFontCache.cpp:200
CAtlArray< CFontInfo > m_Fonts
Definition: CFontCache.hpp:41
void Insert(CAtlList< CFontInfo > &fonts, const CStringW &KeyName)
Definition: CFontCache.cpp:184
size_t Size()
Definition: CFontCache.cpp:126
void SetFontDir(const LPCWSTR Path)
Definition: CFontCache.cpp:120
CStringW m_Name
Definition: CFontCache.hpp:14
FILETIME m_FileWriteTime
Definition: CFontCache.hpp:20
LARGE_INTEGER m_FileSize
Definition: CFontCache.hpp:19
CStringW m_File
Definition: CFontCache.hpp:15
const CStringW & Name() const
Definition: CFontCache.cpp:24
bool m_FileRead
Definition: CFontCache.hpp:16
const bool Valid() const
Definition: CFontCache.cpp:29
CFontInfo(LPCWSTR name=L"")
Definition: CFontCache.cpp:14
const CStringW & File()
Definition: CFontCache.cpp:34
const FILETIME & FileWriteTime()
Definition: CFontCache.cpp:100
const LARGE_INTEGER & FileSize()
Definition: CFontCache.cpp:92
DWORD m_dwFileAttributes
Definition: CFontCache.hpp:21
void ReadAttrs()
Definition: CFontCache.cpp:69
DWORD FileAttributes()
Definition: CFontCache.cpp:108
bool m_AttrsRead
Definition: CFontCache.hpp:18
Definition: File.h:16
#define ERROR_MORE_DATA
Definition: dderror.h:13
#define ERROR_SUCCESS
Definition: deptool.c:10
static LSTATUS(WINAPI *pRegDeleteTreeW)(HKEY
#define NULL
Definition: types.h:112
#define FONT_HIVE
Definition: precomp.h:30
#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:2830
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define ERROR_NO_MORE_ITEMS
Definition: compat.h:105
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(LPCWSTR lpszPath)
Definition: path.c:1579
unsigned long DWORD
Definition: ntddk_ex.h:95
Status
Definition: gdiplustypes.h:25
GLdouble n
Definition: glext.h:7729
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
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:56
_In_ HANDLE hFile
Definition: mswsock.h:90
CAtlStringW CStringW
Definition: atlstr.h:130
unsigned int UINT
Definition: ndis.h:50
#define KEY_READ
Definition: nt_native.h:1023
#define false
Definition: stdbool.h:37
WCHAR Name[1]
Definition: fontpidl.hpp:17
Definition: copy.c:22
Definition: name.c:39
uint16_t * PWSTR
Definition: typedefs.h:56
LONGLONG QuadPart
Definition: typedefs.h:114
ULONG LowPart
Definition: typedefs.h:106
_In_ WDFCOLLECTION _In_ ULONG Index
_Must_inspect_result_ _In_ WDFDEVICE _In_ PCUNICODE_STRING KeyName
Definition: wdfdevice.h:2699
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _Out_opt_ PUSHORT _Inout_opt_ PUNICODE_STRING Value
Definition: wdfregistry.h:413
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185