-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXObjectPlacement.php
More file actions
50 lines (41 loc) · 1.27 KB
/
XObjectPlacement.php
File metadata and controls
50 lines (41 loc) · 1.27 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
<?php
// SPDX-FileCopyrightText: 2026 LibreSign
// SPDX-License-Identifier: AGPL-3.0-or-later
declare(strict_types=1);
namespace LibreSign\XObjectTemplate\Integration;
use InvalidArgumentException;
final readonly class XObjectPlacement
{
public function __construct(
public float $scaleX,
public float $scaleY,
public float $width,
public float $height,
public float $translateX,
public float $translateY,
) {
}
public function toPdfCommand(string $alias): string
{
$normalizedAlias = ltrim(trim($alias), '/');
if ($normalizedAlias === '') {
throw new InvalidArgumentException('Placement alias must not be empty.');
}
return sprintf(
'q %s 0 0 %s %s %s cm /%s Do Q',
$this->formatNumber($this->scaleX),
$this->formatNumber($this->scaleY),
$this->formatNumber($this->translateX),
$this->formatNumber($this->translateY),
$normalizedAlias,
);
}
private function formatNumber(float $value): string
{
$formatted = rtrim(rtrim(sprintf('%.6F', $value), '0'), '.');
if ($formatted === '' || $formatted === '-0') {
return '0';
}
return $formatted;
}
}