Pol*_*hic 1 c++ directx reference directx-11
我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);
Run Code Online (Sandbox Code Playgroud)
我想它不起作用,因为这样我创建和修改新的 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)
);
Run Code Online (Sandbox Code Playgroud)
那么,如何访问权限IDWriteTextFormat(当我将修改时,将对渲染产生影响而不是新渲染的那个)?
据我所知FW1FontWrapper,没有自己的方法来设置行间距.
查看FW1FontWrapper模块的源代码,您可以看到DrawString内部使用IDWriteTextFormat您无法访问的默认值.
解决方案是使用DrawTextLayout而不是DrawString.
要执行此操作(使用变量名称),请首先创建工厂,IFW1FontWrapper并在现有代码中创建:
HRESULT hResult = FW1CreateFactory(FW1_VERSION, &FW1Factory);
hResult = FW1Factory->CreateFontWrapper(device, L"Arial", &fontWrapper);
Run Code Online (Sandbox Code Playgroud)
然后获取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);
Run Code Online (Sandbox Code Playgroud)
接下来,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);
Run Code Online (Sandbox Code Playgroud)
在IDWriteTextLayout对象中设置字体大小:
// set the required font size
DWRITE_TEXT_RANGE allText = {0, stringLength};
pTextLayout->SetFontSize(fontSize, allText);
Run Code Online (Sandbox Code Playgroud)
最后,使用DrawTextLayout(再次使用您的变量名称)绘制文本:
// draw the text
fontWrapper->DrawTextLayout(
context,
pTextLayout,
startTextPosition.getX(),
startTextPosition.getX(),
color,
NULL,
NULL,
FW1_RESTORESTATE);
// tidy up interfaces
pTextLayout->Release();
pTextLayout = NULL;
Run Code Online (Sandbox Code Playgroud)
我可能从您现有的代码中做了一些错误的假设,但您可以看到如何IDWriteTextFormat通过IDWriteTextLayout实例使用自己的假设,然后使用DrawTextLayout而不是DrawString.我希望这有帮助.