a59302bc32433dd163c4a61e8eb0989674e800af
[pear] / Fpdf / Chinese.php
1 <?php
2 require_once ('FPDF.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 PDF_Chinese extends FPDF
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                         $l+=$cw[$c];
116                         $i++;
117                 }
118                 else
119                 {
120                         $l+=1000;
121                         $i+=2;
122                 }
123         }
124         return $l*$this->FontSize/1000;
125 }
126
127 function MultiCell($w,$h,$txt,$border=0,$align='L',$fill=0)
128 {
129         if($this->CurrentFont['type']=='Type0')
130                 $this->MBMultiCell($w,$h,$txt,$border,$align,$fill);
131         else
132                 parent::MultiCell($w,$h,$txt,$border,$align,$fill);
133 }
134
135 function MBMultiCell($w,$h,$txt,$border=0,$align='L',$fill=0)
136 {
137         //Multi-byte version of MultiCell()
138         $cw=&$this->CurrentFont['cw'];
139         if($w==0)
140                 $w=$this->w-$this->rMargin-$this->x;
141         $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
142         $s=str_replace("\r",'',$txt);
143         $nb=strlen($s);
144         if($nb>0 and $s[$nb-1]=="\n")
145                 $nb--;
146         $b=0;
147         if($border)
148         {
149                 if($border==1)
150                 {
151                         $border='LTRB';
152                         $b='LRT';
153                         $b2='LR';
154                 }
155                 else
156                 {
157                         $b2='';
158                         if(is_int(strpos($border,'L')))
159                                 $b2.='L';
160                         if(is_int(strpos($border,'R')))
161                                 $b2.='R';
162                         $b=is_int(strpos($border,'T')) ? $b2.'T' : $b2;
163                 }
164         }
165         $sep=-1;
166         $i=0;
167         $j=0;
168         $l=0;
169         $nl=1;
170         while($i<$nb)
171         {
172                 //Get next character
173                 $c=$s[$i];
174                 //Check if ASCII or MB
175                 $ascii=(ord($c)<128);
176                 if($c=="\n")
177                 {
178                         //Explicit line break
179                         $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
180                         $i++;
181                         $sep=-1;
182                         $j=$i;
183                         $l=0;
184                         $nl++;
185                         if($border and $nl==2)
186                                 $b=$b2;
187                         continue;
188                 }
189                 if(!$ascii)
190                 {
191                         $sep=$i;
192                         $ls=$l;
193                 }
194                 elseif($c==' ')
195                 {
196                         $sep=$i;
197                         $ls=$l;
198                 }
199                 $l+=$ascii ? $cw[$c] : 1000;
200                 if($l>$wmax)
201                 {
202                         //Automatic line break
203                         if($sep==-1 or $i==$j)
204                         {
205                                 if($i==$j)
206                                         $i+=$ascii ? 1 : 2;
207                                 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
208                         }
209                         else
210                         {
211                                 $this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);
212                                 $i=($s[$sep]==' ') ? $sep+1 : $sep;
213                         }
214                         $sep=-1;
215                         $j=$i;
216                         $l=0;
217                         $nl++;
218                         if($border and $nl==2)
219                                 $b=$b2;
220                 }
221                 else
222                         $i+=$ascii ? 1 : 2;
223         }
224         //Last chunk
225         if($border and is_int(strpos($border,'B')))
226                 $b.='B';
227         $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
228         $this->x=$this->lMargin;
229 }
230
231 function Write($h,$txt,$link='')
232 {
233         if($this->CurrentFont['type']=='Type0')
234                 $this->MBWrite($h,$txt,$link);
235         else
236                 parent::Write($h,$txt,$link);
237 }
238
239 function MBWrite($h,$txt,$link)
240 {
241         //Multi-byte version of Write()
242         $cw=&$this->CurrentFont['cw'];
243         $w=$this->w-$this->rMargin-$this->x;
244         $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
245         $s=str_replace("\r",'',$txt);
246         $nb=strlen($s);
247         $sep=-1;
248         $i=0;
249         $j=0;
250         $l=0;
251         $nl=1;
252         while($i<$nb)
253         {
254                 //Get next character
255                 $c=$s[$i];
256                 //Check if ASCII or MB
257                 $ascii=(ord($c)<128);
258                 if($c=="\n")
259                 {
260                         //Explicit line break
261                         $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
262                         $i++;
263                         $sep=-1;
264                         $j=$i;
265                         $l=0;
266                         if($nl==1)
267                         {
268                                 $this->x=$this->lMargin;
269                                 $w=$this->w-$this->rMargin-$this->x;
270                                 $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
271                         }
272                         $nl++;
273                         continue;
274                 }
275                 if(!$ascii or $c==' ')
276                         $sep=$i;
277                 $l+=$ascii ? $cw[$c] : 1000;
278                 if($l>$wmax)
279                 {
280                         //Automatic line break
281                         if($sep==-1 or $i==$j)
282                         {
283                                 if($this->x>$this->lMargin)
284                                 {
285                                         //Move to next line
286                                         $this->x=$this->lMargin;
287                                         $this->y+=$h;
288                                         $w=$this->w-$this->rMargin-$this->x;
289                                         $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
290                                         $i++;
291                                         $nl++;
292                                         continue;
293                                 }
294                                 if($i==$j)
295                                         $i+=$ascii ? 1 : 2;
296                                 $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
297                         }
298                         else
299                         {
300                                 $this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',0,$link);
301                                 $i=($s[$sep]==' ') ? $sep+1 : $sep;
302                         }
303                         $sep=-1;
304                         $j=$i;
305                         $l=0;
306                         if($nl==1)
307                         {
308                                 $this->x=$this->lMargin;
309                                 $w=$this->w-$this->rMargin-$this->x;
310                                 $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
311                         }
312                         $nl++;
313                 }
314                 else
315                         $i+=$ascii ? 1 : 2;
316         }
317         //Last chunk
318         if($i!=$j)
319                 $this->Cell($l/1000*$this->FontSize,$h,substr($s,$j,$i-$j),0,0,'',0,$link);
320 }
321
322 function _putfonts()
323 {
324         $nf=$this->n;
325         foreach($this->diffs as $diff)
326         {
327                 //Encodings
328                 $this->_newobj();
329                 $this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>');
330                 $this->_out('endobj');
331         }
332         $mqr=get_magic_quotes_runtime();
333         //set_magic_quotes_runtime(0);
334         foreach($this->FontFiles as $file=>$info)
335         {
336                 //Font file embedding
337                 $this->_newobj();
338                 $this->FontFiles[$file]['n']=$this->n;
339                 if(defined('FPDF_FONTPATH'))
340                         $file=FPDF_FONTPATH.$file;
341                 $size=filesize($file);
342                 if(!$size)
343                         $this->Error('Font file not found');
344                 $this->_out('<</Length '.$size);
345                 if(substr($file,-2)=='.z')
346                         $this->_out('/Filter /FlateDecode');
347                 $this->_out('/Length1 '.$info['length1']);
348                 if(isset($info['length2']))
349                         $this->_out('/Length2 '.$info['length2'].' /Length3 0');
350                 $this->_out('>>');
351                 $f=fopen($file,'rb');
352                 $this->_putstream(fread($f,$size));
353                 fclose($f);
354                 $this->_out('endobj');
355         }
356 //      set_magic_quotes_runtime($mqr);
357         foreach($this->fonts as $k=>$font)
358         {
359                 //Font objects
360                 $this->_newobj();
361                 $this->fonts[$k]['n']=$this->n;
362                 $this->_out('<</Type /Font');
363                 if($font['type']=='Type0')
364                         $this->_putType0($font);
365                 else
366                 {
367                         $name=$font['name'];
368                         $this->_out('/BaseFont /'.$name);
369                         if($font['type']=='core')
370                         {
371                                 //Standard font
372                                 $this->_out('/Subtype /Type1');
373                                 if($name!='Symbol' and $name!='ZapfDingbats')
374                                         $this->_out('/Encoding /WinAnsiEncoding');
375                         }
376                         else
377                         {
378                                 //Additional font
379                                 $this->_out('/Subtype /'.$font['type']);
380                                 $this->_out('/FirstChar 32');
381                                 $this->_out('/LastChar 255');
382                                 $this->_out('/Widths '.($this->n+1).' 0 R');
383                                 $this->_out('/FontDescriptor '.($this->n+2).' 0 R');
384                                 if($font['enc'])
385                                 {
386                                         if(isset($font['diff']))
387                                                 $this->_out('/Encoding '.($nf+$font['diff']).' 0 R');
388                                         else
389                                                 $this->_out('/Encoding /WinAnsiEncoding');
390                                 }
391                         }
392                         $this->_out('>>');
393                         $this->_out('endobj');
394                         if($font['type']!='core')
395                         {
396                                 //Widths
397                                 $this->_newobj();
398                                 $cw=&$font['cw'];
399                                 $s='[';
400                                 for($i=32;$i<=255;$i++)
401                                         $s.=$cw[chr($i)].' ';
402                                 $this->_out($s.']');
403                                 $this->_out('endobj');
404                                 //Descriptor
405                                 $this->_newobj();
406                                 $s='<</Type /FontDescriptor /FontName /'.$name;
407                                 foreach($font['desc'] as $k=>$v)
408                                         $s.=' /'.$k.' '.$v;
409                                 $file=$font['file'];
410                                 if($file)
411                                         $s.=' /FontFile'.($font['type']=='Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R';
412                                 $this->_out($s.'>>');
413                                 $this->_out('endobj');
414                         }
415                 }
416         }
417 }
418
419 function _putType0($font)
420 {
421         //Type0
422         $this->_out('/Subtype /Type0');
423         $this->_out('/BaseFont /'.$font['name'].'-'.$font['CMap']);
424         $this->_out('/Encoding /'.$font['CMap']);
425         $this->_out('/DescendantFonts ['.($this->n+1).' 0 R]');
426         $this->_out('>>');
427         $this->_out('endobj');
428         //CIDFont
429         $this->_newobj();
430         $this->_out('<</Type /Font');
431         $this->_out('/Subtype /CIDFontType0');
432         $this->_out('/BaseFont /'.$font['name']);
433         $this->_out('/CIDSystemInfo <</Registry '.$this->_textstring('Adobe').' /Ordering '.$this->_textstring($font['registry']['ordering']).' /Supplement '.$font['registry']['supplement'].'>>');
434         $this->_out('/FontDescriptor '.($this->n+1).' 0 R');
435         if($font['CMap']=='ETen-B5-H')
436                 $W='13648 13742 500';
437         elseif($font['CMap']=='GBK-EUC-H')
438                 $W='814 907 500 7716 [500]';
439         else
440                 $W='1 ['.implode(' ',$font['cw']).']';
441         $this->_out('/W ['.$W.']>>');
442         $this->_out('endobj');
443         //Font descriptor
444         $this->_newobj();
445         $this->_out('<</Type /FontDescriptor');
446         $this->_out('/FontName /'.$font['name']);
447         $this->_out('/Flags 6');
448         $this->_out('/FontBBox [0 -200 1000 900]');
449         $this->_out('/ItalicAngle 0');
450         $this->_out('/Ascent 800');
451         $this->_out('/Descent -200');
452         $this->_out('/CapHeight 800');
453         $this->_out('/StemV 50');
454         $this->_out('>>');
455         $this->_out('endobj');
456 }
457
458 }
459 ?>