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

Revision 21829, 6.3 KB checked in by Seasoft, 12 years ago (diff)

#1613 (typo修正・ソース整形・ソースコメントの改善)

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