我正在使用FW1FontWrapper库来包装DirectX 11 C++应用程序中使用的字体。

我想更改用FW1FontWrapper渲染的文本中的行距(两行文本之间的距离)。

我知道在DirectX中我可以使用IDWriteTextFormat::SetLineSpacing(DWRITE_LINE_SPACING_METHOD_UNIFORM, 30.0f, 20.0f)

不幸的是,我不知道如何访问正确的IDWriteTextFormat结构。

我试图:

HRESULT hResult = FW1CreateFactory(FW1_VERSION, &FW1Factory);
hResult = FW1Factory->CreateFontWrapper(device, L"Arial", &fontWrapper);

//my attemp - first get the IDWriteFactory
IDWriteFactory *pDWriteFactory;
hResult = fontWrapper->GetDWriteFactory(&pDWriteFactory);

//and now the IDWriteTextFormat
IDWriteTextFormat *pTextFormat;
hResult = pDWriteFactory->CreateTextFormat(L"Arial", NULL,
    DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STYLE_NORMAL,
    DWRITE_FONT_STRETCH_NORMAL, 10.0f, L"", &pTextFormat);
pTextFormat->SetLineSpacing(DWRITE_LINE_SPACING_METHOD_UNIFORM, 100.f, 20.0f);

我猜这是行不通的,因为这样我就可以创建和修改新的 IDWriteTextFormat,而不是会通过以下方式影响渲染的代码:
fontWrapper->DrawString(
   context,
   s2ws(content).c_str(),// String
   fontSize,// Font size
   startTextPosition.getX(),// X position
   startTextPosition.getY(),// Y position
   color,// Text color, 0xAaBbGgRr
   FW1_RESTORESTATE// Flags (for example FW1_RESTORESTATE to keep
        //context states unchanged)
 );

那么,如何访问正确的IDWriteTextFormat(我将对其进行修改的那一代码将对呈现产生影响;而不是新的一一)?

据我所知,FW1FontWrapper没有设置行间距的方法。

最佳答案

查看FW1FontWrapper模块的源代码,您可以看到DrawString在内部使用了无法访问的默认IDWriteTextFormat

解决方案是使用DrawTextLayout而不是DrawString

为此(使用您的变量名),首先按照现有代码创建工厂和IFW1FontWrapper:

HRESULT hResult = FW1CreateFactory(FW1_VERSION, &FW1Factory);
hResult = FW1Factory->CreateFontWrapper(device, L"Arial", &fontWrapper);

然后获取DWrite工厂,并使用所需的行距创建IDWriteTextFormat(同样,这是您现有的代码):
// get the DirectWrite factory used by the font-wrapper
IDWriteFactory *pDWriteFactory;
hResult = fontWrapper->GetDWriteFactory(&pDWriteFactory);

// create an IDWriteTextFormat and set line spacing
IDWriteTextFormat *pTextFormat;
hResult = pDWriteFactory->CreateTextFormat(L"Arial", NULL,
    DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STYLE_NORMAL,
    DWRITE_FONT_STRETCH_NORMAL, 10.0f, L"", &pTextFormat);
pTextFormat->SetLineSpacing(DWRITE_LINE_SPACING_METHOD_UNIFORM, 100.f, 20.0f);

接下来,为您的字符串(使用变量名)并使用上面创建的IDWriteTextLayout创建一个IDWriteTextFormat:
// create a DirectWrite text layout for the string
// using the above IDWriteTextFormat
IDWriteTextLayout *pTextLayout;
unsigned int stringLength = s2ws(content).size();
hResult = pDWriteFactory->CreateTextLayout(
    s2ws(content).c_str(),
    stringLength,
    pTextFormat,
    0.0f,
    0.0f,
    &pTextLayout);

IDWriteTextLayout对象中设置字体大小:
// set the required font size
DWRITE_TEXT_RANGE allText = {0, stringLength};
pTextLayout->SetFontSize(fontSize, allText);

最后,使用DrawTextLayout绘制文本(同样,使用变量名):
// draw the text
fontWrapper->DrawTextLayout(
    context,
    pTextLayout,
    startTextPosition.getX(),
    startTextPosition.getX(),
    color,
    NULL,
    NULL,
    FW1_RESTORESTATE);

// tidy up interfaces
pTextLayout->Release();
pTextLayout = NULL;

我可能从您现有的代码中做出了一些错误的假设,但是您可以看到如何通过IDWriteTextFormat实例使用自己的IDWriteTextLayout,然后使用DrawTextLayout而不是DrawString。我希望这有帮助。

10-08 08:33