Charger une police à partir d'un fichier CFF | C++

Sur cette page, nous verrons des exemples de chargement de la police CenturyGothic placée dans le fichier CenturyGothic.cff.


Si vous n’avez pas lu les principes fondamentaux du chargement d’Aspose.Font, accédez à Comment charger les polices ? page.

Vous devez d’abord notifier les espaces de noms suivants en tête du fichier :

1using namespace Aspose::Font;
2using namespace Aspose::Font::Sources;
3using namespace System.IO;

Chargement à partir du fichier à l’aide de l’objet FileInfo

Suivez l’algorithme pour effectuer le chargement de la police :

  1. Construisez le chemin d’accès au fichier.
  2. Initiez l’objet FontDefiniton en passant CFF comme valeur FontType.
  3. Obtenez la valeur calculée automatiquement FileExtension.
  4. Chargez la police.
 1    // Construct path to the file.
 2    System::String fontPath = System::IO::Path::Combine(get_DataDir(), u"CenturyGothic.cff");
 3
 4    // Initialize FontDefinition object passing CFF as FontType value and using FontFileDefinition
 5    // based on FileInfo object, fileExtension value is calculated automatically from FileInfo fields.
 6    System::SharedPtr<FontFileDefinition> fileDef = System::MakeObject<FontFileDefinition>(System::MakeObject<System::IO::FileInfo>(fontPath));
 7    System::SharedPtr<FontDefinition> fontDef = System::MakeObject<FontDefinition>(Aspose::Font::FontType::CFF, fileDef);
 8
 9    // Load the font 
10    System::SharedPtr<Aspose::Font::Font> font = Aspose::Font::Font::Open(fontDef);

Chargement de police avec la variable de type byte[] et avec l’utilisation de l’objet de type ByteContentStreamSource

Pour charger la police de cette façon, vous devez suivre les étapes suivantes :

  1. Construisez le chemin d’accès au fichier.
  2. Initialisez l’objet FontDefiniton en passant CFF comme valeur FontType, cff comme valeur FileExtension et ByteContentStreamSource objet basé sur le tableau fontBytes.
  3. Chargez la police.
 1    // Construct path to the file
 2    System::String fontPath = System::IO::Path::Combine(get_DataDir(), u"CenturyGothic.cff");
 3
 4    // Load font binary data into byte array
 5    System::ArrayPtr<uint8_t> fontBytes = System::IO::File::ReadAllBytes(fontPath);
 6    
 7    // Initialize FontDefinition object  passing CFF as FontType value, "cff" as fileExtension value, 
 8    // and ByteContentStreamSource object based on fontBytes array
 9    System::SharedPtr<FontDefinition> fontDef = System::MakeObject<FontDefinition>(Aspose::Font::FontType::CFF, u"ttf", System::MakeObject<ByteContentStreamSource>(fontBytes));
10    
11    // Load font and print results
12    System::SharedPtr<Aspose::Font::Font> font = Aspose::Font::Font::Open(fontDef);

More examples on how to use Aspose.Font are in Aspose.Font.Examples.CPP.sln solution, in the cpp-examples of the Aspose.Font Documentation.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.