0

I am trying to load a font file from memory. The resource script defines the font like this:

ID_FONT CUSTOM_RC "res/Montserrat-Regular.ttf"

And then I try to load the font resource like this:

IDWriteFactory5* dwFactory = nullptr;
DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory5),
    reinterpret_cast<IUnknown**>(&dwFactory));

HRSRC resourceInfo = FindResourceW(instance, MAKEINTRESOURCEW(ID_FONT),
    L"CUSTOM_RC");

HGLOBAL resourceData = LoadResource(instance, resourceInfo);

LPVOID resource = LockResource(resourceData);

DWORD size = SizeofResource(instance, resourceInfo);

DWORD fonts = 0;
HANDLE font = AddFontMemResourceEx(resource, size, 0, &fonts);

Then I create the IDWriteTextFormat like this:

IDWriteTextFormat* format;
dwFactory->CreateTextFormat(L"Montserrat", nullptr, DWRITE_FONT_WEIGHT_REGULAR,
    DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, 20.0f, L"en-us",
    &format);

I get no issues loading the resource, but when I try to use the font, it doesn't work. It just gives me a different font, which I think is Ariel.

I also tried loading the font like this:

IDWriteInMemoryFontFileLoader* loader = nullptr;
dwFactory->CreateInMemoryFontFileLoader(&loader);
dwFactory->RegisterFontFileLoader(loader);

IDWriteFontFile* fontFile = nullptr;
loader->CreateInMemoryFontFileReference(dwFactory, resource, size, nullptr,
    &fontFile);

But now I have a IDWriteFontFile and I'm not sure how I am supposed to make a IDWriteTextFormat from it, or if it even is the proper way to load the resource.

I'm not sure what I am doing wrong or where to go from here.

  • 1
    [Creating a custom font set using font data loaded into memory](https://learn.microsoft.com/en-us/windows/win32/directwrite/custom-font-sets-win10#creating-a-custom-font-set-using-font-data-loaded-into-memory) – user7860670 Jul 01 '23 at 07:00
  • I got the last block of code from that page. I just don't know what to do with the IDWriteFontFile object after that. How can I use it when I call CreateTextFormat of CreateTextLayout? – Isaiah Lateer Jul 03 '23 at 10:55
  • *"I just don't know what to do with the IDWriteFontFile object after that."* - that's exactly what is explained at the linked page... – user7860670 Jul 03 '23 at 11:39

1 Answers1

0

For each font file in the local file system, create an IDWriteFontFile that refers to it via CreateFontFileReference. And then add the IDWriteFontFile object to the font set builder using the AddFontFile method.

For more details, I suggest you could refer to the Doc:Custom Font Sets

Jeaninez - MSFT
  • 3,210
  • 1
  • 5
  • 20
  • How do I use an IDWriteFontFile when I create a text format or text layout? I can't seem to find any examples online of how to use the IDWriteFontFile object. – Isaiah Lateer Jul 03 '23 at 10:59
  • "how to use the IDWriteFontFile object." Add the IDWriteFontFile object to the font set builder using the `AddFontFile method`. After all of the files have been added to the font set builder, the custom font set can be created via `CreateFontSet method`. I suggest you could refer to the Doc:[Custom Font Sets](https://learn.microsoft.com/en-us/windows/win32/directwrite/custom-font-sets-win10). – Jeaninez - MSFT Jul 04 '23 at 01:44
  • @IsaiahLateer Haven't heard from you for a while. Have you got any updates about this issue? Could you please check my answer? – Jeaninez - MSFT Jul 14 '23 at 08:31