如何判断字体是否是符号字体?
给定一个 HFONT,我如何判断它是否是符号字体?我正在使用的 pdf 库需要以不同的方式处理符号字体,因此我需要一种方法来以编程方式判断任何给定字体是否是符号字体。
Given an HFONT, how do I tell if it's a symbol font? A pdf library I'm using needs to treat symbol fonts differently, so I need a way to programatically tell if any given font is a symbol font or not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 GetObject 将字体的属性获取到 < href="http://msdn.microsoft.com/en-us/library/dd145037(VS.85).aspx" rel="noreferrer">LOGFONT 结构。检查lfCharSet成员;如果是 SYMBOL_CHARSET,则表示有符号字体。
Use GetObject to get the font's properties to a LOGFONT structure. Check the lfCharSet member; if it's SYMBOL_CHARSET, you have a symbol font.
Mark Ransom 的答案在 99.999% 的情况下都有效,但理论上它可能会给出错误的答案。
为了避免这种可能性,您应该使用
GetTextMetrics
获取实际字体的TEXTMETRICS
并检查tmCharSet
是否为SYMBOL_CHARSET.
检查
lfCharSet
和tmCharSet
之间有什么区别?当您创建
HFONT
时,Windows 会创建LOGFONT
的内部副本。它描述了您想要的字体,该字体可能与您获得的字体不同。当您将
HFONT
选择到设备(或信息)上下文中时,字体映射器会找到与该HFONT
关联的LOGFONT
最匹配的实际字体>。然而,最佳匹配可能不是完全匹配。因此,当您需要了解有关实际字体的信息时,应注意查询HDC
而不是HFONT
。如果您使用
GetObject
查询HFONT
,您只会得到原始的LOGFONT
。GetObject
不会告诉您有关实际字体的任何信息,因为它不知道字体映射器选择(或将选择)的实际字体。询问特定 DC 中选择的字体的 API(例如 GetTextMetrics、GetTextFace 等)将为您提供有关实际字体的信息。
对于这个问题,Mark 的答案(使用 GetObject)可能总是有效,因为当您需要文本字体时,字体映射器选择符号字体(反之亦然)的可能性很小。不过,一般来说,当您想了解有关实际字体的信息时,请想办法询问
HDC
。Mark Ransom's answer is going to work 99.999% of the time, but there's a theoretical possibility that it could give the wrong answer.
To avoid this possibility, you should use
GetTextMetrics
to get theTEXTMETRICS
of the actual font and check if thetmCharSet
isSYMBOL_CHARSET
.What's the difference between checking
lfCharSet
andtmCharSet
?When you create an
HFONT
, Windows makes an internal copy of theLOGFONT
. It describes the font you want, which could be different than the font you get.When you select the
HFONT
into a device (or information) context, the font mapper finds the actual font that best matches theLOGFONT
associated with thatHFONT
. The best match, however, might not be an exact match. So when you need to find out something about the actual font, you should take care to query theHDC
rather than theHFONT
.If you query the
HFONT
withGetObject
, you just get the originalLOGFONT
back.GetObject
doesn't tell you anything about the actual font because it doesn't know what actual font the font mapper chose (or will choose).APIs that ask about the font selected into a particular DC, like
GetTextMetrics
,GetTextFace
, etc., will give you information about the actual font.For this problem, Mark's answer (using
GetObject
) is probably always going to work, because the odds of the font mapper choosing a symbol font when you want a textual font (or vice versa) are minuscule. In general, though, when you want to know something about the actual font, find a way to ask theHDC
.