Glyphen und Metriken von TrueType-Schriftarten | .NETTO

Rufen Sie Schriftmetriken ab

Schriftartmetriken enthalten Informationen wie „Ascender“, „Descender“, „TypoAscender“, „TypoDescender“ und „UnitsPerEm“. Aspose.Font für .NET API kann mithilfe des folgenden Beispielcodes die Schriftartmetrikinformationen aus der TrueType-Schriftartdatei lesen.

 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}

Erkennen Sie lateinische Symbole

Mit Aspose.Font für .NET können Sie lateinische Symbole aus TrueType-Schriftdateien erkennen. Dies kann mit dem folgenden Beispielcode erreicht werden.

 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}

Lizenzbeschränkungen extrahieren

Verwenden Sie die Aspose.Font for .NET-API, um Lizenzbeschränkungen aus Schriftartdateien zu extrahieren. In Schriftarten können Lizenzinformationen in einem der folgenden Modi eingebettet sein.

Das folgende Codebeispiel zeigt, wie Sie ein Objekt Aspose.Font.Ttf.LicenseFlags verwenden, um Informationen zu Schriftartlizenzeinschränkungen (Flag fsType aus Tabelle OS/2) in praktischer Form abzurufen.

 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.