]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/fpdf/japanese.php
Remove rcs_id
[SourceForge/phpwiki.git] / lib / fpdf / japanese.php
1 <?php // -*-php-*-
2 // $Id$
3
4 // PDF functions taken from FPDF http://www.fpdf.org
5
6 require_once('lib/pdf.php');
7
8 class PDF_Japanese extends PDF {
9     var $B;
10     var $I;
11     var $U;
12     var $HREF;
13     var $SJIS_widths = array(' '=>278,'!'=>299,'"'=>353,'#'=>614,'$'=>614,'%'=>721,'&'=>735,'\''=>216,
14                              '('=>323,')'=>323,'*'=>449,'+'=>529,','=>219,'-'=>306,'.'=>219,'/'=>453,'0'=>614,'1'=>614,
15                              '2'=>614,'3'=>614,'4'=>614,'5'=>614,'6'=>614,'7'=>614,'8'=>614,'9'=>614,':'=>219,';'=>219,
16                              '<'=>529,'='=>529,'>'=>529,'?'=>486,'@'=>744,'A'=>646,'B'=>604,'C'=>617,'D'=>681,'E'=>567,
17                              'F'=>537,'G'=>647,'H'=>738,'I'=>320,'J'=>433,'K'=>637,'L'=>566,'M'=>904,'N'=>710,'O'=>716,
18                              'P'=>605,'Q'=>716,'R'=>623,'S'=>517,'T'=>601,'U'=>690,'V'=>668,'W'=>990,'X'=>681,'Y'=>634,
19                              'Z'=>578,'['=>316,'\\'=>614,']'=>316,'^'=>529,'_'=>500,'`'=>387,'a'=>509,'b'=>566,'c'=>478,
20                              'd'=>565,'e'=>503,'f'=>337,'g'=>549,'h'=>580,'i'=>275,'j'=>266,'k'=>544,'l'=>276,'m'=>854,
21                              'n'=>579,'o'=>550,'p'=>578,'q'=>566,'r'=>410,'s'=>444,'t'=>340,'u'=>575,'v'=>512,'w'=>760,
22                              'x'=>503,'y'=>529,'z'=>453,'{'=>326,'|'=>380,'}'=>326,'~'=>387);
23
24     function AddCIDFont($family,$style,$name,$cw,$CMap,$registry)
25     {
26         $fontkey=strtolower($family).strtoupper($style);
27         if(isset($this->fonts[$fontkey]))
28             $this->Error("CID font already added: $family $style");
29         $i=count($this->fonts)+1;
30         $this->fonts[$fontkey]=array('i'=>$i,'type'=>'Type0','name'=>$name,'up'=>-120,'ut'=>40,'cw'=>$cw,'CMap'=>$CMap,'registry'=>$registry);
31     }
32
33     function AddCIDFonts($family,$name,$cw,$CMap,$registry)
34     {
35         $this->AddCIDFont($family,'',$name,$cw,$CMap,$registry);
36         $this->AddCIDFont($family,'B',$name.',Bold',$cw,$CMap,$registry);
37         $this->AddCIDFont($family,'I',$name.',Italic',$cw,$CMap,$registry);
38         $this->AddCIDFont($family,'BI',$name.',BoldItalic',$cw,$CMap,$registry);
39     }
40
41     function AddSJISFont($family='SJIS')
42     {
43         //Add SJIS font with proportional Latin
44         $name='KozMinPro-Regular-Acro';
45         $cw=$this->SJIS_widths;
46         $CMap='90msp-RKSJ-H';
47         $registry=array('ordering'=>'Japan1','supplement'=>2);
48         $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
49     }
50     
51     function AddSJIShwFont($family='SJIS-hw')
52     {
53         //Add SJIS font with half-width Latin
54         $name='KozMinPro-Regular-Acro';
55         for($i=32;$i<=126;$i++)
56             $cw[chr($i)]=500;
57         $CMap='90ms-RKSJ-H';
58         $registry=array('ordering'=>'Japan1','supplement'=>2);
59         $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
60     }
61
62     function GetStringWidth($s)
63     {
64         if($this->CurrentFont['type']=='Type0')
65             return $this->GetSJISStringWidth($s);
66         else
67             return parent::GetStringWidth($s);
68     }
69
70     function GetSJISStringWidth($s)
71     {
72         //SJIS version of GetStringWidth()
73         $l=0;
74         $cw=&$this->CurrentFont['cw'];
75         $nb=strlen($s);
76         $i=0;
77         while($i<$nb) {
78             $o=ord($s{$i});
79             if($o<128) {
80                 //ASCII
81                 $l+=$cw[$s{$i}];
82                 $i++;
83             }
84             elseif($o>=161 and $o<=223) {
85                 //Half-width katakana
86                 $l+=500;
87                 $i++;
88             } else {
89                 //Full-width character
90                 $l+=1000;
91                 $i+=2;
92             }
93         }
94         return $l*$this->FontSize/1000;
95     }
96
97     function MultiCell($w,$h,$txt,$border=0,$align='L',$fill=0)
98     {
99         if($this->CurrentFont['type']=='Type0')
100             $this->SJISMultiCell($w,$h,$txt,$border,$align,$fill);
101         else
102             parent::MultiCell($w,$h,$txt,$border,$align,$fill);
103     }
104   
105     function SJISMultiCell($w,$h,$txt,$border=0,$align='L',$fill=0)
106     {
107         //Output text with automatic or explicit line breaks
108         $cw=&$this->CurrentFont['cw'];
109         if($w==0)
110             $w=$this->w-$this->rMargin-$this->x;
111         $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
112         $s=str_replace("\r",'',$txt);
113         $nb=strlen($s);
114         if($nb>0 and $s{$nb-1}=="\n")
115             $nb--;
116         $b=0;
117         if($border) {
118             if($border==1) {
119                 $border='LTRB';
120                 $b='LRT';
121                 $b2='LR';
122             } else {
123                 $b2='';
124                 if(is_int(strpos($border,'L')))
125                     $b2.='L';
126                 if(is_int(strpos($border,'R')))
127                     $b2.='R';
128                 $b=is_int(strpos($border,'T')) ? $b2.'T' : $b2;
129             }
130         }
131         $sep=-1;
132         $i=0;
133         $j=0;
134         $l=0;
135         $nl=1;
136         while($i<$nb) {
137             //Get next character
138             $c=$s{$i};
139             $o=ord($c);
140             if($o==10) {
141                 //Explicit line break
142                 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
143                 $i++;
144                 $sep=-1;
145                 $j=$i;
146                 $l=0;
147                 $nl++;
148                 if($border and $nl==2)
149                     $b=$b2;
150                 continue;
151             }
152             if($o<128) {
153                 //ASCII
154                 $l+=$cw[$c];
155                 $n=1;
156                 if($o==32)
157                     $sep=$i;
158             } elseif($o>=161 and $o<=223) {
159                 //Half-width katakana
160                 $l+=500;
161                 $n=1;
162                 $sep=$i;
163             } else {
164                 //Full-width character
165                 $l+=1000;
166                 $n=2;
167                 $sep=$i;
168             }
169             if($l>$wmax) {
170                 //Automatic line break
171                 if($sep==-1 or $i==$j) {
172                     if($i==$j)
173                         $i+=$n;
174                     $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
175                 } else {
176                     $this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);
177                     $i=($s[$sep]==' ') ? $sep+1 : $sep;
178                 }
179                 $sep=-1;
180                 $j=$i;
181                 $l=0;
182                 $nl++;
183                 if($border and $nl==2)
184                     $b=$b2;
185             } else {
186                 $i+=$n;
187                 if($o>=128)
188                     $sep=$i;
189             }
190         }
191         //Last chunk
192         if($border and is_int(strpos($border,'B')))
193             $b.='B';
194         $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
195         $this->x=$this->lMargin;
196     }
197
198     function Write($h,$txt,$link='')
199     {
200         if($this->CurrentFont['type']=='Type0')
201             $this->SJISWrite($h,$txt,$link);
202         else
203             parent::Write($h,$txt,$link);
204     }
205
206     function SJISWrite($h,$txt,$link)
207     {
208         //SJIS version of Write()
209         $cw=&$this->CurrentFont['cw'];
210         $w=$this->w-$this->rMargin-$this->x;
211         $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
212         $s=str_replace("\r",'',$txt);
213         $nb=strlen($s);
214         $sep=-1;
215         $i=0;
216         $j=0;
217         $l=0;
218         $nl=1;
219         while($i<$nb) {
220             //Get next character
221             $c=$s{$i};
222             $o=ord($c);
223             if($o==10) {
224                 //Explicit line break
225                 $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
226                 $i++;
227                 $sep=-1;
228                 $j=$i;
229                 $l=0;
230                 if($nl==1) {
231                     //Go to left margin
232                     $this->x=$this->lMargin;
233                     $w=$this->w-$this->rMargin-$this->x;
234                     $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
235                 }
236                 $nl++;
237                 continue;
238             }
239             if($o<128) {
240                 //ASCII
241                 $l+=$cw[$c];
242                 $n=1;
243                 if($o==32)
244                     $sep=$i;
245             } elseif($o>=161 and $o<=223) {
246                 //Half-width katakana
247                 $l+=500;
248                 $n=1;
249                 $sep=$i;
250             } else {
251                 //Full-width character
252                 $l+=1000;
253                 $n=2;
254                 $sep=$i;
255             }
256             if($l>$wmax) {
257                 //Automatic line break
258                 if($sep==-1 or $i==$j) {
259                     if($this->x>$this->lMargin) {
260                         //Move to next line
261                         $this->x=$this->lMargin;
262                         $this->y+=$h;
263                         $w=$this->w-$this->rMargin-$this->x;
264                         $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
265                         $i+=$n;
266                         $nl++;
267                         continue;
268                     }
269                     if($i==$j)
270                         $i+=$n;
271                     $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
272                 } else {
273                     $this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',0,$link);
274                     $i=($s[$sep]==' ') ? $sep+1 : $sep;
275                 }
276                 $sep=-1;
277                 $j=$i;
278                 $l=0;
279                 if($nl==1) {
280                     $this->x=$this->lMargin;
281                     $w=$this->w-$this->rMargin-$this->x;
282                     $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
283                 }
284                 $nl++;
285             } else {
286                 $i+=$n;
287                 if($o>=128)
288                     $sep=$i;
289             }
290         }
291         //Last chunk
292         if($i!=$j)
293             $this->Cell($l/1000*$this->FontSize,$h,substr($s,$j,$i-$j),0,0,'',0,$link);
294     }
295
296     function _putfonts()
297     {
298         $nf=$this->n;
299         foreach($this->diffs as $diff) {
300             //Encodings
301             $this->_newobj();
302             $this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>');
303             $this->_out('endobj');
304         }
305         if (!check_php_version(5,3)) {
306                 $mqr=get_magic_quotes_runtime();
307                 set_magic_quotes_runtime(0);
308         }
309         foreach($this->FontFiles as $file=>$info) {
310             //Font file embedding
311             $this->_newobj();
312             $this->FontFiles[$file]['n']=$this->n;
313             if(defined('FPDF_FONTPATH'))
314                 $file=FPDF_FONTPATH.$file;
315             $size=filesize($file);
316             if(!$size)
317                 $this->Error('Font file not found');
318             $this->_out('<</Length '.$size);
319             if(substr($file,-2)=='.z')
320                 $this->_out('/Filter /FlateDecode');
321             $this->_out('/Length1 '.$info['length1']);
322             if(isset($info['length2']))
323                 $this->_out('/Length2 '.$info['length2'].' /Length3 0');
324             $this->_out('>>');
325             $f=fopen($file,'rb');
326             $this->_putstream(fread($f,$size));
327             fclose($f);
328             $this->_out('endobj');
329         }
330         if (!check_php_version(5,3)) {
331                 set_magic_quotes_runtime($mqr);
332         }
333         foreach($this->fonts as $k=>$font) {
334             //Font objects
335             $this->_newobj();
336             $this->fonts[$k]['n']=$this->n;
337             $this->_out('<</Type /Font');
338             if($font['type']=='Type0')
339                 $this->_putType0($font);
340             else {
341                 $name=$font['name'];
342                 $this->_out('/BaseFont /'.$name);
343                 if($font['type']=='core') {
344                     //Standard font
345                     $this->_out('/Subtype /Type1');
346                     if($name!='Symbol' and $name!='ZapfDingbats')
347                         $this->_out('/Encoding /WinAnsiEncoding');
348                 } else {
349                     //Additional font
350                     $this->_out('/Subtype /'.$font['type']);
351                     $this->_out('/FirstChar 32');
352                     $this->_out('/LastChar 255');
353                     $this->_out('/Widths '.($this->n+1).' 0 R');
354                     $this->_out('/FontDescriptor '.($this->n+2).' 0 R');
355                     if($font['enc']) {
356                         if(isset($font['diff']))
357                             $this->_out('/Encoding '.($nf+$font['diff']).' 0 R');
358                         else
359                             $this->_out('/Encoding /WinAnsiEncoding');
360                     }
361                 }
362                 $this->_out('>>');
363                 $this->_out('endobj');
364                 if($font['type']!='core') {
365                     //Widths
366                     $this->_newobj();
367                     $cw=&$font['cw'];
368                     $s='[';
369                     for($i=32;$i<=255;$i++)
370                         $s.=$cw[chr($i)].' ';
371                     $this->_out($s.']');
372                     $this->_out('endobj');
373                     //Descriptor
374                     $this->_newobj();
375                     $s='<</Type /FontDescriptor /FontName /'.$name;
376                     foreach($font['desc'] as $k=>$v)
377                         $s.=' /'.$k.' '.$v;
378                     $file=$font['file'];
379                     if($file)
380                         $s.=' /FontFile'.($font['type']=='Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R';
381                     $this->_out($s.'>>');
382                     $this->_out('endobj');
383                 }
384             }
385         }
386     }
387
388     function _putType0($font)
389     {
390         //Type0
391         $this->_out('/Subtype /Type0');
392         $this->_out('/BaseFont /'.$font['name'].'-'.$font['CMap']);
393         $this->_out('/Encoding /'.$font['CMap']);
394         $this->_out('/DescendantFonts ['.($this->n+1).' 0 R]');
395         $this->_out('>>');
396         $this->_out('endobj');
397         //CIDFont
398         $this->_newobj();
399         $this->_out('<</Type /Font');
400         $this->_out('/Subtype /CIDFontType0');
401         $this->_out('/BaseFont /'.$font['name']);
402         $this->_out('/CIDSystemInfo <</Registry (Adobe) /Ordering ('.$font['registry']['ordering'].') /Supplement '.$font['registry']['supplement'].'>>');
403         $this->_out('/FontDescriptor '.($this->n+1).' 0 R');
404         $W='/W [1 [';
405         foreach($font['cw'] as $w)
406             $W.=$w.' ';
407         $this->_out($W.'] 231 325 500 631 [500] 326 389 500]');
408         $this->_out('>>');
409         $this->_out('endobj');
410         //Font descriptor
411         $this->_newobj();
412         $this->_out('<</Type /FontDescriptor');
413         $this->_out('/FontName /'.$font['name']);
414         $this->_out('/Flags 6');
415         $this->_out('/FontBBox [0 -200 1000 900]');
416         $this->_out('/ItalicAngle 0');
417         $this->_out('/Ascent 800');
418         $this->_out('/Descent -200');
419         $this->_out('/CapHeight 800');
420         $this->_out('/StemV 60');
421         $this->_out('>>');
422         $this->_out('endobj');
423     }
424 }
425
426 // Local Variables:
427 // mode: php
428 // tab-width: 8
429 // c-basic-offset: 4
430 // c-hanging-comment-ender-p: nil
431 // indent-tabs-mode: nil
432 // End:
433 ?>