Fonts are an integral part of digital documents and web pages that are used to define the appearance of the text. The font files are used to store information about the fonts such as styles, weight, size and etc. There could be the case when you would need to manipulate fonts in order to extract their information. For such scenarios, in this article, you will learn how to load and read information from TrueType, CFF, and Type1 fonts using C++.

C++ Font Manipulation Library

Aspose.Font for C++ is a powerful font manipulation API that allows you to work with TrueType, CFF, OpenType, and Type1, EOT, and many other fonts from within your C++ applications. You can load, save, and extract encoding information from the fonts as well as work with glyphs. The API can be downloaded from the downloads section or installed via NuGet.

Load CFF, TrueType, and Type1 Fonts using C++

Aspose.Font for C++ lets you load the CFF, TrueType, and Type1 fonts from the files located on your storage media. The following are the steps to load a font.

  • Create an object of FontDefinition class to load the font by specifying its type i.e. TrueType, Type1, etc.
  • Use CffFont, TtfFont, or Type1Font class to open CFF, TrueType, and Type1 fonts respectively from FontDefinition object.

Load CFF Fonts using C++

The following code sample shows how to load CFF fonts using C++.

For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
System::String fileName = dataDir + u"OpenSans-Regular.cff";
//Font file name with full path
System::SharedPtr<FontDefinition> fd = System::MakeObject<FontDefinition>(Aspose::Font::FontType::CFF, System::MakeObject<FontFileDefinition>(u"cff", System::MakeObject<FileSystemStreamSource>(fileName)));
System::SharedPtr<CffFont> ttfFont = System::DynamicCast_noexcept<Aspose::Font::Cff::CffFont>(Aspose::Font::Font::Open(fd));

Load TrueType Fonts using C++

The following code sample shows how to load TrueType fonts using C++.

For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
System::String fileName = dataDir + u"Montserrat-Regular.ttf";
//Font file name with 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> ttfFont = System::DynamicCast_noexcept<Aspose::Font::Ttf::TtfFont>(Aspose::Font::Font::Open(fd));

Load Type1 Fonts using C++

The following code sample shows how to load Type1 fonts using C++.

For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
System::String fileName = dataDir + u"courier.pfb";
//Font file name with full path
System::SharedPtr<FontDefinition> fd = System::MakeObject<FontDefinition>(Aspose::Font::FontType::Type1, System::MakeObject<FontFileDefinition>(u"pfb", System::MakeObject<FileSystemStreamSource>(fileName)));
System::SharedPtr<Type1Font> font = System::DynamicCast_noexcept<Aspose::Font::Type1::Type1Font>(Aspose::Font::Font::Open(fd));

Extract Font Metrics from TrueType or Type1 using C++

Aspose.Font for C++ also allows you to extract the font metrics which contain information such as Ascender, Descender, TypoAscender, TypoDescender, and UnitsPerEm. The following are the steps to retrieve font metrics from a TrueType or Type1 font.

  • Create an object of FontDefinition class to load TrueType or Type1 font.
  • Open font using TtfFont or Type1Font class according to the font’s type.
  • Extract the font’s metrics information.

Get Font Metrics from TrueType Font using C++

For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
System::String fileName = dataDir + u"Montserrat-Regular.ttf";
//Font file name with 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 font as object TtfCMapFormatBaseTable to access information about 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)));
}
}
}

Extract Font Metrics from Type1 Font using C++

For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
System::String fileName = dataDir + u"courier.pfb";
//Font file name with full path
System::SharedPtr<FontDefinition> fd = System::MakeObject<FontDefinition>(Aspose::Font::FontType::Type1, System::MakeObject<FontFileDefinition>(u"pfb", System::MakeObject<FileSystemStreamSource>(fileName)));
System::SharedPtr<Type1Font> font = System::DynamicCast_noexcept<Aspose::Font::Type1::Type1Font>(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);

Conclusion

In this article, you have learned how to load CFF, TrueType, and Type1 fonts and extract their information using C++. You can explore more about the C++ Font Manipulation API using the documentation.