How do I tell if a font is a symbol font?
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.
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'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 the TEXTMETRICS
of the actual font and check if the tmCharSet
is SYMBOL_CHARSET
.
What's the difference between checking lfCharSet
and tmCharSet
?
When you create an HFONT
, Windows makes an internal copy of the LOGFONT
. 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 the LOGFONT
associated with that HFONT
. 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 the HDC
rather than the HFONT
.
If you query the HFONT
with GetObject
, you just get the original LOGFONT
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 the HDC
.
精彩评论