Completed
Pull Request — master (#17)
by Stefan
06:06 queued 03:11
created

SizeCollection::getBaseWidths()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace OneSheet\Size;
4
5
/**
6
 * Class WidthCollection
7
 *
8
 * @package OneSheet
9
 */
10
class SizeCollection
11
{
12
    /**
13
     * Constant for default character size.
14
     */
15
    const BASE_SIZE = 12;
16
17
    /**
18
     * Array containing character widths for each font & size.
19
     *
20
     * @var array
21
     */
22
    private static $widths = array();
23
24
    /**
25
     * Create character width map for each font.
26
     */
27 19
    public function __construct()
28
    {
29 19
        self::loadWidthsFromCsv(dirname(__FILE__) . '/size_collection.csv');
30 19
    }
31
32
    /**
33
     * Dirty way to allow developers to load character widths that
34
     * are not yet included.
35
     *
36
     * @param string $csvPath
37
     */
38 19
    public static function loadWidthsFromCsv($csvPath)
39
    {
40 19
        $fh = fopen($csvPath, 'r');
41 19
        $head = fgetcsv($fh);
42 19
        unset($head[0], $head[1]);
43 19
        while ($row = fgetcsv($fh)) {
44 19
            $fontName = array_shift($row);
45 19
            $fontSize = array_shift($row);
46 19
            self::$widths[$fontName][$fontSize] = array_combine($head, $row);
47 19
        }
48 19
    }
49
50
    /**
51
     * Return character widths for given font name.
52
     *
53
     * @param string $fontName
54
     * @param int    $fontSize
55
     * @return array
56
     */
57 9
    public function get($fontName, $fontSize)
58
    {
59 9
        if (isset(self::$widths[$fontName][$fontSize])) {
60 7
            return self::$widths[$fontName][$fontSize];
61
        }
62
63 7
        return self::calculate($fontName, $fontSize);
64
    }
65
66
    /**
67
     * Calculate character widths based on font name and size.
68
     *
69
     * @param string $fontName
70
     * @param int    $fontSize
71
     * @return array
72
     */
73 7
    private static function calculate($fontName, $fontSize)
74
    {
75 7
        foreach (self::getBaseWidths($fontName) as $character => $width) {
76 7
            if ('bold' !== $character) {
77 7
                $width = round($width / self::BASE_SIZE * $fontSize, 3);
78 7
            }
79 7
            self::$widths[$fontName][$fontSize][$character] = $width;
80 7
        }
81
82 7
        return self::$widths[$fontName][$fontSize];
83
    }
84
85
    /**
86
     * Get character base widths by font name or default.
87
     *
88
     * @param string $fontName
89
     * @return array
90
     */
91 7
    private static function getBaseWidths($fontName)
92
    {
93 7
        if (isset(self::$widths[$fontName])) {
94 5
            return self::$widths[$fontName][self::BASE_SIZE];
95
        }
96 2
        return self::$widths['Calibri'][self::BASE_SIZE];
97
    }
98
}
99