Charger une police à partir d'un fichier TTF | Java

Vous trouverez ici quatre exemples sur la façon de charger une police à partir du fichier Montserrat-Regular.ttf réalisé de différentes manières.


Assurez-vous d’abord d’avoir appris les bases du chargement sur la page Comment charger les polices ?.

Utilisez les instructions suivantes :

 1    package com.aspose.font;
 2
 3    import java.io.File;
 4    import java.io.IOException;
 5    import java.nio.file.Files;
 6    import java.nio.file.Paths;
 7
 8    import com.aspose.font.ByteContentStreamSource;
 9    import com.aspose.font.FileSystemStreamSource;
10    import com.aspose.font.Font;
11    import com.aspose.font.FontDefinition;
12    import com.aspose.font.FontFileDefinition;
13    import com.aspose.font.FontType;

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

Suivez les étapes suivantes pour réaliser l’opération :

  1. Construisez le chemin d’accès au fichier.
  2. Initiez l’objet FontDefiniton.
  3. Définissez fileExtension sur ttf.
  4. Chargez la police.
 1    // Construct path to the file
 2    String fontPath = Paths.get(getDataDir(), "Montserrat-Regular.ttf").toString();
 3
 4    // Initialize FontDefinition object passing TTF as FontType value and using FontFileDefinition
 5    FontFileDefinition fileDef = new FontFileDefinition("ttf", new FileSystemStreamSource(fontPath));
 6
 7    // Based on FileSystemStreamSource object, set fileExtension to "ttf"
 8    FontDefinition fontDef = new FontDefinition(FontType.TTF, fileDef);
 9
10    // Load the font
11    Font font = Font.open(fontDef);

Chargement à partir du fichier à l’aide de l’objet java.io.File

Pour effectuer le chargement, procédez comme suit :

  1. Construisez le chemin d’accès au fichier.
  2. Initiez l’objet FontDefiniton en passant TTF comme valeur FontType.
  3. Obtenez la valeur calculée automatiquement fileExtension.
  4. Chargez la police.
 1    // Construct path to the file
 2    String fontPath = Paths.get(getDataDir(), "Montserrat-Regular.ttf").toString();
 3
 4    // Initialize FontDefinition object passing TTF as FontType value and using FontFileDefinition
 5    FontFileDefinition fileDef = new FontFileDefinition(new File(fontPath));
 6
 7    // Based on FileInfo object, fileExtension value is calculated automatically from FileInfo fields.
 8    FontDefinition fontDef = new FontDefinition(FontType.TTF, fileDef);
 9
10    // Load the font 
11    Font font = Font.open(fontDef);

Chargement à partir du fichier excluant l’objet FontFileDefinition de la chaîne d’initialisation

Les actions suivantes doivent être entreprises pour charger la police de cette façon :

  1. Construisez le chemin d’accès au fichier.
  2. Initiez l’objet FontDefiniton en passant TTF comme valeur FontType, ttf comme valeur fileExtension et l’objet FileSystemStreamSource . Le paramètre fileExtension n’est pas ici une valeur en double pour le paramètre FontType.
  3. Chargez la police.
 1    // Construct path to the file
 2    String fontPath = Paths.get(getDataDir(), "Montserrat-Regular.ttf").toString();
 3
 4    // Initialize FontDefinition object passing TTF as FontType value, "ttf" as fileExtension value, 
 5    // and FileSystemStreamSource object. Parameter 'fileExtension' here is not duplicate value 
 6    // for parameter 'FontType' and it's needed for correct font format detection
 7    FontDefinition fontDef = new FontDefinition(FontType.TTF, "ttf", new FileSystemStreamSource(fontPath));
 8
 9    // Load the font
10    Font font = Font.open(fontDef);   

Chargement de la police à partir du tableau d’octets

Pour charger la police à partir du tableau d’octets :

  1. Construisez le chemin d’accès au fichier.
  2. Chargez les données binaires de la police dans le tableau d’octets
  3. Initialisez l’objet FontDefiniton en passant TTF comme valeur FontType, ttf comme valeur fileExtension et ByteContentStreamSource objet basé sur le tableau fontBytes.
  4. Chargez la police.
 1    // Construct path to the file
 2    String fontPath = Paths.get(getDataDir(), "Montserrat-Regular.ttf").toString();
 3
 4    // Load font binary data into byte array
 5    byte[] fontBytes = Files.readAllBytes(Paths.get(fontPath));
 6
 7    // Initialize FontDefinition object  passing TTF as FontType value, "ttf" as fileExtension value, 
 8    // and ByteContentStreamSource object based on fontBytes array
 9    FontDefinition fontDef = new FontDefinition(FontType.TTF, "ttf", new ByteContentStreamSource(fontBytes));
10
11    // Load the font
12    Font font = Font.open(fontDef);

Vous pouvez obtenir des exemples sur la façon d’utiliser Aspose.Font dans la solution Aspose.Font.Examples.sln, dans les java-examples de la documentation Aspose.Font.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.