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