gpt4 book ai didi

c# - 将资源中的字体加载到 PrivateFontCollection 会导致损坏重新发布

转载 作者:太空宇宙 更新时间:2023-11-03 23:28:19 24 4
gpt4 key购买 nike

我在将字体从资源加载到 PrivateFontCollection 时遇到一些困难。

当我开始这个时,我成功地从文件中加载了字体,但是我希望将字体嵌入到我的项目中(这样用户端的文件困惑就会减少,应用程序运行时的 IO 也会少一些)运行)。

以下代码将加载字体、获取正确的名称并允许缩放,但没有一个字符正确显示。

static class Foo {

public static string FontAwesomeTTF { get; set; }

public static Font FontAwesome { get; set; }

public static float Size { get; set; }

public static FontStyle Style { get; set; }

private static PrivateFontCollection pfc { get; set; }

static Foo() {
// This was set when loading from a file.
// FontAwesomeTTF = "fontawesome-webfont.ttf";
Style = FontStyle.Regular;
Size = 20;

if ( pfc==null ) {
pfc=new PrivateFontCollection();
if ( FontAwesomeTTF==null ) {
var fontBytes=Properties.Resources.fontawesome_webfont;
var fontData=Marshal.AllocCoTaskMem( fontBytes.Length );
Marshal.Copy( fontBytes, 0, fontData, fontBytes.Length );
pfc.AddMemoryFont( fontData, fontBytes.Length );
Marshal.FreeCoTaskMem( fontData );
} else {
pfc.AddFontFile( FontAwesomeTTF );
}
}
FontAwesome = new Font(pfc.Families[0], Size, Style);
}

private static string UnicodeToChar( string hex ) {
int code=int.Parse( hex, System.Globalization.NumberStyles.HexNumber );
string unicodeString=char.ConvertFromUtf32( code );
return unicodeString;
}

public static string glass { get { return UnicodeToChar("f000"); } }

}

示例用法:

label1.Font = Foo.FontAwesome;
label1.Text = Foo.glass;

这是从内存加载的样子: Loading from memory

这是从文件加载的样子: Loading from file

我在嵌入式资源和基于文件的测试中都使用了当前的 FontAwesome TTF 文件。似乎当嵌入的东西在翻译中或从嵌入加载时丢失或加扰。我需要帮助才能完成这项工作,以便我可以将字体从嵌入式资源加载到 PrivateFontCollection 中。

我在 SO 上查看了一些“解决方案”,但它们已经过时,并且 Visual Studio 2013 中不再提供某些或所有命令/访问器(文章解决方案来自 4-5 年前)。一些示例“解决方案”以及它们为何不起作用:

Solution #1 - 这不起作用,因为字体的访问器字符串返回 null。在我的例子中,访问器是 MyProject.Properties.Resources.fontawesome_webfont

Solution #2 - 此解决方案最接近,但再次用于访问资源的方法不再有效。我上面的代码实现了一个收获版本,去掉了将 byte[] 数组传递到内存然后从内存加载的核心概念。由于在我的例子中资源的 get{} 属性已经返回一个 byte[] 数组,所以没有必要将它“转换”为字节数组,因此我(看似)能够安全地删除那部分代码更新它以使用更新的访问器。

无论哪种情况,我都想要一个解决这个问题的方法,允许我将字体文件从嵌入式资源加载到 PrivateFontCollection 中。

最佳答案

AddMemoryFont's remarks section说:

To use the memory font, text on a control must be rendered with GDI+. Use the SetCompatibleTextRenderingDefault method, passing true, to set GDI+ rendering on the application, or on individual controls by setting the control's UseCompatibleTextRendering property to true. Some controls cannot be rendered with GDI+.

我在本地重现了您的问题,并将您的使用示例更改为:

label1.Font = Foo.FontAwesome;
label1.Text = Foo.glass;
label1.UseCompatibleTextRendering = true;

这会为该标签打开 GDI+ 呈现,允许使用 AddMemoryFont 添加的字体正确呈现。

关于c# - 将资源中的字体加载到 PrivateFontCollection 会导致损坏重新发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33087848/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com