12

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!

Swingline Rage
  • 1,090
  • 1
  • 8
  • 16

2 Answers2

16

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).

djv
  • 15,168
  • 7
  • 48
  • 72
Duckie
  • 178
  • 4
4

For a WPF app you can add it as a resource.

See here

You would just have to set the build action to resource and then reference it as follows:

<TextBlock FontFamily="./Resources/#Custom Font Name">

yclevine
  • 990
  • 3
  • 9
  • 20
  • Thanks. Upvoted. I wish like heck I could *use* WPF. Unfortunately this app is straight Winforms. – Swingline Rage Jun 09 '10 at 17:29
  • Great tip!! But it is not working for me :( I had and now I am trying to reference by adding seguisym.ttf and then writing . I tried BuildAction: Resource, Embedded Resource and Content but none of them work. The reason for this manual inclusion is that Windows 8 has a newer version of the font with several icons that I need also on clients running Windows 7. – Jakob Lithner Jan 11 '15 at 23:30