PDFlib Cookbook

cookbook

fonts/type3_turkish_character

Download Java Code     Switch to PHP Code     Show Output PDF

/*
 * $Id: type3_turkish_character.java,v 1.9 2013/02/20 10:35:48 stm Exp $
 * 
 * Type 3 turkish character:
 * Create a Type 3 font by using another font to create a new glyph from a
 * combination of two glyphs. Use this font as a fallback font.
 *
 * Create a Type 3 font "CourierTurkish" on the basis of the core font
 * "Courier".
 * Add the synthetic glyph U+0130 LATIN CAPITAL LETTER I WITH DOT ABOVE
 * and output text with that glyph.
 * To construct the synthetic character use the auxiliary diacritical character
 * U+02D9 DOT ABOVE plus U+0049 LATIN CAPITAL LETTER I.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: none
 */
package com.pdflib.cookbook.pdflib.fonts;

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

public class type3_turkish_character {
    public static void main(String argv[]) {
        String outfile = "type3_turkish_character.pdf";
        String title = "Type 3 Turkish Character";

        pdflib p = null;

        try {
            final double fontsize = 14;
            
            p = new pdflib();

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

            /* Start page */
            p.begin_page_ext(0, 0, "width=200 height=200");

            /*
             * --------------------------------------------
             * Step 1: Define the new font "CourierTurkish"
             * --------------------------------------------
             */
            
            /* Load the font from which the glyphs will be taken for creating
             * the glyph for U+0130 LATIN CAPITAL LETTER I WITH DOT ABOVE. 
             */
            int basefont = p.load_font("Courier", "unicode", "");
            if (basefont == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /*
             * Base the width of the new glyph on the width of the similar
             * character 'I', but any character could be used in this example
             * as Courier is a monospace font. Set the font size to 1000 because
             * the new font uses a 1000x1000 coordinate system.
             */
            double glyphwidth = p.info_textline("I", "width",
                            "font=" + basefont + " fontsize=1000");

            p.begin_font("CourierTurkish",
                                0.001, 0.0, 0.0, 0.001, 0.0, 0.0, "");

            /* .notdef is required; the width of .notdef does not really
             * matter
             */
            p.begin_glyph_ext(0x0000, "width=600");
            p.end_glyph();
            
            /*
             * The bounding box must be large enough to contain the
             * respective glyph. Better create the bounding box too large
             * than risk problems.
             */
            p.begin_glyph_ext(0x0130, "width=" + glyphwidth + " "
                        + "boundingbox={0 -250  600 800} "
                        + "glyphname=Idottaccent");
            p.setfont(basefont, 1000);

            /*
             * Now for the crucial trick: Create a new glyph by combining
             * the two existing glyphs "I" ...
             */
            p.fit_textline("I", 0, 0, "");

            /*
             * ... and U+02D9 DOT ABOVE. Shift the accent upwards to
             * nicely position it on top of the "I".
             */
            p.fit_textline("\u02d9", 0.0, 160.0, "");
            p.end_glyph();

            p.end_font();

            /*
             * ------------------------
             * Step 2: Use the new font
             * ------------------------
             */

            /*
             * Load the Courier font, now with the "CourierTurkish" Type 3
             * font as fallback font.
             */
            basefont = p.load_font("Courier", "unicode",
                "fallbackfonts={{fontname=CourierTurkish encoding=unicode}}");
            if (basefont == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /* Output "Istanbul" */
            p.fit_textline("Istanbul", 50, 140, "font=" + basefont
                + " fontsize=" + fontsize);

            /* Output U+0130 LATIN CAPITAL LETTER I WITH DOT ABOVE and
             * "stanbul".
             */
            p.fit_textline("\u0130stanbul", 50, 100, "font=" + basefont
                + " fontsize=" + fontsize);

            /* 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.toString());
        }
        finally {
            if (p != null)
                p.delete();
        }
    }
}