Map template font names with aliases and remove fonts
View as MarkdownThis 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.
Code Snippet
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"));
}
Input
Lobster.ttf
DownloadJost-Regular.ttf
DownloadOutput
FontAliasesAndRemoval.png
FontAliasesAndRemoval.txt
Lobster: Added
Jost-Regular: Added
Lobster: AlreadyPresent
Before removal: Lobster = True, Brand-Heading = True
After removal: Lobster = False, Brand-Heading = False
Used APIs
- Aurigma.GraphicsMill.Bitmap.GetGraphics
- Aurigma.GraphicsMill.Drawing.CustomFontRegistry
- Aurigma.GraphicsMill.Drawing.CustomFontRegistry.Add(String)
- Aurigma.GraphicsMill.Drawing.CustomFontRegistry.AddFontAlias(String,String)
- Aurigma.GraphicsMill.Drawing.CustomFontRegistry.PostscriptNameCollisionPolicy
- Aurigma.GraphicsMill.Drawing.CustomFontRegistry.Remove(String)
- Aurigma.GraphicsMill.Drawing.FontAddResult.PostscriptName
- Aurigma.GraphicsMill.Drawing.FontAddResult.Status
- Aurigma.GraphicsMill.Drawing.FontRegistry.Contains(String)
- Aurigma.GraphicsMill.Drawing.Graphics.CreateFont(String,Single)
- Aurigma.GraphicsMill.Drawing.Graphics.DrawText(Text)
- Aurigma.GraphicsMill.Drawing.Graphics.FontRegistry
- Aurigma.GraphicsMill.Drawing.PlainText
- Aurigma.GraphicsMill.Drawing.PlainText.Position
- Aurigma.GraphicsMill.Drawing.PostscriptNameCollisionPolicy