Java Load Font TTF Arquivo | Java
Contents
[
Hide
Show
]Aqui você encontrará quatro exemplos sobre como carregar uma fonte do arquivo Montserrat-Regular.ttf cumprido de maneiras diferentes.
Certifique -se de aprender os fundamentos de carregamento na página Como carregar fontes? primeiro.
Use as próximas declarações:
1 pacote 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;
Carregando do arquivo usando o objeto FileSystreamSource
Dê os próximos passos para cumprir a operação:
- Construa o caminho para o arquivo.
- Iniciar fontDefiniton Objeto.
- Definir
FILEEXTENNENTE como
TTF
. - Carregue a fonte.
1 // Construa o caminho para o arquivo
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);
Carregando do arquivo usando o objeto java.io.file
Para cumprir o carregamento, faça o próximo:
- Construa o caminho para o arquivo.
- Iniciar
fontDefiniton Passagem do objeto
ttf
como fontType valor. - Obtenha o valor calculado automaticamente FILEEXTENNENTE.
- Carregue a fonte.
1 // Construa o caminho para o arquivo
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);
Carregando do arquivo excluindo o objeto FontfiledEfinition da cadeia de inicialização
As próximas ações devem ser tomadas para carregar a fonte desta maneira:
- Construa o caminho para o arquivo.
- Inicie
fontDefiniton Passagem do objeto
ttf
como fontType Valor,ttf
como fileExtension valor e FIOSSYSTEMSTREAMSOURCE objeto. Parâmetro fileExtension Aqui não há um valor duplicado para o parâmetro FontType. - Carregue a fonte.
1 // Construa o caminho para o arquivo
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);
Carregando a fonte da matriz de bytes
Para carregar a fonte da matriz de bytes:
- Construa o caminho para o arquivo.
- Carregue dados binários da fonte na matriz de bytes
- Inicialize
fontDefiniton Passagem do objeto
ttf
como fontType valor,ttf
como fileExtension valor e byteContentSTreamSource objeto baseado em fontbytes Array. - Carregue a fonte.
1 // Construa o caminho para o arquivo
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);
Você pode obter exemplos sobre como usar aspose.font em aspose.font.examples.sln Solution, nos java-examples da documentação aspose.font.