Class FontBuilder

java.lang.Object
io.leandev.appfuse.font.FontBuilder

public class FontBuilder extends Object

字體建構器,提供流暢的 API 來建立和配置字體。

支援兩種字體來源:

範例用法:

// 使用系統字體
Font font1 = FontBuilder.newFont(Font.SANS_SERIF)
    .withStyle(Font.BOLD)
    .ofSize(14)
    .build();

// 使用 Locale 選擇適合的系統字體
Font font2 = FontBuilder.newFont(Locale.JAPANESE)
    .ofSize(12)
    .build();

// 載入應用層提供的自訂字體檔案
InputStream fontStream = getClass().getResourceAsStream("fonts/MyFont.ttf");
Font font3 = FontBuilder.newFont(fontStream)
    .withStyle(Font.ITALIC)
    .ofSize(16)
    .build();
  • Method Details

    • newFont

      public static FontBuilder newFont(String fontName)
      使用系統字體名稱建立字體建構器。
      Parameters:
      fontName - 字體名稱(如 Font.SANS_SERIF, Font.SERIF, "Arial" 等)
      Returns:
      FontBuilder 實例
    • newFont

      public static FontBuilder newFont(FontFamily fontFamily)
      使用字體家族枚舉建立字體建構器(使用系統字體)。
      Parameters:
      fontFamily - 字體家族枚舉
      Returns:
      FontBuilder 實例
    • newFont

      public static FontBuilder newFont(InputStream inputStream)

      從輸入串流載入字體檔案建立字體建構器。

      應用層需自行提供字體檔案的 InputStream。 若載入失敗,將降級使用系統預設 SANS_SERIF 字體。

      Parameters:
      inputStream - 字體檔案的輸入串流
      Returns:
      FontBuilder 實例
    • newFont

      public static FontBuilder newFont(Locale locale)

      根據 Locale 選擇適合的系統字體。

      對於 CJK 語系,會優先選擇 Dialog 字體(較好的多語言支援); 其他語系使用 SANS_SERIF 字體。

      Parameters:
      locale - 語言環境
      Returns:
      FontBuilder 實例
    • newFont

      public static FontBuilder newFont(Font font)
      直接使用 Font 物件建立字體建構器。
      Parameters:
      font - Font 物件
      Returns:
      FontBuilder 實例
    • withStyle

      public FontBuilder withStyle(int style)
      設定字體樣式。
      Parameters:
      style - 字體樣式(Font.PLAIN, Font.BOLD, Font.ITALIC 或其組合)
      Returns:
      FontBuilder 實例(供鏈式呼叫)
    • ofSize

      public FontBuilder ofSize(int size)
      設定字體大小。
      Parameters:
      size - 字體大小(點數)
      Returns:
      FontBuilder 實例(供鏈式呼叫)
    • build

      public Font build()

      建構並註冊字體到系統。

      套用先前設定的樣式和大小,並將字體註冊到 GraphicsEnvironment。

      Returns:
      建構完成的 Font 物件