/*
* 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 10
* Required data: font file
*/
package com.pdflib.cookbook.pdflib.fonts;
import com.pdflib.pdflib;
import com.pdflib.PDFlibException;
import java.util.Locale;
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;
int exitcode = 0;
Locale.setDefault(Locale.US);
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);
/* Start page */
p.begin_page_ext(0, 0, "width=300 height=200");
font = p.load_font("NotoSerif-Regular", "unicode", "");
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");
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: " + String.format("%.2f",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: " + String.format("%.2f", 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: " + String.format("%.2f", 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: " + String.format("%.1f", 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.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);
}
}
}