Javaを使用してフォントメトリックを取得する方法

このクイックチュートリアルでは、Javaを使用してフォントメトリックをフェッチする方法について説明します。 Javaを使用してフォントメトリックを取得するためのサンプルコードとともに詳細な説明が利用可能になり、最初に環境が構成され、次にPFBをロードするためのステップバイステップの手順と動作するJavaコードが取得されます。フォントとフォントメトリックを取得します。フォントメトリックを取得するこのプロセスは、Windows、macOS、Linuxなどの一般的なオペレーティングシステムのいずれかで使用できます。

Javaを使用してフォントメトリックを取得する手順

  1. MavenリポジトリからAspose.Fontライブラリを構成して、アプリケーションでフォントメトリックをフェッチします
  2. サンプルPFBフォントファイルをFileSystemStreamSourceクラスオブジェクトにロードして、メトリックを取得します
  3. FileSystemStreamSourceオブジェクトを提供して、FontFileDefinitionクラスオブジェクトを宣言します
  4. FontDefinitionクラスオブジェクトを初期化し、FontTypeおよびFontFileDefinitionオブジェクトを設定します
  5. Font.open関数を使用してフォント定義をロードし、Type1Fontとしてキャストします。
  6. Type1Fontオブジェクトを使用して、必要なすべてのフォントメトリック情報を取得して表示します

これらの手順では、PFBフォントファイルをロードし、フォント定義をフェッチしてJavaを使用してフォントメトリックを抽出するために必要な一連の異なるクラスオブジェクトを使用するための段階的なアプローチについて説明します。必要なすべてのフォントメトリック情報は、Type1Fontクラスオブジェクトで利用できます。

Javaを使用してフォントメトリックを取得するコード

import com.aspose.cells.License;
import com.aspose.font.FileSystemStreamSource;
import com.aspose.font.Font;
import com.aspose.font.FontDefinition;
import com.aspose.font.FontFileDefinition;
import com.aspose.font.FontType;
import com.aspose.font.Type1Font;
public class HowToFetchFontMetricsInJava {
public static void main(String[] args) throws Exception { //main function for HowToFetchFontMetricsInJava class to get Type1Font Metrics
// Instantiate the license for Aspose.Font to avoid trial version limitations before fetching the Font Metrics
License licenseFont = new License();
licenseFont.setLicense("Aspose.Font.lic");
// Load the sample Type1Font PFB file into FileSystemStreamSource
FileSystemStreamSource fileSystemStreamSource = new FileSystemStreamSource("Type1FontSampleInput.pfb");
// Instantiate FontFileDefinition class object using the FileSystemStreamSource object as argument
FontFileDefinition fontFileDefinition = new FontFileDefinition(fileSystemStreamSource);
// Instantiate font definition class object by providing the FontType and FontFileDefinition class object
FontDefinition fontDefinition = new FontDefinition(FontType.Type1, fontFileDefinition);
// Using the font definition, initialize the Type1Font class object
Type1Font type1Font = (Type1Font) Font.open(fontDefinition);
// Using this TypeFont class object, retrieve the font metrics information and display it on the console
System.out.print(
"FontName = " + type1Font.getFontName() +
", FontGlyphcount = " + type1Font.getNumGlyphs()+
", FontMetricsAscender = " + type1Font.getMetrics().getAscender() +
", FontMetricsDescender = " + type1Font.getMetrics().getDescender() +
", FontMetricsTypoAscender = " + type1Font.getMetrics().getTypoAscender() +
", FontMetricsTypoDescender = " + type1Font.getMetrics().getTypoDescender() +
", FontMetricsUnitsPerEM = " + type1Font.getMetrics().getUnitsPerEM()
);
}
}

このサンプルコードでは、最初にPFBファイルをロードし、次にType1Fontクラスを使用してロードされたフォントの定義を開きます。 Type1Fontクラスには、FontName、NumGlyphs、Ascender、Descender、TypoAscender、TypoDescender、UnitsPerEMなどのプロパティが含まれています。これらのプロパティを取得してコンソールに表示したり、アプリケーションの要件に従って処理したりできます。

このチュートリアルでは、Javaを使用してフォントメトリックを取得する手順を実行しました。 WordファイルをJPGに変換するなどの他の機能について知りたい場合は、JavaでWordをJPGに変換する方法の記事を参照してください。

 日本語