Completed
Pull Request — master (#8)
by satoru
04:05
created

TcpdfWrapper::setValTategaki()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 8.9381
c 0
b 0
f 0
cc 4
nc 4
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace TcpdfWrapper;
4
5
use setasign\Fpdi\Tcpdf\Fpdi;
6
use \TCPDF_FONTS;
7
/**
8
* TcpdfWrapper
9
* TcpdfWrapperを記載しやすくするためのラッパー
10
*/
11
class TcpdfWrapper
12
{
13
    private $__pdf;
14
    private $__fonts = [];
15
    private $__tcpdfFonts;
16
17
    public const TATEGAKI_TYPE_NORMAL = 1;
18
    public const TATEGAKI_TYPE_ROUND = 2;
19
    public const TATEGAKI_TYPE_RIGHT = 3;
20
21
    // publicにしておくので必要に応じて設定
22
    public $setTategakiType = [
23
        self::TATEGAKI_TYPE_ROUND => [
24
            'ー',
25
            '-',
26
            '=',
27
            '=',
28
            '(',
29
            ')',
30
            '(',
31
            ')',
32
            '>',
33
            '<',
34
            '>',
35
            '<',
36
            '》',
37
            '《',
38
            '≫',
39
            '≪',
40
            '{',
41
            '{',
42
            '}',
43
            '}',
44
            '[',
45
            ']',
46
            '[',
47
            ']',
48
            '「',
49
            '」',
50
            '~',
51
            '~',
52
            '|',
53
            '|',
54
            '『',
55
            '』',
56
            '【',
57
            '】',
58
            '〔',
59
            '〕',
60
            '‹',
61
            '›',
62
            '〖',
63
            '〗',
64
            '〚',
65
            '〛',
66
            '〘',
67
            '〙',
68
        ],
69
        self::TATEGAKI_TYPE_RIGHT => [
70
            'ぁ',
71
            'ぃ',
72
            'ぅ',
73
            'ぇ',
74
            'ぉ',
75
            'ゃ',
76
            'ゅ',
77
            'ょ',
78
            'っ',
79
            'ァ',
80
            'ィ',
81
            'ぅ',
82
            'ェ',
83
            'ォ',
84
            'ャ',
85
            'ュ',
86
            'ョ',
87
            'ッ',
88
            'ァ',
89
            'ィ',
90
            'ゥ',
91
            'ェ',
92
            'ォ',
93
            'ャ',
94
            'ュ',
95
            'ョ',
96
            'ッ',
97
            '、',
98
            '。',
99
            '.',
100
            ',',
101
        ],
102
    ];
103
104
    /**
105
    * __construct
106
    *
107
    * @author hagiwara
108
    */
109
    public function __construct()
110
    {
111
        $this->__pdf = new Fpdi();
112
        $this->__tcpdfFonts = new TCPDF_FONTS();
113
    }
114
115
    /**
116
    * setPrintHeader
117
    *
118
    * @param boolean $print 出力フラグ
119
    * @author hagiwara
120
    */
121
    public function setPrintHeader($print)
122
    {
123
        $this->__pdf->setPrintHeader($print);
124
    }
125
126
    /**
127
    * setPrintFooter
128
    *
129
    * @param boolean $print 出力フラグ
130
    * @author hagiwara
131
    */
132
    public function setPrintFooter($print)
133
    {
134
        $this->__pdf->setPrintFooter($print);
135
    }
136
137
    /**
138
    * setFont
139
    *
140
    * @param string $name フォント名
141
    * @param string $path フォントパス nullでデフォルトセット
142
    * @author hagiwara
143
    */
144
    public function setFont($name, $path)
145
    {
146
        $this->__fonts[$name] = $this->__tcpdfFonts->addTTFfont($path);
147
    }
148
149
    /**
150
    * addPage
151
    *
152
    * @param string $template テンプレートパス
153
    * @param integer $templateIndex テンプレートページ
154
    * @author hagiwara
155
    */
156
    public function addPage($template, $templateIndex)
157
    {
158
        // ページを追加
159
        $this->__pdf->AddPage();
160
161
        // テンプレートを読み込み
162
        $this->__pdf->setSourceFile($template);
163
164
        // 読み込んだPDFの1ページ目のインデックスを取得
165
        $tplIdx = $this->__pdf->importPage($templateIndex);
166
167
        // 読み込んだPDFの1ページ目をテンプレートとして使用
168
        $this->__pdf->useTemplate($tplIdx, null, null, null, null, true);
169
    }
170
171
    /**
172
    * setVal
173
    *
174
    * @param string $text テキスト
175
    * @param array $option オプション
176
    * @author hagiwara
177
    */
178
    public function setVal($text, $option)
179
    {
180
        $default_option = [
181
            'w' => 0,
182
            'h' => 0,
183
            'border' => 0,
184
            'align' => '',
185
            'fill' => false,
186
            'link' => '',
187
            'x' => 0,
188
            'y' => 0,
189
            'color' => '000000',
190
            'font' => '',
191
            'size' => 11,
192
            'stretch' => 0,
193
            'auto_size' => false,
194
        ];
195
        $option = array_merge($default_option ,$option);
196
        
197
        // 自動で枠に収めるかどうかのチェック
198
        if ($option['auto_size'] == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
199
            $fontDefaultWidth = $this->getStringWidth($text, $option['font'], '', $option['size']);
200
            if ($fontDefaultWidth > $option['w']) {
201
                $option['align'] ='J';
202
                $option['stretch'] =1;
203
            }
204
        }
205
        
206
        // 書き込む文字列のフォントを指定
207
        $this->__pdf->SetFont($this->getFont($option['font']), '', $option['size']);
208
        // 書き込む文字列の文字色を指定
209
        $concertColor = $this->colorCodeConvert($option['color']);
210
        $this->__pdf->SetTextColor($concertColor['r'], $concertColor['g'], $concertColor['b']);
211
212
        $this->__pdf->SetXY($option['x'], $option['y']);
213
        // 文字列を書き込む
214
        $this->__pdf->Cell($option['w'], $option['h'], $text, $option['border'], 0, $option['align'], $option['fill'], $option['link'], $option['stretch']);
215
    }
216
217
    /**
218
    * setValTategaki
219
    * 縦書き対応/改行は対応しきれない。折り返しもしない
220
    *
221
    * @param string $text テキスト
222
    * @param array $option オプション
223
    * @author hagiwara
224
    */
225
    public function setValTategaki($text, $option)
226
    {
227
        $default_option = [
228
            'h' => 0,
229
            'border' => 0,
230
            'fill' => false,
231
            'link' => '',
232
            'x' => 0,
233
            'y' => 0,
234
            'color' => '000000',
235
            'font' => '',
236
            'size' => 11,
237
        ];
238
        $option = array_merge($default_option ,$option);
239
240
        // 設定している固定の高さとする
241
        $wordHeight = $option['h'];
242
        // 文字の幅は対応する文字の一番幅の大きい文字とする
243
        $wordWidth = max($this->getStringWidth($text, $option['font'], '', $option['size'], true));
244
        $splitWord = preg_split("//u", $text, -1, PREG_SPLIT_NO_EMPTY);
245
        $top = $option['y'];
246
        foreach ($splitWord as $word) {
247
            // 一文字ことにオプションを設定
248
            $partsOption = $option;
249
            $partsOption['w'] = $wordWidth;
250
            $partsOption['h'] = $wordHeight;
251
            $partsOption['auto_size'] = false;
252
            $partsOption['align'] = 'C';
253
            $partsOption['stretch'] = '0';
254
            $partsOption['y'] = $top;
255
256
            // 縦書き対応
257
            $rotateOption = [];
258
            switch ($this->getTategakiWordType($word)) {
259
                // 回転が必要な文字
260
                case self::TATEGAKI_TYPE_ROUND:
261
                    $rotateOption = [
262
                        'angle' => -90,
263
                        'x' => $partsOption['x'] + ($partsOption['w'] * 0.5),
264
                        'y' => $partsOption['y'] + ($partsOption['h'] * 0.5),
265
                    ];
266
                    break;
267
                // 小さいゃゅょ、句読点を少し右寄せする
268
                case self::TATEGAKI_TYPE_RIGHT:
269
                    $partsOption['x'] += $partsOption['size'] * 0.05;
270
                    break;
271
272
                default:
273
                    break;
274
            }
275
276
            $this->setVal($word, $partsOption, $rotateOption);
0 ignored issues
show
Unused Code introduced by
The call to TcpdfWrapper::setVal() has too many arguments starting with $rotateOption.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
277
278
            // 固定の高さ分文字幅を取る
279
            $top += $wordHeight;
280
        }
281
    }
282
283
    /**
284
    * getTategakiWordType
285
    * 縦書きに必要な種別の取得
286
    *
287
    * @param string $word テキスト
288
    * @return int
289
    * @author hagiwara
290
    */
291
    private function getTategakiWordType($word)
292
    {
293
        if (in_array($word, $this->setTategakiType[self::TATEGAKI_TYPE_ROUND], true)) {
294
            return self::TATEGAKI_TYPE_ROUND;
295
        } elseif (in_array($word, $this->setTategakiType[self::TATEGAKI_TYPE_RIGHT], true)) {
296
            return self::TATEGAKI_TYPE_RIGHT;
297
        } else {
298
            return self::TATEGAKI_TYPE_NORMAL;
299
        }
300
    }
301
302
    /**
303
    * setHtml
304
    *
305
    * @param string $html HTML
306
    * @param array $option オプション
307
    * @author hagiwara
308
    */
309
    public function setHtml($html, $option)
310
    {
311
        $default_option = [
312
            'w' => 0,
313
            'h' => 0,
314
            'border' => 0,
315
            'align' => '',
316
            'fill' => false,
317
            'link' => '',
318
            'x' => 0,
319
            'y' => 0,
320
            'color' => '000000',
321
            'font' => '',
322
            'size' => '',
323
            'reseth' => true,
324
            'autopadding' => false,
325
        ];
326
        $option = array_merge($default_option ,$option);
327
        // 書き込む文字列の文字色を指定
328
        //$concertColor = $this->colorCodeConvert($option['color']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
329
        //var_dump($concertColor);
330
        //$this->__pdf->SetTextColor($concertColor['r'], $concertColor['g'], $concertColor['b']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
331
332
        // 書き込む文字列のフォントを指定
333
        $this->__pdf->SetFont($this->getFont($option['font']), '', $option['size']);
334
        
335
        $this->__pdf->writeHTMLCell( $option['w'], $option['h'], $option['x'], $option['y'], $html, $option['border'], 0, $option['fill'], $option['reseth'], $option['align'], $option['autopadding']);
336
    }
337
338
    /**
339
    * getFont
340
    *
341
    * @param string $font フォント名
342
    * @author hagiwara
343
    */
344
    private function getFont($font)
345
    {
346
        if (array_key_exists($font, $this->__fonts)) {
347
            return $this->__fonts[$font];
348
        } else {
349
            return $font;
350
        }
351
    }
352
353
    /**
354
    * setImage
355
    *
356
    * @param string $image 画像パス
357
    * @param array $option オプション
358
    * @author hagiwara
359
    */
360
    public function setImage($image, $option)
361
    {
362
        $default_option = [
363
            'x' => 0,
364
            'y' => 0,
365
            'w' => 0,
366
            'h' => 0,
367
            'link' => '',
368
            'resize' => true,
369
            'dpi' => '300',
370
        ];
371
        $option = array_merge($default_option ,$option);
372
        $this->__pdf->Image($image, $option['x'], $option['y'], $option['w'], $option['h'], '', $option['link'], '', $option['resize'], $option['dpi']);
373
    }
374
375
376
    /**
377
    * colorCodeConvert
378
    *
379
    * @param string $color カラーコード(16進数)
380
    * @author hagiwara
381
    */
382
    private function colorCodeConvert($color)
383
    {
384
        if (
385
            preg_match('/^([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/', $color, $colorCheck)
386
        ) {
387
            return [
388
                'r' => hexdec($colorCheck[1]),
389
                'g' => hexdec($colorCheck[2]),
390
                'b' => hexdec($colorCheck[3]),
391
            ];
392
        } else {
393
            return [
394
                'r' => 0,
395
                'g' => 0,
396
                'b' => 0,
397
            ];
398
        }
399
    }
400
401
    /**
402
     * setAutoPageBreak
403
     * page brackeを自動で行うかどうか。画像を下部に埋め込む際には切っておいたほうが良さげ
404
     * @param int $auto
405
     * @param int $margin
406
     */
407
    public function setAutoPageBreak($auto, $margin = 0)
408
    {
409
        $this->__pdf->SetAutoPageBreak($auto, $margin);
410
    }
411
    
412
   /**
413
    * getStringWidth
414
    *
415
    * @param string $text テキスト
416
    * @param string $font フォント名
417
    * @param string $fontstyle フォントスタイル
418
    * @param integer $fontsize サイズ
419
    * @param bool $getarray 結果を1文字ずつ配列で返すか
420
    * @author hagiwara
421
    */
422
    public function getStringWidth($text, $font, $fontstyle, $fontsize, $getarray = false) {
423
        return $this->__pdf->GetStringWidth( $text, $font, $fontstyle, $fontsize, $getarray);
424
    }
425
426
    /**
427
    * write
428
    *
429
    * @param string $file 出力ファイル
430
    * @author hagiwara
431
    */
432
    public function write($file)
433
    {
434
        $pdf_info = $this->__pdf->Output(null, 'S');
435
436
        $fp = fopen($file, 'w');
437
        fwrite($fp ,$pdf_info);
438
        fclose($fp);
439
    }
440
441
}
442