上篇文章可以把英文字体名映射到文件名,这篇文章(其实只有代码)通过DirectWrite库接着把中英文字体做个映射。
#include <iostream>
#include <dwrite.h>
#include <string.h>
#include <new>
#include <string>
#include <map>
#include <locale>
// SafeRelease inline function.
template <class T> inline void SafeRelease(T **ppT)
{
if (*ppT)
{
(*ppT)->Release();
*ppT = NULL;
}
}
std::wstring GetFontFaceNamePair(IDWriteLocalizedStrings* pFamilyNames, UINT32 index)
{
std::wstring result;
UINT32 length = 0;
HRESULT hr = pFamilyNames->GetStringLength(index, &length);
// Allocate a string big enough to hold the name.
wchar_t* name = new (std::nothrow) wchar_t[length+1];
if (name == NULL)
{
hr = E_OUTOFMEMORY;
}
// Get the family name.
if (SUCCEEDED(hr))
{
hr = pFamilyNames->GetString(index, name, length+1);
}
if (SUCCEEDED(hr))
{
result = name;
}
delete [] name;
return result;
}
std::map<std::wstring, std::wstring> FontFaceMap()
{
std::map<std::wstring, std::wstring> map;
wchar_t localeName[LOCALE_NAME_MAX_LENGTH];
// Get the default locale for this user.
int defaultLocaleSuccess = GetUserDefaultLocaleName(localeName, LOCALE_NAME_MAX_LENGTH);
if (!defaultLocaleSuccess || wcscmp(localeName, L"en-us") == 0)
return map;
IDWriteFactory* pDWriteFactory = NULL;
HRESULT hr = DWriteCreateFactory(
DWRITE_FACTORY_TYPE_SHARED,
__uuidof(IDWriteFactory),
reinterpret_cast<IUnknown**>(&pDWriteFactory)
);
IDWriteFontCollection* pFontCollection = NULL;
// 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))
{
hr = pFontCollection->GetFontFamily(i, &pFontFamily);
}
IDWriteLocalizedStrings* pFamilyNames = NULL;
// Get a list of localized strings for the family name.
if (SUCCEEDED(hr))
{
hr = pFontFamily->GetFamilyNames(&pFamilyNames);
UINT32 defaultLocaleIndex = 0;
UINT32 EnglishIndex = 0;
BOOL exists1 = false, exists2 = false;
if (SUCCEEDED(hr))
{
HRESULT hr1, hr2;
hr1 = pFamilyNames->FindLocaleName(localeName, &defaultLocaleIndex, &exists1);
hr2 = pFamilyNames->FindLocaleName(L"en-us", &EnglishIndex, &exists2);
if (SUCCEEDED(hr1) && exists1 && SUCCEEDED(hr2) && exists2 && defaultLocaleIndex != EnglishIndex)
{
auto defaultLocaleName = GetFontFaceNamePair(pFamilyNames, defaultLocaleIndex);
auto EnglishName = GetFontFaceNamePair(pFamilyNames, EnglishIndex);
map[defaultLocaleName] = EnglishName;
std::wcout << defaultLocaleName.c_str() << "=>" << EnglishName.c_str() << std::endl;
}
SafeRelease(&pFamilyNames);
}
SafeRelease(&pFontFamily);
}
}
SafeRelease(&pFontCollection);
}
SafeRelease(&pDWriteFactory);
}
return map;
}
int main()
{
std::wcout.imbue(std::locale("zh-CN"));
FontFaceMap();
}
输出
微软雅黑=>Microsoft YaHei
Microsoft YaHei UI=>Microsoft YaHei UI
宋体=>SimSun
新宋体=>NSimSun
等线=>DengXian
仿宋=>FangSong
楷体=>KaiTi
黑体=>SimHei