Test Failed
Push — develop ( 4dd88b...940a79 )
by Felipe
08:14
created

TTF::File()   F

Complexity

Conditions 18
Paths 1601

Size

Total Lines 84
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 18
eloc 39
c 2
b 0
f 0
nc 1601
nop 3
dl 0
loc 84
rs 0.7

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
2
3
/**
4
 * JPGraph v4.1.0-beta.01
5
 */
6
7
namespace Amenadiel\JpGraph\Text;
8
9
use Amenadiel\JpGraph\Util;
10
11
/**
12
 * @class TTF
13
 * // Description: Handle TTF font names and mapping and loading of
14
 * //              font files
15
 */
16
class TTF
17
{
18
    private $font_files;
19
    private $style_names;
20
    public static $FONT_BASEPATH;
21
    public function __construct()
22
    {
23
        self::$FONT_BASEPATH = getenv('JPGRAPH_FONT_BASEPATH') ?
24
        getenv('JPGRAPH_FONT_BASEPATH') :
25
        dirname(__DIR__) . '/fonts/';
26
        // String names for font styles to be used in error messages
27
        $this->style_names = [
28
            Configs::FS_NORMAL     => 'normal',
29
            Configs::FS_BOLD       => 'bold',
30
            Configs::FS_ITALIC     => 'italic',
31
            Configs::FS_BOLDITALIC => 'bolditalic',
32
        ];
33
34
        // File names for available fonts
35
        $this->font_files = [
36
            Configs::FF_COURIER          => [
37
                Configs::FS_NORMAL     => 'cour.ttf',
38
                Configs::FS_BOLD       => 'courbd.ttf',
39
                Configs::FS_ITALIC     => 'couri.ttf',
40
                Configs::FS_BOLDITALIC => 'courbi.ttf',
41
            ],
42
            Configs::FF_GEORGIA          => [
43
                Configs::FS_NORMAL     => 'georgia.ttf',
44
                Configs::FS_BOLD       => 'georgiab.ttf',
45
                Configs::FS_ITALIC     => 'georgiai.ttf',
46
                Configs::FS_BOLDITALIC => '',
47
            ],
48
            Configs::FF_TREBUCHE         => [
49
                Configs::FS_NORMAL     => 'trebuc.ttf',
50
                Configs::FS_BOLD       => 'trebucbd.ttf',
51
                Configs::FS_ITALIC     => 'trebucit.ttf',
52
                Configs::FS_BOLDITALIC => 'trebucbi.ttf',
53
            ],
54
            Configs::FF_VERDANA          => [
55
                Configs::FS_NORMAL     => 'verdana.ttf',
56
                Configs::FS_BOLD       => 'verdanab.ttf',
57
                Configs::FS_ITALIC     => 'verdanai.ttf',
58
                Configs::FS_BOLDITALIC => '',
59
            ],
60
            Configs::FF_TIMES            => [
61
                Configs::FS_NORMAL     => 'times.ttf',
62
                Configs::FS_BOLD       => 'timesbd.ttf',
63
                Configs::FS_ITALIC     => 'timesi.ttf',
64
                Configs::FS_BOLDITALIC => 'timesbi.ttf',
65
            ],
66
            Configs::FF_COMIC            => [
67
                Configs::FS_NORMAL     => 'comic.ttf',
68
                Configs::FS_BOLD       => 'comicbd.ttf',
69
                Configs::FS_ITALIC     => '',
70
                Configs::FS_BOLDITALIC => '',
71
            ],
72
            /*Configs::FF_ARIAL => array(Configs::FS_NORMAL => 'arial.ttf',
73
            Configs::FS_BOLD => 'arialbd.ttf',
74
            Configs::FS_ITALIC => 'ariali.ttf',
75
            Configs::FS_BOLDITALIC => 'arialbi.ttf'),
76
             */
77
            Configs::FF_ARIAL            => [
78
                Configs::FS_NORMAL     => 'arial.ttf',
79
                Configs::FS_BOLD       => 'msttcorefonts/Arial_Black.ttf',
80
                Configs::FS_ITALIC     => 'ariali.ttf',
81
                Configs::FS_BOLDITALIC => 'arialbi.ttf',
82
            ],
83
            Configs::FF_VERA             => [
84
                Configs::FS_NORMAL     => 'Vera.ttf',
85
                Configs::FS_BOLD       => 'VeraBd.ttf',
86
                Configs::FS_ITALIC     => 'VeraIt.ttf',
87
                Configs::FS_BOLDITALIC => 'VeraBI.ttf',
88
            ],
89
            Configs::FF_VERAMONO         => [
90
                Configs::FS_NORMAL     => 'VeraMono.ttf',
91
                Configs::FS_BOLD       => 'VeraMoBd.ttf',
92
                Configs::FS_ITALIC     => 'VeraMoIt.ttf',
93
                Configs::FS_BOLDITALIC => 'VeraMoBI.ttf',
94
            ],
95
            Configs::FF_VERASERIF        => [
96
                Configs::FS_NORMAL     => 'VeraSe.ttf',
97
                Configs::FS_BOLD       => 'VeraSeBd.ttf',
98
                Configs::FS_ITALIC     => '',
99
                Configs::FS_BOLDITALIC => '',
100
            ],
101
102
            /* Chinese fonts */
103
            Configs::FF_SIMSUN           => [
104
                Configs::FS_NORMAL     => 'simsun.ttc',
105
                Configs::FS_BOLD       => 'simhei.ttf',
106
                Configs::FS_ITALIC     => '',
107
                Configs::FS_BOLDITALIC => '',
108
            ],
109
            Configs::FF_CHINESE          => [
110
                Configs::FS_NORMAL     => Configs::CHINESE_TTF_FONT,
111
                Configs::FS_BOLD       => '',
112
                Configs::FS_ITALIC     => '',
113
                Configs::FS_BOLDITALIC => '',
114
            ],
115
            Configs::FF_BIG5             => [
116
                Configs::FS_NORMAL     => Configs::CHINESE_TTF_FONT,
117
                Configs::FS_BOLD       => '',
118
                Configs::FS_ITALIC     => '',
119
                Configs::FS_BOLDITALIC => '',
120
            ],
121
122
            /* Japanese fonts */
123
            Configs::FF_MINCHO           => [
124
                Configs::FS_NORMAL     => Configs::MINCHO_TTF_FONT,
125
                Configs::FS_BOLD       => '',
126
                Configs::FS_ITALIC     => '',
127
                Configs::FS_BOLDITALIC => '',
128
            ],
129
130
            Configs::FF_PMINCHO          => [
131
                Configs::FS_NORMAL     => Configs::PMINCHO_TTF_FONT,
132
                Configs::FS_BOLD       => '',
133
                Configs::FS_ITALIC     => '',
134
                Configs::FS_BOLDITALIC => '',
135
            ],
136
137
            Configs::FF_GOTHIC           => [
138
                Configs::FS_NORMAL     => Configs::GOTHIC_TTF_FONT,
139
                Configs::FS_BOLD       => '',
140
                Configs::FS_ITALIC     => '',
141
                Configs::FS_BOLDITALIC => '',
142
            ],
143
144
            Configs::FF_PGOTHIC          => [
145
                Configs::FS_NORMAL     => Configs::PGOTHIC_TTF_FONT,
146
                Configs::FS_BOLD       => '',
147
                Configs::FS_ITALIC     => '',
148
                Configs::FS_BOLDITALIC => '',
149
            ],
150
151
            /* Hebrew fonts */
152
            Configs::FF_DAVID            => [
153
                Configs::FS_NORMAL     => 'DAVIDNEW.TTF',
154
                Configs::FS_BOLD       => '',
155
                Configs::FS_ITALIC     => '',
156
                Configs::FS_BOLDITALIC => '',
157
            ],
158
159
            Configs::FF_MIRIAM           => [
160
                Configs::FS_NORMAL     => 'MRIAMY.TTF',
161
                Configs::FS_BOLD       => '',
162
                Configs::FS_ITALIC     => '',
163
                Configs::FS_BOLDITALIC => '',
164
            ],
165
166
            Configs::FF_AHRON            => [
167
                Configs::FS_NORMAL     => 'ahronbd.ttf',
168
                Configs::FS_BOLD       => '',
169
                Configs::FS_ITALIC     => '',
170
                Configs::FS_BOLDITALIC => '',
171
            ],
172
173
            /* Misc fonts */
174
            Configs::FF_DIGITAL          => [
175
                Configs::FS_NORMAL     => 'DIGIRU__.TTF',
176
                Configs::FS_BOLD       => 'Digirtu_.ttf',
177
                Configs::FS_ITALIC     => 'Digir___.ttf',
178
                Configs::FS_BOLDITALIC => 'DIGIRT__.TTF',
179
            ],
180
181
            /* This is an experimental font for the speedometer development
182
            Configs::FF_SPEEDO =>    array(
183
            Configs::FS_NORMAL =>'Speedo.ttf',
184
            Configs::FS_BOLD =>'',
185
            Configs::FS_ITALIC =>'',
186
            Configs::FS_BOLDITALIC =>''
187
            ),
188
             */
189
190
            Configs::FF_COMPUTER         => [
191
                Configs::FS_NORMAL     => 'COMPUTER.TTF',
192
                Configs::FS_BOLD       => '',
193
                Configs::FS_ITALIC     => '',
194
                Configs::FS_BOLDITALIC => '',
195
            ],
196
197
            Configs::FF_CALCULATOR       => [
198
                Configs::FS_NORMAL     => 'Triad_xs.ttf',
199
                Configs::FS_BOLD       => '',
200
                Configs::FS_ITALIC     => '',
201
                Configs::FS_BOLDITALIC => '',
202
            ],
203
204
            /* Dejavu fonts */
205
            Configs::FF_DV_SANSSERIF     => [
206
                Configs::FS_NORMAL     => [
207
                    'DejaVuSans.ttf',
208
                ],
209
                Configs::FS_BOLD       => [
210
                    'DejaVuSans-Bold.ttf', 'DejaVuSansBold.ttf',
211
                ],
212
                Configs::FS_ITALIC     => [
213
                    'DejaVuSans-Oblique.ttf', 'DejaVuSansOblique.ttf',
214
                ],
215
                Configs::FS_BOLDITALIC => [
216
                    'DejaVuSans-BoldOblique.ttf', 'DejaVuSansBoldOblique.ttf',
217
                ],
218
            ],
219
220
            Configs::FF_DV_SANSSERIFMONO => [
221
                Configs::FS_NORMAL     => [
222
                    'DejaVuSansMono.ttf', 'DejaVuMonoSans.ttf',
223
                ],
224
                Configs::FS_BOLD       => [
225
                    'DejaVuSansMono-Bold.ttf', 'DejaVuMonoSansBold.ttf',
226
                ],
227
                Configs::FS_ITALIC     => [
228
                    'DejaVuSansMono-Oblique.ttf', 'DejaVuMonoSansOblique.ttf',
229
                ],
230
                Configs::FS_BOLDITALIC => [
231
                    'DejaVuSansMono-BoldOblique.ttf', 'DejaVuMonoSansBoldOblique.ttf',
232
                ],
233
            ],
234
235
            Configs::FF_DV_SANSSERIFCOND => [
236
                Configs::FS_NORMAL     => [
237
                    'DejaVuSansCondensed.ttf', 'DejaVuCondensedSans.ttf',
238
                ],
239
                Configs::FS_BOLD       => [
240
                    'DejaVuSansCondensed-Bold.ttf', 'DejaVuCondensedSansBold.ttf',
241
                ],
242
                Configs::FS_ITALIC     => [
243
                    'DejaVuSansCondensed-Oblique.ttf', 'DejaVuCondensedSansOblique.ttf',
244
                ],
245
                Configs::FS_BOLDITALIC => [
246
                    'DejaVuSansCondensed-BoldOblique.ttf', 'DejaVuCondensedSansBoldOblique.ttf',
247
                ],
248
            ],
249
250
            Configs::FF_DV_SERIF         => [
251
                Configs::FS_NORMAL     => [
252
                    'DejaVuSerif.ttf',
253
                ],
254
                Configs::FS_BOLD       => [
255
                    'DejaVuSerif-Bold.ttf', 'DejaVuSerifBold.ttf',
256
                ],
257
                Configs::FS_ITALIC     => [
258
                    'DejaVuSerif-Italic.ttf', 'DejaVuSerifItalic.ttf',
259
                ],
260
                Configs::FS_BOLDITALIC => [
261
                    'DejaVuSerif-BoldItalic.ttf', 'DejaVuSerifBoldItalic.ttf',
262
                ],
263
            ],
264
265
            Configs::FF_DV_SERIFCOND     => [
266
                Configs::FS_NORMAL     => [
267
                    'DejaVuSerifCondensed.ttf', 'DejaVuCondensedSerif.ttf',
268
                ],
269
                Configs::FS_BOLD       => [
270
                    'DejaVuSerifCondensed-Bold.ttf', 'DejaVuCondensedSerifBold.ttf',
271
                ],
272
                Configs::FS_ITALIC     => [
273
                    'DejaVuSerifCondensed-Italic.ttf', 'DejaVuCondensedSerifItalic.ttf',
274
                ],
275
                Configs::FS_BOLDITALIC => [
276
                    'DejaVuSerifCondensed-BoldItalic.ttf', 'DejaVuCondensedSerifBoldItalic.ttf',
277
                ],
278
            ],
279
280
            /* Placeholders for defined fonts */
281
            Configs::FF_USERFONT1        => [
282
                Configs::FS_NORMAL     => '',
283
                Configs::FS_BOLD       => '',
284
                Configs::FS_ITALIC     => '',
285
                Configs::FS_BOLDITALIC => '',
286
            ],
287
288
            Configs::FF_USERFONT2        => [
289
                Configs::FS_NORMAL     => '',
290
                Configs::FS_BOLD       => '',
291
                Configs::FS_ITALIC     => '',
292
                Configs::FS_BOLDITALIC => '',
293
            ],
294
295
            Configs::FF_USERFONT3        => [
296
                Configs::FS_NORMAL     => '',
297
                Configs::FS_BOLD       => '',
298
                Configs::FS_ITALIC     => '',
299
                Configs::FS_BOLDITALIC => '',
300
            ],
301
        ];
302
    }
303
304
    /**
305
     * Encapsulates the logic to check for a file existance
306
     * If it exists and it's readable, return full path. Otherwise return false.
307
     *
308
     * @param <type> $file      The file
0 ignored issues
show
Documentation Bug introduced by
The doc comment <type> at position 0 could not be parsed: Unknown type name '<' at position 0 in <type>.
Loading history...
309
     * @param bool   $folder    The folder
310
     * @param mixed  $font_file
311
     * @param mixed  $font_path
312
     *
313
     * @return bool the full path if exists
314
     */
315
    private static function getFullPathIfExists($font_file, $font_path = '')
316
    {
317
        $full_path = sprintf('%s%s', $font_path, $font_file);
318
        //kdump($full_path);
319
320
        if (file_exists($full_path) && is_readable($full_path)) {
321
            return $full_path;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $full_path returns the type string which is incompatible with the documented return type boolean.
Loading history...
322
        }
323
        return false;
324
    }
325
326
    /**
327
     * Given a font family and a style, returns the full path to the font
328
     * Please check your DoxyDoxygen user configuration file.
329
     *
330
     * @param mixed  $family    font family (e.g. Configs::FF_COURIER, Configs::FF_VERDANA)
331
     * @param mixed  $style     optional font style, defaults to  Configs::FS_NORMAL
332
     * @param string $font_path optional font_path. If set, it takes precedence over other paths
333
     *
334
     * @example ` $ttf->File(Configs::FF_DV_SANSSERIF, Configs::FS_BOLD); // would return <self::LIBRARY ROOT>/self::src/fonts/DejaVuSans-Bold-ttf
335
     * ` */
336
    public function File($family, $style = Configs::FS_NORMAL, $font_path = null)
337
    {
338
        $font_translation = null;
339
        $fam              = @$this->font_files[$family];
340
        if (array_key_exists($family, Configs::$font_dict) &&
341
            array_key_exists($style, Configs::$font_dict)
342
            // && !array_key_exists($family,$this->font_files)
343
        ) {
344
            $font_translation = sprintf('%s::%s',
345
                Configs::$font_dict[$family],
346
                Configs::$font_dict[$style]
347
            );
348
            if (array_key_exists($font_translation, Configs::$FOUND_FONTS)) {
349
                return Configs::$FOUND_FONTS[$font_translation];
350
            }
351
352
        }
353
        if (!$fam) {
354
            Util\JpGraphError::RaiseL(25046, $family); //("Specified TTF font family (id=$family) is unknown or does not exist. Please note that TTF fonts are not distributed with JpGraph for copyright reasons. You can find the MS TTF WEB-fonts (arial, courier etc) for download at http://corefonts.sourceforge.net/");
355
        }
356
        $ff = @$fam[$style];
357
358
        // There are several optional file names. They are tried in order
359
        // and the first one found is used
360
        if (!is_array($ff)) {
361
            $ff = [$ff];
362
        }
363
364
        $jpgraph_font_dir = dirname(dirname(__FILE__)) . '/fonts/';
0 ignored issues
show
Unused Code introduced by
The assignment to $jpgraph_font_dir is dead and can be removed.
Loading history...
365
366
        foreach ($ff as $font_file) {
367
            // All font families are guaranteed to have the normal style
368
369
            if ($font_file === '') {
370
                Util\JpGraphError::RaiseL(25047, $this->style_names[$style], $this->font_files[$family][Configs::FS_NORMAL]);
371
            }
372
            //('Style "'.$this->style_names[$style].'" is not available for font family '.$this->font_files[$family][Configs::FS_NORMAL].'.');
373
            if (!$font_file) {
374
                Util\JpGraphError::RaiseL(25048, $fam); //("Unknown font style specification [$fam].");
0 ignored issues
show
Bug introduced by
$fam of type array<integer,array<inte...>|array<integer,string> is incompatible with the type string expected by parameter $a1 of Amenadiel\JpGraph\Util\JpGraphError::RaiseL(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

374
                Util\JpGraphError::RaiseL(25048, /** @scrutinizer ignore-type */ $fam); //("Unknown font style specification [$fam].");
Loading history...
375
            }
376
377
            if ($font_candidate = self::getFullPathIfExists($font_file, $font_path)) {
378
                $font_file = $font_candidate;
379
380
                break;
381
            }
382
            if ($font_candidate = self::getFullPathIfExists($font_file, self::$FONT_BASEPATH)) {
383
                $font_file = $font_candidate;
384
385
                break;
386
            }
387
388
            // check OS font dir
389
            if ($family >= Configs::FF_MINCHO && $family <= Configs::FF_PGOTHIC) {
390
                $font_file = Configs::MBTTF_DIR . $font_file;
391
            } else {
392
                $font_file = Configs::TTF_DIR . $font_file;
393
            }
394
            if (file_exists($font_file) === true && is_readable($font_file) === true) {
395
                break;
396
            }
397
        }
398
399
        if (!file_exists($font_file)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $font_file seems to be defined by a foreach iteration on line 366. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
400
            //Util\JpGraphError::RaiseL(25049, $font_file); //("Font file \"$font_file\" is not readable or does not exist.");
401
            // Fallback to FF_DV_SANSSERIF
402
            // which is DejaVuSans in Ubuntu
403
            $font_file = $this->File(Configs::FF_DV_SANSSERIF, $style);
404
        }
405
        // Try to use the canonical path
406
        if (is_readable(realpath($font_file))) {
407
            $font_file = realpath($font_file);
408
        }
409
        if ($font_translation) {
410
            // Will store found font to skip next search
411
            Configs::$FOUND_FONTS[$font_translation] = $font_file;
412
            /*kdump([
413
        'family'      => sprintf('%s (%d)', Configs::$font_dict[$family], $family),
414
        'style'       => sprintf('%s (%d)', Configs::$font_dict[$style], $style),
415
        'ff'          => $fam[$style],
416
        'FOUND_FONTS' => Configs::$FOUND_FONTS,
417
        ]);*/
418
        }
419
        return $font_file;
420
    }
421
422
    public function SetUserFont($aNormal, $aBold = '', $aItalic = '', $aBoldIt = '')
423
    {
424
        $this->font_files[Configs::FF_USERFONT] =
425
            [
426
            Configs::FS_NORMAL     => $aNormal,
427
            Configs::FS_BOLD       => $aBold,
428
            Configs::FS_ITALIC     => $aItalic,
429
            Configs::FS_BOLDITALIC => $aBoldIt,
430
        ];
431
    }
432
433
    public function SetUserFont1($aNormal, $aBold = '', $aItalic = '', $aBoldIt = '')
434
    {
435
        $this->font_files[Configs::FF_USERFONT1] =
436
            [
437
            Configs::FS_NORMAL     => $aNormal,
438
            Configs::FS_BOLD       => $aBold,
439
            Configs::FS_ITALIC     => $aItalic,
440
            Configs::FS_BOLDITALIC => $aBoldIt,
441
        ];
442
    }
443
444
    public function SetUserFont2($aNormal, $aBold = '', $aItalic = '', $aBoldIt = '')
445
    {
446
        $this->font_files[Configs::FF_USERFONT2] =
447
            [
448
            Configs::FS_NORMAL     => $aNormal,
449
            Configs::FS_BOLD       => $aBold,
450
            Configs::FS_ITALIC     => $aItalic,
451
            Configs::FS_BOLDITALIC => $aBoldIt,
452
        ];
453
    }
454
455
    public function SetUserFont3($aNormal, $aBold = '', $aItalic = '', $aBoldIt = '')
456
    {
457
        $this->font_files[Configs::FF_USERFONT3] =
458
            [
459
            Configs::FS_NORMAL     => $aNormal,
460
            Configs::FS_BOLD       => $aBold,
461
            Configs::FS_ITALIC     => $aItalic,
462
            Configs::FS_BOLDITALIC => $aBoldIt,
463
        ];
464
    }
465
}
466
467
// @class
468