Convert Type1 | API Solution for Java
In this article, you will see how to convert Type 1
format. Aspose.Font for Java offers you solutions for conversion Type 1
to TTF
, Type 1
to WOFF
, and Type 1
to WOFF2
.
For all three conversions you need to use the following statements:
1
2 package com.aspose.font;
3
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.nio.file.Files;
7 import java.nio.file.Paths;
8
9 import com.aspose.font.ByteContentStreamSource;
10 import com.aspose.font.FileSystemStreamSource;
11 import com.aspose.font.Font;
12 import com.aspose.font.FontDefinition;
13 import com.aspose.font.FontFileDefinition;
14 import com.aspose.font.FontSavingFormats;
15 import com.aspose.font.FontType;
16 import com.aspose.font.TtfFont;
Reach How to convert the font into the desired format? chapter to see about the Aspose conversion fundamentals.
Convert Type1 to TTF
Fulfill the actions written below to tranform Type one
to True Type Font
:
- Open
Type1
font. - Apply the output settings for the
Type1
font. - Fulfill the conversion.
1
2 // Open Type 1 font
3 String fontPath = Paths.get(getDataDir(),"Courier.pfb").toString();
4 FontDefinition fontDefinition = new FontDefinition(FontType.Type1, new FontFileDefinition("pfb", new FileSystemStreamSource(fontPath)));
5 Font font = Font.open(fontDefinition);
6
7 // Ttf output settings
8 String outPath = Paths.get(getOutputDir(), "Type1ToTtf_out1.ttf").toString();
9 FileOutputStream outStream = new FileOutputStream(outPath);
10
11 // Convert type1 to ttf
12 font.saveToFormat(outStream, FontSavingFormats.TTF);
Convert font formats from Type 1 to Woff
For this you will need to take the next steps:
- Open
Type1
font. - Add the output settings.
- Fulfill the conversion.
1
2 // Open Type 1 font
3 byte[] fontMemoryData = Files.readAllBytes(Paths.get(getDataDir(), "Courier.pfb"));
4 FontDefinition fontDefinition = new FontDefinition(FontType.Type1, new FontFileDefinition("pfb", new ByteContentStreamSource(fontMemoryData)));
5 Font font = Font.open(fontDefinition);
6
7 // Woff output settings
8 String outPath = Paths.get(getOutputDir(), "Type1ToWoff_out3.ttf").toString();
9 FileOutputStream outStream = new FileOutputStream(outPath);
10
11 // Convert type1 to woff
12 font.saveToFormat(outStream, FontSavingFormats.WOFF);
Convert Type 1 to WOFF2
Follow the written below algorithm to convert the fonts:
- Open
Type1
font. - Notify the next output settings.
- Fulfill the conversion.
1
2 // Open Type 1 font
3 byte[] fontMemoryData = Files.readAllBytes(Paths.get(getDataDir(), "Courier.pfb"));
4 FontDefinition fontDefinition = new FontDefinition(FontType.Type1, new FontFileDefinition("pfb", new ByteContentStreamSource(fontMemoryData)));
5 Font font = Font.open(fontDefinition);
6
7 // Woff2 output settings
8 String outPath = Paths.get(getOutputDir(), "Type1ToWoff2_out4.ttf").toString();
9 FileOutputStream outStream = new FileOutputStream(outPath);
10
11 // Convert type1 to woff2
12 font.saveToFormat(outStream, FontSavingFormats.WOFF2);
For getting more examples of using the solution go to the Aspose.Font.Examples.sln solution, in the java-examples folder of the Aspose.Font Documentation Github repository.
Aspose also offers you a free online Font Conversion application where you can try the existing functionality.