Glyphes et mesures des polices TrueType | .NET

Obtenir les métriques de police

Les métriques de police contiennent des informations telles que « Ascender », « Descender », « TypoAscender », « TypoDescender » et « UnitsPerEm ». L’API Aspose.Font pour .NET peut lire les informations sur les métriques de police à partir du fichier de police TrueType à l’aide de l’exemple de code suivant.

 1// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-.NET
 2string fileName = dataDir + "Montserrat-Regular.ttf"; //Font file name with full path
 3
 4FontDefinition fd = new FontDefinition(FontType.TTF, new FontFileDefinition("ttf", new FileSystemStreamSource(fileName)));
 5TtfFont font = Aspose.Font.Font.Open(fd) as TtfFont;
 6
 7string name = font.FontName;
 8Console.WriteLine("Font name: " + name);
 9Console.WriteLine("Glyph count: " + font.NumGlyphs);
10string metrics = string.Format(
11    "Font metrics: ascender - {0}, descender - {1}, typo ascender = {2}, typo descender = {3}, UnitsPerEm = {4}",
12    font.Metrics.Ascender, font.Metrics.Descender,
13    font.Metrics.TypoAscender, font.Metrics.TypoDescender, font.Metrics.UnitsPerEM);
14
15Console.WriteLine(metrics);
16
17//Get cmap unicode encoding table from font as object TtfCMapFormatBaseTable to access information about font glyph for symbol 'A'.
18//Also check that font has object TtfGlyfTable (table 'glyf') to access glyph.
19Aspose.Font.TtfCMapFormats.TtfCMapFormatBaseTable cmapTable = null;
20if (font.TtfTables.CMapTable != null)
21{
22    cmapTable = font.TtfTables.CMapTable.FindUnicodeTable();
23}
24if (cmapTable != null && font.TtfTables.GlyfTable != null)
25{
26    Console.WriteLine("Font cmap unicode table: PlatformID = " + cmapTable.PlatformId + ", PlatformSpecificID = " + cmapTable.PlatformSpecificId);
27
28    //Code for 'A' symbol
29    char unicode = (char)65;
30
31    //Glyph index for 'A'
32    uint glIndex = cmapTable.GetGlyphIndex(unicode);
33
34    if (glIndex != 0)
35    {
36        //Glyph for 'A'
37        Glyph glyph = font.GetGlyphById(glIndex);
38        if (glyph != null)
39        {
40            //Print glyph metrics
41            Console.WriteLine("Glyph metrics for 'A' symbol:");
42            string bbox = string.Format(
43                "Glyph BBox: Xmin = {0}, Xmax = {1}" + ", Ymin = {2}, Ymax = {3}",
44                glyph.GlyphBBox.XMin, glyph.GlyphBBox.XMax,
45                glyph.GlyphBBox.YMin, glyph.GlyphBBox.YMax);
46            Console.WriteLine(bbox);
47            Console.WriteLine("Width:" + font.Metrics.GetGlyphWidth(new GlyphUInt32Id(glIndex)));
48        }
49    }
50}

Détecter les symboles latins

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

 1// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-.NET
 2string fileName = dataDir + "Montserrat-Regular.ttf"; //Font file name with full path
 3
 4FontDefinition fd = new FontDefinition(FontType.TTF, new FontFileDefinition("ttf", new FileSystemStreamSource(fileName)));
 5TtfFont ttfFont = Aspose.Font.Font.Open(fd) as TtfFont;
 6
 7bool latinText = true;
 8
 9
10for (uint code = 65; code < 123; code++)
11{
12    GlyphId gid = ttfFont.Encoding.DecodeToGid(code);
13    if (gid == null || gid == GlyphUInt32Id.NotDefId)
14    {
15        latinText = false;
16    }
17}
18
19if (latinText)
20{
21    Console.WriteLine(string.Format("Font {0} supports latin symbols.", ttfFont.FontName));
22}
23else
24{
25    Console.WriteLine(string.Format("Latin symbols are not supported by font {0}.", ttfFont.FontName));
26}

Extraire les restrictions de licence

Utilisez l’API Aspose.Font pour .NET pour extraire les restrictions de licence des fichiers de polices. Les polices peuvent contenir des informations de licence intégrées dans l’un des modes suivants .

L’exemple de code 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.

 1// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-.NET
 2string dataDir = RunExamples.GetDataDir_Data();
 3//Font to check
 4string fileName = dataDir + "Montserrat-Regular.ttf"; //Font file name with full path
 5
 6FontDefinition fd = new FontDefinition(FontType.TTF, new FontFileDefinition("ttf", new FileSystemStreamSource(fileName)));
 7TtfFont font = Aspose.Font.Font.Open(fd) as TtfFont;
 8LicenseFlags licenseFlags = null;
 9if (font.TtfTables.Os2Table != null)
10{
11    licenseFlags = font.TtfTables.Os2Table.GetLicenseFlags();
12}
13
14if (licenseFlags == null || licenseFlags.FSTypeAbsent)
15{
16    Console.WriteLine(string.Format("Font {0} has no embedded license restrictions", font.FontName));
17}
18else
19{
20    if (licenseFlags.IsEditableEmbedding)
21    {
22        Console.WriteLine(
23            string.Format("Font {0} may be embedded, and may be temporarily loaded on other systems.", font.FontName)
24            + " In addition, editing is permitted, including ability to format new text"
25            + " using the embedded font, and changes may be saved.");
26    }
27    else if (licenseFlags.IsInstallableEmbedding)
28    {
29        Console.WriteLine(
30            string.Format("Font {0} may be embedded, and may be permanently installed", font.FontName)
31            + " for use on a remote systems, or for use by other users.");
32    }
33    else if (licenseFlags.IsPreviewAndPrintEmbedding)
34    {
35        Console.WriteLine(
36            string.Format("Font {0} may be embedded, and may be temporarily loaded", font.FontName)
37            + "  on other systems for purposes of viewing or printing the document.");
38    }
39    else if (licenseFlags.IsRestrictedLicenseEmbedding)
40    {
41        Console.WriteLine(
42            string.Format("Font {0} must not be modified, embedded or exchanged in any manner", font.FontName)
43            + " without first obtaining explicit permission of the legal owner.");
44    }
45}

Have any questions about Aspose.Font?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.