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 $widths = array(); |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* SizeCollection constructor. |
26
|
|
|
*/ |
27
|
19 |
|
public function __construct() |
28
|
|
|
{ |
29
|
19 |
|
$this->loadWidthsFromCsv(); |
30
|
19 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Return character widths for given font name. |
34
|
|
|
* |
35
|
|
|
* @param string $fontName |
36
|
|
|
* @param int $fontSize |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
9 |
|
public function get($fontName, $fontSize) |
40
|
|
|
{ |
41
|
9 |
|
if (isset($this->widths[$fontName][$fontSize])) { |
42
|
5 |
|
return $this->widths[$fontName][$fontSize]; |
43
|
|
|
} |
44
|
|
|
|
45
|
9 |
|
return $this->calculate($fontName, $fontSize); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Calculate character widths based on font name and size. |
50
|
|
|
* |
51
|
|
|
* @param string $fontName |
52
|
|
|
* @param int $fontSize |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
9 |
|
private function calculate($fontName, $fontSize) |
56
|
|
|
{ |
57
|
9 |
|
foreach ($this->getBaseWidths($fontName) as $character => $width) { |
58
|
9 |
|
if ('bold' !== $character) { |
59
|
9 |
|
$width = round($width / self::BASE_SIZE * $fontSize, 3); |
60
|
9 |
|
} |
61
|
9 |
|
$this->widths[$fontName][$fontSize][$character] = $width; |
62
|
9 |
|
} |
63
|
|
|
|
64
|
9 |
|
return $this->widths[$fontName][$fontSize]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get character base widths by font name or default. |
69
|
|
|
* |
70
|
|
|
* @param string $fontName |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
9 |
|
private function getBaseWidths($fontName) |
74
|
|
|
{ |
75
|
9 |
|
if (isset($this->widths[$fontName])) { |
76
|
9 |
|
return $this->widths[$fontName][self::BASE_SIZE]; |
77
|
|
|
} |
78
|
2 |
|
return $this->widths['Calibri'][self::BASE_SIZE]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Initialize collection from csv file. |
83
|
|
|
*/ |
84
|
19 |
|
public function loadWidthsFromCsv() |
85
|
|
|
{ |
86
|
19 |
|
$fh = fopen(dirname(__FILE__) . '/size_collection.csv', 'r'); |
87
|
19 |
|
$head = fgetcsv($fh); |
88
|
19 |
|
unset($head[0], $head[1]); |
89
|
|
|
|
90
|
19 |
|
while ($row = fgetcsv($fh)) { |
91
|
19 |
|
$this->addSizesToCollection($head, $row); |
92
|
19 |
|
} |
93
|
19 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Add character widths for a single font. |
97
|
|
|
* |
98
|
|
|
* @param array $head |
99
|
|
|
* @param array$row |
100
|
|
|
*/ |
101
|
19 |
|
private function addSizesToCollection(array $head, array $row) |
102
|
|
|
{ |
103
|
19 |
|
$fontName = array_shift($row); |
104
|
19 |
|
$fontSize = array_shift($row); |
105
|
19 |
|
$this->widths[$fontName][$fontSize] = array_combine($head, $row); |
106
|
19 |
|
} |
107
|
|
|
} |
108
|
|
|
|