CFFファイルからフォントをロード| Java

ファイルCenturygothic.cffに配置された特性「Centurygothic」にフォントをロードする方法を詳しく調べてください。


Aspose.Fontロードの基礎を読んでいない場合は、 フォントをロードする方法?ページ。

まず、使用する必要があるステートメントに通知しましょう。

 1    パッケージ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;

java.io.fileオブジェクトを使用してファイルからロードします

アルゴリズムに従って、フォントのロードを満たします。

  1. ファイルへのパスを構築します。
  2. fontdefinitonfonttype値として渡すオブジェクトを開始します。
  3. 自動的に計算された値 fileExtensionを取得します。
  4. フォントをロードします。
 1    //ファイルへのパスを構築します。
 2    string fontpath = paths.get(getDatadir() "Centurygothic.cff")toString();
 3	
 4    // Initialize FontDefinition object passing CFF 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.CFF, fileDef);
 9
10    // Load the font
11    Font font = Font.open(fontDef);

バイト[]タイプ変数を使用して、bytecontentsStreamSourceタイプオブジェクトを使用してフォントロード

この方法でフォントをロードするには、次の手順を実行する必要があります。

  1. ファイルへのパスを構築します。
  2. CFFFontType 値として、cfffileExtension 値として、そして fontBytes 配列に基づいて ByteContentStreamSource オブジェクトとして渡して、 FontDefiniton オブジェクトを初期化します。
  3. フォントをロードします。
 1    //ファイルへのパスを構築します
 2    string fontpath = paths.get(getDatadir() "Centurygothic.cff")toString();
 3
 4    // Load font binary data into byte array
 5    byte[] fontBytes = Files.readAllBytes(Paths.get(fontPath));
 6
 7    // Initialize FontDefinition object  passing CFF as FontType value, "cff" as fileExtension value, 
 8    // and ByteContentStreamSource object based on fontBytes array
 9    FontDefinition fontDef = new FontDefinition(FontType.CFF, "ttf", new ByteContentStreamSource(fontBytes));
10
11    // Load the font
12    Font font = Font.open(fontDef);

Aspose.fontを使用する方法のその他の例は、 aspose.font documentationJava-examples(10)、 aspose.font.examples.sln solutionにあります。

Have any questions about Aspose.Font?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.