我想在 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?和 Custom Font Collections ,我开始有点了解如何获得自定义集合。
我修改了微软给出的示例代码,原因如下:
在 Windows Store Apps 中,不允许使用 GetModuleHandle。我在 ResourceFontFileEnumerator 中直接使用了 CreateFontFileReference 而不是 CreateCustomFontFileReference。
它在应用程序内部使用字体,但在我的例子中,字体位于用户提供的另一个文件中。因此,我使用了一个变通方法,通过 ResourceFontContext::CreateFontCollection 将字体路径列表提供给 ResourceFontFileEnumerator
现在,它工作正常。
关于c++ - 在 directx-11 中使用指定的字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31112857/