Find out the list of fonts available in AWS Lambda + Java

Overview

--Run a Java program on AWS Lambda to get a list of available fonts

Source code

Source code list

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

FontList.java

A Java program that retrieves available font information.

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>> {

  /**
   *This is the entry point when running as a standalone Java application.
   *
   * @param args not used
   */
  public static void main(String[] args) {

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

    //Font name
    for (String name : (String[]) output.get("names")) {
      System.out.println(name);
    }

    //Font name and number of glyphs
    for (String info : (String[]) output.get("numGlyphs")) {
      System.out.println(info);
    }

    //System information
    System.out.println(output.get("system"));
  }

  /**
   *Entry point when executing AWS Lambda function(Handler method)is.
   *
   * @param input input data
   * @param context AWS Lambda Context object
   * @return output data
   */
  @Override
  public Map<String, Object> handleRequest(Map<String, Object> input, Context context) {
    return invoke();
  }

  /**
   *Returns information about available fonts.
   *
   * @return Information on available fonts
   */
  private static Map<String, Object> invoke() {

    //Get a list of available fonts
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    Font[] allFonts = ge.getAllFonts();

    //Summarize the font information you want to output
    String[] names = Arrays.stream(allFonts)
      .map(Font::getName) //Font name
      .toArray(String[]::new);

    //Arrange in ascending order of glyph number
    String[] numGlyphs = Arrays.stream(allFonts)
      .sorted(Comparator.comparing(Font::getNumGlyphs)) //Sort by glyph number
      .map(f -> f.getName() + ": " + f.getNumGlyphs()) //Font name and number of glyphs
      .toArray(String[]::new);

    //Get system information
    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"));

    //Build output data
    Map output = new HashMap<String, Object>();
    output.put("names", names);
    output.put("numGlyphs", numGlyphs);
    output.put("system", system);
    return output;
  }

  /**
   *Gets the system properties indicated by the specified key.
   *
   * @param key The name of the system property
   * @return System property string value(Null if no property with that key exists)
   */
  private static String getSystemProperty(String key) {
    try {
      return System.getProperty(key);
    } catch (SecurityException e) {
      return e.toString();
    }
  }
}

pom.xml

Configuration file for building with Maven.

<?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>

Execution result on AWS Lambda + Java8

{
  "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"
  ]
}

Reference material

Recommended Posts

Find out the list of fonts available in AWS Lambda + Java
Examine the system information of AWS Lambda operating environment in Java
Examine the list of timezone IDs available in the Java ZoneId class
Find the maximum and minimum values out of the 5 numbers entered in Java (correction ver)
[Java] Delete the elements of List
List of members added in Java 9
List of types added in Java 9
The origin of Java lambda expressions
I want to find out which version of java the jar file I have is available
Get the result of POST in Java
The story of writing Java in Emacs
How to find out the Java version of a compiled class file
How to find the total number of pages when paging in Java
Find the maximum and minimum of the five numbers you entered in Java
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
About the idea of anonymous classes in Java
The story of learning Java in the first programming
Measure the size of a folder in Java
Feel the passage of time even in Java
Import files of the same hierarchy in Java
Code that deletes all files of the specified prefix in AWS S3 (Java)
Validate the identity token of a user authenticated with AWS Cognito in Java
Get the URL of the HTTP redirect destination in Java
[java] sort in list
[For beginners] Quickly understand the basics of Java 8 Lambda
[Java] Sort the list using streams and lambda expressions
Use Java lambda expressions outside of the Stream API
[Java] Get the file in the jar regardless of the environment
The objects in the List were references, right? Confirmation of
[Java] Get the file path in the folder with List
Change the storage quality of JPEG images in Java
Find the approximate value of log (1 + x) in Swift
Summarize the additional elements of the Optional class in Java 9
I tried to find out what changed in Java 9
Was done in the base year of the Java calendar week
A quick explanation of the five types of static in Java
Find out all timezone IDs supported by the Java TimeZone class
Create a SlackBot with AWS lambda & API Gateway in Java
Count the number of digits after the decimal point in Java
A program that counts the number of words in a List
How to derive the last day of the month in Java
Find the number of days in a month with Kotlin
Cannot find javax.annotation.Generated in Java 11
Find a subset in Java
Implementation of gzip in java
List aggregation in Java (Collectors.groupingBy)
Implementation of tri-tree in Java
Find out the maximum heap size when you specify the -Xmx or -XX: MaxRAM option in Java
[Java] Get the dates of the past Monday and Sunday in order
Output the difference between each field of two objects in Java
I tried the input / output type of Java Lambda ~ Map edition ~
The milliseconds to set in /lib/calendars.properties of Java jre is UTC
Cast an array of Strings to a List of Integers in Java
Get the public URL of a private Flickr file in Java
Let's create a TODO application in Java 5 Switch the display of TODO
How to get the length of an audio file in java
How to increment the value of Map in one line in Java
Find the difference between List types
Guess the character code in Java
Exceptions encountered in the AWS SDK