问题
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