简体   繁体   English

如何使用 dwrite API 从资源加载字体?

[英]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.当我使用 GDI 时,我可以使用 AddFontMemResourceEx 加载它。 However, direct 2D has it own Dwrite API which is not compatible with GDI.然而,直接 2D 有它自己的 Dwrite API,它与 GDI 不兼容。 The official document suggests using CreateCustomFontFileReference + CreateCustomFontCollection . 官方文档建议使用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 ).查看 GitHub 上 microsoft/windows-classic-samples 存储库中的 DirectWriteCustomFontSets 示例( 此处)。 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."它演示了使用自定义字体集的五个场景,根据您的问题,听起来案例 4 将适用于您的情况:“使用内存缓冲区中包含的字体数据创建自定义字体集。”

For this scenario, the key interface is IDWriteInMemoryFontFileLoader , which was added in the Windows 10 Creators Update (spring 2017).对于这种情况,关键接口是IDWriteInMemoryFontFileLoader ,它是在 Windows 10 创意者更新(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 CustomFontSetManager.h/.cpp — 这包括也适用于其他场景的成员
  • BinaryResources.h/.cpp二进制资源.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. Document.h/.cpp — 这被描述为模拟包含字体数据的文档文件(例如,.doc 文件),但它可以是任何类型的二进制文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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