xref: /webtrees/app/Report/AbstractRenderer.php (revision a393a2a10b5cb4d1933ff36493f54162ce579761)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Report;
21
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\MediaFile;
24use Fisharebest\Webtrees\Webtrees;
25use League\Flysystem\FilesystemInterface;
26
27use function current;
28
29/**
30 * Class AbstractRenderer - base for PDF and HTML reports
31 */
32abstract class AbstractRenderer
33{
34    // Reports layouts are measured in points.
35    protected const UNITS = 'pt';
36
37    // A point is 1/72 of an inch
38    protected const INCH_TO_POINTS = 72.0;
39    protected const MM_TO_POINTS   = 72.0 / 25.4;
40
41    protected const PAPER_SIZES = [
42        // ISO 216
43        'A0'         => [841.0 * self::MM_TO_POINTS, 1189.0 * self::MM_TO_POINTS],
44        'A1'         => [594.0 * self::MM_TO_POINTS, 841.0 * self::MM_TO_POINTS],
45        'A2'         => [420.0 * self::MM_TO_POINTS, 594.0 * self::MM_TO_POINTS],
46        'A3'         => [297.0 * self::MM_TO_POINTS, 420.0 * self::MM_TO_POINTS],
47        'A4'         => [210.0 * self::MM_TO_POINTS, 297.0 * self::MM_TO_POINTS],
48        // US
49        'US-Letter'  => [8.5 * self::INCH_TO_POINTS, 11.0 * self::INCH_TO_POINTS],
50        'US-Legal'   => [8.5 * self::INCH_TO_POINTS, 14.0 * self::INCH_TO_POINTS],
51        'US-Tabloid' => [11.0 * self::INCH_TO_POINTS, 17.0 * self::INCH_TO_POINTS],
52    ];
53
54    /** @var float Left Margin */
55    public $left_margin = 18.0 * self::MM_TO_POINTS;
56
57    /** @var float Right Margin */
58    public $right_margin = 9.9 * self::MM_TO_POINTS;
59
60    /** @var float Top Margin */
61    public $top_margin = 26.8 * self::MM_TO_POINTS;
62
63    /** @var float Bottom Margin */
64    public $bottom_margin = 21.6 * self::MM_TO_POINTS;
65
66    /** @var float Header Margin */
67    public $header_margin = 4.9 * self::MM_TO_POINTS;
68
69    /** @var float Footer Margin */
70    public $footer_margin = 9.9 * self::MM_TO_POINTS;
71
72    /** @var string Page orientation (portrait, landscape) */
73    public $orientation = 'portrait';
74
75    /** @var string Page format name */
76    public $page_format = 'A4';
77
78    /** @var float Height of page format in points */
79    public $page_height = 0.0;
80
81    /** @var float Width of page format in points */
82    public $page_width = 0.0;
83
84    /** @var string[][] An array of the Styles elements found in the document */
85    public $styles = [];
86
87    /** @var string The default Report font name */
88    public $default_font = 'dejavusans';
89
90    /** @var float The default Report font size */
91    public $default_font_size = 12.0;
92
93    /** @var string Header (H), Body (B) or Footer (F) */
94    public $processing = 'H';
95
96    /** @var bool RTL Language (false=LTR, true=RTL) */
97    public $rtl = false;
98
99    /** @var bool Show the Generated by... (true=show the text) */
100    public $show_generated_by = true;
101
102    /** @var string Generated By... text */
103    public $generated_by = '';
104
105    /** @var string The report title */
106    public $title = '';
107
108    /** @var string Author of the report, the users full name */
109    public $rauthor = Webtrees::NAME . ' ' . Webtrees::VERSION;
110
111    /** @var string Keywords */
112    public $rkeywords = '';
113
114    /** @var string Report Description / Subject */
115    public $rsubject = '';
116
117    /**
118     * Clear the Header.
119     *
120     * @return void
121     */
122    abstract public function clearHeader(): void;
123
124    /**
125     * Add an element.
126     *
127     * @param ReportBaseElement|string $element
128     *
129     * @return void
130     */
131    abstract public function addElement($element): void;
132
133    /**
134     * Run the report.
135     *
136     * @return void
137     */
138    abstract public function run(): void;
139
140    /**
141     * Create a new Cell object.
142     *
143     * @param int    $width   cell width (expressed in points)
144     * @param int    $height  cell height (expressed in points)
145     * @param mixed  $border  Border style
146     * @param string $align   Text alignement
147     * @param string $bgcolor Background color code
148     * @param string $style   The name of the text style
149     * @param int    $ln      Indicates where the current position should go after the call
150     * @param mixed  $top     Y-position
151     * @param mixed  $left    X-position
152     * @param int    $fill    Indicates if the cell background must be painted (1) or transparent (0). Default value: 1
153     * @param int    $stretch Stretch carachter mode
154     * @param string $bocolor Border color
155     * @param string $tcolor  Text color
156     * @param bool   $reseth
157     *
158     * @return ReportBaseCell
159     */
160    abstract public function createCell($width, $height, $border, $align, $bgcolor, $style, $ln, $top, $left, $fill, $stretch, $bocolor, $tcolor, $reseth): ReportBaseCell;
161
162    /**
163     * Create a new TextBox object.
164     *
165     * @param float  $width   Text box width
166     * @param float  $height  Text box height
167     * @param bool   $border
168     * @param string $bgcolor Background color code in HTML
169     * @param bool   $newline
170     * @param float  $left
171     * @param float  $top
172     * @param bool   $pagecheck
173     * @param string $style
174     * @param bool   $fill
175     * @param bool   $padding
176     * @param bool   $reseth
177     *
178     * @return ReportBaseTextbox
179     */
180    abstract public function createTextBox(
181        float $width,
182        float $height,
183        bool $border,
184        string $bgcolor,
185        bool $newline,
186        float $left,
187        float $top,
188        bool $pagecheck,
189        string $style,
190        bool $fill,
191        bool $padding,
192        bool $reseth
193    ): ReportBaseTextbox;
194
195    /**
196     * Create a text element.
197     *
198     * @param string $style
199     * @param string $color
200     *
201     * @return ReportBaseText
202     */
203    abstract public function createText(string $style, string $color): ReportBaseText;
204
205    /**
206     * Create an HTML element.
207     *
208     * @param string   $tag
209     * @param string[] $attrs
210     *
211     * @return ReportBaseHtml
212     */
213    abstract public function createHTML(string $tag, array $attrs): ReportBaseHtml;
214
215    /**
216     * Create a line.
217     *
218     * @param float $x1
219     * @param float $y1
220     * @param float $x2
221     * @param float $y2
222     *
223     * @return ReportBaseLine
224     */
225    abstract public function createLine(float $x1, float $y1, float $x2, float $y2): ReportBaseLine;
226
227    /**
228     * Create a new image object.
229     *
230     * @param string $file  Filename
231     * @param float  $x
232     * @param float  $y
233     * @param float  $w     Image width
234     * @param float  $h     Image height
235     * @param string $align L:left, C:center, R:right or empty to use x/y
236     * @param string $ln    T:same line, N:next line
237     *
238     * @return ReportBaseImage
239     */
240    abstract public function createImage(string $file, float $x, float $y, float $w, float $h, string $align, string $ln): ReportBaseImage;
241
242    /**
243     * Create a new image object from Media Object.
244     *
245     * @param MediaFile           $media_file
246     * @param float               $x
247     * @param float               $y
248     * @param float               $w     Image width
249     * @param float               $h     Image height
250     * @param string              $align L:left, C:center, R:right or empty to use x/y
251     * @param string              $ln    T:same line, N:next line
252     * @param FilesystemInterface $data_filesystem
253     *
254     * @return ReportBaseImage
255     */
256    abstract public function createImageFromObject(
257        MediaFile $media_file,
258        float $x,
259        float $y,
260        float $w,
261        float $h,
262        string $align,
263        string $ln,
264        FilesystemInterface $data_filesystem
265    ): ReportBaseImage;
266
267    /**
268     * Create a new Footnote object.
269     *
270     * @param string $style Style name
271     *
272     * @return ReportBaseFootnote
273     */
274    abstract public function createFootnote($style): ReportBaseFootnote;
275
276    /**
277     * Initial Setup
278     * Setting up document wide defaults that will be inherited of the report modules
279     * As DEFAULT A4 and Portrait will be used if not set
280     *
281     * @return void
282     */
283    public function setup(): void
284    {
285        $this->rtl = I18N::direction() === 'rtl';
286
287        $this->rkeywords = '';
288
289        // I18N: This is a report footer. %s is the name of the application.
290        $this->generated_by = I18N::translate('Generated by %s', Webtrees::NAME . ' ' . Webtrees::VERSION);
291
292        // Paper size - defaults to A4 if the report fails to define a size.
293        [$this->page_width, $this->page_height] = self::PAPER_SIZES[$this->page_format] ?? self::PAPER_SIZES['A4'];
294    }
295
296    /**
297     * Process the Header, Body or Footer
298     *
299     * @param string $p Header (H), Body (B) or Footer (F)
300     *
301     * @return void
302     */
303    public function setProcessing(string $p): void
304    {
305        $this->processing = $p;
306    }
307
308    /**
309     * Add the Title when raw character data is used in Title
310     *
311     * @param string $data
312     *
313     * @return void
314     */
315    public function addTitle(string $data): void
316    {
317        $this->title .= $data;
318    }
319
320    /**
321     * Add the Description when raw character data is used in Description
322     *
323     * @param string $data
324     *
325     * @return void
326     */
327    public function addDescription(string $data): void
328    {
329        $this->rsubject .= $data;
330    }
331
332    /**
333     * Add Style to Styles array
334     *
335     * @param string[] $style
336     *
337     * @return void
338     */
339    public function addStyle(array $style): void
340    {
341        $this->styles[$style['name']] = $style;
342    }
343
344    /**
345     * Get a style from the Styles array
346     *
347     * @param string $s Style name
348     *
349     * @return array
350     */
351    public function getStyle(string $s): array
352    {
353        if (!isset($this->styles[$s])) {
354            return current($this->styles);
355        }
356
357        return $this->styles[$s];
358    }
359}
360