Load font files from a byte array

C++ API Solution to load TTF, WOFF, EOT, Type 1, and CFF fonts from a MemoryStream.

 

Aspose.Font API Solution provides you with extensive capabilities for working with fonts, including conversion, glyph manipulation, Latin symbol detection, and many more. But the first step in font manipulation is loading the font files to use them later.

This article outlines how to load font files from streams, but for more detailed information on font loading, refer to the article How to Load Files? . There you will find numerous C++ code examples and understand the objects and parameters necessary for font loading.

The API supports several font formats, including TrueType (single font), TrueType (font collection), Web Open Font Format, Web Open Font Format version 2.0, Embedded OpenType, Adobe Type 1 font (pfa, pfb, afm, pfm), and Compact Font Format. The code snippets provided demonstrate how to load a True Type Font (TTF) file from a stream.

As we can learn from Wikipedia TTF stands for TrueType Font and is a file format for scalable computer fonts. It was originally developed by Apple Computer, but is now widely used on both Mac and Windows operating systems. TTF files contain instructions for how to render each character of a font at different font sizes and styles, and they can be easily resized and used in different applications without losing quality. TTF fonts are widely used for digital typography and are commonly used in web design, word processing, and desktop publishing.

To load fonts, you will need the Aspose.Font for C++ API, which is a powerful, easy-to-use document manipulation and conversion API for the C++ platform. To get the API, open the NuGet package manager and search for Aspose.Font, then install it. You can also use the following command in the Package Manager Console.

Package Manager Console Command


    PM> Install-Package Aspose.Font

Load TTF from byte array/MemoryStream using C++

This code snippet is written in C++ and demonstrates how to load a TrueType Font (TTF) file using the Aspose.Font API. The code performs the following steps:

  1. Construct the path to the font file. Here, DataDir is a predefined variable containing the path to the directory where the font file Montserrat-Regular.ttf is located. The Path.Combine method combines the two paths to create the full path to the font file.
  2. Load the font binary data into a byte array. The File.ReadAllBytes() Method reads the contents of the font file and returns the data as a byte array.
  3. Initialize a FontDefinition Object. FontType.TTF indicates that the font file is a TrueType Font(.ttf) and the new ByteContentStreamSource (fontBytes) creates a ByteContentStreamSource object based on the font binary data stored in the fontBytes array.
  4. Load the font. The Font.Open() Method takes the FontDefinition object as a parameter and returns the loaded font, which can then be used for further manipulations.

C++ Code for loading fonts from the stream

    using Aspose::Font;
    using Aspose::Font::Sources;
    using Aspose::Font::Ttf;
     // Construct the path to the file
    System::String fontPath = System::IO::Path::Combine(get_DataDir(), u"Montserrat-Regular.ttf");

	// Load font binary data into the byte array
    System::ArrayPtr<uint8_t> fontBytes = System::IO::File::ReadAllBytes(fontPath);
    
    // Initialize the FontDefinition object  passing TTF as the FontType value, "ttf" as the fileExtension value, 
    // and ByteContentStreamSource object based on the fontBytes array
    System::SharedPtr<FontDefinition> fontDef = System::MakeObject<FontDefinition (Aspose::Font::FontType::TTF, u"ttf", System::MakeObject<ByteContentStreamSource>(fontBytes));

     // Load the font
    System::SharedPtr<Aspose::Font::Font> font = Aspose::Font::Font::Open(fontDef);



FAQ

1. What are the parameters to load fonts programmatically?

For correct font loading, you need to know two key parameters. They are font format and storage.

2. Where are fonts located on my computer?

To see the fonts available on your computer go to the C:>Windows>Fonts directory. There you will be able to manage your fonts.

3. How to load a font from a stream?

Set the path array using FontFileDefinition and create the object by means of Open() Method. Specify the font format. Finish loading using Aspose.Font.Font.Open() Method.