在java中操作字體

字體 在文檔和網頁中的文本顯示中起著重要作用。可以使用各種字體系列,使您可以使用花哨的字符來使您的文本更具吸引力。由於 Aspose 處理文件格式自動化,我們推出了專用的字體操作 API 來處理字體文件格式。在本文中,您將熟悉我們的 Java 字體 API,並了解如何在您的 Java 應用程序中使用 CFF、TrueType、OpenType 和 Type1 字體。

Java 字體操作 API

Aspose.Font for Java 是字體操作 API,允許您加載、保存和操作流行字體,包括 CFFTrueTypeType1 EOTOpenType。此外,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>

使用 Java 加載 CFF、TrueType 和 Type1 字體

您可以在 Java 程序中輕鬆地從存儲中的文件加載字體。加載字體後,您可以根據需要執行進一步的操作。以下是使用 Aspose.Font for Java 加載字體的步驟。

  • 使用 FontDefinition 類通過指定其類型(如 TrueType、Type1 等)來加載字體。
  • 使用 CffFontTtfFontType1Font 類分別從 FontDefinition 對象訪問 CFF、TrueType 和 Type1 字體。

使用 Java 加載 CFF 字體

這是使用 Java 從文件加載 CFF 字體的方法。

// 完整的示例和數據文件,請訪問 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");

使用 Java 加載 TrueType 字體

以下 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);        

使用 Java 加載 Type1 字體

以下代碼示例顯示如何使用 Java 加載 Type1 字體。

// 完整的示例和數據文件,請訪問 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);

使用 Java 從 TrueType 或 Type1 字體中提取字體規格

字體文件還包含用於指定行間距、下標和上標的位置、對齊方式等的字體信息。Aspose.Font for Java 還允許您提取字體度量信息,包括 Ascender、Descender、TypoAscender、TypoDescender和 UnitsPerEm。以下是執行此操作的步驟。

  • 使用 FontDefinition 類加載 TrueType 或 Type1 字體。
  • 根據字體類型使用 TtfFontType1Font 類訪問字體。
  • 提取字體的規格信息。

使用 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);

      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 以訪問有關符號“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)));
              }
          }
      }

使用 Java 從 Type1 字體中提取字體規格

// 有關完整示例和數據文件,請訪問 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);

結論

在本文中,您學習瞭如何使用 Java 以編程方式使用 CFF、TrueType 和 Type1 字體。此外,您還了解瞭如何訪問特定字體的字體規格信息。您可以使用 文檔源代碼示例 探索有關 Java 字體操作 API 的更多信息。