Ejemplo n.º 1
0
int
main(void)
{
    /* This is where the data files are. Adjust as necessary. */
    const wstring searchpath = L"../data";
    const wstring outfile = L"starter_opentype.pdf";

    wostringstream optlist;
    PDFlib p;
    int row, col, table, test, font;
    const double llx=50, lly=50, urx=800, ury=550;
    wstring result;

    /* This font will be used unless another one is specified in the table */
    const wstring defaulttestfont = L"DejaVuSerif";

    const wstring header[] = {
        L"OpenType feature",
        L"Option list",
        L"Font name",
        L"Raw input (feature disabled)",
        L"Feature enabled"
    };
    const int MAXCOL = sizeof(header) / sizeof(header[0]);

    typedef struct {
        const wstring description;
        /* the default test font above will be used if this string is empty */
        const wstring fontname;
        const wstring feature;
        const wstring text;
    } testcase;

    static const testcase testcases[] = {
    {
      L"ligatures",
      L"",
      L"liga",
       L"ff fi fl ffi ffl"
    },
    {
      L"discretionary ligatures",
      L"",
      L"dlig",
       L"st c/o"
    },
    {
      L"historical ligatures",
      L"",
      L"hlig",
       L"&.longs;b &.longs;t"
    },
    {
      L"small capitals",
      L"",
      L"smcp",
       L"PostScript"
    },
    {
      L"ordinals",
      L"",
      L"ordn",
       L"1o 2a 3o"
    },
    {
      L"fractions",
      L"",
      L"frac",
       L"1/2 1/4 3/4"
    },
    {
      L"alternate fractions",
      L"",
      L"afrc",
       L"1/2 1/4 3/4"
    },
    {
      L"slashed zero",
      L"",
      L"zero",
       L"0"
    },
    {
      L"historical forms",
      L"",
      L"hist",
       L"s"
    },
    {
      L"proportional figures",
      L"",
      L"pnum",
       L"0123456789"
    },
    {
      L"old-style figures",
      L"",
      L"onum",
       L"0123456789"
    },
    {
      L"lining figures",
      L"",
      L"lnum",
       L"0123456789"
    },
    {
      L"superscript",
      L"",
      L"sups",
       L"0123456789"
    },
    };
    const int n_testcases = sizeof(testcases) / sizeof(testcases[0]);

    try {
        p.set_parameter(L"SearchPath", searchpath);
        p.set_parameter(L"charref", L"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_parameter(L"errorpolicy", L"exception");

        /* Set an output path according to the name of the topic */
        if (p.begin_document(outfile, L"") == -1) {
            wcerr << L"Error: " << p.get_errmsg() << endl;
            return 2;
        }

        p.set_info(L"Creator", L"PDFlib starter sample");
        p.set_info(L"Title", L"starter_opentype");

        /* Start Page */
        p.begin_page_ext(0, 0, L"width=a4.height height=a4.width");

        table = -1;

        /* Table header */
        for (row=1, col=1; col <= MAXCOL; col++)
        {
            optlist.str(L"");
            optlist << L"fittextline={fontname=Helvetica-Bold encoding=unicode "
                       L"fontsize=12} margin=4";
            table = p.add_table_cell(table, col, row, header[col-1],
                                        optlist.str());
        }

        /* Create a table with feature samples, one feature per table row */
        for (row=2, test=0; test < n_testcases; row++, test++)
        {
            wstring testfont;
            wostringstream buf;

            /* Use the entry in the test table if available, and the
             * default test font otherwise. This way we can easily check
             * a font for all features, as well as insert suitable fonts
             * for individual features.
             */
            if (testcases[test].fontname.length() > 0)
                testfont = testcases[test].fontname;
            else
                testfont = defaulttestfont;

            col = 1;

            /* Common option list for columns 1-3 */
            optlist.str(L"");
            optlist << L"fittextline={fontname=Helvetica encoding=unicode "
                       L"fontsize=12} margin=4";

            /* Column 1: feature description */
            table = p.add_table_cell(table, col++, row,
                testcases[test].description,optlist.str());

            /* Column 2: option list */
            buf << L"features={" << testcases[test].feature << L"}";
            table = p.add_table_cell(table, col++, row, buf.str(),
                                        optlist.str());

            /* Column 3: font name */
            table = p.add_table_cell(table, col++, row, testfont,
                                        optlist.str());

            /* Column 4: raw input text with  feature disabled */
            optlist.str(L"");
            optlist << L"fittextline={fontname={" << testfont << L"} "
                       L"encoding=unicode fontsize=12 embedding} margin=4";
            table = p.add_table_cell(table, col++, row,
                    testcases[test].text, optlist.str());

            /* Column 5: text with enabled feature, or warning if the
             * feature is not available in the font
             */
            font = p.load_font(testfont, L"unicode", L"embedding");

            /* Check whether font contains the required feature table */
            optlist.str(L"");
            optlist << L"name=" << testcases[test].feature;
            if (p.info_font(font, L"feature", optlist.str()) == 1)
            {
                /* feature is available: apply it to the text */                optlist.clear();
                optlist.str(L"");
                optlist << L"fittextline={fontname={" << testfont << L"} "
                    << L"encoding=unicode fontsize=12 embedding features={"
                    << testcases[test].feature << L"}} margin=4";
                table = p.add_table_cell(table, col++, row,
                   testcases[test].text, optlist.str());
            }
            else
            {
                /* feature is not available: emit a warning */
                optlist.str(L"");
                optlist << L"fittextline={fontname=Helvetica encoding=unicode "
                           L"fontsize=12 fillcolor=red} margin=4";
                table = p.add_table_cell(table, col++, row,
                        L"(feature not available in this font)", optlist.str());
            }
        }

        /* Place the table */
        optlist.str(L"");
        optlist << L"header=1 fill={{area=rowodd fillcolor={gray 0.9}}} "
                   L"stroke={{line=other}}";
        result = p.fit_table(table, llx, lly, urx, ury, optlist.str());

        if (result == L"_error")
        {
            wcerr << L"Couldn't place table: " << p.get_errmsg() << endl;
            return 2;
        }

        p.end_page_ext(L"");
        p.end_document(L"");
    }
    catch (PDFlib::Exception &ex) {
        wcerr << L"PDFlib exception occurred:" << endl
              << L"[" << ex.get_errnum() << L"] " << ex.get_apiname()
              << L": " << ex.get_errmsg() << endl;
        return 2;
    }

    return 0;
}
Ejemplo n.º 2
0
int
main(void)
{
    try {
	/* This is where the data files are. Adjust as necessary. */
	string searchpath = "../data";

	string imagefile = "nesrin.jpg";

	int row, col, font, image, tf=-1, tbl=-1;
	int rowmax=50, colmax=5;
	PDFlib p;
	double llx= 50, lly=50, urx=550, ury=800;
	string headertext = "Table header (centered across all columns)";
	string result;
	char optlist[256];

	/* Dummy text for filling a cell with multi-line Textflow */
	string tf_text = 
"Lorem ipsum dolor sit amet, consectetur adi&shy;pi&shy;sicing elit, \
sed do eius&shy;mod tempor incidi&shy;dunt ut labore et dolore magna \
ali&shy;qua. Ut enim ad minim ve&shy;niam, quis nostrud exer&shy;citation \
ull&shy;amco la&shy;bo&shy;ris nisi ut ali&shy;quip ex ea commodo \
con&shy;sequat. Duis aute irure dolor in repre&shy;henderit in voluptate \
velit esse cillum dolore eu fugiat nulla pari&shy;atur. Excep&shy;teur \
sint occae&shy;cat cupi&shy;datat non proident, sunt in culpa qui officia \
dese&shy;runt mollit anim id est laborum. ";

	//  This means we must check return values of load_font() etc.
	p.set_parameter("errorpolicy", "return");

	p.set_parameter("SearchPath", searchpath);

	if (p.begin_document("starter_table.pdf", "") == -1) {
	    cerr << "Error: " << p.get_errmsg() << endl; return 2;
	}
	p.set_info("Creator", "PDFlib starter sample");
	p.set_info("Title", "starter_table");

	/* -------------------- Add table cells -------------------- */

	/* ---------- Row 1: table header (spans all columns) */
	row = 1; col = 1;
	font = p.load_font("Times-Bold", "winansi", "");
	if (font == -1) {
	    cerr << "Error: " << p.get_errmsg() << endl; return(2);
	}

	sprintf(optlist,"fittextline={position=center font=%d fontsize=14} "
			 "colspan=%d", font, colmax);

	tbl = p.add_table_cell(tbl, col, row, headertext, optlist);

	/* ---------- Row 2: various kinds of content */
	/* ----- Simple text cell */
	row++; col=1;

	sprintf(optlist, "fittextline={font=%d fontsize=10 orientate=west}", 
			 font);

	tbl = p.add_table_cell(tbl, col, row, "vertical line", optlist);
	if (tbl == -1) {
	    cerr << "Error: " << p.get_errmsg() << endl; return 2;
	}

	/* ----- Colorized background */
	col++;

	sprintf(optlist, "fittextline={font=%d fontsize=10} "
			 "matchbox={fillcolor={rgb 0.9 0.5 0}}", font);

	tbl = p.add_table_cell(tbl, col, row, "some color", optlist); 
	/* ----- Multi-line text with Textflow */
	col++;
	font = p.load_font("Times-Roman", "winansi", "");
	if (font == -1) {
	    cerr << "Error: " << p.get_errmsg() << endl; return(2);
	}

	sprintf(optlist, 
	    "charref fontname=Times-Roman encoding=winansi fontsize=8 ");

	tf = p.add_textflow(tf, tf_text, optlist);
	if (tf == -1) {
	    cerr << "Error: " << p.get_errmsg() << endl; return(2);
	}

	sprintf(optlist, 
	    "marginleft=2 marginright=2 margintop=2 marginbottom=2 " 
	    "textflow=%d",  tf);

	tbl = p.add_table_cell(tbl, col, row, "", optlist);
	if (tbl == -1) {
	    cerr << "Error: " << p.get_errmsg() << endl; return 2;
	}

	/* ----- Rotated image */
	col++;

	image = p.load_image("auto", imagefile, "");
	if (image == -1) {
	    cerr << "Couldn't load image: " << p.get_errmsg() << endl; return 2;
	}

	sprintf(optlist, "image=%d fitimage={orientate=west}", image);

	tbl = p.add_table_cell(tbl, col, row, "", optlist);
	if (tbl == -1) {
	    cerr << "Error: " << p.get_errmsg() << endl; return 2;
	}

	/* ----- Diagonal stamp */
	col++;

	sprintf(optlist, "fittextline={font=%d  fontsize=10 stamp=ll2ur}", 
			 font);

	tbl = p.add_table_cell(tbl, col, row, "entry void", optlist);
	if (tbl == -1) {
	    cerr << "Error: " << p.get_errmsg() << endl; return 2;
	}

	/* ---------- Fill row 3 and above with their numbers */
	for (row++; row <= rowmax; row++) {
	    for (col = 1; col <= colmax; col++) {
		char num[80];

		sprintf(num, "Col %d/Row %d", col, row);
		sprintf(optlist,
		    "colwidth=20%% fittextline={font=%d fontsize=10}", font);
		tbl = p.add_table_cell(tbl, col, row, num, optlist);
	    }
	}

	/* ---------- Place the table on one or more pages ---------- */

	/*
	 * 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.width height=a4.height");

	    /* Shade every other row; draw lines for all table cells.
	     * Add "showcells showborder" to visualize cell borders 
	     */
	    sprintf(optlist,
	    "header=1 fill={{area=rowodd fillcolor={gray 0.9}}} " 
	    "stroke={{line=other}} ");

	    /* Place the table instance */
	    result = p.fit_table(tbl, llx, lly, urx, ury, optlist);
	    if (result == "_error") {
		cerr << "Couldn't place table: " << p.get_errmsg()
		    << endl; return 2;
	    }

	    p.end_page_ext("");

	} while (result == "_boxfull");

	/* Check the result; "_stop" means all is ok. */
	if (result != "_stop") {
	    if (result == "_error") {
		cerr << "Error when placing table: " << p.get_errmsg()
		    << endl; return 2;
	    } else {
		/* Any other return value is a user exit caused by
		 * the "return" option; this requires dedicated code to
		 * deal with.
		 */
		cerr << "User return found in Textflow" << endl; return 2;
	    }
	}
	/* This will also delete Textflow handles used in the table */
	p.delete_table(tbl, "");

	p.end_document("");
    }

    catch (PDFlib::Exception &ex) {
	cerr << "PDFlib exception occurred:" << endl;
	cerr << "[" << ex.get_errnum() << "] " << ex.get_apiname()
	    << ": " << ex.get_errmsg() << endl;
	return 2;
    }

    return 0;
}