PDFlib
BASKET
PDFlib

fonts/type3_turkish_character

Create a Type 3 font by cloning another font and adding a further glyph. We use encoding iso8859-9 (since unicode doesn't work well with 8-bit Type 3 fonts). 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 (0xDD in iso8859-9) and output text with that glyph. To construct the synthetic character use the auxiliary diacritical character "dotaccent" U+02D9 DOT ABOVE plus U+0049 LATIN CAPITAL LETTER I. Place "dotaccent" on auxiliary slot 0x90 which is unused in iso8859-9.

Download Java Code     Show Output PDF

/* $Id: type3_turkish_character.java,v 1.2.4.1 2008/06/17 12:30:04 katja Exp $

 * Type 3 turkish character:

 * Create a Type 3 font by cloning another font and adding a further glyph

 *

 * We use encoding iso8859-9 (since unicode doesn't work well with 8-bit Type 3

 * fonts). 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

 * (0xDD in iso8859-9) and output text with that glyph.

 * To construct the synthetic character use the auxiliary diacritical character

 * "dotaccent" U+02D9 DOT ABOVE plus U+0049 LATIN CAPITAL LETTER I.

 * Place "dotaccent" on auxiliary slot 0x90 which is unused in iso8859-9.

 *

 * Required software: PDFlib/PDFlib+PDI/PPS 7

 * 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[])

    {

    /* This is where the data files are. Adjust as necessary. */

    String searchpath = "../input";

    String outfile = "type3_turkish_character.pdf";

    String title = "Type 3 Turkish Character";


    pdflib p = null;

    pdflib q = null;


    double info;

    int i, basefont, auxfont, newfont;


    String glyphnames[] = new String[256];

    double glyphwidths[] = new double [256];



    try {

        p = new pdflib();


        p.set_parameter("SearchPath", searchpath);


        /* This means we must check return values of load_font() etc. */

        p.set_parameter("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.2.4.1 $");


        /* Start page */

        p.begin_page_ext(0, 0, "width=200 height=200");



        /* -----------------------------

         * Step 1: Construct an encoding

         * -----------------------------

         */


        /* Load an auxiliary font with the encoding of interest "iso8859-9" */

        auxfont = p.load_font("Helvetica", "iso8859-9", "");


        /* Fetch all glyph names for the encoding */

        for (i = 0; i < 255; i++) {

            info = p.info_font(auxfont, "glyphname", "code=" + i);

            glyphnames[i] = p.get_parameter("string", info);

        }


        /* Add the auxiliary diacritical character "dotaccent" U+02D9 DOT ABOVE

         * to the extended encoding to slot 0x90. This character will be used to

         * construct the new glyph.

         */

        glyphnames[0x90] = "dotaccent";


        /* Construct an auxiliary encoding based on iso8859-9 and add all wanted

         * glyph names

         */

        for (i = 0; i < 255; i++)

            p.encoding_set_char("iso8859-9-extended", i, glyphnames[i], 0);


       

        /* --------------------------------------------------------------

         * Step 2: Derive the glyph metrics by fetching the glyph widths;

         * actually not required for monospaced fonts like Courier

         * --------------------------------------------------------------

         */


        /* To avoid the base font in the Output PDF perform the task with a

         * scratch PDF document. Supply an empty file name to begin_document()

         * to create the scratch file in memory.

         */

        try {

            q = new pdflib();

           

            /* Set the errorpolicy to "exception". With this setting PDFlib

             * functions will throw an exception if an error occurs

             */

            q.set_parameter("errorpolicy", "exception");


            q.begin_document("", "");


            /* Load the base font with "iso8859-9" encoding.

             *

             * Note: Since the "iso8859-9-extended" encoding is not available in

             * the scratch document context we use "iso8859-9". "iso8859-92" has

             * .notdef at position 0x90, and glyphwidths[0x90] will contain the

             * widths of .notdef. "iso8859-9-extended" has dotaccent at 0x90,

             * and the corresponding width. However, since we don't use the

             * width of the accent (only the width of the base character) it

             * doesn't hurt; for other situations (different pair of combined

             * characters) it could be relevant.

             */

            basefont = q.load_font("Courier", "iso8859-9", "");

                   

            q.begin_page_ext(595, 842, "");

           

            q.setfont(basefont, 1000);

            for (i = 0; i < 255; i++)

                glyphwidths[i] =

                    q.info_textline(String.valueOf((char) i), "width", "");


                    /* Set the glyph width of the synthetic glyph (0xDD) to the width

                     * of the 'T' character

                     */

            glyphwidths[0xDD] =

                        q.info_textline(String.valueOf('T'), "width", "");

           

                    q.end_page_ext("");


            q.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");

            throw (new Exception("Problem with scratch document; couldn't " +

                "determine glyph metrics"));

        } catch (Exception e) {

            System.err.println(e.getMessage());

            throw (new Exception("Problem with scratch document; couldn't " +

            "determine glyph metrics"));

        } finally {

            if (q != null)

                q.delete();

        }

       


        /* --------------------------------------------

         * Step 3: Define the new font "CourierTurkish"

         * --------------------------------------------

         */

        basefont = p.load_font("Courier", "iso8859-9-extended", "");

        if (basefont == -1)

            throw new Exception("Error: " + p.get_errmsg());

   

        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(".notdef", 600, 0, 0, 0, 0);

        p.end_glyph();


        /* Clone all available glyphs from the input font with the exception of

         * any ".notdef" glyphs

         */

        for (i = 0; i < 255; i++) {

            if (glyphnames[i].equals(".notdef")) {

                                continue;

                        }

            /* The bounding box must be large enough to contain the respective

             * glyph. We are lazy and use the font bounding box as a safe

             * estimate. Better create the bounding box too large than risk

             * problems.

             */

                        p.begin_glyph(glyphnames[i], glyphwidths[i], -23, -250, 623, 805);

                        p.setfont(basefont, 1000);


                        /* Now for the crucial trick:

             * Create a new glyph at position "0xDD" by combining the two

             * existing glyphs "I" and "dotaccent". The name of the new glyph

             * has already been set to "Idotaccent" above.

             */

            if (i == 0xDD) {

                /* Combine the "I" glyph... */

                p.show_xy("I", 0, 0);

               

                /* ...with the character at position 0x90 (which has been set

                 * to "dotaccent" above. Shift the accent upwards to nicely

                 * position it on top of the "I".

                 */

                p.show_xy(String.valueOf((char) 0x90), 0.0, 160.0);

            }

            else {

                p.show_xy(String.valueOf((char) i), 0, 0);

            }

            p.end_glyph();

        }


        p.end_font();



        /* ------------------------

         * Step 4: Use the new font

         * ------------------------

         */


        /* With the Courier core font output "Istanbul" */

        basefont = p.load_font("Courier", "iso8859-9", "");

        if (basefont == -1)

            throw new Exception("Error: " + p.get_errmsg());

       

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


        /* With the new Type 3 font output "\335stanbul" with "\335" being the

         * octal representation of the "Idotaccent" glyph (0xDD) defined above

         */

        newfont = p.load_font("CourierTurkish", "iso8859-9", "");

        if (newfont == -1)

            throw new Exception("Error: " + p.get_errmsg());

       

        p.fit_textline("\335stanbul", 50, 100, "font=" + newfont +

            " fontsize=14 charref");


        /* 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();

    }

    }

}