1

I'm using the ChooseFont system dialog to populate a LOGFONT structure that is passed to CreateFontIndirect and the resulting font handle is selected into the device context and used. But it doesn't resemble the requested font in any way (well, OK, similar character sets, but otherwise not the same). Here's an image of the screen showing the ChooseFont dialog selecting an old DOS-style VGA font and in the background (behind the ChooseFont dialog) shows the font that I got when I selected the values shown in the dialog:

Screen Capture of ChooseFont and result

Notice that the Sample in the dialog and what Windows puts on the screen when the created font is selected into the DC are quite different. This doesn't happen for ALL fonts, only some of them, which is why I'm trying to correct and/or detect this.

I've tried getting the LOGFONT for the new font from the DC using (GetCurrentObject and GetObject) and it is identical to my request, except that the lfQuality has changed to NONANTIALIASED_QUALITY from anything else I've tried setting it to.

I have a kinda two part question:

  1. How can I force Windows to use the font I've selected. Obviously there is a way, or it wouldn't look right in the ChooseFont dialog, either.
  2. If no one can answer #1, how can I reliably find out that Windows has selected a totally wrong font, when asking Windows for the font info about the created font when selected into the device context doesn't work?

EDIT: July 8

If this helps at all, my basic calling sequence is:

LOGFONT    lf;
CHOOSEFONT cf;
HFONT      hf;

cf.lpLogFont = &lf; // plus other necessary stuff
ChooseFont( &cf );
hf = CreateFontIndirect( &lf );
SelectObject( hdc, hf );
ExtTextOut( hdc, ... );

Then, when I use the font, it doesn't always match what I requested. At all. (I just tried using Arial Monospaced MT and was rewarded with Courier -- Lucida Console would be a better substitute, at least it doesn't have serifs!) I don't do ANYTHING to the DC other than call GetDC(). I don't call any GDI setup/configuration functions, either, so I'm in whatever default mode one gets with a standard Windows program written in C to the WIN32API.

I've discovered that the Windows version of PuTTY shows the correct fonts while my program doesn't, but I have not yet been able to find anything different between the functions/parameters it uses to handle font creation and the way I do it. I'll keep looking though. Thanks to anyone who's given my question any thought, with or without suggestions. :-)

Steve Valliere
  • 1,119
  • 7
  • 12
  • It looks like windows is falling back to a known registered font in the same generic family (or panose classification). It may be worth having a look at which font you have installed from which these letters are getting pulled, and looking at whether that font shares any properties with the that gets derived that might make windows fall back to it. Won't *solve* the problem, but does give you more information to work with figuring out what's going on. – Mike 'Pomax' Kamermans Jul 07 '15 at 16:47
  • My best guess is that Windows is using Courier instead of what I requested. The main thing they have in common is they're both fixed width (I don't know fonts more than that). What I find most difficult to understand is why the ChooseFont dialog shows the correct sample, but Windows won't give me the same font for the same screen. – Steve Valliere Jul 07 '15 at 16:53
  • "Perfect" is rather a misnomer for this font. Most likely basic problem is that this font was designed to be usable only in a console mode session, 437 is the OEM code page that matches the olden IBM PC character ROM. In other words, LOGFONT.lfCharSet is OEM_CHARSET. Which is not compatible with your GUI app, it operates in the system default code page, 1252 with a name like yours. So the font mapper decided to substitute, you got Courier New. Consider Consolas or Lucida Sans. – Hans Passant Jul 07 '15 at 17:02
  • OEM_CHARSET *IS* compatible with my app. In fact, it is preferable. The fonts that work best are almost all old bitmap (.FON) fonts. The "Perfect VGA" font shown in the image is just one example. However, you've given me another thing to look into (the code page) thanks. – Steve Valliere Jul 07 '15 at 17:52
  • My experience has been that the font mapper doesn't prioritize mismatched parameters the way you would expect. I'd expect the `LOGFONT` structure returned by `ChooseFont` to be an exact match. But your question implies you're not using that `LOGFONT` but rather choosing the font based on the values presented in the dialog? There are some critical parameters missing from the dialog, so I'm not surprised it doesn't work. – Mark Ransom Jul 07 '15 at 22:09
  • My comments starts by saying I'm getting a LOGFONT from ChooseFont() and using it in CreateFontIndirect(). Later, I'm selecting the font into the DC and getting the LOGFONT for the currently selected font to see if it matches what I requested (as suggested by other StackOverflow answers) and it always matches ... except the visible font on the screen DOES NOT MATCH. Some fonts match fine, others get changed (usually to Courier), but I can only detect the change with my good ol' Mark I Eyeballs -- the code (and Windows) says they match. – Steve Valliere Jul 08 '15 at 12:13
  • Mea culpa. I'm an idiot. I found that, at some point, a subroutine that tested to see if the font was OEM_CHARSET was actually SETTING the LOGFONT to OEM_CHARSET between the calls to ChooseFont and CreateFontIndirect. When the font WAS NOT already an OEM font, the font creation failed. HOWEVER... sometimes I STILL get a substitute (but much, much less often) and I STILL cannot detect that happening, which was (and still is) my original question. – Steve Valliere Jul 08 '15 at 16:41
  • This explains why GetObject returns the original LOGFONT instead of one that matches what the font mapper actually suggested: http://stackoverflow.com/a/7193439/1386054 – Adrian McCarthy Apr 12 '16 at 15:55
  • 1
    Possible duplicate of [How can I find what font was actually used for my CreateFont call?](http://stackoverflow.com/questions/7154858/how-can-i-find-what-font-was-actually-used-for-my-createfont-call) – Adrian McCarthy Apr 12 '16 at 21:21
  • The original question was due to a bug, as explained in the comments. The remaining question is a duplicate, so I'm voting to close. http://stackoverflow.com/q/7154858/1386054 – Adrian McCarthy Apr 12 '16 at 21:22

0 Answers0