-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemplateDocumentBuilder.php
More file actions
152 lines (134 loc) · 4.31 KB
/
TemplateDocumentBuilder.php
File metadata and controls
152 lines (134 loc) · 4.31 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
<?php
// SPDX-FileCopyrightText: 2026 LibreSign
// SPDX-License-Identifier: AGPL-3.0-or-later
declare(strict_types=1);
namespace LibreSign\XObjectTemplate\Pdf;
use Closure;
use LibreSign\XObjectTemplate\Dto\CompileRequest;
use LibreSign\XObjectTemplate\Dto\CompileResult;
use LibreSign\XObjectTemplate\Layout\LayoutImage;
use LibreSign\XObjectTemplate\Layout\LayoutResult;
final readonly class TemplateDocumentBuilder
{
private const DEFAULT_FONT_RESOURCES = [
'F1' => [
'Type' => '/Font',
'Subtype' => '/Type1',
'BaseFont' => '/Helvetica',
],
'F2' => [
'Type' => '/Font',
'Subtype' => '/Type1',
'BaseFont' => '/Helvetica-Bold',
],
'F3' => [
'Type' => '/Font',
'Subtype' => '/Type1',
'BaseFont' => '/Times-Roman',
],
'F4' => [
'Type' => '/Font',
'Subtype' => '/Type1',
'BaseFont' => '/Times-Bold',
],
'F5' => [
'Type' => '/Font',
'Subtype' => '/Type1',
'BaseFont' => '/Courier',
],
'F6' => [
'Type' => '/Font',
'Subtype' => '/Type1',
'BaseFont' => '/Courier-Bold',
],
];
private Closure $clock;
/**
* @param array<string, array<string, mixed>> $fontResources
*/
public function __construct(
private PdfEscaper $pdfEscaper = new PdfEscaper(),
private ColorParser $colorParser = new ColorParser(),
private array $fontResources = self::DEFAULT_FONT_RESOURCES,
?Closure $clock = null,
) {
$this->clock = $clock ?? static fn (): int => hrtime(true);
}
public function build(
CompileRequest $request,
LayoutResult $layout,
int $startedAtNs,
int $nodeCount = 0,
): CompileResult {
return new CompileResult(
contentStream: $this->buildContentStream($layout),
resources: $this->buildResources($layout),
bbox: [0.0, 0.0, $request->width, $request->height],
metadata: $this->buildMetadata($layout, $startedAtNs, $nodeCount),
);
}
public function buildContentStream(LayoutResult $layout): string
{
$stream = ['q'];
foreach ($layout->images as $image) {
$stream[] = $this->buildImageCommand($image);
}
$stream[] = 'BT';
foreach ($layout->lines as $line) {
$stream[] = sprintf('/%s %F Tf', $line->fontAlias, $line->fontSize);
$stream[] = $this->colorParser->toPdfRgb($line->rgbColor);
$stream[] = sprintf('%F %F Td', $line->x, $line->y);
$stream[] = sprintf('(%s) Tj', $this->pdfEscaper->escapeLiteralString($line->text));
}
$stream[] = 'ET';
$stream[] = 'Q';
return implode("\n", $stream);
}
/**
* @return array<string, mixed>
*/
public function buildResources(LayoutResult $layout): array
{
$resources = [
'Font' => $this->fontResources,
'XObject' => [],
];
foreach ($layout->images as $image) {
$resources['XObject'][$image->alias] = [
'Type' => '/XObject',
'Subtype' => '/Image',
'Source' => $image->source,
'Width' => $image->width,
'Height' => $image->height,
];
}
return $resources;
}
/**
* @return array<string, mixed>
*/
public function buildMetadata(LayoutResult $layout, int $startedAtNs, int $nodeCount = 0): array
{
return [
'render_ms' => round((($this->clock)() - $startedAtNs) / 1_000_000, 3),
'line_count' => count($layout->lines),
'image_count' => count($layout->images),
'node_count' => $nodeCount,
];
}
public function withFontResources(array $fontResources): self
{
return new self($this->pdfEscaper, $this->colorParser, $fontResources, $this->clock);
}
private function buildImageCommand(LayoutImage $image): string
{
return sprintf(
'q %F 0 0 %F %F %F cm /%s Do Q',
$image->width,
$image->height,
$image->x,
$image->y,
$image->alias,
);
}
}