How to load fonts? | Aspose.Font for Java
Overview
To answer the question, how to load fonts, first we need to learn that any font is physically an array of bytes in which data is represented in some format. Therefore, for correct font loading you need to know two key parameters:
- Font format. Fonts can have different formats like
TrueType,Type1, etc. - Storage, where the binary data that represents the font is being held. Each font is represented as binary data but the way this data is stored may differ. In most cases, the font is being stored as a file on a hard drive. But sometimes font binary data can be placed in a different file which is not a font itself. For example, files created by Microsoft Word or Adobe Acrobat. These are files with extensions
.docx,.pdf. They can include different fonts.
Objects and parameters required for font loading
Aspose.Font for Java gives object FontDefinition for notification of two key parameters required for font loading.
After creating and initiating FontDefinition object, you just need to call static method com.aspose.font.Font.open() and pass just initialized object FontDefinition as a parameter to load the desired font and get a reference on it.
Let’s describe it with the pseudocode:
1
2 package com.aspose.font;
3
4 import com.aspose.font.FontDefinition;
5 ...
6
7 // Initialize object FontDefinition with appropriate properties
8 FontDefinition fontDef = new FontDefinition();
9 Font desiredFont = Font.open(fontDef);The final result of the font loading is getting an object of com.aspose.font.Font type. Class com.aspose.font.Font is a base font class of com.aspose.font package for the Java library. It represents the main functionality of working with fonts.
Getting loaded a font into com.aspose.font.Font type object you will be able to render text with this font, get encoding data, and font metrics. You will also be able to modify or save this font and many more.
To initiate the FontDefinition object we need, as was mentioned before, to give it 2 font parameters: the format type and the object which describes the storage for font binary data. The first parameter is notified with FontType enumeration.
To simplify work with the second parameter, com.aspose.font offers a series of objects that gives universal access to the font binary data independently from where the font is placed.
This universal access is linked with such a thing as a byte stream. So, independently from where the font is placed - into a file on a disk, inside another file, byte array in memory - in any case, special com.aspose.font objects will provide an access to the byte stream, related to the desired font.
Let’s describe these objects:
StreamSource class. This abstract base class defines the properties and methods that give access to the font byte stream. getFontStream() method of this class returns a stream, related to binary font data. Property Offset points on the position at this stream, from where we must start to read data.
Two most used classes inherited from the base StreamSource:
- FileSystemStreamSource - designed to provide access to the stream, based on the file system. Property FileName returns the name of the font file. getFontStream() method returns a byte stream, related to this file.
- ByteContentStreamSource - provides access to the stream, based on the byte array.
How to initiate classes?
Here are examples of the initiation of these classes.
- Let’s assume that the font is in the file named Lora.ttf. In this case, to get access to the byte stream of this font we need to create the object of type FileSystemStreamSource using this single constructor:
1
2 FileSystemStreamSource fontSource = new FileSystemStreamSource("Lora.ttf");- If the font binary data is located in a binary array defined by the byte [] fontArray variable, then an object of type ByteContentStreamSource will provide an access to the font data stream, based on the fontArray variable.
To get access to the font byte stream, we have to create an object of type ByteContentStreamSource to get access to the stream, related to the font byte array using code like this:
1
2 ByteContentStreamSource fontSource = new ByteContentStreamSource(fontArray);So, to provide access to font binary data, use objects inherited from the base StreamSource class. Further, we will call such objects font stream sources.
FontFileDefinition object
Most fronts are located in regular files but at the same time, part of fonts may have different data sources. To join into one object terms file and font binary stream, com.aspose.font library provides special object FontFileDefinition.
This object has such properties:
- FileName - name of font file.
- FileExtension - extension of a font file.
- StreamSource - font stream source.
- Offset - offset inside the font byte stream.
The simplest and one of the most used constructors of this object is the constructor with the following signature: FontFileDefinition (File fontFile), use this constructor for cases when the desired font is in a file on the hard drive.
Here is an example of initiating such an object for font Montserrat which is located in the file C:\Windows\Fonts\Montserrat.ttf:
1
2 FontFileDefinition fileDef = new FontFileDefinition(new File("C:\\Windows\\Fonts\\Montserrat.ttf"));Also, FontFileDefinition can reference Montserrat font using the following constructors:
1
2 FontFileDefinition fileDef = new FontFileDefinition(new FileSystemStreamSource("C:\\Windows\\Fonts\\Montserrat.ttf"));
3
4 FontFileDefinition fileDef = new FontFileDefinition("ttf", new FileSystemStreamSource("C:\\Windows\\Fonts\\Montserrat.ttf"));Parameter
fileExtension should match the standard, for the font format, extension. For example, if the font format is TrueType, then the value of the fileExtension parameter may be ttf or ttc (if the font file is a collection of TrueType fonts).
If the font format is Embedded Open Type, then the value of fileExtension parameter should be eot.
The table below shows the most often used in Aspose.Font font file formats. There they are with the matching values that the fileExtension parameter should take.
| Font file Format | fileExtension |
|---|---|
TrueType, single font | ttf |
TrueType font collection | ttc |
Web Open Font Format | woff |
Web Open Font Format version 2.0 | woff2 |
Embedded OpenType | eot |
Adobe Type 1 font | pfa, pfb, afm, pfm |
Compact Font Format | cff, can be null |
If font data starts not from the position 0 of the byte stream, use the following constructor with the offset parameter:
1
2 FontFileDefinition(String fileExtension, StreamSource streamSource, long offset).How to initiate FontDefinition object?
Now, when you have an idea about the objects inherited from the class StreamSource and providing access to font byte stream, and about the FontFileDefinition object, we will explain to you how to initiate the FontDefinition object with these objects properly for different situations.
The common feature for all existing constructors is the parameter FontType. It describes the type of font format. As was mentioned before, apart from the FontType value, the object FontDefinition should be initialized with reference to font binary data.
In this case, the next parameters of FontDefinition constructors can be used:
- fontName;
- fileExtension;
- object of the StreamSource type;
- object of the FontFileDefinition type.
You may have a question. Why would we pass the parameter fileExtension to the FontDefinition or the FontFileDefinition object, when we always pass the parameter FontType which seems to coincide by its value with fileExtension? The problem is that FontType doesn’t always coincide with the value of fileExtension. FontType defines the common font format but not the font format of a specific file. Some fonts of one format can have different file formats. For example, the value FontType.TTF defines the “TrueType” format. But at the same time “TrueType” includes a few font formats and there are font files with extensions “ttf”, “eot”, “ttc”, etc. in it. And if, for example, for the font in “EOT” format we pass to FontDefinition only the FontType.TTF value, then how will this object understand that the font belongs to “EOT” format and not to “TTF”?
In the same way, the value of FontType.Type1 is the common definition for fonts of this format. At the same time, font files of “Type1” format have extensions “.pfa”, “.pfb”, “.afm”, “.pfm”. So, you can’t properly define font file format depending only on FonType.Type1 value. Therefore, to define the font format correctly we need to specify the parameter FontType with the value of the parameter fileExtension.
Below you can learn examples of FontDefinition object initialization and following font loading for different cases.
In all the cases the final font loading result is put into the variable of com.aspose.font.Font type. This type is the basic font class of com.aspose.font and objects of this type provide common base functionality to work with fonts.
Loading the font into this object you will be able to render text with this font, get coding information, font metrics, etc.
Examples of how to load fonts
Let’s have as an example, loading font Montserrat from the file Montserrat-Regular.ttf.
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;You can load this font using the FontDefinition and FontFileDefinition objects in a few different ways:
Loading with the help of java.io.File object
To fulfil loading do the next:
- Construct path to the file.
- Initiate the FontDefinition 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 FontFileDefinition object, fileExtension value is calculated automatically from FontFileDefinition fields.
8 FontDefinition fontDef = new FontDefinition(FontType.TTF, fileDef);
9
10 // Load the font
11 Font font = Font.open(fontDef);Loading with the help of FileSystemStreamSource type object
Take the next steps to fulfil the operation:
- Construct path to the file.
- Initiate the FontDefinition 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);Font loading without FontFileDefinition object, with passing FileSystemStreamSource directly to FontDefinition
The next actions have to be taken for loading the font this way:
- Construct the path to the file.
- Initiate the FontDefinition object passing “TTF” as FontType value, “ttf” as fileExtension value, and the FileSystemStreamSource object. The parameter fileExtension here is not a duplicate value for the 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);Font loading with the byte[] type variable and with using ByteContentStreamSource type object
To load a font from the byte array you need to:
- Construct path to the file.
- Load font binary data into the byte array
- Initialize the FontDefinition object passing “TTF” as FontType value, “ttf” as fileExtension value and the ByteContentStreamSource object based on the 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);For getting more examples of using the Aspose.Font go to Aspose.Font.Examples.sln solution, in the java-examples of the Aspose.Font Documentation
If you have any problems or questions left, you may post them at the Aspose.Font.Product Family section of the Free Support Forum and within a few hours our support team will clear everything up for you.