Convert TTF to WOFF2 | Java
Contents
[
Hide
]
On this page you will find the example of using the Aspose solution for converting font format from TrueType
font to Web Open Font Format version 2
.
Use the following statements:
package com.aspose.font;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import com.aspose.font.ByteContentStreamSource;
import com.aspose.font.FileSystemStreamSource;
import com.aspose.font.Font;
import com.aspose.font.FontDefinition;
import com.aspose.font.FontFileDefinition;
import com.aspose.font.FontSavingFormats;
import com.aspose.font.FontType;
import com.aspose.font.TtfFont;
If you need to know the Aspose conversion fundamentals get to How to convert the font into the desired format? chapter.
Then take the next steps:
-
Open
TTF
font.// Open ttf font String fontPath = Paths.get(getDataDir(), "Montserrat-Regular.ttf").toString(); FontDefinition fontDefinition = new FontDefinition(FontType.TTF, new FontFileDefinition(new FileSystemStreamSource(fontPath))); Font font = Font.open(fontDefinition);
-
Write the output settings for the
WOFF2
format.// Woff2 output settings String outPath = Paths.get(getOutputDir(), "TtfToWoff2_out1.woff2").toString(); FileOutputStream outStream = new FileOutputStream(outPath);
-
Make conversion from
TTF
toWOFF2
and save the result using saveToFormat() method of the base abstract Font class instance.// Convert ttf to woff2 font.saveToFormat(outStream, FontSavingFormats.WOFF2);
For more examples check Aspose.Font.Examples.sln solution, in the java-examples folder of the Aspose.Font Documentation Github repository.
Also go to our free online Font Conversion application from Aspose to see the functionality.