Starter sample for text shaping features.
Demonstrates text shaping for Arabic, Hebrew, Devanagari, and Thai scripts.
Right-to-left text is reordered according to the Bidi algorithm.
Download PHP Code Switch to Java Code Show Output PDF
/* $Id: starter_shaping.php,v 1.2 2012/05/03 14:00:42 stm Exp $
* Starter sample for text shaping features
* Demonstrate text shaping for Arabic, Hebrew, Devanagari, and Thai scripts
* Right-to-left text is reordered according to the Bidi algorithm.
*
* Required software: PDFlib/PDFlib+PDI/PPS 8
* Required data: suitable fonts for the scripts
*/
/* This is where the data files are. Adjust as necessary. */
$searchpath = dirname(dirname(dirname(__FILE__)))."/input";
$outfile = "";
$llx = 50; $lly = 50; $urx = 800; $ury = 550;
$header = array(
"Language", "Raw input", "Reordered and shaped output"
);
$MAXCOL = count($header);
class shaping {
public function shaping($fontname, $optlist, $textflow, $language, $text) {
$this->fontname = $fontname;
$this->optlist = $optlist;
$this->textflow = $textflow;
$this->language = $language;
$this->text = $text;
}
public $fontname; /* name of the font for this script */
public $optlist; /* text options */
public $textflow; /* can't use Textflow for Bidi text */
public $language; /* language name */
public $text; /* sample text */
}
$shapingsamples = array(
/* Dummys to compensate for header row and +/-1 indexing in C */
new shaping( "", "", 0, "", "" /* dummy1 */ ),
new shaping( "", "", 0, "", "" /* dummy2 */ ),
/* -------------------------- Arabic -------------------------- */
new shaping( "ScheherazadeRegOT", "shaping script=arab", 0, "Arabic",
"العَرَبِ" .
"ية" ),
new shaping( "ScheherazadeRegOT", "shaping script=arab", 0, "Arabic",
"مرحبا! (Hello)"),
new shaping( "ScheherazadeRegOT", "shaping script=arab", 0, "Arabic",
"المادة " .
"1 يولد ج" .
"ميع النا" .
"س أحرارً" .
"ا متساوي" .
"ن في الك" .
"رامة وال" .
"حقوق. "),
new shaping( "ScheherazadeRegOT", "shaping script=arab", 0, "Arabic",
"وقد وهبو" .
"ا عقلاً " .
"وضميرًا " .
"وعليهم أ" .
"ن يعامل " .
"بعضهم بع" .
"ضًا بروح" .
" الإخاء."),
/* -------------------------- Hebrew -------------------------- */
new shaping( "SILEOT", "shaping script=hebr", 0, "Hebrew",
"עִבְרִית"),
new shaping( "SILEOT", "shaping script=hebr", 0, "Hebrew",
"סעיף א. " .
"כל בני א" .
"דם נולדו" .
" בני חור" .
"ין ושווי" .
"ם בערכם " .
"ובזכויות" .
"יהם. "),
new shaping( "SILEOT", "shaping script=hebr", 0, "Hebrew",
"כולם חונ" .
"נו בתבונ" .
"ה ובמצפו" .
"ן, "),
new shaping( "SILEOT", "shaping script=hebr", 0, "Hebrew",
"לפיכך חו" .
"בה עליהם" .
" לנהוג א" .
"יש ברעהו" .
" ברוח של" .
" אחוה."),
/* -------------------------- Hindi -------------------------- */
new shaping( "raghu8", "shaping script=deva", 1, "Hindi",
"हिन्दी"),
new shaping( "raghu8", "shaping script=deva advancedlinebreak", 1, "Hindi",
"अनुच्छेद" .
" १. सभी " .
"मनुष्यों" .
" को गौरव" .
" और अधिक" .
"ारों के " .
"मामले मे" .
"ं जन्मजा" .
"त स्वतन्" .
"त्रता और" .
" समानता " .
"प्राप्त " .
"है । उन्" .
"हें बुद्" .
"घि और अन" .
"्तरात्मा" .
" की देन " .
"प्राप्त " .
"है और पर" .
"स्पर उन्" .
"हें भाईच" .
"ारे के भ" .
"ाव से बर" .
"्ताव करन" .
"ा चाहिए " .
"।"),
/* -------------------------- Sanskrit -------------------------- */
new shaping( "raghu8", "shaping script=deva", 1, "Sanskrit",
"संस्कृतम" .
"्"),
new shaping( "raghu8", "shaping script=deva", 1, "Sanskrit",
"अनुच्छेद" .
": 1 सर्व" .
"े मानवा:" .
" स्वतन्त" .
"्रा: समु" .
"त्पन्ना:" .
" वर्तन्त" .
"े अपि च," .
" गौरवदृश" .
"ा अधिकार" .
"दृशा च स" .
"माना: एव" .
" वर्तन्त" .
"े। एते स" .
"र्वे चेत" .
"ना-तर्क-" .
"शक्तिभ्य" .
"ां सुसम्" .
"पन्ना: स" .
"न्ति। अप" .
"ि च, सर्" .
"वेऽपि बन" .
"्धुत्व-भ" .
"ावनया पर" .
"स्परं व्" .
"यवहरन्तु" .
"।"),
/* -------------------------- Thai -------------------------- */
new shaping( "Norasi", "shaping script=thai advancedlinebreak locale=THA", 1, "Thai",
"ไทย"),
new shaping( "Norasi", "shaping script=thai advancedlinebreak", 1, "Thai",
"ข้อ 1 มน" .
"ุษย์ทั้ง" .
"หลายเกิด" .
"มามีอิสร" .
"ะและเสมอ" .
"ภาคกันใน" .
"เกียรติศ" .
"ักด[เกีย" .
"รติศักดิ" .
"์]และสิท" .
"ธิ ต่างม" .
"ีเหตุผลแ" .
"ละมโนธรร" .
"ม และควร" .
"ปฏิบัติต" .
"่อกันด้ว" .
"ยเจตนารม" .
"ณ์แห่งภร" .
"าดรภาพ"),
);
$MAXROW = count($shapingsamples);
try {
$p = new pdflib();
$p->set_parameter("SearchPath", $searchpath);
$p->set_parameter("textformat", "bytes");
$p->set_parameter("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_parameter("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_errmsg());
}
$p->set_info("Creator", "PDFlib starter sample");
$p->set_info("Title", "starter_shaping");
$table = 0;
/* Create table header */
for ($row=1, $col=1; $col <= $MAXCOL; $col++)
{
$optlist = sprintf(
"fittextline={fontname=Helvetica-Bold encoding=winansi fontsize=14} " .
"colwidth=%s", $col==1 ? "10%" : "45%" );
$table = $p->add_table_cell($table, $col, $row, $header[$col-1],
$optlist);
}
/* Create shaping samples */
for ($row=2; $row < $MAXROW; $row++)
{
$col=1;
/* Column 1: language name */
$optlist = sprintf(
"fittextline={fontname=Helvetica encoding=unicode fontsize=12}");
$table = $p->add_table_cell($table, $col++, $row,
$shapingsamples[$row]->language, $optlist);
/* Column 2: raw text */
$optlist = sprintf("fontname={%s} encoding=unicode fontsize=13 " .
"leading=150%% alignment=left", $shapingsamples[$row]->fontname);
$tf = $p->create_textflow($shapingsamples[$row]->text, $optlist);
$optlist = sprintf(
"margin=4 fittextflow={verticalalign=top} textflow=%d", $tf);
$table = $p->add_table_cell($table, $col++, $row, NULL, $optlist);
/* Column 3: shaped and reordered text (Textline or Textflow) */
if ($shapingsamples[$row]->textflow)
{
$optlist = sprintf("fontname={%s} encoding=unicode fontsize=13 %s ".
"leading=150%% alignment=left",
$shapingsamples[$row]->fontname, $shapingsamples[$row]->optlist);
$tf = $p->create_textflow($shapingsamples[$row]->text, $optlist);
$optlist = sprintf(
"margin=4 fittextflow={verticalalign=top} textflow=%d", $tf);
$table = $p->add_table_cell($table, $col++, $row, NULL, $optlist);
} else {
$optlist = sprintf( "fittextline={fontname={%s} encoding=unicode " .
"fontsize=13 %s}", $shapingsamples[$row]->fontname,
$shapingsamples[$row]->optlist);
$table = $p->add_table_cell($table, $col++, $row,
$shapingsamples[$row]->text, $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.height height=a4.width");
/* Shade every other row; draw lines for all table cells. */
$optlist = sprintf( "header=1 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("Error: " . $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=starter_shaping.pdf");
print $buf;
}
catch (PDFlibException $e) {
die("PDFlib exception occurred in starter_shaping sample:\n" .
"[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
$e->get_errmsg() . "\n");
}
catch (Exception $e) {
die($e);
}
$p = 0;
?>