truetype字体的字形和指标| .NET
Contents
[
Hide
Show
]获取字体指标
字体指标包含诸如ascender
,‘descender,
typoAscender,
typodescender`和’unitsperem’之类的信息。 适用于 .NET 的 Aspose.Font API可以使用以下示例代码从TRUETYPE字体文件中读取字体指标信息。
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}
检测拉丁符号
适用于 .NET 的 Aspose.Font可让您从truetype字体文件中检测拉丁符号。这可以使用以下示例代码来实现。
- 使用fontfiledefinition加载字体文件
- 使用DecodeTogid()方法解码滑翔机
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}
提取许可限制
使用适用于 .NET 的 Aspose.Font api从字体文件中提取许可限制。字体可以在以下模式之一中具有嵌入其中的许可信息。
- ``可编辑嵌入’-字体可能被嵌入,并且可以暂时加载到其他系统上。此外,允许编辑,包括使用嵌入式字体格式化新文本的能力,并且可以保存更改。
- 可嵌入的“可安装嵌入” - 可以嵌入字体,并且可以永久安装在远程系统上,也可以被其他用户使用。
- “预览和打印嵌入” - 字体可以嵌入,并且可以暂时加载到其他系统上,以查看或打印文档。
- 不得以任何方式修改,嵌入或交换“限制嵌入” - 未经首先获得法律所有者的明确许可。
以下代码示例显示了如何以方便的形式使用对象aspose.font.ttf.licenseflags以获取有关字体许可限制(来自表OS/2的FLAG FSTYPE)的信息。
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}