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 type | Font object |
|---|---|
TTF | Aspose.Font.Ttf.TtfFont |
Type1 | Aspose.Font.Type1.Type1Font |
CFF | Aspose.Font.Cff.CffFont |
OTF | Aspose.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:
- Load the source CFF font. Build the input path from
DataDir,CFF, and29645.cff. Create aFontDefinitionwithFontType.CFFand aFontFileDefinitionthat specifies thecffextension and aFileSystemStreamSource. Load it withFont.Open. - Convert the font to TrueType. Call
font.Convert(FontType.TTF)and cast the result toTtfFont. The example throwsInvalidOperationExceptionif the conversion does not return a TrueType font. - Rename the converted font. Set
destFont.FontNameto the originalfont.FontNamefollowed by_Converted. - Prepare the output path. Combine
OutputDirandCffToTtf_out.ttfwithPath.Combine. - 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.