xref: /webtrees/app/Report/AbstractRenderer.php (revision 9458f20a3c5e8e676f65488153a8d8a76b1773fb)
1b6f35a76SGreg Roach<?php
2b6f35a76SGreg Roach
3b6f35a76SGreg Roach/**
4b6f35a76SGreg Roach * webtrees: online genealogy
55bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team
6b6f35a76SGreg Roach * This program is free software: you can redistribute it and/or modify
7b6f35a76SGreg Roach * it under the terms of the GNU General Public License as published by
8b6f35a76SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9b6f35a76SGreg Roach * (at your option) any later version.
10b6f35a76SGreg Roach * This program is distributed in the hope that it will be useful,
11b6f35a76SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12b6f35a76SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13b6f35a76SGreg Roach * GNU General Public License for more details.
14b6f35a76SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16b6f35a76SGreg Roach */
17b6f35a76SGreg Roach
18b6f35a76SGreg Roachdeclare(strict_types=1);
19b6f35a76SGreg Roach
20b6f35a76SGreg Roachnamespace Fisharebest\Webtrees\Report;
21b6f35a76SGreg Roach
22b6f35a76SGreg Roachuse Fisharebest\Webtrees\I18N;
23b6f35a76SGreg Roachuse Fisharebest\Webtrees\MediaFile;
24b6f35a76SGreg Roachuse Fisharebest\Webtrees\Webtrees;
25b6f35a76SGreg Roach
26b6f35a76SGreg Roach/**
27b6f35a76SGreg Roach * Class AbstractRenderer - base for PDF and HTML reports
28b6f35a76SGreg Roach */
29b6f35a76SGreg Roachabstract class AbstractRenderer
30b6f35a76SGreg Roach{
31b6f35a76SGreg Roach    // Reports layouts are measured in points.
32b6f35a76SGreg Roach    protected const UNITS = 'pt';
33b6f35a76SGreg Roach
34b6f35a76SGreg Roach    // A point is 1/72 of an inch
35b6f35a76SGreg Roach    protected const INCH_TO_POINTS = 72.0;
36b6f35a76SGreg Roach    protected const MM_TO_POINTS   = 72.0 / 25.4;
37b6f35a76SGreg Roach
38b6f35a76SGreg Roach    protected const PAPER_SIZES = [
39b6f35a76SGreg Roach        // ISO 216
40b6f35a76SGreg Roach        'A0'         => [841.0 * self::MM_TO_POINTS, 1189.0 * self::MM_TO_POINTS],
41b6f35a76SGreg Roach        'A1'         => [594.0 * self::MM_TO_POINTS, 841.0 * self::MM_TO_POINTS],
42b6f35a76SGreg Roach        'A2'         => [420.0 * self::MM_TO_POINTS, 594.0 * self::MM_TO_POINTS],
43b6f35a76SGreg Roach        'A3'         => [297.0 * self::MM_TO_POINTS, 420.0 * self::MM_TO_POINTS],
44b6f35a76SGreg Roach        'A4'         => [210.0 * self::MM_TO_POINTS, 297.0 * self::MM_TO_POINTS],
45b6f35a76SGreg Roach        // US
46b6f35a76SGreg Roach        'US-Letter'  => [8.5 * self::INCH_TO_POINTS, 11.0 * self::INCH_TO_POINTS],
47b6f35a76SGreg Roach        'US-Legal'   => [8.5 * self::INCH_TO_POINTS, 14.0 * self::INCH_TO_POINTS],
48b6f35a76SGreg Roach        'US-Tabloid' => [11.0 * self::INCH_TO_POINTS, 17.0 * self::INCH_TO_POINTS],
49b6f35a76SGreg Roach    ];
50b6f35a76SGreg Roach
51345fcc8aSGreg Roach    public float $left_margin = 18.0 * self::MM_TO_POINTS;
52b6f35a76SGreg Roach
53345fcc8aSGreg Roach    public float $right_margin = 9.9 * self::MM_TO_POINTS;
54b6f35a76SGreg Roach
55345fcc8aSGreg Roach    public float $top_margin = 26.8 * self::MM_TO_POINTS;
56b6f35a76SGreg Roach
57345fcc8aSGreg Roach    public float $bottom_margin = 21.6 * self::MM_TO_POINTS;
58b6f35a76SGreg Roach
59345fcc8aSGreg Roach    public float $header_margin = 4.9 * self::MM_TO_POINTS;
60b6f35a76SGreg Roach
61345fcc8aSGreg Roach    public float $footer_margin = 9.9 * self::MM_TO_POINTS;
62b6f35a76SGreg Roach
63b6f35a76SGreg Roach    /** @var string Page orientation (portrait, landscape) */
64345fcc8aSGreg Roach    public string $orientation = 'portrait';
65b6f35a76SGreg Roach
66b6f35a76SGreg Roach    /** @var string Page format name */
67345fcc8aSGreg Roach    public string $page_format = 'A4';
68b6f35a76SGreg Roach
69b6f35a76SGreg Roach    /** @var float Height of page format in points */
70345fcc8aSGreg Roach    public float $page_height = 0.0;
71b6f35a76SGreg Roach
72b6f35a76SGreg Roach    /** @var float Width of page format in points */
73345fcc8aSGreg Roach    public float $page_width = 0.0;
74b6f35a76SGreg Roach
75345fcc8aSGreg Roach    /** @var array<array<string,string>> An array of the Styles elements found in the document */
76345fcc8aSGreg Roach    public array $styles = [];
77b6f35a76SGreg Roach
78b6f35a76SGreg Roach    /** @var string The default Report font name */
79345fcc8aSGreg Roach    public string $default_font = 'dejavusans';
80b6f35a76SGreg Roach
81b6f35a76SGreg Roach    /** @var float The default Report font size */
82345fcc8aSGreg Roach    public float $default_font_size = 12.0;
83b6f35a76SGreg Roach
84a393a2a1SGreg Roach    /** @var string Header (H), Body (B) or Footer (F) */
85345fcc8aSGreg Roach    public string $processing = 'H';
86b6f35a76SGreg Roach
87b6f35a76SGreg Roach    /** @var bool RTL Language (false=LTR, true=RTL) */
88345fcc8aSGreg Roach    public bool $rtl = false;
89b6f35a76SGreg Roach
90b6f35a76SGreg Roach    /** @var bool Show the Generated by... (true=show the text) */
910a709a28SGreg Roach    public bool $show_generated_by = true;
92b6f35a76SGreg Roach
93b6f35a76SGreg Roach    /** @var string Generated By... text */
94345fcc8aSGreg Roach    public string $generated_by = '';
95b6f35a76SGreg Roach
96b6f35a76SGreg Roach    /** @var string The report title */
97345fcc8aSGreg Roach    public string $title = '';
98b6f35a76SGreg Roach
99b6f35a76SGreg Roach    /** @var string Author of the report, the users full name */
100345fcc8aSGreg Roach    public string $rauthor = Webtrees::NAME . ' ' . Webtrees::VERSION;
101b6f35a76SGreg Roach
102b6f35a76SGreg Roach    /** @var string Keywords */
103345fcc8aSGreg Roach    public string $rkeywords = '';
104b6f35a76SGreg Roach
105b6f35a76SGreg Roach    /** @var string Report Description / Subject */
106345fcc8aSGreg Roach    public string $rsubject = '';
107345fcc8aSGreg Roach
108345fcc8aSGreg Roach    /** @var array<ReportBaseElement|string> */
109345fcc8aSGreg Roach    public array $headerElements = [];
110345fcc8aSGreg Roach
111345fcc8aSGreg Roach    /** @var array<ReportBaseElement|string> */
11277bab461SGreg Roach    public array $footerElements = [];
113345fcc8aSGreg Roach
114345fcc8aSGreg Roach    /** @var array<ReportBaseElement|string> */
115345fcc8aSGreg Roach    public array $bodyElements = [];
116345fcc8aSGreg Roach
117345fcc8aSGreg Roach    public string $currentStyle = '';
118b6f35a76SGreg Roach
119b6f35a76SGreg Roach    /**
120b6f35a76SGreg Roach     * Clear the Header.
121b6f35a76SGreg Roach     *
122b6f35a76SGreg Roach     * @return void
123b6f35a76SGreg Roach     */
124b6f35a76SGreg Roach    abstract public function clearHeader(): void;
125b6f35a76SGreg Roach
126345fcc8aSGreg Roach
127b6f35a76SGreg Roach    /**
128b6f35a76SGreg Roach     * @param ReportBaseElement|string $element
129b6f35a76SGreg Roach     *
130b6f35a76SGreg Roach     * @return void
131b6f35a76SGreg Roach     */
132345fcc8aSGreg Roach    public function addElement($element): void
133345fcc8aSGreg Roach    {
134345fcc8aSGreg Roach        if ($this->processing === 'B') {
135345fcc8aSGreg Roach            $this->addElementToBody($element);
136345fcc8aSGreg Roach        } elseif ($this->processing === 'H') {
137345fcc8aSGreg Roach            $this->addElementToHeader($element);
138345fcc8aSGreg Roach        } elseif ($this->processing === 'F') {
139345fcc8aSGreg Roach            $this->addElementToFooter($element);
140345fcc8aSGreg Roach        }
141345fcc8aSGreg Roach    }
142345fcc8aSGreg Roach
143345fcc8aSGreg Roach    /**
144345fcc8aSGreg Roach     * @param ReportBaseElement|string $element
145345fcc8aSGreg Roach     *
146345fcc8aSGreg Roach     * @return void
147345fcc8aSGreg Roach     */
148345fcc8aSGreg Roach    public function addElementToHeader($element): void
149345fcc8aSGreg Roach    {
150345fcc8aSGreg Roach        $this->headerElements[] = $element;
151345fcc8aSGreg Roach    }
152345fcc8aSGreg Roach
153345fcc8aSGreg Roach    /**
154345fcc8aSGreg Roach     * @param ReportBaseElement|string $element
155345fcc8aSGreg Roach     *
156345fcc8aSGreg Roach     * @return void
157345fcc8aSGreg Roach     */
158345fcc8aSGreg Roach    public function addElementToBody($element): void
159345fcc8aSGreg Roach    {
160345fcc8aSGreg Roach        $this->bodyElements[] = $element;
161345fcc8aSGreg Roach    }
162345fcc8aSGreg Roach
163345fcc8aSGreg Roach    /**
164345fcc8aSGreg Roach     * @param ReportBaseElement|string $element
165345fcc8aSGreg Roach     *
166345fcc8aSGreg Roach     * @return void
167345fcc8aSGreg Roach     */
168345fcc8aSGreg Roach    public function addElementToFooter($element): void
169345fcc8aSGreg Roach    {
170345fcc8aSGreg Roach        $this->footerElements[] = $element;
171345fcc8aSGreg Roach    }
172b6f35a76SGreg Roach
173b6f35a76SGreg Roach    /**
174b6f35a76SGreg Roach     * Run the report.
175b6f35a76SGreg Roach     *
176b6f35a76SGreg Roach     * @return void
177b6f35a76SGreg Roach     */
178b6f35a76SGreg Roach    abstract public function run(): void;
179b6f35a76SGreg Roach
180b6f35a76SGreg Roach    /**
181b6f35a76SGreg Roach     * Create a new Cell object.
182b6f35a76SGreg Roach     *
18377bab461SGreg Roach     * @param float  $width   cell width (expressed in points)
18477bab461SGreg Roach     * @param float  $height  cell height (expressed in points)
18577bab461SGreg Roach     * @param string $border  Border style
186b6f35a76SGreg Roach     * @param string $align   Text alignement
187b6f35a76SGreg Roach     * @param string $bgcolor Background color code
188b6f35a76SGreg Roach     * @param string $style   The name of the text style
189b6f35a76SGreg Roach     * @param int    $ln      Indicates where the current position should go after the call
190b6f35a76SGreg Roach     * @param mixed  $top     Y-position
191b6f35a76SGreg Roach     * @param mixed  $left    X-position
192b6f35a76SGreg Roach     * @param int    $fill    Indicates if the cell background must be painted (1) or transparent (0). Default value: 1
193b6f35a76SGreg Roach     * @param int    $stretch Stretch carachter mode
194b6f35a76SGreg Roach     * @param string $bocolor Border color
195b6f35a76SGreg Roach     * @param string $tcolor  Text color
196b6f35a76SGreg Roach     * @param bool   $reseth
197b6f35a76SGreg Roach     *
198b6f35a76SGreg Roach     * @return ReportBaseCell
199b6f35a76SGreg Roach     */
20024f2a3afSGreg Roach    abstract public function createCell(
20177bab461SGreg Roach        float $width,
20277bab461SGreg Roach        float $height,
20377bab461SGreg Roach        string $border,
20424f2a3afSGreg Roach        string $align,
20524f2a3afSGreg Roach        string $bgcolor,
20624f2a3afSGreg Roach        string $style,
20724f2a3afSGreg Roach        int $ln,
20824f2a3afSGreg Roach        $top,
20924f2a3afSGreg Roach        $left,
21024f2a3afSGreg Roach        int $fill,
21124f2a3afSGreg Roach        int $stretch,
21224f2a3afSGreg Roach        string $bocolor,
21324f2a3afSGreg Roach        string $tcolor,
21424f2a3afSGreg Roach        bool $reseth
21524f2a3afSGreg Roach    ): ReportBaseCell;
216b6f35a76SGreg Roach
217b6f35a76SGreg Roach    /**
218b6f35a76SGreg Roach     * Create a new TextBox object.
219b6f35a76SGreg Roach     *
220b6f35a76SGreg Roach     * @param float  $width   Text box width
221b6f35a76SGreg Roach     * @param float  $height  Text box height
222b6f35a76SGreg Roach     * @param bool   $border
223b6f35a76SGreg Roach     * @param string $bgcolor Background color code in HTML
224b6f35a76SGreg Roach     * @param bool   $newline
225b6f35a76SGreg Roach     * @param float  $left
226b6f35a76SGreg Roach     * @param float  $top
227b6f35a76SGreg Roach     * @param bool   $pagecheck
228b6f35a76SGreg Roach     * @param string $style
229b6f35a76SGreg Roach     * @param bool   $fill
230b6f35a76SGreg Roach     * @param bool   $padding
231b6f35a76SGreg Roach     * @param bool   $reseth
232b6f35a76SGreg Roach     *
233b6f35a76SGreg Roach     * @return ReportBaseTextbox
234b6f35a76SGreg Roach     */
235b6f35a76SGreg Roach    abstract public function createTextBox(
236b6f35a76SGreg Roach        float $width,
237b6f35a76SGreg Roach        float $height,
238b6f35a76SGreg Roach        bool $border,
239b6f35a76SGreg Roach        string $bgcolor,
240b6f35a76SGreg Roach        bool $newline,
241b6f35a76SGreg Roach        float $left,
242b6f35a76SGreg Roach        float $top,
243b6f35a76SGreg Roach        bool $pagecheck,
244b6f35a76SGreg Roach        string $style,
245b6f35a76SGreg Roach        bool $fill,
246b6f35a76SGreg Roach        bool $padding,
247b6f35a76SGreg Roach        bool $reseth
248b6f35a76SGreg Roach    ): ReportBaseTextbox;
249b6f35a76SGreg Roach
250b6f35a76SGreg Roach    /**
251b6f35a76SGreg Roach     * Create a text element.
252b6f35a76SGreg Roach     *
253b6f35a76SGreg Roach     * @param string $style
254b6f35a76SGreg Roach     * @param string $color
255b6f35a76SGreg Roach     *
256b6f35a76SGreg Roach     * @return ReportBaseText
257b6f35a76SGreg Roach     */
258b6f35a76SGreg Roach    abstract public function createText(string $style, string $color): ReportBaseText;
259b6f35a76SGreg Roach
260b6f35a76SGreg Roach    /**
261b6f35a76SGreg Roach     * Create a line.
262b6f35a76SGreg Roach     *
263b6f35a76SGreg Roach     * @param float $x1
264b6f35a76SGreg Roach     * @param float $y1
265b6f35a76SGreg Roach     * @param float $x2
266b6f35a76SGreg Roach     * @param float $y2
267b6f35a76SGreg Roach     *
268b6f35a76SGreg Roach     * @return ReportBaseLine
269b6f35a76SGreg Roach     */
270b6f35a76SGreg Roach    abstract public function createLine(float $x1, float $y1, float $x2, float $y2): ReportBaseLine;
271b6f35a76SGreg Roach
272b6f35a76SGreg Roach    /**
273b6f35a76SGreg Roach     * Create a new image object.
274b6f35a76SGreg Roach     *
275b6f35a76SGreg Roach     * @param string $file  Filename
276b6f35a76SGreg Roach     * @param float  $x
277b6f35a76SGreg Roach     * @param float  $y
278b6f35a76SGreg Roach     * @param float  $w     Image width
279b6f35a76SGreg Roach     * @param float  $h     Image height
280b6f35a76SGreg Roach     * @param string $align L:left, C:center, R:right or empty to use x/y
281b6f35a76SGreg Roach     * @param string $ln    T:same line, N:next line
282b6f35a76SGreg Roach     *
283b6f35a76SGreg Roach     * @return ReportBaseImage
284b6f35a76SGreg Roach     */
285b6f35a76SGreg Roach    abstract public function createImage(string $file, float $x, float $y, float $w, float $h, string $align, string $ln): ReportBaseImage;
286b6f35a76SGreg Roach
287b6f35a76SGreg Roach    /**
288b6f35a76SGreg Roach     * Create a new image object from Media Object.
289b6f35a76SGreg Roach     *
290b6f35a76SGreg Roach     * @param MediaFile          $media_file
291b6f35a76SGreg Roach     * @param float              $x
292b6f35a76SGreg Roach     * @param float              $y
293b6f35a76SGreg Roach     * @param float              $w     Image width
294b6f35a76SGreg Roach     * @param float              $h     Image height
295b6f35a76SGreg Roach     * @param string             $align L:left, C:center, R:right or empty to use x/y
296b6f35a76SGreg Roach     * @param string             $ln    T:same line, N:next line
297b6f35a76SGreg Roach     *
298b6f35a76SGreg Roach     * @return ReportBaseImage
299b6f35a76SGreg Roach     */
300b6f35a76SGreg Roach    abstract public function createImageFromObject(
301b6f35a76SGreg Roach        MediaFile $media_file,
302b6f35a76SGreg Roach        float $x,
303b6f35a76SGreg Roach        float $y,
304b6f35a76SGreg Roach        float $w,
305b6f35a76SGreg Roach        float $h,
306b6f35a76SGreg Roach        string $align,
307*9458f20aSGreg Roach        string $ln
308b6f35a76SGreg Roach    ): ReportBaseImage;
309b6f35a76SGreg Roach
310b6f35a76SGreg Roach    /**
311b6f35a76SGreg Roach     * Create a new Footnote object.
312b6f35a76SGreg Roach     *
313b6f35a76SGreg Roach     * @param string $style Style name
314b6f35a76SGreg Roach     *
315b6f35a76SGreg Roach     * @return ReportBaseFootnote
316b6f35a76SGreg Roach     */
31724f2a3afSGreg Roach    abstract public function createFootnote(string $style): ReportBaseFootnote;
318b6f35a76SGreg Roach
319b6f35a76SGreg Roach    /**
320b6f35a76SGreg Roach     * Initial Setup
321b6f35a76SGreg Roach     * Setting up document wide defaults that will be inherited of the report modules
322b6f35a76SGreg Roach     * As DEFAULT A4 and Portrait will be used if not set
323b6f35a76SGreg Roach     *
324b6f35a76SGreg Roach     * @return void
325b6f35a76SGreg Roach     */
326b6f35a76SGreg Roach    public function setup(): void
327b6f35a76SGreg Roach    {
328b6f35a76SGreg Roach        $this->rtl = I18N::direction() === 'rtl';
329b6f35a76SGreg Roach
330b6f35a76SGreg Roach        $this->rkeywords = '';
331b6f35a76SGreg Roach
332b6f35a76SGreg Roach        // I18N: This is a report footer. %s is the name of the application.
333b6f35a76SGreg Roach        $this->generated_by = I18N::translate('Generated by %s', Webtrees::NAME . ' ' . Webtrees::VERSION);
334b6f35a76SGreg Roach
335b6f35a76SGreg Roach        // Paper size - defaults to A4 if the report fails to define a size.
336b6f35a76SGreg Roach        [$this->page_width, $this->page_height] = self::PAPER_SIZES[$this->page_format] ?? self::PAPER_SIZES['A4'];
337b6f35a76SGreg Roach    }
338b6f35a76SGreg Roach
339b6f35a76SGreg Roach    /**
340a393a2a1SGreg Roach     * Process the Header, Body or Footer
341b6f35a76SGreg Roach     *
342a393a2a1SGreg Roach     * @param string $p Header (H), Body (B) or Footer (F)
343b6f35a76SGreg Roach     *
344b6f35a76SGreg Roach     * @return void
345b6f35a76SGreg Roach     */
346b6f35a76SGreg Roach    public function setProcessing(string $p): void
347b6f35a76SGreg Roach    {
348b6f35a76SGreg Roach        $this->processing = $p;
349b6f35a76SGreg Roach    }
350b6f35a76SGreg Roach
351b6f35a76SGreg Roach    /**
352b6f35a76SGreg Roach     * Add the Title when raw character data is used in Title
353b6f35a76SGreg Roach     *
354b6f35a76SGreg Roach     * @param string $data
355b6f35a76SGreg Roach     *
356b6f35a76SGreg Roach     * @return void
357b6f35a76SGreg Roach     */
358b6f35a76SGreg Roach    public function addTitle(string $data): void
359b6f35a76SGreg Roach    {
360b6f35a76SGreg Roach        $this->title .= $data;
361b6f35a76SGreg Roach    }
362b6f35a76SGreg Roach
363b6f35a76SGreg Roach    /**
364b6f35a76SGreg Roach     * Add the Description when raw character data is used in Description
365b6f35a76SGreg Roach     *
366b6f35a76SGreg Roach     * @param string $data
367b6f35a76SGreg Roach     *
368b6f35a76SGreg Roach     * @return void
369b6f35a76SGreg Roach     */
370b6f35a76SGreg Roach    public function addDescription(string $data): void
371b6f35a76SGreg Roach    {
372b6f35a76SGreg Roach        $this->rsubject .= $data;
373b6f35a76SGreg Roach    }
374b6f35a76SGreg Roach
375b6f35a76SGreg Roach    /**
376b6f35a76SGreg Roach     * Add Style to Styles array
377b6f35a76SGreg Roach     *
37809482a55SGreg Roach     * @param array<string> $style
379b6f35a76SGreg Roach     *
380b6f35a76SGreg Roach     * @return void
381b6f35a76SGreg Roach     */
382b6f35a76SGreg Roach    public function addStyle(array $style): void
383b6f35a76SGreg Roach    {
384b6f35a76SGreg Roach        $this->styles[$style['name']] = $style;
385b6f35a76SGreg Roach    }
386b6f35a76SGreg Roach
387b6f35a76SGreg Roach    /**
388b6f35a76SGreg Roach     * Get a style from the Styles array
389b6f35a76SGreg Roach     *
390b6f35a76SGreg Roach     * @param string $s Style name
391b6f35a76SGreg Roach     *
3924348fc02SGreg Roach     * @return array<string>
393b6f35a76SGreg Roach     */
394b6f35a76SGreg Roach    public function getStyle(string $s): array
395b6f35a76SGreg Roach    {
3964348fc02SGreg Roach        return $this->styles[$s];
397b6f35a76SGreg Roach    }
398b6f35a76SGreg Roach}
399