Tutorial singkat ini akan memandu Anda tentang cara mengambil metrik font menggunakan Java. Deskripsi terperinci bersama dengan kode contoh untuk mendapatkan metrik font menggunakan Java akan tersedia sehingga pertama-tama lingkungan akan dikonfigurasi dan kemudian Anda akan mendapatkan prosedur langkah demi langkah dan kode Java yang berfungsi untuk memuat PFB font dan ambil metrik font. Proses untuk mengambil metrik font ini dapat digunakan di salah satu sistem operasi umum seperti Windows, macOS, atau Linux.
Langkah-langkah untuk Mengambil Metrik Font menggunakan Java
- Konfigurasikan pustaka Aspose.Font dari Maven Repository untuk mengambil metrik font dalam aplikasi
- Muat file font PFB sampel ke objek kelas FileSystemStreamSource untuk mengambil metrik
- Deklarasikan objek kelas FontFileDefinition dengan menyediakan objek FileSystemStreamSource
- Inisialisasi objek kelas FontDefinition dan atur objek FontType dan FontFileDefinition
- Gunakan fungsi Font.open untuk memuat definisi font dan menampilkannya sebagai Type1Font
- Ambil dan tampilkan semua informasi metrik font yang diinginkan menggunakan objek Type1Font
Langkah-langkah ini menjelaskan pendekatan langkah demi langkah untuk memuat file font PFB dan kemudian menggunakan urutan objek kelas yang berbeda yang diperlukan untuk mengambil definisi font dan kemudian mengekstrak metrik font menggunakan Java. Semua informasi metrik font yang diperlukan tersedia di objek kelas Type1Font.
Kode untuk Mengambil Metrik Font menggunakan 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() | |
); | |
} | |
} |
Dalam kode contoh ini, pertama-tama kita memuat file PFB dan kemudian menggunakan kelas Type1Font untuk membuka definisi font yang dimuat. Kelas Type1Font berisi properti seperti FontName, NumGlyphs, Ascender, Descender, TypoAscender, TypoDescender, dan UnitsPerEM. Anda dapat mengambil properti ini untuk ditampilkan di konsol atau memprosesnya sesuai kebutuhan aplikasi Anda.
Dalam tutorial ini, kita telah melalui langkah-langkah untuk mengambil metrik font menggunakan Java. Jika Anda ingin mempelajari fitur lain seperti mengonversi file Word ke JPG, lihat artikel di cara mengonversi Word ke JPG di Java.