pCOS Cookbook

cookbook

resources/advanced_font_properties

Create a list of all fonts contained in a PDF document and print advanced properties of the fonts.

Download Java Code  Show Output  Show Input (pCOS-path-reference.pdf) 

/*
 * Create a list of all fonts contained in a PDF document and print advanced
 * properties of the fonts.
 *
 * Required software: pCOS interface 6 (PDFlib+PDI/PPS 8, TET 4.x)
 * Required data: PDF document
 */
package com.pdflib.cookbook.pcos.resources;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.pdflib.IpCOS;
import com.pdflib.cookbook.pcos.pcos_cookbook_example;

public class advanced_font_properties extends pcos_cookbook_example {

    /* This is where the data files are. Adjust as necessary. */
    private final static String SEARCH_PATH = "../input";

    public void example_code(IpCOS p, int doc) throws Exception {

        System.out.println("File name: " + p.pcos_get_string(doc, "filename"));

        /* retrieve the number of fonts in the document */
        int fontcount = (int) p.pcos_get_number(doc, "length:fonts");
        System.out.println(fontcount + " fonts found in "
            + p.pcos_get_string(doc, "filename"));

        for (int i = 0; i < fontcount; i++) {
            print_font_info(p, doc, i);
        }
    }

    /**
     * Print advanced information about a single font.
     * 
     * @param p
     *          IpCOS object
     * @param doc
     *          document handle
     * @param f
     *          index of font in the global "fonts" array
     *          
     * @throws Exception
     */
    private void print_font_info(IpCOS p, int doc, int f) throws Exception {
        String prefix = "  ";
        
        System.out.println(p.pcos_get_string(doc, "fonts[" + f + "]/name"));
        
        boolean embedded = p.pcos_get_number(doc, "fonts[" + f
            + "]/embedded") != 0;
        System.out.println(prefix + "embedded: "+ (embedded ? "yes" : "no"));
        
        if (embedded) {
            /*
             * Only for an embedded font is makes sense to determine whether
             * the font is subsetted. This can be found out by looking at the
             * fullname. If it starts with six uppercase letters and a '+'
             * character, the font is a subset.
             * The presence of the prefix is determined with a regular 
             * expression here: Exactly six uppercase letters A-Z, followed
             * by a '+' character, followed by at least one additional
             * character.
             */
            String fullname = p.pcos_get_string(doc, 
                                            "fonts[" + f + "]/fullname");
            Pattern subset_prefix = Pattern.compile("[A-Z]{6}\\+.+");
            Matcher matcher = subset_prefix.matcher(fullname);
            System.out.println(prefix + "subset: " 
                                + (matcher.matches() ? "yes" : "no"));
        }
        else {
            System.out.println(prefix + "subset: n/a");
        }
        
        System.out.println(prefix + "writing mode: " +
            (p.pcos_get_number(doc, "fonts[" + f + "]/vertical") != 0
                ? "vertical"
                : "horizontal"));

        /*
         * Numeric properties: these are always present.
         */
        String numeric_properties[] = new String[] {
            "ascender", "descender", "italicangle", "capheight", "weight",
            "xheight"
        };
        
        for (int i = 0; i < numeric_properties.length; i += 1)
        {
            double value = p.pcos_get_number(doc, "fonts[" + f + "]/"
                                        + numeric_properties[i]);
            System.out.println(prefix + numeric_properties[i] + ": " + value);
        }
    }

    public advanced_font_properties(String[] argv, String readable_name,
        String search_path) {
        super(argv, readable_name, search_path);
    }

    public static void main(String argv[]) {
        advanced_font_properties example = new advanced_font_properties(argv,
            "Advanced Font Properties", SEARCH_PATH);
        example.execute();
    }
}