Read Glyphs and Metrics information

.NET API Solution to calculate string width, calculate glyph coordinates, and other manipulations with glyphs.

 

Aspose.Font API Solution has a rich functionality to work with fonts. Conversion, manipulations with glyphs, detection of Latin symbols, and many more. Some of the features are linked to manipulations with glyphs.

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

This page describes the option on how to read Glyphs and Metrics information but the whole functionality is carefully described in the Using Glyph objects article. There you will find much more C# code examples, and learn the functionality of Aspose.Font for working with glyphs and the Glyph objects in particular. More code examples of using the Aspose.Font to manipulate Glyphs are stored in Aspose.Font.Examples.sln solution.

To work with glyphs 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 Calculate string width using C#:

  1. Declare the text and other constants. Here as an example phrase «Hello world» is used.
  2. Declare a variable for string width.
  3. Get a glyph for each letter in the text using the GlyphId Class. Calculate the width for the whole text.
  4. The same result can be achieved using the MeasureString() Method.
  5. Print the output results.

C# Code for finding out string width

    using System;
    using System.IO;
    using System.Drawing;
    using System.Collections.Generic;
    using Aspose.Font.Sources;
    using Aspose.Font.Glyphs;
    using Aspose.Font.RenderingPath;
    //Declare the text and other constants
    const string text = "Hello world";
    const int fontSize = 10;

    //Declare a variable for string width
    double width = 0;

    //Get a glyph for each letter in text and calculate width for whole text.
    //The same result can be achieved using the method font.Metrics.MeasureString(text, fontSize).
    foreach (char symbol in text)
    {
        GlyphId gid = this._font.Encoding.DecodeToGid(symbol);
        Glyph glyph = this._font.GetGlyphById(gid);
        width += (glyph.WidthVectorX / this._font.Metrics.UnitsPerEM) * fontSize;
    }            

    //Print output results
    Console.WriteLine(string.Format("Width for text \"{0}\" with font size {2} is equal {3}." , 
        text, FontName, fontSize, width));

Steps to calculate all glyph points with coordinates using C#:

  1. Declare the resultant list with points .
  2. Specify the Init service reference on IPathSegment interface.
  3. Iterate all the glyph path segments and sum up the points.

C# Code for finding out glyph points

    Glyph glyph;

    //Declare the resultant list with points
    List<Point> points = new List<Point>();

    //Init service reference on IPathSegment
    IPathSegment prevSegment = null;

    //Iterate all the glyph path segments and collect points
    foreach (IPathSegment segment in glyph.Path.Segments)
    {
        if ((segment is LineTo)
            || (segment is CurveTo))
        {
            if (prevSegment is MoveTo)
            {
                MoveTo moveTo = prevSegment as MoveTo;
                AddPoint((int)moveTo.X, (int)moveTo.Y, points);
            }
            if (segment is LineTo)
            {
                LineTo line = segment as LineTo;
                AddPoint((int)line.X, (int)line.Y, points);
            }
            else if (segment is CurveTo)
            {
                CurveTo curve = segment as CurveTo;
                AddPoint((int)curve.X1, (int)curve.Y1, points);
                AddPoint((int)curve.X2, (int)curve.Y2, points);
                AddPoint((int)curve.X3, (int)curve.Y3, points);
            }
        }
        prevSegment = segment;
    }	

    void AddPoint(int x, int y, List<Point> points)
    {
        Point p = new Point();
        p.X = x;
        p.Y = y;
        points.Add(p);
    }



FAQ

1. What is a glyph metric?

Glyph metrics are the parameters that influence how the glyph is positioned when the text layout is created.

2. What are glyph metrics?

The most used glyph metrics are advance width, origin, side bearings, baseline, ascent, ascender, descent, descender, bounding box, height, width, and kerning.

3. How to manage Glyphs with this API Solution?

To code glyphs in C#, use the entities of the Aspose.Font.Glyphs namespace.

4. What is a glyph?

Glyph is one individually designed character of a typeface. It also can be defined as a graphical representation of a symbol/character.