根据字体的中文名 获取 字体的路径 和 英文名

声明:此方法只适用于windows系统

利用freetype的时候,需要传入字体的路径,找了半天找不到一个合适的方法,经指导研究,找到一种合适的方法,

贴出来共享。

#include<dwrite.h>
#pragma once dwrite.lib

void GetSystemFont(){
	std::map<std::wstring, std::wstring> m_systemFonts  ;
	m_systemFonts.clear();

	IDWriteFontCollection* pFontCollection = NULL;

	// Get the system font collection.
	HRESULT hr = S_OK;
	IDWriteFactory* pDWriteFactory = NULL;

	if (SUCCEEDED(hr))
	{
		hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED,
			__uuidof(IDWriteFactory),
			reinterpret_cast<IUnknown**>(&pDWriteFactory));
	}

	// Get the system font collection.
	if (SUCCEEDED(hr))
	{
		hr = pDWriteFactory->GetSystemFontCollection(&pFontCollection);
	}
	UINT32 familyCount = 0;
	// Get the number of font families in the collection.
	if (SUCCEEDED(hr))
	{
		familyCount = pFontCollection->GetFontFamilyCount();
	}

	for (UINT32 i = 0; i < familyCount; ++i) {
		IDWriteFontFamily* pFontFamily = NULL;
		// Get the font family.
		if (SUCCEEDED(hr))
		{
			IDWriteLocalizedStrings* pFamilyNames = NULL;
			hr = pFontCollection->GetFontFamily(i, &pFontFamily);
			// Get a list of localized strings for the family name.
			if (SUCCEEDED(hr))
			{
				hr = pFontFamily->GetFamilyNames(&pFamilyNames);
			}
			
			UINT32 index = 0;
			BOOL exists = false;
			if (!exists)
				index = 0;

			UINT32 length = 0;
			// Get the string length.
			if (SUCCEEDED(hr))
			{
				hr = pFamilyNames->GetStringLength(index, &length);
			}

			// Allocate a string big enough to hold the name.
			wchar_t* name = new wchar_t[length + 1];
			std::wstring sname;
			std::wstring sname_unicode;
			std::wstring fpath;
			// Get the family name.
			if (SUCCEEDED(hr))
			{
				hr = pFamilyNames->GetString(index, name, length + 1);
				//sname 为英文名
				sname = std::wstring(name);
				 fpath = GetSystemFontFile(sname);
				if (fpath.empty()) {
					continue;
				}
			}
			
			wchar_t localeName[LOCALE_NAME_MAX_LENGTH];
			if (SUCCEEDED(hr))
			{
				// Get the default locale for this user.
				int defaultLocaleSuccess = GetUserDefaultLocaleName(localeName, LOCALE_NAME_MAX_LENGTH);
				//If the default locale is returned, find that locale name, otherwise use "en-us".
				if (defaultLocaleSuccess)
				{
					hr = pFamilyNames->FindLocaleName(localeName, &index, &exists);
				}
				if (SUCCEEDED(hr) && !exists) // if the above find did not find a match, retry with US English
				{
					hr = pFamilyNames->FindLocaleName(L"en-us", &index, &exists);
				}
			}
			// If the specified locale doesn't exist, select the first on the list.
			if (!exists)
				index = 0;

			length = 0;
			// Get the string length.
			if (SUCCEEDED(hr))
			{
				hr = pFamilyNames->GetStringLength(index, &length);
			}

			// Get the family name.
			if (SUCCEEDED(hr))
			{
				hr = pFamilyNames->GetString(index, name, length + 1);
				//sname_unicode为对应的中文名
				sname_unicode = std::wstring(name);
				m_systemFonts[sname_unicode] = fpath;
				
			}
			delete[]name;


			if (pFontFamily)
			{
				pFontFamily->Release();
				pFontFamily = NULL;
			}
			if (pFamilyNames)
			{
				pFamilyNames->Release();
				pFamilyNames = NULL;
			}
		}
	}

	if (pFontCollection)
	{
		pFontCollection->Release();
		pFontCollection = NULL;
	}
	if (pDWriteFactory)
	{
		pDWriteFactory->Release();
		pDWriteFactory = NULL;
	}
	}

}
std::wstring GetSystemFontFile(const std::wstring &faceName) {

	std::wstring wsFontFile;

	static const LPWSTR fontRegistryPath = L"Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts";
	HKEY hKey;
	LONG result;
	std::wstring wsFaceName(faceName.begin(), faceName.end());

	// Open Windows font registry key
	result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, fontRegistryPath, 0, KEY_READ, &hKey);
	if (result != ERROR_SUCCESS) {
		return L"";
	}

	DWORD maxValueNameSize, maxValueDataSize;
	result = RegQueryInfoKey(hKey, 0, 0, 0, 0, 0, 0, 0, &maxValueNameSize, &maxValueDataSize, 0, 0);
	if (result != ERROR_SUCCESS) {
		return L"";
	}

	DWORD valueIndex = 0;
	LPWSTR valueName = new WCHAR[maxValueNameSize];
	LPBYTE valueData = new BYTE[maxValueDataSize];
	DWORD valueNameSize, valueDataSize, valueType;

	// Look for a matching font name
	do {

		wsFontFile.clear();
		valueDataSize = maxValueDataSize;
		valueNameSize = maxValueNameSize;

		result = RegEnumValue(hKey, valueIndex, valueName, &valueNameSize, 0, &valueType, valueData, &valueDataSize);

		valueIndex++;

		if (result != ERROR_SUCCESS || valueType != REG_SZ) {
			continue;
		}

		std::wstring wsValueName(valueName, valueNameSize);

		// Found a match
		if (_wcsnicmp(wsFaceName.c_str(), wsValueName.c_str(), wsFaceName.length()) == 0) {

			wsFontFile.assign((LPWSTR)valueData, valueDataSize);
			break;
		}
	} while (result != ERROR_NO_MORE_ITEMS);

	delete[] valueName;
	delete[] valueData;

	RegCloseKey(hKey);

	if (wsFontFile.empty()) {
		return L"";
	}

	// Build full font file path
	WCHAR winDir[MAX_PATH];
	GetWindowsDirectory(winDir, MAX_PATH);

	std::wstringstream ss;
	ss << winDir << "\\Fonts\\" << wsFontFile;
	wsFontFile = ss.str();
	
	return wsFontFile;
}


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值