繁体   English   中英

获取字距调整信息

[英]Obtaining kerning information

如何获取 GDI 的字距调整信息,然后在GetKerningPairs中使用? 文档指出

lpkrnpair 数组中的对数。 如果字体有超过 nNumPairs 个字距调整对,则 function 返回错误。

但是,我不知道要传入多少对,也看不到查询它的方法。

编辑#2

这是我也尝试过的填充应用程序,它总是为任何字体的对数生成 0。 GetLastError 也将始终返回 0。

#include <windows.h>
#include <Gdiplus.h>
#include <iostream>

using namespace std;
using namespace Gdiplus;

int main(void)
{
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR           gdiplusToken;
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

    Font* myFont = new Font(L"Times New Roman", 12);
    Bitmap* bitmap = new Bitmap(256, 256, PixelFormat32bppARGB);
    Graphics* g = new Graphics(bitmap);

    //HDC hdc = g->GetHDC();
    HDC hdc = GetDC(NULL);
    SelectObject(hdc, myFont->Clone());
    DWORD numberOfKerningPairs = GetKerningPairs(hdc, INT_MAX, NULL );

    cout << GetLastError() << endl;
    cout << numberOfKerningPairs << endl;

    GdiplusShutdown(gdiplusToken);

    return 0;
}

编辑我尝试执行以下操作,但是它仍然给了我 0。

Font* myFont = new Font(L"Times New Roman", 10);
Bitmap* bitmap = new Bitmap(256, 256, PixelFormat32bppARGB);
Graphics* g = new Graphics(bitmap);

SelectObject(g->GetHDC(), myFont);
//DWORD numberOfKerningPairs = GetKerningPairs( g->GetHDC(), -1, NULL );
DWORD numberOfKerningPairs = GetKerningPairs( g->GetHDC(), INT_MAX, NULL );

您首先将第三个参数设置为 NULL 来调用它,在这种情况下,它会返回字体的字距调整对数。 然后分配 memory,并通过该缓冲区再次调用它:

int num_pairs = GetKerningPairs(your_dc, -1, NULL);

KERNINGPAIR *pairs = malloc(sizeof(*pairs) * num_pairs);

GetKernningPairs(your_dc, num_pairs, pairs);

编辑:我做了一个快速测试(使用 MFC 而不是 GDI+)并得到了看起来合理的结果。 我使用的代码是:

CFont font;
font.CreatePointFont(120, "Times New Roman", pDC);
pDC->SelectObject(&font);

int pairs = pDC->GetKerningPairs(1000, NULL);

CString result;
result.Format("%d", pairs);
pDC->TextOut(10, 10, result);

结果打印出116

问题在于您传入的是Gdiplus::Font而不是SelectObject的 HFONT。 您需要将Font* myFont转换为HFONT ,然后将该HFONT传递给 SelectObject。

首先,要将Gdiplus::Font转换为HFONT ,您需要从Gdiplus::Font获取LOGFONT 一旦你这样做了,rest 就很简单了。 获得字距对数量的工作解决方案是

Font* gdiFont = new Font(L"Times New Roman", 12);

Bitmap* bitmap = new Bitmap(256, 256, PixelFormat32bppARGB);
Graphics* g = new Graphics(bitmap);

LOGFONT logFont;
gdiFont->GetLogFontA(g, &logFont);
HFONT hfont = CreateFontIndirect(&logFont);

HDC hdc = GetDC(NULL);
SelectObject(hdc, hfont);
DWORD numberOfKerningPairs = GetKerningPairs(hdc, INT_MAX, NULL );

如您所知,我所做的唯一功能更改是创建一个FONT

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM