Reputation:
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.
Upvotes: 0
Views: 483
Reputation: 4040
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
Upvotes: 0