-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathSize.php
More file actions
212 lines (188 loc) · 5.79 KB
/
Size.php
File metadata and controls
212 lines (188 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
namespace Sabberworm\CSS\Value;
use Sabberworm\CSS\OutputFormat;
use Sabberworm\CSS\Parsing\ParserState;
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
/**
* A `Size` consists of a numeric `size` value and a unit.
*/
class Size extends PrimitiveValue
{
/**
* vh/vw/vm(ax)/vmin/rem/svh/lvh/dvh/svw/lvw/dvw are absolute insofar as they don’t scale to the immediate parent (only the viewport)
*
* @var array<int, string>
*/
private const ABSOLUTE_SIZE_UNITS = [
'px', 'pt', 'pc',
'cm', 'mm', 'mozmm', 'in',
'vh', 'dvh', 'svh', 'lvh',
'vw', 'vmin', 'vmax', 'rem',
'svw', 'lvw', 'dvw',
];
/**
* @var array<int, string>
*/
private const RELATIVE_SIZE_UNITS = ['%', 'em', 'ex', 'ch', 'fr', 'cqw', 'cqh', 'cqi', 'cqb', 'cqmin', 'cqmax'];
/**
* @var array<int, string>
*/
private const NON_SIZE_UNITS = ['deg', 'grad', 'rad', 's', 'ms', 'turn', 'Hz', 'kHz'];
/**
* @var array<int, array<string, string>>|null
*/
private static $SIZE_UNITS = null;
/**
* @var float
*/
private $fSize;
/**
* @var string|null
*/
private $sUnit;
/**
* @var bool
*/
private $bIsColorComponent;
/**
* @param float|int|string $fSize
* @param string|null $sUnit
* @param bool $bIsColorComponent
* @param int $iLineNo
*/
public function __construct($fSize, $sUnit = null, $bIsColorComponent = false, $iLineNo = 0)
{
parent::__construct($iLineNo);
$this->fSize = (float) $fSize;
$this->sUnit = $sUnit;
$this->bIsColorComponent = $bIsColorComponent;
}
/**
* @param bool $bIsColorComponent
*
* @throws UnexpectedEOFException
* @throws UnexpectedTokenException
*/
public static function parse(ParserState $oParserState, $bIsColorComponent = false): Size
{
$sSize = '';
if ($oParserState->comes('-')) {
$sSize .= $oParserState->consume('-');
}
while (\is_numeric($oParserState->peek()) || $oParserState->comes('.') || $oParserState->comes('e', true)) {
if ($oParserState->comes('.')) {
$sSize .= $oParserState->consume('.');
} elseif ($oParserState->comes('e', true)) {
$sLookahead = $oParserState->peek(1, 1);
if (\is_numeric($sLookahead) || $sLookahead === '+' || $sLookahead === '-') {
$sSize .= $oParserState->consume(2);
} else {
break; // Reached the unit part of the number like "em" or "ex"
}
} else {
$sSize .= $oParserState->consume(1);
}
}
$sUnit = null;
$aSizeUnits = self::getSizeUnits();
foreach ($aSizeUnits as $iLength => &$aValues) {
$sKey = \strtolower($oParserState->peek($iLength));
if (\array_key_exists($sKey, $aValues)) {
if (($sUnit = $aValues[$sKey]) !== null) {
$oParserState->consume($iLength);
break;
}
}
}
return new Size((float) $sSize, $sUnit, $bIsColorComponent, $oParserState->currentLine());
}
/**
* @return array<int, array<string, string>>
*/
private static function getSizeUnits()
{
if (!\is_array(self::$SIZE_UNITS)) {
self::$SIZE_UNITS = [];
foreach (\array_merge(self::ABSOLUTE_SIZE_UNITS, self::RELATIVE_SIZE_UNITS, self::NON_SIZE_UNITS) as $val) {
$iSize = \strlen($val);
if (!isset(self::$SIZE_UNITS[$iSize])) {
self::$SIZE_UNITS[$iSize] = [];
}
self::$SIZE_UNITS[$iSize][\strtolower($val)] = $val;
}
\krsort(self::$SIZE_UNITS, SORT_NUMERIC);
}
return self::$SIZE_UNITS;
}
/**
* @param string $sUnit
*/
public function setUnit($sUnit): void
{
$this->sUnit = $sUnit;
}
/**
* @return string|null
*/
public function getUnit()
{
return $this->sUnit;
}
/**
* @param float|int|string $fSize
*/
public function setSize($fSize): void
{
$this->fSize = (float) $fSize;
}
/**
* @return float
*/
public function getSize()
{
return $this->fSize;
}
/**
* @return bool
*/
public function isColorComponent()
{
return $this->bIsColorComponent;
}
/**
* Returns whether the number stored in this Size really represents a size (as in a length of something on screen).
*
* @return false if the unit an angle, a duration, a frequency or the number is a component in a Color object.
*/
public function isSize(): bool
{
if (\in_array($this->sUnit, self::NON_SIZE_UNITS, true)) {
return false;
}
return !$this->isColorComponent();
}
public function isRelative(): bool
{
if (\in_array($this->sUnit, self::RELATIVE_SIZE_UNITS, true)) {
return true;
}
if ($this->sUnit === null && $this->fSize != 0) {
return true;
}
return false;
}
public function __toString(): string
{
return $this->render(new OutputFormat());
}
public function render(OutputFormat $oOutputFormat): string
{
$l = \localeconv();
$sPoint = \preg_quote($l['decimal_point'], '/');
$sSize = \preg_match('/[\\d\\.]+e[+-]?\\d+/i', (string) $this->fSize)
? \preg_replace("/$sPoint?0+$/", '', \sprintf('%f', $this->fSize)) : $this->fSize;
return \preg_replace(["/$sPoint/", '/^(-?)0\\./'], ['.', '$1.'], $sSize)
. ($this->sUnit ?? '');
}
}