Read font information

.NET API Solution to get font information and metrics.

 

Aspose.Font API Solution has a rich functionality to work with fonts. Conversion, glyph manipulations, detection of Latin symbols, and many more. Some of the features are linked to manipulations with glyphs like getting information about glyphs that are present in the font.

A glyph is an individually designed character of a typeface or a graphical representation of a symbol/character. To learn more about this font unit, read the Introduction to Glyph article.

This page describes the option on how to get different font metrics(glyph count, letter height, the width of glyph ‘A’, etc) but the whole functionality is carefully described in the Using Glyph objects article. This example also includes a part of the code to get font metadata, font Name if to be specific. Such functionality can be implemented into web software like the Font Metadata app that Aspose has in its ecosystem.

To receive font metrics we need:

  • Aspose.Font for .NET API which is a feature-rich, powerful and easy-to-use document manipulation and conversion API for C# platform.

  • Open the NuGet package manager, and search for Aspose.Font and install. You may also use the following command from the Package Manager Console.

Package Manager Console Command


    PM> Install-Package Aspose.Font

Steps to get font metrics using C#:

  1. Specify the font to extract info from.
  2. Use an interface Aspose.Font.IFontMetrics to get metrics specified.
  3. Get the cmap Unicode encoding table from the font as object TtfCMapFormatBaseTable to access information about the needed font glyph.
  4. To get the glyph index for ‘A’ symbol use GetGlyphIndex() Method.
  5. Get the glyph metrics printed.

C# Code to get information about glyphs of the font

    using Aspose.Font;
    using Aspose.Font.Glyphs;
    using Aspose.Font.Ttf;
    //Font to extract info from
    TtfFont font;
    string name = font.FontName;
    Console.WriteLine("Font name: " + name);
    Console.WriteLine("Glyph count: " + font.NumGlyphs);
    string metrics = string.Format(
        "Font metrics: ascender - {0}, descender - {1}, typo ascender = {2}, typo descender = {3}, UnitsPerEm = {4}",
        font.Metrics.Ascender, font.Metrics.Descender,
        font.Metrics.TypoAscender, font.Metrics.TypoDescender, font.Metrics.UnitsPerEM);

    Console.WriteLine(metrics);

    //Get the cmap unicode encoding table from the font as an object TtfCMapFormatBaseTable to access information about the font glyph for symbol 'A'.
    //Also check that font has the object TtfGlyfTable (table 'glyf') to access glyph.
    Aspose.Font.TtfCMapFormats.TtfCMapFormatBaseTable cmapTable = null;
    if (font.TtfTables.CMapTable != null)
    {
        cmapTable = font.TtfTables.CMapTable.FindUnicodeTable();
    }
    if (cmapTable != null && font.TtfTables.GlyfTable != null)
    {
        Console.WriteLine("Font cmap unicode table: PlatformID = " + cmapTable.PlatformId + ", PlatformSpecificID = " + cmapTable.PlatformSpecificId);

        //Code for 'A' symbol
        char unicode = (char)65;

        //Glyph index for 'A'
        uint glIndex = cmapTable.GetGlyphIndex(unicode);

        if (glIndex != 0)
        {
            //Glyph for 'A'
            Glyph glyph = font.GetGlyphById(glIndex);
            if (glyph != null)
            {
                //Print glyph metrics
                Console.WriteLine("Glyph metrics for 'A' symbol:");
                string bbox = string.Format(
                    "Glyph BBox: Xmin = {0}, Xmax = {1}" + ", Ymin = {2}, Ymax = {3}",
                    glyph.GlyphBBox.XMin, glyph.GlyphBBox.XMax,
                    glyph.GlyphBBox.YMin, glyph.GlyphBBox.YMax);
                Console.WriteLine(bbox);
                Console.WriteLine("Width:" + font.Metrics.GetGlyphWidth(new GlyphUInt32Id(glIndex)));
            }
        }
    }



FAQ

1. What is the font metric?

It is the property that determines the way the font will be rendered on the screen. Some of the metrics like an ascent, descent, width, and kerning are similar to Glyph metrics .

2. How do I get font metrics?

Specify the font you want to work with. Using the properties of the Aspose.Font.IFontMetrics Class interface, extract the information about font metrics you need.

3. What is font Metadata?

Font Metadata is basically information about it. It includes many data like Font Name, Font Family Name, Postscript Name, licensing, author’s and designer information, and much more.

4. How do I change font metadata?

To see or change font information online use our Font Metadata application.