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.
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”.
Fulfill the next actions:
Load the source CFF font –
- Build the absolute path to the source file using the data directory constant:This ensures the correct file location regardless of the operating system.
1string fontPath = Path.Combine(DataDir, "CenturyGothic.cff"); - Create a
FontFileDefinitionthat tells Aspose.Font the physical file format ("cff") and provides a stream source for the file:1var fontFileDef = new FontFileDefinition("cff", new FileSystemStreamSource(fontPath)); - Build a
FontDefinitionthat combines the desired font type (FontType.CFF) with the previously createdfontFileDef:1var fontDefinition = new FontDefinition(FontType.CFF, fontFileDef); - Open the font using
Font.Open(fontDefinition). The method returns a genericFontinstance that represents the loaded CFF font and can be used for further conversion steps.
- Build the absolute path to the source file using the data directory constant:
Convert the font to TrueType –
- Invoke the conversion method on the previously loaded
Fontobject:1Aspose.Font.Font convertedFont = font.Convert(FontType.TTF); - The
Convertmethod reads the source CFF data, applies the conversion algorithm, and returns a newAspose.Font.Fontinstance that internally represents a TrueType font (TtfFont). - This generic
convertedFontcan be further cast to the concreteAspose.Font.Ttf.TtfFonttype to access TrueType‑specific properties.
- Invoke the conversion method on the previously loaded
Cast the result to the concrete TrueType class and rename –
- Safely cast the generic
convertedFontto the concrete TrueType class:1Aspose.Font.Ttf.TtfFont destFont = convertedFont as Aspose.Font.Ttf.TtfFont; - Verify that the cast succeeded (
destFont != null). If the cast fails, handle the error accordingly. - Set a new name for the converted font:
1destFont.FontName = "CenturyGothic_Converted"; - This updates the internal font name metadata, which will be reflected in the saved file.
- Safely cast the generic
Prepare the output file path –
- Choose the desired output file name, e.g.,
"CffToTtf_out.ttf". - Build the full output path using the output directory constant:
1string outPath = Path.Combine(OutputDir, "CffToTtf_out.ttf"); Path.Combineensures correct path separators across platforms and concatenates the directory and file name safely.
- Choose the desired output file name, e.g.,
Save the converted TrueType font –
- Persist the renamed TrueType font to disk:
1destFont.Save(outPath); - After saving, you can optionally verify that the file was created:
1bool exists = System.IO.File.Exists(outPath); - Logging the
outPathor the existence check helps confirm successful conversion and saving.
- Persist the renamed TrueType font to disk:
1
2 // Open cff font
3 string fontPath = Path.Combine(DataDir, "CenturyGothic.cff");
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 Aspose.Font.Ttf.TtfFont
8 Aspose.Font.Ttf.TtfFont destFont = font.Convert(FontType.TTF) as Aspose.Font.Ttf.TtfFont;
9
10 // Change name of converted font
11 destFont.FontName = "CenturyGothic_Converted";
12
13 // Ttf output settings
14 string outPath = Path.Combine(OutputDir, "CffToTtf_out.ttf");
15
16 // Save resultant font with font name changed
17 destFont.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.