4

How can I test if a font is installed?

Ultimately, I want to implement a HTML-like font selection, i.e. when specifying e.g. "Verdana,Arial", it should pick the first font that is installed.

This Question provides an answer for .NET - it seems the recommended way is to create the font, and then cmpare the font face actually used.

Is that the most efficient way?

Community
  • 1
  • 1
peterchen
  • 40,917
  • 20
  • 104
  • 186

2 Answers2

6

You can either try to create the font and see what you get (thus using the OS's font name matching/substitution).

Or you can enumerate installed fonts and do that matching yourself.

The "most efficient" way will depend on the details of a "match", and, in all likelihood, how many fonts are installed. On a system with, say, 50 fonts you will probably find performance is significantly different to a system with 1000 fonts installed.

In the end you can only profile on representative systems, if you first approach (keep it simple) turns out to be a performance bottleneck.

Richard
  • 106,783
  • 21
  • 203
  • 265
5

You can use EnumFontFamiliesEx to enumerate the list of Fonts on the system, or if you pass a font name you can enumerate the fonts for that family.

Steven
  • 3,878
  • 3
  • 21
  • 21
  • Do you know what is the point of the DC parameter to EnumFonts et al.? It isn't required in CreateFont. Passing the Desktop Window's DC seems to work, but I can't find any documentation (except the "handle to the device context") in MSDN – peterchen Apr 23 '09 at 07:57
  • 1
    I suspect it might be for when using a printer dc but I have only ever used GetDC(NULL) and have never tried anything else. – Steven Apr 23 '09 at 09:18
  • 1
    The DC will restrict the fonts enumerated to ones available to the device. Nowadays, that's not much of a restriction. You don't need a DC when you call CreateFont, because CreateFont just creates an internal version of a LOGFONT and gives you a handle to it. Only when you select that handle into a DC will the actual font be chosen and instantiated. – Adrian McCarthy Sep 17 '12 at 22:52