PDFlib Cookbook

cookbook

fonts/opentype_feature_tester

Demonstrate all supported typographic OpenType features after checking whether a particular feature is supported in a font.

Download PHP Code  Switch to Java Code  Show Output 

<?php
/*
 * Test all supported OpenType features in a font
 *
 * Demonstrate all 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.
 * - Vertical font features will not be visible in the output
 *   since only horizontal text is created.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: NotoSerif-Regular and NotoSerif-Bold fonts
 *                (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.
 */

/* This is where the data files are. Adjust as necessary. */
$searchpath = dirname(__FILE__,3)."/input";
$outfile = "";
$title = "OpenType Feature Tester";

$optlist;
$i; $group; $table; $font;
$llx = 50; $lly = 50; $urx = 800; $ury = 550;
$result;

/* 
 * Name of the test font. REplace with the name of your OpenType font
 */
$testfont = "NotoSerif-Regular";

$headers = array(
    "Feature", "Description", "Feature disabled", "Feature enabled"
);

$groupdescriptions = array(
    "Supported OpenType features for Western typography",
    "Stylistic Sets"
);

class testcase {
    function __construct($feature,
                $additionaloptions,
                $description,
                $text) {
        $this->feature = $feature;
        $this->additionaloptions = $additionaloptions;
        $this->description = $description;
        $this->text = $text;
    }
}

/*
 * Features supported by PDFlib along with common test strings. Change
 * the test strings to match the features in the tested font.
 */
$testcases = array(
    array(
        /* 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("ccmp", "", "glyph composition",
                        "ij"),
        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",
                        "\u{03C3}"),
        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",
                        "abcdefghijABCDFGH0123"),
        new testcase("mgrk", "", "mathematical Greek",
                        "\u{03A9}"),
        new testcase("numr", "", "numerators",
                        "0123456789"),
        new testcase("onum", "", "old-style figures",
                        "0123456789"),
        new testcase("ordn", "", "ordinals",
                        "a o 1o 2a 3o"),
        new testcase("ornm", "", "ornaments",
                        "\u{2022} 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"),
    ),
    array(
        /* Stylistic Sets */
        /* Modify the text as appropriate for your font.
         */
        new testcase("ss01", "", "stylistic set 1",
            "abcdefghijABCDEFGH0123"),
        new testcase("ss02", "", "stylistic set 2",
            "abcdefghijABCDEFGH0123"),
        new testcase("ss03", "", "stylistic set 3",
            "abcdefghijABCDEFGH0123"),
        new testcase("ss04", "", "stylistic set 4",
            "abcdefghijABCDEFGH0123"),
        new testcase("ss05", "", "stylistic set 5",
            "abcdefghijABCDEFGH0123"),
        new testcase("ss06", "", "stylistic set 6",
            "abcdefghijABCDEFGH0123"),
        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();
    $row=0; $col=0;

        $p->set_option("searchpath={" . $searchpath . "}");
        $p->set_option("charref=true");
    $p->set_option("stringformat=utf8");

    /*
     * 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, "") == 0) {
        throw new Exception("Error: " . $p->get_apiname() . ": "
            . $p->get_errmsg());
    }

    $p->set_info("Creator", "PDFlib Cookbook");
    $p->set_info("Title", $title);

    $table = 0;
    $row = 1;
    $col = 1;

    /* Table header */
    $optlist = "fittextline={fontname=NotoSerif-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 < count($headers); $i++) {
        $col = $i + 1;
        $table = $p->add_table_cell($table, $col, $row, $headers[$i], $optlist);
    }
    $row++;

    for ($group = 0; $group < count($testcases); $group += 1) {
        $featurecount = 0;

        $col = 1;

        $optlist = "fittextline={fontname=NotoSerif-Bold "
            . "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 < count($testcases[$group]); $i += 1) {
            $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=NotoSerif-Regular "
                . "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=NotoSerif-Regular "
                . "encoding=unicode fontsize=12} "
                . "margin=4 colspan=4 rowjoingroup= " . $group;
            $table = $p->add_table_cell($table, $col, $row++, "(none available)",
                $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 == "_error")
            throw new Exception("Couldn't place table: "
                . $p->get_errmsg());

        $p->end_page_ext("");

    }
    while ($result == "_boxfull");

    $p->end_document("");
    $buf = $p->get_buffer();
    $len = strlen($buf);

    header("Content-type: application/pdf");
    header("Content-Length: $len");
    header("Content-Disposition: inline; filename=opentype_feature_tester.pdf");
    print $buf;

} catch (PDFlibException $e) {
    echo("PDFlib exception occurred:\n".
        "[" . $e->get_errnum() . "] " . $e->get_apiname() .
        ": " . $e->get_errmsg() . "\n");
    exit(1);
} catch (Exception $e) {
    echo($e->getMessage());
    exit(1);
}

$p = 0;

?>