Completed
Push — master ( 9b7d4c...b9a359 )
by Stefan
03:01
created

WidthCollection::get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
rs 9.4285
cc 3
eloc 6
nc 3
nop 2
crap 3.0416
1
<?php
2
3
namespace OneSheet\Width;
4
5
/**
6
 * Class WidthCollection
7
 *
8
 * @package OneSheet
9
 */
10
class WidthCollection
11
{
12
    /**
13
     * Array containing character widths for each font & size.
14
     *
15
     * @var array
16
     */
17
    private static $widths = array();
18
19
    /**
20
     * Create character width map for each font.
21
     */
22 6
    public function __construct()
23
    {
24 6
        self::loadWidthsFromCsv(dirname(__FILE__) . '/widths.csv');
25 6
    }
26
27
    /**
28
     * Dirty way to allow developers to load character widths that
29
     * are not yet included.
30
     *
31
     * @param string $csvPath
32
     */
33 6
    public static function loadWidthsFromCsv($csvPath)
34
    {
35 6
        foreach (file($csvPath) as $line) {
36 6
            $widths = explode(',', trim($line));
37 6
            if (count(range(33, 126)) + 2 == count($widths)) {
38 6
                self::$widths[array_shift($widths)][array_shift($widths)] =
39 6
                    array_combine(array_map('chr', range(33, 126)), $widths);
40 6
            }
41 6
        }
42 6
    }
43
44
    /**
45
     * Return character widths for given font name.
46
     *
47
     * @param string $fontName
48
     * @param int    $fontSize
49
     * @return array
50
     */
51 4
    public function get($fontName, $fontSize)
52
    {
53 4
        if (isset(self::$widths[$fontName][$fontSize])) {
54 4
            return self::$widths[$fontName][$fontSize];
55 1
        } elseif (isset(self::$widths['Calibri'][$fontSize])) {
56 1
            return self::$widths['Calibri'][$fontSize];
57
        }
58
59
        return self::$widths['Calibri'][11];
60
    }
61
}
62