Font Conversion. Advanced details | Aspose.Font for .NET

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

Method 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 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 Aspose.Font.Font class.

Font typeFont object
TTFAspose.Font.Ttf.TtfFont
Type1Aspose.Font.Type1.Type1Font
CFFAspose.Font.Cff.CffFont
OTFAspose.Font.Ttf.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 CFF/29645.cff, converts it to TrueType, and appends _Converted to the original font name before saving.

Use the Aspose.Font, Aspose.Font.Sources, Aspose.Font.Ttf, System, and System.IO namespaces. Set DataDir to the directory containing the CFF folder and OutputDir to an existing output directory.

Follow these steps:

  1. Load the source CFF font. Build the input path from DataDir, CFF, and 29645.cff. Create a FontDefinition with FontType.CFF and a FontFileDefinition that specifies the cff extension and a FileSystemStreamSource. Load it with Font.Open.
  2. Convert the font to TrueType. Call font.Convert(FontType.TTF) and cast the result to TtfFont. The example throws InvalidOperationException if the conversion does not return a TrueType font.
  3. Rename the converted font. Set destFont.FontName to the original font.FontName followed by _Converted.
  4. Prepare the output path. Combine OutputDir and CffToTtf_out.ttf with Path.Combine.
  5. Save the converted font. Call destFont.Save(outPath) to persist the renamed TrueType font. The accompanying test reopens the saved file and checks that its font name matches the converted font.
 1// Convert a CFF font to TTF and change its name before saving
 2
 3// Open the CFF font
 4string fontPath = Path.Combine(DataDir, "CFF", "29645.cff");
 5FontDefinition fontDefinition = new FontDefinition(
 6    FontType.CFF, new FontFileDefinition("cff", new FileSystemStreamSource(fontPath)));
 7Font font = Font.Open(fontDefinition);
 8
 9// Convert to TrueType and obtain the format-specific font object
10TtfFont destFont = font.Convert(FontType.TTF) as TtfFont
11    ?? throw new InvalidOperationException("Conversion did not return a TrueType font.");
12
13// Change the converted font name before saving
14destFont.FontName = font.FontName + "_Converted";
15
16// Save the renamed TrueType font
17string outPath = Path.Combine(OutputDir, "CffToTtf_out.ttf");
18destFont.Save(outPath);

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