تحميل الخطوط من الملفات والتدفقات ومصفوفات البايت في Java
قم بتحميل وتهيئة خطوط TrueType (TTF) وOpenType (OTF) وWOFF/WOFF2 وEmbedded OpenType (EOT) وType1 وCompact Font Format (CFF) برمجيًا باستخدام Aspose.Font for Java. قم بتحميل الخطوط بسلاسة من مسارات الأقراص أو ملفات النظام أو تدفقات البايت أو المصفوفات الموجودة في الذاكرة دون الاعتماد على Java AWT الأصلي أو تثبيت الخطوط المرتبط بنظام التشغيل.
لفتح أي خط والعمل عليه باستخدام Aspose.Font for Java، يلزم وجود معلمتين أساسيتين:
- تنسيق الخط (FontType): يحدد الفئة العامة للخط (على سبيل المثال، FontType.TTF وFontType.Type1 وFontType.CFF).
- مصدر ملف الخط الثنائي (StreamSource/FontFileDefinition): يغلف التدفق الثنائي الأساسي بغض النظر عما إذا كان موجودًا على القرص أو في قاعدة بيانات أو داخل تدفقات المستندات (مثل PDF/DOCX).
- تربط الفئة المركزية
FontDefinitionهذين المعلمين معًا، مما يسمح لـFont.open(fontDefinition) بإرجاع كائن موحد من نوعcom.aspose.font.Font.
كيفية تحميل خط باستخدام java.io.File؟
الطريقة الأكثر مباشرة لتحميل الخطوط المخزنة على محركات الأقراص المحلية باستخدام FontFileDefinition(File file).
import com.aspose.font.*;
import java.io.File;
import java.nio.file.Paths;
public class LoadFontFromFile {
public static void main(String[] args) {
// Step 1: Define path to font file
String fontPath = Paths.get("data", "Montserrat-Regular.ttf").toString();
// Step 2: Create FontFileDefinition from java.io.File
FontFileDefinition fileDef = new FontFileDefinition(new File(fontPath));
// Step 3: Create FontDefinition (extension is auto-detected from FontFileDefinition)
FontDefinition fontDef = new FontDefinition(FontType.TTF, fileDef);
// Step 4: Open and load font instance
Font font = Font.open(fontDef);
System.out.println("Successfully loaded font: " + font.getFontName());
}
}كيفية تحميل خط باستخدام FileSystemStreamSource
يوفر معالجة صريحة لامتداد الملف لاكتشاف التنسيق بدقة عند تحميل الخطوط من مسارات الأقراص.
import com.aspose.font.*;
import java.nio.file.Paths;
public class LoadFontFromStreamSource {
public static void main(String[] args) {
String fontPath = Paths.get("data", "Montserrat-Regular.ttf").toString();
// Pass 'ttf' explicitly to ensure proper format mapping
FontFileDefinition fileDef = new FontFileDefinition("ttf", new FileSystemStreamSource(fontPath));
FontDefinition fontDef = new FontDefinition(FontType.TTF, fileDef);
Font font = Font.open(fontDef);
System.out.println("Loaded Font Name: " + font.getFontName());
}
}كيفية تحميل خط من مصدر تدفق مباشر (بدون FontFileDefinition)
مرّر FileSystemStreamSource مباشرةً إلى FontDefinition لتبسيط عملية التحميل.
import com.aspose.font.*;
import java.nio.file.Paths;
public class LoadFontDirectStream {
public static void main(String[] args) {
String fontPath = Paths.get("data", "Montserrat-Regular.ttf").toString();
// Provide FontType, file extension, and StreamSource directly
FontDefinition fontDef = new FontDefinition(FontType.TTF, "ttf", new FileSystemStreamSource(fontPath));
Font font = Font.open(fontDef);
System.out.println("Loaded Font Name: " + font.getFontName());
}
}كيفية تحميل خط من مصفوفة بايت موجودة في الذاكرة (byte[])
مثالي لتحميل الخطوط التي يتم جلبها من قواعد البيانات أو طلبات الشبكة أو التخزين السحابي أو أصول PDF/DOCX المضمّنة باستخدام ByteContentStreamSource.
import com.aspose.font.*;
import java.nio.file.Files;
import java.nio.file.Paths;
public class LoadFontFromByteArray {
public static void main(String[] args) throws Exception {
String fontPath = Paths.get("data", "Montserrat-Regular.ttf").toString();
// Read font binary data into byte array
byte[] fontBytes = Files.readAllBytes(Paths.get(fontPath));
// Initialize FontDefinition with ByteContentStreamSource
FontDefinition fontDef = new FontDefinition(FontType.TTF, "ttf", new ByteContentStreamSource(fontBytes));
// Open font from memory
Font font = Font.open(fontDef);
System.out.println("Loaded Font from Memory: " + font.getFontName());
}
}التثبيت والإعداد
أضف Aspose.Font for Java إلى مشروعك عبر Maven.
Package Manager Console Command
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-font</artifactId>
<version>Latest</version>
</dependency>
الأسئلة الشائعة
1. لماذا يجب أن أمرر كلًا من FontType وfileExtension عند تحميل الخطوط؟
يمثل FontType عائلة التنسيق العامة (مثل FontType.TTF)، ولكن تنتمي عدة تنسيقات ملفات مختلفة إلى تلك العائلة (مثل .ttf و.eot و.ttc). ويضمن تحديد fileExtension استخدام المكتبة للمحلل الصحيح لتنسيق الحاوية المحدد.
2. هل يمكنني تحميل الخطوط مباشرةً من حقول BLOB في قاعدة البيانات؟
نعم. استرد بيانات BLOB كمصفوفة byte[] واستخدم ByteContentStreamSource لتهيئة FontDefinition دون كتابة البيانات على القرص.
3. كيف يمكنني تحميل خط عندما تبدأ البيانات الثنائية عند إزاحة محددة داخل ملف؟
استخدم مُنشئ FontFileDefinition(String fileExtension, StreamSource streamSource, long offset)، مع تمرير إزاحة البايت التي تبدأ عندها بيانات الخط.