Read font information
Explore font metrics with Aspose.Font API Solution for C++. Learn the detailed insights about every stroke, curve, and nuance. Our solution lets you effortlessly work with glyphs, enabling you to extract information about the unique characteristics of any font.
Dive into a world of font possibilities with Aspose.Font API Solution for C++ which offers you a kit of powerful features, including font conversions, glyph manipulation, Latin symbol detection, and more. Our API also empowers you to work with glyphs and obtain information about the glyphs included in a font. Elevate your designs, supercharge your projects, and unlock the full potential of glyphs with Aspose.Font for C++. Don't wait and get a free trial!
A glyph is a unique design for a character or symbol in a typeface. To understand this font unit better, read the Introduction to Glyph article.Font metrics are measurements and specifications used to describe the visual appearance of a typeface. These metrics can include information such as the height of characters, the width of characters, the spacing between characters, and the position of the baseline, among others. They allow designers and software developers to accurately predict the layout of text in digital documents and user interfaces.
This page explains how to retrieve different font metrics such as the number of glyphs, letter height, the width of the glyph ‘A’, etc. However, the full functionality of working with glyphs is described in detail in the Using Glyph objects article. The code example includes a section on retrieving font metadata, including the font name. This type of functionality can be integrated into web applications, such as the Font Metadata app available in the Aspose ecosystem. If you want to learn complete examples and data files, please go to Aspose Github Project .
To receive font metrics 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 get font metrics using C++:
- Extract information about a font creating an instance of the TtfFont Class.
- Print the name of the font using the FontName property.
- Print the number of glyphs in the font using the NumGlyphs property. The font metrics, such as the ascender, descender, typo ascender, typo descender, and UnitsPerEm are also printed.
- The code then retrieves the
cmap
unicode encoding table from the font, which is used to map character codes to glyph indices. It checks if the font has both thecmap
table and theglyf
table, which is used to access the glyphs. If both tables are present, it retrieves theglyf
index for the characterA
and then gets the glyph for theA
symbol. - Print the bounding box of the
A
glyph and its width.
C++ Code to get information about glyphs of the font
using Aspose::Font;
using Aspose::Font::Glyphs;
using Aspose::Font::Ttf;
//Font file name with the full path
System::SharedPtr<FontDefinition> fd = System::MakeObject<FontDefinition>(Aspose::Font::FontType::TTF, System::MakeObject<FontFileDefinition>(u"ttf", System::MakeObject<FileSystemStreamSource>(fileName)));
System::SharedPtr<TtfFont> font = System::DynamicCast_noexcept<Aspose::Font::Ttf::TtfFont>(Aspose::Font::Font::Open(fd));
System::String name = font->get_FontName();
System::Console::WriteLine(System::String(u"Font name: ") + name);
System::Console::WriteLine(System::String(u"Glyph count: ") + font->get_NumGlyphs());
System::String metrics = System::String::Format(u"Font metrics: ascender - {0}, descender - {1}, typo ascender = {2}, typo descender = {3}, UnitsPerEm = {4}", font->get_Metrics()->get_Ascender(), font->get_Metrics()->get_Descender(), font->get_Metrics()->get_TypoAscender(), font->get_Metrics()->get_TypoDescender(), font->get_Metrics()->get_UnitsPerEM());
System::Console::WriteLine(metrics);
//Get cmap unicode encoding table from the font as object TtfCMapFormatBaseTable to access the information about the font glyph for symbol 'A'.
//Also check that font has object TtfGlyfTable (table 'glyf') to access glyph.
System::SharedPtr<Aspose::Font::TtfCMapFormats::TtfCMapFormatBaseTable> cmapTable;
if (font->get_TtfTables()->get_CMapTable() != nullptr)
{
cmapTable = font->get_TtfTables()->get_CMapTable()->FindUnicodeTable();
}
if (cmapTable != nullptr && font->get_TtfTables()->get_GlyfTable() != nullptr)
{
System::Console::WriteLine(System::String(u"Font cmap unicode table: PlatformID = ") + cmapTable->get_PlatformId() + u", PlatformSpecificID = " + cmapTable->get_PlatformSpecificId());
//Code for 'A' symbol
char16_t unicode = (char16_t)65;
//Glyph index for 'A'
uint32_t glIndex = cmapTable->GetGlyphIndex(unicode);
if (glIndex != static_cast<uint32_t>(0))
{
//Glyph for 'A'
System::SharedPtr<Glyph> glyph = font->GetGlyphById(glIndex);
if (glyph != nullptr)
{
//Print glyph metrics
System::Console::WriteLine(u"Glyph metrics for 'A' symbol:");
System::String bbox = System::String::Format(System::String(u"Glyph BBox: Xmin = {0}, Xmax = {1}") + u", Ymin = {2}, Ymax = {3}", glyph->get_GlyphBBox()->get_XMin(), glyph->get_GlyphBBox()->get_XMax(), glyph->get_GlyphBBox()->get_YMin(), glyph->get_GlyphBBox()->get_YMax());
System::Console::WriteLine(bbox);
System::Console::WriteLine(System::String(u"Width:") + font->get_Metrics()->GetGlyphWidth(System::MakeObject<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.