0

I'm using the PrivateFontCollection class in a c++ program, with a .ttf file in the "Resource Files" folder. I would like to be able to do something along the lines of this:

privateFontCollection.AddFontFile(L"Exo-Regular.ttf");

But the only way I can seem to get to it working is by accessing it through a local file path, like this:

privateFontCollection.AddFontFile(L"C:\\Users\\maybe\\Desktop\\Exo-Regular.ttf");
Maybegus
  • 35
  • 6
  • Note that [GDI+ is largely deprecated](https://stackoverflow.com/q/10007789/211160); and few people ever programmed to it in C++. Whatever you are doing, I'd bet you are probably using the wrong technology stack to do it with. – HostileFork says dont trust SE Nov 06 '18 at 00:21

1 Answers1

1

You can't do this with the AddFontFile() method; the path string it expects cannot resolve into the resources embedded in your compiled program.

Instead, you will have to use AddMemoryFont()...and pass it a pointer to resource data you've fetched via resource-aware APIs.

There's a question from 2013 with someone doing this in C#: "addFontFile from resources". I don't know what other class libraries you are using, but if you were programming to straight Win32, getting a pointer and size for your font would look something like this:

HMODULE module = NULL; // search current process, override if needed
HRSRC resource = FindResource(module, L"Exo-Regular.ttf", RT_RCDATA);
if (!resource) {...error handling... }
HGLOBAL handle = LoadResource(module, resource);
if (!handle) {...error handling... }

// "It is not necessary to unlock resources because the system
// automatically deletes them when the process that created
// them terminates."
//
void *memory = LockResource(handle);
DWORD length = SizeofResource(module, resource);

privateFontCollection.AddMemoryFont(memory, length);