6

当我打开 Windows 通用字体对话框时,它会为每种字体列出一堆大小。对于所有 OpenType/TrueType 字体,它具有相同的列表 - 9、10、11、12、14、16、18... 对于位图字体,列表根据可用的位图而有所不同。“小字体”有 2、3、4、5、6、7,而普通的旧 Courier 有 10、12、15。我不知道,但我从以前的阅读中了解到,即使是 TrueType 字体,某些尺寸会被提示并且会比所有其他尺寸看起来更好,所以大概我还可以看到 TrueType 字体具有更受限制的尺寸集。

我在我的应用程序中实现了一项功能,即 Ctrl+鼠标滚轮将向上和向下缩放字体大小,就像在浏览器中一样。我想确定字体的可用大小列表,以便如果我当前的大小为 12,我的应用程序知道对于 Courier New,下一个合适的更大大小是 14,而对于普通的旧 Courier,它是 15。

我该怎么做呢?

4

1 回答 1

6

有关如何枚举特定字体的字体/字体大小的说明,请参见此处。请注意,TrueType 字体可以以任何大小显示(而不仅仅是预先确定的),因为它们是基于矢量的。

int EnumFontSizes(char *fontname)
{
    LOGFONT logfont;

    ZeroMemory(&logfont, sizeof logfont);

    logfont.lfHeight = 0;
    logfont.lfCharSet = DEFAULT_CHARSET;
    logfont.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;

    lstrcpy(logfont.lfFaceName, fontname);

    EnumFontFamiliesEx(hdc, &logfont, (FONTENUMPROC)FontSizesProc, 0, 0);

    return 0;
}

int CALLBACK FontSizesProc(
    LOGFONT *plf,      /* pointer to logical-font data */
    TEXTMETRIC *ptm,   /* pointer to physical-font data */
    DWORD FontType,    /* font type */
    LPARAM lParam      /* pointer to application-defined data */
    )
{
    static int truetypesize[] = { 8, 9, 10, 11, 12, 14, 16, 18, 20, 
            22, 24, 26, 28, 36, 48, 72 };

    int i;

    if(FontType != TRUETYPE_FONTTYPE)
    {
        int  logsize    = ptm->tmHeight - ptm->tmInternalLeading;
        long pointsize  = MulDiv(logsize, 72, GetDeviceCaps(hdc, LOGPIXELSY));

        for(i = 0; i < cursize; i++)
            if(currentsizes[i] == pointsize)
                return 1;

        printf("%d ", pointsize);

        currentsizes[cursize] = pointsize;

        if(++cursize == 200) return 0;
        return 1;   
    }
    else
    {

        for(i = 0; i < (sizeof(truetypesize) / sizeof(truetypesize[0])); i++)
        {
            printf("%d ", truetypesize[i]);
        }

        return 0;
    }
}
于 2009-06-16T19:26:02.287 回答