Read Glyphs and Metrics information
C++ API Solution to calculate string width, calculate glyph coordinates, and other manipulations with glyphs.
Aspose.Font API offers a comprehensive range of font-related functions, including conversion, manipulation of glyphs, detection of Latin symbols, and more. Some of these features pertain to the manipulation of glyphs, which are individual characters of a typeface or graphical representations of symbols/characters. To understand more about these units of a font, refer to the Introduction to Glyph article.
This page outlines the option for reading Glyph and Metrics information, but for a complete understanding of the functionality, refer to the article Using Glyph Objects . Here, you will find numerous C++ code examples and learn about Aspose.Font capabilities for working with glyphs and Glyph objects. More code examples demonstrating the use of Aspose.Font for manipulating Glyphs can be found in the Aspose Github Project .
Glyph metrics, the term used here, refer to the information and measurements of a particular glyph in a font. They may include information such as the height, width, advance width, and other dimensions of a glyph, which are important for the proper positioning and spacing of characters in a font. These metrics are used by text layout and rendering systems to ensure a consistent and accurate display of text.
To work with glyphs we need:
Aspose.Font for C++ API which is a feature-rich, powerful and easy-to-use document manipulation and conversion API.
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++:
To calculate the width of a string using glyphs, you need to first determine the width of each individual glyph in the string, and then sum up those widths to get the total number. If explain it with steps the order is next:
- Determine the font and size that you want to use to calculate the width of the string.
- For each character in the string, determine the corresponding glyph in the font. Use the character map, which will give you the index of the corresponding glyph.
- Using the glyph index, look up the metrics for the glyph in the font’s metrics table. The metrics for a glyph will typically include its width, height, and advance width.
- Add up the advanced widths of all the glyphs in the string to get the total width of the string. Note to pay attention to the kerning information.
If describing a similar process with the C++ code it would look the next way.
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 System::String text = u"Hello world";
const int32_t fontSize = 10;
//Declare the variable for string width
double width = 0;
//Get glyph for each letter in text and calculate width for whole text.
//The same result can be achieved using method font.Metrics.MeasureString(text, fontSize).
for (char16_t symbol : text)
{
System::SharedPtr<GlyphId> gid = this->_font->get_Encoding()->DecodeToGid(symbol);
System::SharedPtr<Glyph> glyph = this->_font->GetGlyphById(gid);
width += (glyph->get_WidthVectorX() / this->_font->get_Metrics()->get_UnitsPerEM()) * fontSize;
}
font->get_Metrics()->MeasureString(text, width);
//Print the output results
System::Console::WriteLine(System::String::Format(u"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++:
- Declare the list points of type System::Drawing::Point: This list will store the points of a glyph path segment..
- Specify the Init service reference on IPathSegment interface.
- Iterate all the glyph path segments and sum up the points.
C++ Code for finding out glyph points
//Declare the resultant list with points
System::SharedPtr<System::Collections::Generic::List<System::Drawing::Point>> points =
System::MakeObject<System::Collections::Generic::List<System::Drawing::Point>>();
//Init the service reference on IPathSegment
System::SharedPtr<IPathSegment> prevSegment;
//Iterate all the glyph path segments and collect points
for (auto&& segment : glyph->get_Path()->get_Segments())
{
if ((System::ObjectExt::Is<LineTo>(segment)) || (System::ObjectExt::Is<CurveTo>(segment)))
{
if (System::ObjectExt::Is<MoveTo>(prevSegment))
{
System::SharedPtr<MoveTo> moveTo = System::DynamicCast_noexcept<Aspose::Font::RenderingPath::MoveTo>(prevSegment);
AddPoint((int32_t)moveTo->get_X(), (int32_t)moveTo->get_Y(), points);
}
if (System::ObjectExt::Is<LineTo>(segment))
{
System::SharedPtr<LineTo> line = System::DynamicCast_noexcept<Aspose::Font::RenderingPath::LineTo>(segment);
AddPoint((int32_t)line->get_X(), (int32_t)line->get_Y(), points);
}
else if (System::ObjectExt::Is<CurveTo>(segment))
{
System::SharedPtr<CurveTo> curve = System::DynamicCast_noexcept<Aspose::Font::RenderingPath::CurveTo>(segment);
AddPoint((int32_t)curve->get_X1(), (int32_t)curve->get_Y1(), points);
AddPoint((int32_t)curve->get_X2(), (int32_t)curve->get_Y2(), points);
AddPoint((int32_t)curve->get_X3(), (int32_t)curve->get_Y3(), points);
}
}
prevSegment = segment;
}
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 the importance of glyph metrics?
By understanding and properly adjusting glyph metrics, typographers and designers can ensure optimal spacing, alignment, and legibility of text in printed materials, digital interfaces, or web pages.