Passed
Push — master ( 63d1f8...7c50e7 )
by Joe Nilson
02:33
created

exFPDF::is_hex()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 11
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 16
rs 9.2222
1
<?php
2
 /*********************************************************************
3
 * exFPDF  extend FPDF v1.81                                                    *
4
 *                                                                    *
5
 * Version: 2.2                                                       *
6
 * Date:    12-10-2017                                                *
7
 * Author:  Dan Machado                                               *
8
 * Require  FPDF v1.81, formatedstring v1.0                                                *
9
 **********************************************************************/
10
 include 'formatedstring.php';
11
 class exFPDF extends FPDF
12
 {
13
    public function PageBreak(){
14
       return $this->PageBreakTrigger;
15
   }
16
17
   public function current_font($c){
18
      if($c=='family'){
19
         return $this->FontFamily;
20
      }
21
      elseif($c=='style'){
22
         return $this->FontStyle;
23
      }
24
      elseif($c=='size'){
25
         return $this->FontSizePt;
26
      }
27
   }
28
29
   public function get_color($c){
30
      if($c=='fill'){
31
         return $this->FillColor;
32
      }
33
      elseif($c=='text'){
34
         return $this->TextColor;
35
      }
36
   }
37
38
   public function get_page_width(){
39
      return $this->w;
40
   }
41
42
   public function get_margin($c){
43
      if($c=='l'){
44
         return $this->lMargin;
45
      }
46
      elseif($c=='r'){
47
         return $this->rMargin;
48
      }
49
      elseif($c=='t'){
50
         return $this->tMargin;
51
      }
52
   }
53
54
   public function get_linewidth(){
55
      return $this->LineWidth;
56
   }
57
58
   public function get_orientation(){
59
      return $this->CurOrientation;
60
   }
61
   static private $hex=array('0'=>0,'1'=>1,'2'=>2,'3'=>3,'4'=>4,'5'=>5,'6'=>6,'7'=>7,'8'=>8,'9'=>9,
62
   'A'=>10,'B'=>11,'C'=>12,'D'=>13,'E'=>14,'F'=>15);
63
64
   public function is_rgb($str){
65
      $a=true;
66
      $tmp=explode(',', trim($str, ','));
67
      foreach($tmp as $color){
68
         if(!is_numeric($color) || $color<0 || $color>255){
69
            $a=false;
70
            break;
71
         }
72
      }
73
      return $a;
74
   }
75
76
   public function is_hex($str){
77
      $a=true;
78
      $str=strtoupper($str);
79
      $n=strlen($str);
80
      if(($n==7 || $n==4) && $str[0]=='#'){
81
         for($i=1; $i<$n; $i++){
82
            if(!isset(self::$hex[$str[$i]])){
83
               $a=false;
84
               break;
85
            }
86
         }
87
      }
88
      else{
89
         $a=false;
90
      }
91
      return $a;
92
   }
93
94
   public function hextodec($str){
95
      $result=array();
96
      $str=strtoupper(substr($str,1));
97
      $n=strlen($str);
98
      for($i=0; $i<3; $i++){
99
         if($n==6){
100
            $result[$i]=self::$hex[$str[2*$i]]*16+self::$hex[$str[2*$i+1]];
101
         }
102
         else{
103
            $result[$i]=self::$hex[$str[$i]]*16+self::$hex[$str[$i]];
104
         }
105
      }
106
      return $result;
107
   }
108
   static private $options=array('F'=>'', 'T'=>'', 'D'=>'');
109
110
   public function resetColor($str, $p='F'){
111
      if(isset(self::$options[$p]) && self::$options[$p]!=$str){
112
         self::$options[$p]=$str;
113
         $array=array();
114
         if($this->is_hex($str)){
115
            $array=$this->hextodec($str);
116
         }
117
         elseif($this->is_rgb($str)){
118
            $array=explode(',', trim($str, ','));
119
            for($i=0; $i<3; $i++){
120
               if(!isset($array[$i])){
121
                  $array[$i]=0;
122
               }
123
            }
124
         }
125
         else{
126
            $array=array(null, null, null);
127
            $i=0;
128
            $tmp=explode(' ', $str);
129
            foreach($tmp as $c){
130
               if(is_numeric($c)){
131
                  $array[$i]=$c*256;
132
                  $i++;
133
               }
134
            }
135
         }
136
         if($p=='T'){
137
            $this->SetTextColor($array[0],$array[1],$array[2]);
138
         }
139
         elseif($p=='D'){
140
            $this->SetDrawColor($array[0],$array[1], $array[2]);
141
         }
142
         elseif($p=='F'){
143
            $this->SetFillColor($array[0],$array[1],$array[2]);
144
         }
145
      }
146
   }
147
   static private $font_def='';
148
149
   public function resetFont($font_family, $font_style, $font_size){
150
      if(self::$font_def!=$font_family .'-' . $font_style . '-' .$font_size){
151
         self::$font_def=$font_family .'-' . $font_style . '-' .$font_size;
152
         $this->SetFont($font_family, $font_style, $font_size);
153
      }
154
   }
155
156
   public function resetStaticData(){
157
      self::$font_def='';
158
      self::$options=array('F'=>'', 'T'=>'', 'D'=>'');
159
   }
160
   /***********************************************************************
161
   *
162
   * Based on FPDF method SetFont
163
   *
164
   ************************************************************************/
165
166
   private function &FontData($family, $style, $size){
167
      if($family=='')
168
      $family = $this->FontFamily;
169
      else
170
      $family = strtolower($family);
171
      $style = strtoupper($style);
172
      if(strpos($style,'U')!==false){
173
         $this->underline = true;
174
         $style = str_replace('U','',$style);
175
      }
176
      if($style=='IB')
177
      $style = 'BI';
178
      $fontkey = $family.$style;
179
      if(!isset($this->fonts[$fontkey])){
180
         if($family=='arial')
181
         $family = 'helvetica';
182
         if(in_array($family,$this->CoreFonts)){
183
            if($family=='symbol' || $family=='zapfdingbats')
184
            $style = '';
185
            $fontkey = $family.$style;
186
            if(!isset($this->fonts[$fontkey]))
187
            $this->AddFont($family,$style);
188
         }
189
         else
190
         $this->Error('Undefined font: '.$family.' '.$style);
191
      }
192
      $result['FontSize'] = $size/$this->k;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.
Loading history...
193
      $result['CurrentFont']=&$this->fonts[$fontkey];
194
      return $result;
195
   }
196
    
197
198
   private function setLines(&$fstring, $p, $q){
199
      $parced_str=& $fstring->parced_str;
200
      $lines=& $fstring->lines;
201
      $linesmap=& $fstring->linesmap;
202
      $cfty=$fstring->get_current_style($p);
203
      $ffs=$cfty['font-family'] . $cfty['style'];
204
      if(!isset($fstring->used_fonts[$ffs])){
205
         $fstring->used_fonts[$ffs]=& $this->FontData($cfty['font-family'], $cfty['style'], $cfty['font-size']);
206
      }
207
      $cw=& $fstring->used_fonts[$ffs]['CurrentFont']['cw'];
208
      $wmax = $fstring->width*1000*$this->k;
209
      $j=count($lines)-1;
210
      $k=strlen($lines[$j]);
211
         if(!isset($linesmap[$j][0])) {
212
         $linesmap[$j]=array($p,$p, 0);
213
      }
214
      $sl=$cw[' ']*$cfty['font-size'];
215
      $x=$a=$linesmap[$j][2];
216
      if($k>0){
217
         $x+=$sl;
218
         $lines[$j].=' ';
219
         $linesmap[$j][2]+=$sl;
220
      }
221
      $u=$p;
0 ignored issues
show
Unused Code introduced by
The assignment to $u is dead and can be removed.
Loading history...
222
      $t='';
223
      $l=$p+$q;
224
      $ftmp='';
225
      for($i=$p; $i<$l; $i++){
226
            if($ftmp!=$ffs){
227
            $cfty=$fstring->get_current_style($i);
228
            $ffs=$cfty['font-family'] . $cfty['style'];
229
            if(!isset($fstring->used_fonts[$ffs])){
230
               $fstring->used_fonts[$ffs]=& $this->FontData($cfty['font-family'], $cfty['style'], $cfty['font-size']);
231
            }
232
            $cw=& $fstring->used_fonts[$ffs]['CurrentFont']['cw'];
233
            $ftmp=$ffs;
234
         }
235
         $x+=$cw[$parced_str[$i]]*$cfty['font-size'];
236
         if($x>$wmax){
237
            if($a>0){
238
               $t=substr($parced_str,$p, $i-$p);
239
               $lines[$j]=substr($lines[$j],0,$k);
240
               $linesmap[$j][1]=$p-1;
241
               $linesmap[$j][2]=$a;
242
               $x-=($a+$sl);
243
               $a=0;
244
               $u=$p;
245
            }
246
            else{
247
               $x=$cw[$parced_str[$i]]*$cfty['font-size'];
248
               $t='';
249
               $u=$i;
250
            }
251
            $j++;
252
            $lines[$j]=$t;
253
            $linesmap[$j]=array();
254
            $linesmap[$j][0]=$u;
255
            $linesmap[$j][2]=0;
256
         }
257
         $lines[$j].=$parced_str[$i];
258
         $linesmap[$j][1]=$i;
259
         $linesmap[$j][2]=$x;
260
      }
261
      return;
262
   }
263
264
   public function &extMultiCell($font_family, $font_style, $font_size, $font_color, $w, $txt){
265
      $result=array();
266
      if($w==0){
267
         return $result;
268
      }
269
      $this->current_font=array('font-family'=>$font_family, 'style'=>$font_style, 'font-size'=>$font_size, 'font-color'=>$font_color);
0 ignored issues
show
Bug Best Practice introduced by
The property current_font does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
270
      $fstring=new formatedString($txt, $w, $this->current_font);
271
      $word='';
272
      $p=0;
273
      $i=0;
274
      $n=strlen($fstring->parced_str);
275
      while($i<$n){
276
         $word.=$fstring->parced_str[$i];
277
         if($fstring->parced_str[$i]=="\n" || $fstring->parced_str[$i]==' ' || $i==$n-1){
278
            $word=trim($word);
279
            $this->setLines($fstring, $p, strlen($word));
280
            $p=$i+1;
281
            $word='';
282
            if($fstring->parced_str[$i]=="\n" && $i<$n-1){
283
               $z=0;
0 ignored issues
show
Unused Code introduced by
The assignment to $z is dead and can be removed.
Loading history...
284
               $j=count($fstring->lines);
285
               $fstring->lines[$j]='';
286
               $fstring->linesmap[$j]=array();
287
            }
288
         }
289
         $i++;
290
      }
291
      if($n==0){
292
         return $result;
293
      }
294
      $n=count($fstring->lines);
295
         for($i=0; $i<$n; $i++){
296
         $result[$i]=$fstring->break_by_style($i);
297
      }
298
      return $result;
299
   }
300
301
   private function GetMixStringWidth($line){
302
      $w = 0;
303
      foreach($line['chunks'] as $i=>$chunk){
304
         $t=0;
305
         $cf=& $this->FontData($line['style'][$i]['font-family'], $line['style'][$i]['style'], $line['style'][$i]['font-size']);
306
         $cw=& $cf['CurrentFont']['cw'];
307
         $s=implode('', explode(' ',$chunk));
308
         $l = strlen($s);
309
         for($j=0;$j<$l;$j++){
310
            $t+=$cw[$s[$j]];
311
         }
312
         $w+=$t*$line['style'][$i]['font-size'];
313
      }
314
      return $w;
315
   }
316
317
   public function CellBlock($w, $lh, &$lines, $align='J'){
318
      if($w==0){
319
         return;
320
      }
321
      $ctmp='';
0 ignored issues
show
Unused Code introduced by
The assignment to $ctmp is dead and can be removed.
Loading history...
322
      $ftmp='';
0 ignored issues
show
Unused Code introduced by
The assignment to $ftmp is dead and can be removed.
Loading history...
323
      foreach($lines as $i=>$line){
324
         $k = $this->k;
0 ignored issues
show
Unused Code introduced by
The assignment to $k is dead and can be removed.
Loading history...
325
         if($this->y+$lh*$line['height']>$this->PageBreakTrigger){
326
            break;
327
         }
328
         $dx=0;
329
         $dw=0;
330
         if($line['width']!=0){
331
            if($align=='R'){
332
               $dx = $w-$line['width']/($this->k*1000);
333
            }
334
            elseif($align=='C'){
335
               $dx = ($w-$line['width']/($this->k*1000))/2;
336
            }
337
            if($align=='J'){
338
               $tmp=explode(' ', implode('',$line['chunks']));
339
               $ns=count($tmp);
340
               if($ns>1){
341
                  $sx=implode('',$tmp);
0 ignored issues
show
Unused Code introduced by
The assignment to $sx is dead and can be removed.
Loading history...
342
                  $delta=$this->GetMixStringWidth($line)/($this->k*1000);
343
                  $dw=($w-$delta)*(1/($ns-1));
344
               }
345
            }
346
         }
347
         $xx=$this->x+$dx;
348
         foreach($line['chunks'] as $tj=>$txt){
349
            $this->resetFont($line['style'][$tj]['font-family'], $line['style'][$tj]['style'], $line['style'][$tj]['font-size']);
350
            $this->resetColor($line['style'][$tj]['font-color'], 'T');
351
            $y=$this->y+0.5*$lh*$line['height'] +0.3*$line['height']/$this->k;
352
            if($dw){
353
               $tmp=explode(' ', $txt);
354
               foreach($tmp as $e=>$tt){
355
                  if($e>0){
356
                     $xx+=$dw;
357
                     if($tt==''){
358
                        continue;
359
                     }
360
                  }
361
                  $this->Text($xx, $y, $tt);
362
                     if($line['style'][$tj]['href']){
363
                     $yr=$this->y+0.5*($lh*$line['height']-$line['height']/$this->k);
364
                     $this->Link($xx, $yr, $this->GetStringWidth($txt),$line['height']/$this->k, $line['style'][$tj]['href']);
365
                  }
366
                  $xx+=$this->GetStringWidth($tt);
367
               }
368
            }
369
            else{
370
               $this->Text($xx, $y, $txt);
371
                  if($line['style'][$tj]['href']){
372
                  $yr=$this->y+0.5*($lh*$line['height']-$line['height']/$this->k);
373
                  $this->Link($xx, $yr, $this->GetStringWidth($txt),$line['height']/$this->k, $line['style'][$tj]['href']);
374
               }
375
               $xx+=$this->GetStringWidth($txt);
376
            }
377
         }
378
         unset($lines[$i]);
379
         $this->y += $lh*$line['height'];
380
      }
381
   }
382
   
383
}
384
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
385