Font Conversion. Advanced details | Java

In some cases, there is a sense to read/modify converted font data before saving the font.

Method com.aspose.font.Font.convert(FontType fontType) was designed for such cases. This method converts a font into the type specified and returns the object inherited from com.aspose.font.Font class which corresponds to FontType value, passed into convert() method.

The next table shows a map of coherence between FontType values and objects, inherited from the base com.aspose.font.Font class.

Font typeFont object
TTFcom.aspose.font.TtfFont
Type1com.aspose.font.Type1Font
CFFcom.aspose.font.CffFont
OTFcom.aspose.font.TtfFont

Use resultant font object to access/change font properties before saving resultant font or instead of saving resultant font.

At the current moment method convert() supports conversion only into TrueType font format (FontType.TTF), so it always returns the object of type TtfFont as result of conversion independently of source font used.

The following example loads 29645.cff (ABBAFL+MinionPro-Bold) in place of the original CenturyGothic input, converts it to TrueType, and appends _Converted to the font name before saving CffToTtf_out.ttf. Supply getDataDir() and getOutputDir() for your application and adapt the sibling CFF input path; the output directory must exist and be writable.

First, use the next statements:

1import java.nio.file.Paths;
2import com.aspose.font.FileSystemStreamSource;
3import com.aspose.font.Font;
4import com.aspose.font.FontDefinition;
5import com.aspose.font.FontFileDefinition;
6import com.aspose.font.FontType;
7import com.aspose.font.TtfFont;

Fulfill the next actions:

  1. Open the font.
  2. Convert the font into TrueType format.
  3. Change the name of the converted font.
  4. Notify the output settings.
  5. Save the resultant with the just changed name.
1String fontPath = Paths.get(getDataDir(), "..", "CFF", "29645.cff").normalize().toString();
2FontDefinition fontDefinition = new FontDefinition(FontType.CFF,
3        new FontFileDefinition("cff", new FileSystemStreamSource(fontPath)));
4Font font = Font.open(fontDefinition);
5
6TtfFont destFont = (TtfFont) font.convert(FontType.TTF);
7destFont.setFontName(font.getFontName() + "_Converted");
8String outPath = Paths.get(getOutputDir(), "CffToTtf_out.ttf").toString();
9destFont.save(outPath);

The full range of examples for using Aspose.Font for Java is placed in Aspose.Font.Examples.sln solution, in the java-examples folder of the Aspose.Font Documentation github repository.