MFC从字体名获取字库文件路径(从宋体获取到simsun.ttc)

2023-05-16

上一篇有讲到在QT中从字体名获取文字的路径,这个是MFC版本的:

#QT从字体名获取字库文件路径

#include <string>
using namespace std;

void wcharTochar(const wchar_t *wchar, char *chr, int length)  
{
	memset(chr,0x00,length * sizeof(char));
	WideCharToMultiByte( CP_ACP, 0, wchar, -1, chr, length, NULL, NULL );  
}

void charTowchar(const char *chr, wchar_t *wchar, int size)  
{
	memset(wchar,0x00,size * sizeof(wchar_t));
	MultiByteToWideChar( CP_ACP, 0, chr, strlen(chr)+1, wchar, size/sizeof(wchar[0]) );  
}


// Get system font file path
int GetSystemFontFile(const std::string &faceName, char *pszFontPath) {

	static const char	fontRegistryPath[] = "Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts";
	HKEY				hKey;
	LONG				result;
	std::string			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 0;
	}

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

	DWORD valueIndex = 0;
	char *valueName = new char[maxValueNameSize];
	LPBYTE valueData = new BYTE[maxValueDataSize];
	DWORD valueNameSize, valueDataSize, valueType;
	char	szFontFile[MAX_PATH] = {0x00};

	// Look for a matching font name
	do {

		memset(szFontFile, 0x00, sizeof(szFontFile));
		valueDataSize = maxValueDataSize;
		valueNameSize = maxValueNameSize;

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

		valueIndex++;

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

		std::string wsValueName(valueName, valueNameSize);

		// Found a match
		if (_tcsncicmp(wsFaceName.c_str(), wsValueName.c_str(), wsFaceName.length()) == 0) {
			wsprintf(szFontFile, "%s", valueData);
			break;
		}
	}
	while (result != ERROR_NO_MORE_ITEMS);

	delete[] valueName;
	delete[] valueData;

	RegCloseKey(hKey);

	if (strlen(szFontFile) == 0)
	{
		return 0;
	}

	// Build full font file path
	char winDir[MAX_PATH] = {0x00};
	GetWindowsDirectory(winDir, MAX_PATH);

	wsprintf(pszFontPath, "%s\\Fonts\\%s", winDir, szFontFile);
	return strlen(pszFontPath);
}

#define MAKE_TAG(ch1, ch2, ch3, ch4) (\
	(((UINT)(ch4)) << 24) | \
	(((UINT)(ch3)) << 16) | \
	(((UINT)(ch2)) << 8) | \
	((UINT)(ch1)) \
	)

static inline USHORT getUShort(const unsigned char *p)
{
	USHORT val;
	val = *p++ << 8;
	val |= *p;

	return val;
}

static char * getEnglishName(const UCHAR *table, UINT bytes)
{
	static char	szEnName[MAX_PATH] = {0x00};
	static wchar_t i18n_name[MAX_PATH] = {0x00};
	enum {
		NameRecordSize = 12,
		FamilyId = 1,
		MS_LangIdEnglish = 0x009
	};

	// get the name table
	USHORT count;
	USHORT string_offset;
	const unsigned char *names;

	int microsoft_id = -1;
	int apple_id = -1;
	int unicode_id = -1;

	if(getUShort(table) != 0)
		goto error;

	count = getUShort(table+2);
	string_offset = getUShort(table+4);
	names = table + 6;

	if(string_offset >= bytes || 6 + count*NameRecordSize > string_offset)
		goto error;

	for(int i = 0; i < count; ++i) {
		// search for the correct name entry

		USHORT platform_id = getUShort(names + i*NameRecordSize);
		USHORT encoding_id = getUShort(names + 2 + i*NameRecordSize);
		USHORT language_id = getUShort(names + 4 + i*NameRecordSize);
		USHORT name_id = getUShort(names + 6 + i*NameRecordSize);

		if(name_id != FamilyId)
			continue;

		enum {
			PlatformId_Unicode = 0,
			PlatformId_Apple = 1,
			PlatformId_Microsoft = 3
		};

		USHORT length = getUShort(names + 8 + i*NameRecordSize);
		USHORT offset = getUShort(names + 10 + i*NameRecordSize);
		if(DWORD(string_offset + offset + length) >= bytes)
			continue;

		if ((platform_id == PlatformId_Microsoft
			&& (encoding_id == 0 || encoding_id == 1))
			&& (language_id & 0x3ff) == MS_LangIdEnglish
			&& microsoft_id == -1)
			microsoft_id = i;
		// not sure if encoding id 4 for Unicode is utf16 or ucs4...
		else if(platform_id == PlatformId_Unicode && encoding_id < 4 && unicode_id == -1)
			unicode_id = i;
		else if(platform_id == PlatformId_Apple && encoding_id == 0 && language_id == 0)
			apple_id = i;
	}
	{
		bool unicode = false;
		int id = -1;
		if(microsoft_id != -1) {
			id = microsoft_id;
			unicode = true;
		} else if(apple_id != -1) {
			id = apple_id;
			unicode = false;
		} else if (unicode_id != -1) {
			id = unicode_id;
			unicode = true;
		}
		if(id != -1) {
			USHORT length = getUShort(names + 8 + id*NameRecordSize);
			USHORT offset = getUShort(names + 10 + id*NameRecordSize);
			if(unicode) {
				// utf16

				length /= 2;
				//i18n_name.resize(length);
				wchar_t *uc = (wchar_t *) i18n_name;
				const unsigned char *string = table + string_offset + offset;
				for(int i = 0; i < length; ++i)
					uc[i] = getUShort(string + 2*i);
				memset(szEnName, 0x00, sizeof(szEnName));
				wcharTochar(i18n_name, szEnName, MAX_PATH);
			} else {
				// Apple Roman
				char *uc = (char *) i18n_name;
				const unsigned char *string = table + string_offset + offset;
				for(int i = 0; i < length; ++i)
					uc[i] = string[i];
				memset(szEnName, 0x00, sizeof(szEnName));
				memcpy(szEnName, uc, strlen(uc));
			}
		}
	}
error:
	//qDebug("got i18n name of '%s' for font '%s'", i18n_name.latin1(), familyName.toLocal8Bit().data());
	return szEnName;
}

char * getEnglishName(const char *familyName)
{
	char *i18n_name = 0x00;

	HDC hdc = GetDC( 0 );
	LOGFONT lf;
	memset(&lf, 0, sizeof(LOGFONT));
	memcpy(lf.lfFaceName, familyName, strlen(familyName));
	lf.lfCharSet = DEFAULT_CHARSET;
	HFONT hfont = CreateFontIndirect(&lf);

	if(!hfont) {
		ReleaseDC(0, hdc);
		return i18n_name;
	}

	HGDIOBJ oldobj = SelectObject( hdc, hfont );

	const DWORD name_tag = MAKE_TAG( 'n', 'a', 'm', 'e' );

	// get the name table
	unsigned char *table = 0;

	DWORD bytes = GetFontData( hdc, name_tag, 0, 0, 0 );
	if ( bytes == GDI_ERROR ) {
		// ### Unused variable
		/* int err = GetLastError(); */
		goto error;
	}

	table = new unsigned char[bytes];
	GetFontData(hdc, name_tag, 0, table, bytes);
	if ( bytes == GDI_ERROR )
		goto error;

	i18n_name = getEnglishName(table, bytes);
error:
	delete [] table;
	SelectObject( hdc, oldobj );
	DeleteObject( hfont );
	ReleaseDC( 0, hdc );

	//qDebug("got i18n name of '%s' for font '%s'", i18n_name.latin1(), familyName.toLocal8Bit().data());
	return i18n_name;
}

遍历系统所有可用字体:

BOOL CALLBACK EnumFontFamEx(LPLOGFONT lplf,LPTEXTMETRIC lptm,int iType,LPARAM lpData)
{
	// m_cList.AddString(lplf->lfFaceName);
	return 1;
}

void main()
{
    // 遍历系统所有字体
	HDC     hdc = ::GetDC(NULL);
	LOGFONT lf;

	memset(&lf,0,sizeof(LOGFONT));
	lf.lfCharSet = DEFAULT_CHARSET;
	lf.lfFaceName[0] = NULL;

	m_cList.ResetContent();
	::EnumFontFamiliesEx(hdc,&lf,(FONTENUMPROC)EnumFontFamEx,0,0);

	::ReleaseDC(NULL,hdc);
}

根据faceName来获取字体ttc文件名:

void CEnumFontDlg::OnDblclkList1()
{
	// TODO: 在此添加控件通知处理程序代码
	char	szName[MAX_PATH] = {0x00};
	int		nSel = CB_ERR;

	char	szPath[MAX_PATH] = {0x00};
	char	*pPath = NULL;

	nSel = m_cList.GetCurSel();
	if(nSel == CB_ERR)
		return;

    // 获取到ListBox中双击选中的字体
    // 例如:宋体、微软雅黑
	m_cList.GetText(nSel, szName);

	pPath = getEnglishName(szName);
	if(pPath)
	{
		GetSystemFontFile(pPath, szPath);
		AfxMessageBox(szPath);
	}
}

运行效果如下:

 

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

MFC从字体名获取字库文件路径(从宋体获取到simsun.ttc) 的相关文章

随机推荐

  • 问题——STM32用flyMcu烧录“写入出错在0kb,进度0%,耗时1029毫秒”的错误

    目录 1 原因 2 现象 3 解决办法 1 原因 因勾选 编程到FLASH时写选项字节 出错 2 现象 xff08 1 xff09 flyMcu烧录 xff08 2 xff09 用JTAG烧录提示 xff1a Flash Download
  • ubuntn14.04 版本内核更换详细步骤

    Ubuntu14 04 版本 xff0c 内核为 3 13 XX 更换为 3 2 66 的方法 首先 xff0c 3 2 66第二个数字为偶数 xff0c 就是稳定版 xff08 现在是不是不拿这个区分了 xff1f 没搞清 xff0c 但
  • 设计模式_spring框架中常用的8种设计模式

    spring框架中常用到的8种设计模式清单如下 xff1a 设计模式 使用地方 备注 工厂模式 BeanFactory ApplicationContext 单例模式 Spring中的Bean 代理模式 Spring AOP java反射实
  • eclipse 部署与开发 cas 6.3

    安装前 xff0c 需要准备eclipse xff0c tomcat9 xff0c jdk11安装 eclipse安装gradle插件 打开eclipse xff0c 选择help eclipse market place 搜索builds
  • 2011研发工作总结

    转眼这一年又将过去 xff0c 伴随着公司的成立 xff0c 我们辛苦而又忙碌的从年头走到了年尾 以下是总结下这一年中工作的情况 xff0c 谈谈经验感想 xff0c 说说得失 公司成立 2011年刚开始 xff0c 公司就成立了 我们在喜
  • 论文笔记——EXPLAINING AND HARNESSING ADVERSARIAL EXAMPLES(解释和利用对抗样本)

    本文参考了三篇笔记 xff0c 帮助很大 xff1a Explaining and Harnessing Adversarial Examples 阅读笔记 论文笔记 Explaining amp Harnessing Adversaria
  • C++验证哥德巴赫猜想

    include lt iostream gt using namespace std bool Prime int i 判断是否为质数的函数 int j if i lt 61 1 return 0 如果小于等于1返回0 if i 61 61
  • 能ping通Linux&nbsp;但是ssh连不上问题…

    问题 xff1a 能ping通Linux服务器 但是ssh连不上 lt Linux redhat AS4 版本 gt 解决方法这个问题花了我20分钟去查资料 xff0c 网上写的解决方法也是五花八门 xff0c 不过 xff0c 总算解决了
  • RuntimeError: NCCL error in:torch/lib/c10d/ProcessGroupNCCL.cpp:514, invalid usage, NCCL version 踩坑

    运行pytorch分布式任务时 xff0c 可能在执行dist barrier 这条代码时会触发NCCL错误 xff1a RuntimeError NCCL error in torch lib c10d ProcessGroupNCCL
  • 有一种服务称为MaaS

    Metal as a Service 有一种服务称为MaaS xff08 开源 xff09 xff1a 云管理平台中必不可少的服务 为服务对象提供裸机 xff08 1或n xff09 xff0c 服务对象可以用来安装云OS和其他云功能 使用
  • UDP网络编程服务器与客户端代码

    一 xff0c 通信模型 服务器 xff1a 1 创建socket xff0c 使用socket函数 2 准备通信地址 xff0c 使用结构体类型 3 绑定socket和通信地址 xff0c 使用bind函数 4 监听 xff0c 使用li
  • SAMBA共享工具安装

    1 xff0c 环境win7 b4位 xff0c ubuntu16 04 xff0c 确定ubuntu已连接到互联网 xff0c 执行如下命令下载 Samba 工具 xff1a sudo apt get install samba samb
  • K8s ❉ node节点未找到cni0/flannel.1网络

    问题描述 xff1a 现有测试环境有3台k8s服务器 xff0c 现在新添加两台服务器 xff0c 发现新加的服务器flannel 1和cni0网卡没有生成 root 64 slave1 ifconfig docker0 flags 61
  • 学习一下freertos streambuffer

    学习一下freertos stream buffer 最近在自己从0到1写rtos xff0c 主要是为了学习rtos xff0c 并没有其他卵用 xff0c 光学会调API是不够的 xff0c 看源码又恼火 xff0c 所以学习的最好办法
  • Writable接口

    在org apache hadoop io中 xff0c hadoop使用自己的序列化格式Writables接口 xff0c 具有紧凑 xff0c 快速的特点 xff0c 不过目前只有用Java才能较容易扩展 在hadoop中 xff0c
  • 解决数据库连接慢问题

    解决数据库连接慢问题方法 xff1a 方法一 连接数据库时 xff0c 使用localhost xff0c 不要使用明确的IP地址 xff08 远程数据库除外 xff09 xff1b 方法二 在my cnf配置文件里 xff0c 添加ski
  • 难忘的2011

    前言 xff1a 这些年 xff0c 每到年终之时 xff0c 我都会写个总结 2009年的 优化生活 规划未来 记录了一位初级网络优化工程师09年的所想与所做以及对10年的规划与展望 xff1b 2010年的 今天的2010 xff0c
  • QT从字体名获取字库文件路径(从宋体获取到simsun.ttc)

    MFC版本的从字体名获取字库文件路径点击此处 最近弄的一份项目需要裁剪ttf字库 xff0c 故需要用到此功能 xff01 用了几年QT xff0c 个人感觉类库是非常强大的 xff01 QT相比VS的话亮点在开源的类库非常的丰富 xff0
  • C++编写分形问题

    目的 xff1a 递归是基本的算法思想和设计方法之一 xff0c 也是数据结构重点讲授的部分 xff0c 是许多算法的基础 xff0c 对它们的理解和运用直接关系着其他算法的理解和应用 因此 xff0c 熟练掌握递归是十分重要的 通过本题
  • MFC从字体名获取字库文件路径(从宋体获取到simsun.ttc)

    上一篇有讲到在QT中从字体名获取文字的路径 xff0c 这个是MFC版本的 xff1a QT从字体名获取字库文件路径 include lt string gt using namespace std void wcharTochar con