Glyphs and Metrics of TrueType Fonts | C++

Get Font Metrics

Font files can be contain font metrics information such as Ascender, Descender, TypoAscender, TypoDescender and UnitsPerEm. This information can be retrieved from font files using Aspose.Font for C++ API can read the Font Metrics information from the TrueType Font file using the following sample code.

 1For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
 2System::String fileName = dataDir + u"Montserrat-Regular.ttf";
 3//Font file name with full path
 4    
 5System::SharedPtr<FontDefinition> fd = System::MakeObject<FontDefinition>(Aspose::Font::FontType::TTF, System::MakeObject<FontFileDefinition>(u"ttf", System::MakeObject<FileSystemStreamSource>(fileName)));
 6System::SharedPtr<TtfFont> font = System::DynamicCast_noexcept<Aspose::Font::Ttf::TtfFont>(Aspose::Font::Font::Open(fd));
 7    
 8System::String name = font->get_FontName();
 9System::Console::WriteLine(System::String(u"Font name: ") + name);
10System::Console::WriteLine(System::String(u"Glyph count: ") + font->get_NumGlyphs());
11System::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());
12    
13System::Console::WriteLine(metrics);
14    
15//Get cmap unicode encoding table from font as object TtfCMapFormatBaseTable to access information about font glyph for symbol 'A'.
16//Also check that font has object TtfGlyfTable (table 'glyf') to access glyph.
17System::SharedPtr<Aspose::Font::TtfCMapFormats::TtfCMapFormatBaseTable> cmapTable;
18if (font->get_TtfTables()->get_CMapTable() != nullptr)
19{
20    cmapTable = font->get_TtfTables()->get_CMapTable()->FindUnicodeTable();
21}
22if (cmapTable != nullptr && font->get_TtfTables()->get_GlyfTable() != nullptr)
23{
24    System::Console::WriteLine(System::String(u"Font cmap unicode table: PlatformID = ") + cmapTable->get_PlatformId() + u", PlatformSpecificID = " + cmapTable->get_PlatformSpecificId());
25    
26    //Code for 'A' symbol
27    char16_t unicode = (char16_t)65;
28    
29    //Glyph index for 'A'
30    uint32_t glIndex = cmapTable->GetGlyphIndex(unicode);
31    
32    if (glIndex != static_cast<uint32_t>(0))
33    {
34        //Glyph for 'A'
35        System::SharedPtr<Glyph> glyph = font->GetGlyphById(glIndex);
36        if (glyph != nullptr)
37        {
38            //Print glyph metrics
39            System::Console::WriteLine(u"Glyph metrics for 'A' symbol:");
40            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());
41            System::Console::WriteLine(bbox);
42            System::Console::WriteLine(System::String(u"Width:") + font->get_Metrics()->GetGlyphWidth(System::MakeObject<GlyphUInt32Id>(glIndex)));
43        }
44    }
45}

Detect Latin Symbols

Aspose.Font for C++ can detect Latin Symbols from TrueType font files. This can be achieved using the following sample code.

Extract License Restrictions

Font files can contain licensing information in one of the following modes.

Using Aspose.Font for C++, licensing restrictions can be extracted from font files. The following C++ code sample shows how to use an object of Aspose.Font.Ttf.LicenseFlags to get information about font license restrictions(flag fsType from table OS/2) in convenient form.

 1For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
 2System::String dataDir = RunExamples::GetDataDir_Data();
 3//Font to check
 4System::String fileName = dataDir + u"Montserrat-Regular.ttf";
 5//Font file name with full path
 6    
 7System::SharedPtr<FontDefinition> fd = System::MakeObject<FontDefinition>(Aspose::Font::FontType::TTF, System::MakeObject<FontFileDefinition>(u"ttf", System::MakeObject<FileSystemStreamSource>(fileName)));
 8System::SharedPtr<TtfFont> font = System::DynamicCast_noexcept<Aspose::Font::Ttf::TtfFont>(Aspose::Font::Font::Open(fd));
 9System::SharedPtr<LicenseFlags> licenseFlags;
10if (font->get_TtfTables()->get_Os2Table() != nullptr)
11{
12    licenseFlags = font->get_TtfTables()->get_Os2Table()->GetLicenseFlags();
13}
14    
15if (licenseFlags == nullptr || licenseFlags->get_FSTypeAbsent())
16{
17    System::Console::WriteLine(System::String::Format(u"Font {0} has no embedded license restrictions", font->get_FontName()));
18}
19else
20{
21    if (licenseFlags->get_IsEditableEmbedding())
22    {
23        System::Console::WriteLine(System::String::Format(u"Font {0} may be embedded, and may be temporarily loaded on other systems.", font->get_FontName()) + u" In addition, editing is permitted, including ability to format new text" + u" using the embedded font, and changes may be saved.");
24    }
25    else if (licenseFlags->get_IsInstallableEmbedding())
26    {
27        System::Console::WriteLine(System::String::Format(u"Font {0} may be embedded, and may be permanently installed", font->get_FontName()) + u" for use on a remote systems, or for use by other users.");
28    }
29    else if (licenseFlags->get_IsPreviewAndPrintEmbedding())
30    {
31        System::Console::WriteLine(System::String::Format(u"Font {0} may be embedded, and may be temporarily loaded", font->get_FontName()) + u"  on other systems for purposes of viewing or printing the document.");
32    }
33    else if (licenseFlags->get_IsRestrictedLicenseEmbedding())
34    {
35        System::Console::WriteLine(System::String::Format(u"Font {0} must not be modified, embedded or exchanged in any manner", font->get_FontName()) + u" without first obtaining explicit permission of the legal owner.");
36    }
37}

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.