没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
翻译|其它|编辑:郝浩|2006-02-21 16:46:00.000|阅读 1616 次
概述:
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
TrueType字体在Windows平台下的应用很多,但是涉及到具体的操作层面上中文资料还是很少,遇到了不少问题苦恼了一阵子。
1、通过 CFontDialog 进行字体选择,但是正常情况下得到的字体列表示当前系统中所有支持的字体,当然也包括其它一些非 TrueType 字体,要在
CFontDialog 的列表中剔出非 TrueType 的字体很简单,只需在配置 CFontDialog 时如下设置:
CFontDialog dlg;
dlg.m_cf.Flags |= CF_TTONLY; //only enum TrueType
2、要遍历所选择的字体中所包含的所有字符,这个我们不禁想到可以通过 .ttf文件的解析来完成,但是如何得到选择的字体所对应的 .ttf文件哪?同实际的操作,得知在CfontDialog中显示的字体的名称和所对应的.ttf文件不是一一对应的关系,例如:选择的字体为
Arial 那么在 System/Fonts/下却没有 Arial.ttf 的文件,在网上找了很多例子都不能百分百无误的实现,在 codeproject
中有一个例子 Sample 通过注册表的方法来查找,但是同样存在上面的问题。而且即使能够找到相对应的.ttf文件,要解析这个文件也很有困难,因为存在 ttc的问题(即:一个是一个ttf的集合,一个文件里面有可能定义几种
TrueType 字体),这样还要根据选择的字体然后在读取ttf文件的过程中进行比较,找到描述这种这种字体的部分,后来发现选择的字体有可能和文档中定义的TrueType
字体名称不一致,因为有中文字体的存在,这一部分我并没有进行测试。因为在之后的解决过程找到了另外一种解决的方法。
DWORD CDC::GetFontData( DWORD dwTable, DWORD dwOffset, LPVOID lpData, DWORD
cbData ) const;
Remarks
Retrieves font-metric information from a scalable font file. The information to
retrieve is identified
by specifying an offset into the font file and the length of the information to
return. An application
can sometimes use the GetFontData member function to save a TrueType font with a
document. To do this,
the application determines whether the font can be embedded and then retrieves
the entire font file,
specifying 0 for the dwTable, dwOffset, and cbData parameters.
通过上面的函数可以轻易的得到选入DC中的TrueType字体的 ttf文件中的数据,但是如果想得到整个的字体文件还是会存在ttc的问题,因为这个函数得到的数据是其中选择的那种字体的数据,但是对于各个数据段offset的定义却是针对于整个文档的,如果直接引用就会有问题,也没有找到其他更好的解决方法,但是却可以准确地得到关于其中任意一个Table的数据,刚刚好对于字符集中所包含的字符的定于存在于"cmap"
Table中,问题解决了,如下:
// Macro to pack a TrueType table name into a DWORD.
#define MAKETABLENAME(ch1, ch2, ch3, ch4) (\
(((DWORD)(ch4)) << 24) | \
(((DWORD)(ch3)) << 16) | \
(((DWORD)(ch2)) << 8) | \
((DWORD)(ch1))
)
DWORD tag = MAKETABLENAME( ''c'',''m'',''a'',''p'' );
DWORD size = m_pFontDC->GetFontData(tag,0, NULL, 0);
unsigned char *pBuffer = new unsigned char[size];
m_pFontDC->GetFontData(tag,0,pBuffer,size);
//do something with pBuffer
delete[] pBuffer;
其中具体的一些操作也可以参照 msdn 中的文章。之后就是解析ttf文件了,这要参考MS发布的TrueType
Font规格书了,当然即使看懂了规格书,要自己做解析程序也是短时间难以完成的,还是让我们来找找有没有其他的已经有了的经验,在这里就要提到一个开源的项目了
fontforge 这是个日本人和台湾人维护的一个项目,可以支持多种字体进行解析和编辑,可以是个 Linux 下的程序,不过我们可以使用 Cygwin
来使这个程序在 Windows下运行起来,具体请参考 freefonts.oaka.org,但是这个程序太复杂太庞大了,我们一时半会 还不能理出头绪来,幸好在
cle.linux.org 看到 fontfoge 中有一个小工具叫做 showttf.c 可以简单的对于ttf文件进行解析,下载过来,进行编译,很有效。之后将其改进成可以只针对于“cmap”数据进行解析,并得到字体中支持字符的分段。
/*read a short(2 bytes) from a stream**/
static int Getushort(unsigned char **ttf) {
unsigned char ch1 = **(ttf);
(*ttf)++;
unsigned char ch2 = **(ttf);
(*ttf)++;
return( (ch1<<8)|ch2 );
}
/*read a long(4 bytes) from a stream**/
static int Getlong(unsigned char **ttf) {
unsigned char ch1 = **(ttf);
(*ttf)++;
unsigned char ch2 = **(ttf);
(*ttf)++;
unsigned char ch3 = **(ttf);
(*ttf)++;
unsigned char ch4 = **(ttf);
(*ttf)++;
return( (ch1<<24)|(ch2<<16)|(ch3<<8)|ch4 );
}
/*Parse the table of "cmap"**/
BOOL ReadttfEncodings(unsigned char *start_addr)
{
BOOL bResult = TRUE;
unsigned char *ttf = start_addr;
//local variable
int i;
int nencs, version;
int enc = 0;
int platform, specific;
int offset, encoff;
int format, len;
int segCount;
unsigned short *endchars,*startchars;
version = Getushort(&ttf);
nencs = Getushort(&ttf);
if ( version!=0 && nencs==0 )
nencs = version;/* Sometimes they are backwards */
for ( i=0; i < nencs; ++i )
{
platform = Getushort(&ttf);
specific = Getushort(&ttf);
offset = Getlong(&ttf);
if ( platform==3 /*&& (specific==1 ||
specific==5)*/)
{
enc = 1;
encoff = offset;
} else if ( platform==1 &&
specific==0 && enc!=1 )
{
enc = 2;
encoff = offset;
} else
if ( platform==1 && specific==1 )
{
enc = 1;
encoff = offset;
} else if ( platform==0 ) {
enc = 1;
encoff = offset;
}
if ( platform==3 )
{
//MS Symbol
}
else if ( platform==1 )
{
//Mac Roman;
}
else if ( platform==0 )
{
//Unicode Default
}
else{}
}
if ( enc!=0 )
{
//reset pointer address
ttf = start_addr + encoff;
format = Getushort(&ttf);
if ( format!=12 && format!=10 && format!=8 )
{
len = Getushort(&ttf);
/*Language*/ Getushort(&ttf);
}
else
{
/* padding */ Getushort(&ttf);
len = Getlong(&ttf);
/*Format*/ Getlong(&ttf);
}
if ( format==0 )
{
//can''t be supported
bResult = FALSE;
}
else if ( format==4 )
{
//Format 4 (Windows unicode),only
supported Format 4
segCount = Getushort(&ttf)/2;
/* searchRange = */ Getushort(&ttf);
/* entrySelector = */ Getushort(&ttf);
/* rangeShift = */ Getushort(&ttf);
endchars = new unsigned short[segCount];
for ( i=0; i < segCount; ++i )
endchars[i] = Getushort(&ttf);
if ( Getushort(&ttf)!=0 )
{
//Expected 0 in true type font;
}
startchars = new unsigned short[segCount];
for ( i=0; i < segCount; ++i )
startchars[i] = Getushort(&ttf);
//do something with endchars & startchars
delete[] startchars;
delete[] endchars;
}
else if ( format==6 )
{
/* Apple''s unicode format */
/* Well, the docs say it''s for 2byte encodings, but Apple actually*/
/* uses it for 1 byte encodings which don''t fit into the require-*/
/* ments for a format 0 sub-table. See Zapfino.dfont */
//can''t be supported
bResult = FALSE;
}
else if ( format==2 )
{
//can''t be supported
bResult = FALSE;
}
else if ( format==12 )
{
//can''t be supported
bResult = FALSE;
}
else if ( format==8 )
{
// fprintf(stderr,"I don''t support
mixed 16/32 bit
// characters (no unicode surogates), format=%d", format);
// can''t be supported
bResult = FALSE;
}
else if ( format==10 )
{
//fprintf(stderr,"I don''t support 32
bit characters format=%d", format);
//can''t be supported
bResult = FALSE;
}
else
{
//fprintf(stderr,"Eh? Unknown encoding
format=%d", format);
//can''t be supported
bResult = FALSE;
}
}
return bResult;
}
标签:
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com
面对“数字中国”建设和中国制造2025战略实施的机遇期,中车信息公司紧跟时代的步伐,以“集约化、专业化、标准化、精益化、一体化、平台化”为工作目标,大力推进信息服务、工业软件等核心产品及业务的发展。在慧都3D解决方案的实施下,清软英泰建成了多模型来源的综合轻量化显示平台、实现文件不失真的百倍压缩比、针对模型中的大模型文件,在展示平台上进行流畅展示,提升工作效率,优化了使用体验。
本站的模型资源均免费下载,登录后即可下载。模型仅供学习交流,勿做商业用途。
本站的模型资源均免费下载,登录后即可下载。模型仅供学习交流,勿做商业用途。
本站的模型资源均免费下载,登录后即可下载。模型仅供学习交流,勿做商业用途。
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@evget.com
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢
慧都科技 版权所有 Copyright 2003-
2025 渝ICP备12000582号-13 渝公网安备
50010702500608号