中英文字体名映射

上篇文章可以把英文字体名映射到文件名,这篇文章(其实只有代码)通过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
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值