GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

PDF_Japanese   D
last analyzed

Complexity

Total Complexity 82

Size/Duplication

Total Lines 413
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1
Metric Value
wmc 82
lcom 2
cbo 1
dl 0
loc 413
rs 4.8717

12 Methods

Rating   Name   Duplication   Size   Complexity  
F SJISMultiCell() 0 92 27
F _putfonts() 0 87 19
A AddCIDFont() 0 8 2
A AddCIDFonts() 0 7 1
A AddSJISFont() 0 9 1
A AddSJIShwFont() 0 10 2
A GetStringWidth() 0 7 2
B GetSJISStringWidth() 0 26 5
A MultiCell() 0 7 2
A Write() 0 7 2
D SJISWrite() 0 89 17
B _putType0() 0 36 2

How to fix   Complexity   

Complex Class

Complex classes like PDF_Japanese often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use PDF_Japanese, and based on these observations, apply Extract Interface, too.

1
<?php // -*-php-*-
2
rcs_id('$Id: japanese.php,v 1.1 2004/05/04 22:34:25 rurban Exp $');
3
4
// PDF functions taken from FPDF http://www.fpdf.org
5
6
require_once('lib/pdf.php');
7
8
class PDF_Japanese extends PDF {
9
    var $B;
10
    var $I;
11
    var $U;
12
    var $HREF;
13
    var $SJIS_widths = array(' '=>278,'!'=>299,'"'=>353,'#'=>614,'$'=>614,'%'=>721,'&'=>735,'\''=>216,
14
                             '('=>323,')'=>323,'*'=>449,'+'=>529,','=>219,'-'=>306,'.'=>219,'/'=>453,'0'=>614,'1'=>614,
15
                             '2'=>614,'3'=>614,'4'=>614,'5'=>614,'6'=>614,'7'=>614,'8'=>614,'9'=>614,':'=>219,';'=>219,
16
                             '<'=>529,'='=>529,'>'=>529,'?'=>486,'@'=>744,'A'=>646,'B'=>604,'C'=>617,'D'=>681,'E'=>567,
17
                             'F'=>537,'G'=>647,'H'=>738,'I'=>320,'J'=>433,'K'=>637,'L'=>566,'M'=>904,'N'=>710,'O'=>716,
18
                             'P'=>605,'Q'=>716,'R'=>623,'S'=>517,'T'=>601,'U'=>690,'V'=>668,'W'=>990,'X'=>681,'Y'=>634,
19
                             'Z'=>578,'['=>316,'\\'=>614,']'=>316,'^'=>529,'_'=>500,'`'=>387,'a'=>509,'b'=>566,'c'=>478,
20
                             'd'=>565,'e'=>503,'f'=>337,'g'=>549,'h'=>580,'i'=>275,'j'=>266,'k'=>544,'l'=>276,'m'=>854,
21
                             'n'=>579,'o'=>550,'p'=>578,'q'=>566,'r'=>410,'s'=>444,'t'=>340,'u'=>575,'v'=>512,'w'=>760,
22
                             'x'=>503,'y'=>529,'z'=>453,'{'=>326,'|'=>380,'}'=>326,'~'=>387);
23
24
    function AddCIDFont($family,$style,$name,$cw,$CMap,$registry)
25
    {
26
        $fontkey=strtolower($family).strtoupper($style);
27
        if(isset($this->fonts[$fontkey]))
28
            $this->Error("CID font already added: $family $style");
29
        $i=count($this->fonts)+1;
30
        $this->fonts[$fontkey]=array('i'=>$i,'type'=>'Type0','name'=>$name,'up'=>-120,'ut'=>40,'cw'=>$cw,'CMap'=>$CMap,'registry'=>$registry);
31
    }
32
33
    function AddCIDFonts($family,$name,$cw,$CMap,$registry)
34
    {
35
        $this->AddCIDFont($family,'',$name,$cw,$CMap,$registry);
36
        $this->AddCIDFont($family,'B',$name.',Bold',$cw,$CMap,$registry);
37
        $this->AddCIDFont($family,'I',$name.',Italic',$cw,$CMap,$registry);
38
        $this->AddCIDFont($family,'BI',$name.',BoldItalic',$cw,$CMap,$registry);
39
    }
40
41
    function AddSJISFont($family='SJIS')
42
    {
43
        //Add SJIS font with proportional Latin
44
        $name='KozMinPro-Regular-Acro';
45
        $cw=$this->SJIS_widths;
46
        $CMap='90msp-RKSJ-H';
47
        $registry=array('ordering'=>'Japan1','supplement'=>2);
48
        $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
49
    }
50
    
51
    function AddSJIShwFont($family='SJIS-hw')
52
    {
53
        //Add SJIS font with half-width Latin
54
        $name='KozMinPro-Regular-Acro';
55
        for($i=32;$i<=126;$i++)
56
            $cw[chr($i)]=500;
57
        $CMap='90ms-RKSJ-H';
58
        $registry=array('ordering'=>'Japan1','supplement'=>2);
59
        $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
60
    }
61
62
    function GetStringWidth($s)
63
    {
64
        if($this->CurrentFont['type']=='Type0')
65
            return $this->GetSJISStringWidth($s);
66
        else
67
            return parent::GetStringWidth($s);
68
    }
69
70
    function GetSJISStringWidth($s)
71
    {
72
        //SJIS version of GetStringWidth()
73
        $l=0;
74
        $cw=&$this->CurrentFont['cw'];
75
        $nb=strlen($s);
76
        $i=0;
77
        while($i<$nb) {
78
            $o=ord($s{$i});
79
            if($o<128) {
80
                //ASCII
81
                $l+=$cw[$s{$i}];
82
                $i++;
83
            }
84
            elseif($o>=161 and $o<=223) {
85
                //Half-width katakana
86
                $l+=500;
87
                $i++;
88
            } else {
89
                //Full-width character
90
                $l+=1000;
91
                $i+=2;
92
            }
93
        }
94
        return $l*$this->FontSize/1000;
95
    }
96
97
    function MultiCell($w,$h,$txt,$border=0,$align='L',$fill=0)
98
    {
99
        if($this->CurrentFont['type']=='Type0')
100
            $this->SJISMultiCell($w,$h,$txt,$border,$align,$fill);
101
        else
102
            parent::MultiCell($w,$h,$txt,$border,$align,$fill);
103
    }
104
  
105
    function SJISMultiCell($w,$h,$txt,$border=0,$align='L',$fill=0)
106
    {
107
        //Output text with automatic or explicit line breaks
108
        $cw=&$this->CurrentFont['cw'];
109
        if($w==0)
110
            $w=$this->w-$this->rMargin-$this->x;
111
        $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
112
        $s=str_replace("\r",'',$txt);
113
        $nb=strlen($s);
114
        if($nb>0 and $s{$nb-1}=="\n")
115
            $nb--;
116
        $b=0;
117
        if($border) {
118
            if($border==1) {
119
                $border='LTRB';
120
                $b='LRT';
121
                $b2='LR';
122
            } else {
123
                $b2='';
124
                if(is_int(strpos($border,'L')))
125
                    $b2.='L';
126
                if(is_int(strpos($border,'R')))
127
                    $b2.='R';
128
                $b=is_int(strpos($border,'T')) ? $b2.'T' : $b2;
129
            }
130
        }
131
        $sep=-1;
132
        $i=0;
133
        $j=0;
134
        $l=0;
135
        $nl=1;
136
        while($i<$nb) {
137
            //Get next character
138
            $c=$s{$i};
139
            $o=ord($c);
140
            if($o==10) {
141
                //Explicit line break
142
                $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
143
                $i++;
144
                $sep=-1;
145
                $j=$i;
146
                $l=0;
147
                $nl++;
148
                if($border and $nl==2)
149
                    $b=$b2;
150
                continue;
151
            }
152
            if($o<128) {
153
                //ASCII
154
                $l+=$cw[$c];
155
                $n=1;
156
                if($o==32)
157
                    $sep=$i;
158
            } elseif($o>=161 and $o<=223) {
159
                //Half-width katakana
160
                $l+=500;
161
                $n=1;
162
                $sep=$i;
163
            } else {
164
                //Full-width character
165
                $l+=1000;
166
                $n=2;
167
                $sep=$i;
168
            }
169
            if($l>$wmax) {
170
                //Automatic line break
171
                if($sep==-1 or $i==$j) {
172
                    if($i==$j)
173
                        $i+=$n;
174
                    $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
175
                } else {
176
                    $this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);
177
                    $i=($s[$sep]==' ') ? $sep+1 : $sep;
178
                }
179
                $sep=-1;
180
                $j=$i;
181
                $l=0;
182
                $nl++;
183
                if($border and $nl==2)
184
                    $b=$b2;
185
            } else {
186
                $i+=$n;
187
                if($o>=128)
188
                    $sep=$i;
189
            }
190
        }
191
	//Last chunk
192
	if($border and is_int(strpos($border,'B')))
193
            $b.='B';
194
	$this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
195
	$this->x=$this->lMargin;
196
    }
197
198
    function Write($h,$txt,$link='')
199
    {
200
	if($this->CurrentFont['type']=='Type0')
201
            $this->SJISWrite($h,$txt,$link);
202
	else
203
            parent::Write($h,$txt,$link);
204
    }
205
206
    function SJISWrite($h,$txt,$link)
207
    {
208
	//SJIS version of Write()
209
	$cw=&$this->CurrentFont['cw'];
210
	$w=$this->w-$this->rMargin-$this->x;
211
	$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
212
	$s=str_replace("\r",'',$txt);
213
	$nb=strlen($s);
214
	$sep=-1;
215
	$i=0;
216
	$j=0;
217
	$l=0;
218
	$nl=1;
219
	while($i<$nb) {
220
            //Get next character
221
            $c=$s{$i};
222
            $o=ord($c);
223
            if($o==10) {
224
                //Explicit line break
225
                $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
226
                $i++;
227
                $sep=-1;
228
                $j=$i;
229
                $l=0;
230
                if($nl==1) {
231
                    //Go to left margin
232
                    $this->x=$this->lMargin;
233
                    $w=$this->w-$this->rMargin-$this->x;
234
                    $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
235
                }
236
                $nl++;
237
                continue;
238
            }
239
            if($o<128) {
240
                //ASCII
241
                $l+=$cw[$c];
242
                $n=1;
243
                if($o==32)
244
                    $sep=$i;
245
            } elseif($o>=161 and $o<=223) {
246
                //Half-width katakana
247
                $l+=500;
248
                $n=1;
249
                $sep=$i;
250
            } else {
251
                //Full-width character
252
                $l+=1000;
253
                $n=2;
254
                $sep=$i;
255
            }
256
            if($l>$wmax) {
257
                //Automatic line break
258
                if($sep==-1 or $i==$j) {
259
                    if($this->x>$this->lMargin) {
260
                        //Move to next line
261
                        $this->x=$this->lMargin;
262
                        $this->y+=$h;
263
                        $w=$this->w-$this->rMargin-$this->x;
264
                        $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
265
                        $i+=$n;
266
                        $nl++;
267
                        continue;
268
                    }
269
                    if($i==$j)
270
                        $i+=$n;
271
                    $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
272
                } else {
273
                    $this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',0,$link);
274
                    $i=($s[$sep]==' ') ? $sep+1 : $sep;
275
                }
276
                $sep=-1;
277
                $j=$i;
278
                $l=0;
279
                if($nl==1) {
280
                    $this->x=$this->lMargin;
281
                    $w=$this->w-$this->rMargin-$this->x;
282
                    $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
283
                }
284
                $nl++;
285
            } else {
286
                $i+=$n;
287
                if($o>=128)
288
                    $sep=$i;
289
            }
290
        }
291
	//Last chunk
292
	if($i!=$j)
293
            $this->Cell($l/1000*$this->FontSize,$h,substr($s,$j,$i-$j),0,0,'',0,$link);
294
    }
295
296
    function _putfonts()
297
    {
298
	$nf=$this->n;
299
	foreach($this->diffs as $diff) {
300
            //Encodings
301
            $this->_newobj();
302
            $this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>');
303
            $this->_out('endobj');
304
        }
305
	$mqr=get_magic_quotes_runtime();
306
	set_magic_quotes_runtime(0);
307
	foreach($this->FontFiles as $file=>$info) {
308
            //Font file embedding
309
            $this->_newobj();
310
            $this->FontFiles[$file]['n']=$this->n;
311
            if(defined('FPDF_FONTPATH'))
312
                $file=FPDF_FONTPATH.$file;
313
            $size=filesize($file);
314
            if(!$size)
315
                $this->Error('Font file not found');
316
            $this->_out('<</Length '.$size);
317
            if(substr($file,-2)=='.z')
318
                $this->_out('/Filter /FlateDecode');
319
            $this->_out('/Length1 '.$info['length1']);
320
            if(isset($info['length2']))
321
                $this->_out('/Length2 '.$info['length2'].' /Length3 0');
322
            $this->_out('>>');
323
            $f=fopen($file,'rb');
324
            $this->_putstream(fread($f,$size));
325
            fclose($f);
326
            $this->_out('endobj');
327
        }
328
	set_magic_quotes_runtime($mqr);
329
	foreach($this->fonts as $k=>$font) {
330
            //Font objects
331
            $this->_newobj();
332
            $this->fonts[$k]['n']=$this->n;
333
            $this->_out('<</Type /Font');
334
            if($font['type']=='Type0')
335
                $this->_putType0($font);
336
            else {
337
                $name=$font['name'];
338
                $this->_out('/BaseFont /'.$name);
339
                if($font['type']=='core') {
340
                    //Standard font
341
                    $this->_out('/Subtype /Type1');
342
                    if($name!='Symbol' and $name!='ZapfDingbats')
343
                        $this->_out('/Encoding /WinAnsiEncoding');
344
                } else {
345
                    //Additional font
346
                    $this->_out('/Subtype /'.$font['type']);
347
                    $this->_out('/FirstChar 32');
348
                    $this->_out('/LastChar 255');
349
                    $this->_out('/Widths '.($this->n+1).' 0 R');
350
                    $this->_out('/FontDescriptor '.($this->n+2).' 0 R');
351
                    if($font['enc']) {
352
                        if(isset($font['diff']))
353
                            $this->_out('/Encoding '.($nf+$font['diff']).' 0 R');
354
                        else
355
                            $this->_out('/Encoding /WinAnsiEncoding');
356
                    }
357
                }
358
                $this->_out('>>');
359
                $this->_out('endobj');
360
                if($font['type']!='core') {
361
                    //Widths
362
                    $this->_newobj();
363
                    $cw=&$font['cw'];
364
                    $s='[';
365
                    for($i=32;$i<=255;$i++)
366
                        $s.=$cw[chr($i)].' ';
367
                    $this->_out($s.']');
368
                    $this->_out('endobj');
369
                    //Descriptor
370
                    $this->_newobj();
371
                    $s='<</Type /FontDescriptor /FontName /'.$name;
372
                    foreach($font['desc'] as $k=>$v)
373
                        $s.=' /'.$k.' '.$v;
374
                    $file=$font['file'];
375
                    if($file)
376
                        $s.=' /FontFile'.($font['type']=='Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R';
377
                    $this->_out($s.'>>');
378
                    $this->_out('endobj');
379
                }
380
            }
381
        }
382
    }
383
384
    function _putType0($font)
385
    {
386
	//Type0
387
	$this->_out('/Subtype /Type0');
388
	$this->_out('/BaseFont /'.$font['name'].'-'.$font['CMap']);
389
	$this->_out('/Encoding /'.$font['CMap']);
390
	$this->_out('/DescendantFonts ['.($this->n+1).' 0 R]');
391
	$this->_out('>>');
392
	$this->_out('endobj');
393
	//CIDFont
394
	$this->_newobj();
395
	$this->_out('<</Type /Font');
396
	$this->_out('/Subtype /CIDFontType0');
397
	$this->_out('/BaseFont /'.$font['name']);
398
	$this->_out('/CIDSystemInfo <</Registry (Adobe) /Ordering ('.$font['registry']['ordering'].') /Supplement '.$font['registry']['supplement'].'>>');
399
	$this->_out('/FontDescriptor '.($this->n+1).' 0 R');
400
	$W='/W [1 [';
401
	foreach($font['cw'] as $w)
402
            $W.=$w.' ';
403
	$this->_out($W.'] 231 325 500 631 [500] 326 389 500]');
404
	$this->_out('>>');
405
	$this->_out('endobj');
406
	//Font descriptor
407
	$this->_newobj();
408
	$this->_out('<</Type /FontDescriptor');
409
	$this->_out('/FontName /'.$font['name']);
410
	$this->_out('/Flags 6');
411
	$this->_out('/FontBBox [0 -200 1000 900]');
412
	$this->_out('/ItalicAngle 0');
413
	$this->_out('/Ascent 800');
414
	$this->_out('/Descent -200');
415
	$this->_out('/CapHeight 800');
416
	$this->_out('/StemV 60');
417
	$this->_out('>>');
418
	$this->_out('endobj');
419
    }
420
}
421
422
// Local Variables:
423
// mode: php
424
// tab-width: 8
425
// c-basic-offset: 4
426
// c-hanging-comment-ender-p: nil
427
// indent-tabs-mode: nil
428
// End:
429
?>
430