How to create a font samples document for your system

0
72

Introduction

Recently, I created a simple Java application that creates a text file named “font-list.html”. This HTML document contains sample text for every font in the system. You can view the HTML document in a Web browser on that system. (You can also print it to a PDF file if you have a software PDF printer driver.)

Background

Web designers and graphic artists are always on the look out for new fonts and quickly build up a huge collection of font files installed on their system. Ever since I downloaded and installed all the “Google Web Fonts” collection to my computer, it has become impossible to remember what each font looked like or what options I had for a new project. There are many font viewer applications but they all require the user to select the fonts manually to examine them.

I wondered if I could create a simple HTML or PDF file with all the fonts in the system so that they could be examined at leisure.

Using the code

Here is the source code. (As it is written in Java, it works on Linux systems without any change.) The class uses the getAllFonts() method of the GraphicsEnvironment class to get a list of all fonts. Then, it generates HTML tags with the sample text. The fonts are specified using CSS attributes. The generated HTML tags are stored in a string buffer and then saved to a text file named font-list.html.

import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class FontList {

 public static void main(String[] args) {
 StringBuffer oHTML = new StringBuffer("<doctype html>\n<html>\n<head>\n<title>Font List</title>\n</head>\n<body>");

 GraphicsEnvironment oEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();

 Font[] arAllFonts = oEnv.getAllFonts();

 for (Font oFont : arAllFonts) {
 String sStyle = " ";
 
 if (oFont.getFontName().contains("Bold") ||
 oFont.getFontName().contains("Heavy")) {
 sStyle = "; font-style: normal; font-weight: bold; ";
 }
 
 if (oFont.getFontName().contains("Italic") ||
 oFont.getFontName().contains("Oblique") || oFont.getFontName().contains("Slanted")) {
 sStyle = "; font-style: italic; font-weight: normal; ";
 }
 
 if (oFont.getFontName().contains("Bold") &&
 (oFont.getFontName().contains("Italic") || oFont.getFontName().contains("Oblique") || oFont.getFontName().contains("Slanted"))) {
 sStyle = "; font-style: italic; font-weight: bold; ";
 }

 oHTML.append("<h3>" + oFont.getFontName() + " [ <span style=\"" + sStyle + " font-family: '" + oFont.getFontName() + "'; \">" + oFont.getFontName() + "</span> ]</h3>\n\n\n<ul>");
 oHTML.append("<li style=\"" + sStyle + " font-size: 10pt; font-family: '" + oFont.getFamily() + "'; \">10pt: ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz 0123456789 The Quick Brown Fox Jumps Over Lazy Dog</li>\n");
 oHTML.append("<li style=\"" + sStyle + " font-size: 12pt; font-family: '" + oFont.getFamily() + "'; \">12pt: ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz 0123456789 The Quick Brown Fox Jumps Over Lazy Dog</li>\n");
 oHTML.append("<li style=\"" + sStyle + " font-size: 16pt; font-family: '" + oFont.getFamily() + "'; \">16pt: ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz 0123456789 The Quick Brown Fox Jumps Over Lazy Dog</li>\n");
 oHTML.append("</ul>\n<hr />\n\n");
 }

 File oListFile = new File("font-list.html");
 if (oListFile.exists()) {
 if (!oListFile.delete()) {
 System.out.println("Unable to write to existing font-list.html file");
 return;
 }
 }

 if (createNewFile("font-list.html")) {
 if (writeTextToFile("font-list.html", oHTML.toString())) {
 
 } else {
 System.out.println("Unable to save list to font-list.html");
 }
 } else {
 System.out.println("Unable to create font-list.html");
 }

 }

 public static boolean createNewFile(String asPath) {
 File oFile = new File(asPath);
 try {
 if (oFile.createNewFile()) {
 return(true);
 }
 } catch (IOException e) {
 
 }
 return(false);
 }

 public static boolean writeTextToFile(String asTextFilePathname, String asText) {

 File oFile = new File(asTextFilePathname);

 if (oFile.exists()) {
 if (oFile.isDirectory()) {
 return(false);
 } else {
 if (!oFile.canWrite()) {
 return(false);
 }
 }
 } else {
 try {
 oFile.createNewFile();
 } catch (IOException e) {
 e.printStackTrace();
 return(false);
 }
 }

 FileWriter oWriter;
 try {
 oWriter = new FileWriter(oFile, false);
 BufferedWriter oBuffWriter = new BufferedWriter(oWriter);
 oBuffWriter.write(asText);
 oBuffWriter.close();
 oWriter.close();
 } catch (IOException e) {
 e.printStackTrace();
 return(false);
 } 

 return(true);
 }

} 

As this Java application is in the form of source code, you need JDK, not just JRE. Compile the Java file first and then run the generated class file.

Compile and run the Java source file using JDK.

Open the font-list.html file in a web browser. Depending on your font collection, it may be a one long web page. But, it will be much more easier to scroll through it than working with a font viewer application.

Font samples document can be examined in a web browser.

Points of Interest

You can also print the web page to a PDF file using a software printer driver. The PDF file will have the fonts embedded in it and you will be able to examine it in a different device. In contrast, the HTML document is dependent to the original computer in which the list was generated.

If the HTML document is exported to PDF, it can be viewed in another device.

LEAVE A REPLY