从TTF文件加载字体 | Java

在这里,您将找到有关如何从文件中加载字体的四个示例。


确保您已经在 如何加载字体上学习了加载基础知识?页面。

使用下一个语句:

 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;

使用filesystemstreamsource对象从文件加载

采取下一步完成操作:

  1. 构建文件的路径。
  2. 启动 fontdefiniton对象。
  3. fileextension设置为ttf
  4. 加载字体。
 1    //构造文件的路径
 2    字符串fontpath = paths.get(getDatadir(),“ montserrat-regular.ttf)toString();
 3
 4    // Initialize FontDefinition object passing TTF as FontType value and using FontFileDefinition
 5    FontFileDefinition fileDef = new FontFileDefinition("ttf", new FileSystemStreamSource(fontPath));
 6
 7    // Based on FileSystemStreamSource object, set fileExtension to "ttf"
 8    FontDefinition fontDef = new FontDefinition(FontType.TTF, fileDef);
 9
10    // Load the font
11    Font font = Font.open(fontDef);

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

要实现加载,请执行下一个:

  1. 构建文件的路径。
  2. 启动 fontdefiniton对象传递ttffonttype值。
  3. 获取自动计算的值 fileextension
  4. 加载字体。
 1    //构造文件的路径
 2    字符串fontpath = paths.get(getDatadir(),“ montserrat-regular.ttf)toString();
 3
 4    // Initialize FontDefinition object passing TTF 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.TTF, fileDef);
 9
10    // Load the font 
11    Font font = Font.open(fontDef);

从文件中加载不包括初始化链中的fontfiledefinition对象

必须采取下一个操作以通过这种方式加载字体:

  1. 构建文件的路径。
  2. 启动 fontdefiniton对象传递ttf作为 fonttypevalue,ttf as fileextension值(9)值和 FilesystemStreamSource对象。参数 fileextension这不是参数 fonttype的重复值。
  3. 加载字体。
 1    //构造文件的路径
 2    字符串fontpath = paths.get(getDatadir(),“ montserrat-regular.ttf)toString();
 3
 4    // Initialize FontDefinition object passing TTF as FontType value, "ttf" as fileExtension value, 
 5    // and FileSystemStreamSource object. Parameter 'fileExtension' here is not duplicate value 
 6    // for parameter 'FontType' and it's needed for correct font format detection
 7    FontDefinition fontDef = new FontDefinition(FontType.TTF, "ttf", new FileSystemStreamSource(fontPath));
 8
 9    // Load the font
10    Font font = Font.open(fontDef);   

从字节数组加载字体

从字节数组加载字体:

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

您可以在 aspose.font.examples.sln解决方案中,在 *java-examples中获得有关如何使用aspose.font的示例。

Have any questions about Aspose.Font?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.