将指定的字体与directx-11一起使用

Use specified font with directx-11

本文关键字:directx-11 一起 字体      更新时间:2023-10-16

我想使用DirectX 11 的指定字体文件

搜索后,我被困在创建一个自定义字体集合。

加载指定字体文件的代码:

Platform::String^ pathTEST;
Microsoft::WRL::ComPtr<IDWriteFontFile> fontFileTEST;
Microsoft::WRL::ComPtr<IDWriteFontFileLoader> fontFileLoaderTEST;
Windows::Storage::StorageFolder^ folder = Windows::Storage::ApplicationData::Current->LocalFolder;
pathTEST = folder->Path + L"\Bubblegum.ttf";//#BubbleGum
HRESULT hr = S_OK;
if (SUCCEEDED(hr))
    hr = dwriteFactory->CreateFontFileReference(pathTEST->Data(), NULL, &fontFileTEST);
if (SUCCEEDED(hr))
    hr = fontFileTEST->GetLoader(&fontFileLoaderTEST);
if (SUCCEEDED(hr))
    hr = dwriteFactory->RegisterFontFileLoader(fontFileLoaderTEST.Get());

应该使用字体的代码,但我缺少字体集合:

MyIDWriteFactory2->CreateTextFormat(
    L"BubbleGum", MyWriteFontCollection (nullptr for the moment),
    DWRITE_FONT_WEIGHT_LIGHT, DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL,
    16.0f, L"en-us", &MyIDWriteTextFormat);

我使用Arial这样的系统字体没有任何问题。

谢谢你的帮助。

再次阅读后尝试使用DirectX使用自定义字体文件-集合密钥是什么?和自定义字体集合,我已经开始了解如何获得自定义集合。

我修改了微软给出的示例代码,原因如下:

  • 在Windows应用商店中,它不允许使用GetModuleHandle。我在ResourceFontFileEnumerator中直接使用了CreateFontFileReference,而不是CreateCustomFontFileReference。

  • 它在应用程序中使用字体,但在我的情况下,字体在用户给定的另一个文件中。因此,我使用了一个变通方法,通过ResourceFontContext::CreateFontCollection 将字体的路径列表提供给ResourceFontFileEnumerator

现在,它运行良好。