Skip to content

Commit 457642c

Browse files
authored
[FONTEXT] Refactor (reactos#7821)
Improve code flexibility. JIRA issue: CORE-12861 - Delete excessive virtual keywords and add overload keywords. - Add InstallFontFiles helper function. - Add strMsg parameter to DoInstallFontFile function. - Move DoInstallFontFile and DoGetFontTitle functions outside the class. - Delete needless fontext.h.
1 parent be7d797 commit 457642c

File tree

12 files changed

+197
-228
lines changed

12 files changed

+197
-228
lines changed

dll/shellext/fontext/CDataObject.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
WINE_DEFAULT_DEBUG_CHANNEL(fontext);
1111

12-
1312
#if 0
1413
static inline void DumpDataObjectFormats(IDataObject* pObject)
1514
{
@@ -34,7 +33,6 @@ static inline void DumpDataObjectFormats(IDataObject* pObject)
3433
}
3534
#endif
3635

37-
3836
HRESULT _CDataObject_CreateInstance(PCIDLIST_ABSOLUTE folder, UINT cidl, PCUITEMID_CHILD_ARRAY apidl,
3937
REFIID riid, LPVOID* ppvOut)
4038
{
@@ -99,4 +97,3 @@ HRESULT _CDataObject_CreateInstance(PCIDLIST_ABSOLUTE folder, UINT cidl, PCUITEM
9997

10098
return hr;
10199
}
102-

dll/shellext/fontext/CEnumFonts.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
WINE_DEFAULT_DEBUG_CHANNEL(fontext);
1111

12-
13-
1412
class CEnumFonts :
1513
public CComObjectRootEx<CComMultiThreadModelNoCS>,
1614
public IEnumIDList
@@ -21,20 +19,20 @@ class CEnumFonts :
2119

2220
public:
2321
CEnumFonts()
24-
:m_dwFlags(0)
25-
,m_Index(0)
22+
: m_dwFlags(0)
23+
, m_Index(0)
2624
{
2725
}
2826

29-
STDMETHODIMP Initialize(CFontExt* folder, DWORD flags)
27+
HRESULT Initialize(CFontExt* folder, DWORD flags)
3028
{
3129
m_dwFlags = flags;
3230
m_Index = 0;
3331
return S_OK;
3432
}
3533

3634
// *** IEnumIDList methods ***
37-
STDMETHODIMP Next(ULONG celt, LPITEMIDLIST *rgelt, ULONG *pceltFetched)
35+
STDMETHODIMP Next(ULONG celt, LPITEMIDLIST *rgelt, ULONG *pceltFetched) override
3836
{
3937
if (!rgelt || (!pceltFetched && celt != 1))
4038
return E_POINTER;
@@ -69,22 +67,24 @@ class CEnumFonts :
6967
*pceltFetched = Fetched;
7068
return hr;
7169
}
72-
STDMETHODIMP Skip(ULONG celt)
70+
71+
STDMETHODIMP Skip(ULONG celt) override
7372
{
7473
m_Index += celt;
7574
return S_OK;
7675
}
77-
STDMETHODIMP Reset()
76+
77+
STDMETHODIMP Reset() override
7878
{
7979
m_Index = 0;
8080
return S_OK;
8181
}
82-
STDMETHODIMP Clone(IEnumIDList **ppenum)
82+
83+
STDMETHODIMP Clone(IEnumIDList **ppenum) override
8384
{
8485
return E_NOTIMPL;
8586
}
8687

87-
8888
public:
8989
DECLARE_NOT_AGGREGATABLE(CEnumFonts)
9090
DECLARE_PROTECT_FINAL_CONSTRUCT()
@@ -94,9 +94,7 @@ class CEnumFonts :
9494
END_COM_MAP()
9595
};
9696

97-
9897
HRESULT _CEnumFonts_CreateInstance(CFontExt* zip, DWORD flags, REFIID riid, LPVOID * ppvOut)
9998
{
10099
return ShellObjectCreatorInit<CEnumFonts>(zip, flags, riid, ppvOut);
101100
}
102-

dll/shellext/fontext/CFontCache.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,4 +241,3 @@ void CFontCache::Read()
241241
Index++;
242242
}
243243
}
244-

dll/shellext/fontext/CFontCache.hpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
#pragma once
99

10-
1110
class CFontInfo
1211
{
1312
private:
@@ -34,7 +33,6 @@ class CFontInfo
3433
DWORD FileAttributes();
3534
};
3635

37-
3836
class CFontCache
3937
{
4038
private:
@@ -61,7 +59,4 @@ class CFontCache
6159
friend class CFontExtModule;
6260
};
6361

64-
6562
extern CFontCache* g_FontCache;
66-
67-

dll/shellext/fontext/CFontExt.cpp

Lines changed: 8 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
* LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
44
* PURPOSE: CFontExt implementation
55
* COPYRIGHT: Copyright 2019-2021 Mark Jansen <[email protected]>
6-
* Copyright 2019 Katayama Hirofumi MZ ([email protected])
6+
* Copyright 2019-2025 Katayama Hirofumi MZ ([email protected])
77
*/
88

99
#include "precomp.h"
10-
#include "undocgdi.h" // for GetFontResourceInfoW
1110

1211
WINE_DEFAULT_DEBUG_CHANNEL(fontext);
1312

@@ -41,7 +40,6 @@ static FolderViewColumns g_ColumnDefs[] =
4140
{ IDS_COL_ATTR, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, 12, LVCFMT_RIGHT },
4241
};
4342

44-
4543
// Helper functions to translate a guid to a readable name
4644
bool GetInterfaceName(const WCHAR* InterfaceString, WCHAR* buf, size_t size)
4745
{
@@ -79,7 +77,6 @@ WCHAR* g2s(REFCLSID iid)
7977
return buf[idx];
8078
}
8179

82-
8380
CFontExt::CFontExt()
8481
{
8582
InterlockedIncrement(&g_ModuleRefCnt);
@@ -365,7 +362,6 @@ STDMETHODIMP CFontExt::GetAttributesOf(UINT cidl, PCUITEMID_CHILD_ARRAY apidl, D
365362
return S_OK;
366363
}
367364

368-
369365
STDMETHODIMP CFontExt::GetUIObjectOf(HWND hwndOwner, UINT cidl, PCUITEMID_CHILD_ARRAY apidl, REFIID riid, UINT * prgfInOut, LPVOID * ppvOut)
370366
{
371367
if (riid == IID_IContextMenu ||
@@ -456,7 +452,6 @@ STDMETHODIMP CFontExt::GetCurFolder(LPITEMIDLIST *ppidl)
456452
return E_POINTER;
457453
}
458454

459-
460455
// *** IPersistFolder methods ***
461456
STDMETHODIMP CFontExt::Initialize(LPCITEMIDLIST pidl)
462457
{
@@ -487,7 +482,6 @@ STDMETHODIMP CFontExt::Initialize(LPCITEMIDLIST pidl)
487482
return S_OK;
488483
}
489484

490-
491485
// *** IPersist methods ***
492486
STDMETHODIMP CFontExt::GetClassID(CLSID *lpClassId)
493487
{
@@ -523,7 +517,7 @@ STDMETHODIMP CFontExt::Drop(IDataObject* pDataObj, DWORD grfKeyState, POINTL pt,
523517
*pdwEffect = DROPEFFECT_NONE;
524518

525519
CDataObjectHIDA cida(pDataObj);
526-
if (!cida)
520+
if (!cida || cida->cidl <= 0)
527521
return E_UNEXPECTED;
528522

529523
PCUIDLIST_ABSOLUTE pidlParent = HIDA_GetPIDLFolder(cida);
@@ -533,71 +527,23 @@ STDMETHODIMP CFontExt::Drop(IDataObject* pDataObj, DWORD grfKeyState, POINTL pt,
533527
return E_FAIL;
534528
}
535529

536-
BOOL bOK = TRUE;
537-
CAtlArray<CStringW> FontPaths;
530+
CAtlArray<PCUIDLIST_RELATIVE> apidl;
538531
for (UINT n = 0; n < cida->cidl; ++n)
539532
{
540533
PCUIDLIST_RELATIVE pidlRelative = HIDA_GetPIDLItem(cida, n);
541534
if (!pidlRelative)
542-
continue;
543-
544-
PIDLIST_ABSOLUTE pidl = ILCombine(pidlParent, pidlRelative);
545-
if (!pidl)
546-
{
547-
ERR("ILCombine failed\n");
548-
bOK = FALSE;
549-
break;
550-
}
551-
552-
WCHAR szPath[MAX_PATH];
553-
BOOL ret = SHGetPathFromIDListW(pidl, szPath);
554-
ILFree(pidl);
555-
556-
if (!ret)
557-
{
558-
ERR("SHGetPathFromIDListW failed\n");
559-
bOK = FALSE;
560-
break;
561-
}
562-
563-
if (PathIsDirectoryW(szPath))
564-
{
565-
ERR("PathIsDirectory\n");
566-
bOK = FALSE;
567-
break;
568-
}
569-
570-
LPCWSTR pchDotExt = PathFindExtensionW(szPath);
571-
if (!IsFontDotExt(pchDotExt))
572-
{
573-
ERR("'%S' is not supported\n", pchDotExt);
574-
bOK = FALSE;
575-
break;
576-
}
535+
return E_FAIL;
577536

578-
FontPaths.Add(szPath);
537+
apidl.Add(pidlRelative);
579538
}
580539

581-
if (!bOK)
582-
return E_FAIL;
583-
584-
CRegKey keyFonts;
585-
if (keyFonts.Open(FONT_HIVE, FONT_KEY, KEY_WRITE) != ERROR_SUCCESS)
540+
CStringW strMessage;
541+
if (InstallFontFiles(strMessage, pidlParent, cida->cidl, &apidl[0]) != S_OK)
586542
{
587-
ERR("keyFonts.Open failed\n");
543+
// TODO: Show message
588544
return E_FAIL;
589545
}
590546

591-
for (size_t iItem = 0; iItem < FontPaths.GetCount(); ++iItem)
592-
{
593-
HRESULT hr = DoInstallFontFile(FontPaths[iItem], g_FontCache->FontPath(), keyFonts.m_hKey);
594-
if (FAILED_UNEXPECTEDLY(hr))
595-
{
596-
bOK = FALSE;
597-
break;
598-
}
599-
}
600-
601547
// Invalidate our cache
602548
g_FontCache->Read();
603549

@@ -609,79 +555,5 @@ STDMETHODIMP CFontExt::Drop(IDataObject* pDataObj, DWORD grfKeyState, POINTL pt,
609555

610556
// TODO: Show message
611557

612-
return bOK ? S_OK : E_FAIL;
613-
}
614-
615-
HRESULT CFontExt::DoInstallFontFile(LPCWSTR pszFontPath, LPCWSTR pszFontsDir, HKEY hkeyFonts)
616-
{
617-
WCHAR szDestFile[MAX_PATH];
618-
619-
// Add this font to the font list, so we can query the name
620-
if (!AddFontResourceW(pszFontPath))
621-
{
622-
ERR("AddFontResourceW('%S') failed\n", pszFontPath);
623-
DeleteFileW(szDestFile);
624-
return E_FAIL;
625-
}
626-
627-
CStringW strFontName;
628-
HRESULT hr = DoGetFontTitle(pszFontPath, strFontName);
629-
630-
// We got the name, remove it again
631-
RemoveFontResourceW(pszFontPath);
632-
633-
if (!SUCCEEDED(hr))
634-
{
635-
ERR("DoGetFontTitle failed (err=0x%x)!\n", hr);
636-
return hr;
637-
}
638-
639-
StringCchCopyW(szDestFile, sizeof(szDestFile), pszFontsDir);
640-
641-
LPCWSTR pszFileTitle = PathFindFileName(pszFontPath);
642-
PathAppendW(szDestFile, pszFileTitle);
643-
if (!CopyFileW(pszFontPath, szDestFile, FALSE))
644-
{
645-
ERR("CopyFileW('%S', '%S') failed\n", pszFontPath, szDestFile);
646-
return E_FAIL;
647-
}
648-
649-
DWORD cbData = (wcslen(pszFileTitle) + 1) * sizeof(WCHAR);
650-
LONG nError = RegSetValueExW(hkeyFonts, strFontName, 0, REG_SZ,
651-
(const BYTE *)pszFileTitle, cbData);
652-
if (nError)
653-
{
654-
ERR("RegSetValueExW failed with %ld\n", nError);
655-
DeleteFileW(szDestFile);
656-
return E_FAIL;
657-
}
658-
659-
AddFontResourceW(szDestFile);
660-
661558
return S_OK;
662559
}
663-
664-
HRESULT
665-
CFontExt::DoGetFontTitle(IN LPCWSTR pszFontPath, OUT CStringW& strFontName)
666-
{
667-
DWORD cbInfo = 0;
668-
BOOL ret = GetFontResourceInfoW(pszFontPath, &cbInfo, NULL, 1);
669-
if (!ret || !cbInfo)
670-
{
671-
ERR("GetFontResourceInfoW failed (err: %u)\n", GetLastError());
672-
return E_FAIL;
673-
}
674-
675-
LPWSTR pszBuffer = strFontName.GetBuffer(cbInfo / sizeof(WCHAR));
676-
ret = GetFontResourceInfoW(pszFontPath, &cbInfo, pszBuffer, 1);
677-
DWORD dwErr = GetLastError();;
678-
strFontName.ReleaseBuffer();
679-
if (ret)
680-
{
681-
TRACE("pszFontName: %S\n", (LPCWSTR)strFontName);
682-
return S_OK;
683-
}
684-
685-
ERR("GetFontResourceInfoW failed (err: %u)\n", dwErr);
686-
return E_FAIL;
687-
}

0 commit comments

Comments
 (0)