xref: /webtrees/app/Report/ReportPdfCell.php (revision d11be7027e34e3121be11cc025421873364403f9)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a25f0a04SGreg Roach * (at your option) any later version.
10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a25f0a04SGreg Roach * GNU General Public License for more details.
14a25f0a04SGreg 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/>.
16a25f0a04SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Report;
2176692c8bSGreg Roach
22b6f35a76SGreg Roachuse function hexdec;
23b6f35a76SGreg Roachuse function is_array;
24b6f35a76SGreg Roachuse function preg_match;
25b6f35a76SGreg Roachuse function str_replace;
26b6f35a76SGreg Roach
27a25f0a04SGreg Roach/**
28a25f0a04SGreg Roach * Class ReportPdfCell
29a25f0a04SGreg Roach */
30c1010edaSGreg Roachclass ReportPdfCell extends ReportBaseCell
31c1010edaSGreg Roach{
32a25f0a04SGreg Roach    /**
33a25f0a04SGreg Roach     * PDF Cell renderer
34a25f0a04SGreg Roach     *
35b6f35a76SGreg Roach     * @param PdfRenderer $renderer
36c7ff4153SGreg Roach     *
37c7ff4153SGreg Roach     * @return void
38a25f0a04SGreg Roach     */
3977bab461SGreg Roach    public function render($renderer): void
40c1010edaSGreg Roach    {
41b6f35a76SGreg Roach        $temptext = str_replace('#PAGENUM#', (string) $renderer->tcpdf->PageNo(), $this->text);
42a25f0a04SGreg Roach        // underline «title» part of Source item
43c1010edaSGreg Roach        $temptext = str_replace([
44c1010edaSGreg Roach            '«',
45c1010edaSGreg Roach            '»',
46c1010edaSGreg Roach        ], [
47c1010edaSGreg Roach            '<u>',
48c1010edaSGreg Roach            '</u>',
49c1010edaSGreg Roach        ], $temptext);
5067c69ce5SGreg Roach
5167c69ce5SGreg Roach        // Set up the text style
5267c69ce5SGreg Roach        if ($renderer->getCurrentStyle() !== $this->styleName) {
5367c69ce5SGreg Roach            $renderer->setCurrentStyle($this->styleName);
5467c69ce5SGreg Roach        }
5567c69ce5SGreg Roach
5667c69ce5SGreg Roach        // Background color
5713abd6f3SGreg Roach        $match = [];
58a25f0a04SGreg Roach        // Indicates if the cell background must be painted (1) or transparent (0)
59b6f35a76SGreg Roach        if ($this->fill === 1) {
60a25f0a04SGreg Roach            if (!empty($this->bgcolor)) {
61a25f0a04SGreg Roach                // HTML color to RGB
627a6ee1acSGreg Roach                if (preg_match('/#?(..)(..)(..)/', $this->bgcolor, $match)) {
63a25f0a04SGreg Roach                    $r = hexdec($match[1]);
64a25f0a04SGreg Roach                    $g = hexdec($match[2]);
65a25f0a04SGreg Roach                    $b = hexdec($match[3]);
66b4c5c807SGreg Roach                    $renderer->tcpdf->setFillColor($r, $g, $b);
67a25f0a04SGreg Roach                }
68ce15a17aSGreg Roach            } else {
69ce15a17aSGreg Roach                // If no color set then don't fill
70a25f0a04SGreg Roach                $this->fill = 0;
71a25f0a04SGreg Roach            }
72a25f0a04SGreg Roach        }
7367c69ce5SGreg Roach
7467c69ce5SGreg Roach        // Borders
75a25f0a04SGreg Roach        // HTML color to RGB
767a6ee1acSGreg Roach        if (preg_match('/#?(..)(..)(..)/', $this->bocolor, $match)) {
77a25f0a04SGreg Roach            $r = hexdec($match[1]);
78a25f0a04SGreg Roach            $g = hexdec($match[2]);
79a25f0a04SGreg Roach            $b = hexdec($match[3]);
80b4c5c807SGreg Roach            $renderer->tcpdf->setDrawColor($r, $g, $b);
81a25f0a04SGreg Roach        }
82b6f35a76SGreg Roach
83a25f0a04SGreg Roach        // Paint the text color or they might use inherited colors by the previous function
847a6ee1acSGreg Roach        if (preg_match('/#?(..)(..)(..)/', $this->tcolor, $match)) {
85a25f0a04SGreg Roach            $r = hexdec($match[1]);
86a25f0a04SGreg Roach            $g = hexdec($match[2]);
87a25f0a04SGreg Roach            $b = hexdec($match[3]);
88b4c5c807SGreg Roach            $renderer->tcpdf->setTextColor($r, $g, $b);
89a25f0a04SGreg Roach        } else {
90b4c5c807SGreg Roach            $renderer->tcpdf->setTextColor(0, 0, 0);
91a25f0a04SGreg Roach        }
92a25f0a04SGreg Roach
93a25f0a04SGreg Roach        // If current position (left)
94c21bdddcSGreg Roach        if ($this->left === ReportBaseElement::CURRENT_POSITION) {
95b6f35a76SGreg Roach            $cX = $renderer->tcpdf->GetX();
96ce15a17aSGreg Roach        } else {
97ce15a17aSGreg Roach            // For static position add margin (also updates X)
98a25f0a04SGreg Roach            $cX = $renderer->addMarginX($this->left);
99a25f0a04SGreg Roach        }
100a25f0a04SGreg Roach
101a25f0a04SGreg Roach        // Check the width if set to page wide OR set by xml to larger then page wide
10277bab461SGreg Roach        if ($this->width === 0.0 || $this->width > $renderer->getRemainingWidthPDF()) {
103a25f0a04SGreg Roach            $this->width = $renderer->getRemainingWidthPDF();
104a25f0a04SGreg Roach        }
105a25f0a04SGreg Roach        // For current position
106c21bdddcSGreg Roach        if ($this->top === ReportBaseElement::CURRENT_POSITION) {
107b6f35a76SGreg Roach            $this->top = $renderer->tcpdf->GetY();
108a25f0a04SGreg Roach        } else {
109b4c5c807SGreg Roach            $renderer->tcpdf->setY($this->top);
110a25f0a04SGreg Roach        }
111a25f0a04SGreg Roach
112a25f0a04SGreg Roach        // Check the last cell height and adjust the current cell height if needed
113a25f0a04SGreg Roach        if ($renderer->lastCellHeight > $this->height) {
114a25f0a04SGreg Roach            $this->height = $renderer->lastCellHeight;
115a25f0a04SGreg Roach        }
116a25f0a04SGreg Roach        // Check for pagebreak
117a25f0a04SGreg Roach        if (!empty($temptext)) {
118b6f35a76SGreg Roach            $cHT = $renderer->tcpdf->getNumLines($temptext, $this->width);
119b6f35a76SGreg Roach            $cHT = $cHT * $renderer->tcpdf->getCellHeightRatio() * $renderer->getCurrentStyleHeight();
120b6f35a76SGreg Roach            $cM  = $renderer->tcpdf->getMargins();
121a25f0a04SGreg Roach            // Add padding
122a25f0a04SGreg Roach            if (is_array($cM['cell'])) {
12352135727SGreg Roach                $cHT += $cM['padding_bottom'] + $cM['padding_top'];
124a25f0a04SGreg Roach            } else {
12552135727SGreg Roach                $cHT += $cM['cell'] * 2;
126a25f0a04SGreg Roach            }
127a25f0a04SGreg Roach            // Add a new page if needed
128a25f0a04SGreg Roach            if ($renderer->checkPageBreakPDF($cHT)) {
129b6f35a76SGreg Roach                $this->top = $renderer->tcpdf->GetY();
130a25f0a04SGreg Roach            }
1315183874fSGreg Roach            $temptext = RightToLeftSupport::spanLtrRtl($temptext);
132a25f0a04SGreg Roach        }
133a25f0a04SGreg Roach        // HTML ready - last value is true
134b6f35a76SGreg Roach        $renderer->tcpdf->MultiCell(
135a25f0a04SGreg Roach            $this->width,
136a25f0a04SGreg Roach            $this->height,
137a25f0a04SGreg Roach            $temptext,
138a25f0a04SGreg Roach            $this->border,
139a25f0a04SGreg Roach            $this->align,
140a25f0a04SGreg Roach            $this->fill,
141a25f0a04SGreg Roach            $this->newline,
142a25f0a04SGreg Roach            $cX,
143a25f0a04SGreg Roach            $this->top,
144a25f0a04SGreg Roach            $this->reseth,
145a25f0a04SGreg Roach            $this->stretch,
146a25f0a04SGreg Roach            true
147a25f0a04SGreg Roach        );
148a25f0a04SGreg Roach        // Reset the last cell height for the next line
149a25f0a04SGreg Roach        if ($this->newline >= 1) {
150a25f0a04SGreg Roach            $renderer->lastCellHeight = 0;
151b6f35a76SGreg Roach        } elseif ($renderer->lastCellHeight < $renderer->tcpdf->getLastH()) {
152c6054996SGreg Roach            // OR save the last height if higher then before
153b6f35a76SGreg Roach            $renderer->lastCellHeight = $renderer->tcpdf->getLastH();
154a25f0a04SGreg Roach        }
155a25f0a04SGreg Roach
156a25f0a04SGreg Roach        // Set up the url link if exists ontop of the cell
157a25f0a04SGreg Roach        if (!empty($this->url)) {
158b6f35a76SGreg Roach            $renderer->tcpdf->Link($cX, $this->top, $this->width, $this->height, $this->url);
159a25f0a04SGreg Roach        }
160a25f0a04SGreg Roach        // Reset the border and the text color to black or they will be inherited
161b4c5c807SGreg Roach        $renderer->tcpdf->setDrawColor(0, 0, 0);
162b4c5c807SGreg Roach        $renderer->tcpdf->setTextColor(0, 0, 0);
163a25f0a04SGreg Roach    }
164a25f0a04SGreg Roach}
165