AWS Lambda + Java で使用可能なフォント一覧を調べる

## 概要
- AWS Lambda 上で Java のプログラムを動かして、使用可能なフォント一覧を取得する

## ソースコード

### ソースコード一覧

```
├── pom.xml
└── src
    └── main
        └── java
            └── com
                └── example
                    └── FontList.java
```

### FontList.java

使用可能なフォント情報を取得する Java のプログラム。

```java
package com.example;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

public class FontList implements RequestHandler<Map<String, Object>, Map<String, Object>> {

  /**
   * スタンドアローンの Java アプリケーションとして実行した場合のエントリポイントです。
   *
   * @param args 使用しない
   */
  public static void main(String[] args) {

    Map<String, Object> output = new FontList().invoke();

    // フォント名
    for (String name : (String[]) output.get("names")) {
      System.out.println(name);
    }

    // フォント名とグリフ数
    for (String info : (String[]) output.get("numGlyphs")) {
      System.out.println(info);
    }

    // システム情報
    System.out.println(output.get("system"));
  }

  /**
   * AWS Labmda 関数を実行した場合のエントリポイント (ハンドラーメソッド) です。
   *
   * @param input   入力データ
   * @param context AWS Lambda Context オブジェクト
   * @return 出力データ
   */
  @Override
  public Map<String, Object> handleRequest(Map<String, Object> input, Context context) {
    return invoke();
  }

  /**
   * 使用できるフォントの情報を返します。
   *
   * @return 使用できるフォントの情報
   */
  private static Map<String, Object> invoke() {

    // 使用できるフォントの一覧を取得
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    Font[] allFonts = ge.getAllFonts();

    // 出力したいフォント情報をまとめる
    String[] names = Arrays.stream(allFonts)
      .map(Font::getName) // フォント名
      .toArray(String[]::new);

    // グリフ数が少ない順に並べる
    String[] numGlyphs = Arrays.stream(allFonts)
      .sorted(Comparator.comparing(Font::getNumGlyphs)) // グリフ数でソート
      .map(f -> f.getName() + ": " + f.getNumGlyphs()) // フォント名とグリフ数
      .toArray(String[]::new);

    // システム情報を取得
    Map system = new HashMap<String, String>();
    system.put("os.name", getSystemProperty("os.name"));
    system.put("os.arch", getSystemProperty("os.arch"));
    system.put("os.version", getSystemProperty("os.version"));
    system.put("java.version", getSystemProperty("java.version"));
    system.put("java.specification.version", getSystemProperty("java.specification.version"));
    system.put("java.runtime.name", getSystemProperty("java.runtime.name"));
    system.put("java.runtime.version", getSystemProperty("java.runtime.version"));
    system.put("java.vm.name", getSystemProperty("java.vm.name"));
    system.put("java.vm.version", getSystemProperty("java.vm.version"));

    // 出力データを構築
    Map output = new HashMap<String, Object>();
    output.put("names", names);
    output.put("numGlyphs", numGlyphs);
    output.put("system", system);
    return output;
  }

  /**
   * 指定されたキーによって示されるシステム・プロパティを取得します。
   *
   * @param key システム・プロパティの名前
   * @return システム・プロパティの文字列値 (そのキーを持つプロパティが存在しない場合は null)
   */
  private static String getSystemProperty(String key) {
    try {
      return System.getProperty(key);
    } catch (SecurityException e) {
      return e.toString();
    }
  }
}
```

### pom.xml

Maven でビルドするための設定ファイル。

```xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example</groupId>
  <artifactId>fontlist</artifactId>
  <packaging>jar</packaging>
  <version>1.0</version>
  <name>fontlist</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-lambda-java-core -->
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-lambda-java-core</artifactId>
      <version>1.2.0</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <!-- https://maven.apache.org/plugins/maven-shade-plugin/ -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2.1</version>
        <configuration>
          <createDependencyReducedPom>false</createDependencyReducedPom>
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>
```

## AWS Lambda + Java8 での実行結果

```json
{
  "names": [
    "Bitstream Charter",
    "Bitstream Charter Bold",
    "Bitstream Charter Bold Italic",
    "Bitstream Charter Italic",
    "Century Schoolbook L Bold",
    "Century Schoolbook L Bold Italic",
    "Century Schoolbook L Italic",
    "Century Schoolbook L Roman",
    "Courier 10 Pitch",
    "Courier 10 Pitch Bold",
    "Courier 10 Pitch Bold Italic",
    "Courier 10 Pitch Italic",
    "Cursor",
    "DejaVu Sans",
    "DejaVu Sans Bold",
    "DejaVu Sans Bold Oblique",
    "DejaVu Sans Condensed",
    "DejaVu Sans Condensed Bold",
    "DejaVu Sans Condensed Bold Oblique",
    "DejaVu Sans Condensed Oblique",
    "DejaVu Sans ExtraLight",
    "DejaVu Sans Oblique",
    "DejaVu Serif",
    "DejaVu Serif Bold",
    "DejaVu Serif Bold Italic",
    "DejaVu Serif Condensed",
    "DejaVu Serif Condensed Bold",
    "DejaVu Serif Condensed Bold Italic",
    "DejaVu Serif Condensed Italic",
    "DejaVu Serif Italic",
    "Dialog.bold",
    "Dialog.bolditalic",
    "Dialog.italic",
    "Dialog.plain",
    "DialogInput.bold",
    "DialogInput.bolditalic",
    "DialogInput.italic",
    "DialogInput.plain",
    "Dingbats ",
    "Hershey Gothic- English",
    "Hershey Gothic- German",
    "Hershey Gothic- Italian",
    "Hershey Plain- Duplex",
    "Hershey Plain- Duplex- Italic",
    "Hershey Plain- Triplex",
    "Hershey Plain- Triplex- Italic",
    "Hershey Script- Complex",
    "Hershey Script- Simplex",
    "Monospaced.bold",
    "Monospaced.bolditalic",
    "Monospaced.italic",
    "Monospaced.plain",
    "Nimbus Mono L Bold",
    "Nimbus Mono L Bold Oblique",
    "Nimbus Mono L Regular",
    "Nimbus Mono L Regular Oblique",
    "Nimbus Roman No9 L Medium",
    "Nimbus Roman No9 L Medium Italic",
    "Nimbus Roman No9 L Regular",
    "Nimbus Roman No9 L Regular Italic",
    "Nimbus Sans L Bold",
    "Nimbus Sans L Bold Condensed",
    "Nimbus Sans L Bold Condensed Italic",
    "Nimbus Sans L Bold Italic",
    "Nimbus Sans L Regular",
    "Nimbus Sans L Regular Condensed",
    "Nimbus Sans L Regular Condensed Italic",
    "Nimbus Sans L Regular Italic",
    "SansSerif.bold",
    "SansSerif.bolditalic",
    "SansSerif.italic",
    "SansSerif.plain",
    "Serif.bold",
    "Serif.bolditalic",
    "Serif.italic",
    "Serif.plain",
    "Standard Symbols L",
    "URW Bookman L Demi Bold",
    "URW Bookman L Demi Bold Italic",
    "URW Bookman L Light",
    "URW Bookman L Light Italic",
    "URW Chancery L Medium Italic",
    "URW Gothic L Book",
    "URW Gothic L Book Oblique",
    "URW Gothic L Demi",
    "URW Gothic L Demi Oblique",
    "URW Palladio L Bold",
    "URW Palladio L Bold Italic",
    "URW Palladio L Italic",
    "URW Palladio L Roman",
    "Utopia Bold",
    "Utopia Bold Italic",
    "Utopia Italic",
    "Utopia Regular"
  ],
  "system": {
    "java.runtime.name": "OpenJDK Runtime Environment",
    "java.specification.version": "1.8",
    "java.version": "1.8.0_201",
    "java.vm.version": "25.201-b09",
    "os.arch": "amd64",
    "java.runtime.version": "1.8.0_201-b09",
    "os.name": "Linux",
    "java.vm.name": "OpenJDK 64-Bit Server VM",
    "os.version": "4.14.133-97.112.amzn2.x86_64"
  },
  "numGlyphs": [
    "Cursor: 155",
    "Standard Symbols L: 190",
    "Dingbats : 203",
    "Hershey Plain- Duplex: 224",
    "Hershey Plain- Duplex- Italic: 224",
    "Hershey Plain- Triplex: 224",
    "Hershey Plain- Triplex- Italic: 224",
    "Hershey Script- Complex: 224",
    "Hershey Script- Simplex: 224",
    "Hershey Gothic- English: 228",
    "Hershey Gothic- German: 228",
    "Hershey Gothic- Italian: 228",
    "Bitstream Charter: 229",
    "Bitstream Charter Bold: 229",
    "Bitstream Charter Bold Italic: 229",
    "Bitstream Charter Italic: 229",
    "Courier 10 Pitch: 229",
    "Courier 10 Pitch Bold: 229",
    "Courier 10 Pitch Bold Italic: 229",
    "Courier 10 Pitch Italic: 229",
    "Utopia Bold: 229",
    "Utopia Bold Italic: 229",
    "Utopia Italic: 229",
    "Utopia Regular: 229",
    "URW Chancery L Medium Italic: 503",
    "URW Bookman L Demi Bold Italic: 512",
    "URW Bookman L Demi Bold: 519",
    "URW Bookman L Light Italic: 519",
    "DialogInput.bolditalic: 522",
    "Monospaced.bolditalic: 522",
    "Nimbus Mono L Bold Oblique: 522",
    "URW Palladio L Italic: 522",
    "URW Palladio L Bold: 523",
    "URW Palladio L Bold Italic: 523",
    "URW Palladio L Roman: 525",
    "DialogInput.italic: 533",
    "Monospaced.italic: 533",
    "Nimbus Mono L Regular Oblique: 533",
    "Nimbus Roman No9 L Medium Italic: 535",
    "Nimbus Roman No9 L Regular Italic: 535",
    "DialogInput.bold: 540",
    "Monospaced.bold: 540",
    "Nimbus Mono L Bold: 540",
    "URW Bookman L Light: 545",
    "Nimbus Sans L Bold Condensed: 561",
    "Century Schoolbook L Bold: 563",
    "Century Schoolbook L Bold Italic: 563",
    "Century Schoolbook L Italic: 563",
    "Century Schoolbook L Roman: 563",
    "DialogInput.plain: 563",
    "Monospaced.plain: 563",
    "Nimbus Mono L Regular: 563",
    "Nimbus Roman No9 L Medium: 563",
    "Nimbus Roman No9 L Regular: 563",
    "Nimbus Sans L Bold: 563",
    "Nimbus Sans L Bold Condensed Italic: 563",
    "Nimbus Sans L Bold Italic: 563",
    "Nimbus Sans L Regular: 563",
    "Nimbus Sans L Regular Condensed: 563",
    "Nimbus Sans L Regular Condensed Italic: 563",
    "Nimbus Sans L Regular Italic: 563",
    "URW Gothic L Book: 563",
    "URW Gothic L Book Oblique: 563",
    "URW Gothic L Demi: 563",
    "URW Gothic L Demi Oblique: 563",
    "DejaVu Sans ExtraLight: 1915",
    "DejaVu Serif Bold Italic: 3320",
    "DejaVu Serif Condensed Bold Italic: 3320",
    "Serif.bolditalic: 3320",
    "DejaVu Serif Bold: 3321",
    "DejaVu Serif Condensed Bold: 3321",
    "Serif.bold: 3321",
    "DejaVu Serif: 3399",
    "DejaVu Serif Condensed: 3399",
    "Serif.plain: 3399",
    "DejaVu Serif Condensed Italic: 3449",
    "DejaVu Serif Italic: 3449",
    "Serif.italic: 3449",
    "DejaVu Sans Bold Oblique: 5085",
    "DejaVu Sans Condensed Bold Oblique: 5085",
    "Dialog.bolditalic: 5085",
    "SansSerif.bolditalic: 5085",
    "DejaVu Sans Condensed Oblique: 5155",
    "DejaVu Sans Oblique: 5155",
    "Dialog.italic: 5155",
    "SansSerif.italic: 5155",
    "DejaVu Sans Bold: 5854",
    "DejaVu Sans Condensed Bold: 5854",
    "Dialog.bold: 5854",
    "SansSerif.bold: 5854",
    "DejaVu Sans: 5928",
    "DejaVu Sans Condensed: 5928",
    "Dialog.plain: 5928",
    "SansSerif.plain: 5928"
  ]
}
```

## 参考資料

- [Font \(Java Platform SE 8 \)](https://docs.oracle.com/javase/jp/8/docs/api/java/awt/Font.html)
- [GraphicsEnvironment \(Java Platform SE 8 \)](https://docs.oracle.com/javase/jp/8/docs/api/java/awt/GraphicsEnvironment.html)
- [Lambda Javaで使えるフォント一覧 \- カタヤマンがプログラマチックに今日もコードアシスト](http://c9katayama.hatenablog.com/entry/20141127/1417044739)