1 | <?php |
||
13 | class WidthCalculator |
||
14 | { |
||
15 | /** |
||
16 | * @var WidthCollection |
||
17 | */ |
||
18 | private $widthCollection; |
||
19 | |||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | private $characterWidths; |
||
24 | |||
25 | /** |
||
26 | * Calculator constructor. |
||
27 | */ |
||
28 | 6 | public function __construct() |
|
32 | |||
33 | /** |
||
34 | * Returns the estimated width of a cell value. |
||
35 | * |
||
36 | * @param mixed $value |
||
37 | * @param Font $font |
||
38 | * @return float |
||
39 | */ |
||
40 | 3 | public function getCellWidth($value, Font $font) |
|
41 | { |
||
42 | 3 | $width = 0.07 * $font->getSize(); |
|
43 | 3 | foreach (preg_split('~~u', $value, -1, PREG_SPLIT_NO_EMPTY) as $character) { |
|
44 | 3 | if (isset($this->characterWidths[$character])) { |
|
45 | 3 | $width += $this->characterWidths[$character]; |
|
46 | 3 | } elseif (strlen($character)) { |
|
47 | $width += 0.06 * $font->getSize(); |
||
48 | } |
||
49 | 3 | } |
|
50 | |||
51 | 3 | return $font->isBold() ? $width * $this->characterWidths['bold'] : $width; |
|
52 | } |
||
53 | |||
54 | /** |
||
55 | * Set proper font sizes by font. |
||
56 | * |
||
57 | * @param Font $font |
||
58 | */ |
||
59 | 4 | public function setFont(Font $font) |
|
63 | } |
||
64 |