5

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.

Peter Lang
  • 54,264
  • 27
  • 148
  • 161
Colen
  • 13,428
  • 21
  • 78
  • 107

2 Answers2

5

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
  • 299,747
  • 42
  • 398
  • 622
  • Dayum! I was just reading the LOGFONT page at http://msdn.microsoft.com/en-us/library/dd145037%28v=VS.85%29.aspx and you beat me to the answer... – Adriano Varoli Piazza May 18 '10 at 22:32
  • This will probably be right in practice, but in theory it could fail. GetObject returns a copy of the LOGFONT that was used to specify the font. But the font mapper could have substituted a different font. While it seems extremely unlikely that it would sub a symbol font for a textual font (or vice versa), it could happen in theory. So you might be better off checking the tmCharSet field in the TEXTMETRIC returned by GetTextMetrics, as that should represent the actual font rather than the logical font. – Adrian McCarthy Apr 12 '16 at 17:48
  • @AdrianMcCarthy I thought that `GetObject` returns a `LOGFONT` of the font that was actually selected, not the one that was requested originally; it might be worth an experiment to verify, unless you have a corroborating link. If you think `GetTextMetrics` is better, another answer would be welcome. – Mark Ransom Apr 12 '16 at 19:44
  • @Mark Ranson: The font mapping happens when the HFONT is selected into the DC, because it depends on device specifics. Since GetObject uses only the HFONT and has no information about which DC it has been or will be selected into, it can't return anything but the original LOGFONT. http://stackoverflow.com/a/7193439/1386054 – Adrian McCarthy Apr 12 '16 at 20:15
  • @AdrianMcCarthy it seems I've already seen and upvoted that link in the past, but thanks for reminding me because obviously I'd forgotten that useful information. You really should leave your own answer now. – Mark Ransom Apr 12 '16 at 20:28
1

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.

Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175