Завантажити шрифт із файлу CFF | Java

Дізнайтесь уважніше про те, як завантажити шрифт, зокрема CenturyGothic, розміщений у файлі CenturyGothic.cff.


Якщо ви не читали основи завантаження Aspose.Font, перейдіть до Як завантажити шрифти? сторінка.

Спочатку давайте повідомимо оператори, які вам потрібно буде використовувати:

 1    package 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, передаючи CFF як значення FontType.
  3. Отримати автоматично розраховане значення fileExtension.
  4. Завантажте шрифт.
 1    // Construct path to the file.
 2    String 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);

Завантаження шрифту за допомогою змінної типу byte[] та використання об’єкта типу ByteContentStreamSource

Щоб завантажити шрифт таким чином, потрібно виконати наступні кроки:

  1. Побудувати шлях до файлу.
  2. Ініціалізуйте об’єкт FontDefiniton, передавши CFF як значення FontType, cff як значення fileExtension і ByteContentStreamSource об’єкт на основі масиву fontBytes.
  3. Завантажте шрифт.
 1    // Construct path to the file
 2    String 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]( https://github.com/aspose-font/Aspose.Font-Documentation/tree/master/java-examples/ src/main/java/com/aspose/font/examples), у java-examples Документації Aspose.Font.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.