source: branches/version-2/data/pdf/japanese.php @ 17232

Revision 17232, 12.1 KB checked in by satou, 16 years ago (diff)

納品書発行機能 by Yammy (merge r17182)

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