KAUFEN
Suchen
PDFlib

fonts/opentype_feature_tester

Download Java Code     Switch to PHP Code      Show Output PDF

/*

 * Test supported OpenType features in a font

 * (see opentype_features_for_cjk Cookbook topic for CJK features)

 *

 * Demonstrate supported typographic OpenType features after checking

 * whether a particular feature is supported in a font. Only those features

 * will be shown which are

 * a) available in the font

 * b) are supported by PDFlib's "features" option

 * c) are not used automatically for shaping and layout features

 *

 * Notes:

 * - The sample characters used for showing a particular font feature may

 *   or may not match the font's feature. If a feature is listed, but no

 *   effect is visible you may have to switch to different test characters.

 * - OpenType features for Chinese, Japanese, and Korean text and

 *   demonstrated in the opentype_features_for_cjk Cookbook topic.

 *

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

 * Required data: LinLibertine_R.otf font

 *                (replace with suitable font with OpenType feature tables)

 *

 * Depending on the features in your sample font you must replace the

 * sample text fragments below in order to see some effect of the features.

 */


package com.pdflib.cookbook.pdflib.fonts;


import com.pdflib.pdflib;

import com.pdflib.PDFlibException;


class opentype_feature_tester {

    public static void main(String argv[]) {

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

        final String searchpath = "../input";

        final String outfile = "opentype_feature_tester.pdf";

        final String title = "OpenType Feature Tester";


        String optlist;

        pdflib p = null;

        int i, group, table, font;

        final double llx = 50, lly = 50, urx = 800, ury = 550;

        String result;

        int exitcode = 0;


        /*

         * Name of the test font. Replace with the name of your OpenType font.

         */

        final String testfont = "LinLibertine_R";


        final String headers[] = {

            "Feature", "Description", "Feature disabled", "Feature enabled"

        };


        final String groupdescriptions[] = {

            "Supported OpenType features for Western typography",

            "Stylistic Sets",

            "Supported OpenType features for Chinese, Japanese, and Korean text"

        };


        class testcase {

            testcase(String feature,

                    String additionaloptions,

                    String description,

                    String text) {

                this.feature = feature;

                this.additionaloptions = additionaloptions;

                this.description = description;

                this.text = text;

            }


            String feature;

            String additionaloptions;   // for script and language options

            String description;

            String text;

        }


        /*

         * Features supported by PDFlib along with common test strings. Change

         * the test strings to match the features in the tested font.

         */

        final testcase testcases[][] = {

            {

                /* Western features */

                new testcase("afrc", "", "alternative fractions",

                                "1/2 1/4 3/4"),

                new testcase("c2pc", "", "petite capitals from capitals",

                                "ABCDEFG"),

                new testcase("c2sc", "", "small capitals from capitals",

                                "ABCDEFG"),

                new testcase("calt", "", "contextual alternates",

                                "ABCDEFG"),

                new testcase("case", "", "case-sensitive forms",

                    "(UN) [UN] (ISO693) »A-B«"),

                new testcase("ccmp", "", "glyph composition",

                                "ABCDEFG"),

                new testcase("clig", "", "contextual ligatures",

                                "ABCDEFG"),

                new testcase("cswh", "", "contextual swash",

                                "ABCDEFG"),

                new testcase("dlig", "", "discretionary ligatures",

                    "ch tz st c/o TEL"),

                new testcase("dnom", "", "denominators",

                                "0123456789"),

                new testcase("falt", "", "final glyph alternates",

                                "abcdefghijABCDEFGH0123"),

                new testcase("fina", "", "final forms",

                                "σ"),

                new testcase("frac", "", "fractions",

                                "1/2 1/4 3/4"),

                new testcase("hist", "", "historical forms",

                                "s"),

                new testcase("hlig", "", "historical ligatures",

                    "ct st &.longs;b &.longs;t"),

                new testcase("init", "", "initial forms",

                                "abcdefghijABCDEFGH0123"),

                new testcase("isol", "", "isolated forms",

                                "abcdefghijABCDEFGH0123"),

                new testcase("liga", "", "standard ligatures",

                                "ff fi fl ffi ffl"),

                new testcase("lnum", "", "lining figures",

                                "0123456789"),

                new testcase("locl", "script=latn language=ROM",

                                "localized forms",

                                "&.Scedilla;&.Tcedilla;"),

                new testcase("medi", "", "medial forms",

                                "abcdefghijABCDEFGH0123"),

                new testcase("mgrk", "", "mathematical Greek",

                                "Σ"),

                new testcase("numr", "", "numerators",

                                "0123456789"),

                new testcase("onum", "", "old-style figures",

                                "0123456789"),

                new testcase("ordn", "", "ordinals",

                                "No a o A O 1o 2a 3o"),

                new testcase("ornm", "", "ornaments",

                                "• abcqrstuvwABC"),

                new testcase("pcap", "", "petite capitals",

                                "ABCDEFG"),

                new testcase("pnum", "", "proportional figures",

                                "0123456789"),

                new testcase("salt", "", "stylistic alternates",

                                "& abcdefgh"),

                new testcase("sinf", "", "scientific inferiors",

                                "H2SO4"),

                new testcase("smcp", "", "small capitals",

                                "PostScript"),

                new testcase("subs", "", "subscript ",

                                "0123456789"),

                new testcase("sups", "", "superscript",

                                "0123456789"),

                new testcase("swsh", "", "swash",

                                "PQRSTpqrst &"),

                new testcase("titl", "", "titling",

                                "ABCDEFG"),

                new testcase("tnum", "", "tabular figures",

                                "0123456789"),

                new testcase("unic", "", "unicase",

                                "Filosofia"),

                new testcase("zero", "", "slashed zero", "0"),

            },

            {

                /* Stylistic Sets */

                /* The text fragments are tuned for the stylistic sets which

                 * are present in our sample font.

                 * Modify the text as appropriate for your font.

                 */

                new testcase("ss01", "", "stylistic set 1",

                    "[\u00C4\u00D6\u00DC]"),

                new testcase("ss02", "", "stylistic set 2",

                    "JKR"),

                new testcase("ss03", "", "stylistic set 3",

                    "ß"),

                new testcase("ss04", "", "stylistic set 4",

                    "&.Germandbls;"),

                new testcase("ss05", "", "stylistic set 5",

                    "W"),

                new testcase("ss06", "", "stylistic set 6",

                    "&"),

                new testcase("ss07", "", "stylistic set 7",

                    "abcdefghijABCDEFGH0123"),

                new testcase("ss08", "", "stylistic set 8",

                    "abcdefghijABCDEFGH0123"),

                new testcase("ss09", "", "stylistic set 9",

                    "abcdefghijABCDEFGH0123"),

                new testcase("ss10", "", "stylistic set 10",

                    "abcdefghijABCDEFGH0123"),

                new testcase("ss11", "", "stylistic set 11",

                    "abcdefghijABCDEFGH0123"),

                new testcase("ss12", "", "stylistic set 12",

                    "abcdefghijABCDEFGH0123"),

                new testcase("ss13", "", "stylistic set 13",

                    "abcdefghijABCDEFGH0123"),

                new testcase("ss14", "", "stylistic set 14",

                    "abcdefghijABCDEFGH0123"),

                new testcase("ss15", "", "stylistic set 15",

                    "abcdefghijABCDEFGH0123"),

                new testcase("ss16", "", "stylistic set 16",

                    "abcdefghijABCDEFGH0123"),

                new testcase("ss17", "", "stylistic set 17",

                    "abcdefghijABCDEFGH0123"),

                new testcase("ss18", "", "stylistic set 18",

                    "abcdefghijABCDEFGH0123"),

                new testcase("ss19", "", "stylistic set 19",

                    "abcdefghijABCDEFGH0123"),

                new testcase("ss20", "", "stylistic set 20",

                    "abcdefghijABCDEFGH0123"),

            }

        };


        try {

            p = new pdflib();

            int row, col;


            p.set_option("searchpath={" + searchpath + "}");

            p.set_option("charref=true");

            /*

             * This means that formatting and other errors will raise an

             * exception. This simplifies our sample code, but is not

             * recommended for production code.

             */

            p.set_option("errorpolicy=exception");


            /* Set an output path according to the name of the topic */

            if (p.begin_document(outfile, "") == -1) {

                throw new Exception("Error: " + p.get_apiname() + ": "

                    + p.get_errmsg());

            }


            p.set_info("Creator", "PDFlib Cookbook");

            p.set_info("Title", title);


            table = -1;

            row = 1;

            col = 1;


            /* Table header */

            optlist = "fittextline={fontname=Helvetica-Bold encoding=unicode "

                + "fontsize=12} margin=4";

            table = p.add_table_cell(table, col, row++,

                "Supported OpenType features in font " + testfont, optlist

                    + " colspan=4");


            for (i = 0; i < headers.length; i++) {

                col = i + 1;

                table = p.add_table_cell(table, col, row, headers[i], optlist);

            }

            row++;


            for (group = 0; group < testcases.length; group += 1) {

                int featurecount = 0;


                col = 1;


                optlist = "fittextline={fontname=Helvetica-BoldOblique "

                    + "encoding=unicode fontsize=12} "

                    + "margin=4 colspan=4 rowjoingroup= " + group;

                table = p.add_table_cell(table, col, row++,

                    groupdescriptions[group], optlist);


                /*

                 * Create a table with feature samples, one feature per table

                 * row

                 */

                for (i = 0; i < testcases[group].length; i += 1) {

                    final testcase testcase = testcases[group][i];

                    col = 1;


                    /* skip unavailable features */

                    font = p.load_font(testfont, "unicode", "embedding");

                    optlist = "name=" + testcase.feature;

                    if (p.info_font(font, "feature", optlist) != 1)

                        continue;


                    featurecount++;


                    /* Common option list for columns 1-3 */

                    optlist = "fittextline={fontname=Helvetica "

                        + "encoding=unicode fontsize=12} margin=4";


                    /* Column 1: feature name */

                    table = p.add_table_cell(table, col++, row,

                        testcase.feature + " " + testcase.additionaloptions,

                        optlist);


                    /* Column 2: feature description */

                    table = p.add_table_cell(table, col++, row,

                        testcase.description, optlist);


                    /* Column 3: raw input text with feature disabled */

                    optlist = "fittextline={fontname={" + testfont

                        + "} encoding=unicode fontsize=12 embedding} margin=4";

                    table = p.add_table_cell(table, col++, row, testcase.text,

                        optlist);


                    /* Column 4: text with enabled feature */

                    optlist = "fittextline={fontname={" + testfont

                        + "} encoding=unicode fontsize=12 embedding "

                        + "features={" + testcase.feature"} "

                        + testcase.additionaloptions + "} margin=4";

                    table = p.add_table_cell(table, col++, row, testcase.text,

                        optlist);


                    row++;

                }


                if (featurecount == 0) {

                    col = 1;


                    optlist = "fittextline={fontname=Helvetica "

                        + "encoding=unicode fontsize=12} "

                        + "margin=4 colspan=4 rowjoingroup= " + group;

                    table = p.add_table_cell(table, col, row++, "(none)",

                        optlist);

                }

            }


            /*

             * Loop until all of the table is placed; create new pages as long

             * as more table instances need to be placed.

             */

            do {

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


                optlist = "header=2 fill={{area=rowodd fillcolor={gray 0.9}}} "

                    + "stroke={{line=other}} ";


                /* Place the table instance */

                result = p.fit_table(table, llx, lly, urx, ury, optlist);


                if (result.equals("_error"))

                    throw new Exception("Couldn't place table: "

                        + p.get_errmsg());


                p.end_page_ext("");


            }

            while (result.equals("_boxfull"));


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

            exitcode = 1;

        }

        catch (Exception e) {

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

            exitcode = 1;

        }

        finally {

            if (p != null) {

                p.delete();

            }

            System.exit(exitcode);

        }

    }

}