PDFlib Cookbook

cookbook

fonts/glyph_replacement

Show the effects of glyph substitution in case of glyphs missing in the font.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * 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
 */
package com.pdflib.cookbook.pdflib.fonts;

import com.pdflib.pdflib;
import com.pdflib.PDFlibException;

public class glyph_replacement
{
public static void main (String argv[])
    {
        /* This is where the data files are. Adjust as necessary. */
        String searchpath = "../input";
        String outfile = "glyph_replacement.pdf";
        String title = "Glyph Replacement";

        pdflib p = null;
        int font, x=30, x2=180, x3=360, x4=520, y=550, yoff=30;
        String inp, repl, norepl;
        int exitcode = 0;
       
        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);
            
            /* 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 == -1)
                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");
            String 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.
             * ------------------------------------------------------------------ 
             */
            
            /* Java Unicode notation */
            p.fit_textline("U+20AC literal", x, y-=yoff, inp);  
            p.fit_textline("\u20AC", x2, y, repl);       
            p.fit_textline("\u20AC", 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
             * --------------------------------------------------------------------- 
             */
            
            /* Java Unicode notation */
            p.fit_textline("U+FB03 literal", x, y-=yoff, inp);
            p.fit_textline("\uFB03", x2, y, repl);
            p.fit_textline("\uFB03", 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("\u05D0", x2, y, repl); 
            p.fit_textline("\u05D0", 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("");

        } 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);
        }
    }
}