---
title: "Load fonts on demand with the FontMissing event"
canonical: https://www.graphicsmill.com/samples/Text/FontRegistry/LoadFontsOnDemand_cs
product: "Graphics Mill"
source: "Text/FontRegistry/LoadFontsOnDemand.cs"
---

# Load fonts on demand with the FontMissing event

This sample shows how to start with an empty font registry and add only the fonts a design actually uses, at the moment it asks for them. When a font is requested by a PostScript name that is not registered, ` CustomFontRegistry ` raises the ` FontMissing ` event; the handler looks the name up in the application's own font catalog and adds the file with ` CustomFontRegistry.Add `, and the request then succeeds. A server with a large font library no longer has to parse every font at startup.

## Instruction

Use the text API to draw text with fonts from a catalog of font files without registering the whole catalog up front: handle the FontMissing event of CustomFontRegistry and add the requested font file there.

## Usings

```csharp
using System;
using System.Collections.Generic;
using Aurigma.GraphicsMill.Drawing;
```

## Code Snippet

```csharp
// The application's font catalog: PostScript names and their files. Nothing is loaded yet.
var catalog = new Dictionary<string, string>
{
    ["Lobster"] = "Lobster.ttf",
    ["Jost-Regular"] = "Jost-Regular.ttf",
    ["Montserrat-Medium"] = "Montserrat-Medium.ttf",
};

using (var fontRegistry = new CustomFontRegistry())
{
    fontRegistry.FontMissing += (sender, e) =>
    {
        if (catalog.TryGetValue(e.PostscriptName, out var fileName))
        {
            Console.WriteLine("Loading {0} from {1}", e.PostscriptName, fileName);
            e.FontRegistry.Add(fileName);
        }
    };

    using (var bitmap = new Bitmap(500, 200, PixelFormat.Format24bppRgb, RgbColor.White))
    using (var graphics = bitmap.GetGraphics())
    {
        graphics.FontRegistry = fontRegistry;

        // Each font is added the first time it is asked for.
        var title = new PlainText("Summer Sale", graphics.CreateFont("Lobster", 60f))
        {
            Position = new System.Drawing.PointF(250, 95),
            Alignment = TextAlignment.Center,
            Brush = new SolidBrush(new RgbColor(214, 69, 65)),
        };

        var subtitle = new PlainText("Up to 50% off all items", graphics.CreateFont("Jost-Regular", 24f))
        {
            Position = new System.Drawing.PointF(250, 150),
            Alignment = TextAlignment.Center,
        };

        graphics.DrawText(title);
        graphics.DrawText(subtitle);

        bitmap.Save("LoadFontsOnDemand.png");
    }

    Console.WriteLine("Fonts in the registry: {0} of {1}", fontRegistry.Fonts.Count, catalog.Count);
}
```

## Explanation

The code:

- Describes the application's font catalog as a map from PostScript names to font files; nothing is loaded yet.
- Creates an empty ` CustomFontRegistry ` and subscribes to its ` FontMissing ` event.
- In the handler, finds ` FontMissingEventArgs.PostscriptName ` in the catalog and adds the file to ` FontMissingEventArgs.FontRegistry ` with ` CustomFontRegistry.Add `.
- Assigns the registry to the ` Graphics ` of a new ` Bitmap ` and draws two ` PlainText ` objects whose fonts are created by PostScript name with ` Graphics.CreateFont `.
- Prints which fonts were loaded and how many of the catalog fonts ended up in the registry, then saves "LoadFontsOnDemand.png".

## Tags

- [Font](<https://www.graphicsmill.com/samples/tags/font_md>): Managing fonts and font metrics
- [Text](<https://www.graphicsmill.com/samples/tags/text_md>): Rendering and formatting text

## Input

- [Lobster.ttf](<https://www.graphicsmill.com/samples/Text/compiled/Debug/Lobster_ttf>) (download)
- [Jost-Regular.ttf](<https://www.graphicsmill.com/samples/Text/compiled/Debug/Jost-Regular_ttf>) (download)
- [Montserrat-Medium.ttf](<https://www.graphicsmill.com/samples/Text/compiled/Debug/Montserrat-Medium_ttf>) (download)

## Output

- [LoadFontsOnDemand.png](<https://www.graphicsmill.com/samples/Text/compiled/Debug/LoadFontsOnDemand_png>) (preview)
- [LoadFontsOnDemand.txt](<https://www.graphicsmill.com/samples/Text/compiled/Debug/LoadFontsOnDemand_txt>) (preview)

## Used APIs

- [Aurigma.GraphicsMill.Bitmap.GetGraphics](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Bitmap_GetGraphics.htm.md>)
- [Aurigma.GraphicsMill.Drawing.CustomFontRegistry](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Drawing_CustomFontRegistry.htm.md>)
- [Aurigma.GraphicsMill.Drawing.CustomFontRegistry.Add(String)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_CustomFontRegistry_Add_1_bb3a7a4f.htm.md>)
- [Aurigma.GraphicsMill.Drawing.FontMissingEventArgs.FontRegistry](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Drawing_FontMissingEventArgs_FontRegistry.htm.md>)
- [Aurigma.GraphicsMill.Drawing.FontMissingEventArgs.PostscriptName](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Drawing_FontMissingEventArgs_PostscriptName.htm.md>)
- [Aurigma.GraphicsMill.Drawing.FontRegistry.Fonts](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Drawing_FontRegistry_Fonts.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Graphics.CreateFont(String,Single)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_Graphics_CreateFont_2_1dbfa9fa.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Graphics.DrawText(Text)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_Graphics_DrawText_1_f576d03f.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Graphics.FontRegistry](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Drawing_Graphics_FontRegistry.htm.md>)
- [Aurigma.GraphicsMill.Drawing.PlainText](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Drawing_PlainText.htm.md>)
- [Aurigma.GraphicsMill.Drawing.PlainText.Alignment](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Drawing_PlainText_Alignment.htm.md>)
- [Aurigma.GraphicsMill.Drawing.PlainText.Position](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Drawing_PlainText_Position.htm.md>)
- [Aurigma.GraphicsMill.Drawing.SolidBrush](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Drawing_SolidBrush.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Text.Brush](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Drawing_Text_Brush.htm.md>)
- [Aurigma.GraphicsMill.Drawing.TextAlignment](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Drawing_TextAlignment.htm.md>)
