PDFlib Cookbook

cookbook

fonts/font_info

Download Java Code     Switch to PHP Code     Show Output PDF

/* $Id: font_info.java,v 1.12 2014/01/30 14:05:23 rjs Exp $
 * Font info:
 * Get various properties of a font such as font name, font style, or encoding
 *
 * Use info_font() with various keys and options to retrieve the required font
 * information
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * 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, fontstyle;
	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 + " $Revision: 1.12 $");

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

	    /* Load a font */
	    font = p.load_font("LuciduxSans-Oblique", "unicode", "embedding");
	    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 path name for the font outline file. This will be successful
	     * since in our case the font file name is identical to the font name
	     * "LuciduxSans-Oblique". In other cases the font file name can only be
	     * retrieved if it has been configured using set_parameter() and the
	     * "FontOutline" resource category.
	     */
	    p.fit_textline("fontfile:", x, y-=yoffset, "");

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

	    /* With PostScript Type 1 fonts, get the path name for the font metrics
	     * file (AFM or PFM). This will be successful since in our case the
	     * font metrics file name is identical to the font name
	     * "LuciduxSans-Oblique". In other cases the font file name can only be
	     * retrieved if it has been configured using set_parameter() and the
	     * "FontAFM" or "FontPFM" resource category.
	     */
	    p.fit_textline("metricsfile:", x, y-=yoffset, "");

	    info = p.info_font(font, "metricsfile", "");
	    if (info > -1) {
		fontname = p.get_string((int) info, "");
		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, "");
	    }

	    /* Get information about if the font is embedded as a CID font */
	    p.fit_textline("cidfont:", x, y-=yoffset, "");

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

	    /* Get information about if the font is 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, "");

	    /* Get the supplement number of the character collection for fonts with
	     * a standard CJK CMap
	     */
	    p.fit_textline("supplement:", x, y-=yoffset, "");

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

	    /* Get information about if the font is a standard font */
	    p.fit_textline("standardfont:", x, y-=yoffset, "");

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

	    /* Get information about if the font is 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, "");

	    /* Get information about if the font is embedded as a CID font */
	    p.fit_textline("cidfont:", x, y-=yoffset, "");

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

	    /* Get information about if the font is loaded with an encoding which
	     * allows unicode text
	     */
	    p.fit_textline("unicodefont:", x, y-=yoffset, "");

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

	    /* Get information about if a font subset will be created */
	    p.fit_textline("willsubset:", x, y-=yoffset, "");

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

	    /* Get information about if the font will be embedded */
	    p.fit_textline("willembed:", x, y-=yoffset, "");

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

	    /* Get information about if the font is for vertical writing mode */
	    p.fit_textline("vertical:", x, y-=yoffset, "");

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

	    /* Get the style of the font */
	    p.fit_textline("fontstyle:", x, y-=yoffset, "");

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

	    /* Get information about if the font style will be faked */
	    p.fit_textline("fontstyle (faked):", x, y-=yoffset, "");

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

	    /* Get the italic angle of the font (/ItalicAngle in the PDF font
	     * descriptor)
	     */
	    p.fit_textline("italicangle:", x, y-=yoffset, "");

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

	    /* Get the 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, "");

	    /* Get the number of CIDs if the font uses a standard CMap */
	    p.fit_textline("numcids:", x, y-=yoffset, "");

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

	    /* Get the 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, "");

	    /* Get the highest code value for the encoding of the font */
	    p.fit_textline("maxcode:", x, y-=yoffset, "");

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

	    /* Unicode value (for Unicode-compatible fonts) or code value
	     * (for Symbol fonts) of the replacement character of the font
	     */
	    p.fit_textline("replacementchar:", x, y-=yoffset, "");

	    info = p.info_font(font, "replacementchar", "");
	    if (info > -1) {
		p.fit_textline("U+00" +
		    Integer.toHexString((int) info).toUpperCase(), xindent, y, "");
	    }

	    /* Get the 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, "");

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

	    p.end_document("");

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