fonts/font_info
Query various properties of a font such as font name, font style, or encoding.
Download PHP Code Switch to Java Code Show Output
<?php
/*
* 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
*/
/* This is where the data files are. Adjust as necessary. */
$searchpath = dirname(__FILE__,3)."/input";
$outfile = "";
$title = "Font Info";
$x=20;
$xindent=150;
$y=820;
$yoffset=20;
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, "") == 0)
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 == 0)
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($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 = explode("/", $p->get_string($info, ""));
$fontname = $tmpname[count($tmpname)-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($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($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($info, $xindent, $y, "");
/* will the font be embedded? */
$p->fit_textline("willsubset:", $x, $y-=$yoffset, "");
$info = $p->info_font($font, "willsubset", "");
$p->fit_textline($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($info, $xindent, $y, "");
/* Italic angle of the font */
$p->fit_textline("italicangle:", $x, $y-=$yoffset, "");
$info = $p->info_font($font, "italicangle", "");
$p->fit_textline(sprintf("%.1f",$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($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($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($info, $xindent, $y, "");
/* Asender for the font (at size 1000) */
$p->fit_textline("ascender:", $x, $y-=$yoffset, "");
$info = $p->info_font($font, "ascender", "fontsize=1000");
$p->fit_textline(sprintf("%.1f", $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(sprintf("%.1f", $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(sprintf("%.1f", $info), $xindent, $y, "");
/* Finish page */
$p->end_page_ext("");
$p->end_document("");
$buf = $p->get_buffer();
$len = strlen($buf);
header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=font_info.pdf");
print $buf;
} catch (PDFlibException $e) {
echo("PDFlib exception occurred:\n".
"[" . $e->get_errnum() . "] " . $e->get_apiname() .
": " . $e->get_errmsg() . "\n");
exit(1);
} catch (Throwable $e) {
echo("PHP exception occurred: " . $e->getMessage() . "\n");
exit(1);
}
$p = 0;
?>