Show Custom Font Metrics
View as MarkdownAdds font to FontRegistry from file and shows its metrics.
Code Snippet
public static void Run()
{
using (var bitmap = new Bitmap(600, 250, PixelFormat.Format24bppRgb, new RgbColor(255, 255, 255, 255)))
using (var graphics = bitmap.GetGraphics())
using (var fontRegistry = new CustomFontRegistry())
{
var fontSize = 60f;
// Load custom TrueType font
var psName = fontRegistry.Add("Lobster.ttf")[0].PostscriptName;
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
DownloadOutput
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.703
Y: 151.7007
Black box
X: 392.406
Y: 63.40136
Width: 192.594
Height: 176.5986
ShowCustomFontMetrics.png
Used APIs
- Aurigma.GraphicsMill.Bitmap.DpiX
- Aurigma.GraphicsMill.Bitmap.DpiY
- Aurigma.GraphicsMill.Bitmap.GetGraphics
- Aurigma.GraphicsMill.Drawing.Art.ArtText
- Aurigma.GraphicsMill.Drawing.Art.ArtText.Center
- Aurigma.GraphicsMill.Drawing.Art.RoundText
- Aurigma.GraphicsMill.Drawing.Art.RoundText.Bend
- Aurigma.GraphicsMill.Drawing.CharStyle.PostScriptName
- Aurigma.GraphicsMill.Drawing.CharStyle.Size
- Aurigma.GraphicsMill.Drawing.CustomFontRegistry
- Aurigma.GraphicsMill.Drawing.CustomFontRegistry.Add(String)
- Aurigma.GraphicsMill.Drawing.Font.MeasureString(String)
- Aurigma.GraphicsMill.Drawing.FontAddResult.PostscriptName
- Aurigma.GraphicsMill.Drawing.Graphics
- Aurigma.GraphicsMill.Drawing.Graphics.CreateFont(String,Single)
- Aurigma.GraphicsMill.Drawing.Graphics.DpiX
- Aurigma.GraphicsMill.Drawing.Graphics.DpiY
- Aurigma.GraphicsMill.Drawing.Graphics.DrawLine(Pen,Single,Single,Single,Single)
- Aurigma.GraphicsMill.Drawing.Graphics.DrawRectangle(Pen,RectangleF)
- Aurigma.GraphicsMill.Drawing.Graphics.DrawText(Text)
- Aurigma.GraphicsMill.Drawing.Graphics.FillEllipse(Brush,Single,Single,Single,Single)
- Aurigma.GraphicsMill.Drawing.Graphics.FontRegistry
- Aurigma.GraphicsMill.Drawing.Pen
- Aurigma.GraphicsMill.Drawing.PlainText
- Aurigma.GraphicsMill.Drawing.PlainText.Position
- Aurigma.GraphicsMill.Drawing.SolidBrush
- Aurigma.GraphicsMill.Drawing.StringMeasure.Ascender
- Aurigma.GraphicsMill.Drawing.StringMeasure.Descender
- Aurigma.GraphicsMill.Drawing.Text.CharStyle
- Aurigma.GraphicsMill.Drawing.Text.GetBlackBox(FontRegistry,Single,Single)
- Aurigma.GraphicsMill.Drawing.Text.String