winapi - 检索 Windows 上可用的字体大小

标签 winapi fonts font-size

当我打开 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。

我该如何去做呢?

最佳答案

参见here有关如何枚举特定字体的字体/字体大小的说明。请注意,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;
    }
}

关于winapi - 检索 Windows 上可用的字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1003450/

相关文章:

winapi - 在 *.VBS 文件中导入 WinAPI 函数

objective-c - 如何在 macOS 中以编程方式安装 mac 中的自定义字体

r - 在R数字中指定字体大小

html - 如何增加 Bootstrap navbar-brand 字体大小?

winapi - 计算使用 GDI 渲染的文本(文本矩形)的大小

c++ - 如何使用 Code::Blocks 链接到库?

c++ - WideCharToMultiByte() 与 wcstombs()

c - Visual-C Win32 API 分层窗口闪烁

css - 从背景中剪下的透明文本

html - 是否可以加载可变字体并使用 javascript 对其进行操作?