1

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.

nelbok
  • 21
  • 4
  • The [MSDN sample](https://msdn.microsoft.com/en-us/library/windows/desktop/dd368197(v=vs.85).aspx) shows just creating the font directly from the ``IDWriteFontFile``. Have you tried doing that? – Chuck Walbourn Jun 29 '15 at 16:46
  • Thanks for your answer. Yes, but when i use this to create the text format or to write a text on my screen, its ignore the specified font. – nelbok Jun 30 '15 at 07:58

1 Answers1

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.

nelbok
  • 21
  • 4