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_Chinese::MBMultiCell()   F
last analyzed

Complexity

Conditions 27
Paths 6000

Size

Total Lines 95
Code Lines 67

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 95
rs 2
cc 27
eloc 67
nc 6000
nop 6

How to fix   Long Method    Complexity   

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 // -*-php-*-
2
rcs_id('$Id: chinese.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('lib/pdf.php');
7
8
$Big5_widths=array(' '=>250,'!'=>250,'"'=>408,'#'=>668,'$'=>490,'%'=>875,'&'=>698,'\''=>250,
9
	'('=>240,')'=>240,'*'=>417,'+'=>667,','=>250,'-'=>313,'.'=>250,'/'=>520,'0'=>500,'1'=>500,
10
	'2'=>500,'3'=>500,'4'=>500,'5'=>500,'6'=>500,'7'=>500,'8'=>500,'9'=>500,':'=>250,';'=>250,
11
	'<'=>667,'='=>667,'>'=>667,'?'=>396,'@'=>921,'A'=>677,'B'=>615,'C'=>719,'D'=>760,'E'=>625,
12
	'F'=>552,'G'=>771,'H'=>802,'I'=>354,'J'=>354,'K'=>781,'L'=>604,'M'=>927,'N'=>750,'O'=>823,
13
	'P'=>563,'Q'=>823,'R'=>729,'S'=>542,'T'=>698,'U'=>771,'V'=>729,'W'=>948,'X'=>771,'Y'=>677,
14
	'Z'=>635,'['=>344,'\\'=>520,']'=>344,'^'=>469,'_'=>500,'`'=>250,'a'=>469,'b'=>521,'c'=>427,
15
	'd'=>521,'e'=>438,'f'=>271,'g'=>469,'h'=>531,'i'=>250,'j'=>250,'k'=>458,'l'=>240,'m'=>802,
16
	'n'=>531,'o'=>500,'p'=>521,'q'=>521,'r'=>365,'s'=>333,'t'=>292,'u'=>521,'v'=>458,'w'=>677,
17
	'x'=>479,'y'=>458,'z'=>427,'{'=>480,'|'=>496,'}'=>480,'~'=>667);
18
19
$GB_widths=array(' '=>207,'!'=>270,'"'=>342,'#'=>467,'$'=>462,'%'=>797,'&'=>710,'\''=>239,
20
	'('=>374,')'=>374,'*'=>423,'+'=>605,','=>238,'-'=>375,'.'=>238,'/'=>334,'0'=>462,'1'=>462,
21
	'2'=>462,'3'=>462,'4'=>462,'5'=>462,'6'=>462,'7'=>462,'8'=>462,'9'=>462,':'=>238,';'=>238,
22
	'<'=>605,'='=>605,'>'=>605,'?'=>344,'@'=>748,'A'=>684,'B'=>560,'C'=>695,'D'=>739,'E'=>563,
23
	'F'=>511,'G'=>729,'H'=>793,'I'=>318,'J'=>312,'K'=>666,'L'=>526,'M'=>896,'N'=>758,'O'=>772,
24
	'P'=>544,'Q'=>772,'R'=>628,'S'=>465,'T'=>607,'U'=>753,'V'=>711,'W'=>972,'X'=>647,'Y'=>620,
25
	'Z'=>607,'['=>374,'\\'=>333,']'=>374,'^'=>606,'_'=>500,'`'=>239,'a'=>417,'b'=>503,'c'=>427,
26
	'd'=>529,'e'=>415,'f'=>264,'g'=>444,'h'=>518,'i'=>241,'j'=>230,'k'=>495,'l'=>228,'m'=>793,
27
	'n'=>527,'o'=>524,'p'=>524,'q'=>504,'r'=>338,'s'=>336,'t'=>277,'u'=>517,'v'=>450,'w'=>652,
28
	'x'=>466,'y'=>452,'z'=>407,'{'=>370,'|'=>258,'}'=>370,'~'=>605);
29
30
class PDF_Chinese extends PDF
31
{
32
    function AddCIDFont($family,$style,$name,$cw,$CMap,$registry)
33
    {
34
	$fontkey=strtolower($family).strtoupper($style);
35
	if(isset($this->fonts[$fontkey]))
36
		$this->Error("Font already added: $family $style");
37
	$i=count($this->fonts)+1;
38
	$name=str_replace(' ','',$name);
39
	$this->fonts[$fontkey]=array('i'=>$i,'type'=>'Type0','name'=>$name,'up'=>-130,'ut'=>40,'cw'=>$cw,'CMap'=>$CMap,'registry'=>$registry);
40
    }
41
42
    function AddCIDFonts($family,$name,$cw,$CMap,$registry)
43
    {
44
	$this->AddCIDFont($family,'',$name,$cw,$CMap,$registry);
45
	$this->AddCIDFont($family,'B',$name.',Bold',$cw,$CMap,$registry);
46
	$this->AddCIDFont($family,'I',$name.',Italic',$cw,$CMap,$registry);
47
	$this->AddCIDFont($family,'BI',$name.',BoldItalic',$cw,$CMap,$registry);
48
    }
49
50
    function AddBig5Font($family='Big5',$name='MSungStd-Light-Acro')
51
    {
52
	//Add Big5 font with proportional Latin
53
	$cw=$GLOBALS['Big5_widths'];
54
	$CMap='ETenms-B5-H';
55
	$registry=array('ordering'=>'CNS1','supplement'=>0);
56
	$this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
57
    }
58
59
    function AddBig5hwFont($family='Big5-hw',$name='MSungStd-Light-Acro')
60
    {
61
	//Add Big5 font with half-witdh Latin
62
	for($i=32;$i<=126;$i++)
63
		$cw[chr($i)]=500;
64
	$CMap='ETen-B5-H';
65
	$registry=array('ordering'=>'CNS1','supplement'=>0);
66
	$this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
67
    }
68
69
    function AddGBFont($family='GB',$name='STSongStd-Light-Acro')
70
    {
71
	//Add GB font with proportional Latin
72
	$cw=$GLOBALS['GB_widths'];
73
	$CMap='GBKp-EUC-H';
74
	$registry=array('ordering'=>'GB1','supplement'=>2);
75
	$this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
76
    }
77
78
    function AddGBhwFont($family='GB-hw',$name='STSongStd-Light-Acro')
79
    {
80
	//Add GB font with half-width Latin
81
	for($i=32;$i<=126;$i++)
82
		$cw[chr($i)]=500;
83
	$CMap='GBK-EUC-H';
84
	$registry=array('ordering'=>'GB1','supplement'=>2);
85
	$this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
86
    }
87
88
    function GetStringWidth($s)
89
    {
90
	if ($this->CurrentFont['type']=='Type0')
91
		return $this->GetMBStringWidth($s);
92
	else
93
		return parent::GetStringWidth($s);
94
    }
95
96
    function GetMBStringWidth($s)
97
    {
98
	//Multi-byte version of GetStringWidth()
99
	$l=0;
100
	$cw=&$this->CurrentFont['cw'];
101
	$nb=strlen($s);
102
	$i=0;
103
	while($i<$nb)
104
	{
105
		$c=$s[$i];
106
		if(ord($c)<128)
107
		{
108
			$l+=$cw[$c];
109
			$i++;
110
		}
111
		else
112
		{
113
			$l+=1000;
114
			$i+=2;
115
		}
116
	}
117
	return $l*$this->FontSize/1000;
118
    }
119
120
    function MultiCell($w,$h,$txt,$border=0,$align='L',$fill=0)
121
    {
122
	if($this->CurrentFont['type']=='Type0')
123
		$this->MBMultiCell($w,$h,$txt,$border,$align,$fill);
124
	else
125
		parent::MultiCell($w,$h,$txt,$border,$align,$fill);
126
    }
127
128
    function MBMultiCell($w,$h,$txt,$border=0,$align='L',$fill=0)
129
    {
130
	//Multi-byte version of MultiCell()
131
	$cw=&$this->CurrentFont['cw'];
132
	if($w==0)
133
		$w=$this->w-$this->rMargin-$this->x;
134
	$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
135
	$s=str_replace("\r",'',$txt);
136
	$nb=strlen($s);
137
	if($nb>0 and $s[$nb-1]=="\n")
138
		$nb--;
139
	$b=0;
140
	if($border)
141
	{
142
		if($border==1)
143
		{
144
			$border='LTRB';
145
			$b='LRT';
146
			$b2='LR';
147
		}
148
		else
149
		{
150
			$b2='';
151
			if(is_int(strpos($border,'L')))
152
				$b2.='L';
153
			if(is_int(strpos($border,'R')))
154
				$b2.='R';
155
			$b=is_int(strpos($border,'T')) ? $b2.'T' : $b2;
156
		}
157
	}
158
	$sep=-1;
159
	$i=0;
160
	$j=0;
161
	$l=0;
162
	$nl=1;
163
	while($i<$nb)
164
	{
165
		//Get next character
166
		$c=$s[$i];
167
		//Check if ASCII or MB
168
		$ascii=(ord($c)<128);
169
		if($c=="\n")
170
		{
171
			//Explicit line break
172
			$this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
173
			$i++;
174
			$sep=-1;
175
			$j=$i;
176
			$l=0;
177
			$nl++;
178
			if($border and $nl==2)
179
				$b=$b2;
180
			continue;
181
		}
182
		if(!$ascii)
183
		{
184
			$sep=$i;
185
			$ls=$l;
186
		}
187
		elseif($c==' ')
188
		{
189
			$sep=$i;
190
			$ls=$l;
191
		}
192
		$l+=$ascii ? $cw[$c] : 1000;
193
		if($l>$wmax)
194
		{
195
			//Automatic line break
196
			if($sep==-1 or $i==$j)
197
			{
198
				if($i==$j)
199
					$i+=$ascii ? 1 : 2;
200
				$this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
201
			}
202
			else
203
			{
204
				$this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);
205
				$i=($s[$sep]==' ') ? $sep+1 : $sep;
206
			}
207
			$sep=-1;
208
			$j=$i;
209
			$l=0;
210
			$nl++;
211
			if($border and $nl==2)
212
				$b=$b2;
213
		}
214
		else
215
			$i+=$ascii ? 1 : 2;
216
	}
217
	//Last chunk
218
	if($border and is_int(strpos($border,'B')))
219
		$b.='B';
220
	$this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
221
	$this->x=$this->lMargin;
222
    }
223
224
    function Write($h,$txt,$link='')
225
    {
226
	if($this->CurrentFont['type']=='Type0')
227
		$this->MBWrite($h,$txt,$link);
228
	else
229
		parent::Write($h,$txt,$link);
230
    }
231
232
    function MBWrite($h,$txt,$link)
233
    {
234
	//Multi-byte version of Write()
235
	$cw=&$this->CurrentFont['cw'];
236
	$w=$this->w-$this->rMargin-$this->x;
237
	$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
238
	$s=str_replace("\r",'',$txt);
239
	$nb=strlen($s);
240
	$sep=-1;
241
	$i=0;
242
	$j=0;
243
	$l=0;
244
	$nl=1;
245
	while($i<$nb)
246
	{
247
		//Get next character
248
		$c=$s[$i];
249
		//Check if ASCII or MB
250
		$ascii=(ord($c)<128);
251
		if($c=="\n")
252
		{
253
			//Explicit line break
254
			$this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
255
			$i++;
256
			$sep=-1;
257
			$j=$i;
258
			$l=0;
259
			if($nl==1)
260
			{
261
				$this->x=$this->lMargin;
262
				$w=$this->w-$this->rMargin-$this->x;
263
				$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
264
			}
265
			$nl++;
266
			continue;
267
		}
268
		if(!$ascii or $c==' ')
269
			$sep=$i;
270
		$l+=$ascii ? $cw[$c] : 1000;
271
		if($l>$wmax)
272
		{
273
			//Automatic line break
274
			if($sep==-1 or $i==$j)
275
			{
276
				if($this->x>$this->lMargin)
277
				{
278
					//Move to next line
279
					$this->x=$this->lMargin;
280
					$this->y+=$h;
281
					$w=$this->w-$this->rMargin-$this->x;
282
					$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
283
					$i++;
284
					$nl++;
285
					continue;
286
				}
287
				if($i==$j)
288
					$i+=$ascii ? 1 : 2;
289
				$this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
290
			}
291
			else
292
			{
293
				$this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',0,$link);
294
				$i=($s[$sep]==' ') ? $sep+1 : $sep;
295
			}
296
			$sep=-1;
297
			$j=$i;
298
			$l=0;
299
			if($nl==1)
300
			{
301
				$this->x=$this->lMargin;
302
				$w=$this->w-$this->rMargin-$this->x;
303
				$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
304
			}
305
			$nl++;
306
		}
307
		else
308
			$i+=$ascii ? 1 : 2;
309
	}
310
	//Last chunk
311
	if($i!=$j)
312
		$this->Cell($l/1000*$this->FontSize,$h,substr($s,$j,$i-$j),0,0,'',0,$link);
313
    }
314
315
    function _putfonts()
316
    {
317
	$nf=$this->n;
318
	foreach($this->diffs as $diff)
319
	{
320
		//Encodings
321
		$this->_newobj();
322
		$this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>');
323
		$this->_out('endobj');
324
	}
325
	$mqr=get_magic_quotes_runtime();
326
	set_magic_quotes_runtime(0);
327
	foreach($this->FontFiles as $file=>$info)
328
	{
329
		//Font file embedding
330
		$this->_newobj();
331
		$this->FontFiles[$file]['n']=$this->n;
332
		if(defined('FPDF_FONTPATH'))
333
			$file=FPDF_FONTPATH.$file;
334
		$size=filesize($file);
335
		if(!$size)
336
			$this->Error('Font file not found');
337
		$this->_out('<</Length '.$size);
338
		if(substr($file,-2)=='.z')
339
			$this->_out('/Filter /FlateDecode');
340
		$this->_out('/Length1 '.$info['length1']);
341
		if(isset($info['length2']))
342
			$this->_out('/Length2 '.$info['length2'].' /Length3 0');
343
		$this->_out('>>');
344
		$f=fopen($file,'rb');
345
		$this->_putstream(fread($f,$size));
346
		fclose($f);
347
		$this->_out('endobj');
348
	}
349
	set_magic_quotes_runtime($mqr);
350
	foreach($this->fonts as $k=>$font)
351
	{
352
		//Font objects
353
		$this->_newobj();
354
		$this->fonts[$k]['n']=$this->n;
355
		$this->_out('<</Type /Font');
356
		if($font['type']=='Type0')
357
			$this->_putType0($font);
358
		else
359
		{
360
			$name=$font['name'];
361
			$this->_out('/BaseFont /'.$name);
362
			if($font['type']=='core')
363
			{
364
				//Standard font
365
				$this->_out('/Subtype /Type1');
366
				if($name!='Symbol' and $name!='ZapfDingbats')
367
					$this->_out('/Encoding /WinAnsiEncoding');
368
			}
369
			else
370
			{
371
				//Additional font
372
				$this->_out('/Subtype /'.$font['type']);
373
				$this->_out('/FirstChar 32');
374
				$this->_out('/LastChar 255');
375
				$this->_out('/Widths '.($this->n+1).' 0 R');
376
				$this->_out('/FontDescriptor '.($this->n+2).' 0 R');
377
				if($font['enc'])
378
				{
379
					if(isset($font['diff']))
380
						$this->_out('/Encoding '.($nf+$font['diff']).' 0 R');
381
					else
382
						$this->_out('/Encoding /WinAnsiEncoding');
383
				}
384
			}
385
			$this->_out('>>');
386
			$this->_out('endobj');
387
			if($font['type']!='core')
388
			{
389
				//Widths
390
				$this->_newobj();
391
				$cw=&$font['cw'];
392
				$s='[';
393
				for($i=32;$i<=255;$i++)
394
					$s.=$cw[chr($i)].' ';
395
				$this->_out($s.']');
396
				$this->_out('endobj');
397
				//Descriptor
398
				$this->_newobj();
399
				$s='<</Type /FontDescriptor /FontName /'.$name;
400
				foreach($font['desc'] as $k=>$v)
401
					$s.=' /'.$k.' '.$v;
402
				$file=$font['file'];
403
				if($file)
404
					$s.=' /FontFile'.($font['type']=='Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R';
405
				$this->_out($s.'>>');
406
				$this->_out('endobj');
407
			}
408
		}
409
	}
410
    }
411
412
    function _putType0($font)
413
    {
414
	//Type0
415
	$this->_out('/Subtype /Type0');
416
	$this->_out('/BaseFont /'.$font['name'].'-'.$font['CMap']);
417
	$this->_out('/Encoding /'.$font['CMap']);
418
	$this->_out('/DescendantFonts ['.($this->n+1).' 0 R]');
419
	$this->_out('>>');
420
	$this->_out('endobj');
421
	//CIDFont
422
	$this->_newobj();
423
	$this->_out('<</Type /Font');
424
	$this->_out('/Subtype /CIDFontType0');
425
	$this->_out('/BaseFont /'.$font['name']);
426
	$this->_out('/CIDSystemInfo <</Registry '.$this->_textstring('Adobe').' /Ordering '.$this->_textstring($font['registry']['ordering']).' /Supplement '.$font['registry']['supplement'].'>>');
427
	$this->_out('/FontDescriptor '.($this->n+1).' 0 R');
428
	if($font['CMap']=='ETen-B5-H')
429
		$W='13648 13742 500';
430
	elseif($font['CMap']=='GBK-EUC-H')
431
		$W='814 907 500 7716 [500]';
432
	else
433
		$W='1 ['.implode(' ',$font['cw']).']';
434
	$this->_out('/W ['.$W.']>>');
435
	$this->_out('endobj');
436
	//Font descriptor
437
	$this->_newobj();
438
	$this->_out('<</Type /FontDescriptor');
439
	$this->_out('/FontName /'.$font['name']);
440
	$this->_out('/Flags 6');
441
	$this->_out('/FontBBox [0 -200 1000 900]');
442
	$this->_out('/ItalicAngle 0');
443
	$this->_out('/Ascent 800');
444
	$this->_out('/Descent -200');
445
	$this->_out('/CapHeight 800');
446
	$this->_out('/StemV 50');
447
	$this->_out('>>');
448
	$this->_out('endobj');
449
    }
450
}
451
452
// Local Variables:
453
// mode: php
454
// tab-width: 8
455
// c-basic-offset: 4
456
// c-hanging-comment-ender-p: nil
457
// indent-tabs-mode: nil
458
// End:
459
?>