2 /*******************************************************************************
3 * Software: UFPDF, Unicode Free PDF generator *
5 * based on FPDF 1.52 by Olivier PLATHEY *
7 * Author: Steven Wittens <steven@acko.net> *
10 * UFPDF is a modification of FPDF to support Unicode through UTF-8. *
12 *******************************************************************************/
14 if(!class_exists('UFPDF'))
16 define('UFPDF_VERSION','0.1');
18 include_once './libraries/fpdf/fpdf.php';
20 class UFPDF
extends FPDF
23 /*******************************************************************************
27 *******************************************************************************/
28 function UFPDF($orientation='P',$unit='mm',$format='A4')
30 FPDF
::FPDF($orientation, $unit, $format);
33 function GetStringWidth($s)
35 //Get width of a string in the current font
37 $codepoints=$this->utf8_to_codepoints($s);
38 $cw=&$this->CurrentFont
['cw'];
40 foreach($codepoints as $cp)
41 $w+
=isset($cw[$cp])?
$cw[$cp]:0;
42 return $w*$this->FontSize
/1000;
45 function AddFont($family,$style='',$file='')
47 //Add a TrueType or Type1 font
48 $family=strtolower($family);
51 $style=strtoupper($style);
54 if(isset($this->fonts
[$family.$style]))
55 $this->Error('Font already added: '.$family.' '.$style);
57 $file=str_replace(' ','',$family).strtolower($style).'.php';
58 if(defined('FPDF_FONTPATH'))
59 $file=FPDF_FONTPATH
.$file;
62 $this->Error('Could not include font definition file');
63 $i=count($this->fonts
)+
1;
64 $this->fonts
[$family.$style]=array('i'=>$i,'type'=>$type,'name'=>$name,'desc'=>$desc,'up'=>$up,'ut'=>$ut,'cw'=>$cw,'file'=>$file,'ctg'=>$ctg);
67 if($type=='TrueTypeUnicode')
68 $this->FontFiles
[$file]=array('length1'=>$originalsize);
70 $this->FontFiles
[$file]=array('length1'=>$size1,'length2'=>$size2);
74 function Text($x,$y,$txt)
77 $s=sprintf('BT %.2f %.2f Td %s Tj ET',$x*$this->k
,($this->h
-$y)*$this->k
,$this->_escapetext($txt));
78 if($this->underline
and $txt!='')
79 $s.=' '.$this->_dounderline($x,$y,$this->GetStringWidth($txt),$txt);
81 $s='q '.$this->TextColor
.' '.$s.' Q';
85 function AcceptPageBreak()
87 //Accept automatic page break or not
88 return $this->AutoPageBreak
;
91 function Cell($w,$h=0,$txt='',$border=0,$ln=0,$align='',$fill=0,$link='')
95 if($this->y+
$h>$this->PageBreakTrigger
and !$this->InFooter
and $this->AcceptPageBreak())
97 //Automatic page break
105 $this->AddPage($this->CurOrientation
);
110 $this->_out(sprintf('%.3f Tw',$ws*$k));
114 $w=$this->w
-$this->rMargin
-$this->x
;
116 if($fill==1 or $border==1)
119 $op=($border==1) ?
'B' : 'f';
122 $s=sprintf('%.2f %.2f %.2f %.2f re %s ',$this->x
*$k,($this->h
-$this->y
)*$k,$w*$k,-$h*$k,$op);
124 if(is_string($border))
128 if(is_int(strpos($border,'L')))
129 $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h
-$y)*$k,$x*$k,($this->h
-($y+
$h))*$k);
130 if(is_int(strpos($border,'T')))
131 $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h
-$y)*$k,($x+
$w)*$k,($this->h
-$y)*$k);
132 if(is_int(strpos($border,'R')))
133 $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',($x+
$w)*$k,($this->h
-$y)*$k,($x+
$w)*$k,($this->h
-($y+
$h))*$k);
134 if(is_int(strpos($border,'B')))
135 $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h
-($y+
$h))*$k,($x+
$w)*$k,($this->h
-($y+
$h))*$k);
139 $width = $this->GetStringWidth($txt);
141 $dx=$w-$this->cMargin
-$width;
147 $s.='q '.$this->TextColor
.' ';
148 $txtstring=$this->_escapetext($txt);
149 $s.=sprintf('BT %.2f %.2f Td %s Tj ET',($this->x+
$dx)*$k,($this->h
-($this->y+
.5*$h+
.3*$this->FontSize
))*$k,$txtstring);
151 $s.=' '.$this->_dounderline($this->x+
$dx,$this->y+
.5*$h+
.3*$this->FontSize
,$width,$txt);
155 $this->Link($this->x+
$dx,$this->y+
.5*$h-.5*$this->FontSize
,$width,$this->FontSize
,$link);
165 $this->x
=$this->lMargin
;
171 /*******************************************************************************
173 * Protected methods *
175 *******************************************************************************/
177 function _puttruetypeunicode($font) {
180 $this->_out('<</Type /Font');
181 $this->_out('/Subtype /Type0');
182 $this->_out('/BaseFont /'. $font['name'] .'-UCS');
183 $this->_out('/Encoding /Identity-H');
184 $this->_out('/DescendantFonts ['. ($this->n +
1) .' 0 R]');
186 $this->_out('endobj');
190 $this->_out('<</Type /Font');
191 $this->_out('/Subtype /CIDFontType2');
192 $this->_out('/BaseFont /'. $font['name']);
193 $this->_out('/CIDSystemInfo <</Registry (Adobe) /Ordering (UCS) /Supplement 0>>');
194 $this->_out('/FontDescriptor '. ($this->n +
1) .' 0 R');
197 foreach ($font['cw'] as $i => $w) {
198 $widths .= $i .' ['. $w.'] ';
200 $this->_out('/W ['. $widths .']');
201 $this->_out('/CIDToGIDMap '. ($this->n +
2) .' 0 R');
203 $this->_out('endobj');
207 $this->_out('<</Type /FontDescriptor');
208 $this->_out('/FontName /'.$font['name']);
210 foreach ($font['desc'] as $k => $v) {
211 $s .= ' /'. $k .' '. $v;
214 $s .= ' /FontFile2 '. $this->FontFiles
[$font['file']]['n'] .' 0 R';
218 $this->_out('endobj');
222 if(defined('FPDF_FONTPATH'))
223 $file=FPDF_FONTPATH
.$font['ctg'];
226 $size=filesize($file);
228 $this->Error('Font file not found');
229 $this->_out('<</Length '.$size);
230 if(substr($file,-2) == '.z')
231 $this->_out('/Filter /FlateDecode');
233 $f = fopen($file,'rb');
234 $this->_putstream(fread($f,$size));
236 $this->_out('endobj');
239 function _dounderline($x,$y,$width,$txt)
242 $up=$this->CurrentFont
['up'];
243 $ut=$this->CurrentFont
['ut'];
244 $w=$width+
$this->ws
*substr_count($txt,' ');
245 return sprintf('%.2f %.2f %.2f %.2f re f',$x*$this->k
,($this->h
-($y-$up/1000*$this->FontSize
))*$this->k
,$w*$this->k
,-$ut/1000*$this->FontSizePt
);
248 function _textstring($s)
250 //Convert to UTF-16BE
251 $s = $this->utf8_to_utf16be($s);
252 //Escape necessary characters
253 return '('. strtr($s, array(')' => '\\)', '(' => '\\(', '\\' => '\\\\')) .')';
256 function _strreplace($what, $to, $where) {
258 return str_replace($this->utf8_to_utf16be($what, false), $this->utf8_to_utf16be($to, false), $where);
261 function _escapetext($s)
263 //Convert to UTF-16BE
264 $s = $this->utf8_to_utf16be($s, false);
265 //Escape necessary characters
266 return '('. strtr($s, array(')' => '\\)', '(' => '\\(', '\\' => '\\\\')) .')';
271 $this->_out('/Producer '.$this->_textstring('UFPDF '. UFPDF_VERSION
));
272 if(!empty($this->title
))
273 $this->_out('/Title '.$this->_textstring($this->title
));
274 if(!empty($this->subject
))
275 $this->_out('/Subject '.$this->_textstring($this->subject
));
276 if(!empty($this->author
))
277 $this->_out('/Author '.$this->_textstring($this->author
));
278 if(!empty($this->keywords
))
279 $this->_out('/Keywords '.$this->_textstring($this->keywords
));
280 if(!empty($this->creator
))
281 $this->_out('/Creator '.$this->_textstring($this->creator
));
282 $this->_out('/CreationDate '.$this->_textstring('D:'.date('YmdHis')));
285 // UTF-8 to UTF-16BE conversion.
286 // Correctly handles all illegal UTF-8 sequences.
287 function utf8_to_utf16be(&$txt, $bom = true) {
289 $out = $bom ?
"\xFE\xFF" : '';
290 for ($i = 0; $i < $l; ++
$i) {
294 $out .= "\x00". $txt{$i};
296 // Lost continuation byte
297 else if ($c < 0xC0) {
301 // Multibyte sequence leading byte
306 else if ($c < 0xF0) {
309 else if ($c < 0xF8) {
312 // 5/6 byte sequences not possible for Unicode.
315 while (ord($txt{$i +
1}) >= 0x80 && ord($txt{$i +
1}) < 0xC0) { ++
$i; }
320 // Fetch rest of sequence
322 while ($i +
1 < $l && ord($txt{$i +
1}) >= 0x80 && ord($txt{$i +
1}) < 0xC0) { ++
$i; $q[] = ord($txt{$i}); }
325 if (count($q) != $s) {
332 $cp = (($q[0] ^
0xC0) << 6) |
($q[1] ^
0x80);
338 $out .= chr($cp >> 8);
339 $out .= chr($cp & 0xFF);
344 $cp = (($q[0] ^
0xE0) << 12) |
(($q[1] ^
0x80) << 6) |
($q[2] ^
0x80);
349 // Check for UTF-8 encoded surrogates (caused by a bad UTF-8 encoder)
350 else if ($c > 0xD800 && $c < 0xDFFF) {
354 $out .= chr($cp >> 8);
355 $out .= chr($cp & 0xFF);
360 $cp = (($q[0] ^
0xF0) << 18) |
(($q[1] ^
0x80) << 12) |
(($q[2] ^
0x80) << 6) |
($q[3] ^
0x80);
365 // Outside of the Unicode range
366 else if ($cp >= 0x10FFFF) {
372 $s1 = 0xD800 |
($cp >> 10);
373 $s2 = 0xDC00 |
($cp & 0x3FF);
375 $out .= chr($s1 >> 8);
376 $out .= chr($s1 & 0xFF);
377 $out .= chr($s2 >> 8);
378 $out .= chr($s2 & 0xFF);
387 // UTF-8 to codepoint array conversion.
388 // Correctly handles all illegal UTF-8 sequences.
389 function utf8_to_codepoints(&$txt) {
392 for ($i = 0; $i < $l; ++
$i) {
396 $out[] = ord($txt{$i});
398 // Lost continuation byte
399 else if ($c < 0xC0) {
403 // Multibyte sequence leading byte
408 else if ($c < 0xF0) {
411 else if ($c < 0xF8) {
414 // 5/6 byte sequences not possible for Unicode.
417 while (ord($txt{$i +
1}) >= 0x80 && ord($txt{$i +
1}) < 0xC0) { ++
$i; }
422 // Fetch rest of sequence
424 while ($i +
1 < $l && ord($txt{$i +
1}) >= 0x80 && ord($txt{$i +
1}) < 0xC0) { ++
$i; $q[] = ord($txt{$i}); }
427 if (count($q) != $s) {
434 $cp = (($q[0] ^
0xC0) << 6) |
($q[1] ^
0x80);
445 $cp = (($q[0] ^
0xE0) << 12) |
(($q[1] ^
0x80) << 6) |
($q[2] ^
0x80);
450 // Check for UTF-8 encoded surrogates (caused by a bad UTF-8 encoder)
451 else if ($c > 0xD800 && $c < 0xDFFF) {
460 $cp = (($q[0] ^
0xF0) << 18) |
(($q[1] ^
0x80) << 12) |
(($q[2] ^
0x80) << 6) |
($q[3] ^
0x80);
465 // Outside of the Unicode range
466 else if ($cp >= 0x10FFFF) {