在.NET应用程序中嵌入/部署自定义字体 [英] Embedding/deploying custom font in .NET app

查看:229
本文介绍了在.NET应用程序中嵌入/部署自定义字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我们有一个(公共领域)LED字体,用于打印(部署)特定字体数字与复古LED仪表面看。这是一个标准的True Type或Open Type字体,除了它看起来很有趣。



显然,为了使它工作,这种字体需要在用户的机器上。但是我们不希望强制用户将我们的特殊字体安装到您的字体文件夹。我们更喜欢直接从TTF加载Font对象,或者编程安装字体,以便可用。



应用程序如何处理这种事情?例如,我注意到Adobe XYZ在系统上安装各种字体,无需用户干预。这是我们想做的。



编辑:好,理想情况下,我们希望直接安装字体。我们不希望我们漂亮的主题LED字体显示在MS Word的用户字体下拉列表中。我们更喜欢使用这种字体,但限制其使用或外观到我们的应用程序。任何方式这样做?



编辑2:这是一个WinForms .NET 2.0应用程序。



谢谢

解决方案

我在asp.net网站上为自定义图形库使用自定义字体,但这个应该同样适用于winform而没有问题。您只需指定字体文件,字体大小和字体样式,并返回字体类型。

  public static LoadedFont LoadFont(FileInfo file,int fontSize,FontStyle fontStyle)
{
var fontCollection =新的PrivateFontCollection();
fontCollection.AddFontFile(file.FullName);
if(fontCollection.Families.Length< 0)
{
throw new InvalidOperationException(加载字体时找不到字体familiy);
}

var loadedFont = new LoadedFont();
loadedFont.FontFamily = fontCollection.Families [0];
loadedFont.Font = new Font(loadedFont.FontFamily,fontSize,fontStyle,GraphicsUnit.Pixel);
return loadedFont;
}

LoadedFont是一个简单的结构

  public struct LoadedFont 
{
public Font Font {get;组; }
public FontFamily FontFamily {get;组; }
}

这是需要防止FontFamily被GC和字体不工作(asp.net,我不知道是否需要winform)。


Is there an official way to distribute (deploy) a specific font with a .NET application?

We have a (public domain) "LED font" that prints numbers with the retro LED instrumentface look. This is a standard True Type or Open Type font like any other except it looks funky.

Obviously for that to work, this font needs to be on the user's machine. But we'd prefer to not force the user to "install our special font into your font folder". We'd prefer to either load a Font object directly from the TTF, or programatically install the font so it's available.

How do applications handle this sort of things? Eg, I notice Adobe XYZ installs various fonts on the system without user intervention. That's what we'd like to do.

EDIT: okay, ideally, we'd prefer not to install the font directly. We don't want our nifty themed LED font showing up in the user's font dropdown in MS Word. We'd prefer to use this font, but restrict its use or appearance to our app. Any way to do this?

EDIT 2: This is for a WinForms .NET 2.0 app.

Thanks!

解决方案

I use a custom font for my custom graphics-library on an asp.net site, but this should also work on winform without issues. You just specify the font-file, the font-size and the font-style, and the font-type is returned.

public static LoadedFont LoadFont(FileInfo file, int fontSize, FontStyle fontStyle)
{
    var fontCollection = new PrivateFontCollection();
    fontCollection.AddFontFile(file.FullName);
    if (fontCollection.Families.Length < 0)
    {
        throw new InvalidOperationException("No font familiy found when loading font");
    }

    var loadedFont = new LoadedFont();
    loadedFont.FontFamily = fontCollection.Families[0];
    loadedFont.Font = new Font(loadedFont.FontFamily, fontSize, fontStyle, GraphicsUnit.Pixel);
    return loadedFont;
}

LoadedFont is a simple struct

public struct LoadedFont
{
    public Font Font { get; set; }
    public FontFamily FontFamily { get; set; }
}

This is needed to prevent the FontFamily to be GC'ed and the font not working (asp.net, i do not know if it is needed on winform).

这篇关于在.NET应用程序中嵌入/部署自定义字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆