简体   繁体   中英

How to load font from resource using dwrite API?

I want to load a font from a compiled resource which is the program itself. I can load it using AddFontMemResourceEx when I was using GDI. However, direct 2D has it own Dwrite API which is not compatible with GDI. The official document suggests using CreateCustomFontFileReference + CreateCustomFontCollection . Could anyone give me an example how to do it?

Take a look at the DirectWriteCustomFontSets sample in the microsoft/windows-classic-samples repo on GitHub ( here ). It demonstrates five scenarios using a custom font set, and based on your question it sounds like case 4 would apply to your situation: "Creates a custom font set using font data contained in in-memory buffers."

For this scenario, the key interface is IDWriteInMemoryFontFileLoader , which was added in the Windows 10 Creators Update (spring 2017).

The specific files in the sample project that will be most relevant are:

  • CustomFontSetManager.h/.cpp — this includes members applicable to the other scenarios as well
  • BinaryResources.h/.cpp
  • Document.h/.cpp — this is described as simulating a document file containing the font data (eg, a .doc file), but it could be any kind of binary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM