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.
- Load the Font file using FontFileDefinition
- Decode the GlyphId using the DecodeToGid() method
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> ttfFont = System::DynamicCast_noexcept<Aspose::Font::Ttf::TtfFont>(Aspose::Font::Font::Open(fd)); 7 8bool latinText = true; 9 10 11for (uint32_t code = 65; code < static_cast<uint32_t>(123); code++) 12{ 13 System::SharedPtr<GlyphId> gid = ttfFont->get_Encoding()->DecodeToGid(code); 14 if (gid == nullptr || gid == System::StaticCast<System::Object>(GlyphUInt32Id::get_NotDefId())) 15 { 16 latinText = false; 17 } 18} 19 20if (latinText) 21{ 22 System::Console::WriteLine(System::String::Format(u"Font {0} supports latin symbols.", ttfFont->get_FontName())); 23} 24else 25{ 26 System::Console::WriteLine(System::String::Format(u"Latin symbols are not supported by font {0}.", ttfFont->get_FontName())); 27}
Extract License Restrictions
Font files can contain licensing information in one of the following modes.
Editable Embedding
- Font may be embedded, and may be temporarily loaded on other systems. In addition, editing is permitted, including ability to format new text using the embedded font, and changes may be saved.Installable Embedding
- Font may be embedded, and may be permanently installed for use on a remote systems, or for use by other users.Preview and Print Embedding
- Font may be embedded, and may be temporarily loaded on other systems for purposes of viewing or printing the document.Restricted Embedding
- Font must not be modified, embedded or exchanged in any manner without first obtaining explicit permission of the legal owner.
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}