WOFF (Web Open Font Format) is basically a TTF (True Type Font) but it contains format-specific compression and additional XML metadata. WOFF2 is an extended version of WOFF. It offers more compression as compared to WOFF. Both WOFF and WOFF2 are used on webpages. On the other hand, TTF (True Type Font) works fine on all digital platforms and gives excellent quality on-screen and in printed documents. In certain cases, we may need to convert true-type fonts into Web fonts programmatically in Java applications. In this article, we will learn how to convert TTF to WOFF and WOFF2 using Java.
The following topics shall be covered in this article:
- Java API to Convert TTF to WOFF & WOFF2
- Convert TTF to WOFF using Java
- Convert TTF to WOFF2 in using Java
Java API to Convert TTF to WOFF & WOFF2
For converting TTF to WOFF, we will be using the Aspose.Font for Java API. It provides the FontFileDefinition class that represents Font file definition. It also provides FontDefinition class that represents Font file set definition and provides basic details of the Font, such as name, type, etc. The Open() method of this class returns the FontDefinition for font file and font type. We can convert the loaded font to other formats using the SaveToFormat() method of the Font class. Moreover, the API allows loading, saving, and extracting information from supported font types.
Please either download the JAR of the API or add the following pom.xml configuration in a Maven-based Java application.
<repository> <id>AsposeJavaAPI</id> <name>Aspose Java API</name> <url>https://repository.aspose.com/repo/</url> </repository>
<dependency> <groupId>com.aspose</groupId> <artifactId>aspose-font</artifactId> <version>21.12</version> <type>pom</type> </dependency>
Convert TTF to WOFF using Java
We can easily convert True Type Fonts (TTF) to Web Open Font Format (WOFF) by following the steps given below:
- Firstly, load the font file using the FileSystemStreamSource class.
- Next, create an instance of the FontFileDefinition class with the source file object as an argument.
- Then, initialize the FontDefinition class object with Font type as TTF and FontFileDefinition object.
- After that, open the font using the Font.Open() method with the FontDefinition object as an argument.
- Finally, call the SaveToFormat() method to save it. It takes the output file path and FontSavingFormats to WOFF as arguments.
The following code sample demonstrates how to convert TTF to WOFF using Java.
Convert TTF to WOFF2 using Java
WOFF2 is an updated version of WOFF. It offers more compression due to which it is smaller in file size and provides better performance for modern browsers. We can also convert TTF to WOFF2 by following the steps mentioned earlier. However, we just need to set the FontSavingFormats as WOFF2 in the last step.
The following code sample demonstrates how to convert TTF to WOFF2 using Java.
Get a Free License
You can get a free temporary license to try the library without evaluation limitations.
In this article, we have learned how to convert True Type Fonts to Web Open Fonts in Java. We have also seen how to save TTF as WOFF2 programmatically. Besides, you can learn more about Aspose.Font for Java API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.