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

标签 c++ windows fonts 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?Custom Font Collections ,我开始有点了解如何获得自定义集合。

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

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

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

现在,它工作正常。

关于c++ - 在 directx-11 中使用指定的字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31112857/

相关文章:

c++ - 检查子成员是否为NULL

qt - QFont.toString() 的格式是否在 Qt 版本中有记录和/或稳定?

c++ - 对齐静态字符串文字

c++ - 带有 D-Bus BlueZ 的基本 BLE 客户端

c++ - 如何获取 insertRows 源?

windows - 在 ConEmu 中,是否可以在一行中获得所有命令提示符?

适用于 Windows 的 MySQL 命令行客户端

.net - 我应该在哪里保存一个 xml 文件,以便它可以在 Windows 注销时由服务写入?

.net - 为什么 FontStretch 在 WPF 中不起作用?

fonts - 如何 "decode"eexec 的字体文件?