Using Glyph objects | API Solution for C++

Working with glyphs is crucial when coding fonts as it is their unit. When the basic information about glyphs is explained in the article of the What is font paragraph, here we are going to teach you the functionality of Aspose.Font for working with glyphs and the Glyph objects in particular.

Glyph object

Glyphs of any font are represented by Glyph type in Aspose.Font library. So, Glyph type is a universal object for font glyphs, independently from font format(TrueType, Compact Font Format, etc).

The functionality provided by the Glyph object

What functionality does this object provide for us?

This question is answered by the properties it has.

Let’s have an example with the next code snippet that shows how to calculate the width for the text “Hello world” in pixels when the font size is 10.

Add the next namespaces at the head of the file:

1using namespace Aspose::Font::Glyphs;
2System::SharedPtr<Aspose::Font::Font> font;

Then you need to take the next steps:

 1    //Declare text and other constants
 2    const System::String text = u"Hello world";
 3    const int32_t fontSize = 10;
 4    
 5    //Declare variable for string width
 6    double width = 0;
 7    
 8    //Get glyph for each letter in text and calculate width for whole text.
 9    //The same result can be achieved using method font->get_Metrics()->MeasureString(text, fontSize).
10    for (char16_t symbol : text)
11    {
12        System::SharedPtr<GlyphId> gid = this->_font->get_Encoding()->DecodeToGid(symbol);
13        System::SharedPtr<Glyph> glyph = this->_font->GetGlyphById(gid);
14        width += (glyph->get_WidthVectorX() / this->_font->get_Metrics()->get_UnitsPerEM()) * fontSize;
15    }

To get the glyph’s Bounding box use the GlyphBBox property of the Glyph object.

To get a visual representation of glyphs you need to know the coordinates for all glyphs’ points.

How to get coordinates for all glyph points from the Glyph object?

The next properties, IsEmpty and Path were designed for this case.

Property IsEmpty is auxiliary. It tells us whether the glyph’s path is empty, or in other words glyph has no drawing instructions at all. If it has the value false, it’s time to construct the whole glyph’s figure using the very useful property Path.

In the concept of Aspose.Font library the representation of any glyph is divided into the simplest graphic primitives, called segments, and represented by interface IPathSegment. Interface IPathSegment is a base abstract graphic primitive.

Concrete graphic primitives are represented by such types as MoveTo, LineTo, CurveTo, and ClosePath.

Type ClosePath is used to indicate the end of the current graphic contour.

Types MoveTo, LineTo, and CurveTo by their definition correspond with the identical postscript operators.

Also, the types MoveTo and LineTo by their definition correspond to the functions MoveToEx() and LineTo() from windows GDI lib, type CurveTo is used to describe Bézier curves.

Glyph property Path provides us with a collection of all graphic primitives for that glyph.

Property Path has type SegmentPath and every object of this type has property Segments of type PathSegmentCollection. This property Segments returns all the graphic primitives which object SegmentPath includes. In other words, we can get all the graphic primitives for the glyph using the entry glyph.Path.Segments.

The next example calculates all the points which glyph has and stores them in variable points, which represents an array of objects with Point type.

The logic used by this sample is simple and it doesn’t extract glyph contours. To get these contours using type ClosePath must be added to segments processing.

Add the next namespaces at the head of the file:

1using System::Collections::Generic;
2using System::Drawing;
3using Aspose::Font::Glyphs;
4using Aspose::Font::RenderingPath;

Then you need to take the next steps:

 1    System::SharedPtr<Glyph> glyph;
 2
 3    //Declare resultant list with pints
 4    System::SharedPtr<System::Collections::Generic::List<System::Drawing::Point>> points = System::MakeObject<System::Collections::Generic::List<System::Drawing::Point>>();
 5    
 6    //Init service reference on IPathSegment
 7    System::SharedPtr<IPathSegment> prevSegment;
 8    
 9    //Iterate all glyph path segments and collect points
10    for (auto&& segment : glyph->get_Path()->get_Segments())
11    {
12        if ((System::ObjectExt::Is<LineTo>(segment)) || (System::ObjectExt::Is<CurveTo>(segment)))
13        {
14            if (System::ObjectExt::Is<MoveTo>(prevSegment))
15            {
16                System::SharedPtr<MoveTo> moveTo = System::DynamicCast_noexcept<Aspose::Font::RenderingPath::MoveTo>(prevSegment);
17                AddPoint((int32_t)moveTo->get_X(), (int32_t)moveTo->get_Y(), points);
18            }
19            if (System::ObjectExt::Is<LineTo>(segment))
20            {
21                System::SharedPtr<LineTo> line = System::DynamicCast_noexcept<Aspose::Font::RenderingPath::LineTo>(segment);
22                AddPoint((int32_t)line->get_X(), (int32_t)line->get_Y(), points);
23            }
24            else if (System::ObjectExt::Is<CurveTo>(segment))
25            {
26                System::SharedPtr<CurveTo> curve = System::DynamicCast_noexcept<Aspose::Font::RenderingPath::CurveTo>(segment);
27                AddPoint((int32_t)curve->get_X1(), (int32_t)curve->get_Y1(), points);
28                AddPoint((int32_t)curve->get_X2(), (int32_t)curve->get_Y2(), points);
29                AddPoint((int32_t)curve->get_X3(), (int32_t)curve->get_Y3(), points);
30            }
31        }
32        prevSegment = segment;
33    }	
34
35void GlyphMetrics::AddPoint(int32_t x, int32_t y, System::SharedPtr<System::Collections::Generic::List<System::Drawing::Point>> points)
36{
37    System::Drawing::Point p;
38    p.set_X(x);
39    p.set_Y(y);
40    points->Add(p);
41}

All the examples of using the Aspose.Font are stored in Aspose.Font.Examples.CPP.sln solution, in the cpp-examples of the Aspose.Font Documentation

If you have any issues, you may post them at the Aspose.Font.Product Family section of the Free Support Forum and within a few hours our support team will clear everything up for you.

Have any questions about Aspose.Font?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.