字体转换。高级详细信息| .NET

在某些情况下,在保存字体之前,可以读取/修改转换的字体数据的感觉。

方法 aspose.font.font.convert(fonttype fonttype)是为这种情况设计的。此方法将字体转换为指定类型的字体和 返回从 aspose.font.font类继承的对象的对应的对象,该类别对应于 fonttype值,将其传递到 convert()方法中。

下表显示了 fonttype值和对象之间的相干性图,该值是从基础 aspose.font.font.font类中继承的。

字体类型字体对象
TTFAspose.Font.Ttf.TtfFont
Type1Aspose.Font.Type1.Type1Font
CFFAspose.Font.Cff.CffFont
OTFAspose.Font.Ttf.TtfFont

在保存结果字体或保存结果字体之前,请使用结果字体对象访问/更改字体属性。

在当前时刻方法 convert()仅支持转换为trueType字体格式(fonttype.ttf),因此它始终返回 类型 ttffont类型的对象是由所使用的源字体独立于转换的结果。

下一个代码片段从磁盘加载CFF字体CenturyGothic,将其转换为TrueType格式,并将转换后的字体的名称更改为“CenturyGothic_Converted”。

执行下一个动作:

  1. 加载源 CFF 字体
1string fontPath = Path.Combine(DataDir, "CenturyGothic.cff");

这样可以确保无论使用哪种操作系统,文件位置都正确。

1var fontFileDef = new FontFileDefinition("cff", new FileSystemStreamSource(fontPath));
1var fontDefinition = new FontDefinition(FontType.CFF, fontFileDef);
  1. 将字体转换为 TrueType
1Aspose.Font.Font convertedFont = font.Convert(FontType.TTF);
  1. 将结果转换为具体的 TrueType 类并重命名
1Aspose.Font.Ttf.TtfFont destFont = convertedFont as Aspose.Font.Ttf.TtfFont;
1destFont.FontName = "CenturyGothic_Converted";
  1. 准备输出文件路径
1string outPath = Path.Combine(OutputDir, "CffToTtf_out.ttf");
  1. 保存转换后的 TrueType 字体
1destFont.Save(outPath);
1bool exist = System.IO.File.Exists(outPath);
 1 
 2    // Open cff font
 3    string fontPath = Path.Combine(DataDir, "CenturyGothic.cff");
 4    FontDefinition fontDefinition = new FontDefinition(FontType.CFF, new FontFileDefinition("cff", new FileSystemStreamSource(fontPath)));
 5    Font font = Font.Open(fontDefinition);
 6
 7    // Convert font into TrueType format and cast font returned to Aspose.Font.Ttf.TtfFont
 8    Aspose.Font.Ttf.TtfFont destFont = font.Convert(FontType.TTF) as Aspose.Font.Ttf.TtfFont;
 9
10    // Change name of converted font
11    destFont.FontName = "CenturyGothic_Converted";
12
13    // Ttf output settings
14    string outPath = Path.Combine(OutputDir, "CffToTtf_out.ttf");
15
16    // Save resultant font with font name changed
17    destFont.Save(outPath);

使用aspose.font for.net的完整示例放在 aspose.font.examples.sln解决方案中,在 net-examples aspose.font文档文件夹中。

Have any questions about Aspose.Font?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.