2 // PDF functions taken from FPDF http://www.fpdf.org
4 require_once 'lib/pdf.php';
6 class PDF_Japanese extends PDF {
11 var $SJIS_widths = array(' '=>278,'!'=>299,'"'=>353,'#'=>614,'$'=>614,'%'=>721,'&'=>735,'\''=>216,
12 '('=>323,')'=>323,'*'=>449,'+'=>529,','=>219,'-'=>306,'.'=>219,'/'=>453,'0'=>614,'1'=>614,
13 '2'=>614,'3'=>614,'4'=>614,'5'=>614,'6'=>614,'7'=>614,'8'=>614,'9'=>614,':'=>219,';'=>219,
14 '<'=>529,'='=>529,'>'=>529,'?'=>486,'@'=>744,'A'=>646,'B'=>604,'C'=>617,'D'=>681,'E'=>567,
15 'F'=>537,'G'=>647,'H'=>738,'I'=>320,'J'=>433,'K'=>637,'L'=>566,'M'=>904,'N'=>710,'O'=>716,
16 'P'=>605,'Q'=>716,'R'=>623,'S'=>517,'T'=>601,'U'=>690,'V'=>668,'W'=>990,'X'=>681,'Y'=>634,
17 'Z'=>578,'['=>316,'\\'=>614,']'=>316,'^'=>529,'_'=>500,'`'=>387,'a'=>509,'b'=>566,'c'=>478,
18 'd'=>565,'e'=>503,'f'=>337,'g'=>549,'h'=>580,'i'=>275,'j'=>266,'k'=>544,'l'=>276,'m'=>854,
19 'n'=>579,'o'=>550,'p'=>578,'q'=>566,'r'=>410,'s'=>444,'t'=>340,'u'=>575,'v'=>512,'w'=>760,
20 'x'=>503,'y'=>529,'z'=>453,'{'=>326,'|'=>380,'}'=>326,'~'=>387);
22 function AddCIDFont($family,$style,$name,$cw,$CMap,$registry)
24 $fontkey=strtolower($family).strtoupper($style);
25 if(isset($this->fonts[$fontkey]))
26 $this->Error("CID font already added: $family $style");
27 $i=count($this->fonts)+1;
28 $this->fonts[$fontkey]=array('i'=>$i,'type'=>'Type0','name'=>$name,'up'=>-120,'ut'=>40,'cw'=>$cw,'CMap'=>$CMap,'registry'=>$registry);
31 function AddCIDFonts($family,$name,$cw,$CMap,$registry)
33 $this->AddCIDFont($family,'',$name,$cw,$CMap,$registry);
34 $this->AddCIDFont($family,'B',$name.',Bold',$cw,$CMap,$registry);
35 $this->AddCIDFont($family,'I',$name.',Italic',$cw,$CMap,$registry);
36 $this->AddCIDFont($family,'BI',$name.',BoldItalic',$cw,$CMap,$registry);
39 function AddSJISFont($family='SJIS')
41 //Add SJIS font with proportional Latin
42 $name='KozMinPro-Regular-Acro';
43 $cw=$this->SJIS_widths;
45 $registry=array('ordering'=>'Japan1','supplement'=>2);
46 $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
49 function AddSJIShwFont($family='SJIS-hw')
51 //Add SJIS font with half-width Latin
52 $name='KozMinPro-Regular-Acro';
53 for($i=32;$i<=126;$i++)
56 $registry=array('ordering'=>'Japan1','supplement'=>2);
57 $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
60 function GetStringWidth($s)
62 if($this->CurrentFont['type']=='Type0')
63 return $this->GetSJISStringWidth($s);
65 return parent::GetStringWidth($s);
68 function GetSJISStringWidth($s)
70 //SJIS version of GetStringWidth()
72 $cw=&$this->CurrentFont['cw'];
82 elseif($o>=161 and $o<=223) {
87 //Full-width character
92 return $l*$this->FontSize/1000;
95 function MultiCell($w,$h,$txt,$border=0,$align='L',$fill=0)
97 if($this->CurrentFont['type']=='Type0')
98 $this->SJISMultiCell($w,$h,$txt,$border,$align,$fill);
100 parent::MultiCell($w,$h,$txt,$border,$align,$fill);
103 function SJISMultiCell($w,$h,$txt,$border=0,$align='L',$fill=0)
105 //Output text with automatic or explicit line breaks
106 $cw=&$this->CurrentFont['cw'];
108 $w=$this->w-$this->rMargin-$this->x;
109 $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
110 $s=str_replace("\r",'',$txt);
112 if($nb>0 and $s{$nb-1}=="\n")
122 if(is_int(strpos($border,'L')))
124 if(is_int(strpos($border,'R')))
126 $b=is_int(strpos($border,'T')) ? $b2.'T' : $b2;
139 //Explicit line break
140 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
146 if($border and $nl==2)
156 } elseif($o>=161 and $o<=223) {
157 //Half-width katakana
162 //Full-width character
168 //Automatic line break
169 if($sep==-1 or $i==$j) {
172 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
174 $this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);
175 $i=($s[$sep]==' ') ? $sep+1 : $sep;
181 if($border and $nl==2)
190 if($border and is_int(strpos($border,'B')))
192 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
193 $this->x=$this->lMargin;
196 function Write($h,$txt,$link='')
198 if($this->CurrentFont['type']=='Type0')
199 $this->SJISWrite($h,$txt,$link);
201 parent::Write($h,$txt,$link);
204 function SJISWrite($h,$txt,$link)
206 //SJIS version of Write()
207 $cw=&$this->CurrentFont['cw'];
208 $w=$this->w-$this->rMargin-$this->x;
209 $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
210 $s=str_replace("\r",'',$txt);
222 //Explicit line break
223 $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
230 $this->x=$this->lMargin;
231 $w=$this->w-$this->rMargin-$this->x;
232 $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
243 } elseif($o>=161 and $o<=223) {
244 //Half-width katakana
249 //Full-width character
255 //Automatic line break
256 if($sep==-1 or $i==$j) {
257 if($this->x>$this->lMargin) {
259 $this->x=$this->lMargin;
261 $w=$this->w-$this->rMargin-$this->x;
262 $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
269 $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
271 $this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',0,$link);
272 $i=($s[$sep]==' ') ? $sep+1 : $sep;
278 $this->x=$this->lMargin;
279 $w=$this->w-$this->rMargin-$this->x;
280 $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
291 $this->Cell($l/1000*$this->FontSize,$h,substr($s,$j,$i-$j),0,0,'',0,$link);
297 foreach($this->diffs as $diff) {
300 $this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>');
301 $this->_out('endobj');
303 if (!check_php_version(5,3)) {
304 $mqr=get_magic_quotes_runtime();
305 set_magic_quotes_runtime(0);
307 foreach($this->FontFiles as $file=>$info) {
308 //Font file embedding
310 $this->FontFiles[$file]['n']=$this->n;
311 if(defined('FPDF_FONTPATH'))
312 $file=FPDF_FONTPATH.$file;
313 $size=filesize($file);
315 $this->Error('Font file not found');
316 $this->_out('<</Length '.$size);
317 if(substr($file,-2)=='.z')
318 $this->_out('/Filter /FlateDecode');
319 $this->_out('/Length1 '.$info['length1']);
320 if(isset($info['length2']))
321 $this->_out('/Length2 '.$info['length2'].' /Length3 0');
323 $f=fopen($file,'rb');
324 $this->_putstream(fread($f,$size));
326 $this->_out('endobj');
328 if (!check_php_version(5,3)) {
329 set_magic_quotes_runtime($mqr);
331 foreach($this->fonts as $k=>$font) {
334 $this->fonts[$k]['n']=$this->n;
335 $this->_out('<</Type /Font');
336 if($font['type']=='Type0')
337 $this->_putType0($font);
340 $this->_out('/BaseFont /'.$name);
341 if($font['type']=='core') {
343 $this->_out('/Subtype /Type1');
344 if($name!='Symbol' and $name!='ZapfDingbats')
345 $this->_out('/Encoding /WinAnsiEncoding');
348 $this->_out('/Subtype /'.$font['type']);
349 $this->_out('/FirstChar 32');
350 $this->_out('/LastChar 255');
351 $this->_out('/Widths '.($this->n+1).' 0 R');
352 $this->_out('/FontDescriptor '.($this->n+2).' 0 R');
354 if(isset($font['diff']))
355 $this->_out('/Encoding '.($nf+$font['diff']).' 0 R');
357 $this->_out('/Encoding /WinAnsiEncoding');
361 $this->_out('endobj');
362 if($font['type']!='core') {
367 for($i=32;$i<=255;$i++)
368 $s.=$cw[chr($i)].' ';
370 $this->_out('endobj');
373 $s='<</Type /FontDescriptor /FontName /'.$name;
374 foreach($font['desc'] as $k=>$v)
378 $s.=' /FontFile'.($font['type']=='Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R';
379 $this->_out($s.'>>');
380 $this->_out('endobj');
386 function _putType0($font)
389 $this->_out('/Subtype /Type0');
390 $this->_out('/BaseFont /'.$font['name'].'-'.$font['CMap']);
391 $this->_out('/Encoding /'.$font['CMap']);
392 $this->_out('/DescendantFonts ['.($this->n+1).' 0 R]');
394 $this->_out('endobj');
397 $this->_out('<</Type /Font');
398 $this->_out('/Subtype /CIDFontType0');
399 $this->_out('/BaseFont /'.$font['name']);
400 $this->_out('/CIDSystemInfo <</Registry (Adobe) /Ordering ('.$font['registry']['ordering'].') /Supplement '.$font['registry']['supplement'].'>>');
401 $this->_out('/FontDescriptor '.($this->n+1).' 0 R');
403 foreach($font['cw'] as $w)
405 $this->_out($W.'] 231 325 500 631 [500] 326 389 500]');
407 $this->_out('endobj');
410 $this->_out('<</Type /FontDescriptor');
411 $this->_out('/FontName /'.$font['name']);
412 $this->_out('/Flags 6');
413 $this->_out('/FontBBox [0 -200 1000 900]');
414 $this->_out('/ItalicAngle 0');
415 $this->_out('/Ascent 800');
416 $this->_out('/Descent -200');
417 $this->_out('/CapHeight 800');
418 $this->_out('/StemV 60');
420 $this->_out('endobj');
428 // c-hanging-comment-ender-p: nil
429 // indent-tabs-mode: nil