Глифы и метрика шрифтов TrueType | С++

Получить метрики шрифта

Файлы шрифтов могут содержать информацию о метриках шрифта, такую ​​как Ascender, Descender, TypoAscender, TypoDescender и UnitsPerEm. Эту информацию можно получить из файлов шрифтов с помощью API Aspose.Font for C++ можно прочитать информацию о метриках шрифта из файла шрифта TrueType, используя следующий пример кода.

 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}

Обнаружение латинских символов

Aspose.Font для C++ может обнаруживать латинские символы в файлах шрифтов TrueType. Этого можно добиться, используя следующий пример кода.

 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}

Извлечение ограничений лицензии

Файлы шрифтов могут содержать информацию о лицензировании в одном из следующих режимов.

Используя Aspose.Font для C++, лицензионные ограничения можно извлечь из файлов шрифтов. В следующем примере кода C++ показано, как использовать объект Aspose.Font.Ttf.LicenseFlags для получения информации об ограничениях лицензии на шрифты (флаг fsType из таблицы OS/2) в удобной форме.

 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}

Have any questions about Aspose.Font?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.