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.

Next code snippet loads CFF font CenturyGothic from disk, converts it into TrueType format, and changes the name of the converted font to “CenturyGothic_Converted”.

First, use the next 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;

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.
 1 
 2    // Open cff font
 3    String fontPath = Paths.get(getDataDir(), "CenturyGothic.cff").toString();
 4    FontDefinition fontDefinition = new FontDefinition(FontType.CFF, new FontFileDefinition("cff", new FileSystemStreamSource(fontPath)));
 5    Font font = Font.open(fontDefinition);
 6
 7    //Convert font into TrueType format and cast font returned to com.aspose.font.TtfFont
 8    Font destFont = (TtfFont)font.convert(FontType.TTF);
 9
10    //Change name of converted font
11    destFont.setFontName("CenturyGothic_Converted");		
12		
13    // Output Ttf settings
14    String outPath = Paths.get(getOutputDir(), "CffToTtf_out.ttf").toString();
15		
16    //Save the resultant font with the changed font name
17    destFont.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.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.