-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXObjectPlacementCalculator.php
More file actions
98 lines (82 loc) · 2.85 KB
/
XObjectPlacementCalculator.php
File metadata and controls
98 lines (82 loc) · 2.85 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
<?php
// SPDX-FileCopyrightText: 2026 LibreSign
// SPDX-License-Identifier: AGPL-3.0-or-later
declare(strict_types=1);
namespace LibreSign\XObjectTemplate\Integration;
use InvalidArgumentException;
use LibreSign\XObjectTemplate\Dto\CompileResult;
final readonly class XObjectPlacementCalculator
{
public function fromWidth(
CompileResult $result,
float $targetWidth,
float $x = 0.0,
float $y = 0.0,
): XObjectPlacement {
if ($targetWidth <= 0.0) {
throw new InvalidArgumentException('Placement target width must be greater than zero.');
}
[$minX, $minY, $baseWidth, $baseHeight] = $this->resolveBoundingBox($result);
$scale = $targetWidth / $baseWidth;
return new XObjectPlacement(
scaleX: $scale,
scaleY: $scale,
width: $baseWidth * $scale,
height: $baseHeight * $scale,
translateX: $x - ($minX * $scale),
translateY: $y - ($minY * $scale),
);
}
public function fromHeight(
CompileResult $result,
float $targetHeight,
float $x = 0.0,
float $y = 0.0,
): XObjectPlacement {
if ($targetHeight <= 0.0) {
throw new InvalidArgumentException('Placement target height must be greater than zero.');
}
[$minX, $minY, $baseWidth, $baseHeight] = $this->resolveBoundingBox($result);
$scale = $targetHeight / $baseHeight;
return new XObjectPlacement(
scaleX: $scale,
scaleY: $scale,
width: $baseWidth * $scale,
height: $baseHeight * $scale,
translateX: $x - ($minX * $scale),
translateY: $y - ($minY * $scale),
);
}
public function fromScale(
CompileResult $result,
float $scale,
float $x = 0.0,
float $y = 0.0,
): XObjectPlacement {
if ($scale <= 0.0) {
throw new InvalidArgumentException('Placement scale must be greater than zero.');
}
[$minX, $minY, $baseWidth, $baseHeight] = $this->resolveBoundingBox($result);
return new XObjectPlacement(
scaleX: $scale,
scaleY: $scale,
width: $baseWidth * $scale,
height: $baseHeight * $scale,
translateX: $x - ($minX * $scale),
translateY: $y - ($minY * $scale),
);
}
/**
* @return array{0: float, 1: float, 2: float, 3: float}
*/
private function resolveBoundingBox(CompileResult $result): array
{
[$minX, $minY, $maxX, $maxY] = $result->bbox;
$width = $maxX - $minX;
$height = $maxY - $minY;
if ($width <= 0.0 || $height <= 0.0) {
throw new InvalidArgumentException('CompileResult bbox must describe a positive area.');
}
return [$minX, $minY, $width, $height];
}
}