]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/fpdf/chinese.php
Remove rcs_id
[SourceForge/phpwiki.git] / lib / fpdf / chinese.php
1 <?php // -*-php-*-
2 // $Id$
3
4 // PDF functions taken from FPDF http://www.fpdf.org
5
6 require('lib/pdf.php');
7
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);
18
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);
29
30 class PDF_Chinese extends PDF
31 {
32     function AddCIDFont($family,$style,$name,$cw,$CMap,$registry)
33     {
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);
40     }
41
42     function AddCIDFonts($family,$name,$cw,$CMap,$registry)
43     {
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);
48     }
49
50     function AddBig5Font($family='Big5',$name='MSungStd-Light-Acro')
51     {
52         //Add Big5 font with proportional Latin
53         $cw=$GLOBALS['Big5_widths'];
54         $CMap='ETenms-B5-H';
55         $registry=array('ordering'=>'CNS1','supplement'=>0);
56         $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
57     }
58
59     function AddBig5hwFont($family='Big5-hw',$name='MSungStd-Light-Acro')
60     {
61         //Add Big5 font with half-witdh Latin
62         for($i=32;$i<=126;$i++)
63                 $cw[chr($i)]=500;
64         $CMap='ETen-B5-H';
65         $registry=array('ordering'=>'CNS1','supplement'=>0);
66         $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
67     }
68
69     function AddGBFont($family='GB',$name='STSongStd-Light-Acro')
70     {
71         //Add GB font with proportional Latin
72         $cw=$GLOBALS['GB_widths'];
73         $CMap='GBKp-EUC-H';
74         $registry=array('ordering'=>'GB1','supplement'=>2);
75         $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
76     }
77
78     function AddGBhwFont($family='GB-hw',$name='STSongStd-Light-Acro')
79     {
80         //Add GB font with half-width Latin
81         for($i=32;$i<=126;$i++)
82                 $cw[chr($i)]=500;
83         $CMap='GBK-EUC-H';
84         $registry=array('ordering'=>'GB1','supplement'=>2);
85         $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
86     }
87
88     function GetStringWidth($s)
89     {
90         if ($this->CurrentFont['type']=='Type0')
91                 return $this->GetMBStringWidth($s);
92         else
93                 return parent::GetStringWidth($s);
94     }
95
96     function GetMBStringWidth($s)
97     {
98         //Multi-byte version of GetStringWidth()
99         $l=0;
100         $cw=&$this->CurrentFont['cw'];
101         $nb=strlen($s);
102         $i=0;
103         while($i<$nb)
104         {
105                 $c=$s[$i];
106                 if(ord($c)<128)
107                 {
108                         $l+=$cw[$c];
109                         $i++;
110                 }
111                 else
112                 {
113                         $l+=1000;
114                         $i+=2;
115                 }
116         }
117         return $l*$this->FontSize/1000;
118     }
119
120     function MultiCell($w,$h,$txt,$border=0,$align='L',$fill=0)
121     {
122         if($this->CurrentFont['type']=='Type0')
123                 $this->MBMultiCell($w,$h,$txt,$border,$align,$fill);
124         else
125                 parent::MultiCell($w,$h,$txt,$border,$align,$fill);
126     }
127
128     function MBMultiCell($w,$h,$txt,$border=0,$align='L',$fill=0)
129     {
130         //Multi-byte version of MultiCell()
131         $cw=&$this->CurrentFont['cw'];
132         if($w==0)
133                 $w=$this->w-$this->rMargin-$this->x;
134         $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
135         $s=str_replace("\r",'',$txt);
136         $nb=strlen($s);
137         if($nb>0 and $s[$nb-1]=="\n")
138                 $nb--;
139         $b=0;
140         if($border)
141         {
142                 if($border==1)
143                 {
144                         $border='LTRB';
145                         $b='LRT';
146                         $b2='LR';
147                 }
148                 else
149                 {
150                         $b2='';
151                         if(is_int(strpos($border,'L')))
152                                 $b2.='L';
153                         if(is_int(strpos($border,'R')))
154                                 $b2.='R';
155                         $b=is_int(strpos($border,'T')) ? $b2.'T' : $b2;
156                 }
157         }
158         $sep=-1;
159         $i=0;
160         $j=0;
161         $l=0;
162         $nl=1;
163         while($i<$nb)
164         {
165                 //Get next character
166                 $c=$s[$i];
167                 //Check if ASCII or MB
168                 $ascii=(ord($c)<128);
169                 if($c=="\n")
170                 {
171                         //Explicit line break
172                         $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
173                         $i++;
174                         $sep=-1;
175                         $j=$i;
176                         $l=0;
177                         $nl++;
178                         if($border and $nl==2)
179                                 $b=$b2;
180                         continue;
181                 }
182                 if(!$ascii)
183                 {
184                         $sep=$i;
185                         $ls=$l;
186                 }
187                 elseif($c==' ')
188                 {
189                         $sep=$i;
190                         $ls=$l;
191                 }
192                 $l+=$ascii ? $cw[$c] : 1000;
193                 if($l>$wmax)
194                 {
195                         //Automatic line break
196                         if($sep==-1 or $i==$j)
197                         {
198                                 if($i==$j)
199                                         $i+=$ascii ? 1 : 2;
200                                 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
201                         }
202                         else
203                         {
204                                 $this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);
205                                 $i=($s[$sep]==' ') ? $sep+1 : $sep;
206                         }
207                         $sep=-1;
208                         $j=$i;
209                         $l=0;
210                         $nl++;
211                         if($border and $nl==2)
212                                 $b=$b2;
213                 }
214                 else
215                         $i+=$ascii ? 1 : 2;
216         }
217         //Last chunk
218         if($border and is_int(strpos($border,'B')))
219                 $b.='B';
220         $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
221         $this->x=$this->lMargin;
222     }
223
224     function Write($h,$txt,$link='')
225     {
226         if($this->CurrentFont['type']=='Type0')
227                 $this->MBWrite($h,$txt,$link);
228         else
229                 parent::Write($h,$txt,$link);
230     }
231
232     function MBWrite($h,$txt,$link)
233     {
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);
239         $nb=strlen($s);
240         $sep=-1;
241         $i=0;
242         $j=0;
243         $l=0;
244         $nl=1;
245         while($i<$nb)
246         {
247                 //Get next character
248                 $c=$s[$i];
249                 //Check if ASCII or MB
250                 $ascii=(ord($c)<128);
251                 if($c=="\n")
252                 {
253                         //Explicit line break
254                         $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
255                         $i++;
256                         $sep=-1;
257                         $j=$i;
258                         $l=0;
259                         if($nl==1)
260                         {
261                                 $this->x=$this->lMargin;
262                                 $w=$this->w-$this->rMargin-$this->x;
263                                 $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
264                         }
265                         $nl++;
266                         continue;
267                 }
268                 if(!$ascii or $c==' ')
269                         $sep=$i;
270                 $l+=$ascii ? $cw[$c] : 1000;
271                 if($l>$wmax)
272                 {
273                         //Automatic line break
274                         if($sep==-1 or $i==$j)
275                         {
276                                 if($this->x>$this->lMargin)
277                                 {
278                                         //Move to next line
279                                         $this->x=$this->lMargin;
280                                         $this->y+=$h;
281                                         $w=$this->w-$this->rMargin-$this->x;
282                                         $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
283                                         $i++;
284                                         $nl++;
285                                         continue;
286                                 }
287                                 if($i==$j)
288                                         $i+=$ascii ? 1 : 2;
289                                 $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
290                         }
291                         else
292                         {
293                                 $this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',0,$link);
294                                 $i=($s[$sep]==' ') ? $sep+1 : $sep;
295                         }
296                         $sep=-1;
297                         $j=$i;
298                         $l=0;
299                         if($nl==1)
300                         {
301                                 $this->x=$this->lMargin;
302                                 $w=$this->w-$this->rMargin-$this->x;
303                                 $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
304                         }
305                         $nl++;
306                 }
307                 else
308                         $i+=$ascii ? 1 : 2;
309         }
310         //Last chunk
311         if($i!=$j)
312                 $this->Cell($l/1000*$this->FontSize,$h,substr($s,$j,$i-$j),0,0,'',0,$link);
313     }
314
315     function _putfonts()
316     {
317         $nf=$this->n;
318         foreach($this->diffs as $diff)
319         {
320                 //Encodings
321                 $this->_newobj();
322                 $this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>');
323                 $this->_out('endobj');
324         }
325
326         if (!check_php_version(5,3)) {
327                 $mqr=get_magic_quotes_runtime();
328                 set_magic_quotes_runtime(0);
329         }
330         foreach($this->FontFiles as $file=>$info)
331         {
332                 //Font file embedding
333                 $this->_newobj();
334                 $this->FontFiles[$file]['n']=$this->n;
335                 if(defined('FPDF_FONTPATH'))
336                         $file=FPDF_FONTPATH.$file;
337                 $size=filesize($file);
338                 if(!$size)
339                         $this->Error('Font file not found');
340                 $this->_out('<</Length '.$size);
341                 if(substr($file,-2)=='.z')
342                         $this->_out('/Filter /FlateDecode');
343                 $this->_out('/Length1 '.$info['length1']);
344                 if(isset($info['length2']))
345                         $this->_out('/Length2 '.$info['length2'].' /Length3 0');
346                 $this->_out('>>');
347                 $f=fopen($file,'rb');
348                 $this->_putstream(fread($f,$size));
349                 fclose($f);
350                 $this->_out('endobj');
351         }
352         if (!check_php_version(5,3)) {
353                 set_magic_quotes_runtime($mqr);
354         }
355         foreach($this->fonts as $k=>$font)
356         {
357                 //Font objects
358                 $this->_newobj();
359                 $this->fonts[$k]['n']=$this->n;
360                 $this->_out('<</Type /Font');
361                 if($font['type']=='Type0')
362                         $this->_putType0($font);
363                 else
364                 {
365                         $name=$font['name'];
366                         $this->_out('/BaseFont /'.$name);
367                         if($font['type']=='core')
368                         {
369                                 //Standard font
370                                 $this->_out('/Subtype /Type1');
371                                 if($name!='Symbol' and $name!='ZapfDingbats')
372                                         $this->_out('/Encoding /WinAnsiEncoding');
373                         }
374                         else
375                         {
376                                 //Additional font
377                                 $this->_out('/Subtype /'.$font['type']);
378                                 $this->_out('/FirstChar 32');
379                                 $this->_out('/LastChar 255');
380                                 $this->_out('/Widths '.($this->n+1).' 0 R');
381                                 $this->_out('/FontDescriptor '.($this->n+2).' 0 R');
382                                 if($font['enc'])
383                                 {
384                                         if(isset($font['diff']))
385                                                 $this->_out('/Encoding '.($nf+$font['diff']).' 0 R');
386                                         else
387                                                 $this->_out('/Encoding /WinAnsiEncoding');
388                                 }
389                         }
390                         $this->_out('>>');
391                         $this->_out('endobj');
392                         if($font['type']!='core')
393                         {
394                                 //Widths
395                                 $this->_newobj();
396                                 $cw=&$font['cw'];
397                                 $s='[';
398                                 for($i=32;$i<=255;$i++)
399                                         $s.=$cw[chr($i)].' ';
400                                 $this->_out($s.']');
401                                 $this->_out('endobj');
402                                 //Descriptor
403                                 $this->_newobj();
404                                 $s='<</Type /FontDescriptor /FontName /'.$name;
405                                 foreach($font['desc'] as $k=>$v)
406                                         $s.=' /'.$k.' '.$v;
407                                 $file=$font['file'];
408                                 if($file)
409                                         $s.=' /FontFile'.($font['type']=='Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R';
410                                 $this->_out($s.'>>');
411                                 $this->_out('endobj');
412                         }
413                 }
414         }
415     }
416
417     function _putType0($font)
418     {
419         //Type0
420         $this->_out('/Subtype /Type0');
421         $this->_out('/BaseFont /'.$font['name'].'-'.$font['CMap']);
422         $this->_out('/Encoding /'.$font['CMap']);
423         $this->_out('/DescendantFonts ['.($this->n+1).' 0 R]');
424         $this->_out('>>');
425         $this->_out('endobj');
426         //CIDFont
427         $this->_newobj();
428         $this->_out('<</Type /Font');
429         $this->_out('/Subtype /CIDFontType0');
430         $this->_out('/BaseFont /'.$font['name']);
431         $this->_out('/CIDSystemInfo <</Registry '.$this->_textstring('Adobe').' /Ordering '.$this->_textstring($font['registry']['ordering']).' /Supplement '.$font['registry']['supplement'].'>>');
432         $this->_out('/FontDescriptor '.($this->n+1).' 0 R');
433         if($font['CMap']=='ETen-B5-H')
434                 $W='13648 13742 500';
435         elseif($font['CMap']=='GBK-EUC-H')
436                 $W='814 907 500 7716 [500]';
437         else
438                 $W='1 ['.implode(' ',$font['cw']).']';
439         $this->_out('/W ['.$W.']>>');
440         $this->_out('endobj');
441         //Font descriptor
442         $this->_newobj();
443         $this->_out('<</Type /FontDescriptor');
444         $this->_out('/FontName /'.$font['name']);
445         $this->_out('/Flags 6');
446         $this->_out('/FontBBox [0 -200 1000 900]');
447         $this->_out('/ItalicAngle 0');
448         $this->_out('/Ascent 800');
449         $this->_out('/Descent -200');
450         $this->_out('/CapHeight 800');
451         $this->_out('/StemV 50');
452         $this->_out('>>');
453         $this->_out('endobj');
454     }
455 }
456
457 // Local Variables:
458 // mode: php
459 // tab-width: 8
460 // c-basic-offset: 4
461 // c-hanging-comment-ender-p: nil
462 // indent-tabs-mode: nil
463 // End:
464 ?>