/* * ドロップキャップ : * 段落の先頭文字を大きく表示する「ドロップキャップ」を 作成する。 * * より大きなタイプでテキストフローの最初の文字を指定し、これを除いたテキストを小さく * します。最初の文字は、テキストフローによって複数行にわたるよう配置されます。 * matchbox と matchbox end のインラインオプションは、文字のフィットボックスの矩形を表し * ます。負の値を持つ textrise オプションは、残された文字列を複数行に従えます。 * createwrapbox オプションは、マッチボックスがテキストを回り込ませるためにラップボックス * として挿入されることを示します。 * * 必要な製品 : PDFlib/PDFlib+PDI/PPS 9 * 必要なデータ : 無し */ package com.pdflib.cookbook.pdflib.textflow; import com.pdflib.pdflib; import com.pdflib.PDFlibException; public class drop_caps { public static void main (String argv[]) { pdflib p = null; int exitcode = 0; String searchpath = "../input"; String outfile = "drop_caps.pdf"; String title = "Drop Caps"; int tf = -1; String result, text, optlist = ""; final double llx = 100, lly = 50, urx = 450, ury = 800; final int t_fontsize = 16; // テキストのフォントサイズ final int t_leading = 20; // テキストの行間 final int c_num = 3; // ドロップキャップが覆う行数 int c_textrise = -((c_num - 1) * t_leading); // ドロップキャップを複数行に従えるための値 double c_fontsize = -(c_textrise * 1.8); // ドロップキャップのフォントサイズ try { p = new pdflib(); p.set_option("searchpath={" + searchpath + "}"); /* load_font() 等でエラーが起きた場合、-1を返す */ 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); /* ドロップキャップを開始、終了するための2つのマクロ("cap_start"、"cap_end")を * オプションリストに指定する。 * インラインオプション "matchbox" と "matchbox end" は文字のはめ込み枠を表す。 * 負の値を持つ"textrise" オプションは、残りのテキストを複数行に従える。 * "createwrapbox" オプションは 残りのテキストを回り込ませるための枠として挿入される。 */ optlist = "fontname=Helvetica encoding=unicode alignment=justify " + "charref " + "macro " + "{cap_start {fontsize=" + c_fontsize + " leading=" + t_leading + " textrise=" + c_textrise + " matchbox={createwrapbox boxheight={leading textrise}}} " + "cap_end {matchbox=end fontsize=" + t_fontsize + " textrise=0}}"; /* ページ上にテキストを配置する。ソフトハイフンは文字参照"­"で表す。 * (文字参照はcharrefオプションで有効となる) */ text = "<&cap_start>O<&cap_end>ur Paper Planes are the ideal way of " + "passing the time. We offer revolutionary " + "new develop­ments of the traditional com­mon " + "paper planes. If your lesson, conference, or lecture " + "turn out to be deadly boring, you can have a wonderful time " + "with our planes. All our models are fol­ded from one paper " + "sheet. They are exclu­sively folded with­out using any " + "adhesive. Several models are equipped with a folded landing " + "gear enabling a safe landing on the intended loca­tion " + "provided that you have aimed well. Other models are able to fly " + "loops or cover long distances. Let them start from a vista " + "point in the mountains and see where they touch the ground. "; /* 上記で指定したオプションリストを使用してテキストフローを生成する */ tf = p.create_textflow(text, optlist); if (tf == -1) throw new Exception("Error: " + p.get_errmsg()); do { p.begin_page_ext(0, 0, "width=a4.width height=a4.height"); /* ドロップキャップの周りを覆うように、テキストフローを配置する */ result = p.fit_textflow(tf, llx, lly, urx, ury, ""); p.end_page_ext(""); /* "_boxfull" は、テキストがまだ残っているため、続けて残りのテキストを * 処理する必要がある。 * "_nextpage" は「新規段組の開始」して解釈される。 */ } while (result.equals("_boxfull") || result.equals("_nextpage")); /* エラーをチェックする */ if (!result.equals("_stop")) { /* "_boxempty"は、はめ込み枠が小さすぎて、テキストが全く入っていない場合 */ if (result.equals( "_boxempty")) throw new Exception ("Error: Textflow box too small"); else { /* それ以外の戻り値は「return」オプションによるユーザー終了。 * これを扱うにはそのためのコードが必要。 */ throw new Exception ("User return '" + result + "' found in Textflow"); } } p.delete_textflow(tf); p.end_document(""); } catch (PDFlibException e) { System.err.println("PDFlib exception occurred:"); System.err.println("[" + e.get_errnum() + "] " + e.get_apiname() + ": " + e.get_errmsg()); exitcode = 1; } catch (Exception e) { System.err.println(e); exitcode = 1; } finally { if (p != null) { p.delete(); } System.exit(exitcode); } } }