| 1 |
<?php |
| 2 |
|
| 3 |
namespace Svg; |
| 4 |
|
| 5 |
class CssLength |
| 6 |
{ |
| 7 |
/** |
| 8 |
* Array of valid css length units. |
| 9 |
* Should be pre-sorted by unit text length so no earlier length can be |
| 10 |
* contained within a latter (eg. 'in' within 'vmin'). |
| 11 |
* |
| 12 |
* @var string[] |
| 13 |
*/ |
| 14 |
protected static $units = [ |
| 15 |
'vmax', |
| 16 |
'vmin', |
| 17 |
'rem', |
| 18 |
'px', |
| 19 |
'pt', |
| 20 |
'cm', |
| 21 |
'mm', |
| 22 |
'in', |
| 23 |
'pc', |
| 24 |
'em', |
| 25 |
'ex', |
| 26 |
'ch', |
| 27 |
'vw', |
| 28 |
'vh', |
| 29 |
'%', |
| 30 |
'q', |
| 31 |
]; |
| 32 |
|
| 33 |
/** |
| 34 |
* A list of units that are inch-relative, and their unit division within an inch. |
| 35 |
* |
| 36 |
* @var array<string, float> |
| 37 |
*/ |
| 38 |
protected static $inchDivisions = [ |
| 39 |
'in' => 1, |
| 40 |
'cm' => 2.54, |
| 41 |
'mm' => 25.4, |
| 42 |
'q' => 101.6, |
| 43 |
'pc' => 6, |
| 44 |
'pt' => 72, |
| 45 |
]; |
| 46 |
|
| 47 |
/** |
| 48 |
* The CSS length unit indicator. |
| 49 |
* Will be lower-case and one of the units listed in the '$units' array or empty. |
| 50 |
* |
| 51 |
* @var string |
| 52 |
*/ |
| 53 |
protected $unit = ''; |
| 54 |
|
| 55 |
/** |
| 56 |
* The numeric value of the given length. |
| 57 |
* |
| 58 |
* @var float |
| 59 |
*/ |
| 60 |
protected $value = 0; |
| 61 |
|
| 62 |
/** |
| 63 |
* The original unparsed length provided. |
| 64 |
* |
| 65 |
* @var string |
| 66 |
*/ |
| 67 |
protected $unparsed; |
| 68 |
|
| 69 |
public function __construct(string $length) |
| 70 |
{ |
| 71 |
$this->unparsed = $length; |
| 72 |
$this->parseLengthComponents($length); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Parse out the unit and value components from the given string length. |
| 77 |
*/ |
| 78 |
protected function parseLengthComponents(string $length): void |
| 79 |
{ |
| 80 |
$length = strtolower($length); |
| 81 |
|
| 82 |
foreach (self::$units as $unit) { |
| 83 |
$pos = strpos($length, $unit); |
| 84 |
if ($pos) { |
| 85 |
$this->value = floatval(substr($length, 0, $pos)); |
| 86 |
$this->unit = $unit; |
| 87 |
return; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
$this->unit = ''; |
| 92 |
$this->value = floatval($length); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Get the unit type of this css length. |
| 97 |
* Units are standardised to be lower-cased. |
| 98 |
* |
| 99 |
* @return string |
| 100 |
*/ |
| 101 |
public function getUnit(): string |
| 102 |
{ |
| 103 |
return $this->unit; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Get this CSS length in the equivalent pixel count size. |
| 108 |
* |
| 109 |
* @param float $referenceSize |
| 110 |
* @param float $dpi |
| 111 |
* |
| 112 |
* @return float |
| 113 |
*/ |
| 114 |
public function toPixels(float $referenceSize = 11.0, float $dpi = 96.0): float |
| 115 |
{ |
| 116 |
// Standard relative units |
| 117 |
if (in_array($this->unit, ['em', 'rem', 'ex', 'ch'])) { |
| 118 |
return $this->value * $referenceSize; |
| 119 |
} |
| 120 |
|
| 121 |
// Percentage relative units |
| 122 |
if (in_array($this->unit, ['%', 'vw', 'vh', 'vmin', 'vmax'])) { |
| 123 |
return $this->value * ($referenceSize / 100); |
| 124 |
} |
| 125 |
|
| 126 |
// Inch relative units |
| 127 |
if (in_array($this->unit, array_keys(static::$inchDivisions))) { |
| 128 |
$inchValue = $this->value * $dpi; |
| 129 |
$division = static::$inchDivisions[$this->unit]; |
| 130 |
return $inchValue / $division; |
| 131 |
} |
| 132 |
|
| 133 |
return $this->value; |
| 134 |
} |
| 135 |
} |