BASKET
Search
PDFlib

fonts/font_metrics_info

Download Java Code     Switch to PHP Code     Show Output PDF

/* $Id: font_metrics_info.java,v 1.8 2013/01/15 10:11:58 stm Exp $

 * Font metrics info:

 * Get various font related metrics such as the ascender or descender

 *

 * Use the info_font() function to get the metrics values for the capheight,

 * ascender, descender, or xheight of the font. Visualize these metrics

 * using the "matchbox" feature.

 *

 * Required software: PDFlib/PDFlib+PDI/PPS 9

 * Required data: font file

 */

package com.pdflib.cookbook.pdflib.fonts;


import java.text.*;

import com.pdflib.pdflib;

import com.pdflib.PDFlibException;


public class font_metrics_info

{

    public static void main (String argv[])

    {

    /* This is where the data files are. Adjust as necessary. */

    String searchpath = "../input";

    String outfile = "font_metrics_info.pdf";

    String title = "Font Metrics Info";


    pdflib p = null;

    int font;

    double capheight, ascender, descender, xheight;

    String optlist, text = "ABCdefghij";

    int x=150, y=140;


    NumberFormat form = NumberFormat.getInstance();


    try {

        p = new pdflib();


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


        /* This means we must check return values of load_font() etc. */

        p.set_option("errorpolicy=return");


        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.8 $");


        /* Start page */

        p.begin_page_ext(0, 0, "width=300 height=200");


        font = p.load_font("LuciduxSans-Oblique", "unicode", "embedding");

        if (font == -1)

            throw new Exception("Error: " + p.get_errmsg());


        /* Retrieve the font metrics for a font size of 10. If no fontsize

         * is supplied the metrics will be based on a font size of 1000.

         */

        capheight = p.info_font(font, "capheight", "fontsize=10");

        ascender = p.info_font(font, "ascender", "fontsize=10");

        descender = p.info_font(font, "descender", "fontsize=10");

        xheight = p.info_font(font, "xheight", "fontsize=10");


        /* Set the format of the metrics values to two fractional digits */

        form.setMaximumFractionDigits(2);


        p.setfont(font, 10);


        /* Output each of the font metrics with appropriate formatting as well

         * as the sample text "ABCdefghij" with the metrics value colorized

         */

        p.fit_textline("capheight for font size 10: " + form.format(capheight),

            x, y, "alignchar :");

        optlist = "matchbox={fillcolor={rgb 1 0.8 0.8} " +

            "boxheight={capheight none}}";

        p.fit_textline(text, x+60, y, optlist);


        p.fit_textline("ascender for font size 10: " + form.format(ascender),

            x, y-=30, "alignchar :");

        optlist = "matchbox={fillcolor={rgb 1 0.8 0.8} " +

            "boxheight={ascender none}}";

        p.fit_textline(text, x+60, y, optlist);


        p.fit_textline("descender for font size 10: " + form.format(descender),

            x, y-=30, "alignchar :");

        optlist = "matchbox={fillcolor={rgb 1 0.8 0.8} " +

            "boxheight={none descender}}";

        p.fit_textline(text, x+60, y, optlist);


        p.fit_textline("xheight for font size 10: " + form.format(xheight),

            x, y-=30, "alignchar :");

        optlist = "matchbox={fillcolor={rgb 1 0.8 0.8} " +

            "boxheight={xheight none}}";

        p.fit_textline(text, x+60, y, optlist);


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

        } catch (Exception e) {

            System.err.println(e.getMessage());

        } finally {

            if (p != null) {

                p.delete();

            }

        }

    }

}