4 // PDF functions taken from FPDF http://www.fpdf.org
6 require('lib/pdf.php');
8 $Big5_widths=array(' '=>250,'!'=>250,'"'=>408,'#'=>668,'$'=>490,'%'=>875,'&'=>698,'\''=>250,
9 '('=>240,')'=>240,'*'=>417,'+'=>667,','=>250,'-'=>313,'.'=>250,'/'=>520,'0'=>500,'1'=>500,
10 '2'=>500,'3'=>500,'4'=>500,'5'=>500,'6'=>500,'7'=>500,'8'=>500,'9'=>500,':'=>250,';'=>250,
11 '<'=>667,'='=>667,'>'=>667,'?'=>396,'@'=>921,'A'=>677,'B'=>615,'C'=>719,'D'=>760,'E'=>625,
12 'F'=>552,'G'=>771,'H'=>802,'I'=>354,'J'=>354,'K'=>781,'L'=>604,'M'=>927,'N'=>750,'O'=>823,
13 'P'=>563,'Q'=>823,'R'=>729,'S'=>542,'T'=>698,'U'=>771,'V'=>729,'W'=>948,'X'=>771,'Y'=>677,
14 'Z'=>635,'['=>344,'\\'=>520,']'=>344,'^'=>469,'_'=>500,'`'=>250,'a'=>469,'b'=>521,'c'=>427,
15 'd'=>521,'e'=>438,'f'=>271,'g'=>469,'h'=>531,'i'=>250,'j'=>250,'k'=>458,'l'=>240,'m'=>802,
16 'n'=>531,'o'=>500,'p'=>521,'q'=>521,'r'=>365,'s'=>333,'t'=>292,'u'=>521,'v'=>458,'w'=>677,
17 'x'=>479,'y'=>458,'z'=>427,'{'=>480,'|'=>496,'}'=>480,'~'=>667);
19 $GB_widths=array(' '=>207,'!'=>270,'"'=>342,'#'=>467,'$'=>462,'%'=>797,'&'=>710,'\''=>239,
20 '('=>374,')'=>374,'*'=>423,'+'=>605,','=>238,'-'=>375,'.'=>238,'/'=>334,'0'=>462,'1'=>462,
21 '2'=>462,'3'=>462,'4'=>462,'5'=>462,'6'=>462,'7'=>462,'8'=>462,'9'=>462,':'=>238,';'=>238,
22 '<'=>605,'='=>605,'>'=>605,'?'=>344,'@'=>748,'A'=>684,'B'=>560,'C'=>695,'D'=>739,'E'=>563,
23 'F'=>511,'G'=>729,'H'=>793,'I'=>318,'J'=>312,'K'=>666,'L'=>526,'M'=>896,'N'=>758,'O'=>772,
24 'P'=>544,'Q'=>772,'R'=>628,'S'=>465,'T'=>607,'U'=>753,'V'=>711,'W'=>972,'X'=>647,'Y'=>620,
25 'Z'=>607,'['=>374,'\\'=>333,']'=>374,'^'=>606,'_'=>500,'`'=>239,'a'=>417,'b'=>503,'c'=>427,
26 'd'=>529,'e'=>415,'f'=>264,'g'=>444,'h'=>518,'i'=>241,'j'=>230,'k'=>495,'l'=>228,'m'=>793,
27 'n'=>527,'o'=>524,'p'=>524,'q'=>504,'r'=>338,'s'=>336,'t'=>277,'u'=>517,'v'=>450,'w'=>652,
28 'x'=>466,'y'=>452,'z'=>407,'{'=>370,'|'=>258,'}'=>370,'~'=>605);
30 class PDF_Chinese extends PDF
32 function AddCIDFont($family,$style,$name,$cw,$CMap,$registry)
34 $fontkey=strtolower($family).strtoupper($style);
35 if(isset($this->fonts[$fontkey]))
36 $this->Error("Font already added: $family $style");
37 $i=count($this->fonts)+1;
38 $name=str_replace(' ','',$name);
39 $this->fonts[$fontkey]=array('i'=>$i,'type'=>'Type0','name'=>$name,'up'=>-130,'ut'=>40,'cw'=>$cw,'CMap'=>$CMap,'registry'=>$registry);
42 function AddCIDFonts($family,$name,$cw,$CMap,$registry)
44 $this->AddCIDFont($family,'',$name,$cw,$CMap,$registry);
45 $this->AddCIDFont($family,'B',$name.',Bold',$cw,$CMap,$registry);
46 $this->AddCIDFont($family,'I',$name.',Italic',$cw,$CMap,$registry);
47 $this->AddCIDFont($family,'BI',$name.',BoldItalic',$cw,$CMap,$registry);
50 function AddBig5Font($family='Big5',$name='MSungStd-Light-Acro')
52 //Add Big5 font with proportional Latin
53 $cw=$GLOBALS['Big5_widths'];
55 $registry=array('ordering'=>'CNS1','supplement'=>0);
56 $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
59 function AddBig5hwFont($family='Big5-hw',$name='MSungStd-Light-Acro')
61 //Add Big5 font with half-witdh Latin
62 for($i=32;$i<=126;$i++)
65 $registry=array('ordering'=>'CNS1','supplement'=>0);
66 $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
69 function AddGBFont($family='GB',$name='STSongStd-Light-Acro')
71 //Add GB font with proportional Latin
72 $cw=$GLOBALS['GB_widths'];
74 $registry=array('ordering'=>'GB1','supplement'=>2);
75 $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
78 function AddGBhwFont($family='GB-hw',$name='STSongStd-Light-Acro')
80 //Add GB font with half-width Latin
81 for($i=32;$i<=126;$i++)
84 $registry=array('ordering'=>'GB1','supplement'=>2);
85 $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
88 function GetStringWidth($s)
90 if ($this->CurrentFont['type']=='Type0')
91 return $this->GetMBStringWidth($s);
93 return parent::GetStringWidth($s);
96 function GetMBStringWidth($s)
98 //Multi-byte version of GetStringWidth()
100 $cw=&$this->CurrentFont['cw'];
117 return $l*$this->FontSize/1000;
120 function MultiCell($w,$h,$txt,$border=0,$align='L',$fill=0)
122 if($this->CurrentFont['type']=='Type0')
123 $this->MBMultiCell($w,$h,$txt,$border,$align,$fill);
125 parent::MultiCell($w,$h,$txt,$border,$align,$fill);
128 function MBMultiCell($w,$h,$txt,$border=0,$align='L',$fill=0)
130 //Multi-byte version of MultiCell()
131 $cw=&$this->CurrentFont['cw'];
133 $w=$this->w-$this->rMargin-$this->x;
134 $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
135 $s=str_replace("\r",'',$txt);
137 if($nb>0 and $s[$nb-1]=="\n")
151 if(is_int(strpos($border,'L')))
153 if(is_int(strpos($border,'R')))
155 $b=is_int(strpos($border,'T')) ? $b2.'T' : $b2;
167 //Check if ASCII or MB
168 $ascii=(ord($c)<128);
171 //Explicit line break
172 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
178 if($border and $nl==2)
192 $l+=$ascii ? $cw[$c] : 1000;
195 //Automatic line break
196 if($sep==-1 or $i==$j)
200 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
204 $this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);
205 $i=($s[$sep]==' ') ? $sep+1 : $sep;
211 if($border and $nl==2)
218 if($border and is_int(strpos($border,'B')))
220 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
221 $this->x=$this->lMargin;
224 function Write($h,$txt,$link='')
226 if($this->CurrentFont['type']=='Type0')
227 $this->MBWrite($h,$txt,$link);
229 parent::Write($h,$txt,$link);
232 function MBWrite($h,$txt,$link)
234 //Multi-byte version of Write()
235 $cw=&$this->CurrentFont['cw'];
236 $w=$this->w-$this->rMargin-$this->x;
237 $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
238 $s=str_replace("\r",'',$txt);
249 //Check if ASCII or MB
250 $ascii=(ord($c)<128);
253 //Explicit line break
254 $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
261 $this->x=$this->lMargin;
262 $w=$this->w-$this->rMargin-$this->x;
263 $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
268 if(!$ascii or $c==' ')
270 $l+=$ascii ? $cw[$c] : 1000;
273 //Automatic line break
274 if($sep==-1 or $i==$j)
276 if($this->x>$this->lMargin)
279 $this->x=$this->lMargin;
281 $w=$this->w-$this->rMargin-$this->x;
282 $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
289 $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
293 $this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',0,$link);
294 $i=($s[$sep]==' ') ? $sep+1 : $sep;
301 $this->x=$this->lMargin;
302 $w=$this->w-$this->rMargin-$this->x;
303 $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
312 $this->Cell($l/1000*$this->FontSize,$h,substr($s,$j,$i-$j),0,0,'',0,$link);
318 foreach($this->diffs as $diff)
322 $this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>');
323 $this->_out('endobj');
325 $mqr=get_magic_quotes_runtime();
326 set_magic_quotes_runtime(0);
327 foreach($this->FontFiles as $file=>$info)
329 //Font file embedding
331 $this->FontFiles[$file]['n']=$this->n;
332 if(defined('FPDF_FONTPATH'))
333 $file=FPDF_FONTPATH.$file;
334 $size=filesize($file);
336 $this->Error('Font file not found');
337 $this->_out('<</Length '.$size);
338 if(substr($file,-2)=='.z')
339 $this->_out('/Filter /FlateDecode');
340 $this->_out('/Length1 '.$info['length1']);
341 if(isset($info['length2']))
342 $this->_out('/Length2 '.$info['length2'].' /Length3 0');
344 $f=fopen($file,'rb');
345 $this->_putstream(fread($f,$size));
347 $this->_out('endobj');
349 set_magic_quotes_runtime($mqr);
350 foreach($this->fonts as $k=>$font)
354 $this->fonts[$k]['n']=$this->n;
355 $this->_out('<</Type /Font');
356 if($font['type']=='Type0')
357 $this->_putType0($font);
361 $this->_out('/BaseFont /'.$name);
362 if($font['type']=='core')
365 $this->_out('/Subtype /Type1');
366 if($name!='Symbol' and $name!='ZapfDingbats')
367 $this->_out('/Encoding /WinAnsiEncoding');
372 $this->_out('/Subtype /'.$font['type']);
373 $this->_out('/FirstChar 32');
374 $this->_out('/LastChar 255');
375 $this->_out('/Widths '.($this->n+1).' 0 R');
376 $this->_out('/FontDescriptor '.($this->n+2).' 0 R');
379 if(isset($font['diff']))
380 $this->_out('/Encoding '.($nf+$font['diff']).' 0 R');
382 $this->_out('/Encoding /WinAnsiEncoding');
386 $this->_out('endobj');
387 if($font['type']!='core')
393 for($i=32;$i<=255;$i++)
394 $s.=$cw[chr($i)].' ';
396 $this->_out('endobj');
399 $s='<</Type /FontDescriptor /FontName /'.$name;
400 foreach($font['desc'] as $k=>$v)
404 $s.=' /FontFile'.($font['type']=='Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R';
405 $this->_out($s.'>>');
406 $this->_out('endobj');
412 function _putType0($font)
415 $this->_out('/Subtype /Type0');
416 $this->_out('/BaseFont /'.$font['name'].'-'.$font['CMap']);
417 $this->_out('/Encoding /'.$font['CMap']);
418 $this->_out('/DescendantFonts ['.($this->n+1).' 0 R]');
420 $this->_out('endobj');
423 $this->_out('<</Type /Font');
424 $this->_out('/Subtype /CIDFontType0');
425 $this->_out('/BaseFont /'.$font['name']);
426 $this->_out('/CIDSystemInfo <</Registry '.$this->_textstring('Adobe').' /Ordering '.$this->_textstring($font['registry']['ordering']).' /Supplement '.$font['registry']['supplement'].'>>');
427 $this->_out('/FontDescriptor '.($this->n+1).' 0 R');
428 if($font['CMap']=='ETen-B5-H')
429 $W='13648 13742 500';
430 elseif($font['CMap']=='GBK-EUC-H')
431 $W='814 907 500 7716 [500]';
433 $W='1 ['.implode(' ',$font['cw']).']';
434 $this->_out('/W ['.$W.']>>');
435 $this->_out('endobj');
438 $this->_out('<</Type /FontDescriptor');
439 $this->_out('/FontName /'.$font['name']);
440 $this->_out('/Flags 6');
441 $this->_out('/FontBBox [0 -200 1000 900]');
442 $this->_out('/ItalicAngle 0');
443 $this->_out('/Ascent 800');
444 $this->_out('/Descent -200');
445 $this->_out('/CapHeight 800');
446 $this->_out('/StemV 50');
448 $this->_out('endobj');
456 // c-hanging-comment-ender-p: nil
457 // indent-tabs-mode: nil