บทช่วยสอนฉบับย่อนี้จะแนะนำคุณเกี่ยวกับ วิธีดึงเมตริกแบบอักษรโดยใช้ Java คำอธิบายโดยละเอียดพร้อมกับโค้ดตัวอย่างเพื่อรับเมตริกแบบอักษรโดยใช้ Java จะพร้อมใช้งาน โดยก่อนอื่นจะมีการกำหนดค่าสภาพแวดล้อม จากนั้นคุณจะได้รับขั้นตอนทีละขั้นตอนและโค้ด Java ที่ใช้งานได้เพื่อโหลด PFB แบบอักษรและดึงเมตริกแบบอักษร กระบวนการเรียกเมตริกแบบอักษรนี้สามารถใช้ได้ในระบบปฏิบัติการทั่วไปใดๆ เช่น Windows, macOS หรือ Linux
ขั้นตอนในการดึง Font Metrics โดยใช้ Java
- กำหนดค่าไลบรารี Aspose.Font จาก Maven Repository เพื่อดึงข้อมูลแบบอักษรในแอปพลิเคชัน
- โหลดตัวอย่างไฟล์ฟอนต์ PFB ลงในอ็อบเจ็กต์คลาส FileSystemStreamSource เพื่อดึงเมตริก
- ประกาศวัตถุคลาส FontFileDefinition โดยจัดเตรียมวัตถุ FileSystemStreamSource
- เริ่มต้นวัตถุคลาส FontDefinition และตั้งค่าวัตถุ FontType และ FontFileDefinition
- ใช้ฟังก์ชัน Font.open เพื่อโหลดคำจำกัดความของฟอนต์และส่งเป็น Type1Font
- ดึงและแสดงข้อมูลเมตริกแบบอักษรที่ต้องการทั้งหมดโดยใช้วัตถุ Type1Font
ขั้นตอนเหล่านี้อธิบายแนวทางทีละขั้นตอนในการโหลดไฟล์ฟอนต์ PFB จากนั้นใช้ลำดับของคลาสอ็อบเจ็กต์ต่างๆ ที่จำเป็นเพื่อดึงข้อมูลนิยามฟอนต์ จากนั้น แยกเมตริกฟอนต์โดยใช้ Java ข้อมูลเมตริกแบบอักษรที่จำเป็นทั้งหมดมีอยู่ในวัตถุคลาส Type1Font
รหัสเพื่อดึง Font Metrics โดยใช้ 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 โปรดดูบทความใน วิธีแปลง Word เป็น JPG ใน Java