Glyphes et mesures des polices TrueType | C++

Obtenir les métriques de police

Les fichiers de polices peuvent contenir des informations sur les métriques de police telles que « Ascender », « Descender », « TypoAscender », « TypoDescender » et « UnitsPerEm ». Ces informations peuvent être récupérées à partir des fichiers de polices à l’aide de l’API Aspose.Font pour C++ qui peut lire les informations de métriques de police à partir du fichier de police TrueType à l’aide de l’exemple de code suivant.

 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}

Détecter les symboles latins

Aspose.Font pour C++ peut détecter les symboles latins à partir des fichiers de polices TrueType. Ceci peut être réalisé en utilisant l’exemple de code suivant.

 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}

Extraire les restrictions de licence

Les fichiers de polices peuvent contenir des informations de licence dans l’un des modes suivants.

En utilisant Aspose.Font pour C++, les restrictions de licence peuvent être extraites des fichiers de polices. L’exemple de code C++ suivant montre comment utiliser un objet Aspose.Font.Ttf.LicenseFlags pour obtenir des informations sur les restrictions de licence de police (drapeau fsType de la table OS/2) sous une forme pratique.

 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.