-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayoutStyleResolver.php
More file actions
151 lines (120 loc) · 4.75 KB
/
LayoutStyleResolver.php
File metadata and controls
151 lines (120 loc) · 4.75 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
<?php
// SPDX-FileCopyrightText: 2026 LibreSign
// SPDX-License-Identifier: AGPL-3.0-or-later
declare(strict_types=1);
namespace LibreSign\XObjectTemplate\Layout;
use LibreSign\XObjectTemplate\Css\StyleMap;
final readonly class LayoutStyleResolver
{
public function styleValue(StyleMap $style, string $property, string $default): string
{
return $style->get($property, $default) ?? $default;
}
public function toPoints(string $value): float
{
$normalized = strtolower($value);
$number = (float) preg_replace('/[^0-9.\-]/', '', $normalized);
if (str_ends_with($normalized, 'px')) {
return $number * 0.75;
}
return $number;
}
public function resolveRelativeDimension(string $value, float $reference): float
{
$normalized = strtolower(trim($value));
if ($normalized === '') {
return 0.0;
}
if (str_ends_with($normalized, '%')) {
$number = (float) preg_replace('/[^0-9.\-]/', '', $normalized);
return $reference * ($number / 100.0);
}
return $this->toPoints($normalized);
}
public function resolveLineHeight(StyleMap $style, float $fontSize): float
{
$defaultLineHeight = $fontSize * 1.2;
$configuredLineHeight = $this->styleValue($style, 'line-height', '');
if ($configuredLineHeight === '') {
return $defaultLineHeight;
}
return max($defaultLineHeight, $this->toPoints($configuredLineHeight));
}
/**
* @return array{top: float, right: float, bottom: float, left: float}
*/
public function parseBoxSpacing(string $value): array
{
preg_match_all('/\S+/', $value, $matches);
$tokens = $matches[0];
if ($tokens === []) {
return ['top' => 0.0, 'right' => 0.0, 'bottom' => 0.0, 'left' => 0.0];
}
$points = array_map(fn (string $token): float => $this->toPoints($token), $tokens);
$count = count($points);
return match ($count) {
1 => ['top' => $points[0], 'right' => $points[0], 'bottom' => $points[0], 'left' => $points[0]],
2 => ['top' => $points[0], 'right' => $points[1], 'bottom' => $points[0], 'left' => $points[1]],
3 => ['top' => $points[0], 'right' => $points[1], 'bottom' => $points[2], 'left' => $points[1]],
default => ['top' => $points[0], 'right' => $points[1], 'bottom' => $points[2], 'left' => $points[3]],
};
}
/**
* @return array{top: float, right: float, bottom: float, left: float}
*/
public function parseBoxSpacingRelative(string $value, float $widthReference, float $heightReference): array
{
preg_match_all('/\S+/', $value, $matches);
$tokens = $matches[0];
if ($tokens === []) {
return ['top' => 0.0, 'right' => 0.0, 'bottom' => 0.0, 'left' => 0.0];
}
[$top, $right, $bottom, $left] = $this->expandSpacingTokens($tokens);
return [
'top' => $this->resolveRelativeDimension($top, $heightReference),
'right' => $this->resolveRelativeDimension($right, $widthReference),
'bottom' => $this->resolveRelativeDimension($bottom, $heightReference),
'left' => $this->resolveRelativeDimension($left, $widthReference),
];
}
public function resolveFontAlias(string $fontFamily, string $fontWeight): string
{
$primary = strtolower(explode(',', $fontFamily)[0]);
$isBold = $this->isBoldWeight($fontWeight);
if (str_contains($primary, 'times')) {
return $isBold ? 'F4' : 'F3';
}
if (str_contains($primary, 'courier')) {
return $isBold ? 'F6' : 'F5';
}
return $isBold ? 'F2' : 'F1';
}
public function isAbsolutelyPositioned(StyleMap $style): bool
{
return strtolower(trim($this->styleValue($style, 'position', ''))) === 'absolute';
}
/**
* @param list<string> $tokens
* @return array{0: string, 1: string, 2: string, 3: string}
*/
private function expandSpacingTokens(array $tokens): array
{
return match (count($tokens)) {
1 => [$tokens[0], $tokens[0], $tokens[0], $tokens[0]],
2 => [$tokens[0], $tokens[1], $tokens[0], $tokens[1]],
3 => [$tokens[0], $tokens[1], $tokens[2], $tokens[1]],
default => [$tokens[0], $tokens[1], $tokens[2], $tokens[3]],
};
}
private function isBoldWeight(string $fontWeight): bool
{
$normalized = strtolower($fontWeight);
if ($normalized === 'bold' || $normalized === 'bolder') {
return true;
}
if (is_numeric($normalized)) {
return $normalized >= 600;
}
return false;
}
}