views:

242

answers:

3

Question:

Is there a way to check if a given font is one of Microsoft's ClearType-optimized fonts?

I guess I could simply hard-code the list of font names, since it's a relatively short list, but that seems a bit ugly. Would the font names be the same regardless of Windows' locale and language settings?

Background:

PuTTY looks really ugly with bold, ClearType-enabled, Consolas text. I decided to play around with the source and see if I could figure out the problem, and I think I tracked it down to the following (abbreviated) code:

font_height = cfg.font.height;
if (font_height > 0) {
    font_height =
        -MulDiv(font_height, GetDeviceCaps(hdc, LOGPIXELSY), 72);
    }
}
font_width = 0;

#define f(i,c,w,u) \
    fonts[i] = CreateFont (font_height, font_width, 0, 0, w, FALSE, u, FALSE, \
                           c, OUT_DEFAULT_PRECIS, \
                           CLIP_DEFAULT_PRECIS, FONT_QUALITY(cfg.font_quality), \
                           FIXED_PITCH | FF_DONTCARE, cfg.font.name)

f(FONT_NORMAL, cfg.font.charset, fw_dontcare, FALSE);

SelectObject(hdc, fonts[FONT_NORMAL]);
GetTextMetrics(hdc, &tm);

font_height = tm.tmHeight;
font_width = tm.tmAveCharWidth;

f(FONT_BOLD, cfg.font.charset, fw_bold, FALSE);

The intent is to pick a bold font that fits the same dimensions as the normal font. I'm assuming that PuTTY's Consolas text looks ugly because, since Consolas is so heavily optimized to be layed out on specific pixel boundaries, trying to shoehorn it into arbitrary dimensions produces bad results.

So it seems like an appropriate fix would be to detect ClearType-optimized fonts and try and create the bold versions of those fonts using the same width and height as the initial CreateFont call.

+1  A: 
Kristjan Jonasson
The "(abbreviated)" code was a simplified version of windows\window.c. Your analysis is basically the same as mine. The workaround I did for my local copy of PuTTY was to pass 0 for the font width if the font name is Consolas. (Underlined fonts can be messed up too; I think your fix will fail to handle those.)
Josh Kelley
A: 

Not sure if this is an answer for you, but making sure the width of the font is an exact integer pixel width, will make fixed width TrueType fonts look good under ClearType.

To achieve this, you will need to 'round' your font height with some trial and error.

leppie
From my testing, it's best to not specify a font width at all and instead let Windows pick a font width that's appropriate for the height.
Josh Kelley
I did try that, but you will notice some off by 1 pixel spacing issues.
leppie
A: 

Leppie's answer seems not to hold in all cases, because (as already explained above), setting the height and width to (15,7) produces the ugly result, and last time I checked, 7 was an integer (:-). Kristjan.

Kristjan Jonasson
How can you set the width? The font defines a ratio. I am saying, alter the height on a floating point scale so the width is effectively an exact integer.
leppie