Font Conversion. Advanced details | Java

Contents
[ ]

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 type Font object
TTF com.aspose.font.TtfFont
Type1 com.aspose.font.Type1Font
CFF com.aspose.font.CffFont
OTF com.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:

 
    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;

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.
 
    // Open cff font
    String fontPath = Paths.get(getDataDir(), "CenturyGothic.cff").toString();
    FontDefinition fontDefinition = new FontDefinition(FontType.CFF, new FontFileDefinition("cff", new FileSystemStreamSource(fontPath)));
    Font font = Font.open(fontDefinition);

    //Convert font into TrueType format and cast font returned to com.aspose.font.TtfFont
    Font destFont = (TtfFont)font.convert(FontType.TTF);

    //Change name of converted font
    destFont.setFontName("CenturyGothic_Converted");		
		
    // Output Ttf settings
    String outPath = Paths.get(getOutputDir(), "CffToTtf_out.ttf").toString();
		
    //Save the resultant font with the changed font name
    destFont.save(outPath);