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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2021 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Fisharebest\Webtrees\Report;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\MediaFile;
use Fisharebest\Webtrees\Webtrees;
use League\Flysystem\FilesystemOperator;
use function current;
/**
* Class AbstractRenderer - base for PDF and HTML reports
*/
abstract class AbstractRenderer
{
// Reports layouts are measured in points.
protected const UNITS = 'pt';
// A point is 1/72 of an inch
protected const INCH_TO_POINTS = 72.0;
protected const MM_TO_POINTS = 72.0 / 25.4;
protected const PAPER_SIZES = [
// ISO 216
'A0' => [841.0 * self::MM_TO_POINTS, 1189.0 * self::MM_TO_POINTS],
'A1' => [594.0 * self::MM_TO_POINTS, 841.0 * self::MM_TO_POINTS],
'A2' => [420.0 * self::MM_TO_POINTS, 594.0 * self::MM_TO_POINTS],
'A3' => [297.0 * self::MM_TO_POINTS, 420.0 * self::MM_TO_POINTS],
'A4' => [210.0 * self::MM_TO_POINTS, 297.0 * self::MM_TO_POINTS],
// US
'US-Letter' => [8.5 * self::INCH_TO_POINTS, 11.0 * self::INCH_TO_POINTS],
'US-Legal' => [8.5 * self::INCH_TO_POINTS, 14.0 * self::INCH_TO_POINTS],
'US-Tabloid' => [11.0 * self::INCH_TO_POINTS, 17.0 * self::INCH_TO_POINTS],
];
public float $left_margin = 18.0 * self::MM_TO_POINTS;
public float $right_margin = 9.9 * self::MM_TO_POINTS;
public float $top_margin = 26.8 * self::MM_TO_POINTS;
public float $bottom_margin = 21.6 * self::MM_TO_POINTS;
public float $header_margin = 4.9 * self::MM_TO_POINTS;
public float $footer_margin = 9.9 * self::MM_TO_POINTS;
/** @var string Page orientation (portrait, landscape) */
public string $orientation = 'portrait';
/** @var string Page format name */
public string $page_format = 'A4';
/** @var float Height of page format in points */
public float $page_height = 0.0;
/** @var float Width of page format in points */
public float $page_width = 0.0;
/** @var array<array<string,string>> An array of the Styles elements found in the document */
public array $styles = [];
/** @var string The default Report font name */
public string $default_font = 'dejavusans';
/** @var float The default Report font size */
public float $default_font_size = 12.0;
/** @var string Header (H), Body (B) or Footer (F) */
public string $processing = 'H';
/** @var bool RTL Language (false=LTR, true=RTL) */
public bool $rtl = false;
/** @var bool Show the Generated by... (true=show the text) */
public bool $show_generated_by = true;
/** @var string Generated By... text */
public string $generated_by = '';
/** @var string The report title */
public string $title = '';
/** @var string Author of the report, the users full name */
public string $rauthor = Webtrees::NAME . ' ' . Webtrees::VERSION;
/** @var string Keywords */
public string $rkeywords = '';
/** @var string Report Description / Subject */
public string $rsubject = '';
/** @var array<ReportBaseElement|string> */
public array $headerElements = [];
/** @var array<ReportBaseElement|string> */
public array $footerElements = [];
/** @var array<ReportBaseElement|string> */
public array $bodyElements = [];
public string $currentStyle = '';
/**
* Clear the Header.
*
* @return void
*/
abstract public function clearHeader(): void;
/**
* @param ReportBaseElement|string $element
*
* @return void
*/
public function addElement($element): void
{
if ($this->processing === 'B') {
$this->addElementToBody($element);
} elseif ($this->processing === 'H') {
$this->addElementToHeader($element);
} elseif ($this->processing === 'F') {
$this->addElementToFooter($element);
}
}
/**
* @param ReportBaseElement|string $element
*
* @return void
*/
public function addElementToHeader($element): void
{
$this->headerElements[] = $element;
}
/**
* @param ReportBaseElement|string $element
*
* @return void
*/
public function addElementToBody($element): void
{
$this->bodyElements[] = $element;
}
/**
* @param ReportBaseElement|string $element
*
* @return void
*/
public function addElementToFooter($element): void
{
$this->footerElements[] = $element;
}
/**
* Run the report.
*
* @return void
*/
abstract public function run(): void;
/**
* Create a new Cell object.
*
* @param float $width cell width (expressed in points)
* @param float $height cell height (expressed in points)
* @param string $border Border style
* @param string $align Text alignement
* @param string $bgcolor Background color code
* @param string $style The name of the text style
* @param int $ln Indicates where the current position should go after the call
* @param mixed $top Y-position
* @param mixed $left X-position
* @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 1
* @param int $stretch Stretch carachter mode
* @param string $bocolor Border color
* @param string $tcolor Text color
* @param bool $reseth
*
* @return ReportBaseCell
*/
abstract public function createCell(
float $width,
float $height,
string $border,
string $align,
string $bgcolor,
string $style,
int $ln,
$top,
$left,
int $fill,
int $stretch,
string $bocolor,
string $tcolor,
bool $reseth
): ReportBaseCell;
/**
* Create a new TextBox object.
*
* @param float $width Text box width
* @param float $height Text box height
* @param bool $border
* @param string $bgcolor Background color code in HTML
* @param bool $newline
* @param float $left
* @param float $top
* @param bool $pagecheck
* @param string $style
* @param bool $fill
* @param bool $padding
* @param bool $reseth
*
* @return ReportBaseTextbox
*/
abstract public function createTextBox(
float $width,
float $height,
bool $border,
string $bgcolor,
bool $newline,
float $left,
float $top,
bool $pagecheck,
string $style,
bool $fill,
bool $padding,
bool $reseth
): ReportBaseTextbox;
/**
* Create a text element.
*
* @param string $style
* @param string $color
*
* @return ReportBaseText
*/
abstract public function createText(string $style, string $color): ReportBaseText;
/**
* Create a line.
*
* @param float $x1
* @param float $y1
* @param float $x2
* @param float $y2
*
* @return ReportBaseLine
*/
abstract public function createLine(float $x1, float $y1, float $x2, float $y2): ReportBaseLine;
/**
* Create a new image object.
*
* @param string $file Filename
* @param float $x
* @param float $y
* @param float $w Image width
* @param float $h Image height
* @param string $align L:left, C:center, R:right or empty to use x/y
* @param string $ln T:same line, N:next line
*
* @return ReportBaseImage
*/
abstract public function createImage(string $file, float $x, float $y, float $w, float $h, string $align, string $ln): ReportBaseImage;
/**
* Create a new image object from Media Object.
*
* @param MediaFile $media_file
* @param float $x
* @param float $y
* @param float $w Image width
* @param float $h Image height
* @param string $align L:left, C:center, R:right or empty to use x/y
* @param string $ln T:same line, N:next line
* @param FilesystemOperator $data_filesystem
*
* @return ReportBaseImage
*/
abstract public function createImageFromObject(
MediaFile $media_file,
float $x,
float $y,
float $w,
float $h,
string $align,
string $ln,
FilesystemOperator $data_filesystem
): ReportBaseImage;
/**
* Create a new Footnote object.
*
* @param string $style Style name
*
* @return ReportBaseFootnote
*/
abstract public function createFootnote(string $style): ReportBaseFootnote;
/**
* Initial Setup
* Setting up document wide defaults that will be inherited of the report modules
* As DEFAULT A4 and Portrait will be used if not set
*
* @return void
*/
public function setup(): void
{
$this->rtl = I18N::direction() === 'rtl';
$this->rkeywords = '';
// I18N: This is a report footer. %s is the name of the application.
$this->generated_by = I18N::translate('Generated by %s', Webtrees::NAME . ' ' . Webtrees::VERSION);
// Paper size - defaults to A4 if the report fails to define a size.
[$this->page_width, $this->page_height] = self::PAPER_SIZES[$this->page_format] ?? self::PAPER_SIZES['A4'];
}
/**
* Process the Header, Body or Footer
*
* @param string $p Header (H), Body (B) or Footer (F)
*
* @return void
*/
public function setProcessing(string $p): void
{
$this->processing = $p;
}
/**
* Add the Title when raw character data is used in Title
*
* @param string $data
*
* @return void
*/
public function addTitle(string $data): void
{
$this->title .= $data;
}
/**
* Add the Description when raw character data is used in Description
*
* @param string $data
*
* @return void
*/
public function addDescription(string $data): void
{
$this->rsubject .= $data;
}
/**
* Add Style to Styles array
*
* @param array<string> $style
*
* @return void
*/
public function addStyle(array $style): void
{
$this->styles[$style['name']] = $style;
}
/**
* Get a style from the Styles array
*
* @param string $s Style name
*
* @return array
*/
public function getStyle(string $s): array
{
return $this->styles[$s] ?? current($this->styles);
}
}
|