Load font files from disk

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

 

Aspose.Font API offers a comprehensive set of functions for font management, including conversion, manipulation of glyphs, Latin symbol detection, and more. A crucial step in font manipulation is loading font files, which can be used for further operations.

This page explains how to load font files from a disk, but for a more deep understanding of the functionality, check out the article How to Load Fonts . It includes numerous C++ code examples and details on the required objects and parameters for font loading.

Aspose.Font API supports the following font formats: 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), SVG and Compact Font Format. The attached code snippet demonstrates loading a font collection (TTF) from a disk.

To load fonts we need:

  • Aspose.Font for C++ API which is a feature-rich, powerful and easy-to-use document manipulation and conversion API.

  • Open the NuGet package manager, and search for Aspose.Font and install. You may also use the following command from the Package Manager Console.

Package Manager Console Command


    PM> Install-Package Aspose.Font

Load a TrueType Font from a Disk using Aspose.Font for C++ API

TrueType fonts are stored on disks as files and Aspose.Font for C++ API can be utilized to load them and read font information. This code demonstrates how to load a TrueType font file from a disk and read font information from it. The next steps are taken:

  1. Create an instance of the FontDefinition Class.
  2. Specify FontType as TTF and FontFileDefinition as .ttf.
  3. Declare a TtfFont object and open the font file from the FontDefinition object created in the previous step.
  4. Declare a TtfFont object named ttfFont and open the font file from the created earlier FontDefinition object. The Open() Method of the Font Class is used to open the font file. Then the returned object is cast to the TtfFont type. This allows the code to access the font information and perform operations on it.

C++ Code for loading fonts from the disc

    using Aspose::Font;
    using Aspose::Font::Sources;
    using Aspose::Font::Ttf;
    System::String fileName = dataDir + u"Montserrat-Regular.ttf";
    //Font file name with full path
    
    System::SharedPtr<FontDefinition> fd = System::MakeObject<FontDefinition>(Aspose::Font::FontType::TTF
    System::MakeObject<FontFileDefinition>(u"ttf", System::MakeObject<FileSystemStreamSource>(fileName)));
    System::SharedPtr<TtfFont> ttfFont = System::DynamicCast_noexcept<Aspose::Font::Ttf::TtfFont>(Aspose::Font::Font::Open(fd));



FAQ

1. How to load a font from a disc?

Set the path to the documents directory. Define the font using objects of the FontFileDefinition Class and load the font by means of the Aspose.Font.Font.Open() Method.

2. How do I load fonts on a PC?

Download font files or archives to your PC. Unpack the archive. Right-click on the font file to choose the Open option. Choose the Install option and as soon as installed the font is ready to use.

3. How do I import and use fonts?

If using windows, go to the C:>Windows>Fonts directory. To use the loaded font you need to copy and paste the font file to this folder.