PDFlib Cookbook

cookbook

fonts/font_info

Query various properties of a font such as font name, font style, or encoding.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Font info:
 * Get various properties of a font such as font name and number of kerning pairs
 *
 * Use info_font() with various keys and options to retrieve the required font
 * information.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 10
 * Required data: font file
 */
package com.pdflib.cookbook.pdflib.fonts;

import com.pdflib.pdflib;
import com.pdflib.PDFlibException;

public class font_info
{
    public static void main (String argv[])
    {
        /* This is where the data files are. Adjust as necessary. */
        String searchpath = "../input";
        String outfile = "font_info.pdf";
        String title = "Font Info";

        pdflib p = null;
        int font;
        double info;
        String fontname;
        String[] tmpname;
        int x=20, xindent=150, y=820, yoffset=20;
        int exitcode = 0;

        try {
            p = new pdflib();

            /* This means we must check return values of load_font() etc. */
            p.set_option("errorpolicy=return");

            p.set_option("searchpath={" + searchpath + "}");

            if (p.begin_document(outfile, "") == -1)
                throw new Exception("Error: " + p.get_errmsg());

            p.set_info("Creator", "PDFlib Cookbook");
            p.set_info("Title", title);

            /* Start page */
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");

            /* Load a font */
            font = p.load_font("NotoSerif-Regular", "unicode", "");
            if (font == -1)
                throw new Exception("Error: " + p.get_errmsg());

            p.setfont(font, 12);

            /* Get the font name: use "api" to get the font name used by PDFlib.
             * (use "acrobat" for the Acrobat font name or use "full" for
             * retrieving the full font name)
             */
            p.fit_textline("fontname (api):", x, y-=yoffset, "");

            info = p.info_font(font, "fontname", "api");
            if (info > -1) {
                fontname = p.get_string((int) info, "");
                p.fit_textline(fontname, xindent, y, "");
            }

            /* Get the file name for the font outline file. This will be successful
             * since in our case the font file name is identical to the font name.
             * In other cases the font file name can only be
             * retrieved if it has been configured using set_option() and the
             * "FontOutline" resource category.
             */
            p.fit_textline("fontfile:", x, y-=yoffset, "");

            info = p.info_font(font, "fontfile", "");
            if (info > -1) {
                tmpname = (p.get_string((int) info, "")).split("/");
                fontname = tmpname[tmpname.length-1];
                p.fit_textline(fontname, xindent, y, "");
            }

            /* Get the encoding of the font: use "api" to get the encoding name
             * as specified in PDFlib (use "actual" for the name of the encoding
             * actually used for the font)
             */
            p.fit_textline("encoding (api):", x, y-=yoffset, "");

            info = p.info_font(font, "encoding", "api");
            if (info > -1) {
                fontname = p.get_string((int) info, "");
                p.fit_textline(fontname, xindent, y, "");
            }

            /* Is the font a host font? */
            p.fit_textline("hostfont:", x, y-=yoffset, "");

            info = p.info_font(font, "hostfont", "");
            p.fit_textline(Integer.toString((int) info), xindent, y, "");

            /* Is the font a symbol font? */
            p.fit_textline("symbolfont:", x, y-=yoffset, "");

            info = p.info_font(font, "symbolfont", "");
            p.fit_textline(Integer.toString((int) info), xindent, y, "");

            /* Will a font subset be created? */
            p.fit_textline("willsubset:", x, y-=yoffset, "");

            info = p.info_font(font, "willsubset", "");
            p.fit_textline(Integer.toString((int) info), xindent, y, "");

            /* Will the font be embedded? */
            p.fit_textline("willembed:", x, y-=yoffset, "");

            info = p.info_font(font, "willembed", "");
            p.fit_textline(Integer.toString((int) info), xindent, y, "");

            /* Italic angle of the font */
            p.fit_textline("italicangle:", x, y-=yoffset, "");

            info = p.info_font(font, "italicangle", "");
            p.fit_textline(Double.toString(info), xindent, y, "");

            /* Weight of the font (between 100 and 900); 400=normal, 700=bold */
            p.fit_textline("weight:", x, y-=yoffset, "");

            info = p.info_font(font, "weight", "");
            p.fit_textline(Integer.toString((int) info), xindent, y, "");

            /* Number of glyphs in the font */
            p.fit_textline("numglyphs:", x, y-=yoffset, "");

            info = p.info_font(font, "numglyphs", "");
            p.fit_textline(Integer.toString((int) info), xindent, y, "");

            /* Number of kerning pairs in the font */
            p.fit_textline("kerningpairs:", x, y-=yoffset, "");

            info = p.info_font(font, "kerningpairs", "");
            p.fit_textline(Integer.toString((int) info), xindent, y, "");
            
            /* Ascender for the font (at size 1000) */
            p.fit_textline("ascender:", x, y-=yoffset, "");

            info = p.info_font(font, "ascender", "fontsize=1000");
            p.fit_textline(Double.toString(info), xindent, y, "");            
            
            /* Descender for the font (at size 1000) */
            p.fit_textline("descender:", x, y-=yoffset, "");

            info = p.info_font(font, "descender", "fontsize=1000");
            p.fit_textline(Double.toString(info), xindent, y, ""); 

            /* Recommended additional line spacing for the font (at font size 1000) */
            p.fit_textline("linegap:", x, y-=yoffset, "");

            info = p.info_font(font, "linegap", "fontsize=1000");
            p.fit_textline(Double.toString(info), xindent, y, "");

            /* Finish page */
            p.end_page_ext("");

            p.end_document("");

        } catch (PDFlibException e) {
            System.err.println("PDFlib exception occurred:");
            System.err.println("[" + e.get_errnum() + "] " + e.get_apiname() +
                ": " + e.get_errmsg());
            exitcode = 1;
        } catch (Exception e) {
            System.err.println(e);
            exitcode = 1;
        } finally {
            if (p != null) {
                p.delete();
            }
            System.exit(exitcode);
        }
    }
}