Glyphs and Metrics of Type1 Fonts | .NET

Get Font Metrics

Font metrics contain information such as Ascender, Descender, TypoAscender, TypoDescender and UnitsPerEm. Aspose.Font for .NET API can read the Font Metrics information from the Type1 Font File using the following sample code.

  1. Create a FontDefinition – Build the full path to the Type1 font file (e.g., string fontPath = Path.Combine(dataDir, "courier.pfb");) and create a FontFileDefinition for the .pfb format, then construct a FontDefinition with FontType.Type1 and the file definition.
  2. Open the font and access metrics – Open the font using Aspose.Font.Font.Open(fd) which returns a Font object. Cast it to Type1Font to access Type1‑specific properties, then read metric properties such as Ascender, Descender, TypoAscender, TypoDescender, and UnitsPerEm.
  3. Use the metrics – These values can be used for layout calculations, scaling, or any typographic processing required by your application.
 1// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-.NET
 2string fileName = dataDir + "courier.pfb"; //Font file name with full path
 3
 4FontDefinition fd = new FontDefinition(FontType.Type1, new FontFileDefinition("pfb", new FileSystemStreamSource(fileName)));
 5Type1Font font = Aspose.Font.Font.Open(fd) as Type1Font;
 6
 7string name = font.FontName;
 8Console.WriteLine("Font name: " + name);
 9Console.WriteLine("Glyph count: " + font.NumGlyphs);
10string metrics = string.Format(
11    "Font metrics: ascender - {0}, descender - {1}, typo ascender = {2}, typo descender = {3}, UnitsPerEm = {4}",
12    font.Metrics.Ascender, font.Metrics.Descender,
13    font.Metrics.TypoAscender, font.Metrics.TypoDescender, font.Metrics.UnitsPerEM);
14
15Console.WriteLine(metrics);

Detect Latin Symbols

Aspose.Font for .NET lets you detect Latin Symbols from Type1 font files. This can be achieved using the following sample code.

  1. Load the Type1 font – Create a FontFileDefinition for the .pfb file (e.g., string fontPath = Path.Combine(dataDir, "courier.pfb");), then build a FontDefinition with FontType.Type1 and the file definition.

  2. Detect supported Latin symbols – Iterate over the font’s glyph collection and use DecodeToGid() to translate each Unicode character to its internal Glyph ID. If the method returns a non‑zero ID, the glyph is present in the font, indicating support for that Latin character.

 1// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-.NET
 2string fileName = dataDir + "courier.pfb"; //Font file name with full path
 3
 4FontDefinition fd = new FontDefinition(FontType.Type1, new FontFileDefinition("pfb", new FileSystemStreamSource(fileName)));
 5Type1Font font = Aspose.Font.Font.Open(fd) as Type1Font;
 6
 7bool latinText = true;
 8
 9
10for (uint code = 65; code < 123; code++)
11{
12    GlyphId gid = font.Encoding.DecodeToGid(code);
13    if (gid == null || gid == GlyphUInt32Id.NotDefId)
14    {
15        latinText = false;
16    }
17}
18
19if (latinText)
20{
21    Console.WriteLine(string.Format("Font {0} supports latin symbols.", font.FontName));
22}
23else
24{
25    Console.WriteLine(string.Format("Latin symbols are not supported by font {0}.", font.FontName));
26}

Have any questions about Aspose.Font?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.