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

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

Aspose.Font.Font.Convert(FontType fontType) 方法专为此类情况而设计。此方法将字体转换为指定的类型,并返回继承自 Aspose.Font.Font 类的对象,该对象对应于传递给 Convert() 方法的 FontType 方法的 FontType 值。

下表显示了 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文档文件夹中。