Load a font from TTF file | Java
Contents
[
Hide
Show
]Here you will find four examples on how to load a font from the file Montserrat-Regular.ttf fulfilled in different ways.
Make sure you have learned the loading fundamentals at the How to load fonts? page first.
Use the next statements:
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;
Loading from the file using FileSystemStreamSource object
Take the next steps to fulfil the operation:
- Construct path to the file.
- Initiate FontDefiniton object.
- Set
fileExtension to
ttf
. - Load the font.
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);
Loading from the file using java.io.File object
To fulfil loading do the next:
- Construct path to the file.
- Initiate
FontDefiniton object passing
TTF
as FontType value. - Get automatically calculated value fileExtension.
- Load the font.
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);
Loading from the file excluding FontFileDefinition object from initialization chain
The next actions have to be taken to load the font this way:
- Construct path to the file.
- Initiate
FontDefiniton object passing
TTF
as FontType value,ttf
as fileExtension value and FileSystemStreamSource object. Parameter fileExtension here is not a duplicate value for parameter FontType. - Load the font.
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);
Loading the font from the byte array
To load the font from the byte array:
- Construct path to the file.
- Load font binary data into the byte array
- Initialize
FontDefiniton object passing
TTF
as FontType value,ttf
as fileExtension value, and ByteContentStreamSource object based on fontBytes array. - Load the font.
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);
You can get examples on how to use Aspose.Font in Aspose.Font.Examples.sln solution, in the java-examples of the Aspose.Font Documentation.