2 require_once 'FpdfAlt.php';
4 $GLOBALS['Big5_widths']=array(' '=>250,'!'=>250,'"'=>408,'#'=>668,'$'=>490,'%'=>875,'&'=>698,'\''=>250,
5 '('=>240,')'=>240,'*'=>417,'+'=>667,','=>250,'-'=>313,'.'=>250,'/'=>520,'0'=>500,'1'=>500,
6 '2'=>500,'3'=>500,'4'=>500,'5'=>500,'6'=>500,'7'=>500,'8'=>500,'9'=>500,':'=>250,';'=>250,
7 '<'=>667,'='=>667,'>'=>667,'?'=>396,'@'=>921,'A'=>677,'B'=>615,'C'=>719,'D'=>760,'E'=>625,
8 'F'=>552,'G'=>771,'H'=>802,'I'=>354,'J'=>354,'K'=>781,'L'=>604,'M'=>927,'N'=>750,'O'=>823,
9 'P'=>563,'Q'=>823,'R'=>729,'S'=>542,'T'=>698,'U'=>771,'V'=>729,'W'=>948,'X'=>771,'Y'=>677,
10 'Z'=>635,'['=>344,'\\'=>520,']'=>344,'^'=>469,'_'=>500,'`'=>250,'a'=>469,'b'=>521,'c'=>427,
11 'd'=>521,'e'=>438,'f'=>271,'g'=>469,'h'=>531,'i'=>250,'j'=>250,'k'=>458,'l'=>240,'m'=>802,
12 'n'=>531,'o'=>500,'p'=>521,'q'=>521,'r'=>365,'s'=>333,'t'=>292,'u'=>521,'v'=>458,'w'=>677,
13 'x'=>479,'y'=>458,'z'=>427,'{'=>480,'|'=>496,'}'=>480,'~'=>667);
15 $GLOBALS['GB_widths']=array(' '=>207,'!'=>270,'"'=>342,'#'=>467,'$'=>462,'%'=>797,'&'=>710,'\''=>239,
16 '('=>374,')'=>374,'*'=>423,'+'=>605,','=>238,'-'=>375,'.'=>238,'/'=>334,'0'=>462,'1'=>462,
17 '2'=>462,'3'=>462,'4'=>462,'5'=>462,'6'=>462,'7'=>462,'8'=>462,'9'=>462,':'=>238,';'=>238,
18 '<'=>605,'='=>605,'>'=>605,'?'=>344,'@'=>748,'A'=>684,'B'=>560,'C'=>695,'D'=>739,'E'=>563,
19 'F'=>511,'G'=>729,'H'=>793,'I'=>318,'J'=>312,'K'=>666,'L'=>526,'M'=>896,'N'=>758,'O'=>772,
20 'P'=>544,'Q'=>772,'R'=>628,'S'=>465,'T'=>607,'U'=>753,'V'=>711,'W'=>972,'X'=>647,'Y'=>620,
21 'Z'=>607,'['=>374,'\\'=>333,']'=>374,'^'=>606,'_'=>500,'`'=>239,'a'=>417,'b'=>503,'c'=>427,
22 'd'=>529,'e'=>415,'f'=>264,'g'=>444,'h'=>518,'i'=>241,'j'=>230,'k'=>495,'l'=>228,'m'=>793,
23 'n'=>527,'o'=>524,'p'=>524,'q'=>504,'r'=>338,'s'=>336,'t'=>277,'u'=>517,'v'=>450,'w'=>652,
24 'x'=>466,'y'=>452,'z'=>407,'{'=>370,'|'=>258,'}'=>370,'~'=>605);
26 class FpdfAlt_Chinese extends FpdfAlt
28 function AddCIDFont($family,$style,$name,$cw,$CMap,$registry)
30 $fontkey=strtolower($family).strtoupper($style);
31 if(isset($this->fonts[$fontkey]))
32 $this->Error("Font already added: $family $style");
33 $i=count($this->fonts)+1;
34 $name=str_replace(' ','',$name);
35 $this->fonts[$fontkey]=array('i'=>$i,'type'=>'Type0','name'=>$name,'up'=>-130,'ut'=>40,'cw'=>$cw,'CMap'=>$CMap,'registry'=>$registry);
38 function AddCIDFonts($family,$name,$cw,$CMap,$registry)
40 $this->AddCIDFont($family,'',$name,$cw,$CMap,$registry);
41 $this->AddCIDFont($family,'B',$name.',Bold',$cw,$CMap,$registry);
42 $this->AddCIDFont($family,'I',$name.',Italic',$cw,$CMap,$registry);
43 $this->AddCIDFont($family,'BI',$name.',BoldItalic',$cw,$CMap,$registry);
46 function AddBig5Font($family='Big5',$name='MSungStd-Light-Acro')
48 //Add Big5 font with proportional Latin
49 $cw=$GLOBALS['Big5_widths'];
51 $registry=array('ordering'=>'CNS1','supplement'=>0);
52 $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
55 function AddBig5hwFont($family='Big5-hw',$name='MMingStd-Light-Acro')
57 //Add Big5 font with half-witdh Latin
58 for($i=32;$i<=126;$i++)
61 $registry=array('ordering'=>'CNS1','supplement'=>0);
62 $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
64 function AddUniCNShwFont($family='Uni-hw',$name='PMingLiU')
66 //Add Unicode font with half-witdh Latin
67 for($i=32;$i<=126;$i++)
69 $CMap='UniCNS-UTF16-H';
70 $registry=array('ordering'=>'CNS1','supplement'=>4);
71 $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
75 function AddGBFont($family='GB',$name='STSongStd-Light-Acro')
77 //Add GB font with proportional Latin
78 $cw=$GLOBALS['GB_widths'];
80 $registry=array('ordering'=>'GB1','supplement'=>2);
81 $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
84 function AddGBhwFont($family='GB-hw',$name='STSongStd-Light-Acro')
86 //Add GB font with half-width Latin
87 for($i=32;$i<=126;$i++)
90 $registry=array('ordering'=>'GB1','supplement'=>2);
91 $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
94 function GetStringWidth($s)
96 if($this->CurrentFont['type']=='Type0')
97 return $this->GetMBStringWidth($s);
99 return parent::GetStringWidth($s);
102 function GetMBStringWidth($s)
104 //Multi-byte version of GetStringWidth()
106 $cw=&$this->CurrentFont['cw'];
123 return $l*$this->FontSize/1000;
126 function MultiCell($w,$h,$txt,$border=0,$align='L',$fill=0)
128 if($this->CurrentFont['type']=='Type0')
129 $this->MBMultiCell($w,$h,$txt,$border,$align,$fill);
131 parent::MultiCell($w,$h,$txt,$border,$align,$fill);
134 function MBMultiCell($w,$h,$txt,$border=0,$align='L',$fill=0)
136 //Multi-byte version of MultiCell()
137 $cw=&$this->CurrentFont['cw'];
139 $w=$this->w-$this->rMargin-$this->x;
140 $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
141 $s=str_replace("\r",'',$txt);
143 if($nb>0 and $s[$nb-1]=="\n")
157 if(is_int(strpos($border,'L')))
159 if(is_int(strpos($border,'R')))
161 $b=is_int(strpos($border,'T')) ? $b2.'T' : $b2;
173 //Check if ASCII or MB
174 $ascii=(ord($c)<128);
177 //Explicit line break
178 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
184 if($border and $nl==2)
198 $l+=$ascii ? $cw[$c] : 1000;
201 //Automatic line break
202 if($sep==-1 or $i==$j)
206 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
210 $this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);
211 $i=($s[$sep]==' ') ? $sep+1 : $sep;
217 if($border and $nl==2)
224 if($border and is_int(strpos($border,'B')))
226 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
227 $this->x=$this->lMargin;
230 function Write($h,$txt,$link='')
232 if($this->CurrentFont['type']=='Type0')
233 $this->MBWrite($h,$txt,$link);
235 parent::Write($h,$txt,$link);
238 function MBWrite($h,$txt,$link)
240 //Multi-byte version of Write()
241 $cw=&$this->CurrentFont['cw'];
242 $w=$this->w-$this->rMargin-$this->x;
243 $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
244 $s=str_replace("\r",'',$txt);
255 //Check if ASCII or MB
256 $ascii=(ord($c)<128);
259 //Explicit line break
260 $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
267 $this->x=$this->lMargin;
268 $w=$this->w-$this->rMargin-$this->x;
269 $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
274 if(!$ascii or $c==' ')
276 $l+=$ascii ? $cw[$c] : 1000;
279 //Automatic line break
280 if($sep==-1 or $i==$j)
282 if($this->x>$this->lMargin)
285 $this->x=$this->lMargin;
287 $w=$this->w-$this->rMargin-$this->x;
288 $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
295 $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
299 $this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',0,$link);
300 $i=($s[$sep]==' ') ? $sep+1 : $sep;
307 $this->x=$this->lMargin;
308 $w=$this->w-$this->rMargin-$this->x;
309 $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
318 $this->Cell($l/1000*$this->FontSize,$h,substr($s,$j,$i-$j),0,0,'',0,$link);
324 foreach($this->diffs as $diff)
328 $this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>');
329 $this->_out('endobj');
331 foreach($this->FontFiles as $file=>$info)
333 //Font file embedding
335 $this->FontFiles[$file]['n']=$this->n;
336 if(defined('FPDF_FONTPATH'))
337 $file=FPDF_FONTPATH.$file;
338 $size=filesize($file);
340 $this->Error('Font file not found');
341 $this->_out('<</Length '.$size);
342 if(substr($file,-2)=='.z')
343 $this->_out('/Filter /FlateDecode');
344 $this->_out('/Length1 '.$info['length1']);
345 if(isset($info['length2']))
346 $this->_out('/Length2 '.$info['length2'].' /Length3 0');
348 $f=fopen($file,'rb');
349 $this->_putstream(fread($f,$size));
351 $this->_out('endobj');
353 foreach($this->fonts as $k=>$font)
357 $this->fonts[$k]['n']=$this->n;
358 $this->_out('<</Type /Font');
359 if($font['type']=='Type0')
360 $this->_putType0($font);
364 $this->_out('/BaseFont /'.$name);
365 if($font['type']=='core')
368 $this->_out('/Subtype /Type1');
369 if($name!='Symbol' and $name!='ZapfDingbats')
370 $this->_out('/Encoding /WinAnsiEncoding');
375 $this->_out('/Subtype /'.$font['type']);
376 $this->_out('/FirstChar 32');
377 $this->_out('/LastChar 255');
378 $this->_out('/Widths '.($this->n+1).' 0 R');
379 $this->_out('/FontDescriptor '.($this->n+2).' 0 R');
382 if(isset($font['diff']))
383 $this->_out('/Encoding '.($nf+$font['diff']).' 0 R');
385 $this->_out('/Encoding /WinAnsiEncoding');
389 $this->_out('endobj');
390 if($font['type']!='core')
396 for($i=32;$i<=255;$i++)
397 $s.=$cw[chr($i)].' ';
399 $this->_out('endobj');
402 $s='<</Type /FontDescriptor /FontName /'.$name;
403 foreach($font['desc'] as $k=>$v)
407 $s.=' /FontFile'.($font['type']=='Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R';
408 $this->_out($s.'>>');
409 $this->_out('endobj');
415 function _putType0($font)
418 $this->_out('/Subtype /Type0');
419 $this->_out('/BaseFont /'.$font['name'].'-'.$font['CMap']);
420 $this->_out('/Encoding /'.$font['CMap']);
421 $this->_out('/DescendantFonts ['.($this->n+1).' 0 R]');
423 $this->_out('endobj');
426 $this->_out('<</Type /Font');
427 $this->_out('/Subtype /CIDFontType0');
428 $this->_out('/BaseFont /'.$font['name']);
429 $this->_out('/CIDSystemInfo <</Registry (Adobe) /Ordering ('.$font['registry']['ordering'].') /Supplement '.$font['registry']['supplement'].'>>');
430 $this->_out('/FontDescriptor '.($this->n+1).' 0 R');
431 if($font['CMap']=='ETen-B5-H')
432 $W='13648 13742 500';
433 elseif($font['CMap']=='GBK-EUC-H')
434 $W='814 907 500 7716 [500]';
436 //print_r( $font['cw']);exit;
437 $W='1 ['.implode(' ',$font['cw']).']';
438 $this->_out('/W ['.$W.']>>');
439 $this->_out('endobj');
442 $this->_out('<</Type /FontDescriptor');
443 $this->_out('/FontName /'.$font['name']);
444 $this->_out('/Flags 6');
445 $this->_out('/FontBBox [0 -200 1000 900]');
446 $this->_out('/ItalicAngle 0');
447 $this->_out('/Ascent 800');
448 $this->_out('/Descent -200');
449 $this->_out('/CapHeight 800');
450 $this->_out('/StemV 50');
452 $this->_out('endobj');