Use specified font with directx-11

大城市里の小女人 提交于 2019-12-11 12:34:47

问题


I would like to use a specified font file with DirectX 11

After searching, i am stuck to create a custom Font Collection.

Code to load the specified font file:

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());

Code that should use the font, but i am missing the Font collection:

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);

I don't have any problem to use a system Font like Arial.

Thanks for your help.


回答1:


After reading again Trying to use a custom font file using DirectX - What is the collection key? and Custom Font Collections, i have beginning to understand a little how to get a custom collection.

I have modified the sample code given by Microsoft for this reasons:

  • In Windows Store Apps, it don't allow to use GetModuleHandle. I used directly CreateFontFileReference and not CreateCustomFontFileReference in ResourceFontFileEnumerator.

  • It use fonts inside the application but in my case, the fonts are in another file given by the user. So I used a work around to give a list of the path to the font to ResourceFontFileEnumerator by ResourceFontContext::CreateFontCollection

Now, its works fine.



来源:https://stackoverflow.com/questions/31112857/use-specified-font-with-directx-11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!