Repository navigation
Expand file tree
/
Copy pathTypography.php
More file actions
384 lines (302 loc) · 11.9 KB
/
Copy pathTypography.php
File metadata and controls
384 lines (302 loc) · 11.9 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<?php
declare(strict_types=1);
namespace ItalyStrap\ThemeJsonGenerator\Settings;
use ItalyStrap\ThemeJsonGenerator\Schema\ThemeSchemaCoverage;
use ItalyStrap\ThemeJsonGenerator\Settings;
use ItalyStrap\ThemeJsonGenerator\Settings\Typography\Fluid;
use ItalyStrap\ThemeJsonGenerator\Settings\Typography\FontFamily;
use ItalyStrap\ThemeJsonGenerator\Settings\Typography\FontSize;
use ItalyStrap\ThemeJsonGenerator\Settings\Typography\TextShadow;
use ItalyStrap\ThemeJsonGenerator\Settings\Typography\Utilities\Fluid as FontSizeFluid;
use ItalyStrap\ThemeJsonGenerator\Settings\Typography\Utilities\FontFace;
final readonly class Typography
{
use ScopedSettingsWriterTrait;
public const SECTION = 'typography';
public const CUSTOM_FONT_SIZE = 'customFontSize';
public const DEFAULT_FONT_SIZES = 'defaultFontSizes';
public const DEFAULT_TEXT_SHADOW_PRESETS = 'defaultTextShadowPresets';
public const TEXT_SHADOW = 'textShadow';
public const DROP_CAP = 'dropCap';
public const FLUID = 'fluid';
public const FONT_STYLE = 'fontStyle';
public const FONT_WEIGHT = 'fontWeight';
public const LETTER_SPACING = 'letterSpacing';
public const LINE_HEIGHT = 'lineHeight';
public const TEXT_ALIGN = 'textAlign';
public const TEXT_COLUMNS = 'textColumns';
public const TEXT_DECORATION = 'textDecoration';
public const TEXT_INDENT = 'textIndent';
public const TEXT_TRANSFORM = 'textTransform';
public const WRITING_MODE = 'writingMode';
public function __construct(
private Settings $settings,
) {
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, self::DEFAULT_FONT_SIZES])]
public function enableDefaultFontSizes(): self
{
return $this->setBoolean(self::DEFAULT_FONT_SIZES, true);
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, self::DEFAULT_FONT_SIZES])]
public function disableDefaultFontSizes(): self
{
return $this->setBoolean(self::DEFAULT_FONT_SIZES, false);
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, self::TEXT_SHADOW])]
public function enableTextShadow(): self
{
return $this->setBoolean(self::TEXT_SHADOW, true);
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, self::TEXT_SHADOW])]
public function disableTextShadow(): self
{
return $this->setBoolean(self::TEXT_SHADOW, false);
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, self::DEFAULT_TEXT_SHADOW_PRESETS])]
public function enableDefaultTextShadowPresets(): self
{
return $this->setBoolean(self::DEFAULT_TEXT_SHADOW_PRESETS, true);
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, self::DEFAULT_TEXT_SHADOW_PRESETS])]
public function disableDefaultTextShadowPresets(): self
{
return $this->setBoolean(self::DEFAULT_TEXT_SHADOW_PRESETS, false);
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, TextShadow::SECTION])]
public function addTextShadow(string $slug, string $name, string $textShadow): self
{
$this->settings->addPreset(new TextShadow($slug, $name, $textShadow));
return $this;
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, self::CUSTOM_FONT_SIZE])]
public function enableCustomFontSize(): self
{
return $this->setBoolean(self::CUSTOM_FONT_SIZE, true);
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, self::CUSTOM_FONT_SIZE])]
public function disableCustomFontSize(): self
{
return $this->setBoolean(self::CUSTOM_FONT_SIZE, false);
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, self::FONT_STYLE])]
public function enableFontStyle(): self
{
return $this->setBoolean(self::FONT_STYLE, true);
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, self::FONT_STYLE])]
public function disableFontStyle(): self
{
return $this->setBoolean(self::FONT_STYLE, false);
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, self::FONT_WEIGHT])]
public function enableFontWeight(): self
{
return $this->setBoolean(self::FONT_WEIGHT, true);
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, self::FONT_WEIGHT])]
public function disableFontWeight(): self
{
return $this->setBoolean(self::FONT_WEIGHT, false);
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, self::FLUID])]
public function enableFluid(): self
{
return $this->setFluidBoolean(true);
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, self::FLUID])]
public function disableFluid(): self
{
return $this->setFluidBoolean(false);
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, Fluid::SECTION])]
public function fluid(): Fluid
{
return new Fluid($this);
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, self::LETTER_SPACING])]
public function enableLetterSpacing(): self
{
return $this->setBoolean(self::LETTER_SPACING, true);
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, self::LETTER_SPACING])]
public function disableLetterSpacing(): self
{
return $this->setBoolean(self::LETTER_SPACING, false);
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, self::LINE_HEIGHT])]
public function enableLineHeight(): self
{
return $this->setBoolean(self::LINE_HEIGHT, true);
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, self::LINE_HEIGHT])]
public function disableLineHeight(): self
{
return $this->setBoolean(self::LINE_HEIGHT, false);
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, self::TEXT_INDENT])]
public function textIndent(string $textIndent): self
{
if (!\in_array($textIndent, ['subsequent', 'all'], true)) {
throw new \InvalidArgumentException(
\sprintf('Expected text indent "subsequent" or "all", got "%s".', $textIndent)
);
}
$this->set(self::TEXT_INDENT, $textIndent);
return $this;
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, self::TEXT_INDENT])]
public function disableTextIndent(): self
{
return $this->setBoolean(self::TEXT_INDENT, false);
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, self::TEXT_ALIGN])]
public function enableTextAlign(): self
{
return $this->setBoolean(self::TEXT_ALIGN, true);
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, self::TEXT_ALIGN])]
public function disableTextAlign(): self
{
return $this->setBoolean(self::TEXT_ALIGN, false);
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, self::TEXT_COLUMNS])]
public function enableTextColumns(): self
{
return $this->setBoolean(self::TEXT_COLUMNS, true);
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, self::TEXT_COLUMNS])]
public function disableTextColumns(): self
{
return $this->setBoolean(self::TEXT_COLUMNS, false);
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, self::TEXT_DECORATION])]
public function enableTextDecoration(): self
{
return $this->setBoolean(self::TEXT_DECORATION, true);
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, self::TEXT_DECORATION])]
public function disableTextDecoration(): self
{
return $this->setBoolean(self::TEXT_DECORATION, false);
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, self::WRITING_MODE])]
public function enableWritingMode(): self
{
return $this->setBoolean(self::WRITING_MODE, true);
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, self::WRITING_MODE])]
public function disableWritingMode(): self
{
return $this->setBoolean(self::WRITING_MODE, false);
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, self::TEXT_TRANSFORM])]
public function enableTextTransform(): self
{
return $this->setBoolean(self::TEXT_TRANSFORM, true);
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, self::TEXT_TRANSFORM])]
public function disableTextTransform(): self
{
return $this->setBoolean(self::TEXT_TRANSFORM, false);
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, self::DROP_CAP])]
public function enableDropCap(): self
{
return $this->setBoolean(self::DROP_CAP, true);
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, self::DROP_CAP])]
public function disableDropCap(): self
{
return $this->setBoolean(self::DROP_CAP, false);
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, FontFamily::SECTION])]
public function addFontFamily(string $slug, string $name, string $fontFamily, FontFace ...$fontFace): self
{
$this->settings->addPreset(new FontFamily($slug, $name, $fontFamily, ...$fontFace));
return $this;
}
#[ThemeSchemaCoverage([Settings::SECTION, self::SECTION, FontSize::SECTION])]
public function addFontSize(
string $slug,
string $name,
string $size,
FontSizeFluid|false|null $fluid = null
): self {
$this->guardAgainstFluidClampConflict($size, $fluid);
$this->settings->addPreset(new FontSize($slug, $name, $size, $fluid));
return $this;
}
public function writeFluidConfig(string $property, string $value): bool
{
$fluid = $this->settings->read([self::SECTION, self::FLUID]);
if (\is_bool($fluid)) {
throw new \LogicException(
'Global fluid typography is already configured as a boolean and cannot be replaced with an object.'
);
}
$this->guardAgainstRegisteredClampFontSizes();
return $this->set([self::FLUID, $property], $value);
}
private function setFluidBoolean(bool $value): self
{
$fluid = $this->settings->read([self::SECTION, self::FLUID]);
if (\is_array($fluid)) {
throw new \LogicException(
'Global fluid typography is already configured and cannot be replaced with a boolean.'
);
}
if ($value) {
$this->guardAgainstRegisteredClampFontSizes();
}
return $this->setBoolean(self::FLUID, $value);
}
private function guardAgainstFluidClampConflict(
string $size,
FontSizeFluid|false|null $fontSizeFluid
): void {
if ($fontSizeFluid === false) {
return;
}
if (!$this->containsClamp($size)) {
return;
}
$fluid = $this->settings->read([self::SECTION, self::FLUID], false);
if ($fluid === false || $fluid === null) {
return;
}
throw new \InvalidArgumentException(
'Fluid typography cannot be applied to a font size that already uses clamp().'
);
}
private function guardAgainstRegisteredClampFontSizes(): void
{
if (!$this->containsConflictingFontSize($this->settings->readPresets(FontSize::TYPE))) {
return;
}
throw new \InvalidArgumentException(
'Fluid typography cannot be applied to a font size that already uses clamp().'
);
}
private function containsConflictingFontSize(mixed $fontSizes): bool
{
if ($fontSizes instanceof FontSize) {
$fontSize = $fontSizes->toArray();
return ($fontSize['fluid'] ?? null) !== false
&& $this->containsClamp($fontSize['size']);
}
if (!\is_array($fontSizes)) {
return false;
}
foreach ($fontSizes as $fontSize) {
if ($this->containsConflictingFontSize($fontSize)) {
return true;
}
}
return false;
}
private function containsClamp(string $value): bool
{
return \stripos($value, 'clamp(') !== false;
}
}