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

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

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

  • 個人的には elseif の前で改行するのは良いと思うのですが、Zend Framework PHP 標準コーディング規約では想定していないようなので。ただし、elseif の前にコメントがある場合は除外。
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            } elseif ($o >= 161 && $o <= 223) {
92                // Half-width katakana
93                $l += 500;
94                $n = 1;
95                $sep = $i;
96            }
97            else {
98                // Full-width character
99                $l += 1000;
100                $n = 2;
101                $sep = $i;
102            }
103            if ($l > $wmax) {
104                // Automatic line break
105                if ($sep == -1 || $i == $j) {
106                    if ($i == $j) {
107                        $i += $n;
108                    }
109                    parent::Cell($w, $h, substr($s, $j, $i - $j), $b, 2, $align, $fill);
110                }
111                else {
112                    parent::Cell($w, $h, substr($s, $j, $sep - $j), $b, 2, $align, $fill);
113                    $i = ($s[$sep] == ' ') ? $sep + 1 : $sep;
114                }
115                $rise_h += $h; // 高さ計算用
116                $sep = -1;
117                $j = $i;
118                $l = 0;
119                $nl++;
120                if ($border && $nl == 2) {
121                    $b = $b2;
122                }
123            }
124            else {
125                $i += $n;
126                if ($o >= 128) {
127                    $sep = $i;
128                }
129            }
130        }
131        // Last chunk
132        if ($border && is_int(strpos($border, 'B'))) {
133            $b .= 'B';
134        }
135        parent::Cell($w, $h, substr($s, $j, $i - $j), $b, 0, $align, $fill);
136        // メソッド内でY軸を増す操作を行う場合があるので戻す。
137        $this->y = $y;
138
139        return $rise_h;
140    }
141
142    /**
143     * Colored table
144     *
145     * FIXME: 後の列の高さが大きい場合、表示が乱れる。
146     */
147    function FancyTable($header, $data, $w) {
148        // Colors, line width and bold font
149        $this->SetFillColor(216, 216, 216);
150        $this->SetTextColor(0);
151        $this->SetDrawColor(0, 0, 0);
152        $this->SetLineWidth(.3);
153        $this->SetFont('', 'B');
154        // Header
155        for ($i = 0; $i < count($header); $i++) {
156            $this->Cell($w[$i], 7, $header[$i], 1, 0, 'C', 1);
157        }
158        $this->Ln();
159        // Color and font restoration
160        $this->SetFillColor(235, 235, 235);
161        $this->SetTextColor(0);
162        $this->SetFont('');
163        // Data
164        $fill = 0;
165        $h = 4;
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.