source: branches/version-2_12-dev/data/class/helper/SC_Helper_FPDI.php @ 22567

Revision 22567, 6.3 KB checked in by shutta, 11 years ago (diff)

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.12.4)
Zend Framework PHP 標準コーディング規約のコーディングスタイルへ準拠。
classおよびfunctionの開始波括弧「{」のスタイルを修正。

Line 
1<?php
2require DATA_REALDIR . 'module/fpdf/fpdf.php';
3require DATA_REALDIR . 'module/fpdi/japanese.php';
4
5// japanese.php のバグ回避
6$GLOBALS[SJIS_widths] = $SJIS_widths;
7
8class SC_Helper_FPDI extends PDF_Japanese
9{
10    /**
11     * PDF_Japanese の明朝フォントに加えゴシックフォントを追加定義
12     *
13     * @return void
14     */
15    function AddSJISFont()
16    {
17        parent::AddSJISFont();
18        $cw = $GLOBALS['SJIS_widths'];
19        $c_map = '90msp-RKSJ-H';
20        $registry = array('ordering'=>'Japan1','supplement'=>2);
21        $this->AddCIDFonts('Gothic', 'KozGoPro-Medium-Acro,MS-PGothic,Osaka', $cw, $c_map, $registry);
22    }
23
24    /**
25     * FancyTable から利用するための SJISMultiCell
26     *
27     * PDF_Japanese#SJISMultiCell をベースにカスタマイズ。
28     */
29    function SJISMultiCellForFancyTable($w, $h, $txt, $border = 0, $align = 'L', $fill = 0)
30    {
31        $y = $this->y;
32
33        // ここで SJIS に変換する。そのため、このメソッドの中では、PDF_Japanese#Cell を直接呼ぶ。
34        $txt = $this->lfConvSjis($txt);
35        // Output text with automatic or explicit line breaks
36        $cw =& $this->CurrentFont['cw'];
37        if ($w == 0) {
38            $w = $this->w - $this->rMargin - $this->x;
39        }
40        $wmax = ($w - 2 * $this->cMargin) * 1000 / $this->FontSize;
41        $s = str_replace("\r", '', $txt);
42        $nb = strlen($s);
43        if ($nb > 0 && $s[$nb - 1] == "\n") {
44            $nb--;
45        }
46        $b = 0;
47        if ($border) {
48            if ($border == 1) {
49                $border = 'LTRB';
50                $b = 'LRT';
51                $b2 = 'LR';
52            }
53            else {
54                $b2 = '';
55                if (is_int(strpos($border, 'L')))
56                    $b2 .= 'L';
57                if (is_int(strpos($border, 'R')))
58                    $b2 .= 'R';
59                $b = is_int(strpos($border, 'T')) ? $b2.'T' : $b2;
60            }
61        }
62        $sep =- 1;
63        $i = 0;
64        $j = 0;
65        $l = 0;
66        $nl = 1;
67        $rise_h = $h; // 高さ計算用
68
69        while ($i < $nb) {
70            // Get next character
71            $c = $s{$i};
72            $o = ord($c);
73            if ($o == 10) {
74                // Explicit line break
75                parent::Cell($w, $h, substr($s, $j, $i - $j), $b, 2, $align, $fill);
76                $i++;
77                $sep = -1;
78                $j = $i;
79                $l = 0;
80                $nl++;
81                $rise_h += $h; // 高さ計算用
82                if ($border && $nl == 2) {
83                    $b=$b2;
84                }
85                continue;
86            }
87            if ($o < 128) {
88                // ASCII
89                $l += $cw[$c];
90                $n = 1;
91                if ($o == 32) {
92                    $sep=$i;
93                }
94            } elseif ($o >= 161 && $o <= 223) {
95                // Half-width katakana
96                $l += 500;
97                $n = 1;
98                $sep = $i;
99            }
100            else {
101                // Full-width character
102                $l += 1000;
103                $n = 2;
104                $sep = $i;
105            }
106            if ($l > $wmax) {
107                // Automatic line break
108                if ($sep == -1 || $i == $j) {
109                    if ($i == $j) {
110                        $i += $n;
111                    }
112                    parent::Cell($w, $h, substr($s, $j, $i - $j), $b, 2, $align, $fill);
113                }
114                else {
115                    parent::Cell($w, $h, substr($s, $j, $sep - $j), $b, 2, $align, $fill);
116                    $i = ($s[$sep] == ' ') ? $sep + 1 : $sep;
117                }
118                $rise_h += $h; // 高さ計算用
119                $sep = -1;
120                $j = $i;
121                $l = 0;
122                $nl++;
123                if ($border && $nl == 2) {
124                    $b = $b2;
125                }
126            }
127            else {
128                $i += $n;
129                if ($o >= 128) {
130                    $sep = $i;
131                }
132            }
133        }
134        // Last chunk
135        if ($border && is_int(strpos($border, 'B'))) {
136            $b .= 'B';
137        }
138        parent::Cell($w, $h, substr($s, $j, $i - $j), $b, 0, $align, $fill);
139        // メソッド内でY軸を増す操作を行う場合があるので戻す。
140        $this->y = $y;
141
142        return $rise_h;
143    }
144
145    /**
146     * Colored table
147     *
148     * FIXME: 後の列の高さが大きい場合、表示が乱れる。
149     */
150    function FancyTable($header, $data, $w)
151    {
152        // Colors, line width and bold font
153        $this->SetFillColor(216, 216, 216);
154        $this->SetTextColor(0);
155        $this->SetDrawColor(0, 0, 0);
156        $this->SetLineWidth(.3);
157        $this->SetFont('', 'B');
158        // Header
159        for ($i = 0; $i < count($header); $i++) {
160            $this->Cell($w[$i], 7, $header[$i], 1, 0, 'C', 1);
161        }
162        $this->Ln();
163        // Color and font restoration
164        $this->SetFillColor(235, 235, 235);
165        $this->SetTextColor(0);
166        $this->SetFont('');
167        // Data
168        $fill = 0;
169        $h = 4;
170        foreach ($data as $row) {
171            $h = 4;
172            $i = 0;
173            $this->Cell(5, $h, '', 0, 0, '', 0, '');
174            foreach ($row as $col) {
175                // FIXME 汎用的ではない処理。この指定は呼び出し元で行うようにしたい。
176                if ($i == 0) {
177                    $align = 'L';
178                } else {
179                    $align = 'R';
180                }
181                $h = $this->SJISMultiCellForFancyTable($w[$i], $h, $col, 1, $align, $fill, 0);
182                $i++;
183            }
184            $this->Ln();
185            $fill = !$fill;
186        }
187        $this->Cell(5, $h, '', 0, 0, '', 0, '');
188        $this->Cell(array_sum($w), 0, '', 'T');
189        $this->SetFillColor(255);
190    }
191
192    function Text($x, $y, $txt)
193    {
194        parent::Text($x, $y, $this->lfConvSjis($txt));
195    }
196
197    function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='')
198    {
199        parent::Cell($w, $h, $this->lfConvSjis($txt), $border, $ln, $align, $fill, $link);
200    }
201
202    // 文字コードSJIS変換 -> japanese.phpで使用出来る文字コードはSJIS-winのみ
203    function lfConvSjis($conv_str)
204    {
205        return mb_convert_encoding($conv_str, 'SJIS-win', CHAR_CODE);
206    }
207}
Note: See TracBrowser for help on using the repository browser.