我在某些图形上使用 Direct2D,我已经让它用 DirectDraw 绘制文本(使用 ID2D1RenderTarget 上的 DrawText 函数),但由于某种原因,它不会用我想要的字体绘制。当我为我想要的字体创建文本格式时,我没有收到任何错误。我正在使用多字节选项集进行编译。
字体加载代码:
std::wstring unicode(name, name + strlen(name));
HRESULT r = g_writeFactory->CreateTextFormat(unicode.c_str(), NULL, DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, (float)size, L"en-us", &g_fonts[i]);
g_fonts[i]->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_LEADING);
g_fonts[i]->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_NEAR);
if (FAILED(r))
{
dgSetLastError(dgERROR_CREATIONFAILED);
return -1;
}
return i;
文字绘制:
std::wstring unicode(text, text + strlen(text));
D2D1_RECT_F r = D2D1::RectF(x, y, x + width, y + height);
int len = unicode.size();
if (g_isRenderToTexture)
g_textureTarget->DrawText(unicode.c_str(), len, g_fonts[fontId], &r, g_solidBrush);
else
g_bufferTarget->DrawText(unicode.c_str(), len, g_fonts[fontId], &r, g_solidBrush);