Completed
Push — master ( 7c1f6c...b55f79 )
by Stefan
03:19
created

SizeCollection::getBaseSizes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 8
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 SizeCollection to build and hold widths & heights
7
 * of individual characters.
8
 *
9
 * @package OneSheet
10
 */
11
class SizeCollection
12
{
13
    /**
14
     * Constant for default character size.
15
     */
16
    const BASE_SIZE = 12;
17
18
    /**
19
     * Array containing character widths/heights for each font & size.
20
     *
21
     * @var array
22
     */
23
    private $sizes = array();
24
25
    /**
26
     * SizeCollection constructor.
27
     */
28 19
    public function __construct()
29
    {
30 19
        $this->loadSizesFromCsv();
31 19
    }
32
33
    /**
34
     * Return character sizes for given font name.
35
     *
36
     * @param string $fontName
37
     * @param int    $fontSize
38
     * @return array
39
     */
40 9
    public function get($fontName, $fontSize)
41
    {
42 9
        if (isset($this->sizes[$fontName][$fontSize])) {
43 5
            return $this->sizes[$fontName][$fontSize];
44
        }
45
46 9
        return $this->calculate($fontName, $fontSize);
47
    }
48
49
    /**
50
     * Calculate character widths based on font name and size.
51
     *
52
     * @param string $fontName
53
     * @param int    $fontSize
54
     * @return array
55
     */
56 9
    private function calculate($fontName, $fontSize)
57
    {
58 9
        foreach ($this->getBaseSizes($fontName) as $character => $size) {
59 9
            if ('bold' !== $character) {
60 9
                $size = round($size / self::BASE_SIZE * $fontSize, 3);
61 9
            }
62 9
            $this->sizes[$fontName][$fontSize][$character] = $size;
63 9
        }
64
65 9
        return $this->sizes[$fontName][$fontSize];
66
    }
67
68
    /**
69
     * Get character base widths by font name or default.
70
     *
71
     * @param string $fontName
72
     * @return array
73
     */
74 9
    private function getBaseSizes($fontName)
75
    {
76 9
        if (isset($this->sizes[$fontName])) {
77 9
            return $this->sizes[$fontName][self::BASE_SIZE];
78
        }
79
80 2
        return $this->sizes['Calibri'][self::BASE_SIZE];
81
    }
82
83
    /**
84
     * Initialize collection from csv file.
85
     */
86 19
    public function loadSizesFromCsv()
87
    {
88 19
        $fh = fopen(dirname(__FILE__) . '/size_collection.csv', 'r');
89 19
        $head = fgetcsv($fh);
90 19
        unset($head[0], $head[1]);
91
92 19
        while ($row = fgetcsv($fh)) {
93 19
            $this->addSizesToCollection($head, $row);
94 19
        }
95 19
    }
96
97
    /**
98
     * Add character widths for a single font.
99
     *
100
     * @param array $head
101
     * @param array$row
102
     */
103 19
    private function addSizesToCollection(array $head, array $row)
104
    {
105 19
        $fontName = array_shift($row);
106 19
        $fontSize = array_shift($row);
107 19
        $this->sizes[$fontName][$fontSize] = array_combine($head, $row);
108 19
    }
109
}
110