---
title: "Map template font names with aliases and remove fonts"
canonical: https://www.graphicsmill.com/samples/Text/FontRegistry/FontAliasesAndRemoval_cs
product: "Graphics Mill"
source: "Text/FontRegistry/FontAliasesAndRemoval.cs"
---

# Map template font names with aliases and remove fonts

This sample shows how to keep a font registry in step with a font library that changes while the application runs. ` CustomFontRegistry.Add ` reports for every face it reads a ` FontAddResult ` with the PostScript name and a status, for example ` AlreadyPresent ` when the same file is added twice. ` CustomFontRegistry.AddFontAlias ` attaches extra names to a registered font, so templates can refer to fonts by role ("Brand-Heading") rather than by file. ` CustomFontRegistry.Remove ` unregisters a font that was deleted from the library without rebuilding the registry, and ` PostscriptNameCollisionPolicy ` decides whether a new file with an already registered PostScript name replaces the old one.

## Instruction

Use the text API to manage a CustomFontRegistry at run time: add font files and print each FontAddResult, attach role-based aliases to the fonts and draw text with fonts created by alias, then remove one font and check that its alias no longer resolves.

## Usings

```csharp
using System;
using Aurigma.GraphicsMill.Drawing;
```

## Code Snippet

```csharp
using (var fontRegistry = new CustomFontRegistry())
{
    // An updated file of an already registered font replaces it instead of being skipped.
    fontRegistry.PostscriptNameCollisionPolicy = PostscriptNameCollisionPolicy.Replace;

    var heading = fontRegistry.Add("Lobster.ttf")[0];
    var body = fontRegistry.Add("Jost-Regular.ttf")[0];
    var again = fontRegistry.Add("Lobster.ttf")[0];

    foreach (var result in new[] { heading, body, again })
    {
        Console.WriteLine("{0}: {1}", result.PostscriptName, result.Status);
    }

    // Templates refer to fonts by role, so the brand fonts can change without editing them.
    fontRegistry.AddFontAlias(heading.PostscriptName, "Brand-Heading");
    fontRegistry.AddFontAlias(body.PostscriptName, "Brand-Body");

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

        var title = new PlainText("Brand Heading", graphics.CreateFont("Brand-Heading", 56f))
        {
            Position = new System.Drawing.PointF(30, 85),
        };

        var text = new PlainText("Body text set in the brand body font.", graphics.CreateFont("Brand-Body", 22f))
        {
            Position = new System.Drawing.PointF(30, 135),
        };

        graphics.DrawText(title);
        graphics.DrawText(text);

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

    Console.WriteLine("Before removal: {0} = {1}, Brand-Heading = {2}", heading.PostscriptName, fontRegistry.Contains(heading.PostscriptName), fontRegistry.Contains("Brand-Heading"));

    // The font was deleted from the library: unregister it without rebuilding the registry.
    fontRegistry.Remove(heading.PostscriptName);

    Console.WriteLine("After removal:  {0} = {1}, Brand-Heading = {2}", heading.PostscriptName, fontRegistry.Contains(heading.PostscriptName), fontRegistry.Contains("Brand-Heading"));
}
```

## Explanation

The code:

- Creates a ` CustomFontRegistry ` and sets ` PostscriptNameCollisionPolicy ` to ` Replace `, so an updated font file with the same PostScript name would replace the registered one.
- Adds two font files with ` CustomFontRegistry.Add ` and prints the ` PostscriptName ` and ` Status ` of each ` FontAddResult `; adding the same file again reports ` AlreadyPresent `.
- Attaches the aliases "Brand-Heading" and "Brand-Body" with ` CustomFontRegistry.AddFontAlias `.
- Draws a heading and a body line with fonts created by alias through ` Graphics.CreateFont ` and saves "FontAliasesAndRemoval.png".
- Removes the heading font with ` CustomFontRegistry.Remove ` and prints ` FontRegistry.Contains ` for its PostScript name and its alias before and after.

## 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)

## Output

- [FontAliasesAndRemoval.png](<https://www.graphicsmill.com/samples/Text/compiled/Debug/FontAliasesAndRemoval_png>) (preview)
- [FontAliasesAndRemoval.txt](<https://www.graphicsmill.com/samples/Text/compiled/Debug/FontAliasesAndRemoval_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.CustomFontRegistry.AddFontAlias(String,String)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_CustomFontRegistry_AddFontAlias_2_f3570541.htm.md>)
- [Aurigma.GraphicsMill.Drawing.CustomFontRegistry.PostscriptNameCollisionPolicy](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Drawing_CustomFontRegistry_PostscriptNameCollisionPolicy.htm.md>)
- [Aurigma.GraphicsMill.Drawing.CustomFontRegistry.Remove(String)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_CustomFontRegistry_Remove_1_bb3a7a4f.htm.md>)
- [Aurigma.GraphicsMill.Drawing.FontAddResult.PostscriptName](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Drawing_FontAddResult_PostscriptName.htm.md>)
- [Aurigma.GraphicsMill.Drawing.FontAddResult.Status](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Drawing_FontAddResult_Status.htm.md>)
- [Aurigma.GraphicsMill.Drawing.FontRegistry.Contains(String)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_FontRegistry_Contains_1_bb3a7a4f.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.Position](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Drawing_PlainText_Position.htm.md>)
- [Aurigma.GraphicsMill.Drawing.PostscriptNameCollisionPolicy](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Drawing_PostscriptNameCollisionPolicy.htm.md>)
