Fix #8052 - fixing pdf
[pear] / Fpdf / Chinese.php
1 <?php
2 require_once 'Fpdf/tFPDF.php';
3
4 $GLOBALS['UTF8']=array(' '=>250,'!'=>300,'"'=>410,'#'=>668,'$'=>490,'%'=>875,'&'=>700,'\''=>250,
5         '('=>240,')'=>240,'*'=>417,'+'=>667,','=>250,'-'=>313,'.'=>250,'/'=>520,'0'=>550,'1'=>500,
6         '2'=>500,'3'=>500,'4'=>500,'5'=>500,'6'=>500,'7'=>530,'8'=>500,'9'=>510,':'=>250,';'=>250,
7         '<'=>667,'='=>667,'>'=>667,'?'=>400,'@'=>921,'A'=>600,'B'=>600,'C'=>600,'D'=>650,'E'=>460,
8         'F'=>500,'G'=>720,'H'=>750,'I'=>500,'J'=>450,'K'=>650,'L'=>500,'M'=>850,'N'=>680,'O'=>750,
9         'P'=>563,'Q'=>750,'R'=>600,'S'=>542,'T'=>550,'U'=>650,'V'=>690,'W'=>948,'X'=>650,'Y'=>550,
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'=>400,'t'=>292,'u'=>521,'v'=>480,'w'=>700,
13         'x'=>479,'y'=>458,'z'=>427,'{'=>480,'|'=>496,'}'=>480,'~'=>667);
14
15 $GLOBALS['Big5_widths']=array(' '=>250,'!'=>250,'"'=>408,'#'=>668,'$'=>490,'%'=>875,'&'=>698,'\''=>250,
16         '('=>240,')'=>240,'*'=>417,'+'=>667,','=>250,'-'=>313,'.'=>250,'/'=>520,'0'=>500,'1'=>500,
17         '2'=>500,'3'=>500,'4'=>500,'5'=>500,'6'=>500,'7'=>500,'8'=>500,'9'=>500,':'=>250,';'=>250,
18         '<'=>667,'='=>667,'>'=>667,'?'=>396,'@'=>921,'A'=>677,'B'=>615,'C'=>719,'D'=>760,'E'=>625,
19         'F'=>552,'G'=>771,'H'=>802,'I'=>354,'J'=>354,'K'=>781,'L'=>604,'M'=>927,'N'=>750,'O'=>823,
20         'P'=>563,'Q'=>823,'R'=>729,'S'=>542,'T'=>698,'U'=>771,'V'=>729,'W'=>948,'X'=>771,'Y'=>677,
21         'Z'=>635,'['=>344,'\\'=>520,']'=>344,'^'=>469,'_'=>500,'`'=>250,'a'=>469,'b'=>521,'c'=>427,
22         'd'=>521,'e'=>438,'f'=>271,'g'=>469,'h'=>531,'i'=>250,'j'=>250,'k'=>458,'l'=>240,'m'=>802,
23         'n'=>531,'o'=>500,'p'=>521,'q'=>521,'r'=>365,'s'=>333,'t'=>292,'u'=>521,'v'=>458,'w'=>677,
24         'x'=>479,'y'=>458,'z'=>427,'{'=>480,'|'=>496,'}'=>480,'~'=>667);
25
26 $GLOBALS['GB_widths']=array(' '=>207,'!'=>270,'"'=>342,'#'=>467,'$'=>462,'%'=>797,'&'=>710,'\''=>239,
27         '('=>374,')'=>374,'*'=>423,'+'=>605,','=>238,'-'=>375,'.'=>238,'/'=>334,'0'=>462,'1'=>462,
28         '2'=>462,'3'=>462,'4'=>462,'5'=>462,'6'=>462,'7'=>462,'8'=>462,'9'=>462,':'=>238,';'=>238,
29         '<'=>605,'='=>605,'>'=>605,'?'=>344,'@'=>748,'A'=>684,'B'=>560,'C'=>695,'D'=>739,'E'=>563,
30         'F'=>511,'G'=>729,'H'=>793,'I'=>318,'J'=>312,'K'=>666,'L'=>526,'M'=>896,'N'=>758,'O'=>772,
31         'P'=>544,'Q'=>772,'R'=>628,'S'=>465,'T'=>607,'U'=>753,'V'=>711,'W'=>972,'X'=>647,'Y'=>620,
32         'Z'=>607,'['=>374,'\\'=>333,']'=>374,'^'=>606,'_'=>500,'`'=>239,'a'=>417,'b'=>503,'c'=>427,
33         'd'=>529,'e'=>415,'f'=>264,'g'=>444,'h'=>518,'i'=>241,'j'=>230,'k'=>495,'l'=>228,'m'=>793,
34         'n'=>527,'o'=>524,'p'=>524,'q'=>504,'r'=>338,'s'=>336,'t'=>277,'u'=>517,'v'=>450,'w'=>652,
35         'x'=>466,'y'=>452,'z'=>407,'{'=>370,'|'=>258,'}'=>370,'~'=>605);
36
37 class FPDF_Chinese extends tFPDF
38 {
39 function AddCIDFont($family,$style,$name,$cw,$CMap,$registry)
40 {
41         $fontkey=strtolower($family).strtoupper($style);
42         if(isset($this->fonts[$fontkey]))
43                 $this->Error("Font already added: $family $style");
44         $i=count($this->fonts)+1;
45         $name=str_replace(' ','',$name);
46         $this->fonts[$fontkey]=array('i'=>$i,'type'=>'Type0','name'=>$name,'up'=>-130,'ut'=>40,'cw'=>$cw,'CMap'=>$CMap,'registry'=>$registry);
47 }
48
49 function AddCIDFonts($family,$name,$cw,$CMap,$registry)
50 {
51         $this->AddCIDFont($family,'',$name,$cw,$CMap,$registry);
52         $this->AddCIDFont($family,'B',$name.',Bold',$cw,$CMap,$registry);
53         $this->AddCIDFont($family,'I',$name.',Italic',$cw,$CMap,$registry);
54         $this->AddCIDFont($family,'BI',$name.',BoldItalic',$cw,$CMap,$registry);
55 }
56
57 function AddBig5Font($family='Big5',$name='MSungStd-Light-Acro')
58 {
59         //Add Big5 font with proportional Latin
60         $cw=$GLOBALS['Big5_widths'];
61         $CMap='ETenms-B5-H';
62         $registry=array('ordering'=>'CNS1','supplement'=>0);
63         $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
64 }
65
66 function AddBig5hwFont($family='Big5-hw',$name='MSungStd-Light-Acro')
67 {
68         //Add Big5 font with half-witdh Latin
69         for($i=32;$i<=126;$i++)
70                 $cw[chr($i)]=500;
71         $CMap='ETen-B5-H';
72         $registry=array('ordering'=>'CNS1','supplement'=>0);
73         $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
74 }
75
76 function AddGBFont($family='GB',$name='STSongStd-Light-Acro')
77 {
78         //Add GB font with proportional Latin
79         $cw=$GLOBALS['GB_widths'];
80         $CMap='GBKp-EUC-H';
81         $registry=array('ordering'=>'GB1','supplement'=>2);
82         $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
83 }
84
85 function AddGBhwFont($family='GB-hw',$name='STSongStd-Light-Acro')
86 {
87         //Add GB font with half-width Latin
88         for($i=32;$i<=126;$i++)
89                 $cw[chr($i)]=500;
90         $CMap='GBK-EUC-H';
91         $registry=array('ordering'=>'GB1','supplement'=>2);
92         $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
93 }
94
95 function GetStringWidth($s)
96 {
97         if($this->CurrentFont['type']=='Type0')
98                 return $this->GetMBStringWidth($s);
99         else
100                 return parent::GetStringWidth($s);
101 }
102
103 function GetMBStringWidth($s)
104 {
105         //Multi-byte version of GetStringWidth()
106         $l=0;
107         $cw=&$this->CurrentFont['cw'];
108         $nb=strlen($s);
109         $i=0;
110         while($i<$nb)
111         {
112                 $c=$s[$i];
113                 if(ord($c)<128)
114                 {
115                         if (!isset($cw[$c])) {
116                                 $cw[$c] = 240;//print_R($cw);exit;
117                         }
118                         
119                         $l+=$cw[$c];
120                         $i++;
121                 }
122                 else
123                 {
124                         $l+=1000;
125                         $i+=2;
126                 }
127         }
128         return $l*$this->FontSize/1000;
129 }
130
131 function MultiCell($w,$h,$txt,$border=0,$align='L',$fill=0)
132 {
133         if($this->CurrentFont['type']=='Type0')
134                 $this->MBMultiCell($w,$h,$txt,$border,$align,$fill);
135         else
136                 parent::MultiCell($w,$h,$txt,$border,$align,$fill);
137 }
138
139 function MBMultiCell($w,$h,$txt,$border=0,$align='L',$fill=0)
140 {
141         //Multi-byte version of MultiCell()
142         $cw=&$this->CurrentFont['cw'];
143         if($w==0)
144                 $w=$this->w-$this->rMargin-$this->x;
145         $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
146         $s=str_replace("\r",'',$txt);
147         $nb=strlen($s);
148         if($nb>0 and $s[$nb-1]=="\n")
149                 $nb--;
150         $b=0;
151         if($border)
152         {
153                 if($border==1)
154                 {
155                         $border='LTRB';
156                         $b='LRT';
157                         $b2='LR';
158                 }
159                 else
160                 {
161                         $b2='';
162                         if(is_int(strpos($border,'L')))
163                                 $b2.='L';
164                         if(is_int(strpos($border,'R')))
165                                 $b2.='R';
166                         $b=is_int(strpos($border,'T')) ? $b2.'T' : $b2;
167                 }
168         }
169         $sep=-1;
170         $i=0;
171         $j=0;
172         $l=0;
173         $nl=1;
174         while($i<$nb)
175         {
176                 //Get next character
177                 $c=$s[$i];
178                 //Check if ASCII or MB
179                 $ascii=(ord($c)<128);
180                 if($c=="\n")
181                 {
182                         //Explicit line break
183                         $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
184                         $i++;
185                         $sep=-1;
186                         $j=$i;
187                         $l=0;
188                         $nl++;
189                         if($border and $nl==2)
190                                 $b=$b2;
191                         continue;
192                 }
193                 if(!$ascii)
194                 {
195                         $sep=$i;
196                         $ls=$l;
197                 }
198                 elseif($c==' ')
199                 {
200                         $sep=$i;
201                         $ls=$l;
202                 }
203                 $l+=$ascii ? $cw[$c] : 1000;
204                 if($l>$wmax)
205                 {
206                         //Automatic line break
207                         if($sep==-1 or $i==$j)
208                         {
209                                 if($i==$j)
210                                         $i+=$ascii ? 1 : 2;
211                                 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
212                         }
213                         else
214                         {
215                                 $this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);
216                                 $i=($s[$sep]==' ') ? $sep+1 : $sep;
217                         }
218                         $sep=-1;
219                         $j=$i;
220                         $l=0;
221                         $nl++;
222                         if($border and $nl==2)
223                                 $b=$b2;
224                 }
225                 else
226                         $i+=$ascii ? 1 : 2;
227         }
228         //Last chunk
229         if($border and is_int(strpos($border,'B')))
230                 $b.='B';
231         $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
232         $this->x=$this->lMargin;
233 }
234
235 function Write($h,$txt,$link='')
236 {
237         if($this->CurrentFont['type']=='Type0')
238                 $this->MBWrite($h,$txt,$link);
239         else
240                 parent::Write($h,$txt,$link);
241 }
242
243 function MBWrite($h,$txt,$link)
244 {
245         //Multi-byte version of Write()
246         $cw=&$this->CurrentFont['cw'];
247         $w=$this->w-$this->rMargin-$this->x;
248         $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
249         $s=str_replace("\r",'',$txt);
250         $nb=strlen($s);
251         $sep=-1;
252         $i=0;
253         $j=0;
254         $l=0;
255         $nl=1;
256         while($i<$nb)
257         {
258                 //Get next character
259                 $c=$s[$i];
260                 //Check if ASCII or MB
261                 $ascii=(ord($c)<128);
262                 if($c=="\n")
263                 {
264                         //Explicit line break
265                         $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
266                         $i++;
267                         $sep=-1;
268                         $j=$i;
269                         $l=0;
270                         if($nl==1)
271                         {
272                                 $this->x=$this->lMargin;
273                                 $w=$this->w-$this->rMargin-$this->x;
274                                 $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
275                         }
276                         $nl++;
277                         continue;
278                 }
279                 if(!$ascii or $c==' ')
280                         $sep=$i;
281                 $l+=$ascii ? $cw[$c] : 1000;
282                 if($l>$wmax)
283                 {
284                         //Automatic line break
285                         if($sep==-1 or $i==$j)
286                         {
287                                 if($this->x>$this->lMargin)
288                                 {
289                                         //Move to next line
290                                         $this->x=$this->lMargin;
291                                         $this->y+=$h;
292                                         $w=$this->w-$this->rMargin-$this->x;
293                                         $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
294                                         $i++;
295                                         $nl++;
296                                         continue;
297                                 }
298                                 if($i==$j)
299                                         $i+=$ascii ? 1 : 2;
300                                 $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
301                         }
302                         else
303                         {
304                                 $this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',0,$link);
305                                 $i=($s[$sep]==' ') ? $sep+1 : $sep;
306                         }
307                         $sep=-1;
308                         $j=$i;
309                         $l=0;
310                         if($nl==1)
311                         {
312                                 $this->x=$this->lMargin;
313                                 $w=$this->w-$this->rMargin-$this->x;
314                                 $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
315                         }
316                         $nl++;
317                 }
318                 else
319                         $i+=$ascii ? 1 : 2;
320         }
321         //Last chunk
322         if($i!=$j)
323                 $this->Cell($l/1000*$this->FontSize,$h,substr($s,$j,$i-$j),0,0,'',0,$link);
324 }
325
326 function _putfonts()
327 {
328         $nf=$this->n;
329         foreach($this->diffs as $diff)
330         {
331                 //Encodings
332                 $this->_newobj();
333                 $this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>');
334                 $this->_out('endobj');
335         }
336          
337         foreach($this->FontFiles as $file=>$info)
338         {
339                 //Font file embedding
340                 $this->_newobj();
341                 $this->FontFiles[$file]['n']=$this->n;
342                 if(defined('FPDF_FONTPATH'))
343                         $file=FPDF_FONTPATH.$file;
344                 $size=filesize($file);
345                 if(!$size)
346                         $this->Error('Font file not found');
347                 $this->_out('<</Length '.$size);
348                 if(substr($file,-2)=='.z')
349                         $this->_out('/Filter /FlateDecode');
350                 $this->_out('/Length1 '.$info['length1']);
351                 if(isset($info['length2']))
352                         $this->_out('/Length2 '.$info['length2'].' /Length3 0');
353                 $this->_out('>>');
354                 $f=fopen($file,'rb');
355                 $this->_putstream(fread($f,$size));
356                 fclose($f);
357                 $this->_out('endobj');
358         }
359         foreach($this->fonts as $k=>$font)
360         {
361                 //Font objects
362                 $this->_newobj();
363                 $this->fonts[$k]['n']=$this->n;
364                 $this->_out('<</Type /Font');
365                 if($font['type']=='Type0')
366                         $this->_putType0($font);
367                 else
368                 {
369                         $name=$font['name'];
370                         $this->_out('/BaseFont /'.$name);
371                         if($font['type']=='core')
372                         {
373                                 //Standard font
374                                 $this->_out('/Subtype /Type1');
375                                 if($name!='Symbol' and $name!='ZapfDingbats')
376                                         $this->_out('/Encoding /WinAnsiEncoding');
377                         }
378                         else
379                         {
380                                 //Additional font
381                                 $this->_out('/Subtype /'.$font['type']);
382                                 $this->_out('/FirstChar 32');
383                                 $this->_out('/LastChar 255');
384                                 $this->_out('/Widths '.($this->n+1).' 0 R');
385                                 $this->_out('/FontDescriptor '.($this->n+2).' 0 R');
386                                 if($font['enc'])
387                                 {
388                                         if(isset($font['diff']))
389                                                 $this->_out('/Encoding '.($nf+$font['diff']).' 0 R');
390                                         else
391                                                 $this->_out('/Encoding /WinAnsiEncoding');
392                                 }
393                         }
394                         $this->_out('>>');
395                         $this->_out('endobj');
396                         if($font['type']!='core')
397                         {
398                                 //Widths
399                                 $this->_newobj();
400                                 $cw=&$font['cw'];
401                                 $s='[';
402                                 for($i=32;$i<=255;$i++)
403                                         $s.=$cw[chr($i)].' ';
404                                 $this->_out($s.']');
405                                 $this->_out('endobj');
406                                 //Descriptor
407                                 $this->_newobj();
408                                 $s='<</Type /FontDescriptor /FontName /'.$name;
409                                 foreach($font['desc'] as $k=>$v)
410                                         $s.=' /'.$k.' '.$v;
411                                 $file=$font['file'];
412                                 if($file)
413                                         $s.=' /FontFile'.($font['type']=='Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R';
414                                 $this->_out($s.'>>');
415                                 $this->_out('endobj');
416                         }
417                 }
418         }
419 }
420
421 function _putType0($font)
422 {
423         //Type0
424         $this->_out('/Subtype /Type0');
425         $this->_out('/BaseFont /'.$font['name'].'-'.$font['CMap']);
426         $this->_out('/Encoding /'.$font['CMap']);
427         $this->_out('/DescendantFonts ['.($this->n+1).' 0 R]');
428         $this->_out('>>');
429         $this->_out('endobj');
430         //CIDFont
431         $this->_newobj();
432         $this->_out('<</Type /Font');
433         $this->_out('/Subtype /CIDFontType0');
434         $this->_out('/BaseFont /'.$font['name']);
435         $this->_out('/CIDSystemInfo <</Registry '.$this->_textstring('Adobe').' /Ordering '.$this->_textstring($font['registry']['ordering']).' /Supplement '.$font['registry']['supplement'].'>>');
436         $this->_out('/FontDescriptor '.($this->n+1).' 0 R');
437         if($font['CMap']=='ETen-B5-H')
438                 $W='13648 13742 500';
439         elseif($font['CMap']=='GBK-EUC-H')
440                 $W='814 907 500 7716 [500]';
441         else
442                 $W='1 ['.implode(' ',$font['cw']).']';
443         $this->_out('/W ['.$W.']>>');
444         $this->_out('endobj');
445         //Font descriptor
446         $this->_newobj();
447         $this->_out('<</Type /FontDescriptor');
448         $this->_out('/FontName /'.$font['name']);
449         $this->_out('/Flags 6');
450         $this->_out('/FontBBox [0 -200 1000 900]');
451         $this->_out('/ItalicAngle 0');
452         $this->_out('/Ascent 800');
453         $this->_out('/Descent -200');
454         $this->_out('/CapHeight 800');
455         $this->_out('/StemV 50');
456         $this->_out('>>');
457         $this->_out('endobj');
458 }
459
460 }
461 ?>