Show Custom Font Metrics

Text Drawing Font

Adds font to FontRegistry from file and shows its metrics.

Сode Snippet

public static void Run()
{
    using (var bitmap = new Bitmap(600, 250, PixelFormat.Format24bppRgb, new RgbColor(255, 255, 255, 255)))
    using (var graphics = bitmap.GetAdvancedGraphics())
    using (var fontRegistry = new CustomFontRegistry())
    {
        var fontSize = 60f;

        // Load custom TrueType font
        var psName = fontRegistry.Add("Lobster.ttf");

        graphics.FontRegistry = fontRegistry;

        var font = graphics.CreateFont(psName, fontSize);

        // Plain text metrics
        var plainText = new PlainText("plain text", font);
        plainText.Position = new System.Drawing.PointF(5, plainText.GetBlackBox(fontRegistry, bitmap.DpiX, bitmap.DpiY).Height + 10);

        ShowTextPosition(plainText.String, plainText.Position, plainText.GetBlackBox(fontRegistry, bitmap.DpiX, bitmap.DpiY));

        DrawPlainTextWithMarkup(plainText, graphics);

        // Round text metrics
        var roundText = new RoundText("Art round text", font, new System.Drawing.PointF(50, 50))
        {
            Bend = 0.9f,
        };

        roundText.Center = new System.Drawing.PointF(
            graphics.Width - (roundText.GetBlackBox(fontRegistry, bitmap.DpiX, bitmap.DpiY).Width / 2) - 15,
            graphics.Height - (roundText.GetBlackBox(fontRegistry, bitmap.DpiX, bitmap.DpiY).Height / 2) - 10);

        ShowTextPosition(roundText.String, roundText.Center, roundText.GetBlackBox(fontRegistry, bitmap.DpiX, bitmap.DpiY));

        DrawArtTextWithMarkup(roundText, graphics);

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

private static void ShowTextPosition(string str, System.Drawing.PointF pos, System.Drawing.RectangleF blackBox)
{
    Console.WriteLine("Text metrics (\"{0}\")", str);
    Console.WriteLine("  Position");
    Console.WriteLine("    X:       {0}", pos.X);
    Console.WriteLine("    Y:       {0}", pos.Y);
    Console.WriteLine("  Black box");
    Console.WriteLine("    X:       {0}", blackBox.X);
    Console.WriteLine("    Y:       {0}", blackBox.Y);
    Console.WriteLine("    Width:   {0}", blackBox.Width);
    Console.WriteLine("    Height:  {0}", blackBox.Height);
    Console.WriteLine();
}

/// <summary>
/// Draws art text with center point and black box
/// </summary>
private static void DrawArtTextWithMarkup(ArtText text, Graphics graphics)
{
    // Draw round text, its black box and center point
    graphics.DrawText(text);

    graphics.DrawRectangle(new Pen(RgbColor.Gray, 1f), text.GetBlackBox(graphics.FontRegistry, graphics.DpiX, graphics.DpiY));

    graphics.FillEllipse(new SolidBrush(RgbColor.Red), text.Center.X - 3, text.Center.Y - 3, 6, 6);
}

/// <summary>
/// Draws PlainText with ascender and descender lines
/// </summary>
private static void DrawPlainTextWithMarkup(PlainText text, Graphics graphics)
{
    // Draw plain text, its black box, ascender, descender and position point
    graphics.DrawText(text);

    var bbox = text.GetBlackBox(graphics.FontRegistry, graphics.DpiX, graphics.DpiY);

    graphics.DrawRectangle(new Pen(RgbColor.Gray, 1f), bbox);

    var font = graphics.CreateFont(text.CharStyle.PostScriptName, text.CharStyle.Size);

    var stringMeasure = font.MeasureString("plain text");

    graphics.DrawLine(
        new Pen(RgbColor.Blue, 1f),
        bbox.X,
        text.Position.Y - stringMeasure.Ascender,
        bbox.X + bbox.Width,
        text.Position.Y - stringMeasure.Ascender);

    graphics.DrawLine(
        new Pen(RgbColor.Green, 1f),
        bbox.X,
        text.Position.Y - stringMeasure.Descender,
        bbox.X + bbox.Width,
        text.Position.Y - stringMeasure.Descender);

    graphics.DrawLine(
        new Pen(RgbColor.IndianRed, 1f),
        bbox.X,
        text.Position.Y,
        bbox.X + bbox.Width,
        text.Position.Y);

    graphics.FillEllipse(new SolidBrush(RgbColor.Red), text.Position.X - 3, text.Position.Y - 3, 6, 6);
}

Input

Lobster.ttf

Download

Output

ShowCustomFontMetrics.txt

Text metrics ("plain text")
  Position
    X:       5
    Y:       67.78125
  Black box
    X:       -0.453125
    Y:       25.1
    Width:   215.7031
    Height:  57.78125

Text metrics ("Art round text")
  Position
    X:       488.6667
    Y:       151.5586
  Black box
    X:       392.3335
    Y:       63.11716
    Width:   192.6665
    Height:  176.8828

ShowCustomFontMetrics.png

For AI-assisted development: Download Graphics Mill Code Samples XML Catalog