source: branches/version-2_12-dev/data/module/fpdi/japanese.php @ 21826

Revision 21826, 8.2 KB checked in by Seasoft, 12 years ago (diff)

#1794 (data/module/fpdi/japanese.php のカスタマイズを分離する)
#1802 (PDF 納品書の備考欄に背景色が塗られる場合がある)

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    $cw=$GLOBALS['SJIS_widths'];
39    $CMap='90msp-RKSJ-H';
40    $registry=array('ordering'=>'Japan1','supplement'=>2);
41    $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
42}
43
44function AddSJIShwFont($family='SJIS-hw')
45{
46    // Add SJIS font with half-width Latin
47    $name='KozMinPro-Regular-Acro';
48    for($i=32;$i<=126;$i++)
49        $cw[chr($i)]=500;
50    $CMap='90ms-RKSJ-H';
51    $registry=array('ordering'=>'Japan1','supplement'=>2);
52    $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
53}
54
55function GetStringWidth($s)
56{
57    if($this->CurrentFont['type']=='Type0')
58        return $this->GetSJISStringWidth($s);
59    else
60        return parent::GetStringWidth($s);
61}
62
63function GetSJISStringWidth($s)
64{
65    // SJIS version of GetStringWidth()
66    $l=0;
67    $cw=&$this->CurrentFont['cw'];
68    $nb=strlen($s);
69    $i=0;
70    while($i<$nb)
71    {
72        $o=ord($s[$i]);
73        if($o<128)
74        {
75            // ASCII
76            $l+=$cw[$s[$i]];
77            $i++;
78        }
79        elseif($o>=161 && $o<=223)
80        {
81            // Half-width katakana
82            $l+=500;
83            $i++;
84        }
85        else
86        {
87            // Full-width character
88            $l+=1000;
89            $i+=2;
90        }
91    }
92    return $l*$this->FontSize/1000;
93}
94
95function MultiCell($w, $h, $txt, $border=0, $align='L', $fill=false)
96{
97    if($this->CurrentFont['type']=='Type0')
98        $this->SJISMultiCell($w,$h,$txt,$border,$align,$fill);
99    else
100        parent::MultiCell($w,$h,$txt,$border,$align,$fill);
101}
102
103function SJISMultiCell($w, $h, $txt, $border=0, $align='L', $fill=false)
104{
105    // Output text with automatic or explicit line breaks
106    $cw=&$this->CurrentFont['cw'];
107    if($w==0)
108        $w=$this->w-$this->rMargin-$this->x;
109    $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
110    $s=str_replace("\r",'',$txt);
111    $nb=strlen($s);
112    if($nb>0 && $s[$nb-1]=="\n")
113        $nb--;
114    $b=0;
115    if($border)
116    {
117        if($border==1)
118        {
119            $border='LTRB';
120            $b='LRT';
121            $b2='LR';
122        }
123        else
124        {
125            $b2='';
126            if(is_int(strpos($border,'L')))
127                $b2.='L';
128            if(is_int(strpos($border,'R')))
129                $b2.='R';
130            $b=is_int(strpos($border,'T')) ? $b2.'T' : $b2;
131        }
132    }
133    $sep=-1;
134    $i=0;
135    $j=0;
136    $l=0;
137    $nl=1;
138    while($i<$nb)
139    {
140        // Get next character
141        $c=$s[$i];
142        $o=ord($c);
143        if($o==10)
144        {
145            // Explicit line break
146            $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
147            $i++;
148            $sep=-1;
149            $j=$i;
150            $l=0;
151            $nl++;
152            if($border && $nl==2)
153                $b=$b2;
154            continue;
155        }
156        if($o<128)
157        {
158            // ASCII
159            $l+=$cw[$c];
160            $n=1;
161            if($o==32)
162                $sep=$i;
163        }
164        elseif($o>=161 && $o<=223)
165        {
166            // Half-width katakana
167            $l+=500;
168            $n=1;
169            $sep=$i;
170        }
171        else
172        {
173            // Full-width character
174            $l+=1000;
175            $n=2;
176            $sep=$i;
177        }
178        if($l>$wmax)
179        {
180            // Automatic line break
181            if($sep==-1 || $i==$j)
182            {
183                if($i==$j)
184                    $i+=$n;
185                $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
186            }
187            else
188            {
189                $this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);
190                $i=($s[$sep]==' ') ? $sep+1 : $sep;
191            }
192            $sep=-1;
193            $j=$i;
194            $l=0;
195            $nl++;
196            if($border && $nl==2)
197                $b=$b2;
198        }
199        else
200        {
201            $i+=$n;
202            if($o>=128)
203                $sep=$i;
204        }
205    }
206    // Last chunk
207    if($border && is_int(strpos($border,'B')))
208        $b.='B';
209    $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
210    $this->x=$this->lMargin;
211}
212
213function Write($h, $txt, $link='')
214{
215    if($this->CurrentFont['type']=='Type0')
216        $this->SJISWrite($h,$txt,$link);
217    else
218        parent::Write($h,$txt,$link);
219}
220
221function SJISWrite($h, $txt, $link)
222{
223    // SJIS version of Write()
224    $cw=&$this->CurrentFont['cw'];
225    $w=$this->w-$this->rMargin-$this->x;
226    $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
227    $s=str_replace("\r",'',$txt);
228    $nb=strlen($s);
229    $sep=-1;
230    $i=0;
231    $j=0;
232    $l=0;
233    $nl=1;
234    while($i<$nb)
235    {
236        // Get next character
237        $c=$s[$i];
238        $o=ord($c);
239        if($o==10)
240        {
241            // Explicit line break
242            $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
243            $i++;
244            $sep=-1;
245            $j=$i;
246            $l=0;
247            if($nl==1)
248            {
249                // Go to left margin
250                $this->x=$this->lMargin;
251                $w=$this->w-$this->rMargin-$this->x;
252                $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
253            }
254            $nl++;
255            continue;
256        }
257        if($o<128)
258        {
259            // ASCII
260            $l+=$cw[$c];
261            $n=1;
262            if($o==32)
263                $sep=$i;
264        }
265        elseif($o>=161 && $o<=223)
266        {
267            // Half-width katakana
268            $l+=500;
269            $n=1;
270            $sep=$i;
271        }
272        else
273        {
274            // Full-width character
275            $l+=1000;
276            $n=2;
277            $sep=$i;
278        }
279        if($l>$wmax)
280        {
281            // Automatic line break
282            if($sep==-1 || $i==$j)
283            {
284                if($this->x>$this->lMargin)
285                {
286                    // Move to next line
287                    $this->x=$this->lMargin;
288                    $this->y+=$h;
289                    $w=$this->w-$this->rMargin-$this->x;
290                    $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
291                    $i+=$n;
292                    $nl++;
293                    continue;
294                }
295                if($i==$j)
296                    $i+=$n;
297                $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
298            }
299            else
300            {
301                $this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',0,$link);
302                $i=($s[$sep]==' ') ? $sep+1 : $sep;
303            }
304            $sep=-1;
305            $j=$i;
306            $l=0;
307            if($nl==1)
308            {
309                $this->x=$this->lMargin;
310                $w=$this->w-$this->rMargin-$this->x;
311                $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
312            }
313            $nl++;
314        }
315        else
316        {
317            $i+=$n;
318            if($o>=128)
319                $sep=$i;
320        }
321    }
322    // Last chunk
323    if($i!=$j)
324        $this->Cell($l/1000*$this->FontSize,$h,substr($s,$j,$i-$j),0,0,'',0,$link);
325}
326
327function _putType0($font)
328{
329    // Type0
330    $this->_newobj();
331    $this->_out('<</Type /Font');
332    $this->_out('/Subtype /Type0');
333    $this->_out('/BaseFont /'.$font['name'].'-'.$font['CMap']);
334    $this->_out('/Encoding /'.$font['CMap']);
335    $this->_out('/DescendantFonts ['.($this->n+1).' 0 R]');
336    $this->_out('>>');
337    $this->_out('endobj');
338    // CIDFont
339    $this->_newobj();
340    $this->_out('<</Type /Font');
341    $this->_out('/Subtype /CIDFontType0');
342    $this->_out('/BaseFont /'.$font['name']);
343    $this->_out('/CIDSystemInfo <</Registry (Adobe) /Ordering ('.$font['registry']['ordering'].') /Supplement '.$font['registry']['supplement'].'>>');
344    $this->_out('/FontDescriptor '.($this->n+1).' 0 R');
345    $W='/W [1 [';
346    foreach($font['cw'] as $w)
347        $W.=$w.' ';
348    $this->_out($W.'] 231 325 500 631 [500] 326 389 500]');
349    $this->_out('>>');
350    $this->_out('endobj');
351    // Font descriptor
352    $this->_newobj();
353    $this->_out('<</Type /FontDescriptor');
354    $this->_out('/FontName /'.$font['name']);
355    $this->_out('/Flags 6');
356    $this->_out('/FontBBox [0 -200 1000 900]');
357    $this->_out('/ItalicAngle 0');
358    $this->_out('/Ascent 800');
359    $this->_out('/Descent -200');
360    $this->_out('/CapHeight 800');
361    $this->_out('/StemV 60');
362    $this->_out('>>');
363    $this->_out('endobj');
364}
365}
366?>
Note: See TracBrowser for help on using the repository browser.