จัดการฟอนต์ใน java

แบบอักษร มีบทบาทสำคัญในการนำเสนอข้อความภายในเอกสารและหน้าเว็บ มีตระกูลฟอนต์ให้เลือกมากมายที่ให้คุณใช้อักขระแฟนซีเพื่อทำให้ข้อความของคุณน่าสนใจ เนื่องจาก Aspose จัดการกับรูปแบบไฟล์อัตโนมัติ เราจึงได้เปิดตัว API การจัดการฟอนต์โดยเฉพาะเพื่อทำงานกับรูปแบบไฟล์ฟอนต์ ในบทความนี้ คุณจะทำความคุ้นเคยกับ Java Font API และเรียนรู้วิธีทำงานกับฟอนต์ CFF, TrueType, OpenType และ Type1 จากภายในแอปพลิเคชัน Java ของคุณ

API การจัดการแบบอักษร Java

Aspose.Font for Java คือ API การจัดการฟอนต์ที่ให้คุณโหลด บันทึก และจัดการฟอนต์ยอดนิยม รวมถึง CFF, TrueType, Type1, EOT และ OpenType นอกจากนี้ API ยังสนับสนุนการเรียกเมตริกแบบอักษรและการแสดงข้อความโดยใช้ประเภทแบบอักษรที่รองรับ คุณสามารถ ดาวน์โหลด API หรือติดตั้งภายในแอปพลิเคชันที่ใช้ Maven โดยใช้การกำหนดค่าต่อไปนี้

<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>20.10</version>
</dependency>

โหลดแบบอักษร CFF, TrueType และ Type1 โดยใช้ Java

คุณสามารถโหลดฟอนต์จากไฟล์ที่อยู่ในที่เก็บข้อมูลของคุณจากภายในโปรแกรม Java ของคุณได้อย่างง่ายดาย เมื่อคุณโหลดฟอนต์แล้ว คุณสามารถดำเนินการเพิ่มเติมได้ตามต้องการ ต่อไปนี้เป็นขั้นตอนการโหลดฟอนต์โดยใช้ Aspose.Font for Java

  • ใช้คลาส FontDefinition เพื่อโหลดฟอนต์โดยระบุประเภทของมัน เช่น TrueType, Type1 เป็นต้น
  • ใช้คลาส CffFont, TtfFont หรือ Type1Font เพื่อเข้าถึงแบบอักษร CFF, TrueType และ Type1 ตามลำดับจากวัตถุ FontDefinition

โหลดแบบอักษร CFF โดยใช้ Java

นี่คือวิธีที่คุณสามารถโหลดฟอนต์ CFF จากไฟล์โดยใช้ Java

// สำหรับตัวอย่างและไฟล์ข้อมูลทั้งหมด โปรดไปที่ https://github.com/aspose-font/Aspose.Font-for-Java
String fileName = Utils.getDataDir() + "OpenSans-Regular.cff"; //Font file name with full path

      FontDefinition fd = new FontDefinition(FontType.CFF, new FontFileDefinition("cff", new FileSystemStreamSource(fileName)));
      CffFont ttfFont = (CffFont) Font.open(fd);
      
      System.out.println("Font has been loaded");

โหลด TrueType Fonts โดยใช้ Java

ตัวอย่างโค้ด Java ต่อไปนี้โหลดแบบอักษร TrueType

// สำหรับตัวอย่างและไฟล์ข้อมูลทั้งหมด โปรดไปที่ https://github.com/aspose-font/Aspose.Font-for-Java
String fileName = Utils.getDataDir() + "Montserrat-Regular.ttf"; //Font file name with full path

      FontDefinition fd = new FontDefinition(FontType.TTF, new FontFileDefinition("ttf", new FileSystemStreamSource(fileName)));
      TtfFont font = (TtfFont) Font.open(fd);        

โหลดแบบอักษร Type1 โดยใช้ Java

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีโหลดฟอนต์ Type1 โดยใช้ Java

// สำหรับตัวอย่างและไฟล์ข้อมูลทั้งหมด โปรดไปที่ https://github.com/aspose-font/Aspose.Font-for-Java
String fileName = Utils.getDataDir() + "courier.pfb"; //Font file name with full path

FontDefinition fd = new FontDefinition(FontType.Type1, new FontFileDefinition("pfb", new FileSystemStreamSource(fileName)));
      Type1Font font = (Type1Font) Font.open(fd);

แยก Font Metrics จาก TrueType หรือ Type1 Fonts โดยใช้ Java

ไฟล์ฟอนต์ยังมีข้อมูลของฟอนต์ที่ใช้เพื่อระบุระยะห่างระหว่างบรรทัด การจัดวางตัวห้อยและตัวยก การจัดตำแหน่ง และอื่นๆ Aspose.Font for Java ยังให้คุณดึงข้อมูลเมตริกฟอนต์ เช่น Ascender, Descender, TypoAscender, TypoDescender และ UnitsPerEm ต่อไปนี้เป็นขั้นตอนในการดำเนินการนี้

  • ใช้คลาส FontDefinition เพื่อโหลดฟอนต์ TrueType หรือ Type1
  • เข้าถึงแบบอักษรโดยใช้คลาส TtfFont หรือ Type1Font ตามประเภทของแบบอักษร
  • แยกข้อมูลเมตริกของแบบอักษร

รับ Font Metrics จาก TrueType Font โดยใช้ Java

// สำหรับตัวอย่างและไฟล์ข้อมูลทั้งหมด โปรดไปที่ https://github.com/aspose-font/Aspose.Font-for-Java
String fileName = Utils.getDataDir() + "Montserrat-Regular.ttf"; //Font file name with full path

      FontDefinition fd = new FontDefinition(FontType.TTF, new FontFileDefinition("ttf", new FileSystemStreamSource(fileName)));
      TtfFont font = (TtfFont) Font.open(fd);

      String name = font.getFontName();
      System.out.println("Font name: " + name);
      System.out.println("Glyph count: " + font.getNumGlyphs());
      String metrics = MessageFormat.format(
          "Font metrics: ascender - {0}, descender - {1}, typo ascender = {2}, typo descender = {3}, UnitsPerEm = {4}",
          font.getMetrics().getAscender(), font.getMetrics().getDescender(),
          font.getMetrics().getTypoAscender(), font.getMetrics().getTypoDescender(), font.getMetrics().getUnitsPerEM());

      System.out.println(metrics);
      
    //รับตารางการเข้ารหัส cmap unicode จากฟอนต์เป็นวัตถุ TtfCMapFormatBaseTable เพื่อเข้าถึงข้อมูลเกี่ยวกับฟอนต์ glyph สำหรับสัญลักษณ์ 'A'
      //ตรวจสอบด้วยว่าแบบอักษรมีวัตถุ TtfGlyfTable (ตาราง 'glyf') เพื่อเข้าถึงสัญลักษณ์
      TtfCMapFormatBaseTable cmapTable = null;
      if (font.getTtfTables().getCMapTable() != null)
      {
          cmapTable = font.getTtfTables().getCMapTable().findUnicodeTable();
      }
      if (cmapTable != null && font.getTtfTables().getGlyfTable() != null)
      {
      	System.out.println("Font cmap unicode table: PlatformID = " + cmapTable.getPlatformId() +
      			", PlatformSpecificID = " + cmapTable.getPlatformSpecificId());

          //รหัสสำหรับสัญลักษณ์ 'A'
          char unicode = (char)65;

          //ดัชนีสัญลักษณ์สำหรับ 'A'
          long glIndex = cmapTable.getGlyphIndex(unicode);

          if (glIndex != 0)
          {
              //สัญลักษณ์สำหรับ 'A'
              Glyph glyph = font.getGlyphById(glIndex);
              if (glyph != null)
              {
                  //พิมพ์สัญลักษณ์เมตริก
              	System.out.println("Glyph metrics for 'A' symbol:");
                  String bbox = MessageFormat.format(
                      "Glyph BBox: Xmin = {0}, Xmax = {1}" + ", Ymin = {2}, Ymax = {3}",
                      glyph.getGlyphBBox().getXMin(), glyph.getGlyphBBox().getXMax(),
                      glyph.getGlyphBBox().getYMin(), glyph.getGlyphBBox().getYMax());
                  System.out.println(bbox);
                  System.out.println("Width:" + font.getMetrics().getGlyphWidth(new GlyphUInt32Id(glIndex)));
              }
          }
      }

แยก Font Metrics จาก Type1 Font โดยใช้ Java

// สำหรับตัวอย่างและไฟล์ข้อมูลทั้งหมด โปรดไปที่ https://github.com/aspose-font/Aspose.Font-for-Java
String fileName = Utils.getDataDir() + "courier.pfb"; //Font file name with full path

FontDefinition fd = new FontDefinition(FontType.Type1, new FontFileDefinition("pfb", new FileSystemStreamSource(fileName)));
      Type1Font font = (Type1Font) Font.open(fd);

      String name = font.getFontName();
      System.out.println("Font name: " + name);
      System.out.println("Glyph count: " + font.getNumGlyphs());
      String metrics = MessageFormat.format(
          "Font metrics: ascender - {0}, descender - {1}, typo ascender = {2}, typo descender = {3}, UnitsPerEm = {4}",
          font.getMetrics().getAscender(), font.getMetrics().getDescender(),
          font.getMetrics().getTypoAscender(), font.getMetrics().getTypoDescender(), font.getMetrics().getUnitsPerEM());

      System.out.println(metrics);

บทสรุป

ในบทความนี้ คุณได้เรียนรู้วิธีการทำงานกับฟอนต์ CFF, TrueType และ Type1 โดยทางโปรแกรมโดยใช้ Java นอกจากนี้ คุณได้เห็นวิธีเข้าถึงข้อมูลเมตริกแบบอักษรของแบบอักษรเฉพาะ คุณสามารถสำรวจเพิ่มเติมเกี่ยวกับ API การจัดการฟอนต์ Java ได้โดยใช้ เอกสารประกอบ และ ตัวอย่างซอร์สโค้ด