In this step by step guide, we’ll explain how to get font metrics in C#. This tutorial provides code to get Type1 font metrics in C#, but Aspose.Font for .NET, a C# font library helps you read font metrics in C# from other font types as well including TruType and OpenType font formats.
Steps to Get Font Metrics in C#
- Install Aspose.Font for .NET package from NuGet.org
- Include Aspose.Font, Aspose.Font.Sources, and Aspose.Font.Type1 namespaces
- Apply license to Aspose.Font for .NET to avoid evaluation watermark
- Load the input Type1 font into FileSystemStreamSource object
- Create a FontFileDefinition object from the stream source
- Create a FontDefinition object from the file definition
- Open font definition as Type1Font object
- Now, get all the metrics attributes of the Type1 font
A font contains metrics information that helps describe the glyphs inside a font. This metrics information of the fonts helps a computer learn how to draw characters and sentences on the screen. If we want to process this metrics information via code in our .NET applications, it’s not an easy task. However, Aspose.Font for .NET helps make the whole process very simple and easy using a few steps as mentioned above.
Code to Get Font Metrics in C#
using System; | |
//Add reference to Aspose.Font for .NET API | |
//Use following namespaces to get font metrics | |
using Aspose.Font; | |
using Aspose.Font.Sources; | |
using Aspose.Font.Type1; | |
namespace GetFontMetrics | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//Set Aspose license before getting font metrics | |
//using Aspose.Font for .NET | |
Aspose.Font.License AsposeFontLicense = new Aspose.Font.License(); | |
AsposeFontLicense.SetLicense(@"c:\asposelicense\license.lic"); | |
//Load Type1 font into a file stream object & read font definition | |
FileSystemStreamSource Type1FontFileSource = new FileSystemStreamSource("SampleInputType1Font.pfb"); | |
FontFileDefinition Type1FontFileDefintion = new FontFileDefinition(Type1FontFileSource); | |
FontDefinition Type1FontDefinition = new FontDefinition(FontType.Type1, Type1FontFileDefintion); | |
//Open Type1 font | |
Type1Font InputType1Font = Font.Open(Type1FontDefinition) as Type1Font; | |
//Read font metrics information and use it for further processing | |
string FontName = InputType1Font.FontName; | |
int FontGlyphcount = InputType1Font.NumGlyphs; | |
double FontMetricsAscender = InputType1Font.Metrics.Ascender; | |
double FontMetricsDescender = InputType1Font.Metrics.Descender; | |
double FontMetricsTypoAscender = InputType1Font.Metrics.TypoAscender; | |
double FontMetricsTypoDescender = InputType1Font.Metrics.TypoDescender; | |
double FontMetricsUnitsPerEM = InputType1Font.Metrics.UnitsPerEM; | |
//display information to console in this case | |
Console.WriteLine( | |
"Font Name: {0}, " + | |
"Glyph Count: {1}, " + | |
"Asender: {2}, " + | |
"Descender: {3}, " + | |
"Typo Ascender: {4}, " + | |
"Typo Descender: {5}, " + | |
"Units Per EM: {6}", | |
FontName, | |
FontGlyphcount, | |
FontMetricsAscender, | |
FontMetricsDescender, | |
FontMetricsTypoAscender, | |
FontMetricsTypoDescender, | |
FontMetricsUnitsPerEM | |
); | |
} | |
} | |
} |
In the above code, we’re loading a PFB file format, which contains our Type1 font we’re interested to process in C# code. Once we have loaded the font and converted it to a Type1Font object, we’re able to extract font metrics in C# from this file.
This code can help you create a C# font reader easily in your .NET applications including web, desktop, windows and Microsoft Store applications.