Convert CFF | .NET

The article will explain to you with the code how to transform CFF format. Aspose solution now gives you the opportunity to convert CFF to TTF, CFF to WOFF, and CFF to WOFF2.


First, add the next namespaces at the top of the head of the title:

 
using Aspose.Font.Sources;
using System;
using System.IO;

In How to convert the font into the desired format? section you can find the font conversion fundamentals.

Convert CFF to TTF

To fulfill the operation follow the next steps:

  1. Open CFF font.
  2. Notify the output settings.
  3. Run the conversion.
 
    // Open cff font
    string fontPath = Path.Combine(DataDir, "OpenSans-Regular.cff");
    FontDefinition fontDefinition = new FontDefinition(FontType.CFF, new FontFileDefinition("cff", new FileSystemStreamSource(fontPath)));
    Font font = Font.Open(fontDefinition);

    // Ttf output settings
    string outPath = Path.Combine(OutputDir, "CffToTtf_out1.ttf");
    FileStream outStream = File.Create(outPath);

    // Convert cff to ttf
    font.SaveToFormat(outStream, FontSavingFormats.TTF);

Convert CFF to WOFF

To transform Compact Font Format to Web Open Font Format you will need to follow the algorithm:

  1. Open CFF font.
  2. Place the settings for the output font stream.
  3. Process the conversion.
 
    // Open cff font
    byte[] fontMemoryData = File.ReadAllBytes(Path.Combine(DataDir, "OpenSans-Regular.cff"));
    FontDefinition fontDefinition = new FontDefinition(FontType.CFF, new FontFileDefinition("cff", new ByteContentStreamSource(fontMemoryData)));
    Font font = Font.Open(fontDefinition);

    // Woff output settings
    string outPath = Path.Combine(OutputDir, "CffToWoff_out3.woff");
    FileStream outStream = File.Create(outPath);

    // Convert cff to woff
    font.SaveToFormat(outStream, FontSavingFormats.WOFF);

Convert CFF to WOFF2

To fulfill Compact Font Format to Web Open Font Format version 2 conversion, take the three steps below:

  1. Open the CFF.
  2. Notify the output settings.
  3. Convert CFF to WOFF2 and save the result.
     
        // Open cff font
        byte[] fontMemoryData = File.ReadAllBytes(Path.Combine(DataDir, "OpenSans-Regular.cff"));
        FontDefinition fontDefinition = new FontDefinition(FontType.CFF, new FontFileDefinition("cff", new ByteContentStreamSource(fontMemoryData)));
        Font font = Font.Open(fontDefinition);
    
        // Woff2 output settings
        string outPath = Path.Combine(OutputDir, "CffToWoff2_out4.woff2");
        FileStream outStream = File.Create(outPath);
    
        // Convert cff to woff2
        font.SaveToFormat(outStream, FontSavingFormats.WOFF2);