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

Тут ви знайдете чотири приклади того, як завантажити шрифт із файлу Montserrat-Regular.ttf, виконаного різними способами.


Переконайтеся, що ви спочатку вивчили основи завантаження на сторінці Як завантажувати шрифти?.

Використовуйте наступні твердження:

 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;

Завантаження з файлу за допомогою об’єкта FileSystemStreamSource

Виконайте наступні кроки для виконання операції:

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

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.