909e06b3606f7248d6cd14c7f59a7f154db2528b
[pear] / FpdfAlt / Chinese.php
1 <?php
2 require_once 'FpdfAlt.php';
3
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);
14
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);
25
26 class FpdfAlt_Chinese extends FpdfAlt
27 {
28     function AddCIDFont($family,$style,$name,$cw,$CMap,$registry)
29     {
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);
36     }
37     
38     function AddCIDFonts($family,$name,$cw,$CMap,$registry)
39     {
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);
44     }
45     
46     function AddBig5Font($family='Big5',$name='MSungStd-Light-Acro')
47     {
48         //Add Big5 font with proportional Latin
49         $cw=$GLOBALS['Big5_widths'];
50         $CMap='ETenms-B5-H';
51         $registry=array('ordering'=>'CNS1','supplement'=>0);
52         $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
53     }
54     
55     function AddBig5hwFont($family='Big5-hw',$name='MMingStd-Light-Acro')
56     {
57         //Add Big5 font with half-witdh Latin
58         for($i=32;$i<=126;$i++)
59             $cw[chr($i)]=500;
60         $CMap='ETen-B5-H';
61         $registry=array('ordering'=>'CNS1','supplement'=>0);
62         $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
63     }
64     function AddUniCNShwFont($family='Uni-hw',$name='PMingLiU')
65     {
66         //Add Unicode font with half-witdh Latin
67         for($i=32;$i<=126;$i++)
68         $cw[chr($i)]=500;
69         $CMap='UniCNS-UTF16-H';
70         $registry=array('ordering'=>'CNS1','supplement'=>4);
71         $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
72     } 
73      
74     
75     function AddGBFont($family='GB',$name='STSongStd-Light-Acro')
76     {
77         //Add GB font with proportional Latin
78         $cw=$GLOBALS['GB_widths'];
79         $CMap='GBKp-EUC-H';
80         $registry=array('ordering'=>'GB1','supplement'=>2);
81         $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
82     }
83     
84     function AddGBhwFont($family='GB-hw',$name='STSongStd-Light-Acro')
85     {
86         //Add GB font with half-width Latin
87         for($i=32;$i<=126;$i++)
88             $cw[chr($i)]=500;
89         $CMap='GBK-EUC-H';
90         $registry=array('ordering'=>'GB1','supplement'=>2);
91         $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
92     }
93     
94     function GetStringWidth($s)
95     {
96         if($this->CurrentFont['type']=='Type0')
97             return $this->GetMBStringWidth($s);
98         else
99             return parent::GetStringWidth($s);
100     }
101     
102     function GetMBStringWidth($s)
103     {
104         //Multi-byte version of GetStringWidth()
105         $l=0;
106         $cw=&$this->CurrentFont['cw'];
107         $nb=strlen($s);
108         $i=0;
109         while($i<$nb)
110         {
111             $c=$s[$i];
112             if(ord($c)<128)
113             {
114                 $l+=$cw[$c];
115                 $i++;
116             }
117             else
118             {
119                 $l+=1000;
120                 $i+=2;
121             }
122         }
123         return $l*$this->FontSize/1000;
124     }
125     
126     function MultiCell($w,$h,$txt,$border=0,$align='L',$fill=0)
127     {
128         if($this->CurrentFont['type']=='Type0')
129             $this->MBMultiCell($w,$h,$txt,$border,$align,$fill);
130         else
131             parent::MultiCell($w,$h,$txt,$border,$align,$fill);
132     }
133     
134     function MBMultiCell($w,$h,$txt,$border=0,$align='L',$fill=0)
135     {
136         //Multi-byte version of MultiCell()
137         $cw=&$this->CurrentFont['cw'];
138         if($w==0)
139             $w=$this->w-$this->rMargin-$this->x;
140         $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
141         $s=str_replace("\r",'',$txt);
142         $nb=strlen($s);
143         if($nb>0 and $s[$nb-1]=="\n")
144             $nb--;
145         $b=0;
146         if($border)
147         {
148             if($border==1)
149             {
150                 $border='LTRB';
151                 $b='LRT';
152                 $b2='LR';
153             }
154             else
155             {
156                 $b2='';
157                 if(is_int(strpos($border,'L')))
158                     $b2.='L';
159                 if(is_int(strpos($border,'R')))
160                     $b2.='R';
161                 $b=is_int(strpos($border,'T')) ? $b2.'T' : $b2;
162             }
163         }
164         $sep=-1;
165         $i=0;
166         $j=0;
167         $l=0;
168         $nl=1;
169         while($i<$nb)
170         {
171             //Get next character
172             $c=$s[$i];
173             //Check if ASCII or MB
174             $ascii=(ord($c)<128);
175             if($c=="\n")
176             {
177                 //Explicit line break
178                 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
179                 $i++;
180                 $sep=-1;
181                 $j=$i;
182                 $l=0;
183                 $nl++;
184                 if($border and $nl==2)
185                     $b=$b2;
186                 continue;
187             }
188             if(!$ascii)
189             {
190                 $sep=$i;
191                 $ls=$l;
192             }
193             elseif($c==' ')
194             {
195                 $sep=$i;
196                 $ls=$l;
197             }
198             $l+=$ascii ? $cw[$c] : 1000;
199             if($l>$wmax)
200             {
201                 //Automatic line break
202                 if($sep==-1 or $i==$j)
203                 {
204                     if($i==$j)
205                         $i+=$ascii ? 1 : 2;
206                     $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
207                 }
208                 else
209                 {
210                     $this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);
211                     $i=($s[$sep]==' ') ? $sep+1 : $sep;
212                 }
213                 $sep=-1;
214                 $j=$i;
215                 $l=0;
216                 $nl++;
217                 if($border and $nl==2)
218                     $b=$b2;
219             }
220             else
221                 $i+=$ascii ? 1 : 2;
222         }
223         //Last chunk
224         if($border and is_int(strpos($border,'B')))
225             $b.='B';
226         $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
227         $this->x=$this->lMargin;
228     }
229     
230     function Write($h,$txt,$link='')
231     {
232         if($this->CurrentFont['type']=='Type0')
233             $this->MBWrite($h,$txt,$link);
234         else
235             parent::Write($h,$txt,$link);
236     }
237     
238     function MBWrite($h,$txt,$link)
239     {
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);
245         $nb=strlen($s);
246         $sep=-1;
247         $i=0;
248         $j=0;
249         $l=0;
250         $nl=1;
251         while($i<$nb)
252         {
253             //Get next character
254             $c=$s[$i];
255             //Check if ASCII or MB
256             $ascii=(ord($c)<128);
257             if($c=="\n")
258             {
259                 //Explicit line break
260                 $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
261                 $i++;
262                 $sep=-1;
263                 $j=$i;
264                 $l=0;
265                 if($nl==1)
266                 {
267                     $this->x=$this->lMargin;
268                     $w=$this->w-$this->rMargin-$this->x;
269                     $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
270                 }
271                 $nl++;
272                 continue;
273             }
274             if(!$ascii or $c==' ')
275                 $sep=$i;
276             $l+=$ascii ? $cw[$c] : 1000;
277             if($l>$wmax)
278             {
279                 //Automatic line break
280                 if($sep==-1 or $i==$j)
281                 {
282                     if($this->x>$this->lMargin)
283                     {
284                         //Move to next line
285                         $this->x=$this->lMargin;
286                         $this->y+=$h;
287                         $w=$this->w-$this->rMargin-$this->x;
288                         $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
289                         $i++;
290                         $nl++;
291                         continue;
292                     }
293                     if($i==$j)
294                         $i+=$ascii ? 1 : 2;
295                     $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
296                 }
297                 else
298                 {
299                     $this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',0,$link);
300                     $i=($s[$sep]==' ') ? $sep+1 : $sep;
301                 }
302                 $sep=-1;
303                 $j=$i;
304                 $l=0;
305                 if($nl==1)
306                 {
307                     $this->x=$this->lMargin;
308                     $w=$this->w-$this->rMargin-$this->x;
309                     $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
310                 }
311                 $nl++;
312             }
313             else
314                 $i+=$ascii ? 1 : 2;
315         }
316         //Last chunk
317         if($i!=$j)
318             $this->Cell($l/1000*$this->FontSize,$h,substr($s,$j,$i-$j),0,0,'',0,$link);
319     }
320     
321     function _putfonts()
322     {
323         $nf=$this->n;
324         foreach($this->diffs as $diff)
325         {
326             //Encodings
327             $this->_newobj();
328             $this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>');
329             $this->_out('endobj');
330         }
331         foreach($this->FontFiles as $file=>$info)
332         {
333             //Font file embedding
334             $this->_newobj();
335             $this->FontFiles[$file]['n']=$this->n;
336             if(defined('FPDF_FONTPATH'))
337                 $file=FPDF_FONTPATH.$file;
338             $size=filesize($file);
339             if(!$size)
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');
347             $this->_out('>>');
348             $f=fopen($file,'rb');
349             $this->_putstream(fread($f,$size));
350             fclose($f);
351             $this->_out('endobj');
352         }
353         foreach($this->fonts as $k=>$font)
354         {
355             //Font objects
356             $this->_newobj();
357             $this->fonts[$k]['n']=$this->n;
358             $this->_out('<</Type /Font');
359             if($font['type']=='Type0')
360                 $this->_putType0($font);
361             else
362             {
363                 $name=$font['name'];
364                 $this->_out('/BaseFont /'.$name);
365                 if($font['type']=='core')
366                 {
367                     //Standard font
368                     $this->_out('/Subtype /Type1');
369                     if($name!='Symbol' and $name!='ZapfDingbats')
370                         $this->_out('/Encoding /WinAnsiEncoding');
371                 }
372                 else
373                 {
374                     //Additional font
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');
380                     if($font['enc'])
381                     {
382                         if(isset($font['diff']))
383                             $this->_out('/Encoding '.($nf+$font['diff']).' 0 R');
384                         else
385                             $this->_out('/Encoding /WinAnsiEncoding');
386                     }
387                 }
388                 $this->_out('>>');
389                 $this->_out('endobj');
390                 if($font['type']!='core')
391                 {
392                     //Widths
393                     $this->_newobj();
394                     $cw=&$font['cw'];
395                     $s='[';
396                     for($i=32;$i<=255;$i++)
397                         $s.=$cw[chr($i)].' ';
398                     $this->_out($s.']');
399                     $this->_out('endobj');
400                     //Descriptor
401                     $this->_newobj();
402                     $s='<</Type /FontDescriptor /FontName /'.$name;
403                     foreach($font['desc'] as $k=>$v)
404                         $s.=' /'.$k.' '.$v;
405                     $file=$font['file'];
406                     if($file)
407                         $s.=' /FontFile'.($font['type']=='Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R';
408                     $this->_out($s.'>>');
409                     $this->_out('endobj');
410                 }
411             }
412         }
413     }
414     
415     function _putType0($font)
416 {
417         //Type0
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]');
422         $this->_out('>>');
423         $this->_out('endobj');
424         //CIDFont
425         $this->_newobj();
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]';
435         else
436         //print_r( $font['cw']);exit;
437                 $W='1 ['.implode(' ',$font['cw']).']';
438         $this->_out('/W ['.$W.']>>');
439         $this->_out('endobj');
440         //Font descriptor
441         $this->_newobj();
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');
451         $this->_out('>>');
452         $this->_out('endobj');
453 }
454 }
455 ?>