fonts/glyph_replacement
Show the effects of glyph substitution in case of glyphs missing in the font.
Download PHP Code Switch to Java Code Show Output
<?php
/*
* Glyph replacement:
* Show the effects of glyph substitution in case of glyphs missing in the font
*
* Load the font and specify a replacement character to be used to output
* missing glyphs.
* Demonstrate various notations to output the Euro sign as U+20AC which is
* available in the font.
* Show various other combinations of available and unavailable glyphs.
*
* Required software: PDFlib/PDFlib+PDI/PPS 9
* Required data: font file
*/
/* This is where the data files are. Adjust as necessary. */
$searchpath = dirname(__FILE__,3)."/input";
$outfile = "";
$title = "Glyph Replacement";
$x=30;
$x2=180;
$x3=360;
$x4=520;
$y=550;
$yoff=30;
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, "") == 0)
throw new Exception("Error: " . $p->get_errmsg());
$p->set_info("Creator", "PDFlib Cookbook");
$p->set_info("Title", $title);
/* Load the font "NotoSerif-Regular" with "unicode" encoding.
* "replacementchar=?" defines a question mark to be used for glyph
* substitution in case of a missing glyph.
*/
$font = $p->load_font("NotoSerif-Regular", "unicode", "replacementchar=?");
if ($font == 0)
throw new Exception("Error: " . $p->get_errmsg());
/* Start page */
$p->begin_page_ext(0, 0, "width=a4.height height=a4.width");
/* Define three option lists for fit_textline().
* The "inp" option list is just for descriptive text.
* The "repl" option list enables the interpretation of character
* references using "charref" and uses glyph substitution which is
* enabled by default ("glyphcheck=replace").
* The second option list enables the interpretation of character
* references using "charref" but explicitly disables glyph
* substitution. Alternatively, glyph substitution can be disabled
* with $p->set_option("glyphcheck=none");
*/
$inp = "font=" . $font . " fontsize=14";
$repl = "charref font=" . $font . " fontsize=22";
$norepl = "charref glyphcheck=none font=" . $font . " fontsize=22";
/* Output some descriptive header lines for the input, output, and
* remark column
*/
$p->fit_textline("Glyphs in the NotoSerif-Regular font", $x, $y,
"font=" . $font . " fontsize=16");
$opts = " underline underlinewidth=1";
$p->fit_textline("Input", $x, $y-=2*$yoff, $inp . $opts);
$p->fit_textline("Output", $x2, $y, $inp . $opts);
$p->fit_textline("Remark", $x4, $y, $inp . $opts);
$p->fit_textline("glyphcheck=replace", $x2, $y-=$yoff, $inp . $opts);
$p->fit_textline("glyphcheck=none", $x3, $y, $inp . $opts);
/* ------------------------------------------------------------------
* Use various notations to output the Euro symbol as U+20AC which is
* available in the font.
* ------------------------------------------------------------------
*/
/* PHP Unicode notation,
* The first string "\\u{20AC}" is a descriptive text and results
* in an output of "\u{20AC}" since the two slashes are resolved by
* the PHP interpreter to one slash.
*/
$p->fit_textline("U+20AC literal", $x, $y-=$yoff, $inp);
$p->fit_textline("\u{20AC}", $x2, $y, $repl );
$p->fit_textline("\u{20AC}", $x3, $y, $norepl);
$p->fit_textline("Euro glyph available in the font", $x4, $y, $inp);
/* Character reference in HTML style with hexadecimal number */
$p->fit_textline("€", $x, $y-=$yoff, $inp);
$p->fit_textline("€", $x2, $y, $repl);
$p->fit_textline("€", $x3, $y, $norepl);
$p->fit_textline("Euro glyph available in the font", $x4, $y, $inp);
/* Character reference in HTML style with decimal number */
$p->fit_textline("€", $x, $y-=$yoff, $inp);
$p->fit_textline("€", $x2, $y, $repl);
$p->fit_textline("€", $x3, $y, $norepl);
$p->fit_textline("Euro glyph available in the font", $x4, $y, $inp);
/* Character reference in HTML style with entity name */
$p->fit_textline("€", $x, $y-=$yoff, $inp);
$p->fit_textline("€", $x2, $y, $repl);
$p->fit_textline("€", $x3, $y, $norepl);
$p->fit_textline("Euro glyph available in the font", $x4, $y, $inp);
/* Character reference using a glyph name provided by the font or the
* Adobe Glyph List (AGL)
*/
$p->fit_textline("&.Euro;", $x, $y-=$yoff, $inp);
$p->fit_textline("&.Euro;", $x2, $y, $repl);
$p->fit_textline("&.Euro;", $x3, $y, $norepl);
$p->fit_textline("Euro glyph available in the font", $x4, $y, $inp);
/* Character reference using a glyph name provided by the Adobe Glyph
* list (AGL)
*/
$p->fit_textline("&.uni20AC;", $x, $y-=$yoff, $inp);
$p->fit_textline("&.uni20AC;", $x2, $y, $repl);
$p->fit_textline("&.uni20AC;", $x3, $y, $norepl);
$p->fit_textline("Euro glyph available in the font", $x4, $y, $inp);
/* ---------------------------------------------------------------------
* Use various notations to output the ligature "ffi" which is available
* in the font
* ---------------------------------------------------------------------
*/
/* PHP Unicode notation */
$p->fit_textline("U+FB03 literal", $x, $y-=$yoff, $inp);
$p->fit_textline("\u{FB03}", $x2, $y, $repl );
$p->fit_textline("\u{FB03}", $x3, $y, $norepl );
$p->fit_textline("ffi ligature available in the font", $x4, $y, $inp);
/* Character reference using a glyph name provided by the font or the
* Adobe Glyph List (AGL)
*/
$p->fit_textline("&.ffi;", $x, $y-=$yoff, $inp);
$p->fit_textline("&.ffi;", $x2, $y, $repl);
$p->fit_textline("&.ffi;", $x3, $y, $norepl);
$p->fit_textline("ffi ligature available in the font", $x4, $y, $inp);
/* --------------------------------------------------------------------
* Output the glyph for a glyph which is not available in the font
* (this problem can be addressed with fallback fonts)
* --------------------------------------------------------------------
*/
$p->fit_textline("U+05D0 literal", $x, $y-=$yoff, $inp);
$p->fit_textline("\u{05D0}", $x2, $y, $repl);
$p->fit_textline("\u{05D0}", $x3, $y, $norepl);
$p->fit_textline("Hebrew glyph Aleph not available in the font", $x4, $y, $inp);
/* 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=glyph_replacement.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;
?>