在 directx-11 中使用指定的字体

Use specified font with directx-11

我想在 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 这样的系统字体没有任何问题。

感谢您的帮助。

又看了一遍Trying to use a custom font file using DirectX - What is the collection key? and Custom Font Collections,开始有点明白怎么弄自定义了collection。

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

  • 在Windows Store Apps中,不允许使用GetModuleHandle。我在 ResourceFontFileEnumerator 中直接使用了 CreateFontFileReference 而不是 CreateCustomFontFileReference。

  • 它在应用程序内部使用字体,但就我而言,字体位于用户提供的另一个文件中。所以我使用了一个变通方法,通过 ResourceFontContext::CreateFontCollection

  • 给出了 ResourceFontFileEnumerator 的字体路径列表

现在,它工作正常。