从CFF文件加载字体|爪哇

仔细观察如何在File CenturyGothic.cff中放置在centurygothic中的字体。


如果您没有阅读Aspose.font加载基本面,请转到 如何加载字体?页。

首先,让我们通知您需要使用的语句:

 1    com.aspose.font;
 2
 3    import java.io.File;
 4    import java.io.IOException;
 5    import java.nio.file.Files;
 6    import java.nio.file.Paths;
 7
 8    import com.aspose.font.ByteContentStreamSource;
 9    import com.aspose.font.FileSystemStreamSource;
10    import com.aspose.font.Font;
11    import com.aspose.font.FontDefinition;
12    import com.aspose.font.FontFileDefinition;
13    import com.aspose.font.FontType;

使用java.io.file对象从文件加载

遵循该算法来实现字体加载:

  1. 构建文件的路径。
  2. 启动 fontdefiniton对象传递cfffonttype值。
  3. 获取自动计算的值 fileextension
  4. 加载字体。
 1    //构造文件的路径。
 2    字符串fontpath = paths.get(getDatadir(),“ centurygothic.cff)toString();
 3	
 4    // Initialize FontDefinition object passing CFF as FontType value and using FontFileDefinition
 5    FontFileDefinition fileDef = new FontFileDefinition(new File(fontPath));
 6
 7    // Based on FileInfo object, fileExtension value is calculated automatically from FileInfo fields.
 8    FontDefinition fontDef = new FontDefinition(FontType.CFF, fileDef);
 9
10    // Load the font
11    Font font = Font.open(fontDef);

字体加载字节[]类型变量以及使用ByteContentsTreamSource类型对象

要通过这种方式加载字体,您需要采取以下步骤:

  1. 构建文件的路径。
  2. 初始化 fontdefiniton对象将cff作为 fonttype值,cff as fileextension值(7)和 bytecontentstreamsource基于fontbytes array array array array上。
  3. 加载字体。
 1    //构造文件的路径
 2    字符串fontpath = paths.get(getDatadir(),“ centurygothic.cff)toString();
 3
 4    // Load font binary data into byte array
 5    byte[] fontBytes = Files.readAllBytes(Paths.get(fontPath));
 6
 7    // Initialize FontDefinition object  passing CFF as FontType value, "cff" as fileExtension value, 
 8    // and ByteContentStreamSource object based on fontBytes array
 9    FontDefinition fontDef = new FontDefinition(FontType.CFF, "ttf", new ByteContentStreamSource(fontBytes));
10
11    // Load the font
12    Font font = Font.open(fontDef);

有关如何使用 Aspose.Font 的更多示例,请参阅 Aspose.Font.Examples.sln 解决方案Aspose.Font 文档

Have any questions about Aspose.Font?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.