--Run a Java program on AWS Lambda to get a list of available fonts
├── 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>
{
"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"
]
}
Recommended Posts