source: branches/version-2_5-dev/data/module/fpdf/japanese.php @ 19716

Revision 19716, 15.3 KB checked in by Seasoft, 13 years ago (diff)

#403(インクルードしているライブラリ群をバージョンアップする)

  • FPDF関連 (PHP4について、動作環境無く未確認)
Line 
1<?php
2require('fpdi.php');
3
4$SJIS_widths=array(' '=>278,'!'=>299,'"'=>353,'#'=>614,'$'=>614,'%'=>721,'&'=>735,'\''=>216,
5    '('=>323,')'=>323,'*'=>449,'+'=>529,','=>219,'-'=>306,'.'=>219,'/'=>453,'0'=>614,'1'=>614,
6    '2'=>614,'3'=>614,'4'=>614,'5'=>614,'6'=>614,'7'=>614,'8'=>614,'9'=>614,':'=>219,';'=>219,
7    '<'=>529,'='=>529,'>'=>529,'?'=>486,'@'=>744,'A'=>646,'B'=>604,'C'=>617,'D'=>681,'E'=>567,
8    'F'=>537,'G'=>647,'H'=>738,'I'=>320,'J'=>433,'K'=>637,'L'=>566,'M'=>904,'N'=>710,'O'=>716,
9    'P'=>605,'Q'=>716,'R'=>623,'S'=>517,'T'=>601,'U'=>690,'V'=>668,'W'=>990,'X'=>681,'Y'=>634,
10    'Z'=>578,'['=>316,'\\'=>614,']'=>316,'^'=>529,'_'=>500,'`'=>387,'a'=>509,'b'=>566,'c'=>478,
11    'd'=>565,'e'=>503,'f'=>337,'g'=>549,'h'=>580,'i'=>275,'j'=>266,'k'=>544,'l'=>276,'m'=>854,
12    'n'=>579,'o'=>550,'p'=>578,'q'=>566,'r'=>410,'s'=>444,'t'=>340,'u'=>575,'v'=>512,'w'=>760,
13    'x'=>503,'y'=>529,'z'=>453,'{'=>326,'|'=>380,'}'=>326,'~'=>387);
14
15class PDF_Japanese extends FPDI
16{
17function AddCIDFont($family,$style,$name,$cw,$CMap,$registry)
18{
19    $fontkey=strtolower($family).strtoupper($style);
20    if(isset($this->fonts[$fontkey]))
21        $this->Error("CID font already added: $family $style");
22    $i=count($this->fonts)+1;
23    $this->fonts[$fontkey]=array('i'=>$i,'type'=>'Type0','name'=>$name,'up'=>-120,'ut'=>40,'cw'=>$cw,'CMap'=>$CMap,'registry'=>$registry);
24}
25
26function AddCIDFonts($family,$name,$cw,$CMap,$registry)
27{
28    $this->AddCIDFont($family,'',$name,$cw,$CMap,$registry);
29    $this->AddCIDFont($family,'B',$name.',Bold',$cw,$CMap,$registry);
30    $this->AddCIDFont($family,'I',$name.',Italic',$cw,$CMap,$registry);
31    $this->AddCIDFont($family,'BI',$name.',BoldItalic',$cw,$CMap,$registry);
32}
33
34function AddSJISFont($family='SJIS')
35{
36    //Add SJIS font with proportional Latin
37    $name='KozMinPro-Regular-Acro';
38    //$name='Gothic'; // •ÏX
39    $cw=$GLOBALS['SJIS_widths'];
40    $CMap='90msp-RKSJ-H';
41    $registry=array('ordering'=>'Japan1','supplement'=>2);
42    $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
43}
44
45function AddSJIShwFont($family='SJIS-hw')
46{
47    //Add SJIS font with half-width Latin
48    $name='KozMinPro-Regular-Acro';
49    for($i=32;$i<=126;$i++)
50        $cw[chr($i)]=500;
51    $CMap='90ms-RKSJ-H';
52    $registry=array('ordering'=>'Japan1','supplement'=>2);
53    $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
54}
55
56function GetStringWidth($s)
57{
58    if($this->CurrentFont['type']=='Type0')
59        return $this->GetSJISStringWidth($s);
60    else
61        return parent::GetStringWidth($s);
62}
63
64function GetSJISStringWidth($s)
65{
66    //SJIS version of GetStringWidth()
67    $l=0;
68    $cw=&$this->CurrentFont['cw'];
69    $nb=strlen($s);
70    $i=0;
71    while($i<$nb)
72    {
73        $o=ord($s{$i});
74        if($o<128)
75        {
76            //ASCII
77            $l+=$cw[$s{$i}];
78            $i++;
79        }
80        elseif($o>=161 and $o<=223)
81        {
82            //Half-width katakana
83            $l+=500;
84            $i++;
85        }
86        else
87        {
88            //Full-width character
89            $l+=1000;
90            $i+=2;
91        }
92    }
93    return $l*$this->FontSize/1000;
94}
95
96function MultiCell($w,$h,$txt,$border=0,$align='L',$fill=0,$ln=2)
97{
98    if($this->CurrentFont['type']=='Type0')
99        $this->SJISMultiCell($w,$h,$txt,$border,$align,$fill,$ln);
100    else
101        parent::MultiCell($w,$h,$txt,$border,$align,$fill,$ln);
102}
103
104function SJISMultiCell($w,$h,$txt,$border=0,$align='L',$fill=0,$ln=2)
105{
106    //Output text with automatic or explicit line breaks
107    $cw=&$this->CurrentFont['cw'];
108    if($w==0)
109        $w=$this->w-$this->rMargin-$this->x;
110    $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
111    $s=str_replace("\r",'',$txt);
112    $nb=strlen($s);
113    if($nb>0 and $s{$nb-1}=="\n")
114        $nb--;
115    $b=0;
116    if($border)
117    {
118        if($border==1)
119        {
120            $border='LTRB';
121            $b='LRT';
122            $b2='LR';
123        }
124        else
125        {
126            $b2='';
127            if(is_int(strpos($border,'L')))
128                $b2.='L';
129            if(is_int(strpos($border,'R')))
130                $b2.='R';
131            $b=is_int(strpos($border,'T')) ? $b2.'T' : $b2;
132        }
133    }
134    $sep=-1;
135    $i=0;
136    $j=0;
137    $l=0;
138    $nl=1;
139    $this->rise_h = 0; //‚‚³ŒvŽZ—p
140
141    while($i<$nb)
142    {
143        //Get next character
144        $c=$s{$i};
145        $o=ord($c);
146        if($o==10)
147        {
148            //Explicit line break
149            $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
150            $i++;
151            $sep=-1;
152            $j=$i;
153            $l=0;
154            $nl++;
155            $this->rise_h += $h; //‚‚³ŒvŽZ—p
156            if($border and $nl==2)
157                $b=$b2;
158            continue;
159        }
160        if($o<128)
161        {
162            //ASCII
163            $l+=$cw[$c];
164            $n=1;
165            if($o==32)
166                $sep=$i;
167        }
168        elseif($o>=161 and $o<=223)
169        {
170            //Half-width katakana
171            $l+=500;
172            $n=1;
173            $sep=$i;
174        }
175        else
176        {
177            //Full-width character
178            $l+=1000;
179            $n=2;
180            $sep=$i;
181        }
182        if($l>$wmax)
183        {
184            //Automatic line break
185            if($sep==-1 or $i==$j)
186            {
187                if($i==$j)
188                    $i+=$n;
189                $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
190            }
191            else
192            {
193                $this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);
194                $i=($s[$sep]==' ') ? $sep+1 : $sep;
195            }
196            $this->rise_h += $h; //‚‚³ŒvŽZ—p
197            $sep=-1;
198            $j=$i;
199            $l=0;
200            $nl++;
201            if($border and $nl==2)
202                $b=$b2;
203        }
204        else
205        {
206            $i+=$n;
207            if($o>=128)
208                $sep=$i;
209        }
210    }
211    //Last chunk
212    if($border and is_int(strpos($border,'B')))
213        $b.='B';
214    $this->Cell($w,$h,substr($s,$j,$i-$j),$b,$ln,$align,$fill);
215    $this->rise_h += $h; //‘‰Á•ª‚̍‚‚³‚ðŒvŽZ
216    //‰üs‚È‚µÝ’è‚©‚A‚‚³‚ª‹K’è‚̍‚‚³ˆÈã‚Å‚ ‚ê‚ÎYŽ²‚ðÝ’肵‚È‚¨‚·B
217    if($ln == 0 and $h < $this->rise_h) {
218      $this->y = $this->y - $this->rise_h + $h;
219    }
220
221    //$this->x=$this->lMargin;
222}
223
224function Write($h,$txt,$link='')
225{
226    if($this->CurrentFont['type']=='Type0')
227        $this->SJISWrite($h,$txt,$link);
228    else
229        parent::Write($h,$txt,$link);
230}
231
232function SJISWrite($h,$txt,$link)
233{
234    //SJIS 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        $o=ord($c);
250        if($o==10)
251        {
252            //Explicit line break
253            $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
254            $i++;
255            $sep=-1;
256            $j=$i;
257            $l=0;
258            if($nl==1)
259            {
260                //Go to left margin
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($o<128)
269        {
270            //ASCII
271            $l+=$cw[$c];
272            $n=1;
273            if($o==32)
274                $sep=$i;
275        }
276        elseif($o>=161 and $o<=223)
277        {
278            //Half-width katakana
279            $l+=500;
280            $n=1;
281            $sep=$i;
282        }
283        else
284        {
285            //Full-width character
286            $l+=1000;
287            $n=2;
288            $sep=$i;
289        }
290        if($l>$wmax)
291        {
292            //Automatic line break
293            if($sep==-1 or $i==$j)
294            {
295                if($this->x>$this->lMargin)
296                {
297                    //Move to next line
298                    $this->x=$this->lMargin;
299                    $this->y+=$h;
300                    $w=$this->w-$this->rMargin-$this->x;
301                    $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
302                    $i+=$n;
303                    $nl++;
304                    continue;
305                }
306                if($i==$j)
307                    $i+=$n;
308                $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
309            }
310            else
311            {
312                $this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',0,$link);
313                $i=($s[$sep]==' ') ? $sep+1 : $sep;
314            }
315            $sep=-1;
316            $j=$i;
317            $l=0;
318            if($nl==1)
319            {
320                $this->x=$this->lMargin;
321                $w=$this->w-$this->rMargin-$this->x;
322                $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
323            }
324            $nl++;
325        }
326        else
327        {
328            $i+=$n;
329            if($o>=128)
330                $sep=$i;
331        }
332    }
333    //Last chunk
334    if($i!=$j)
335        $this->Cell($l/1000*$this->FontSize,$h,substr($s,$j,$i-$j),0,0,'',0,$link);
336}
337
338function _putfonts()
339{
340    $nf=$this->n;
341    foreach($this->diffs as $diff)
342    {
343        //Encodings
344        $this->_newobj();
345        $this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>');
346        $this->_out('endobj');
347    }
348    $mqr=get_magic_quotes_runtime();
349    set_magic_quotes_runtime(0);
350    foreach($this->FontFiles as $file=>$info)
351    {
352        //Font file embedding
353        $this->_newobj();
354        $this->FontFiles[$file]['n']=$this->n;
355        if(defined('FPDF_FONTPATH'))
356            $file=FPDF_FONTPATH.$file;
357        $size=filesize($file);
358        if(!$size)
359            $this->Error('Font file not found');
360        $this->_out('<</Length '.$size);
361        if(substr($file,-2)=='.z')
362            $this->_out('/Filter /FlateDecode');
363        $this->_out('/Length1 '.$info['length1']);
364        if(isset($info['length2']))
365            $this->_out('/Length2 '.$info['length2'].' /Length3 0');
366        $this->_out('>>');
367        $f=fopen($file,'rb');
368        $this->_putstream(fread($f,$size));
369        fclose($f);
370        $this->_out('endobj');
371    }
372    set_magic_quotes_runtime($mqr);
373    foreach($this->fonts as $k=>$font)
374    {
375        //Font objects
376        $this->_newobj();
377        $this->fonts[$k]['n']=$this->n;
378        $this->_out('<</Type /Font');
379        if($font['type']=='Type0')
380            $this->_putType0($font);
381        else
382        {
383            $name=$font['name'];
384            $this->_out('/BaseFont /'.$name);
385            if($font['type']=='core')
386            {
387                //Standard font
388                $this->_out('/Subtype /Type1');
389                if($name!='Symbol' and $name!='ZapfDingbats')
390                    $this->_out('/Encoding /WinAnsiEncoding');
391            }
392            else
393            {
394                //Additional font
395                $this->_out('/Subtype /'.$font['type']);
396                $this->_out('/FirstChar 32');
397                $this->_out('/LastChar 255');
398                $this->_out('/Widths '.($this->n+1).' 0 R');
399                $this->_out('/FontDescriptor '.($this->n+2).' 0 R');
400                if($font['enc'])
401                {
402                    if(isset($font['diff']))
403                        $this->_out('/Encoding '.($nf+$font['diff']).' 0 R');
404                    else
405                        $this->_out('/Encoding /WinAnsiEncoding');
406                }
407            }
408            $this->_out('>>');
409            $this->_out('endobj');
410            if($font['type']!='core')
411            {
412                //Widths
413                $this->_newobj();
414                $cw=&$font['cw'];
415                $s='[';
416                for($i=32;$i<=255;$i++)
417                    $s.=$cw[chr($i)].' ';
418                $this->_out($s.']');
419                $this->_out('endobj');
420                //Descriptor
421                $this->_newobj();
422                $s='<</Type /FontDescriptor /FontName /'.$name;
423                foreach($font['desc'] as $k=>$v)
424                    $s.=' /'.$k.' '.$v;
425                $file=$font['file'];
426                if($file)
427                    $s.=' /FontFile'.($font['type']=='Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R';
428                $this->_out($s.'>>');
429                $this->_out('endobj');
430            }
431        }
432    }
433}
434
435function _putType0($font)
436{
437    //Type0
438    $this->_out('/Subtype /Type0');
439    $this->_out('/BaseFont /'.$font['name'].'-'.$font['CMap']);
440    $this->_out('/Encoding /'.$font['CMap']);
441    $this->_out('/DescendantFonts ['.($this->n+1).' 0 R]');
442    $this->_out('>>');
443    $this->_out('endobj');
444    //CIDFont
445    $this->_newobj();
446    $this->_out('<</Type /Font');
447    $this->_out('/Subtype /CIDFontType0');
448    $this->_out('/BaseFont /'.$font['name']);
449    $this->_out('/CIDSystemInfo <</Registry (Adobe) /Ordering ('.$font['registry']['ordering'].') /Supplement '.$font['registry']['supplement'].'>>');
450    $this->_out('/FontDescriptor '.($this->n+1).' 0 R');
451    $W='/W [1 [';
452    foreach($font['cw'] as $w)
453        $W.=$w.' ';
454    $this->_out($W.'] 231 325 500 631 [500] 326 389 500]');
455    $this->_out('>>');
456    $this->_out('endobj');
457    //Font descriptor
458    $this->_newobj();
459    $this->_out('<</Type /FontDescriptor');
460    $this->_out('/FontName /'.$font['name']);
461    $this->_out('/Flags 6');
462    $this->_out('/FontBBox [0 -200 1000 900]');
463    $this->_out('/ItalicAngle 0');
464    $this->_out('/Ascent 800');
465    $this->_out('/Descent -200');
466    $this->_out('/CapHeight 800');
467    $this->_out('/StemV 60');
468    $this->_out('>>');
469    $this->_out('endobj');
470}
471
472//Load data
473function LoadData($file)
474{
475    //Read file lines
476    $lines=file($file);
477    $data=array();
478    foreach($lines as $line)
479        $data[]=explode(';',chop($line));
480    return $data;
481}
482
483//Colored table
484function FancyTable($header,$data,$w)
485{
486    //Colors, line width and bold font
487    $this->SetFillColor(216,216,216);
488    $this->SetTextColor(0);
489    $this->SetDrawColor(0,0,0);
490    $this->SetLineWidth(.3);
491    $this->SetFont('','B');
492    //Header
493    for($i=0;$i<count($header);$i++)
494        $this->Cell($w[$i],7,$header[$i],1,0,'C',1);
495    $this->Ln();
496    //Color and font restoration
497    $this->SetFillColor(235,235,235);
498    $this->SetTextColor(0);
499    $this->SetFont('');
500    //Data
501    $fill=0;
502    foreach($data as $row)
503    {
504    $h = 4;
505    $i = 0;
506    $this->Cell(5, $h, '', 0, 0, '', 0, '');
507    foreach($row as $col) {
508        if($i > 3) { $i = 0; }
509        if ($i != 0) {
510            //$this->MultiCell($w[$i],$h,number_format($col),1,'R',$fill, 0);
511            $this->MultiCell($w[$i],$h,$col,1,'R',$fill, 0);
512        } else {
513            $this->MultiCell($w[$i],$h,$col,1,'L',$fill, 0);
514        }
515        $h = $this->rise_h;
516        $i++;
517    }
518    $this->Ln();
519        $fill=!$fill;
520
521    }
522    $this->Cell(5, $h, '', 0, 0, '', 0, '');
523    $this->Cell(array_sum($w),0,'','T');
524}
525
526function Footer()
527{
528    //‰º’[‚©‚ç1.5 cm ‚Ɉړ®
529    $this->SetY(-15);
530    //ƒtƒHƒ“ƒg‚ðÝ’èB Arial italic 8
531    $this->SetFont('Arial','I',8);
532    //Œ»Ý‚̃y[ƒW”ԍ†‚Æ‘ƒy[ƒW”‚ðo—Í
533    #$this->Cell(0,10,''.$this->PageNo().' / {nb}',0,0,'C');
534}
535
536}
537?>
Note: See TracBrowser for help on using the repository browser.