xref: /webtrees/app/Report/ReportHtmlImage.php (revision 6f59525087dd4ee1d17c7375524d95ace7942861)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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 <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Report;
21
22/**
23 * Class ReportHtmlImage
24 */
25class ReportHtmlImage extends ReportBaseImage
26{
27    /**
28     * Image renderer
29     *
30     * @param HtmlRenderer $renderer
31     *
32     * @return void
33     */
34    public function render($renderer): void
35    {
36        static $lastpicbottom, $lastpicpage, $lastpicleft, $lastpicright;
37
38        // Get the current positions
39        if ($this->x === ReportBaseElement::CURRENT_POSITION) {
40            $this->x = $renderer->getX();
41        }
42        if ($this->y === ReportBaseElement::CURRENT_POSITION) {
43            //-- first check for a collision with the last picture
44            if ($lastpicbottom !== null && $renderer->pageNo() === $lastpicpage && $lastpicbottom >= $renderer->getY() && $this->x >= $lastpicleft && $this->x <= $lastpicright) {
45                $renderer->setY($lastpicbottom + $renderer->cPadding * 2);
46            }
47            $this->y = $renderer->getY();
48        }
49
50        // Image alignment
51        switch ($this->align) {
52            case 'L':
53                echo '<div style="position:absolute;top:', $this->y, 'pt;left:0pt;width:', $renderer->getRemainingWidth(), "pt;text-align:left;\">\n";
54                echo '<img src="', $this->file, '" style="width:', $this->width, 'pt;height:', $this->height, "pt;\" alt=\"\">\n</div>\n";
55                break;
56            case 'C':
57                echo '<div style="position:absolute;top:', $this->y, 'pt;left:0pt;width:', $renderer->getRemainingWidth(), "pt;text-align:center;\">\n";
58                echo '<img src="', $this->file, '" style="width:', $this->width, 'pt;height:', $this->height, "pt;\" alt=\"\">\n</div>\n";
59                break;
60            case 'R':
61                echo '<div style="position:absolute;top:', $this->y, 'pt;left:0pt;width:', $renderer->getRemainingWidth(), "pt;text-align:right;\">\n";
62                echo '<img src="', $this->file, '" style="width:', $this->width, 'pt;height:', $this->height, "pt;\" alt=\"\">\n</div>\n";
63                break;
64            default:
65                echo '<img src="', $this->file, '" style="position:absolute;', $renderer->alignRTL, ':', $this->x, 'pt;top:', $this->y, 'pt;width:', $this->width, 'pt;height:', $this->height, "pt;\" alt=\"\">\n";
66        }
67
68        $lastpicpage   = $renderer->pageNo();
69        $lastpicleft   = $this->x;
70        $lastpicright  = $this->x + $this->width;
71        $lastpicbottom = $this->y + $this->height;
72        // Setup for the next line
73        if ($this->line === 'N') {
74            $renderer->setY($lastpicbottom);
75        }
76        // Keep max Y updated
77        $renderer->addMaxY($lastpicbottom);
78    }
79
80    /**
81     * Get the image height
82     * This would be called from the TextBox only for multiple images
83     * so we add a bit bottom space between the images
84     *
85     * @param HtmlRenderer $renderer
86     *
87     * @return float
88     */
89    public function getHeight($renderer): float
90    {
91        return $this->height + $renderer->cPadding * 2;
92    }
93}
94