| 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 | * @param WidthCollection $widthCollection |
||
| 29 | */ |
||
| 30 | 16 | public function __construct(WidthCollection $widthCollection) |
|
| 34 | |||
| 35 | /** |
||
| 36 | * Returns the estimated width of a cell value. |
||
| 37 | * |
||
| 38 | * @param mixed $value |
||
| 39 | * @param Font $font |
||
| 40 | * @return float |
||
| 41 | */ |
||
| 42 | 5 | public function getCellWidth($value, Font $font) |
|
| 43 | { |
||
| 44 | 5 | $width = 0.07 * $font->getSize(); |
|
| 45 | 5 | foreach (preg_split('~~u', $value, -1, PREG_SPLIT_NO_EMPTY) as $character) { |
|
| 46 | 5 | if (isset($this->characterWidths[$character])) { |
|
| 47 | 5 | $width += $this->characterWidths[$character]; |
|
| 48 | 5 | } elseif (strlen($character)) { |
|
| 49 | 2 | $width += 0.06 * $font->getSize(); |
|
| 50 | 2 | } |
|
| 51 | 5 | } |
|
| 52 | |||
| 53 | 5 | return $font->isBold() ? $width * $this->characterWidths['bold'] : $width; |
|
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Set proper font sizes by font. |
||
| 58 | * |
||
| 59 | * @param Font $font |
||
| 60 | */ |
||
| 61 | 6 | public function setFont(Font $font) |
|
| 65 | } |
||
| 66 |