检测 ClearType 优化字体

发布于 2024-08-26 14:12:35 字数 1417 浏览 12 评论 0原文

问题:

有没有办法检查给定字体是否是 Microsoft 的 ClearType 优化字体

我想我可以简单地对字体名称列表进行硬编码,因为它是一个相对较短的列表,但这看起来有点难看。无论 Windows 的区域设置和语言设置如何,字体名称是否都相同?

背景:

PuTTY 外观粗体、启用 ClearType 的 Consolas 文本确实很难看。我决定研究源代码,看看是否可以解决问题,我想我将其跟踪到以下(缩写)代码:

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);

目的是选择与普通字体尺寸相同的粗体字体。我假设 PuTTY 的 Consolas 文本看起来很难看,因为 Consolas 进行了如此严格的优化以布局在特定的像素边界上,试图将其硬塞到任意维度会产生糟糕的结果。

因此,适当的修复似乎是检测 ClearType 优化字体,并尝试使用与初始 CreateFont 调用相同的宽度和高度来创建这些字体的粗体版本。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-09-02 14:12:35

我不确定我是否完全理解“跟踪到以下(缩写)代码”的含义,但可以肯定的是,Consolas 粗体在 Putty 中看起来很糟糕。这里是 windows/window.c 的 putty 源代码(通过 subversion“svn co svn://svn.tartarus.org/sgt/putty”获得的修订版 8914,于 2010 年 4 月 5 日)。

      1386      fonts[i] = CreateFont (font_height, font_width, 0, 0, w, FALSE, u, FALSE, \
      1387                             c, OUT_DEFAULT_PRECIS, \
      1388                             CLIP_DEFAULT_PRECIS, FONT_QUALITY(cfg.font_quality), \
      1389                             FIXED_PITCH | FF_DONTCARE, cfg.font.name)
      1390  
      1391      f(FONT_NORMAL, cfg.font.charset, fw_dontcare, FALSE);
      1392  
      1393      SelectObject(hdc, fonts[FONT_NORMAL]);
      1394      GetTextMetrics(hdc, &tm);
      1395  
      1396      GetObject(fonts[FONT_NORMAL], sizeof(LOGFONT), &lfont);
      1397  
      1398      if (pick_width == 0 || pick_height == 0) {
      1399            font_height = tm.tmHeight;
      1400            font_width = tm.tmAveCharWidth;
      1401      }
      ...
      1477      if (bold_mode == BOLD_FONT) {
      1478      f(FONT_BOLD, cfg.font.charset, fw_bold, FALSE);
      1479      }

对于 10 pt Consolas,当为 FONT_NORMAL(通过宏 f)调用 CreateFont 时,font_height 和 font_width 的值为 -13 和 0,但为 FONT_BOLD 调用时,font_height 和 font_width 的值为 15 和 7(这些值在第 1399,1400 行更改) 。显式设置宽度值与 Consolas 粗体字体不太一致。如果修改 window.c 以便将第 1477-1479 行移动到第 1391 行之后,则会获得更好的粗体 Consolas 字体(我还尝试了所有其他可用字体(Courier、Bitstream、Lucida 等),它们并没有产生不利影响我已将此建议发送给 Putty 团队,下图显示了差异:
Bold-consolas-example

于 2011 年 1 月 16 日添加:“bug”仍然存在,并且修复仍然有效。现在,window.c 中的行号是第 1510-1512 行应移至第 1417 行之后。Kristjan。

添加于一月。 2012年12月13日:该错误仍然存​​在于putty 0.62版本(2011年12月10日发布)中。要移动的行号现在是 1439 之后的 1532-1534。为了完整起见,这里是我在 cygwin 中执行的步骤:

$ svn co svn://svn.tartarus.org/sgt/putty2
$ cd putty
$ perl mkfiles.pl
$ cd windows
$ --- edit window.c and move the 3 lines---
$ make CC=gcc-3 -f Makefile.cyg
$ --- move resulting executable files (including putty.exe) to a suitable folder
$ --- add shortcuts to the executables to Windows start menu

Kristjan

I am not sure that I follow quite what is meant by "tracked it down to the following (abbreviated) code", but sure enough Consolas bold looks bad in Putty. Here is putty source of windows/window.c (revision 8914 obtained with subversion "svn co svn://svn.tartarus.org/sgt/putty", on 5 April 2010).

      1386      fonts[i] = CreateFont (font_height, font_width, 0, 0, w, FALSE, u, FALSE, \
      1387                             c, OUT_DEFAULT_PRECIS, \
      1388                             CLIP_DEFAULT_PRECIS, FONT_QUALITY(cfg.font_quality), \
      1389                             FIXED_PITCH | FF_DONTCARE, cfg.font.name)
      1390  
      1391      f(FONT_NORMAL, cfg.font.charset, fw_dontcare, FALSE);
      1392  
      1393      SelectObject(hdc, fonts[FONT_NORMAL]);
      1394      GetTextMetrics(hdc, &tm);
      1395  
      1396      GetObject(fonts[FONT_NORMAL], sizeof(LOGFONT), &lfont);
      1397  
      1398      if (pick_width == 0 || pick_height == 0) {
      1399            font_height = tm.tmHeight;
      1400            font_width = tm.tmAveCharWidth;
      1401      }
      ...
      1477      if (bold_mode == BOLD_FONT) {
      1478      f(FONT_BOLD, cfg.font.charset, fw_bold, FALSE);
      1479      }

For 10 pt Consolas, the values of font_height and font_width were -13 and 0 when CreateFont was called for FONT_NORMAL (via the macro f), but 15 and 7 when it was called for FONT_BOLD (the values are changed on lines 1399,1400). Setting the value of width explicitly does not agree well with the Consolas bold font. If window.c is modified so that lines 1477-1479 are moved to come after line 1391 a much nicer bold Consolas font is obtained (I also tried all the other available fonts (Courier, Bitstream, Lucida etc.) and they were not adversely affected. I have sent this suggestion to the Putty team. Here is a figure showing the difference:
Bold-consolas-example

ADDED ON JANUARY 16, 2011: The "bug" is still present, and the fix still works. Now the line-numbers in window.c are that lines 1510-1512 should be moved to come after line 1417. Kristjan.

ADDED ON JAN. 13, 2012: The bug is still in version 0.62 of putty (released on Dec. 10, 2011). The linenumbers to move are now 1532-1534 to come after 1439. For completeness here are the steps I carried out in cygwin:

$ svn co svn://svn.tartarus.org/sgt/putty2
$ cd putty
$ perl mkfiles.pl
$ cd windows
$ --- edit window.c and move the 3 lines---
$ make CC=gcc-3 -f Makefile.cyg
$ --- move resulting executable files (including putty.exe) to a suitable folder
$ --- add shortcuts to the executables to Windows start menu

Kristjan

死开点丶别碍眼 2024-09-02 14:12:35

不确定这是否适合您,但确保字体的宽度是精确的整数像素宽度,将使固定宽度的 TrueType 字体在 ClearType 下看起来不错。

为了实现这一点,您需要通过一些尝试和错误来“舍入”字体高度。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文