Click here to Skip to main content
15,867,453 members
Articles / Web Development / HTML5
Tip/Trick

How to Create a Font Samples Document for Your Computer

Rate me:
Please Sign up or sign in to vote.
4.48/5 (7 votes)
28 Jun 2017CPOL2 min read 11.9K   4  
A simple Java class to enumerate fonts in your systems and create a text samples document

Introduction

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. So, 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. The HTML document can be viewed in a Web browser and provides an easy preview of all the fonts on the computer. (It can also be printed to make a portable PDF file.)

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.

Java
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 one long web page. But, it will be much more easier to scroll through it than working with a font viewer application.

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer www.VSubhash.in
India India
V. Subhash is an invisible Indian writer, programmer and illustrator. In 2020, he wrote one of the biggest jokebooks of all time and then ended up with over two dozen mostly non-fiction books including Linux Command-Line Tips & Tricks, CommonMark Ready Reference, PC Hardware Explained, Cool Electronic Projects and How To Install Solar. His book Quick Start Guide to FFmpeg has been published by Apress/SpringerNature in 2023. He wrote, illustrated, designed and produced all of his books using only open-source software. Subhash has programmed in more than a dozen languages (as varied as assembly, Java and Javascript); published software for desktop (NetCheck), mobile (Subhash Browser & RSS Reader) and web (TweetsToRSS); and designed several websites. As of 2023, he is working on a portable Javascript-free CMS using plain-jane PHP and SQLite. Subhash also occasionally writes for Open Source For You magazine and CodeProject.com.

Comments and Discussions

 
-- There are no messages in this forum --